Campustream 1.0
A social network MQP for WPI
application/lib/rssreader.php
Go to the documentation of this file.
00001 <?
00002 include_once 'XML/Unserializer.php';
00003 
00009 class rssreader {
00010         private $rss_data;
00011         private $xml;
00012         private $data;
00013         
00014         private $generators = array(
00015                 'wordpress',
00016                 'blogger'
00017         );
00018         
00022         public static function load($url) {
00023                 $opts = array(
00024                         'http' => array(
00025                                 'method' => 'GET',
00026                                 'timeout' => 5
00027                         )
00028                 );
00029                 
00030                 $context = stream_context_create($opts);
00031                 $rss_data = file_get_contents($url, false, $context);
00032 
00033                 if (strlen($rss_data)) {
00034                         return new rssreader($rss_data);
00035                 }
00036         }
00037         
00038         public function __construct($rss_data) {
00039                 $this->rss_data = $rss_data;
00040                 
00041                 $opts = array(
00042                         'complexType' => 'object'
00043                 );
00044                 
00045                 $this->xml = &new XML_Unserializer($opts);
00046                 
00047                 $this->parse();
00048         }
00049         
00050         private function parse() {
00051                 $result = $this->xml->unserialize($this->rss_data, false);
00052                 if ($result) {
00053                         $this->data = $this->xml->getUnserializedData();
00054                 }
00055         }
00056         
00060         public function get() {
00061                 return $this->data->channel;
00062         }
00063         
00064         private function getGenerator() {
00065                 foreach ($this->generators as $gen) {
00066                         if (stristr($this->get()->generator, $gen)) {
00067                                 return $gen;
00068                         }
00069                 }
00070                 
00071                 return 'unknown';
00072         }
00073         
00074         public function __get($key) {
00075                 switch ($key) {
00076                         case 'generator': return $this->getGenerator();
00077                         case 'posts': return $this->get()->item;
00078                 }
00079                 
00080                 return parent::__get($key);
00081         }
00082 }