|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
|
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
class pinp_rss { |
|
7
|
|
|
|
|
8
|
|
|
public static function _loadFromUrl($url, $username='', $password='') { |
|
9
|
|
|
/* Loads an rss feed from a url */ |
|
10
|
|
|
return rss::loadFromUrl($url, $username, $password); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public static function _loadFromString($rss) { |
|
14
|
|
|
return rss::loadFromString($rss); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
class rss { |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
public static function loadFromUrl($url, $username='', $password='') { |
|
22
|
|
|
/* Loads an rss feed from a url */ |
|
23
|
|
|
$context = pobject::getContext(); |
|
24
|
|
|
$me = $context['arCurrentObject']; |
|
25
|
|
|
$rss = new rssFeed($me); |
|
26
|
|
|
$rss->setFeedUrl($url, $username, $password); |
|
27
|
|
|
return $rss; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
View Code Duplication |
public static function loadFromString($string) { |
|
31
|
|
|
$context = pobject::getContext(); |
|
32
|
|
|
$me = $context["arCurrentObject"]; |
|
33
|
|
|
/* parse rss feed and initialize and return an rssFeed object */ |
|
34
|
|
|
$rss = new rssFeed($me); |
|
35
|
|
|
$rss->setFeedString($string); |
|
36
|
|
|
return $rss; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
class rssFeed { |
|
42
|
|
|
public $object; |
|
43
|
|
|
public $rss_url; |
|
44
|
|
|
public $rss_user; |
|
45
|
|
|
public $rss_password; |
|
46
|
|
|
public $error; |
|
47
|
|
|
public $feedstring; |
|
48
|
|
|
public $ns; |
|
49
|
|
|
public $elements; |
|
50
|
|
|
public $rss_items; |
|
51
|
|
|
public $rss_fp; |
|
52
|
|
|
public $xmldata; |
|
53
|
|
|
public $encoding; |
|
54
|
|
|
public $parser; |
|
55
|
|
|
public $attribs; |
|
56
|
|
|
public $newElement; |
|
57
|
|
|
public $n; |
|
58
|
|
|
public $rss_data; |
|
59
|
|
|
public $eof; |
|
60
|
|
|
public $result; |
|
61
|
|
|
|
|
62
|
|
|
public function __construct($object) { |
|
63
|
|
|
$this->object = $object; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setFeedUrl($url, $username='', $password='') { |
|
67
|
|
|
if (preg_match('|^https?://|i', $url)) { |
|
68
|
|
|
$this->rss_url = $url; |
|
69
|
|
|
$this->rss_user = $username; |
|
70
|
|
|
$this->rss_password = $password; |
|
71
|
|
|
$this->reset(); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->error = "$url is not a valid URL"; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function setFeedString($feed) { |
|
78
|
|
|
$this->feedstring = $feed; |
|
79
|
|
|
$this->reset(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function _reset() { |
|
83
|
|
|
return $this->reset(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function _next() { |
|
87
|
|
|
return $this->next(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function _count() { |
|
91
|
|
|
return $this->count(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function _current() { |
|
95
|
|
|
return $this->current(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function _ls($template, $args='', $limit=100, $offset=0) { |
|
99
|
|
|
return $this->ls($template, $args, $limit, $offset); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function _getArray($limit=100, $offset=0) { |
|
103
|
|
|
return $this->getArray($limit, $offset); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function reset() { |
|
107
|
|
|
// reset namestack |
|
108
|
|
|
$this->ns = array(); |
|
109
|
|
|
$this->elements = array(); |
|
110
|
|
|
$this->rss_items = array(); |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
if ($this->rss_fp) { |
|
114
|
|
|
fclose($this->rss_fp); |
|
115
|
|
|
} |
|
116
|
|
|
if ($this->rss_url) { |
|
117
|
|
|
if (!preg_match('|^https?://|i', $this->rss_url)) { |
|
118
|
|
|
$this->error = $this->rss_url." is not a valid URL"; |
|
119
|
|
|
} else { |
|
120
|
|
|
$this->rss_fp = fopen($this->rss_url, "r"); |
|
121
|
|
|
if (!$this->rss_fp) { |
|
122
|
|
|
$this->error = "Could not open RSS ".$this->rss_url; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Finding the RSS feed source encoding - thanks to the pointers on |
|
128
|
|
|
// http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss |
|
129
|
|
|
// |
|
130
|
|
|
// Read the first part of the RSS feed to find the encoding. |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
// FIXME: We kunnen niet terugseeken dus dit werkt niet. |
|
134
|
|
|
if ($this->rss_fp) { |
|
135
|
|
|
$this->xmldata = fread($this->rss_fp, 4096); |
|
136
|
|
|
} else if ($this->feedstring) { |
|
137
|
|
|
$this->xmldata = $this->feedstring; |
|
138
|
|
|
} |
|
139
|
|
|
// Prepare a regexp to find the source encoding, and match it. If we find it, use that - otherwise assume UTF-8 as the default XML encoding. |
|
140
|
|
|
$encoding_regexp = '/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m'; |
|
141
|
|
|
|
|
142
|
|
|
// $encoding_regexp = '/.*encoding=[\'"]/m'; |
|
143
|
|
|
|
|
144
|
|
View Code Duplication |
if (preg_match($encoding_regexp, $this->xmldata, $matches)) { |
|
145
|
|
|
$this->encoding = strtoupper($matches[1]); |
|
146
|
|
|
} else { |
|
147
|
|
|
$this->encoding = "UTF-8"; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// The RSS library only understands UTF-8, US-ASCII and ISO-8859-1, so only give it this. For other encodings we'll default to UTF-8. |
|
151
|
|
|
|
|
152
|
|
View Code Duplication |
if($this->encoding == "UTF-8" || $this->encoding == "US-ASCII" || $this->encoding == "ISO-8859-1") { |
|
153
|
|
|
$this->parser = xml_parser_create($this->encoding); |
|
154
|
|
|
} else { |
|
155
|
|
|
|
|
156
|
|
|
$this->parser = xml_parser_create("UTF-8"); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
//$this->parser = xml_parser_create(); |
|
161
|
|
|
xml_set_object($this->parser, $this); |
|
162
|
|
|
xml_set_element_handler($this->parser, "startElement", "endElement"); |
|
163
|
|
|
xml_set_character_data_handler($this->parser, "characterData"); |
|
164
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); |
|
165
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function startElement($parser, $name, $attribs) { |
|
|
|
|
|
|
170
|
|
|
$newElement = array(); |
|
|
|
|
|
|
171
|
|
|
$element = &$this->elements; |
|
172
|
|
|
foreach ($this->ns as $n) { |
|
173
|
|
|
$element = &$element[$n]; |
|
174
|
|
|
} |
|
175
|
|
|
$this->ns[] = $name; |
|
176
|
|
View Code Duplication |
switch ($name) { |
|
177
|
|
|
default: |
|
|
|
|
|
|
178
|
|
|
$element[$name] = $newElement; |
|
179
|
|
|
if ($attribs) { |
|
180
|
|
|
foreach($attribs as $attribName => $attribValue ) { |
|
181
|
|
|
$element[$name.':'.$attribName] = $attribValue; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
break; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function endElement($parser, $name) { |
|
|
|
|
|
|
189
|
|
|
$element = &$this->elements; |
|
190
|
|
|
foreach ($this->ns as $n) { |
|
191
|
|
|
$parentElement = $element; |
|
192
|
|
|
$element = &$element[$n]; |
|
193
|
|
|
} |
|
194
|
|
|
switch ($name) { |
|
195
|
|
|
case 'item': |
|
196
|
|
|
$this->rss_items[] = $element; |
|
197
|
|
|
unset($parentElement[$name]); |
|
198
|
|
|
break; |
|
199
|
|
|
} |
|
200
|
|
|
array_pop($this->ns); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function characterData($parser, $data) { |
|
|
|
|
|
|
204
|
|
|
$element = &$this->elements; |
|
205
|
|
|
foreach ($this->ns as $n) { |
|
206
|
|
|
$element = &$element[$n]; |
|
207
|
|
|
} |
|
208
|
|
View Code Duplication |
switch ($n) { |
|
|
|
|
|
|
209
|
|
|
case 'item': |
|
210
|
|
|
case 'rss': |
|
211
|
|
|
case 'channel': |
|
212
|
|
|
case 'image': |
|
213
|
|
|
case 'rdf:RDF': |
|
214
|
|
|
case 'rdf:Seq': |
|
215
|
|
|
case 'rdf:li': |
|
216
|
|
|
case 'items': |
|
217
|
|
|
|
|
218
|
|
|
break; |
|
219
|
|
|
default: |
|
220
|
|
|
if (!$element) { |
|
221
|
|
|
$element = ""; |
|
222
|
|
|
} |
|
223
|
|
|
$element .= $data; |
|
224
|
|
|
break; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
public function next() { |
|
230
|
|
|
// this is needed |
|
231
|
|
|
if (!$this->parser) { |
|
232
|
|
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
xml_set_object($this->parser, $this); |
|
236
|
|
|
|
|
237
|
|
|
/* remove the last item from the queue */ |
|
238
|
|
|
if (count($this->rss_items)) { |
|
239
|
|
|
array_shift($this->rss_items); |
|
240
|
|
|
} |
|
241
|
|
|
if (!count($this->rss_items) && ($this->xmldata || ($this->rss_fp && !feof($this->rss_fp)))) { |
|
242
|
|
|
do { |
|
243
|
|
|
// The first read has already been done in the reset() function! |
|
244
|
|
|
if ($this->xmldata) { |
|
|
|
|
|
|
245
|
|
|
$rss_data = $this->xmldata; |
|
246
|
|
|
$this->xmldata = false; |
|
247
|
|
|
if (!($this->rss_fp)) { |
|
248
|
|
|
$eof = true; |
|
249
|
|
|
} |
|
250
|
|
|
} else if ($this->rss_fp) { |
|
251
|
|
|
$rss_data = fread($this->rss_fp, 4096); |
|
252
|
|
|
$eof = feof($this->rss_fp); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/* |
|
256
|
|
|
if(function_exists('mb_convert_encoding')) { |
|
257
|
|
|
$encoded_source = @mb_convert_encoding($rss_data, "UTF-8", $this->encoding); |
|
258
|
|
|
} |
|
259
|
|
|
if($encoded_source != NULL) { |
|
260
|
|
|
$rss_data = str_replace ( $this->xml_enc,'<?xml version="1.0" encoding="utf-8"?>', $encoded_source); |
|
261
|
|
|
} |
|
262
|
|
|
*/ |
|
263
|
|
View Code Duplication |
if (!xml_parse($this->parser, $rss_data, $eof)) { |
|
|
|
|
|
|
264
|
|
|
$this->error = sprintf("XML error: %s at line %d", |
|
265
|
|
|
xml_error_string(xml_get_error_code($this->parser)), |
|
266
|
|
|
xml_get_current_line_number($this->parser)); |
|
267
|
|
|
} |
|
268
|
|
|
} while (!$this->error && !$eof && !count($this->rss_items)); |
|
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
return $this->rss_items[0]; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
public function current() { |
|
274
|
|
|
return $this->rss_items[0]; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
View Code Duplication |
public function call($template, $args=array()) { |
|
278
|
|
|
$current = $this->current(); |
|
279
|
|
|
if (!$current) { // feed is either not yet initialized or ended, in both cases the following line has the correct result |
|
280
|
|
|
$current = $this->next(); |
|
281
|
|
|
} |
|
282
|
|
|
if ($current) { |
|
283
|
|
|
$args['item'] = $current; |
|
284
|
|
|
$result = $this->object->call($template, $args); |
|
285
|
|
|
} |
|
286
|
|
|
return $result; |
|
|
|
|
|
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
public function count() { |
|
290
|
|
|
$this->reset(); |
|
291
|
|
|
$i = 0; |
|
292
|
|
|
while ($this->next()) { $i++; }; |
|
293
|
|
|
return $i; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
View Code Duplication |
public function ls($template, $args='', $limit=100, $offset=0) { |
|
297
|
|
|
global $ARBeenHere; |
|
298
|
|
|
$ARBeenHere = array(); |
|
299
|
|
|
$this->reset(); |
|
300
|
|
|
if ($offset) { |
|
301
|
|
|
while ($offset) { |
|
302
|
|
|
$this->next(); |
|
303
|
|
|
$offset--; |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
do { |
|
307
|
|
|
$ARBeenHere = array(); |
|
308
|
|
|
$this->call($template, $args); |
|
309
|
|
|
$limit--; |
|
310
|
|
|
} while ($this->next() && $limit); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
View Code Duplication |
public function getArray($limit=100, $offset=0) { |
|
314
|
|
|
$result=array(); |
|
315
|
|
|
$this->reset(); |
|
316
|
|
|
if ($offset) { |
|
317
|
|
|
while ($offset) { |
|
318
|
|
|
$this->next(); |
|
319
|
|
|
$offset--; |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
do { |
|
323
|
|
|
$result[]=$this->current(); |
|
324
|
|
|
$limit--; |
|
325
|
|
|
} while ($this->next() && $limit); |
|
326
|
|
|
return $result; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
} |
|
330
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.