]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/RssParser.php
RssFeed plugin by Arnaud Fontaine, plus some helpers
[SourceForge/phpwiki.git] / lib / RssParser.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RssParser.php,v 1.1 2004-04-09 17:46:40 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 $items = array();
37     var $channel = array();
38     var $divers = "";
39     var $date = "";
40
41     function startElement($parser, $name, $attrs=''){
42         global $current_tag;
43
44         $current_tag = $name;
45         if ($current_tag == "ITEM")
46             $this->inside_item = true;
47     } // startElement
48
49     function endElement($parser, $tagName, $attrs=''){
50         global $current_tag;
51
52         if ($tagName == "ITEM") {
53             $this->items[] = array("title" => $this->title,
54                                    "description" => $this->description,
55                                    "link" => $this->link);
56             $this->title       = "";
57             $this->description = "";
58             $this->link        = "";
59             $this->inside_item = false;
60
61         } elseif ($tagName == "CHANNEL") {
62
63             $this->channel = array("title" => $this->title,
64                                    "description" => $this->description,
65                                    "link" => $this->link,
66                                    "date" => $this->date,
67                                    "divers" => $this->divers);
68             $this->title       = "";
69             $this->description = "";
70             $this->link        = "";
71             $this->divers      = "";
72             $this->date        = "";
73         }
74     } // endElement
75
76     function characterData($parser, $data){
77         global $current_tag;
78
79         if ($this->inside_item){
80             switch ($current_tag){
81
82             case "TITLE":
83                 $this->title .= trim($data);
84                 break;
85             case "DESCRIPTION":
86                 $this->description .= trim($data);
87                 break;
88             case "LINK":
89                 $this->link .= trim($data);
90                 break;
91
92             default:
93                 break;
94             }
95         } else {
96             switch($current_tag){
97
98             case "TITLE":
99                 $this->title .= trim($data);
100                 break;
101             case "DESCRIPTION":
102                 $this->description .= trim($data);
103                 break;
104             case "LINK":
105                 $this->link .= trim($data);
106                 break;
107             case "DC:DATE":
108                 $this->date .= trim($data);
109             default:
110                 $this->divers .= $current_tag."/".$data;
111                 break;
112             }
113         }
114     } // characterData
115
116     function parse_results($xml_parser, $rss_parser, $file)   {
117         xml_set_object($xml_parser, &$rss_parser);
118         xml_set_element_handler($xml_parser, "startElement", "endElement");
119         xml_set_character_data_handler($xml_parser, "characterData");
120
121         if (ini_get('allow_url_fopen')) {
122             $fp = fopen("$file","r") or die("Error reading XML file, $file");
123             while ($data = fread($fp, 4096))  {
124                 xml_parse($xml_parser, $data, feof($fp)) or 
125                     die(sprintf("XML error: %s at line %d", 
126                                 xml_error_string(xml_get_error_code($xml_parser)), 
127                                 xml_get_current_line_number($xml_parser)));
128             }
129             fclose($fp);
130         } else {
131             // other url_fopen workarounds: curl, socket (http 80 only)
132             require_once("lib/HttpClient.php");
133             $data = HttpClient::quickGet($file);
134             xml_parse($xml_parser, $data, true) or 
135                 die(sprintf("XML error: %s at line %d", 
136                             xml_error_string(xml_get_error_code($xml_parser)), 
137                             xml_get_current_line_number($xml_parser)));
138         }
139         xml_parser_free($xml_parser);
140     }
141 }
142
143 // For emacs users
144 // Local Variables:
145 // mode: php
146 // tab-width: 8
147 // c-basic-offset: 4
148 // c-hanging-comment-ender-p: nil
149 // indent-tabs-mode: nil
150 // End:
151 ?>