2 rcs_id('$Id: RssParser.php,v 1.4 2004-04-18 01:11:51 rurban Exp $');
4 * RSSParser Class, requires the expat extension
5 * Based on Duncan Gough RSSParser class
6 * Copyleft Arnaud Fontaine
9 This file is part of PhpWiki.
11 PhpWiki is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 PhpWiki is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with PhpWiki; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * 2004-04-09 16:30:50 rurban:
28 * added fsockopen allow_url_fopen = Off workaround
29 * 2004-04-12 20:04:12 rurban:
30 * fixes for IMAGE element (sf.net)
36 var $description = "";
37 var $inside_item = false;
40 var $channel = array();
44 function startElement($parser, $name, $attrs=''){
49 $this->inside_item = true;
50 elseif ($name == "IMAGE")
51 $this->inside_item = true;
54 function endElement($parser, $tagName, $attrs=''){
57 if ($tagName == "ITEM") {
58 $this->items[] = array("title" => $this->item['TITLE'],
59 "description" => $this->item['DESCRIPTION'],
60 "link" => $this->item['LINK']);
61 $this->item['TITLE'] = "";
62 $this->item['DESCRIPTION'] = "";
63 $this->item['LINK'] = "";
64 $this->inside_item = false;
65 } elseif ($tagName == "IMAGE") {
66 $this->item['TITLE'] = "";
67 $this->item['DESCRIPTION'] = "";
68 $this->item['LINK'] = "";
69 $this->inside_item = false;
70 } elseif ($tagName == "CHANNEL") {
71 $this->channel = array("title" => $this->title,
72 "description" => $this->description,
73 "link" => $this->link,
74 "date" => $this->date,
75 "divers" => $this->divers);
77 $this->description = "";
84 function characterData($parser, $data){
87 if ($this->inside_item) {
88 if (empty($this->item[$current_tag]))
89 $this->item[$current_tag] = '';
90 if ($current_tag == 'LINK') {
92 $this->item[$current_tag] = trim($data);
94 $this->item[$current_tag] .= trim($data);
97 switch ($current_tag) {
100 $this->title .= " " . trim($data);
104 $this->description .= trim($data);
108 $this->link = trim($data);
112 $this->date .= " " . trim($data);
115 $this->divers .= " " . $current_tag."/".$data;
121 function parse_results($xml_parser, $rss_parser, $file, $debug=false) {
122 xml_set_object($xml_parser, &$rss_parser);
123 xml_set_element_handler($xml_parser, "startElement", "endElement");
124 xml_set_character_data_handler($xml_parser, "characterData");
126 if (ini_get('allow_url_fopen')) {
127 $fp = fopen("$file","r") or die("Error reading XML file, $file");
128 while ($data = fread($fp, 4096)) {
129 xml_parse($xml_parser, $data, feof($fp)) or
130 die(sprintf("XML error: %s at line %d",
131 xml_error_string(xml_get_error_code($xml_parser)),
132 xml_get_current_line_number($xml_parser)));
136 // other url_fopen workarounds: curl, socket (http 80 only)
137 require_once("lib/HttpClient.php");
138 $bits = parse_url($file);
139 $host = $bits['host'];
140 $port = isset($bits['port']) ? $bits['port'] : 80;
141 $path = isset($bits['path']) ? $bits['path'] : '/';
142 if (isset($bits['query'])) {
143 $path .= '?'.$bits['query'];
145 $client = new HttpClient($host, $port);
146 $client->use_gzip = false;
147 if ($debug) $client->debug = true;
148 if (!$client->get($path)) {
151 $data = $client->getContent();
153 xml_parse($xml_parser, $data, true) or
154 die(sprintf("XML error: %s at line %d",
155 xml_error_string(xml_get_error_code($xml_parser)),
156 xml_get_current_line_number($xml_parser)));
158 xml_parser_free($xml_parser);
162 // $Log: not supported by cvs2svn $
169 // c-hanging-comment-ender-p: nil
170 // indent-tabs-mode: nil