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