Example #1
0
 /**
  * getInstance
  *
  * @return	Instance
  */
 public static function &getInstance()
 {
     if (!self::$_instance) {
         self::$_instance = new ShorturlHelper();
     }
     return self::$_instance;
 }
 /**
  * process
  *
  * @param   object  &$feed          Params
  * @param   array   &$loadResult    Params
  * @param   bool    $onlyFirstItem  Params
  *
  * @return	array
  */
 public function process(&$feed, &$loadResult, $onlyFirstItem = false)
 {
     $this->setParams($feed->xtform);
     $this->routeHelp = RouteHelp::getInstance();
     $this->_textAdjustmentsInitialization();
     $this->_textCleaningInitialization();
     $this->_htmlCleaningInit();
     FeedTextHelper::hookTagCleaningInit();
     $articles = array();
     $feedTitle = $loadResult->title;
     $items = $loadResult->items;
     $this->_logger->log(JLog::INFO, 'FeedProcessorHelper process: ' . $feedTitle);
     $i = 0;
     if (empty($items)) {
         return array();
     }
     foreach ($items as $item) {
         if ($onlyFirstItem && !empty($articles)) {
             return $articles;
         }
         FeedTextHelper::hookTagCleaningItemInit();
         $article = new FeedContent();
         // Basic Initialization
         $article->cat_id = self::$_params->get('cat_id');
         $article->access = self::$_params->get('access');
         $article->featured = self::$_params->get('front_page');
         $article->language = self::$_params->get('language');
         // Hash
         $hash = $this->getHash($item);
         $article->hash = $hash;
         // Permalink
         $permalink = $item->get_permalink();
         $article->permalink = $permalink;
         // Category Term
         $category_term = $item->get_category();
         $article->category_term = null;
         if (isset($category_term->term)) {
             $article->category_term = $category_term->term;
         }
         // FeedItemBase
         preg_match('#^[a-zA-Z\\d\\-+.]+://[^/]+#', $permalink, $matches);
         $feedItemBase = $matches[0] . '/';
         $article->feedItemBase = $feedItemBase;
         $this->_clean_feed_config['base_url'] = $feedItemBase;
         // NamePrefix
         $namePrefix = $hash . '_';
         $article->namePrefix = $namePrefix;
         // Feed Text
         $theText = $this->_createFeedText($item);
         // Default Intro
         if (empty($theText)) {
             $theText = self::$_params->get('introtext');
         }
         // Feed Title
         $title = $item->get_title();
         $this->_logger->log(JLog::INFO, 'FeedProcessorHelper process: ' . $title . ' - ' . $permalink);
         // Get external fulltext
         if (self::$_params->get('fulltext')) {
             $readability_result = $this->_getFullText($permalink);
             if ($readability_result) {
                 $theText = $readability_result->content;
                 if (self::$_params->get('readability_title')) {
                     $title = $readability_result->title;
                 }
             }
         }
         // Text Cleaning
         $theText = $this->_htmlCleaning($theText);
         // Test for empty content
         if (!self::$_params->get('ignore_empty_intro') && empty($theText)) {
             $this->_logger->log(JLog::INFO, "FeedProcessorHelper process: " . $article->title . ' - Empty intro!');
             continue;
         }
         // Title & alias
         $article->title = $this->_createTitle($title, $feedTitle, $theText, $hash);
         $article->alias = $this->_createAlias($article->title);
         if ($this->_isDuplicated($article)) {
             $this->_logger->log(JLog::INFO, "FeedProcessorHelper isDuplicated: " . $article->title . ' - Duplicated!');
             continue;
         }
         // Black White Listing Control
         $article->blacklisted = false;
         $article->whitelisted = false;
         // Check item filtering
         if (self::$_params->get('filtering')) {
             $alltext = array();
             $alltext[] = $article->title;
             $alltext[] = $theText;
             $alltext[] = $article->category_term;
             $this->_alltext = strtolower(implode(' ', $alltext));
             if (self::$_params->get('filter_category_term')) {
                 if ($article->category_term != self::$_params->get('filter_category_term')) {
                     continue;
                 }
             }
             if (self::$_params->get('filter_blacklist')) {
                 $article->blacklisted = $this->_checkBlackListed();
                 if ($article->blacklisted) {
                     if (self::$_params->get('save_filter_result')) {
                         $this->_logger->log(JLog::INFO, "FeedProcessorHelper process: " . $article->title . ' - Blacklisted!');
                     }
                     continue;
                 }
             }
             if (self::$_params->get('filter_whitelist')) {
                 $article->whitelisted = $this->_checkWhiteListed();
                 if (!$article->whitelisted) {
                     if (self::$_params->get('save_filter_result')) {
                         $this->_logger->log(JLog::INFO, "FeedProcessorHelper process: " . $article->title . ' - Not Whitelisted!');
                     }
                     continue;
                 }
             }
         }
         // Set Creator/Author
         $article->created_by = $this->_getCreatedBy();
         $author = $item->get_author();
         $article->created_by_alias = $this->_getCreatedByAlias($author, $article->created_by, $feedTitle);
         // Process Feed Images
         $article->images = $this->_processImages($theText);
         // Enclosures - (!self::$_params->get('create_art', 1
         if (self::$_params->get('process_enc')) {
             $enclosures = $item->get_enclosures();
             $enclosures = $this->_processEnclosures($enclosures);
             $article->enclosures = $enclosures;
         }
         $article->showEnclosureImage = self::$_params->get('process_enc_images') && count($article->images) == 0 && count($article->enclosures);
         if ($article->showEnclosureImage) {
             $article->images = $this->_setDefaultEnclosureImage($article->enclosures);
             // Ups, no image in enclosures
             if (empty($article->images)) {
                 $article->showEnclosureImage = false;
             }
         }
         // Get Image from Text
         $onlyFirstValid = false;
         $article->showImageFromText = false;
         if (empty($article->images) && self::$_params->get('imagefromtext')) {
             $htmlForImages = $this->_getFullText($permalink, true);
             $this->_logger->log(JLog::INFO, "FeedProcessorHelper imagefromtext: length=" . strlen($htmlForImages), $htmlForImages);
             if ($htmlForImages) {
                 $article->images = $this->_processImages($htmlForImages);
                 $this->_logger->log(JLog::INFO, "FeedProcessorHelper images:", $article->images);
                 $onlyFirstValid = true;
                 $article->showImageFromText = true;
             }
         }
         $article->showDefaultImage = self::$_params->get('img') && count($article->images) == 0;
         // Set Default Image
         if ($article->showDefaultImage) {
             $article->images = $this->_setDefaultImage();
         }
         $this->_validateImages($article->images, $onlyFirstValid);
         if (empty($article->images) && $article->featured && self::$_params->get('featured_with_image')) {
             $article->featured = 0;
         }
         if (!empty($article->images) && !$article->featured && self::$_params->get('featured_with_image')) {
             $article->featured = 1;
         }
         $article->introtext = $this->_trimText($theText, self::$_params->get('trim_to'), self::$_params->get('trim_type'));
         list($article->introtext, $article->fulltext) = $this->_onlyIntro($article->introtext, $theText);
         $article->introtext = $this->_dotDotDot($article->introtext);
         // Shortlink (or not)
         $article->shortlink = $article->permalink;
         if (!empty($permalink) && $this->_shorturl_always && self::$_params->get('shortlink')) {
             $article->shortlink = ShorturlHelper::getInstance()->getShortUrl($article->permalink);
         }
         // Category
         if ($category = $item->get_category()) {
             $article->metakey .= $category->get_label();
         }
         // Publication state and dates
         $this->_setPublicationState($article, $item->get_date());
         $articles[] = $article;
         $i++;
         // End Item Processing
     }
     return $articles;
 }
Example #3
0
 /**
  * sharePost
  *
  * @param   object  &$channel  Param
  * @param   object  &$post     Param
  * @param   int     $userid    Param
  *
  * @return	array
  */
 protected function sharePost(&$channel, &$post, $userid = null)
 {
     // Check for duplicate post
     if ($this->dpcheck_enabled) {
         $isDuplicated = PostHelper::isDuplicatedPost($post->id, $post->ref_id, $post->plugin, $post->channel_id, $post->message, $this->dpcheck_time_intval);
         if ($isDuplicated) {
             $this->logger->log(JLog::INFO, 'sendPost: duplicate post detection - message is already posted, article id = ' . $post->ref_id . ', plugin = ' . $post->plugin . ', interval = ' . $this->dpcheck_time_intval);
             return array('state' => AutotweetPostHelper::POST_ERROR, 'result_msg' => 'COM_AUTOTWEET_ERROR_DUPLICATED');
         }
     }
     // Check for banned post
     if ($this->bannedwordscheck_enabled) {
         $isBanned = PostHelper::isBannedPost($post->message, $this->banned_words);
         if ($isBanned) {
             $this->logger->log(JLog::INFO, 'sendPost: banned post detection - message has banned words, article id = ' . $post->ref_id . ', plugin = ' . $post->plugin);
             return array('state' => AutotweetPostHelper::POST_ERROR, 'result_msg' => 'COM_AUTOTWEET_ERROR_BANNED');
         }
     }
     // Get short url one time and if needed only (better performance)
     if (AutotweetPostHelper::SHOWURL_OFF != $post->show_url && !empty($post->org_url) && !array_key_exists($post->org_url, $this->current_short_url)) {
         $shorturlHelper = ShorturlHelper::getInstance();
         $this->current_short_url[$post->org_url] = $shorturlHelper->getShortUrl($post->org_url);
     }
     $current_short_url = null;
     if (array_key_exists($post->org_url, $this->current_short_url)) {
         $current_short_url = $this->current_short_url[$post->org_url];
     }
     // Construct url and truncate message, if necessary
     $finalUrlMessage = TextUtil::getMessageWithUrl($channel, $post, $current_short_url, $this->shorturl_always);
     // Switch original url to short url to use short url also for all other links
     $post->url = $finalUrlMessage['url'];
     $message = $finalUrlMessage['message'];
     // Just in case we want to repeat the message
     // $post->message = $message;
     return $this->sharePostChannel($message, $channel, $post);
 }