function generate_content(&$title)
 {
     global $serendipity;
     $number = $this->get_config('number');
     $displaydate = $this->get_config('displaydate', 'true');
     $dateformat = $this->get_config('dateformat');
     $sidebartitle = $title = $this->get_config('sidebartitle', $this->title);
     $rssuri = $this->get_config('rssuri');
     $target = $this->get_config('target');
     $cachetime = $this->get_config('cachetime');
     $feedtype = $this->get_config('feedtype', 'rss');
     $markup = $this->get_config('markup', 'false');
     $bulletimg = $this->get_config('bulletimg');
     $charset = $this->get_config('charset', 'native');
     if (!$number || !is_numeric($number) || $number < 1) {
         $showAll = true;
     } else {
         $showAll = false;
     }
     if (!$dateformat || strlen($dateformat) < 1) {
         $dateformat = '%A, %B %e. %Y';
     }
     if (!$cachetime || !is_numeric($cachetime)) {
         $cachetime = 10800;
         // 3 hours in seconds
     }
     $smarty = serendipity_db_bool($this->get_config('smarty'));
     if ($this->get_config('template') != 'plugin_remoterss.tpl') {
         $smarty = true;
     }
     if (trim($rssuri)) {
         $feedcache = $serendipity['serendipityPath'] . 'templates_c/remoterss_cache_' . md5(preg_replace('@[^a-z0-9]*@i', '', $rssuri) . $this->get_config('template')) . '.dat';
         if (!file_exists($feedcache) || filesize($feedcache) == 0 || filemtime($feedcache) < time() - $cachetime) {
             $this->debug('Cachefile does not existing.');
             if (!$this->urlcheck($rssuri)) {
                 $this->debug('URLCheck failed');
                 echo '<!-- No valid URL! -->';
             } elseif ($feedtype == 'rss') {
                 $this->debug('URLCheck succeeded. Touching ' . $feedcache);
                 // Touching the feedcache file will prevent loops of death when the RSS target is the same URI than our blog.
                 @touch($feedcache);
                 require_once S9Y_PEAR_PATH . 'Onyx/RSS.php';
                 $c = new Onyx_RSS($charset);
                 $this->debug('Running Onyx Parser');
                 $c->parse($rssuri);
                 $this->encoding = $c->rss['encoding'];
                 $use_rss_link = serendipity_db_bool($this->get_config('use_rss_link'));
                 $rss_elements = explode(',', $this->get_config('show_rss_element'));
                 $escape_rss = serendipity_db_bool($this->get_config('escape_rss'));
                 $i = 0;
                 $content = '';
                 $smarty_items = array();
                 while (($showAll || $i < $number) && ($item = $c->getNextItem())) {
                     if (empty($item['title'])) {
                         continue;
                     }
                     $content .= '<div class="rss_item">';
                     if ($use_rss_link) {
                         $content .= '<div class="rss_link"><a href="' . serendipity_specialchars($this->decode($item['link'])) . '" ' . (!empty($target) ? 'target="' . $target . '"' : '') . '>';
                     }
                     if (!empty($bulletimg)) {
                         $content .= '<img src="' . $bulletimg . '" border="0" alt="*" /> ';
                     }
                     $is_first = true;
                     foreach ($rss_elements as $rss_element) {
                         $rss_element = trim($rss_element);
                         if (!$is_first) {
                             $content .= '<span class="rss_' . preg_replace('@[^a-z0-9]@imsU', '', $rss_element) . '">';
                         }
                         if ($escape_rss) {
                             $content .= $this->decode($item[$rss_element]);
                         } else {
                             $content .= serendipity_specialchars($this->decode($item[$rss_element]));
                         }
                         if ($smarty) {
                             $item['display_elements'][preg_replace('@[^a-z0-9]@imsU', '', $rss_element)] = $this->decode($item[$rss_element]);
                         }
                         if (!$is_first) {
                             $content .= '</span>';
                         }
                         if ($is_first && $use_rss_link) {
                             $content .= '</a></div>';
                             // end of first linked element
                         }
                         $is_first = false;
                     }
                     if ($is_first && $use_rss_link) {
                         // No XML element has been configured.
                         $content .= '</a></div>';
                     }
                     $content .= "<br />\n";
                     $item['timestamp'] = @strtotime(isset($item['pubdate']) ? $item['pubdate'] : $item['dc:date']);
                     if (!($item['timestamp'] == -1) and $displaydate == 'true') {
                         $content .= '<div class="serendipitySideBarDate">' . serendipity_specialchars(serendipity_formatTime($dateformat, $item['timestamp'], false)) . '</div>';
                     }
                     if ($smarty) {
                         $smarty_items['items'][$i] = $item;
                         $smarty_items['items'][$i]['css_class'] = preg_replace('@[^a-z0-9]@imsU', '', $rss_element);
                         foreach ($item as $key => $val) {
                             $smarty_items['items'][$i]['decoded_' . str_replace(':', '_', $key)] = $this->decode($key);
                         }
                     }
                     $content .= '</div>';
                     // end of rss_item
                     ++$i;
                 }
                 if ($smarty) {
                     $smarty_items['use_rss_link'] = $use_rss_link;
                     $smarty_items['bulletimg'] = $bulletimg;
                     $smarty_items['escape_rss'] = $escape_rss;
                     $smarty_items['displaydate'] = $displaydate;
                     $smarty_items['dateformat'] = $dateformat;
                     $smarty_items['target'] = $target;
                     $serendipity['smarty']->assign_by_ref('remoterss_items', $smarty_items);
                     $tpl = $this->get_config('template');
                     if (empty($tpl)) {
                         $tpl = 'plugin_remoterss.tpl';
                     }
                     // Template specifics go here
                     switch ($tpl) {
                         case 'plugin_remoterss_nasaiotd.tpl':
                             $smarty_items['nasa_image'] = $c->getData('image');
                             break;
                     }
                     $content = $this->parseTemplate($tpl);
                 }
                 $this->debug('Caching Feed (' . strlen($content) . ' bytes)');
                 $fp = @fopen($feedcache, 'w');
                 if (trim($content) != '' && $fp) {
                     fwrite($fp, $content);
                     fclose($fp);
                     $this->debug('Feed cache written');
                 } else {
                     $this->debug('Could not write (empty?) cache.');
                     echo '<!-- Cache failed to ' . $feedcache . ' in ' . getcwd() . ' --><br />';
                     if (trim($content) == '') {
                         $this->debug('Getting old feedcache');
                         $content = @file_get_contents($feedcache);
                     }
                 }
                 $this->debug('RSS Plugin finished.');
             } elseif ($feedtype == 'atom') {
                 $this->debug('URLCheck succeeded. Touching ' . $feedcache);
                 // Touching the feedcache file will prevent loops of death when the RSS target is the same URI than our blog.
                 @touch($feedcache);
                 require_once S9Y_PEAR_PATH . '/simplepie/simplepie.inc';
                 $this->debug('Running simplepie Parser');
                 $simplefeed = new SimplePie();
                 $simplefeed->cache = false;
                 $simplefeed->set_feed_url($rssuri);
                 $success = $simplefeed->init();
                 $simplefeed->set_output_encoding($charset);
                 $simplefeed->handle_content_type();
                 $this->encoding = $charset;
                 $use_rss_link = serendipity_db_bool($this->get_config('use_rss_link'));
                 $rss_elements = explode(',', $this->get_config('show_rss_element'));
                 $escape_rss = serendipity_db_bool($this->get_config('escape_rss'));
                 $i = 0;
                 $content = '';
                 $smarty_items = array();
                 foreach ($simplefeed->get_items() as $simpleitem) {
                     // map SimplePie items to s9y items
                     $item['title'] = $simpleitem->get_title();
                     $item['link'] = $simpleitem->get_permalink();
                     $item['pubdate'] = $simpleitem->get_date('U');
                     $item['date'] = $simpleitem->get_date('U');
                     $item['description'] = $simpleitem->get_description();
                     $item['content'] = $simpleitem->get_content();
                     $item['author'] = $simpleitem->get_author();
                     if (!$showAll && $i > $number) {
                         break;
                     }
                     if (empty($item['title'])) {
                         continue;
                     }
                     $content .= '<div class="rss_item">';
                     if ($use_rss_link) {
                         $content .= '<div class="rss_link"><a href="' . serendipity_specialchars($this->decode($item['link'])) . '" ' . (!empty($target) ? 'target="' . $target . '"' : '') . '>';
                     }
                     if (!empty($bulletimg)) {
                         $content .= '<img src="' . $bulletimg . '" border="0" alt="*" /> ';
                     }
                     $is_first = true;
                     foreach ($rss_elements as $rss_element) {
                         $rss_element = trim($rss_element);
                         if (!$is_first) {
                             $content .= '<span class="rss_' . preg_replace('@[^a-z0-9]@imsU', '', $rss_element) . '">';
                         }
                         if ($escape_rss) {
                             $content .= $this->decode($item[$rss_element]);
                         } else {
                             $content .= serendipity_specialchars($this->decode($item[$rss_element]));
                         }
                         if ($smarty) {
                             $item['display_elements'][preg_replace('@[^a-z0-9]@imsU', '', $rss_element)] = $this->decode($item[$rss_element]);
                         }
                         if (!$is_first) {
                             $content .= '</span>';
                         }
                         if ($is_first && $use_rss_link) {
                             $content .= '</a></div>';
                             // end of first linked element
                         }
                         $is_first = false;
                     }
                     if ($is_first && $use_rss_link) {
                         // No XML element has been configured.
                         $content .= '</a></div>';
                     }
                     $content .= "<br />\n";
                     $item['timestamp'] = @strtotime(isset($item['pubdate']) ? $item['pubdate'] : $item['dc:date']);
                     if (!($item['timestamp'] == -1) and $displaydate == 'true') {
                         $content .= '<div class="serendipitySideBarDate">' . serendipity_specialchars(serendipity_formatTime($dateformat, $item['timestamp'], false)) . '</div>';
                     }
                     if ($smarty) {
                         $smarty_items['items'][$i] = $item;
                         $smarty_items['items'][$i]['css_class'] = preg_replace('@[^a-z0-9]@imsU', '', $rss_element);
                         foreach ($item as $key => $val) {
                             $smarty_items['items'][$i]['decoded_' . str_replace(':', '_', $key)] = $this->decode($key);
                         }
                     }
                     $content .= '</div>';
                     // end of rss_item
                     ++$i;
                 }
                 if ($smarty) {
                     $smarty_items['use_rss_link'] = $use_rss_link;
                     $smarty_items['bulletimg'] = $bulletimg;
                     $smarty_items['escape_rss'] = $escape_rss;
                     $smarty_items['displaydate'] = $displaydate;
                     $smarty_items['dateformat'] = $dateformat;
                     $smarty_items['target'] = $target;
                     $serendipity['smarty']->assign_by_ref('remoterss_items', $smarty_items);
                     $tpl = $this->get_config('template');
                     if (empty($tpl)) {
                         $tpl = 'plugin_remoterss.tpl';
                     }
                     // Template specifics go here
                     switch ($tpl) {
                         case 'plugin_remoterss_nasaiotd.tpl':
                             $smarty_items['nasa_image'] = $c->getData('image');
                             break;
                     }
                     $content = $this->parseTemplate($tpl);
                 }
                 $this->debug('Caching Feed (' . strlen($content) . ' bytes)');
                 $fp = @fopen($feedcache, 'w');
                 if (trim($content) != '' && $fp) {
                     fwrite($fp, $content);
                     fclose($fp);
                     $this->debug('Feed cache written');
                 } else {
                     $this->debug('Could not write (empty?) cache.');
                     echo '<!-- Cache failed to ' . $feedcache . ' in ' . getcwd() . ' --><br />';
                     if (trim($content) == '') {
                         $this->debug('Getting old feedcache');
                         $content = @file_get_contents($feedcache);
                     }
                 }
                 $this->debug('RSS Plugin (Atom) finished.');
             } elseif ($feedtype == 'opml') {
                 // Touching the feedcache file will prevent loops of death when the RSS target is the same URI than our blog.
                 @touch($feedcache);
                 $opml = new s9y_remoterss_OPML();
                 $opmltree = $opml->parseOPML($rssuri);
                 if (OPMLDEBUG == 1) {
                     echo "\n<pre>\n";
                     print_r($opmltree);
                     echo "\n</pre>\n";
                 }
                 if ($opmltree['tag'] === 'opml') {
                     $head = $opml->getOPMLHead($opmltree);
                     $ownerName = $opml->getOPMLTag($head, 'ownerName');
                     $blogrolling = $ownerName != false ? $ownerName['value'] == 'Blogroll Owner' ? true : false : false;
                     $i = 0;
                     $content = '';
                     while (($showAll || $i < $number) && ($item = $opml->getOPMLOutlineAttr($opmltree, $i))) {
                         if (!empty($item['url'])) {
                             $url = $this->decode($item['url']);
                         } elseif (!empty($item['htmlUrl'])) {
                             $url = $this->decode($item['htmlUrl']);
                         } elseif (!empty($item['xmlUrl'])) {
                             $url = $this->decode($item['xmlUrl']);
                         } elseif (!empty($item['urlHTTP'])) {
                             $url = $this->decode($item['urlHTTP']);
                         } else {
                             $url = '';
                         }
                         if (!empty($item['text'])) {
                             $text = serendipity_specialchars($this->decode($item['text']));
                         } elseif (!empty($item['title'])) {
                             $text = serendipity_specialchars($this->decode($item['title']));
                         } elseif (!empty($item['description'])) {
                             $text = serendipity_specialchars($this->decode($item['description']));
                         } else {
                             $text = '';
                         }
                         if ($blogrolling === true && (!empty($text) || !empty($url))) {
                             $content .= '&bull; <a href="' . serendipity_specialchars($url) . '" ' . (!empty($target) ? 'target="' . $target . '"' : '') . ' title="' . $text . '">' . $text . "</a>";
                             if (isset($item['isRecent'])) {
                                 $content .= ' <span style="color: Red; ">*</span>';
                             }
                             $content .= "<br />";
                         } elseif (isset($item['type']) && $item['type'] == 'url' || !empty($url)) {
                             $content .= '&bull; <a href="' . serendipity_specialchars($url) . '" ' . (!empty($target) ? 'target="' . $target . '"' : '') . ' title="' . $text . '">' . $text . "</a>";
                             $content .= "<br />";
                         }
                         ++$i;
                     }
                     /* Pretend to be a html_nugget so we can apply markup events. */
                     if ($markup == 'true') {
                         $entry = array('html_nugget' => $content);
                         serendipity_plugin_api::hook_event('frontend_display', $entry);
                         $content = $entry['html_nugget'];
                     }
                     $fp = @fopen($feedcache, 'w');
                     if (trim($content) != '' && $fp) {
                         fwrite($fp, $content);
                         fclose($fp);
                     } else {
                         echo '<!-- Cache failed to ' . $feedcache . ' in ' . getcwd() . ' --><br />';
                         if (trim($content) == '') {
                             $content = @file_get_contents($feedcache);
                         }
                     }
                 } else {
                     echo '<!-- Not a valid OPML feed -->';
                 }
             } else {
                 echo '<!-- no valid feedtype -->';
             }
         } else {
             $this->debug('Got feed from cache ' . $feedcache);
             $content = file_get_contents($feedcache);
         }
         echo $content;
     } else {
         echo PLUGIN_REMOTERSS_NOURI;
     }
 }
     return true;
 }
 function generate_content(&$title)
 {
     global $serendipity;
     $title = $this->get_config('sidebarTitle');
     $deliciousID = $this->get_config('deliciousID');
     $moreLink = $this->get_config('moreLink');
     if (empty($deliciousID)) {
         return false;
     }
     if ($this->get_config('displayNumber') < 30 && $this->get_config('displayNumber') >= 1) {
         $displayNumber = $this->get_config('displayNumber');
     } else {
         $displayNumber = 30;
     }
     if ($this->get_config('cacheTime') > 0) {
         $cacheTime = $this->get_config('cacheTime') * 3600;
     } else {
         $cacheTime = 3600 + 1;
     }
     $gDeliciousURL = 'http://del.icio.us/';
     $gDeliciousCacheLoc = $serendipity['serendipityPath'] . '/templates_c/delicious_';
     // safe write location... need to have local abilit
     $parsedCache = $gDeliciousCacheLoc . md5($deliciousID) . '.cache';
     if (!is_file($parsedCache) || mktime() - filectime($parsedCache) > $cacheTime) {
         if (!is_dir($gDeliciousCacheLoc) && !mkdir($gDeliciousCacheLoc, 0775)) {
             print 'Try to chmod go+rwx - permissions are wrong.';
         }
         require_once 'Onyx/RSS.php';
         $deliciousFeed = new Onyx_RSS();
         //$deliciousFeed->setCachePath($gDeliciousCacheLoc);
         //$deliciousFeed->setExpiryTime($cacheTime);
         //$deliciousFeed->parse($gDeliciousURL .'rss/' . $deliciousID, md5($deliciousID) . '.dat');
         $deliciousFeed->parse($gDeliciousURL . 'rss/' . $deliciousID);
         if ($deliciousFeed->numItems() >= 1) {
             $fileHandle = @fopen($parsedCache, 'w');
             if ($fileHandle) {
                 $deliciousContent = '<ul class="plainList">';
                 for ($i = 0; ($item = $deliciousFeed->getNextItem()) && $i < $displayNumber; $i++) {
                     $deliciousContent .= '<li>' . '<a href="' . delicious_clean_htmlspecialchars($item['link']) . '" title="' . $item['description'] . '" rel="external">' . delicious_clean_htmlspecialchars($item['title']) . '</a></li>';
                 }
                 $deliciousContent .= '</ul>';
                 fwrite($fileHandle, $deliciousContent);
                 fclose($fileHandle);
                 print $deliciousContent;
             } else {
                 print '<p>A del.icio.us error occured! <br />' . 'Error Message: unable to make a delicious cache file: ' . $parsedCache . '!</p>';
             }
         } elseif (is_file($parsedCache)) {
             print file_get_contents($parsedCache);
         } else {
             print '<p>A del.icio.us error occured! <br />' . 'Error Message: rss failed</p>';
         }
     } else {
         print file_get_contents($parsedCache);
     }
     if (serendipity_db_bool($moreLink)) {
         print '<p><a href="' . $gDeliciousURL . $deliciousID . '/">' . $this->get_config('morelink_text') . '</a></p>';
 function fetchFeeds($opt = null)
 {
     global $serendipity;
     set_time_limit(360);
     ignore_user_abort(true);
     $_SESSION['serendipityRightPublish'] = true;
     $serendipity['noautodiscovery'] = true;
     $this->setupDB();
     $feeds = $this->getFeeds($opt);
     $engine = $this->get_config('engine', 'onyx');
     if ($engine == 'onyx') {
         require_once (defined('S9Y_PEAR_PATH') ? S9Y_PEAR_PATH : S9Y_INCLUDE_PATH . 'bundled-libs/') . 'Onyx/RSS.php';
     } elseif ($engine == 'magpierss') {
         // CLSC: NEW "MagpieRSS" include
         require_once dirname(__FILE__) . '/magpierss/rss_fetch.inc';
     } elseif ($engine == 'simplepie') {
         //hwa: NEW "SimplePie" include
         require_once dirname(__FILE__) . '/simplepie/simplepie.inc';
     }
     $cache_authors = array();
     $cache_entries = array();
     $cache_md5 = array();
     $sql_cache_authors = serendipity_db_Query("SELECT authorid, realname\n                                                 FROM {$serendipity['dbPrefix']}authors");
     if (is_array($sql_cache_authors)) {
         foreach ($sql_cache_authors as $idx => $author) {
             $cache_authors[$author['realname']] = $author['authorid'];
         }
     }
     if ($this->debug) {
         printf("DEBUG: cache_authors['realname'] = authorid has %d entries\n", count($cache_authors));
     }
     if ($opt['store_seperate']) {
         $sql_cache_entries = serendipity_db_query("SELECT e.feedid, e.id, e.entrydate, e.entrytitle\n                                                         FROM {$serendipity['dbPrefix']}aggregator_feedlist AS e");
         if (is_array($sql_cache_entries)) {
             foreach ($sql_cache_entries as $idx => $entry) {
                 $cache_entries[$entry['entrytitle']][$entry['feedid']][$entry['entrydate']] = $entry['id'];
             }
         }
     } else {
         $sql_cache_entries = serendipity_db_query("SELECT e.id, e.timestamp, e.authorid, e.title, ep.value\n                                                         FROM {$serendipity['dbPrefix']}entries AS e,\n                                                              {$serendipity['dbPrefix']}entryproperties AS ep\n                                                        WHERE e.id = ep.entryid\n                                                          AND ep.property = 'ep_aggregator_feed'");
         if (is_array($sql_cache_entries)) {
             foreach ($sql_cache_entries as $idx => $entry) {
                 $cache_entries[$entry['title']][$entry['authorid']][$entry['timestamp']] = $entry['id'];
             }
         }
     }
     if ($this->debug) {
         printf("DEBUG: cache_entries['title']['authorid']['timestamp'] = entryid has %d entries.\n", count($cache_entries));
     }
     $sql_cache_md5 = serendipity_db_query("SELECT entryid, md5, timestamp\n                                                     FROM {$serendipity['dbPrefix']}aggregator_md5");
     if (is_array($sql_cache_md5)) {
         foreach ($sql_cache_md5 as $idx => $entry) {
             $cache_md5[$entry['md5']]['entryid'] = $entry['entryid'];
             $cache_md5[$entry['md5']]['timestamp'] = $entry['timestamp'];
         }
     }
     if ($this->debug) {
         printf("DEBUG: cache_md5['md5'] = entryid has %d entries.\n", count($cache_md5));
     }
     foreach ($feeds as $feed) {
         if (!$opt['store_seperate']) {
             printf("Read %s.\n", $feed['feedurl']);
         }
         flush();
         $feed_authorid = $cache_authors[$feed['feedname']];
         if (empty($feed_authorid)) {
             $feed_authorid = 0;
         }
         if ($this->debug) {
             printf("DEBUG: Current authorid = %d\n", $feed_authorid);
         }
         $stack = array();
         if ($engine == 'onyx') {
             if (empty($feed['charset'])) {
                 $this->checkCharset($feed);
             }
             # test multiple likely charsets
             $charsets = array($feed['charset'], "ISO-8859-1", "utf-8");
             $retry = false;
             foreach ($charsets as $ch) {
                 if ($retry) {
                     printf("DEBUG: Retry with charset %s instead of %s\n", $ch, $feed['charset']);
                 }
                 $retry = true;
                 $c = new Onyx_RSS($ch);
                 # does it parse? if so, all is fine...
                 if ($c->parse($feed['feedurl'])) {
                     break;
                 }
             }
             while ($item = $c->getNextItem()) {
                 /* Okay this is where things get tricky. Everybody
                  * encodes their information differently. For now I'm going to focus on
                  * s9y weblogs. */
                 $fake_timestamp = false;
                 $date = $this->parseDate($item['pubdate']);
                 if ($this->debug) {
                     printf("DEBUG: pubDate %s = %s\n", $item['pubdate'], $date);
                 }
                 if ($date == -1) {
                     // Fallback to try for dc:date
                     $date = $this->parseDate($item['dc:date']);
                     if ($this->debug) {
                         printf("DEBUG: falling back to dc:date % s= %s\n", $item['dc:date'], $date);
                     }
                 }
                 if ($date == -1) {
                     // Couldn't figure out the date string. Set it to "now" and hope that the md5hash will get it.
                     $date = time();
                     $fake_timestamp = true;
                     if ($this->debug) {
                         printf("DEBUG: falling back to time() = %s\n", $date);
                     }
                 }
                 if (empty($item['title'])) {
                     if ($this->debug) {
                         printf("DEBUG: skip item: title was empty for %s\n", print_r($item, true));
                     }
                     continue;
                 }
                 $this->decode($c->rss['encoding'], $item);
                 $item['date'] = $date;
                 $stack[] = $item;
             }
         } elseif ($engine == 'magpierss') {
             // ----------------------------------------------------------
             // CLSC: New MagpieRSS code start
             // ----------------------------------------------------------
             $rss = fetch_rss($feed['feedurl']);
             foreach ($rss->items as $item) {
                 $fake_timestamp = false;
                 $date = $item['pubdate'];
                 if ($this->debug) {
                     printf("DEBUG: pubdate = %s\n", $item['pubdate'], $date);
                 }
                 // ----------------------------------------------------------
                 // CLSC:        Try a few different types of timestamp fields
                 //                So that we might get lucky even with non-standard feeds
                 // ----------------------------------------------------------
                 if ($date == "") {
                     // CLSC: magpie syntax for nested fields
                     $date = $item['dc']['date'];
                     if ($this->debug) {
                         printf("DEBUG: falling back to [dc][date] = %s\n", $item['dc:date'], $date);
                     }
                 }
                 if ($date == "") {
                     $date = $item['modified'];
                     if ($this->debug) {
                         printf("DEBUG: falling back to modified = %s\n", $item['modified'], $date);
                     }
                 }
                 if ($date == "") {
                     $date = $item['PubDate'];
                     if ($this->debug) {
                         printf("DEBUG: falling back PubDate = %s\n", $item['PubDate'], $date);
                     }
                 }
                 if ($date == "") {
                     $date = $item['created'];
                     if ($this->debug) {
                         printf("DEBUG: falling back to created = %s\n", $item['created'], $date);
                     }
                 }
                 if ($date == "") {
                     // CLSC: not proper magpie syntax but still catches some
                     $date = $item['dc:date'];
                     if ($this->debug) {
                         printf("DEBUG: falling back to dc:date = %s\n", $item['dc:date'], $date);
                     }
                 }
                 if ($date == "") {
                     $date = $item['updated'];
                     if ($this->debug) {
                         printf("DEBUG: falling back to updated = %s\n", $item['updated'], $date);
                     }
                 }
                 if ($date == "") {
                     $date = $item['published'];
                     if ($this->debug) {
                         printf("DEBUG: falling back to published = %s\n", $item['published'], $date);
                     }
                 }
                 if ($date == "") {
                     // ----------------------------------------------------------
                     //    CLSC:        If none of the above managed to identify a date:
                     //                 Set date to "now" and hope that the md5hash will get it.
                     // ----------------------------------------------------------
                     $date = time();
                     $fake_timestamp = true;
                     if ($this->debug) {
                         printf("DEBUG: falling back to time() = %s\n", $date);
                     }
                 }
                 // CLSC: if date is set to "now" parseDate can't parse it.
                 if ($fake_timestamp != true) {
                     $date = $this->parseDate($date);
                 }
                 if ($item['title'] == "") {
                     if ($this->debug) {
                         printf("DEBUG: skip item: title was empty for %s\n", print_r($item, true));
                     }
                     continue;
                 }
                 $item['date'] = $date;
                 $stack[] = $item;
                 // ----------------------------------------------------------
                 //    CLSC: New MagpieRSS code end
                 // ----------------------------------------------------------
             }
         } elseif ($engine == 'simplepie') {
             // hwa: new SimplePie code  ; lifted from the SimplePie demo
             $simplefeed = new SimplePie();
             $simplefeed->cache = false;
             $simplefeed->set_feed_url($feed['feedurl']);
             // Initialize the whole SimplePie object.  Read the feed, process it, parse it, cache it, and
             // all that other good stuff.  The feed's information will not be available to SimplePie before
             // this is called.
             $success = $simplefeed->init();
             // We'll make sure that the right content type and character encoding gets set automatically.
             // This function will grab the proper character encoding, as well as set the content type to text/html.
             $simplefeed->set_output_encoding(LANG_CHARSET);
             $simplefeed->handle_content_type();
             $item['new_feedicon'] = $simplefeed->get_favicon();
             // error handling
             if ($simplefeed->error()) {
                 if (!$opt['store_seperate']) {
                     printf('<p><b>ERROR:</b> ' . (function_exists('serendipity_specialchars') ? serendipity_specialchars($simplefeed->error()) : htmlspecialchars($simplefeed->error(), ENT_COMPAT, LANG_CHARSET)) . "</p>\r\n");
                 }
             }
             if ($success) {
                 foreach ($simplefeed->get_items() as $simpleitem) {
                     // map SimplePie items to s9y items
                     $item['title'] = $simpleitem->get_title();
                     $item['date'] = $simpleitem->get_date('U');
                     $item['pubdate'] = $simpleitem->get_date('U');
                     $item['description'] = $simpleitem->get_description();
                     $item['content'] = $simpleitem->get_content();
                     $item['link'] = $simpleitem->get_permalink();
                     $item['author'] = $simpleitem->get_author();
                     //if ($this->debug) {
                     //  printf("DEBUG: SimplePie item: author: $item['author'], title: $item['title'], date: $item['date']\n");
                     //}
                     $stack[] = $item;
                 }
             } else {
                 if (!$opt['store_seperate']) {
                     printf('<p><b>ERROR:</b> ' . print_r($success, true) . "</p>\r\n");
                 }
             }
         }
         while (list($key, $item) = each($stack)) {
             if ($opt['store_seperate']) {
                 $ep_id = $cache_entries[$item['title']][$feed['feedid']][$item['date']];
                 if ($this->debug) {
                     printf("DEBUG: lookup cache_entries[%s][%s][%s] finds %s.\n", $item['title'], $feed['feedid'], $item['date'], empty($ep_id) ? "nothing" : $ep_id);
                 }
             } else {
                 $ep_id = $cache_entries[$item['title']][$feed_authorid][$item['date']];
                 if ($this->debug) {
                     printf("DEBUG: lookup cache_entries[%s][%s][%s] finds %s.\n", $item['title'], $feed_authorid, $item['date'], empty($ep_id) ? "nothing" : $ep_id);
                 }
             }
             if (!empty($ep_id) and serendipity_db_bool($this->get_config('ignore_updates'))) {
                 if ($this->debug) {
                     printf("DEBUG: entry %s is known and ignore_updates is set.\n", $ep_id);
                 }
                 continue;
             }
             # NOTE: If $ep_id is NULL or EMPTY, it means that an entry with this title does not
             #       yet exist. Later on we check if a similar entry with the body exists and skip
             #       updates in this case. Else it means that the new entry needs to be inserted
             #       as a new one.
             # The entry is probably new?
             $entry = array('id' => $ep_id, 'title' => $item['title'], 'timestamp' => $item['date'], 'extended' => '', 'isdraft' => serendipity_db_bool($this->get_config('publishflag')) ? 'false' : 'true', 'allow_comments' => serendipity_db_bool($this->get_config('allow_comments')) ? 'true' : 'false', 'categories' => $feed['categoryids'], 'author' => $feed['feedname'], 'authorid' => $feed_authorid);
             // ----------------------------------------------------------
             //    CLSC: Added a few flavours
             if ($item['content:encoded']) {
                 $entry['body'] = $item['content:encoded'];
             } elseif ($item['description']) {
                 $entry['body'] = $item['description'];
             } elseif ($item['content']['encoded']) {
                 $entry['body'] = $item['content']['encoded'];
             } elseif ($item['atom_content']) {
                 $entry['body'] = $item['atom_content'];
             } elseif ($item['content']) {
                 $entry['body'] = $item['content'];
             }
             $md5hash = md5($feed_authorid . $item['title'] . $entry['body']);
             # Check 1: Have we seen this MD5?
             if ($this->debug) {
                 printf("DEBUG: lookup cache_md5[%s] finds %s.\n", $md5hash, empty($cache_md5[$md5hash]) ? "nothing" : $cache_md5[$md5hash]['entryid']);
             }
             # If we have this md5, title and body for this article
             # are unchanged. We do not need to do anything.
             if (isset($cache_md5[$md5hash])) {
                 continue;
             }
             # Check 2 (conditional: expire enabled?):
             #         Is this article too old?
             if ($this->get_config('expire') > 0) {
                 $expire = time() - 86400 * $this->get_config('expire');
                 if ($item['date'] < $expire) {
                     if ($this->debug) {
                         printf("DEBUG: '%s' is too old (%s < %s).\n", $item['title'], $item['date'], $expire);
                     }
                     continue;
                 }
             }
             # Check 3: Does this article match our expressions?
             if (!empty($feed['match_expression'])) {
                 $expressions = explode("~", $feed['match_expression']);
                 $match = 0;
                 foreach ($expressions as $expression) {
                     $expression = ltrim(rtrim($expression));
                     if (preg_match("~{$expression}~imsU", $entry['title'] . $entry['body'])) {
                         $match = 1;
                     }
                 }
                 if ($match == 0) {
                     continue;
                 }
             }
             $feed['articleurl'] = $item['link'];
             if ($item['author']) {
                 $feed['author'] = $item['author'];
             } elseif ($item['dc:creator']) {
                 $feed['author'] = $item['dc:creator'];
             }
             // Store as property
             // Plugins might need this.
             $serendipity['POST']['properties'] = array('fake' => 'fake');
             $markups = explode('^', $this->get_config('markup'));
             if (is_array($markups)) {
                 foreach ($markups as $markup) {
                     $serendipity['POST']['properties']['disable_markups'][] = $markup;
                 }
             }
             if ($opt['store_seperate']) {
                 if ($entry['id'] > 0) {
                     serendipity_db_query("UPDATE {$serendipity['dbPrefix']}aggregator_feedlist \n                        SET feedid      = '" . $feed['feedid'] . "',\n                            categoryid  = '" . $feed['categoryids'][0] . "',\n                            entrydate   = '" . serendipity_db_escape_string($entry['timestamp']) . "',\n                            entrytitle  = '" . serendipity_db_escape_string($entry['title']) . "',\n                            entrybody   = '" . serendipity_db_escape_string($entry['body']) . "',\n                            entryurl    = '" . serendipity_db_escape_string($item['link']) . "'\n                        WHERE id = " . $entry['id']);
                     $entryid = $entry['id'];
                 } else {
                     serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}aggregator_feedlist (\n                            feedid,\n                            categoryid,\n                            entrydate,\n                            entrytitle,\n                            entrybody,\n                            entryurl\n                        ) VALUES (\n                            '" . $feed['feedid'] . "',\n                            '" . $feed['categoryids'][0] . "',\n                            '" . serendipity_db_escape_string($entry['timestamp']) . "',\n                            '" . serendipity_db_escape_string($entry['title']) . "',\n                            '" . serendipity_db_escape_string($entry['body']) . "',\n                            '" . serendipity_db_escape_string($item['link']) . "'\n                        )");
                     $entryid = serendipity_db_insert_id();
                 }
                 $this->feedupdate_finish($feed, $entryid);
             } else {
                 $entryid = serendipity_updertEntry($entry);
                 $this->insertProperties($entryid, $feed, $md5hash);
             }
             if (!$opt['store_seperate']) {
                 printf(" Save '%s' as %s.\n", $item['title'], $entryid);
             }
         }
         if (!$opt['store_seperate']) {
             printf("Finish feed.\n");
         }
     }
     if (!$opt['store_seperate']) {
         printf("Finish planetarium.\n");
     }
 }
Esempio n. 4
0
 function import()
 {
     global $serendipity;
     if (serendipity_db_bool($this->data['wpxrss'])) {
         return $this->import_wpxrss();
     }
     $c = new Onyx_RSS($this->data['charset']);
     $c->parse($this->data['url']);
     $this->data['encoding'] = $c->rss['encoding'];
     $serendipity['noautodiscovery'] = 1;
     while ($item = $c->getNextItem()) {
         $entry = array();
         if ($this->buildEntry($item, $entry)) {
             serendipity_updertEntry($entry);
         }
     }
     return true;
 }
 function readRssRemote($url)
 {
     $this->log("Fetchig remote rss from: " . $url);
     require_once (defined('S9Y_PEAR_PATH') ? S9Y_PEAR_PATH : S9Y_INCLUDE_PATH . 'bundled-libs/') . 'HTTP/Request.php';
     $req = new HTTP_Request($url, array('allowRedirects' => true, 'maxRedirects' => 3));
     if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') {
         $this->log("Error reading {$url}");
         return;
     }
     # Fetch html content:
     $data = $req->getResponseBody();
     $this->log("Have data!");
     // Check if page defines a RSS link
     $matches = array();
     if (preg_match('@<link[^>]*? type="application/rss\\+xml"[^>]*? href="([^"]*?)"@Usi', $data, $matches)) {
         $this->log("rss link found, matches: " . print_r($matches[1], TRUE));
         $rssUrl = $matches[1];
     } else {
         $this->log("rss link not found");
         return;
     }
     // Now fetch the RSS feed:
     require_once (defined('S9Y_PEAR_PATH') ? S9Y_PEAR_PATH : S9Y_INCLUDE_PATH . 'bundled-libs/') . 'Onyx/RSS.php';
     # test multiple likely charsets
     $charsets = array("utf-8", "ISO-8859-1");
     $retry = false;
     foreach ($charsets as $ch) {
         if ($retry) {
             $this->log("Retrying charset {$ch}");
         }
         $retry = true;
         $rss = new Onyx_RSS($ch);
         # does it parse? if so, all is fine...
         if ($rss->parse($rssUrl)) {
             break;
         }
     }
     $articles = array();
     $itemCount = 0;
     $maxItems = $announcerssmax = $this->get_config('announcerssmax', 3);
     // Iterate the items
     while ($item = $rss->getNextItem()) {
         if ($itemCount >= $maxItems) {
             break;
         }
         $article = array();
         $article['title'] = $item['title'];
         $hash = $this->hashString($item['title'] . $item['link']);
         $article['url'] = $hash . "\n" . $item['title'] . "\n" . $item['link'];
         $article['nohashUrl'] = $item['link'];
         $articles[] = $article;
         $itemCount++;
     }
     $result['articles'] = $articles;
     $result['url'] = $url;
     return $result;
 }
 function event_hook($event, &$bag, &$eventData, $addData = null)
 {
     global $serendipity;
     $hooks =& $bag->get('event_hooks');
     if (isset($hooks[$event])) {
         switch ($event) {
             case 'backend_frontpage_display':
                 $number = $this->get_config('number');
                 $title = $this->get_config('title');
                 $rssuri = $this->get_config('rssuri');
                 $target = $this->get_config('target');
                 $cachetime = $this->get_config('cachetime');
                 echo '<h3>' . $title . '</h3>';
                 if (!$number || !is_numeric($number) || $number < 1) {
                     $showAll = true;
                 } else {
                     $showAll = false;
                 }
                 if (!$target || strlen($target) < 1) {
                     $target = '_blank';
                 }
                 if (!$cachetime || !is_numeric($cachetime)) {
                     $cachetime = 10800;
                     // 3 hours in seconds
                 }
                 if (trim($rssuri)) {
                     $feedcache = $serendipity['serendipityPath'] . 'archives/remoterss_cache_' . preg_replace('@[^a-z0-9]*@i', '', $rssuri) . '.dat';
                     if (!file_exists($feedcache) || filesize($feedcache) == 0 || filemtime($feedcache) < time() - $cachetime) {
                         require_once 'bundled-libs/Onyx/RSS.php';
                         $c = new Onyx_RSS();
                         $c->parse($rssuri);
                         $i = 0;
                         $content = '<ul>';
                         while (($showAll || $i < $number) && ($item = $c->getNextItem())) {
                             if (empty($item['title'])) {
                                 continue;
                             }
                             $content .= '<li><a href="' . $this->decode($item['link']) . '" target="' . $target . '">';
                             $content .= $this->decode($item['title']) . '</a></li>';
                             ++$i;
                         }
                         $content .= '</ul>';
                         $fp = @fopen($feedcache, 'w');
                         if ($fp) {
                             fwrite($fp, $content);
                             fclose($fp);
                         } else {
                             echo '<!-- Cache failed to ' . $feedcache . ' in ' . getcwd() . ' -->';
                         }
                     } else {
                         $content = file_get_contents($feedcache);
                     }
                     echo $content;
                 } else {
                     echo PLUGIN_REMOTERSS_NOURI;
                 }
                 break;
             default:
                 return false;
                 break;
         }
     } else {
         return false;
     }
 }