Repository navigation
Expand file tree
/
Copy pathclass.myrssparser.php
More file actions
284 lines (247 loc) · 8.91 KB
/
Copy pathclass.myrssparser.php
File metadata and controls
284 lines (247 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
// http://www.the-art-of-web.com/php/rss/
class myRSSParser
{
// keeps track of current and preceding elements
var $tags = array();
// array containing all feed data
var $output = array();
// return value for display functions
var $retval = "";
var $errorlevel = 0;
// constructor for new object
function myRSSParser($file)
{
$errorlevel = error_reporting();
error_reporting($errorlevel & ~E_NOTICE);
// instantiate xml-parser and assign event handlers
$xml_parser = xml_parser_create("");
xml_set_object($xml_parser, $this);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "parseData");
// open file for reading and send data to xml-parser
$data = preg_match("/^http/", $file) ? $this->http_get_contents($file) : file_get_contents($file);
xml_parse($xml_parser, $data) or die(
sprintf("myRSSParser: Error <b>%s</b> at line <b>%d</b><br>",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser))
);
// dismiss xml parser
xml_parser_free($xml_parser);
error_reporting($errorlevel);
}
function startElement($parser, $tagname, $attrs=array())
{
// RSS 2.0 - ENCLOSURE
if($tagname == "ENCLOSURE" && $attrs) {
$this->startElement($parser, "ENCLOSURE");
foreach($attrs as $attr => $attrval) {
$this->startElement($parser, $attr);
$this->parseData($parser, $attrval);
$this->endElement($parser, $attr);
}
$this->endElement($parser, "ENCLOSURE");
}
// Yahoo! Media RSS - images
if($tagname == "MEDIA:CONTENT" && $attrs['URL'] && $attrs['MEDIUM'] == 'image') {
$this->startElement($parser, "IMAGE");
$this->parseData($parser, $attrs['URL']);
$this->endElement($parser, "IMAGE");
}
// check if this element can contain others - list may be edited
if(preg_match("/^(RDF|RSS|CHANNEL|IMAGE|ITEM)/", $tagname)) {
if($this->tags) {
$depth = count($this->tags);
if(is_array($tmp = end($this->tags))) {
list($parent, $num) = each($tmp);
if($parent) $this->tags[$depth-1][$parent][$tagname]++;
}
}
array_push($this->tags, array($tagname => array()));
} else {
if(!preg_match("/^(A|B|I)$/", $tagname)) {
// add tag to tags array
array_push($this->tags, $tagname);
}
}
}
function endElement($parser, $tagname)
{
if(!preg_match("/^(A|B|I)$/", $tagname)) {
// remove tag from tags array
array_pop($this->tags);
}
}
function parseData($parser, $data)
{
// return if data contains no text
if(!trim($data)) return;
$evalcode = "\$this->output";
foreach($this->tags as $tag) {
if(is_array($tag)) {
list($tagname, $indexes) = each($tag);
$evalcode .= "[\"$tagname\"]";
if(${$tagname}) $evalcode .= "[" . (${$tagname} - 1) . "]";
if($indexes) extract($indexes);
} else {
if(preg_match("/^([A-Z]+):([A-Z]+)$/", $tag, $matches)) {
$evalcode .= "[\"$matches[1]\"][\"$matches[2]\"]";
} else {
$evalcode .= "[\"$tag\"]";
}
}
}
eval("$evalcode = $evalcode . '" . addslashes($data) . "';");
}
// display a single channel as HTML
function display_channel($data, $limit)
{
extract($data);
if($IMAGE) {
// display channel image(s)
foreach($IMAGE as $image) $this->display_image($image);
}
if($TITLE) {
// display channel information
$this->retval .= "<h1>";
if($LINK) $this->retval .= "<a href=\"$LINK\" target=\"_blank\">";
$this->retval .= stripslashes($TITLE);
if($LINK) $this->retval .= "</a>";
$this->retval .= "</h1>\n";
if($DESCRIPTION) $this->retval .= "<p>$DESCRIPTION</p>\n\n";
$tmp = array();
if($PUBDATE) $tmp[] = "<small>Published: $PUBDATE</small>";
if($COPYRIGHT) $tmp[] = "<small>Copyright: $COPYRIGHT</small>";
if($tmp) $this->retval .= "<p>" . implode("<br>\n", $tmp) . "</p>\n\n";
$this->retval .= "<div class=\"divider\"><!-- --></div>\n\n";
}
if($ITEM) {
// display channel item(s)
foreach($ITEM as $item) {
$this->display_item($item, "CHANNEL");
if(is_int($limit) && --$limit <= 0) break;
}
}
}
// display a single image as HTML
function display_image($data, $parent="")
{
extract($data);
if(!$URL) return;
$this->retval .= "<p>";
if($LINK) $this->retval .= "<a href=\"$LINK\" target=\"_blank\">";
$this->retval .= "<img src=\"$URL\"";
if($WIDTH && $HEIGHT) $this->retval .= " width=\"$WIDTH\" height=\"$HEIGHT\"";
$this->retval .= " border=\"0\" alt=\"$TITLE\">";
if($LINK) $this->retval .= "</a>";
$this->retval .= "</p>\n\n";
}
// display a single item as HTML
function display_item($data, $parent)
{
extract($data);
if(!$TITLE) return;
$this->retval .= "<p><b>";
if($LINK) $this->retval .= "<a href=\"$LINK\" target=\"_blank\">";
$this->retval .= stripslashes($TITLE);
if($LINK) $this->retval .= "</a>";
$this->retval .= "</b>";
if(!$PUBDATE && $DC["DATE"]) $PUBDATE = $DC["DATE"];
if($PUBDATE) $this->retval .= " <small>($PUBDATE)</small>";
$this->retval .= "</p>\n";
// use feed-formatted HTML if provided
if($CONTENT['ENCODED']) {
$this->retval .= "<p>" . stripslashes($CONTENT['ENCODED']) . "</p>\n";
} elseif($DESCRIPTION) {
if($IMAGE) {
foreach($IMAGE as $IMG) $this->retval .= "<img src=\"$IMG\">\n";
}
$this->retval .= "<p>" . stripslashes($DESCRIPTION) . "</p>\n\n";
}
// RSS 2.0 - ENCLOSURE
if($ENCLOSURE) {
$this->retval .= "<p><small><b>Media:</b> <a href=\"{$ENCLOSURE['URL']}\">";
$this->retval .= $ENCLOSURE['TYPE'];
$this->retval .= "</a> ({$ENCLOSURE['LENGTH']} bytes)</small></p>\n\n";
}
if($COMMENTS) {
$this->retval .= "<p style=\"text-align: right;\"><small>";
$this->retval .= "<a href=\"$COMMENTS\">Comments</a>";
$this->retval .= "</small></p>\n\n";
}
}
function fixEncoding(&$input, $key, $output_encoding)
{
if(!function_exists('mb_detect_encoding')) return $input;
$encoding = mb_detect_encoding($input);
switch($encoding)
{
case 'ASCII':
case $output_encoding:
break;
case '':
$input = mb_convert_encoding($input, $output_encoding);
break;
default:
$input = mb_convert_encoding($input, $output_encoding, $encoding);
}
}
// display entire feed as HTML
function getOutput($limit=false, $output_encoding='UTF-8')
{
$this->retval = "";
$start_tag = key($this->output);
switch($start_tag)
{
case "RSS":
// new format - channel contains all
foreach($this->output[$start_tag]["CHANNEL"] as $channel) {
$this->display_channel($channel, $limit);
}
break;
case "RDF:RDF":
// old format - channel and items are separate
if(isset($this->output[$start_tag]['IMAGE'])) {
foreach($this->output[$start_tag]['IMAGE'] as $image) {
$this->display_image($image);
}
}
foreach($this->output[$start_tag]['CHANNEL'] as $channel) {
$this->display_channel($channel, $limit);
}
foreach($this->output[$start_tag]['ITEM'] as $item) {
$this->display_item($item, $start_tag);
}
break;
case "HTML":
die("Error: cannot parse HTML document as RSS");
default:
die("Error: unrecognized start tag '$start_tag' in getOutput()");
}
if($this->retval && is_array($this->retval)) {
array_walk_recursive($this->retval, 'myRSSParser::fixEncoding', $output_encoding);
}
return $this->retval;
}
function http_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(FALSE === ($retval = curl_exec($ch))) {
error_log(curl_error($ch));
} else {
return $retval;
}
}
// return raw data as array
function getRawOutput($output_encoding='UTF-8')
{
array_walk_recursive($this->output, 'myRSSParser::fixEncoding', $output_encoding);
return $this->output;
}
}
?>