コード例 #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;
 }
コード例 #2
0
 /**
  * Saves attachments in the Media Library and attaches them to the end of the message.
  *
  * @param WiseChatChannel $channel
  * @param array $attachments Array of attachments
  *
  * @return array Array consisting of the two elements: a shortcode representing the attachments and array of IDs of created attachments
  */
 private function saveAttachments($channel, $attachments)
 {
     if (!is_array($attachments) || count($attachments) === 0) {
         return array(null, array());
     }
     WiseChatContainer::load('rendering/filters/WiseChatShortcodeConstructor');
     $firstAttachment = $attachments[0];
     $data = $firstAttachment['data'];
     $data = substr($data, strpos($data, ",") + 1);
     $decodedData = base64_decode($data);
     $attachmentShortcode = null;
     $attachmentIds = array();
     if ($this->options->isOptionEnabled('enable_images_uploader') && $firstAttachment['type'] === 'image') {
         $image = $this->imagesService->saveImage($decodedData);
         if (is_array($image)) {
             $attachmentShortcode = ' ' . WiseChatShortcodeConstructor::getImageShortcode($image['id'], $image['image'], $image['image-th'], '_');
             $attachmentIds = array($image['id']);
         }
     }
     if ($this->options->isOptionEnabled('enable_attachments_uploader') && $firstAttachment['type'] === 'file') {
         $fileName = $firstAttachment['name'];
         $file = $this->attachmentsService->saveAttachment($fileName, $decodedData, $channel->getName());
         if (is_array($file)) {
             $attachmentShortcode = ' ' . WiseChatShortcodeConstructor::getAttachmentShortcode($file['id'], $file['file'], $fileName);
             $attachmentIds = array($file['id']);
         }
     }
     return array($attachmentShortcode, $attachmentIds);
 }