Exemple #1
0
 /**
  * sendMessage
  *
  * @param   string  $message  Param
  * @param   array   $data     Param
  *
  * @return	bool
  */
 public function sendMessage($message, $data)
 {
     $imagefile = null;
     try {
         $image_url = $data->image_url;
         $media_mode = $this->getMediaMode();
         if ($media_mode != 'message' && !empty($image_url)) {
             $imagefile = ImageUtil::getInstance()->downloadImage($image_url);
             if ($imagefile) {
                 $result = $this->_sendTwitterMessageWithImage($message, $imagefile);
             } else {
                 $result = $this->_sendTwitterMessage($message, null);
             }
         } else {
             $result = $this->_sendTwitterMessage($message, $image_url);
         }
     } catch (Exception $e) {
         $result = array(false, $e->getMessage());
     }
     if ($imagefile) {
         ImageUtil::getInstance()->releaseImage($imagefile);
     }
     return $result;
 }
 /**
  * download
  *
  * @param   array  $params  Params
  *
  * @return	bool
  */
 public function download($params)
 {
     $logger = AutotweetLogger::getInstance();
     $rel_src = $params->get('rel_src');
     $img_folder = $params->get('img_folder');
     $sub_folder = $params->get('sub_folder');
     $img_name_type = $params->get('img_name_type');
     $imageHelper = ImageUtil::getInstance();
     $filename = $imageHelper->downloadImage($this->src);
     if (!$filename || !file_exists($filename)) {
         $logger->log(JLog::ERROR, 'download: failed ' . $this->src);
         return false;
     }
     // Main folder
     $path = JPATH_ROOT . DIRECTORY_SEPARATOR . $img_folder;
     // Sub folder
     $path_subfolder = $this->_getSubfolder($path, $sub_folder);
     if (!JFolder::exists($path_subfolder)) {
         $result = JFolder::create($path_subfolder);
         if (!$result) {
             $imageHelper->releaseImage($filename);
             $logger->log(JLog::ERROR, 'download: JFolder::create subfolder ' . $path_subfolder);
             return false;
         }
     }
     $img_filename = $this->_getImgFilename($filename, $img_name_type);
     $final_filename = $path_subfolder . DIRECTORY_SEPARATOR . $img_filename;
     $result = JFile::move($filename, $final_filename);
     if (!$result) {
         $imageHelper->releaseImage($filename);
         $logger->log(JLog::ERROR, 'download: JFile::move ' . $filename . ' - ' . $final_filename);
         return false;
     }
     $imgurl = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR, '', $final_filename);
     $this->original_src = $this->src;
     if ($rel_src) {
         $this->src = $imgurl;
     } else {
         $this->src = RouteHelp::getInstance()->getRoot() . $imgurl;
     }
     return true;
 }
 /**
  * _validateImages
  *
  * @param   array  &$images         Params
  * @param   bool   $onlyFirstValid  Params
  *
  * @return	bool
  */
 private function _validateImages(&$images, $onlyFirstValid = false)
 {
     $imgs = array();
     $imageHelper = ImageUtil::getInstance();
     foreach ($images as $image) {
         $imageUrl = $image->src;
         if ($imageHelper->isValidImageUrl($imageUrl)) {
             $imgs[] = $image;
             if ($onlyFirstValid) {
                 break;
             }
         }
     }
     $images = $imgs;
 }
Exemple #4
0
 /**
  * publishRequest
  *
  * @param   array   $request  Param
  * @param   object  $userid   Param
  *
  * @return	boolean
  */
 public function publishRequest($request, $userid = null)
 {
     $this->logger->log(JLog::INFO, 'publishRequest request', $request);
     // Default: generate new entry for repost of entry with state success
     $postid = 0;
     $data = $this->_getContentData($request);
     if (!$data) {
         return false;
     }
     // Convert array to object
     $json = json_encode($data);
     $post = json_decode($json);
     if (AUTOTWEETNG_JOOCIAL) {
         $params = AdvancedattrsHelper::getAdvancedAttrByReq($request->id);
     }
     $post->ref_id = $request->ref_id;
     $post->plugin = $request->plugin;
     $post->postdate = $request->publish_up;
     $post->message = $post->text;
     unset($post->text);
     // Url
     if (isset($post->url) && !empty($post->url)) {
         $routeHelp = RouteHelp::getInstance();
         $url = $routeHelp->getAbsoluteUrl($post->url);
         $post->url = $url;
     } elseif (isset($request->url) && !empty($request->url)) {
         $post->url = $request->url;
     } else {
         $this->logger->log(JLog::INFO, 'publishRequest: No url');
         $post->url = '';
     }
     $this->logger->log(JLog::INFO, 'publishRequest: url = ' . $post->url);
     // Image url
     if (isset($post->image_url) && !empty($post->image_url)) {
         // If defined in getExtendedData, use this image_url
         $routeHelp = RouteHelp::getInstance();
         $url = $routeHelp->getAbsoluteUrl($post->image_url);
         // Only if it's a valid Url
         if (!ImageUtil::getInstance()->isValidImageUrl($url)) {
             $url = null;
         }
         $post->image_url = $url;
     } elseif (isset($request->image_url) && !empty($request->image_url)) {
         $url = $request->image_url;
         // Only if it's a valid Url
         if (!ImageUtil::getInstance()->isValidImageUrl($url)) {
             $url = null;
         }
         // Use this image_url (it's already routed)
         $post->image_url = $url;
     } else {
         $this->logger->log(JLog::INFO, 'publishRequest: No image url');
         $post->image_url = null;
     }
     $this->logger->log(JLog::INFO, 'publishRequest: image url = ' . $post->image_url);
     // Title
     // Truncate title and fulltext for new messages ('if' is for backward compatibillity)
     if (isset($post->title)) {
         $title = TextUtil::cleanText($post->title);
         $post->title = TextUtil::truncString($title, self::MAX_CHARS_TITLE);
     } else {
         $post->title = '';
     }
     // Fulltext
     if (!isset($post->fulltext)) {
         $post->fulltext = '';
     }
     if (AUTOTWEETNG_JOOCIAL) {
         if (isset($params->fulltext) && !empty($params->fulltext)) {
             $post->fulltext = $params->fulltext;
         }
     }
     if (AUTOTWEETNG_JOOCIAL && EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_mode') && ($fulltext = EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_titleonly'))) {
         $post->fulltext = $fulltext;
     }
     $fulltext = TextUtil::cleanText($post->fulltext);
     $post->fulltext = TextUtil::truncString($fulltext, self::MAX_CHARS_FULLTEXT);
     $post->xtform = new JRegistry();
     // Catids
     if (isset($post->catids)) {
         $catids = $post->catids;
         unset($post->catids);
         $post->xtform->set('catids', $catids);
     } else {
         $post->xtform->set('catids', array());
     }
     // Author
     if (isset($post->author)) {
         $author = $post->author;
         unset($post->author);
         $post->xtform->set('author', $author);
     }
     // Language
     if (isset($post->language)) {
         $language = $post->language;
         unset($post->language);
         $post->xtform->set('language', $language);
     }
     // Access
     if (isset($post->access)) {
         $access = $post->access;
         unset($post->access);
         $post->xtform->set('access', $access);
     }
     // Target_id
     if (isset($post->target_id)) {
         $target_id = $post->target_id;
         unset($post->target_id);
         $post->xtform->set('target_id', $target_id);
     }
     // Hashtags
     if (isset($post->hashtags)) {
         $hashtags = $post->hashtags;
         unset($post->hashtags);
         $post->xtform->set('hashtags', $hashtags);
     }
     // Native object
     if (isset($request->native_object)) {
         $native_object = $request->native_object;
         $native_object = json_decode($native_object);
         if ($native_object) {
             $post->xtform->set('native_object', $native_object);
         } else {
             $this->logger->log(JLog::INFO, 'publishRequest: Inavlid JSON native_object');
         }
     }
     // Featured
     if (isset($post->featured)) {
         $post->xtform->set('featured', $post->featured);
         unset($post->featured);
     }
     return $this->sendRequest($request, $post, $userid);
 }
Exemple #5
0
 /**
  * uploadImage
  *
  * @param   string  $image_url  Param
  *
  * @return	object
  */
 public function uploadImage($image_url)
 {
     if (empty($image_url)) {
         return false;
     }
     $imagefile = ImageUtil::getInstance()->downloadImage($image_url);
     if (!$imagefile) {
         return false;
     }
     $status = false;
     try {
         $uid = $this->getUserId();
         $gid = $this->channel->params->get('vkgroup_id');
         $vk = $this->getApiInstance();
         if ($gid) {
             $parameters = array('gid' => $gid);
         } else {
             $parameters = array('uid' => $uid);
         }
         $result = $vk->api('photos.getWallUploadServer', $parameters);
         if (array_key_exists('response', $result)) {
             $response = $result['response'];
             if (array_key_exists('upload_url', $response)) {
                 $upload_url = $response['upload_url'];
                 $ch = curl_init();
                 $data = array('photo' => '@' . $imagefile);
                 curl_setopt($ch, CURLOPT_URL, $upload_url);
                 curl_setopt($ch, CURLOPT_POST, 1);
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 $status = curl_exec($ch);
                 if (!$status && curl_errno($ch)) {
                     $logger = AutotweetLogger::getInstance();
                     $logger->log(JLog::ERROR, curl_error($ch));
                 } else {
                     $status = json_decode($status);
                     $photo = json_decode($status->photo);
                     $parameters = array('server' => $status->server, 'photo' => $status->photo, 'hash' => $status->hash);
                     if ($gid) {
                         $parameters['gid'] = $gid;
                     } else {
                         $parameters['uid'] = $uid;
                     }
                     $status = $vk->api('photos.saveWallPhoto', $parameters);
                     if (array_key_exists('response', $status) && is_array($status['response']) && count($status['response']) == 1) {
                         $status = $status['response'][0];
                     }
                 }
                 curl_close($ch);
             }
         }
         if (array_key_exists('error', $result)) {
             $message = 'Error (' . $result['error']['error_code'] . ' ' . $result['error']['error_msg'] . ')';
             $logger = AutotweetLogger::getInstance();
             $logger->log(JLog::ERROR, $message);
         }
     } catch (Exception $e) {
         $logger = AutotweetLogger::getInstance();
         $logger->log(JLog::ERROR, $e->getMessage());
     }
     ImageUtil::getInstance()->releaseImage($imagefile);
     return $status;
 }