Advertisement
Guest User

RSSFeed.php

a guest
Apr 11th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.35 KB | None | 0 0
  1. <?php
  2.     //Hanlde all actions related to RSS Feeds
  3.     require_once('Database.php');
  4.     require_once("model/simplepie/autoloader.php");
  5.     function shorten($string, $length)
  6.     {
  7.         // By default, an ellipsis will be appended to the end of the text.
  8.         $suffix = '&hellip;';
  9.      
  10.         // Convert 'smart' punctuation to 'dumb' punctuation, strip the HTML tags,
  11.         // and convert all tabs and line-break characters to single spaces.
  12.         $short_desc = trim(str_replace(array("\r","\n", "\t"), ' ', strip_tags($string)));
  13.      
  14.         // Cut the string to the requested length, and strip any extraneous spaces
  15.         // from the beginning and end.
  16.         $desc = trim(substr($short_desc, 0, $length));
  17.      
  18.         // Find out what the last displayed character is in the shortened string
  19.         $lastchar = substr($desc, -1, 1);
  20.      
  21.         // If the last character is a period, an exclamation point, or a question
  22.         // mark, clear out the appended text.
  23.         if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix='';
  24.      
  25.         // Append the text.
  26.         $desc .= $suffix;
  27.      
  28.         // Send the new description back to the page.
  29.         return $desc;
  30.     }
  31.     class RSSFeed {
  32.  
  33.         public function getFeed($feed_url) {
  34.             $feed = new SimplePie($feed_url);
  35.             $feed->init();
  36.             $feed->handle_content_type();
  37.             foreach ($feed->get_items() as $item) {
  38.                 $output = "<article>"
  39.                 . "<h3><a href=\"" . $item->get_permalink() . "\" title=\"" . $item->get_title() . "\" class=\"articleTitle\">" . $item->get_title() . "</a></h3><p>";
  40.  
  41.                 if ($category = $item->get_category()) {
  42.                     $output .= $category->get_label() . " ";
  43.                 }
  44.  
  45.                 $output .= $item->get_date();
  46.  
  47.  
  48.                 $output .= "</p><p>";
  49.  
  50.                 $output .= shorten($item->get_description(), 600) . "<br /><br />" . "<a href=\"" . $item->get_permalink() . "\" title=\"Read More\" class=\"btn btn-info\">Read More</a>";
  51.  
  52.                 $output .= "</p>";
  53.  
  54.                 echo $output;
  55.             }//end foreach($feed->get_items() as $item)
  56.         }//end getFeed($feed_url)
  57.  
  58.         public function importRSSFeeds($xmlFile, $DB) {
  59.             $xml = simplexml_load_file($xmlFile);
  60.             foreach($xml as $feed) {
  61.                 foreach($feed->outline as $thisFeed) {
  62.                     if($thisFeed->outline['type'] == "rss") {
  63.                             $DB->addFeedToDatabase($thisFeed['text'], $thisFeed['title'], "folder", "", "");
  64.                         foreach($thisFeed->outline as $feeds) {
  65.                             $DB->addFeedToDatabase($feeds['text'], $feeds['title'], $feeds['type'], $feeds['xmlUrl'], $feeds['htmlUrl']);
  66.                         }
  67.                         echo "<br /><br />";
  68.                     }
  69.                 }
  70.             }
  71.         } //end importRSSFeeds($xmlFile)
  72.  
  73.         public function getFeedList() {
  74.             $lastType = "";
  75.             $DB = new Database();
  76.             $result = $DB->returnFeedList();
  77.             foreach($result as $individualFeed) {
  78.                     if($individualFeed['type'] == "folder") {
  79.                         if ($lastType == "rss") {
  80.                             echo "</ul></div>";
  81.                         }
  82.                         echo "<li><a href=\"#\" data-toggle=\"collapse\" data-target=\"#" . str_replace(" ", "", $individualFeed['title']) ."\"><i class=\"icon-folder-close\"></i>" . $individualFeed['title'] . "</a></li>";
  83.                         echo "<div class=\"collapse in\" id=\"" . str_replace(" ", "", $individualFeed['title']) . "\">";
  84.                         echo "<ul class=\"nav nav-list\">";
  85.                     } else if($individualFeed['type'] == "rss") {
  86.                         echo "<li><a href=\"" . $individualFeed['xmlUrl'] . "\" class=\"feedName\">" . $individualFeed['title'] . "</a></li>";
  87.                     }
  88.                     $lastType = $individualFeed['type'];
  89.                 }
  90.             $DB->closeDatabaseConnection();
  91.         }
  92.     }//end class RSSFeed
  93.  
  94.     if (isset($_GET['url'])) {
  95.         $RSS = new RSSFeed();
  96.         $RSS->getFeed($_GET['url']);
  97.     } else if(isset($_POST['url'])) {
  98.         $RSS = new RSSFeed();
  99.         $RSS->getFeed($_POST['url']);
  100.     }
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement