]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/RssParser.php
more numeric pagename fixes.
[SourceForge/phpwiki.git] / lib / RssParser.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RssParser.php,v 1.4 2004-04-18 01:11:51 rurban Exp $');
3 /**
4  * RSSParser Class, requires the expat extension
5  * Based on Duncan Gough RSSParser class
6  * Copyleft Arnaud Fontaine
7  * Licence : GPL
8
9  This file is part of PhpWiki.
10
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.
15
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.
20
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
24  */
25
26 /**
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)
31  */
32 class RSSParser {
33
34     var $title = "";
35     var $link  = "";
36     var $description = "";
37     var $inside_item = false;
38     var $item  = array();
39     var $items = array();
40     var $channel = array();
41     var $divers = "";
42     var $date = "";
43
44     function startElement($parser, $name, $attrs=''){
45         global $current_tag;
46
47         $current_tag = $name;
48         if ($name == "ITEM")
49             $this->inside_item = true;
50         elseif ($name == "IMAGE")
51             $this->inside_item = true;
52     } // startElement
53
54     function endElement($parser, $tagName, $attrs=''){
55         global $current_tag;
56
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);
76             $this->title       = "";
77             $this->description = "";
78             $this->link        = "";
79             $this->divers      = "";
80             $this->date        = "";
81         }
82     }
83
84     function characterData($parser, $data){
85         global $current_tag;
86
87         if ($this->inside_item) {
88             if (empty($this->item[$current_tag]))
89                 $this->item[$current_tag] = '';
90             if ($current_tag == 'LINK') {
91                 if (trim($data))
92                     $this->item[$current_tag] = trim($data);
93             } else {
94                 $this->item[$current_tag] .= trim($data);
95             }
96         } else {
97             switch ($current_tag) {
98             case "TITLE":
99                 if (trim($data))
100                     $this->title .= " " . trim($data);
101                 break;
102             case "DESCRIPTION":
103                 if (trim($data))
104                     $this->description .= trim($data);
105                 break;
106             case "LINK":
107                 if (trim($data))
108                     $this->link = trim($data);
109                 break;
110             case "DC:DATE":
111                 if (trim($data))
112                     $this->date .= " " . trim($data);
113             default:
114                 if (trim($data))
115                     $this->divers .= " " . $current_tag."/".$data;
116                 break;
117             }
118         }
119     } // characterData
120
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");
125
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)));
133             }
134             fclose($fp);
135         } else {
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'];
144             }
145             $client = new HttpClient($host, $port);
146             $client->use_gzip = false;
147             if ($debug) $client->debug = true;
148             if (!$client->get($path)) {
149                 $data = false;
150             } else {
151                 $data = $client->getContent();
152             }
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)));
157         }
158         xml_parser_free($xml_parser);
159     }
160 }
161
162 // $Log: not supported by cvs2svn $
163
164 // For emacs users
165 // Local Variables:
166 // mode: php
167 // tab-width: 8
168 // c-basic-offset: 4
169 // c-hanging-comment-ender-p: nil
170 // indent-tabs-mode: nil
171 // End:
172 ?>