[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/ -> class.rssparser.inc.php (source)

   1  <?php
   2    /**************************************************************************\
   3    * eGroupWare API - rss parser                                              *
   4    * ------------------------------------------------------------------------ *
   5    * This is not part of eGroupWare, but is used by eGroupWare.               * 
   6    * http://www.egroupware.org/                                               * 
   7    * ------------------------------------------------------------------------ *
   8    * This program is free software; you can redistribute it and/or modify it  *
   9    * under the terms of the GNU Lesser General Public License as published    *
  10    * by the Free Software Foundation; either version 2.1 of the License, or   *
  11    * any later version.                                                       *
  12    \**************************************************************************/
  13  
  14    /* $Id: class.rssparser.inc.php 15134 2004-05-05 12:06:13Z reinerj $ */
  15  
  16  /*
  17   *      rssparse.php3
  18   *
  19   *      Copyright (C) 2000 Jeremey Barrett
  20   *      j@nwow.org
  21   *      http://nwow.org
  22   *
  23   *      Version 0.4
  24   *
  25   *      This library is free software; you can redistribute it and/or modify
  26   *      it under the terms of the GNU Lesser General Public License as published by
  27   *      the Free Software Foundation; either version 2.1 of the License, or
  28   *      (at your option) any later version.
  29   *      
  30   *      This library is distributed in the hope that it will be useful,
  31   *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  32   *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  33   *      GNU Lesser General Public License for more details.
  34   *      
  35   *      You should have received a copy of the GNU Lesser General Public License
  36   *      along with this program; if not, write to the Free Software
  37   *      Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  38   *
  39   *
  40   *      rssparse.php3 is a small PHP script for parsing RDF/RSS XML data. It has been
  41   *      tested with a number of popular web news and information sites such as 
  42   *      slashdot.org, lwn.net, and freshmeat.net. This is not meant to be exhaustive 
  43   *      but merely to provide the basic necessities. It will grow in capabilities 
  44   *      with time, I'm sure.
  45   *
  46   *      This is code I wrote for Nerds WithOut Wires, http://nwow.org.
  47   *
  48   *
  49   *      USAGE:
  50   *      In your PHP script, simply do something akin to this:
  51   *
  52   *      include("rssparse.php3");
  53   *      $fp = fopen("file", "r");
  54   *      $rss = rssparse($fp);
  55   *
  56   *      if (!$rss) {
  57   *          ERROR;
  58   *      }
  59   *
  60   *      while (list(,$item) = each($rss->items)) {
  61   *          printf("Title: %s\n", $item["title"]);
  62   *          printf("Link: %s\n", $item["link"]);
  63   *          printf("Description: %s\n", $item["desc"]);
  64   *      }
  65   *
  66   *      printf("Channel Title: %s\n", $rss->title);
  67   *      printf("Channel Description: %s\n", $rss->desc);
  68   *      printf("Channel Link: %s\n", $rss->link);
  69   *
  70   *      printf("Image Title: %s\n", $rss->image["title"]);
  71   *      printf("Image URL: %s\n", $rss->image["url"]);
  72   *      printf("Image Description: %s\n", $rss->image["desc"]);
  73   *      printf("Image Link: %s\n", $rss->image["link"]);
  74   *
  75   *
  76   *      CHANGES:
  77   *      0.4 - rssparse.php3 now supports the channel image tag and correctly supports
  78   *            RSS-style channels.
  79   *
  80   *
  81   *      BUGS:
  82   *      Width and height tags in image not supported, some other tags not supported
  83   *      yet.
  84   *
  85   * 
  86   *      IMPORTANT NOTE:
  87   *      This requires PHP's XML routines. You must configure PHP with --with-xml.
  88   */
  89  
  90  	function _rssparse_start_elem ($parser, $elem, $attrs)
  91      {
  92          switch($elem)
  93          {
  94              case 'CHANNEL':
  95                  $GLOBALS['_rss']->depth++;
  96                  $GLOBALS['_rss']->state[$GLOBALS['_rss']->depth]    = 'channel';
  97                  $GLOBALS['_rss']->tmptitle[$GLOBALS['_rss']->depth] = '';
  98                  $GLOBALS['_rss']->tmplink[$GLOBALS['_rss']->depth]  = '';
  99                  $GLOBALS['_rss']->tmpdesc[$GLOBALS['_rss']->depth]  = '';
 100                  break;
 101              case 'IMAGE':
 102                  $GLOBALS['_rss']->depth++;
 103                  $GLOBALS['_rss']->state[$GLOBALS['_rss']->depth]    = 'image';
 104                  $GLOBALS['_rss']->tmptitle[$GLOBALS['_rss']->depth] = '';
 105                  $GLOBALS['_rss']->tmplink[$GLOBALS['_rss']->depth]  = '';
 106                  $GLOBALS['_rss']->tmpdesc[$GLOBALS['_rss']->depth]  = '';
 107                  $GLOBALS['_rss']->tmpurl[$GLOBALS['_rss']->depth]   = '';
 108                  break;
 109              case 'ITEM':
 110                  $GLOBALS['_rss']->depth++;
 111                  $GLOBALS['_rss']->state[$GLOBALS['_rss']->depth]    = 'item';
 112                  $GLOBALS['_rss']->tmptitle[$GLOBALS['_rss']->depth] = '';
 113                  $GLOBALS['_rss']->tmplink[$GLOBALS['_rss']->depth]  = '';
 114                  $GLOBALS['_rss']->tmpdesc[$GLOBALS['_rss']->depth]  = '';
 115                  break;
 116              case 'TITLE':
 117                  $GLOBALS['_rss']->depth++;
 118                  $GLOBALS['_rss']->state[$GLOBALS['_rss']->depth] = 'title';
 119                  break;
 120              case 'LINK':
 121                  $GLOBALS['_rss']->depth++;
 122                  $GLOBALS['_rss']->state[$GLOBALS['_rss']->depth] = 'link';
 123                  break;
 124              case 'DESCRIPTION':
 125                  $GLOBALS['_rss']->depth++;
 126                  $GLOBALS['_rss']->state[$GLOBALS['_rss']->depth] = 'desc';
 127                  break;
 128              case 'URL':
 129                  $GLOBALS['_rss']->depth++;
 130                  $GLOBALS['_rss']->state[$GLOBALS['_rss']->depth] = 'url';
 131                  break;
 132          }
 133      }
 134  
 135  	function _rssparse_end_elem ($parser, $elem)
 136      {
 137          switch ($elem)
 138          {
 139              case 'CHANNEL':
 140                  $GLOBALS['_rss']->set_channel(
 141                      $GLOBALS['_rss']->tmptitle[$GLOBALS['_rss']->depth],
 142                      $GLOBALS['_rss']->tmplink[$GLOBALS['_rss']->depth],
 143                      $GLOBALS['_rss']->tmpdesc[$GLOBALS['_rss']->depth]
 144                  );
 145                  $GLOBALS['_rss']->depth--;
 146                  break;
 147              case 'IMAGE':
 148                  $GLOBALS['_rss']->set_image(
 149                      $GLOBALS['_rss']->tmptitle[$GLOBALS['_rss']->depth],
 150                      $GLOBALS['_rss']->tmplink[$GLOBALS['_rss']->depth],
 151                      $GLOBALS['_rss']->tmpdesc[$GLOBALS['_rss']->depth],
 152                      $GLOBALS['_rss']->tmpurl[$GLOBALS['_rss']->depth]
 153                  );
 154                  $GLOBALS['_rss']->depth--;
 155                  break;
 156              case 'ITEM':
 157                  $GLOBALS['_rss']->add_item(
 158                      $GLOBALS['_rss']->tmptitle[$GLOBALS['_rss']->depth],
 159                      $GLOBALS['_rss']->tmplink[$GLOBALS['_rss']->depth],
 160                      $GLOBALS['_rss']->tmpdesc[$GLOBALS['_rss']->depth]
 161                  );
 162                  $GLOBALS['_rss']->depth--;
 163                  break;
 164              case 'TITLE':
 165                  $GLOBALS['_rss']->depth--;
 166                  break;
 167              case 'LINK':
 168                  $GLOBALS['_rss']->depth--;
 169                  break;
 170              case 'DESCRIPTION':
 171                  $GLOBALS['_rss']->depth--;
 172                  break;
 173              case 'URL':
 174                  $GLOBALS['_rss']->depth--;
 175                  break;
 176          }
 177      }
 178  
 179  	function _rssparse_elem_data ($parser, $data)
 180      {
 181          switch ($GLOBALS['_rss']->state[$GLOBALS['_rss']->depth])
 182          {
 183              case 'title':
 184                  $GLOBALS['_rss']->tmptitle[($GLOBALS['_rss']->depth - 1)] .= $data;
 185                  break;
 186              case 'link';
 187                  $GLOBALS['_rss']->tmplink[($GLOBALS['_rss']->depth - 1)] .= $data;
 188                  break;
 189              case 'desc':
 190                  $GLOBALS['_rss']->tmpdesc[($GLOBALS['_rss']->depth - 1)] .= $data;
 191                  break;
 192              case 'url':
 193                  $GLOBALS['_rss']->tmpurl[($GLOBALS['_rss']->depth - 1)] .= $data;
 194                  break;
 195          }
 196      }
 197  
 198      class rssparser
 199      {
 200          var $title;
 201          var $link;
 202          var $desc;
 203          var $items = array();
 204          var $nitems;
 205          var $image = array();
 206          var $state = array();
 207          var $tmptitle = array();
 208          var $tmplink = array();
 209          var $tmpdesc = array();
 210          var $tmpurl = array();
 211          var $depth;
 212  
 213  		function rssparser()
 214          {
 215              $this->nitems = 0;
 216              $this->depth  = 0;
 217          }
 218  
 219  		function set_channel($in_title, $in_link, $in_desc)
 220          {
 221              $this->title = $in_title;
 222              $this->link  = $in_link;
 223              $this->desc  = $in_desc;
 224          }
 225  
 226  		function set_image($in_title, $in_link, $in_desc, $in_url)
 227          {
 228              $this->image['title'] = $in_title;
 229              $this->image['link']  = $in_link;
 230              $this->image['desc']  = $in_desc;
 231              $this->image['url']   = $in_url;
 232          }
 233  
 234  		function add_item($in_title, $in_link, $in_desc)
 235          {
 236              $this->items[$this->nitems]['title'] = $in_title;
 237              $this->items[$this->nitems]['link']  = $in_link;
 238              $this->items[$this->nitems]['desc']  = $in_desc;
 239              $this->nitems++;
 240          }
 241  
 242  		function parse($fp)
 243          {
 244              $xml_parser = xml_parser_create();
 245  
 246              xml_set_element_handler($xml_parser, '_rssparse_start_elem', '_rssparse_end_elem');
 247              xml_set_character_data_handler($xml_parser, '_rssparse_elem_data');
 248  
 249              while ($data = fread($fp, 4096))
 250              {
 251                  if (!xml_parse($xml_parser, $data, feof($fp)))
 252                  {
 253                      return 1;
 254                  }
 255              }
 256  
 257              xml_parser_free($xml_parser);
 258  
 259              return 0;
 260          }
 261      }
 262  
 263  	function rssparse ($fp)
 264      {
 265          $GLOBALS['_rss'] = new rssparser();
 266  
 267          if ($GLOBALS['_rss']->parse($fp))
 268          {
 269              return 0;
 270          }
 271  
 272          return $GLOBALS['_rss'];
 273      }
 274  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7