[ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 /****************************************************************************/ 3 /* feedparserfactory.class.php */ 4 /* */ 5 /****************************************************************************/ 6 /* Copyright (c) 2004 Michael Jervis (mike@fuckingbrit.com) */ 7 /* */ 8 /* This software is licensed under the terms of the ZLIB License: */ 9 /* */ 10 /* This software is provided 'as-is', without any express or implied */ 11 /* warranty. In no event will the authors be held liable for any damages */ 12 /* arising from the use of this software. */ 13 /* */ 14 /* Permission is granted to anyone to use this software for any purpose, */ 15 /* including commercial applications, and to alter it and redistribute it */ 16 /* freely, subject to the following restrictions: */ 17 /* */ 18 /* 1. The origin of this software must not be misrepresented; you must not */ 19 /* claim that you wrote the original software. If you use this software */ 20 /* in a product, an acknowledgment in the product documentation would be*/ 21 /* appreciated but is not required. */ 22 /* */ 23 /* 2. Altered source versions must be plainly marked as such, and must not */ 24 /* be misrepresented as being the original software. */ 25 /* */ 26 /* 3. This notice may not be removed or altered from any source */ 27 /* distribution. */ 28 /****************************************************************************/ 29 30 // Require pear HTTP_REQUEST 31 require_once('HTTP/Request.php'); 32 /** 33 * FeedParserFactory provides generic access to syndication feed formats. 34 * 35 * <p>This library provides abstraction of feed formats. It provides a factory 36 * pattern interface to constructing feed handlers to parse incoming 37 * syndication files, and write outgoing syndication files. The interface is 38 * not tied to any system implementation, however, I plan to provide interface 39 * to geeklog.</p> 40 * 41 * @author Michael Jervis (mike@Fuckingbrit.com) 42 * @copyright Michael Jervis 2004 43 * @see FeedParserBase 44 */ 45 class FeedParserFactory 46 { 47 var $readerName; 48 var $reader; 49 var $userAgent; 50 var $errorStatus; 51 52 /** 53 * Constructor, loads feedparser classes into memory. 54 * 55 * This takes a path on which the supporting feed classes exist, and then 56 * tries to find all *.feed.class.php and brings them into scope. 57 * 58 * @param string $path path to include files from. 59 */ 60 function FeedParserFactory( $path='' ) 61 { 62 if( $path != '' ) 63 { 64 if( is_dir( $path ) ) 65 { 66 $folder = opendir( $path ); 67 while( ( $filename = @readdir( $folder ) ) !== false ) 68 { 69 if( preg_match( '/(.*)\.feed\.class\.php$/i', $filename ) ) 70 { 71 require_once( $path.'/'.$filename ); 72 } 73 } 74 } 75 } 76 } 77 78 /** 79 * Method to get a feed handler class. 80 * 81 * This function takes a url, fetches it, parses it, and thus figures out 82 * what type of feed parser to return, with the contents all parsed for your 83 * viewing pleasure. 84 * 85 * @access public 86 * @param string $url The url to a feed type to syndicate. 87 */ 88 function reader( $url, $targetformat='' ) 89 { 90 if( $data = $this->_getFeed( $url ) ) 91 { 92 return $this->_findFeed( $data, $targetformat ); 93 } else { 94 return false; 95 } 96 } 97 98 /** 99 * Method to get a feed handler class. 100 * 101 * this function assumes you know what you want, and gets you a blank feed 102 * handler to write that data. 103 * 104 * @access public 105 * @param string $feedtype the type of feed to get 106 * @param float $version the version 107 */ 108 function writer( $feedtype, $version=2.0 ) 109 { 110 $feedtype = strtolower($feedtype); 111 switch ($feedtype) 112 { 113 case 'rss': 114 if( $version == '' ) 115 { 116 return new RSS0x(); 117 } else { 118 if( $version < 1.0 ) 119 { 120 return new RSS0x(); 121 } else if( $version >= 2.0 ) { 122 return new RSS20(); 123 } else { 124 return new RSS0x(); 125 } 126 } 127 break; 128 case 'rdf': 129 return new RDF(); 130 break; 131 case 'atom': 132 if( $version == '' ) 133 { 134 return new Atom10(); 135 } else { 136 if( $version < 1.0 ) 137 { 138 return new Atom03(); 139 } else { 140 return new Atom10(); 141 } 142 } 143 break; 144 default: 145 return false; 146 break; 147 } 148 } 149 150 /** 151 * Provides an array of feed types understood. 152 * 153 * Provides an array of feed types understood. Yeah it's manual, but, the 154 * feed reader has to be edited to support new inbounds anyway. 155 * 156 * @access public 157 */ 158 function getFeedTypes() 159 { 160 $types = array(); 161 $types[] = array('name'=>'RSS','version'=>'0.9x'); 162 $types[] = array('name'=>'RSS','version'=>'2.0'); 163 $types[] = array('name'=>'RDF','version'=>'1.0'); 164 $types[] = array('name'=>'Atom','version'=>'0.3'); 165 $types[] = array('name'=>'Atom','version'=>'1.0'); 166 return $types; 167 } 168 169 /** 170 * Opens a url in a file pointer 171 * 172 * @access private 173 * @param string $url The URL to open. 174 */ 175 function _getFeed( $url ) 176 { 177 $req =& new HTTP_Request($url, array('allowRedirects' => true)); 178 if ($this->userAgent != '') 179 { 180 $req->addHeader('User-Agent', $this->userAgent); 181 } 182 $response = $req->sendRequest(); 183 if (!PEAR::isError($response)) { 184 return $req->getResponseBody(); 185 } else { 186 $this->errorStatus = array('HTTP Fetch Failed', $response->getCode(), $response->getMessage()); 187 return false; 188 } 189 } 190 191 /** 192 * Find out what format a feed is in. 193 * 194 * @access private 195 */ 196 function _findFeed( $data, $format='' ) 197 { 198 $xml_parser = xml_parser_create(); 199 xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, true ); 200 if( $format != '' ) 201 { 202 @xml_parser_set_option( $xml_parser, XML_OPTION_TARGET_ENCODING, 203 $format ); 204 } 205 xml_set_element_handler( $xml_parser, array(&$this,'_startElement'), array(&$this, '_endElement') ); 206 xml_set_character_data_handler( $xml_parser, array(&$this,'_charData') ); 207 208 if( !xml_parse( $xml_parser, $data ) ) 209 { 210 $this->errorStatus = array( 'Unable to parse XML', 211 'Error Code: '.xml_get_error_code( $xml_parser ), 212 'Error Message: '.xml_error_string( xml_get_error_code( $xml_parser ) ) 213 ); 214 xml_parser_free( $xml_parser ); 215 return false; 216 } 217 xml_parser_free( $xml_parser ); 218 219 if( $this->reader != false ) 220 { 221 return $this->reader; 222 } else { 223 $this->errorStatus = array( 'Unidentified feed type.', '', '' ); 224 return false; 225 } 226 } 227 228 function _startElement( $parser, $name, $attributes ) 229 { 230 if( !$this->readerName ) 231 { 232 /* Check for atom */ 233 if( $name == 'FEED' ) 234 { 235 if( array_key_exists('VERSION', $attributes) ) 236 { 237 $this->readerName = 'Atom03'; 238 } else { 239 $this->readerName = 'Atom10'; 240 } 241 $this->readerName = 'Atom03'; 242 } elseif ( $name == 'RSS' ) { 243 if( array_key_exists('VERSION', $attributes) ) 244 { 245 $version = $attributes['VERSION']; 246 } else { 247 $version = 0.91; 248 } 249 if( $version < 1 ) 250 { 251 $this->readerName = 'RSS0x'; 252 } else { 253 $this->readerName = 'RSS20'; 254 } 255 } elseif ( $name = 'RDF:RDF' ) { 256 $this->readerName = 'RDF'; 257 } 258 if( $this->readerName ) 259 { 260 $this->reader = new $this->readerName; 261 //echo( "Made a new {$this->readerName} called {$this->reader} it's a ".get_class($this->reader)." I'm a ".get_class($this) ); 262 } 263 } 264 265 if( $this->reader ) 266 { 267 $this->reader->startElement( $parser, $name, $attributes ); 268 } 269 } 270 271 function _endElement( $parser, $name ) 272 { 273 if( $this->reader ) 274 { 275 $this->reader->endElement( $parser, $name ); 276 } 277 } 278 279 function _charData( $parser, $data ) 280 { 281 if( $this->reader ) 282 { 283 $this->reader->charData( $parser, $data ); 284 } 285 } 286 } 287 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 12:27:40 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |