2 rcs_id('$Id: RssParser.php,v 1.3 2004-04-12 17:32:19 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
34 var $description = "";
35 var $inside_item = false;
38 var $channel = array();
42 function startElement($parser, $name, $attrs=''){
47 $this->inside_item = true;
48 elseif ($name == "IMAGE")
49 $this->inside_item = true;
52 function endElement($parser, $tagName, $attrs=''){
55 if ($tagName == "ITEM") {
56 $this->items[] = array("title" => $this->item['TITLE'],
57 "description" => $this->item['DESCRIPTION'],
58 "link" => $this->item['LINK']);
59 $this->item['TITLE'] = "";
60 $this->item['DESCRIPTION'] = "";
61 $this->item['LINK'] = "";
62 $this->inside_item = false;
63 } elseif ($tagName == "IMAGE") {
64 $this->item['TITLE'] = "";
65 $this->item['DESCRIPTION'] = "";
66 $this->item['LINK'] = "";
67 $this->inside_item = false;
68 } elseif ($tagName == "CHANNEL") {
69 $this->channel = array("title" => $this->title,
70 "description" => $this->description,
71 "link" => $this->link,
72 "date" => $this->date,
73 "divers" => $this->divers);
75 $this->description = "";
82 function characterData($parser, $data){
85 if ($this->inside_item) {
86 if (empty($this->item[$current_tag]))
87 $this->item[$current_tag] = '';
88 if ($current_tag == 'LINK') {
90 $this->item[$current_tag] = trim($data);
92 $this->item[$current_tag] .= trim($data);
95 switch ($current_tag) {
98 $this->title .= " " . trim($data);
102 $this->description .= trim($data);
106 $this->link = trim($data);
110 $this->date .= " " . trim($data);
113 $this->divers .= " " . $current_tag."/".$data;
119 function parse_results($xml_parser, $rss_parser, $file, $debug=false) {
120 xml_set_object($xml_parser, &$rss_parser);
121 xml_set_element_handler($xml_parser, "startElement", "endElement");
122 xml_set_character_data_handler($xml_parser, "characterData");
124 if (ini_get('allow_url_fopen')) {
125 $fp = fopen("$file","r") or die("Error reading XML file, $file");
126 while ($data = fread($fp, 4096)) {
127 xml_parse($xml_parser, $data, feof($fp)) or
128 die(sprintf("XML error: %s at line %d",
129 xml_error_string(xml_get_error_code($xml_parser)),
130 xml_get_current_line_number($xml_parser)));
134 // other url_fopen workarounds: curl, socket (http 80 only)
135 require_once("lib/HttpClient.php");
136 $bits = parse_url($file);
137 $host = $bits['host'];
138 $port = isset($bits['port']) ? $bits['port'] : 80;
139 $path = isset($bits['path']) ? $bits['path'] : '/';
140 if (isset($bits['query'])) {
141 $path .= '?'.$bits['query'];
143 $client = new HttpClient($host, $port);
144 $client->use_gzip = false;
145 if ($debug) $client->debug = true;
146 if (!$client->get($path)) {
149 $data = $client->getContent();
151 xml_parse($xml_parser, $data, true) or
152 die(sprintf("XML error: %s at line %d",
153 xml_error_string(xml_get_error_code($xml_parser)),
154 xml_get_current_line_number($xml_parser)));
156 xml_parser_free($xml_parser);
165 // c-hanging-comment-ender-p: nil
166 // indent-tabs-mode: nil