openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

RSSReader.class.php (5364B)


      1 <?php
      2 namespace cms\macros\macro;
      3 // ---------------------------------------------------------------------------
      4 // $Id$
      5 // ---------------------------------------------------------------------------
      6 // OpenRat Content Management System
      7 // Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de
      8 //
      9 // This program is free software; you can redistribute it and/or
     10 // modify it under the terms of the GNU General Public License
     11 // as published by the Free Software Foundation; either version 2
     12 // of the License, or (at your option) any later version.
     13 //
     14 // This program is distributed in the hope that it will be useful,
     15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 // GNU General Public License for more details.
     18 //
     19 // You should have received a copy of the GNU General Public License
     20 // along with this program; if not, write to the Free Software
     21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     22 // ---------------------------------------------------------------------------
     23 // $Log$
     24 // Revision 1.2  2004-12-19 15:18:50  dankert
     25 // Speichern des RSS-Feeds in Session (Performance)
     26 //
     27 // Revision 1.1  2004/10/14 21:15:13  dankert
     28 // Lesen eines RSS-Feeds und erzeugen eines HTML-Abschnittes dafuer
     29 //
     30 // ---------------------------------------------------------------------------
     31 use util\Macro;
     32 
     33 
     34 /**
     35  * @author Jan Dankert
     36  */
     37 class RSSReader extends Macro
     38 {
     39 	/**
     40 	 * Bitte immer alle Parameter in dieses Array schreiben, dies ist fuer den Web-Developer hilfreich.
     41 	 * @type String
     42 	 */
     43 	var $parameters  = Array(
     44 		'url'=>'URL from which the RSS is fetched'
     45 		);
     46 
     47 	/**
     48 	 * Bitte immer eine Beschreibung benutzen, dies ist fuer den Web-Developer hilfreich.
     49 	 * @type String
     50 	 */
     51 	var $description = 'Reads a RSS-Feed and displays its content as a html list';
     52 
     53 	var $url       = 'http://www.heise.de/newsticker/heise.rdf';
     54 
     55 
     56 
     57 	function execute()
     58 	{
     59 	    // TODO: Caching of macro output should be done by the CMS.
     60 
     61         // Sessionvariable mit CRC verschluesseln, falls es mehrere RSS-Feeds im Projekt gibt
     62 		$sessVar = 'RSSReader_'.crc32($this->url);
     63 		$cache = $this->getSessionVar( $sessVar );
     64 		
     65 		if	( !empty($cache) )
     66 		{
     67 			// Wenn Cache vorhanden, dann diesen ausgeben
     68 			$this->output( $cache );
     69 		}
     70 		else
     71 		{
     72 			// Wenn Cache leer, dann RSS erzeugen und in Session speichern
     73 
     74             ob_start();
     75             $this->create();
     76 
     77             $output = ob_get_contents();
     78             ob_end_clean();
     79 
     80             $this->setSessionVar( $sessVar,$output );
     81             echo $output;
     82 		} 
     83 	}
     84 
     85 
     86 
     87 	// Erzeugt den Text des RSS-Feeds
     88 	function create()
     89 	{
     90 		$rss = $this->parse( implode('',file($this->url)) );
     91 		$out = array();
     92 		
     93 		$this->output('<ul>');
     94 
     95 		// Schleife ueber alle Inhalte des RSS-Feeds
     96 		foreach( $rss['items'] as $item )
     97 		{
     98 			$this->output('<li>');
     99 			$this->output('<a href="'.$item['link'].'">'.$item['title'].'</a><br/>'.$item['description']);
    100 			$this->output('</li>');
    101 		}
    102 
    103 		$this->output('</ul>');
    104 	}
    105 
    106 
    107 
    108 	function parse( $feed )
    109 	{
    110 		// Parses the RSS feed into the array
    111 		$arr = array();
    112 		// Determine encoding
    113 		preg_match('/<\?xml version="1\.0" encoding="(.*)"\?>/i', $feed, $sarr);
    114 		if	( !empty($sarr[1]))
    115 			$arr["encoding"] = $sarr[1];
    116 		// Determine title
    117 		preg_match('/<title>(.*)<\/title>/i', $feed, $sarr);
    118 		if	( !empty($sarr[1]))
    119 			$arr["title"] = $sarr[1];
    120 		// Determine title
    121 		preg_match('/<title>(.*)<\/title>/i', $feed, $sarr);
    122 		if	( !empty($sarr[1]))
    123 			$arr["title"] = $sarr[1];
    124 		// Determine description
    125 		preg_match('/<description>(.*)<\/description>/i', $feed, $sarr);
    126 		if	( !empty($sarr[1]))
    127 			$arr["description"] = $sarr[1];
    128 		// Determine link
    129 		preg_match('/<link>(.*)<\/link>/i', $feed, $sarr);
    130 		if	( !empty($sarr[1]))
    131 			$arr["link"] = $sarr[1];
    132 		// Determine language
    133 		preg_match('/<language>(.*)<\/language>/i', $feed, $sarr);
    134 		if	( !empty($sarr[1]))
    135 			$arr["language"] = $sarr[1];
    136 		// Determine generator
    137 		preg_match('/<generator>(.*)<\/generator>/i', $feed, $sarr);
    138 		if	( !empty($sarr[1]))
    139 			$arr["generator"] = $sarr[1];
    140 		// Strip items
    141 		$parts = explode("<item>", $feed);
    142 		$items = array();
    143 		foreach($parts as $part)
    144 		{
    145 			$item = substr($part, 0, strpos($part, "</item>"));
    146 			if	( !empty($item) )
    147 				$items[] = $item;
    148 		}
    149 		// Fill the channel array
    150 		$arr["items"] = array();
    151 		foreach($items as $item)
    152 		{
    153 			$i = array();
    154 			
    155 			// Determine title
    156 			preg_match('/<title>(.*)<\/title>/i', $item, $title);
    157 			if	( !empty($title[1]))
    158 				$i['title'] = $title[1];
    159 			else
    160 				$i['title'] = '';
    161 
    162 			// Determine pubdate
    163 			preg_match('/<pubDate>(.*)<\/pubDate>/i', $item, $pubdate);
    164 			if	( !empty($pubdate[1]))
    165 				$i['pubDate'] = strtotime($pubdate[1]);
    166 			else
    167 				$i['pubDate'] = '';
    168 
    169 			// Determine link
    170 			preg_match('/<link>(.*)<\/link>/i', $item, $link);
    171 			if	( !empty($link[1]))
    172 				$i['link'] = $link[1];
    173 			else
    174 				$i['link'] = '';
    175 
    176 			// Determine description
    177 			if(stristr($item, '<![CDATA['))
    178 				preg_match('/<description><!\[CDATA\[(.*)\]\]><\/description>/is', $item, $description);
    179 			else
    180 				preg_match('/<description>(.*)<\/description>/is', $item, $description);
    181 
    182 			if	( !empty($description[1]))
    183 				$i['description'] = $description[1];
    184 			else
    185 				$i['description'] = '';
    186 
    187 			$arr["items"][] = $i;
    188 		}
    189 		return $arr;
    190 	}
    191 }