RSSReader.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // OpenRat Content Management System
00006 // Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 // ---------------------------------------------------------------------------
00022 // $Log$
00023 // Revision 1.2  2004-12-19 15:18:50  dankert
00024 // Speichern des RSS-Feeds in Session (Performance)
00025 //
00026 // Revision 1.1  2004/10/14 21:15:13  dankert
00027 // Lesen eines RSS-Feeds und erzeugen eines HTML-Abschnittes dafuer
00028 //
00029 // ---------------------------------------------------------------------------
00030 
00031 
00032 
00036 class RSSReader extends Dynamic
00037 {
00042      var $parameters  = Array(
00043           'url'=>'URL from which the RSS is fetched'
00044           );
00045 
00050      var $description = 'Reads a RSS-Feed and displays its content as a html list';
00051 
00052      var $url       = 'http://www.heise.de/newsticker/heise.rdf';
00053 
00054 
00055 
00056      function execute()
00057      {
00058           // Sessionvariable mit CRC verschluesseln, falls es mehrere RSS-Feeds im Projekt gibt
00059           $sessVar = 'RSSReader_'.crc32($this->url);
00060           $cache = $this->getSessionVar( $sessVar );
00061           
00062           if   ( !empty($cache) )
00063           {
00064                // Wenn Cache vorhanden, dann diesen ausgeben
00065                $this->output( $cache );
00066           }
00067           else
00068           {
00069                // Wenn Cache leer, dann RSS erzeugen und in Session speichern
00070                $this->create();
00071                $this->setSessionVar( $sessVar,$this->getOutput() );
00072           } 
00073      }
00074 
00075 
00076 
00077      // Erzeugt den Text des RSS-Feeds
00078      function create()
00079      {
00080           $rss = $this->parse( implode('',file($this->url)) );
00081           $out = array();
00082           
00083           $this->output('<ul>');
00084 
00085           // Schleife ueber alle Inhalte des RSS-Feeds
00086           foreach( $rss['items'] as $item )
00087           {
00088                $this->output('<li>');
00089                $this->output('<a href="'.$item['link'].'">'.$item['title'].'</a><br/>'.$item['description']);
00090                $this->output('</li>');
00091           }
00092 
00093           $this->output('</ul>');
00094      }
00095 
00096 
00097 
00098      function parse( $feed )
00099      {
00100           // Parses the RSS feed into the array
00101           $arr = array();
00102           // Determine encoding
00103           preg_match('/<\?xml version="1\.0" encoding="(.*)"\?>/i', $feed, $sarr);
00104           if   ( !empty($sarr[1]))
00105                $arr["encoding"] = $sarr[1];
00106           // Determine title
00107           preg_match('/<title>(.*)<\/title>/i', $feed, $sarr);
00108           if   ( !empty($sarr[1]))
00109                $arr["title"] = $sarr[1];
00110           // Determine title
00111           preg_match('/<title>(.*)<\/title>/i', $feed, $sarr);
00112           if   ( !empty($sarr[1]))
00113                $arr["title"] = $sarr[1];
00114           // Determine description
00115           preg_match('/<description>(.*)<\/description>/i', $feed, $sarr);
00116           if   ( !empty($sarr[1]))
00117                $arr["description"] = $sarr[1];
00118           // Determine link
00119           preg_match('/<link>(.*)<\/link>/i', $feed, $sarr);
00120           if   ( !empty($sarr[1]))
00121                $arr["link"] = $sarr[1];
00122           // Determine language
00123           preg_match('/<language>(.*)<\/language>/i', $feed, $sarr);
00124           if   ( !empty($sarr[1]))
00125                $arr["language"] = $sarr[1];
00126           // Determine generator
00127           preg_match('/<generator>(.*)<\/generator>/i', $feed, $sarr);
00128           if   ( !empty($sarr[1]))
00129                $arr["generator"] = $sarr[1];
00130           // Strip items
00131           $parts = explode("<item>", $feed);
00132           foreach($parts as $part)
00133           {
00134                $item = substr($part, 0, strpos($part, "</item>"));
00135                if   ( !empty($item) )
00136                     $items[] = $item;
00137           }
00138           // Fill the channel array
00139           $arr["items"] = array();
00140           foreach($items as $item)
00141           {
00142                $i = array();
00143                
00144                // Determine title
00145                preg_match('/<title>(.*)<\/title>/i', $item, $title);
00146                if   ( !empty($title[1]))
00147                     $i['title'] = $title[1];
00148                else
00149                     $i['title'] = '';
00150 
00151                // Determine pubdate
00152                preg_match('/<pubDate>(.*)<\/pubDate>/i', $item, $pubdate);
00153                if   ( !empty($pubdate[1]))
00154                     $i['pubDate'] = strtotime($pubdate[1]);
00155                else
00156                     $i['pubDate'] = '';
00157 
00158                // Determine link
00159                preg_match('/<link>(.*)<\/link>/i', $item, $link);
00160                if   ( !empty($link[1]))
00161                     $i['link'] = $link[1];
00162                else
00163                     $i['link'] = '';
00164 
00165                // Determine description
00166                if(stristr($item, '<![CDATA['))
00167                     preg_match('/<description><!\[CDATA\[(.*)\]\]><\/description>/is', $item, $description);
00168                else
00169                     preg_match('/<description>(.*)<\/description>/is', $item, $description);
00170 
00171                if   ( !empty($description[1]))
00172                     $i['description'] = $description[1];
00173                else
00174                     $i['description'] = '';
00175 
00176                $arr["items"][] = $i;
00177           }
00178           return $arr;
00179      }
00180 }

Erzeugt am Thu May 14 00:55:48 2009 für OpenRat von  doxygen 1.5.8