[ Index ]
 

Code source de GeekLog 1.4.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/system/classes/syndication/ -> rss.feed.class.php (source)

   1  <?php
   2    /****************************************************************************/
   3    /* rss.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  
  30    /**
  31      * Provides feed handlers for RSS 0.9x and RSS 2.0
  32      *
  33      * This library file provides multiple class definitions for dealing with
  34      * variants of the RSS syndication format. We will <b>not</b> handle RSS 1.0
  35      * however, that is RDF and handled in a seperate case. This is purely for
  36      * the original RSS and the 2.0 that was created to deal with the fact
  37      * that RDF was overkill for the original purpose of RSS.
  38      *
  39      * @author Michael Jervis (mike@fuckingbrit.com)
  40      * @version 1.0
  41      */
  42  
  43    /**
  44     * rss0x provides reading and writing of RSS 0.91 format syndication feeds.
  45     *
  46     * @author Michael Jervis (mike@fuckingbrit.com)
  47     * @copyright Michael Jervis 2004
  48     * @abstract
  49     */
  50    class RSS0x extends FeedParserBase
  51    {
  52      /**
  53        * @access private
  54        * @var boolean
  55        */
  56      var $_inItem;
  57  
  58      /**
  59        * @access private
  60        * @var array
  61        */
  62      var $_currentItem;
  63  
  64      function RSS0x()
  65      {
  66        $this->FeedParserBase();
  67        $this->_inItem = false;
  68      }
  69  
  70      /**
  71        * Format an article into an RSS 0.91 <item> tag.
  72        *
  73        * Takes an associative article array and turns it into an XML definition
  74        * of an article. Uses merely title, link and summary.
  75        *
  76        * @param array $article Associative array describing an article.
  77        */
  78      function _formatArticle( $article )
  79      {
  80        $xml = "<item>\n";
  81        $xml .= '<title>'.$this->_safeXML( $article['title'] )."</title>\n";
  82        $xml .= '<link>'.$this->_safeXML( $article['link'] )."</link>\n";
  83        if( array_key_exists( 'summary', $article ) )
  84        {
  85          if( strlen( $article['summary'] ) > 0 )
  86          {
  87            $xml .= '<description>'.$this->_safeXML( $article['summary'] )."</description>\n";
  88          }
  89        }
  90  
  91        if( is_array($article['extensions']) )
  92        {
  93          foreach( $article['extensions'] as $extensionTag )
  94          {
  95              $xml .= "$extensionTag\n";
  96          }
  97        }
  98        $xml .= "</item>\n";
  99        return $xml;
 100      }
 101  
 102      /**
 103        * Return the formatted start of a feed.
 104        *
 105        * This will start the xml and create header information about the feed
 106        * itself.
 107        */
 108      function _feedHeader()
 109      {
 110        $xml = "<?xml version=\"1.0\" encoding=\"{$this->encoding}\"?>\n\n";
 111        $xml .= "<rss version=\"0.91\"".$this->_injectNamespaces().">\n<channel>\n";
 112        $xml .= "<title>".$this->_safeXML( $this->title )."</title>\n";
 113        $xml .= "<link>".$this->_safeXML( $this->sitelink )."</link>\n";
 114        if( strlen( $this->description ) > 0 )
 115        {
 116          $xml .= "<description>".$this->_safeXML( $this->description )."</description>\n";
 117        }
 118        $xml .= "<language>{$this->lang}</language>\n";
 119        if( strlen($this->feedlogo) > 0 )
 120        {
 121          $xml .= "<image>\n";
 122          $xml .= '<url>'.$this->_safeXML( $this->feedlogo )."</url>\n";
 123          $xml .= '<title>'.$this->_safeXML( $this->title )."</title>\n";
 124          $xml .= '<link>'.$this->_safeXML( $this->sitelink )."</link>\n";
 125          $xml .= "</image>\n";
 126        }
 127        foreach( $this->extensions as $extendingTag )
 128        {
 129          $xml .= $extendingTag."\n";
 130        }
 131        return $xml;
 132      }
 133  
 134      /**
 135        * Return the formatted end of a feed.
 136        *
 137        * just closes things off nicely.
 138        */
 139      function _feedFooter()
 140      {
 141        $xml = "</channel>\n</rss>\n";
 142        return $xml;
 143      }
 144  
 145      /**
 146        * Handle the begining of an XML element
 147        *
 148        * This is called from the parserfactory once the type of data has been
 149        * determined. Standard XML_PARSER element handler.
 150        *
 151        * @author Michael Jervis (mike@fuckingbrit.com)
 152        * @copyright Michael Jervis 2004
 153        */
 154      function startElement($parser, $name, $attributes)
 155      {
 156        if( $name == 'ITEM' )
 157        {
 158          $this->_inItem = true;
 159          $this->_currentItem = array();
 160        }
 161        $this->_currentTag = $name;
 162      }
 163  
 164      /**
 165        * Handle the close of an XML element
 166        *
 167        * Called by the parserfactory during parsing.
 168        */
 169      function endElement($parser, $name)
 170      {
 171        if( $name == 'ITEM' )
 172        {
 173          $this->_inItem = false;
 174          $this->articles[] = $this->_currentItem;
 175        }
 176        $this->_currentTag = '';
 177      }
 178  
 179      /**
 180        * Handles character data.
 181        *
 182        * Called by the parserfactory during parsing.
 183        */
 184      function charData($parser, $data)
 185      {
 186        if( $this->_inItem )
 187        {
 188          if( $this->_currentTag == 'TITLE' )
 189          {
 190            if( empty( $this->_currentItem['title'] ) )
 191            {
 192              $this->_currentItem['title'] = $data;
 193            } else {
 194              $this->_currentItem['title'] .= $data;
 195            }
 196          } else if( $this->_currentTag == 'LINK' ) {
 197            if( empty( $this->_currentItem['link'] ) )
 198            {
 199              $this->_currentItem['link'] = $data;
 200            } else {
 201              $this->_currentItem['link'] .= $data;
 202            }
 203          } else if( $this->_currentTag == 'DESCRIPTION' ) {
 204            if( empty( $this->_currentItem['summary'] ) )
 205            {
 206              $this->_currentItem['summary'] = $data;
 207            } else {
 208              $this->_currentItem['summary'] .= $data;
 209            }
 210          }
 211        } else {
 212          if( $this->_currentTag == 'TITLE' )
 213          {
 214            $this->title .= $data;
 215          } else if( $this->_currentTag == 'LINK' ) {
 216            $this->sitelink .= $data;
 217          } else if( $this->_currentTag == 'DESCRIPTION' ) {
 218            $this->description .= $data;
 219          }
 220        }
 221      }
 222    }
 223  
 224  
 225    /**
 226     * rss20 provides reading and writing of RSS 2.0 format syndication feeds.
 227     *
 228     * @author Michael Jervis (mike@fuckingbrit.com)
 229     * @copyright Michael Jervis 2004
 230     * @abstract
 231     */
 232    class RSS20 extends FeedParserBase
 233    {
 234      /**
 235        * @access private
 236        * @var boolean
 237        */
 238      var $_inItem;
 239  
 240      /**
 241        * @access private
 242        * @var array
 243        */
 244      var $_currentItem;
 245  
 246      /**
 247        * Date of feed, for reading only
 248        */
 249      var $date;
 250  
 251      /**
 252        * is the guid element a permalink?
 253        */
 254      var $_permaLink;
 255  
 256      /**
 257        * Have we used GUID for link?
 258        */
 259      var $_linkGUID;
 260  
 261      function RSS20()
 262      {
 263        $this->FeedParserBase();
 264        $this->_inItem = false;
 265        $this->_linkGUID = false;
 266      }
 267  
 268      /**
 269        * Generate an RFC-822 compliant date-time stamp.
 270        *
 271        * @param timestamp $timestamp Date time to format.
 272        */
 273      function _RFC822DateFormat($timestamp='')
 274      {
 275          // Cache the current timezone:
 276          $cache_timezone = setlocale(LC_TIME,0);
 277          // Time locales that will give us the required output
 278          $timezones = array('en_GB','en_US');
 279          setlocale(LC_TIME,$timezones); // set to whichever the system supports.
 280          // format the date
 281          if(!empty($timestamp))
 282          {
 283              $time = strftime( '%a, %d %b %Y %H:%M:%S %z', $timestamp);
 284          } else {
 285              $time = strftime( '%a, %d %b %Y %H:%M:%S %z' );
 286          }
 287          // Set the timezone back
 288          setlocale(LC_TIME, $cache_timezone);
 289          // return the time
 290          return $time;
 291      }
 292  
 293      /**
 294        * Format an article into an RSS 2.0 <item> tag.
 295        *
 296        * Takes an associative article array and turns it into an XML definition
 297        * of an article. Uses merely title, link and summary.
 298        *
 299        * @param array $article Associative array describing an article.
 300        */
 301      function _formatArticle( $article )
 302      {
 303        $xml = "<item>\n";
 304        $xml .= '<title>'.$this->_safeXML( $article['title'] )."</title>\n";
 305        $xml .= '<link>'.$this->_safeXML( $article['link'] )."</link>\n";
 306        $xml .= '<guid isPermaLink="true">'.$this->_safeXML( $article['link'] )."</guid>\n";
 307        $xml .= '<pubDate>'.$this->_RFC822DateFormat( $article['date'] )."</pubDate>\n";
 308        if( array_key_exists( 'commenturl', $article ) )
 309        {
 310          $xml .= '<comments>'.$this->_safeXML( $article['commenturl'] )."</comments>\n";
 311        }
 312        if( array_key_exists( 'topic', $article ) )
 313        {
 314          $topic = $article['topic'];
 315          if( is_array( $topic ))
 316          {
 317            foreach( $topic as $subject )
 318            {
 319              $xml .= '<dc:subject>'.$this->_safeXML( $subject )."</dc:subject>\n";
 320            }
 321          }
 322          else
 323          {
 324            $xml .= '<dc:subject>'.$this->_safeXML( $topic )."</dc:subject>\n";
 325          }
 326        }
 327        if( array_key_exists( 'summary', $article ) )
 328        {
 329          if( strlen( $article['summary'] ) > 0 )
 330          {
 331            $xml .= '<description>'.$this->_safeXML( $article['summary'] )."</description>\n";
 332          }
 333        }
 334        if( isset($article['extensions']) &&  is_array($article['extensions']) )
 335        {
 336          foreach( $article['extensions'] as $extensionTag )
 337          {
 338              $xml .= "$extensionTag\n";
 339          }
 340        }
 341        $xml .= "</item>\n";
 342        return $xml;
 343      }
 344  
 345      /**
 346        * Return the formatted start of a feed.
 347        *
 348        * This will start the xml and create header information about the feed
 349        * itself.
 350        */
 351      function _feedHeader()
 352      {
 353        global $_CONF;
 354  
 355        if( !is_array($this->namespaces) )
 356        {
 357          $this->namespaces = array();
 358        }
 359        $this->namespaces[] = 'xmlns:dc="http://purl.org/dc/elements/1.1/"';
 360  
 361        $xml = "<?xml version=\"1.0\" encoding=\"{$this->encoding}\"?>\n\n";
 362        $xml .= "<rss version=\"2.0\"".$this->_injectNamespaces().">\n";
 363        $xml .= "<channel>\n";
 364        $xml .= "<title>".$this->_safeXML( $this->title )."</title>\n";
 365        $xml .= "<link>".$this->_safeXML( $this->sitelink )."</link>\n";
 366        if( strlen( $this->description ) > 0 )
 367        {
 368          $xml .= "<description>".$this->_safeXML( $this->description )."</description>\n";
 369        }
 370        if( strlen($this->sitecontact) > 0 )
 371        {
 372          $xml .= '<managingEditor>'.$this->_safeXML( $this->sitecontact )."</managingEditor>\n";
 373          $xml .= '<webMaster>'.$this->_safeXML( $this->sitecontact )."</webMaster>\n";
 374        }
 375        if( strlen($this->copyright) > 0 )
 376        {
 377          $xml .= '<copyright>'.$this->_safeXML( $this->copyright )."</copyright>\n";
 378        }
 379        if( strlen($this->system) > 0 )
 380        {
 381          $xml .= '<generator>'.$this->_safeXML( $this->system )."</generator>\n";
 382        }
 383        $xml .= '<pubDate>'.$this->_RFC822DateFormat()."</pubDate>\n";
 384        $xml .= "<language>{$this->lang}</language>\n";
 385  
 386        if( strlen($this->feedlogo) > 0 )
 387        {
 388          $xml .= "<image>\n";
 389          $xml .= '<url>'.$this->_safeXML( $this->feedlogo )."</url>\n";
 390          $xml .= '<title>'.$this->_safeXML( $this->title )."</title>\n";
 391          $xml .= '<link>'.$this->_safeXML( $this->sitelink )."</link>\n";
 392          $xml .= "</image>\n";
 393        }
 394        foreach( $this->extensions as $extendingTag )
 395        {
 396          $xml .= $extendingTag."\n";
 397        }
 398        return $xml;
 399      }
 400  
 401      /**
 402        * Return the formatted end of a feed.
 403        *
 404        * just closes things off nicely.
 405        */
 406      function _feedFooter()
 407      {
 408        $xml = "</channel>\n</rss>\n";
 409        return $xml;
 410      }
 411  
 412      /**
 413        * Handle the begining of an XML element
 414        *
 415        * This is called from the parserfactory once the type of data has been
 416        * determined. Standard XML_PARSER element handler.
 417        *
 418        * @author Michael Jervis (mike@fuckingbrit.com)
 419        * @copyright Michael Jervis 2004
 420        */
 421      function startElement($parser, $name, $attributes)
 422      {
 423        $this->_currentTag = $name;
 424        if( $name == 'ITEM' )
 425        {
 426          $this->_inItem = true;
 427          $this->_currentItem = array();
 428          $this->_permaLink = false;
 429        } else if( ($name == 'GUID') && array_key_exists('ISPERMALINK', $attributes) ) {
 430          if( $attributes['ISPERMALINK'] == 'true' )
 431          {
 432            $this->_permaLink = true;
 433          } else {
 434            $this->_permaLink = false;
 435          }
 436        } else {
 437          $this->_permaLink = false;
 438        }
 439  
 440      }
 441  
 442      /**
 443        * Handle the close of an XML element
 444        *
 445        * Called by the parserfactory during parsing.
 446        */
 447      function endElement($parser, $name)
 448      {
 449        if( $name == 'ITEM' )
 450        {
 451          $this->_inItem = false;
 452          $this->articles[] = $this->_currentItem;
 453        } elseif( $name == 'GUID' && $this->_permaLink ) {
 454          /* if we have a guid that is ALSO a permalink, override link with it */
 455          $this->_currentItem['link'] = $this->_currentItem['guid'];
 456          $this->_linkGUID = true;
 457        }
 458        $this->_currentTag = '';
 459      }
 460  
 461      /**
 462        * Handles character data.
 463        *
 464        * Called by the parserfactory during parsing.
 465        */
 466      function charData($parser, $data)
 467      {
 468        if( $this->_inItem )
 469        {
 470          if( $this->_currentTag == 'TITLE' )
 471          {
 472            if( empty( $this->_currentItem['title'] ) )
 473            {
 474              $this->_currentItem['title'] = $data;
 475            } else {
 476              $this->_currentItem['title'] .= $data;
 477            }
 478          } else if( $this->_currentTag == 'LINK' ) {
 479            if( !$this->_linkGUID )
 480            {
 481                if( empty( $this->_currentItem['link'] ) )
 482                {
 483                  $this->_currentItem['link'] = $data;
 484                } else {
 485                  $this->_currentItem['link'] .= $data;
 486                }
 487            }
 488          } else if( $this->_currentTag == 'DESCRIPTION' ) {
 489            if( empty( $this->_currentItem['summary'] ) )
 490            {
 491              $this->_currentItem['summary'] = $data;
 492            } else {
 493              $this->_currentItem['summary'] .= $data;
 494            }
 495          } else if( $this->_currentTag == 'PUBDATE' ) {
 496            $this->_currentItem['date'] = $data;
 497          } else if( $this->_currentTag == 'GUID' ) {
 498            if( empty( $this->_currentItem['guid'] ) )
 499            {
 500              $this->_currentItem['guid'] = $data;
 501            } else {
 502              $this->_currentItem['guid'] .= $data;
 503            }
 504          }
 505        } else {
 506          if( $this->_currentTag == 'TITLE' )
 507          {
 508            $this->title .= $data;
 509          } else if( $this->_currentTag == 'LINK' ) {
 510            $this->sitelink .= $data;
 511          } else if( $this->_currentTag == 'DESCRIPTION' ) {
 512            $this->description .= $data;
 513          } else if( $this->_currentTag == 'MANAGINGEDITOR' ) {
 514            $this->sitecontact .= $data;
 515          } else if( $this->_currentTag == 'COPYRIGHT' ) {
 516            $this->copyright .= $data;
 517          } else if( $this->_currentTag == 'PUBDATE' ) {
 518            $this->date .= $data;
 519          }
 520        }
 521      }
 522    }
 523  ?>


Généré le : Wed Nov 21 12:27:40 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics