コード例 #1
0
 /**
  * Detects URLs in the text and converts them into shortcodes indicating either regular links or images.
  *
  * @param string $text HTML-encoded string
  * @param boolean $detectAndDownloadImages Whether to check and download images
  * @param boolean $detectYouTubeVideos
  *
  * @return string
  */
 public function filter($text, $detectAndDownloadImages, $detectYouTubeVideos = false)
 {
     $this->replacementOffset = 0;
     $this->createdAttachments = array();
     if (preg_match_all(self::URL_REGEXP, $text, $matches)) {
         if (count($matches) == 0) {
             return $text;
         }
         foreach ($matches[0] as $detectedURL) {
             $shortCode = null;
             $regularLink = false;
             $ytMatches = array();
             if ($detectAndDownloadImages && preg_match(self::URL_IMAGE_REGEXP, $detectedURL)) {
                 $imageUrl = $detectedURL;
                 if (!preg_match(self::URL_PROTOCOLS_REGEXP, $detectedURL)) {
                     $imageUrl = "http://" . $detectedURL;
                 }
                 try {
                     $result = $this->imagesService->downloadImage($imageUrl);
                     $this->createdAttachments[] = $result['id'];
                     $shortCode = WiseChatShortcodeConstructor::getImageShortcode($result['id'], $result['image'], $result['image-th'], $detectedURL);
                 } catch (Exception $ex) {
                     $regularLink = true;
                     $actions = WiseChatContainer::get('services/user/WiseChatActions');
                     $authentication = WiseChatContainer::get('services/user/WiseChatAuthentication');
                     $actions->publishAction('showErrorMessage', array('message' => $ex->getMessage()), $authentication->getUser());
                 }
             } elseif ($detectYouTubeVideos && preg_match(self::URL_YOUTUBE_REGEXP, $detectedURL, $ytMatches)) {
                 $movieId = array_pop($ytMatches);
                 $shortCode = WiseChatShortcodeConstructor::getYouTubeShortcode($movieId, $detectedURL);
             } elseif ($detectYouTubeVideos && preg_match(self::URL_YOUTUBE_REGEXP_2, $detectedURL, $ytMatches)) {
                 $movieId = array_pop($ytMatches);
                 $shortCode = WiseChatShortcodeConstructor::getYouTubeShortcode($movieId, $detectedURL);
             } else {
                 $regularLink = true;
             }
             if ($regularLink) {
                 $shortCode = sprintf('[link src="%s"]', $detectedURL);
             }
             if ($shortCode !== null) {
                 $text = $this->strReplaceFirst($detectedURL, $shortCode, $text);
             }
         }
     }
     return $text;
 }