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