| [ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 /****************************************************************************/ 3 /* atom.feed.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 // $Id: atom.feed.class.php,v 1.11 2006/05/18 20:27:31 mjervis Exp $ 30 31 /** 32 * Provides feed handlers for Atom 0.3 and Atom 1.0 33 * 34 * @author Michael Jervis (mike@fuckingbrit.com) 35 * @version 1.1 36 */ 37 38 /** 39 * atom03 provides reading and writing of Atom 0.3 format syndication feeds. 40 * 41 * @author Michael Jervis (mike@fuckingbrit.com) 42 * @copyright Michael Jervis 2004 43 * @abstract 44 */ 45 class Atom03 extends FeedParserBase 46 { 47 /** 48 * @access private 49 * @var boolean 50 */ 51 var $_inItem; 52 53 /** 54 * @access private 55 * @var array 56 */ 57 var $_currentItem; 58 59 function Atom03() 60 { 61 $this->FeedParserBase(); 62 $this->_inItem = false; 63 } 64 65 function _RFC3339Date($timestamp='') 66 { 67 $return = ''; 68 if( !empty($timestamp) ) 69 { 70 $return = strftime("%Y-%m-%dT%T", $timestamp); 71 $suffix = strftime("%z", $timestamp); 72 } else { 73 $return = strftime("%Y-%m-%dT%T"); 74 $suffix = strftime("%z"); 75 } 76 return $return.substr($suffix,0,3).':'.substr($suffix,3,2); 77 } 78 79 /** 80 * Format an article into an Atom 0.3 <entry> tag. 81 * 82 * Takes an associative article array and turns it into an XML definition 83 * of an article. Uses merely title, link and summary. 84 * 85 * @param array $article Associative array describing an article. 86 */ 87 function _formatArticle( $article ) 88 { 89 $xml = "<entry>\n"; 90 $xml .= '<title mode="escaped">'.$this->_safeXML( $article['title'] )."</title>\n"; 91 $xml .= '<link rel="alternate" type="text/html" href="'.$this->_safeXML( $article['link'] )."\"/>\n"; 92 $xml .= '<id>'.htmlspecialchars( $article['link'] )."</id>\n"; 93 $xml .= '<issued>'.$this->_RFC3339Date( $article['date'] )."</issued>\n"; 94 $xml .= '<modified>'.$this->_RFC3339Date( $article['date'] )."</modified>\n"; 95 if( array_key_exists( 'author', $article ) ) 96 { 97 $xml .= "<author>\n<name>{$article['author']}</name>\n</author>\n"; 98 } 99 if( array_key_exists( 'summary', $article ) ) 100 { 101 if( !empty( $article['summary'] ) ) 102 { 103 $xml .= '<content type="text/html" mode="escaped">' 104 . $this->_safeXML ($article['summary']) 105 . "</content>\n"; 106 } 107 } 108 if( is_array( $this->extensions ) ) 109 { 110 foreach( $this->extensions as $extendingTag ) 111 { 112 $xml .= "$extendingTag\n"; 113 } 114 } 115 $xml .= "</entry>\n"; 116 return $xml; 117 } 118 119 /** 120 * Return the formatted start of a feed. 121 * 122 * This will start the xml and create header information about the feed 123 * itself. 124 */ 125 function _feedHeader() 126 { 127 $xml = '<?xml version="1.0" encoding="' . $this->encoding . '" ?>' . LB . LB 128 . '<feed version="0.3" xmlns="http://purl.org/atom/ns#"'.$this->_injectNamespaces().'>' . LB 129 . '<title mode="escaped">' . $this->_safeXML( $this->title ) . '</title>' . LB 130 . '<tagline mode="escaped">' . $this->_safeXML( $this->description ) . '</tagline>' . LB 131 . '<link rel="alternate" type="text/html" href="' . $this->_safeXML( $this->sitelink ) . '"/>' . LB 132 . '<modified>'.$this->_RFC3339Date().'</modified>' . LB 133 . "<author>\n<name>" . $this->_safeXML( $this->title ) . '</name>' . LB 134 . '<email>' . $this->_safeXML( $this->sitecontact ) . "</email>\n</author>\n"; 135 if( is_array( $this->extensions ) ) 136 { 137 foreach( $this->extensions as $extendingTag ) 138 { 139 $xml .= "$extendingTag\n"; 140 } 141 } 142 return $xml; 143 } 144 145 /** 146 * Return the formatted end of a feed. 147 * 148 * just closes things off nicely. 149 */ 150 function _feedFooter() 151 { 152 $xml = "</feed>\n"; 153 return $xml; 154 } 155 156 /** 157 * Handle the begining of an XML element 158 * 159 * This is called from the parserfactory once the type of data has been 160 * determined. Standard XML_PARSER element handler. 161 * 162 * @author Michael Jervis (mike@fuckingbrit.com) 163 * @copyright Michael Jervis 2004 164 */ 165 function startElement($parser, $name, $attributes) 166 { 167 if( $name == 'ENTRY' ) 168 { 169 $this->_inItem = true; 170 $this->_currentItem = array(); 171 } else if( $this->_inItem ) { 172 if( $name == 'LINK' ) 173 { 174 $this->_currentItem['link'] = $attributes['HREF']; 175 } 176 } else { 177 if( $name == 'LINK' ) 178 { 179 $this->sitelink = $attributes['HREF']; 180 } 181 } 182 $this->_currentTag = $name; 183 } 184 185 /** 186 * Handle the close of an XML element 187 * 188 * Called by the parserfactory during parsing. 189 */ 190 function endElement($parser, $name) 191 { 192 if( $name == 'ENTRY' ) 193 { 194 $this->_inItem = false; 195 $this->articles[] = $this->_currentItem; 196 } 197 $this->_currentTag = ''; 198 } 199 200 /** 201 * Handles character data. 202 * 203 * Called by the parserfactory during parsing. 204 */ 205 function charData($parser, $data) 206 { 207 if( $this->_inItem ) 208 { 209 if( $this->_currentTag == 'TITLE' ) 210 { 211 if( empty( $this->_currentItem['title'] ) ) 212 { 213 $this->_currentItem['title'] = $data; 214 } else { 215 $this->_currentItem['title'] .= $data; 216 } 217 } else if( $this->_currentTag == 'CONTENT' ) { 218 if( empty( $this->_currentItem['summary'] ) ) 219 { 220 $this->_currentItem['summary'] = $data; 221 } else { 222 $this->_currentItem['summary'] .= $data; 223 } 224 } else if( $this->_currentTag == 'MODIFIED' ) { 225 $this->_currentItem['date'] = $data; 226 } else if( $this->_currentTag == 'ISSUED' ) { 227 if( empty( $this->currentItem['date'] ) ) 228 { 229 $this->currentITem['date'] = $data; 230 } 231 } 232 } else { 233 if( $this->_currentTag == 'TITLE' ) 234 { 235 $this->title .= $data; 236 } else if( $this->_currentTag == 'TAGLINE' ) { 237 $this->description .= $data; 238 } 239 } 240 } 241 } 242 243 /** 244 * atom10 provides reading and writing of Atom 1.0 format syndication feeds. 245 * 246 * @author Michael Jervis (mike@fuckingbrit.com) 247 * @copyright Michael Jervis 2005 248 * @abstract 249 */ 250 class Atom10 extends FeedParserBase 251 { 252 /** 253 * @access private 254 * @var boolean 255 */ 256 var $_inItem; 257 258 /** 259 * @access private 260 * @var array 261 */ 262 var $_currentItem; 263 264 function Atom10() 265 { 266 $this->FeedParserBase(); 267 $this->_inItem = false; 268 } 269 270 /** 271 * Format an article into an Atom 0.3 <entry> tag. 272 * 273 * Takes an associative article array and turns it into an XML definition 274 * of an article. Uses merely title, link and summary. 275 * 276 * @param array $article Associative array describing an article. 277 */ 278 function _formatArticle( $article ) 279 { 280 $xml = "<entry>\n"; 281 $xml .= '<title type="html">'.$this->_safeXML( $article['title'] )."</title>\n"; 282 $xml .= '<link rel="alternate" type="text/html" href="'.$this->_safeXML( $article['link'] )."\"/>\n"; 283 $xml .= '<id>' . $this->_createId($article['link'], $article['date']) . "</id>\n"; 284 $xml .= '<published>'.$this->_RFC3339Date( $article['date'] )."</published>\n"; 285 $xml .= '<updated>'.$this->_RFC3339Date( $article['date'] )."</updated>\n"; 286 if( array_key_exists( 'author', $article ) ) 287 { 288 $xml .= "<author>\n<name>{$article['author']}</name>\n</author>\n"; 289 } 290 if( array_key_exists( 'summary', $article ) ) 291 { 292 if( !empty( $article['summary'] ) ) 293 { 294 $xml .= '<content type="html">' 295 . $this->_safeXML ($article['summary']) 296 . "</content>\n"; 297 } 298 } 299 if( is_array( $this->extensions ) ) 300 { 301 foreach( $this->extensions as $extendingTag ) 302 { 303 $xml .= "$extendingTag\n"; 304 } 305 } 306 $xml .= "</entry>\n"; 307 return $xml; 308 } 309 310 function _createId($url, $date) 311 { 312 $start = strpos($url, '/') + 2; 313 $end = strpos($url, '/', $start); 314 $tag = 'tag:'.substr($url, $start, $end-$start); 315 $tag .= strftime(",%Y-%m-%d", $date).':'.substr($url, $end); 316 return $tag; 317 } 318 319 function _RFC3339Date($timestamp='') 320 { 321 $return = ''; 322 if( !empty($timestamp) ) 323 { 324 $return = strftime("%Y-%m-%dT%T", $timestamp); 325 $suffix = strftime("%z", $timestamp); 326 } else { 327 $return = strftime("%Y-%m-%dT%T"); 328 $suffix = strftime("%z"); 329 } 330 return $return.substr($suffix,0,3).':'.substr($suffix,3,2); 331 } 332 333 /** 334 * Return the formatted start of a feed. 335 * 336 * This will start the xml and create header information about the feed 337 * itself. 338 */ 339 function _feedHeader() 340 { 341 $xml = '<?xml version="1.0" encoding="' . $this->encoding . '" ?>' . LB . LB 342 . '<feed xmlns="http://www.w3.org/2005/Atom"'.$this->_injectNamespaces().'>' . LB 343 . '<title type="text">' . $this->_safeXML( $this->title ) . '</title>' . LB 344 . '<subtitle type="text">' . $this->_safeXML( $this->description ) . '</subtitle>' . LB 345 . '<link rel="self" href="' . $this->_safeXML( $this->url ) . '"/>' . LB 346 . '<link rel="alternate" type="text/html" href="' . $this->_safeXML( $this->sitelink ) . '/"/>' . LB; 347 if ($this->feedlogo != '') 348 { 349 $xml .= '<logo>' . $this->_safeXML( $this->feedlogo ) . '</logo>' . LB; 350 } 351 $xml .= '<id>' . $this->_safeXML( $this->sitelink ) . '/</id>' . LB 352 . '<updated>'.$this->_RFC3339Date().'</updated>' . LB 353 . "<author>\n<name>" . $this->_safeXML( $this->title ) . '</name>' . LB 354 . '<email>' . $this->_safeXML( $this->sitecontact ) . "</email>\n</author>\n"; 355 if( is_array( $this->extensions ) ) 356 { 357 foreach( $this->extensions as $extendingTag ) 358 { 359 $xml .= "$extendingTag\n"; 360 } 361 } 362 return $xml; 363 } 364 365 /** 366 * Return the formatted end of a feed. 367 * 368 * just closes things off nicely. 369 */ 370 function _feedFooter() 371 { 372 $xml = "</feed>\n"; 373 return $xml; 374 } 375 376 /** 377 * Handle the begining of an XML element 378 * 379 * This is called from the parserfactory once the type of data has been 380 * determined. Standard XML_PARSER element handler. 381 * 382 * @author Michael Jervis (mike@fuckingbrit.com) 383 * @copyright Michael Jervis 2004 384 */ 385 function startElement($parser, $name, $attributes) 386 { 387 if( $name == 'ENTRY' ) 388 { 389 $this->_inItem = true; 390 $this->_currentItem = array(); 391 } else if( $this->_inItem ) { 392 if( $name == 'LINK' ) 393 { 394 $this->_currentItem['link'] = $attributes['HREF']; 395 } 396 } else { 397 if( $name == 'LINK' ) 398 { 399 $this->sitelink = $attributes['HREF']; 400 } 401 } 402 $this->_currentTag = $name; 403 } 404 405 /** 406 * Handle the close of an XML element 407 * 408 * Called by the parserfactory during parsing. 409 */ 410 function endElement($parser, $name) 411 { 412 if( $name == 'ENTRY' ) 413 { 414 $this->_inItem = false; 415 $this->articles[] = $this->_currentItem; 416 } 417 $this->_currentTag = ''; 418 } 419 420 /** 421 * Handles character data. 422 * 423 * Called by the parserfactory during parsing. 424 */ 425 function charData($parser, $data) 426 { 427 if( $this->_inItem ) 428 { 429 if( $this->_currentTag == 'TITLE' ) 430 { 431 if( empty( $this->_currentItem['title'] ) ) 432 { 433 $this->_currentItem['title'] = $data; 434 } else { 435 $this->_currentItem['title'] .= $data; 436 } 437 } else if( $this->_currentTag == 'CONTENT' ) { 438 if( empty( $this->_currentItem['summary'] ) ) 439 { 440 $this->_currentItem['summary'] = $data; 441 } else { 442 $this->_currentItem['summary'] .= $data; 443 } 444 } else if( $this->_currentTag == 'UPDATED' ) { 445 $this->_currentItem['date'] = $data; 446 } else if( $this->_currentTag == 'PUBLISHED' ) { 447 if( empty( $this->currentItem['date'] ) ) 448 { 449 $this->currentITem['date'] = $data; 450 } 451 } 452 } else { 453 if( $this->_currentTag == 'TITLE' ) 454 { 455 $this->title .= $data; 456 } else if( $this->_currentTag == 'SUBTITLE' ) { 457 $this->description .= $data; 458 } 459 } 460 } 461 }
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 |
|