]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/RssParser.php
more fixes for images and links
[SourceForge/phpwiki.git] / lib / RssParser.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RssParser.php,v 1.3 2004-04-12 17:32:19 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  */
30 class RSSParser {
31
32     var $title = "";
33     var $link  = "";
34     var $description = "";
35     var $inside_item = false;
36     var $item  = array();
37     var $items = array();
38     var $channel = array();
39     var $divers = "";
40     var $date = "";
41
42     function startElement($parser, $name, $attrs=''){
43         global $current_tag;
44
45         $current_tag = $name;
46         if ($name == "ITEM")
47             $this->inside_item = true;
48         elseif ($name == "IMAGE")
49             $this->inside_item = true;
50     } // startElement
51
52     function endElement($parser, $tagName, $attrs=''){
53         global $current_tag;
54
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);
74             $this->title       = "";
75             $this->description = "";
76             $this->link        = "";
77             $this->divers      = "";
78             $this->date        = "";
79         }
80     }
81
82     function characterData($parser, $data){
83         global $current_tag;
84
85         if ($this->inside_item) {
86             if (empty($this->item[$current_tag]))
87                 $this->item[$current_tag] = '';
88             if ($current_tag == 'LINK') {
89                 if (trim($data))
90                     $this->item[$current_tag] = trim($data);
91             } else {
92                 $this->item[$current_tag] .= trim($data);
93             }
94         } else {
95             switch ($current_tag) {
96             case "TITLE":
97                 if (trim($data))
98                     $this->title .= " " . trim($data);
99                 break;
100             case "DESCRIPTION":
101                 if (trim($data))
102                     $this->description .= trim($data);
103                 break;
104             case "LINK":
105                 if (trim($data))
106                     $this->link = trim($data);
107                 break;
108             case "DC:DATE":
109                 if (trim($data))
110                     $this->date .= " " . trim($data);
111             default:
112                 if (trim($data))
113                     $this->divers .= " " . $current_tag."/".$data;
114                 break;
115             }
116         }
117     } // characterData
118
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");
123
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)));
131             }
132             fclose($fp);
133         } else {
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'];
142             }
143             $client = new HttpClient($host, $port);
144             $client->use_gzip = false;
145             if ($debug) $client->debug = true;
146             if (!$client->get($path)) {
147                 $data = false;
148             } else {
149                 $data = $client->getContent();
150             }
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)));
155         }
156         xml_parser_free($xml_parser);
157     }
158 }
159
160 // For emacs users
161 // Local Variables:
162 // mode: php
163 // tab-width: 8
164 // c-basic-offset: 4
165 // c-hanging-comment-ender-p: nil
166 // indent-tabs-mode: nil
167 // End:
168 ?>