|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
define("DIRMODE", 0770); |
|
4
|
|
|
|
|
5
|
|
|
class cacherss { |
|
6
|
|
|
protected $httphelper; |
|
7
|
|
|
protected $timeout; |
|
8
|
|
|
protected $xmldata; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct( $httphelper, $timeout = 1800 ) { |
|
11
|
|
|
$this->httphelper = $httphelper; |
|
12
|
|
|
$this->timeout = $timeout; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function load( $url ) { |
|
16
|
|
|
$data = $this->httphelper->load( $url, "", time() - $this->timeout ); |
|
17
|
|
|
$this->xmldata = $data["data"]; |
|
18
|
|
|
|
|
19
|
|
|
$rssfeed = new cacherssfeed(); // load from string |
|
20
|
|
|
$result = $rssfeed->parseString( $this->xmldata ); |
|
21
|
|
|
|
|
22
|
|
|
return $result; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function titlelink($url) { |
|
26
|
|
|
$data = $this->httphelper->load( $url, "", time() - $this->timeout ); |
|
27
|
|
|
$this->xmldata = $data["data"]; |
|
28
|
|
|
|
|
29
|
|
|
$rssfeed = new cacherssfeed(); // load from string |
|
30
|
|
|
|
|
31
|
|
|
$rssfeed->parseString( $this->xmldata); |
|
32
|
|
|
$rss_channel = $rssfeed->info(); |
|
33
|
|
|
return array("title" => $rss_channel['title'], "link" => $rss_channel['link']); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
class pinp_cacherss { |
|
39
|
|
View Code Duplication |
public function _load( $url, $timeout = 1800 ) { |
|
40
|
|
|
global $AR; |
|
41
|
|
|
$cachelocation = $AR->dir->install . "/files/cache/rss/"; |
|
42
|
|
|
$cache = new cacherss_cache( $cachelocation ); |
|
43
|
|
|
$httphelper = new httphelper( $cache ); |
|
44
|
|
|
$rss = new cacherss( $httphelper, $timeout ); |
|
45
|
|
|
return $rss->load( $url ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public function _titlelink( $url, $timeout = 1800 ) { |
|
49
|
|
|
global $AR; |
|
50
|
|
|
$cachelocation = $AR->dir->install . "/files/cache/rss/"; |
|
51
|
|
|
$cache = new cacherss_cache( $cachelocation ); |
|
52
|
|
|
$httphelper = new httphelper( $cache ); |
|
53
|
|
|
$rss = new cacherss( $httphelper, $timeout ); |
|
54
|
|
|
return $rss->titlelink( $url ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
class cacherssfeed { |
|
60
|
|
|
protected $xmldata; |
|
61
|
|
|
protected $ns; |
|
62
|
|
|
protected $elements; |
|
63
|
|
|
protected $rss_items; |
|
64
|
|
|
protected $rss_channel; |
|
65
|
|
|
protected $encoding; |
|
66
|
|
|
protected $parser; |
|
67
|
|
|
|
|
68
|
|
|
public function parseString( $xmldata ) { |
|
69
|
|
|
// reset namestack |
|
70
|
|
|
$this->xmldata = $xmldata; |
|
71
|
|
|
$this->ns = array(); |
|
72
|
|
|
$this->elements = array(); |
|
73
|
|
|
$this->rss_items = array(); |
|
74
|
|
|
$this->rss_channel = array(); |
|
75
|
|
|
|
|
76
|
|
|
// Finding the RSS feed source encoding - thanks to the pointers on |
|
77
|
|
|
// http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss |
|
78
|
|
|
// |
|
79
|
|
|
// Read the first part of the RSS feed to find the encoding. |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// 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. |
|
83
|
|
|
$encoding_regexp = '/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m'; |
|
84
|
|
|
|
|
85
|
|
|
// $encoding_regexp = '/.*encoding=[\'"]/m'; |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
if (preg_match($encoding_regexp, $this->xmldata, $matches)) { |
|
88
|
|
|
$this->encoding = strtoupper($matches[1]); |
|
89
|
|
|
} else { |
|
90
|
|
|
$this->encoding = "UTF-8"; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// 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. |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
if($this->encoding == "UTF-8" || $this->encoding == "US-ASCII" || $this->encoding == "ISO-8859-1") { |
|
96
|
|
|
$this->parser = xml_parser_create($this->encoding); |
|
97
|
|
|
} else { |
|
98
|
|
|
$this->parser = xml_parser_create("UTF-8"); |
|
99
|
|
|
$this->encoding = "UTF-8"; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Check if we have valid xml |
|
103
|
|
|
$parsetest = xml_parse(xml_parser_create($this->encoding), $xmldata); |
|
104
|
|
|
if (!$parsetest) { |
|
105
|
|
|
//echo "XML doesn't parse\n"; |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
//$this->parser = xml_parser_create(); |
|
110
|
|
|
xml_set_object($this->parser, $this); |
|
111
|
|
|
xml_set_element_handler($this->parser, "startElement", "endElement"); |
|
112
|
|
|
xml_set_character_data_handler($this->parser, "characterData"); |
|
113
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); |
|
114
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); |
|
115
|
|
|
|
|
116
|
|
|
// return the array |
|
117
|
|
|
return $this->getArray(); |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function startElement($parser, $name, $attribs) { |
|
|
|
|
|
|
122
|
|
|
$newElement = array(); |
|
|
|
|
|
|
123
|
|
|
$element = &$this->elements; |
|
124
|
|
View Code Duplication |
foreach ($this->ns as $n) { |
|
125
|
|
|
$parentElement = $element; |
|
|
|
|
|
|
126
|
|
|
if (is_array($element) && isset($element[$n])) { |
|
127
|
|
|
$element = &$element[$n]; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
$this->ns[] = $name; |
|
131
|
|
View Code Duplication |
switch ($name) { |
|
132
|
|
|
default: |
|
|
|
|
|
|
133
|
|
|
if (is_array($attribs)) { |
|
134
|
|
|
foreach ($attribs as $key => $value) { |
|
135
|
|
|
$element[$name . ":" . $key] = $value; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
$element[$name] = $newElement; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function endElement($parser, $name) { |
|
|
|
|
|
|
143
|
|
|
$element = &$this->elements; |
|
144
|
|
View Code Duplication |
foreach ($this->ns as $n) { |
|
145
|
|
|
$parentElement = $element; |
|
146
|
|
|
if (is_array($element) && isset($element[$n])) { |
|
147
|
|
|
$element = &$element[$n]; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
switch ($name) { |
|
151
|
|
|
case 'item': |
|
152
|
|
|
$this->rss_items[] = $element; |
|
153
|
|
|
unset($parentElement[$name]); |
|
154
|
|
|
break; |
|
155
|
|
|
case 'channel': |
|
156
|
|
|
$this->rss_channel = $element; |
|
157
|
|
|
break; |
|
158
|
|
|
} |
|
159
|
|
|
array_pop($this->ns); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function characterData($parser, $data) { |
|
|
|
|
|
|
163
|
|
|
$element = &$this->elements; |
|
164
|
|
View Code Duplication |
foreach ($this->ns as $n) { |
|
165
|
|
|
if (is_array($element) && isset($element[$n])) { |
|
166
|
|
|
$element = &$element[$n]; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
View Code Duplication |
switch ($n) { |
|
|
|
|
|
|
170
|
|
|
case 'textinput': |
|
171
|
|
|
case 'item': |
|
172
|
|
|
case 'rss': |
|
173
|
|
|
case 'channel': |
|
174
|
|
|
case 'image': |
|
175
|
|
|
case 'rdf:RDF': |
|
176
|
|
|
case 'rdf:Seq': |
|
177
|
|
|
case 'rdf:li': |
|
178
|
|
|
case 'items': |
|
179
|
|
|
break; |
|
180
|
|
|
default: |
|
181
|
|
|
if (!$element) { |
|
182
|
|
|
$element = ""; |
|
183
|
|
|
} |
|
184
|
|
|
$element .= $data; |
|
185
|
|
|
break; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function current() { |
|
190
|
|
|
return $this->rss_items[0]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function info() { |
|
194
|
|
|
return $this->rss_channel; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function next() { |
|
198
|
|
|
// this is needed |
|
199
|
|
|
if (!$this->parser) { |
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
xml_set_object($this->parser, $this); |
|
204
|
|
|
|
|
205
|
|
|
/* remove the last item from the queue */ |
|
206
|
|
|
if (count($this->rss_items)) { |
|
207
|
|
|
array_shift($this->rss_items); |
|
208
|
|
|
} |
|
209
|
|
|
if (!count($this->rss_items) && $this->xmldata) { |
|
210
|
|
|
$rss_data = $this->xmldata; |
|
211
|
|
|
// $this->xmldata = false; |
|
212
|
|
|
|
|
213
|
|
View Code Duplication |
if (!xml_parse($this->parser, $rss_data, $eof)) { |
|
|
|
|
|
|
214
|
|
|
$this->error = sprintf("XML error: %s at line %d", |
|
|
|
|
|
|
215
|
|
|
xml_error_string(xml_get_error_code($this->parser)), |
|
216
|
|
|
xml_get_current_line_number($this->parser)); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
return $this->rss_items[0]; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function getArray() { |
|
223
|
|
|
$result=array(); |
|
224
|
|
|
do { |
|
225
|
|
|
$result[]=$this->current(); |
|
226
|
|
|
} while ($this->next()); |
|
227
|
|
|
return $result; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
class httphelper { |
|
232
|
|
|
protected $cache; |
|
233
|
|
|
public $error; |
|
234
|
|
|
|
|
235
|
|
|
public function __construct( $cache ) { |
|
236
|
|
|
$this->cache = $cache; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function load( $url, $meta="", $maxage=0, $user="" ) { |
|
240
|
|
|
$result = $this->cache->load($url, $maxage, $user); |
|
241
|
|
|
if( !$result && $maxage >= 0 ) { |
|
242
|
|
|
$client = ar('http')->client(); |
|
243
|
|
|
$data = $client->get($url); |
|
244
|
|
|
if( $data != "" ) { |
|
245
|
|
|
$result = $this->cache->save($url, $data, $meta, $user); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
return $result; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
class cacherss_cache { |
|
253
|
|
|
protected $path; |
|
254
|
|
|
|
|
255
|
|
|
public function __construct( $path ) { |
|
256
|
|
|
// FIXME: forceer $path eindigen op een / en security. |
|
257
|
|
|
|
|
258
|
|
|
if (!is_dir($path) && !file_exists($path)) { |
|
259
|
|
|
mkdir($path, DIRMODE); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
$this->path = $path; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
public function load( $tag, $maxage = 0, $user = "" ) { |
|
267
|
|
|
|
|
268
|
|
|
if( !$tag ) { |
|
269
|
|
|
return false; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$tag = $this->escape( $tag ); |
|
273
|
|
|
|
|
274
|
|
|
$result = false; |
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
if( $user != "" ) { |
|
278
|
|
|
$user = $user."/"; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$path = $this->path. $user. $tag; |
|
282
|
|
|
if (file_exists($path) && filectime($path) >= $maxage) { |
|
283
|
|
|
$fp = fopen( $path, "rb" ); |
|
284
|
|
|
$data=fread($fp,filesize($path) ); |
|
285
|
|
|
$timestamp = filectime($path); |
|
286
|
|
|
fclose($fp); |
|
287
|
|
|
$metapath = $path.".meta"; |
|
288
|
|
|
$meta = ""; |
|
289
|
|
|
if( file_exists($metapath)) { |
|
290
|
|
|
$fp = fopen( $metapath, "rb" ); |
|
291
|
|
|
$meta = fread($fp, filesize($metapath) ); |
|
292
|
|
|
fclose($fp); |
|
293
|
|
|
} |
|
294
|
|
|
$result = array( "data" => unserialize($data), "meta" => unserialize($meta), "timestamp" => $timestamp ); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return $result; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
public function save( $tag, $data, $meta = "", $user = "" ) { |
|
301
|
|
|
|
|
302
|
|
|
if( !$tag ) { |
|
303
|
|
|
return false; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
$result = false; |
|
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
$user = $this->escape( $user ); |
|
309
|
|
|
|
|
310
|
|
|
if( $user != "" ) { |
|
311
|
|
|
$user = $user."/"; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
$tag = $this->escape( $tag ); |
|
315
|
|
|
$data = serialize( $data ); |
|
316
|
|
|
$meta = serialize( $meta ); |
|
317
|
|
|
|
|
318
|
|
|
$path = $this->path.$user; |
|
319
|
|
|
if( $path != $this->path ) { |
|
320
|
|
|
// user folder maken |
|
321
|
|
|
if( !file_exists($path) ) { |
|
322
|
|
|
if( !@mkdir($path, 0770) ) { |
|
323
|
|
|
// abort abort abort! |
|
324
|
|
|
return false; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
$path = $this->path.$user.$tag; |
|
329
|
|
|
|
|
330
|
|
|
$fp = fopen( $path, "wb"); |
|
331
|
|
|
$result=fwrite($fp, $data); |
|
332
|
|
|
fclose($fp); |
|
333
|
|
|
if( $result ) { |
|
334
|
|
|
$metapath = $path.".meta"; |
|
335
|
|
|
$fp = fopen( $metapath, "wb" ); |
|
336
|
|
|
$result = fwrite($fp, $meta); |
|
337
|
|
|
fclose($fp); |
|
338
|
|
|
} |
|
339
|
|
|
if( $result ) { |
|
340
|
|
|
$result = array( "data" => unserialize($data), "meta" => unserialize($meta), "timestamp" => filectime($path) ); |
|
341
|
|
|
} |
|
342
|
|
|
return $result; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
public function clear( $tag, $user = "" ) { |
|
346
|
|
|
|
|
347
|
|
|
if( !$tag ) { |
|
348
|
|
|
return false; |
|
349
|
|
|
} |
|
350
|
|
|
$tag = $this->escape( $tag ); |
|
351
|
|
|
|
|
352
|
|
|
$user = $this->escape( $user ); |
|
353
|
|
|
|
|
354
|
|
|
if( $user != "" ) { |
|
355
|
|
|
$user = $user."/"; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
$path = $this->path.$user.$tag; |
|
359
|
|
|
if( file_exists($path) ) { |
|
360
|
|
|
$result = unlink($path); |
|
361
|
|
|
$metapath = $path.".meta"; |
|
362
|
|
|
if( file_exists($metapath) ) { |
|
363
|
|
|
unlink($metapath); |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
return $result; |
|
|
|
|
|
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
View Code Duplication |
protected function escape($path) { |
|
371
|
|
|
// This function will return an escaped path. All the characters not supported by Ariadne will be encoded. |
|
372
|
|
|
// See also path_escape_callback |
|
373
|
|
|
|
|
374
|
|
|
// Returns an empty string if no path, or an empty path was given. |
|
375
|
|
|
$result = ""; |
|
376
|
|
|
if ($path) { |
|
377
|
|
|
$result = preg_replace_callback( |
|
378
|
|
|
'/[^A-Za-z0-9-]/', function ($char) { |
|
379
|
|
|
// Replaces characters in the path with their number. |
|
380
|
|
|
// Quite similar to " " -> "%20" for HTML escape, but we use _ instead of % |
|
381
|
|
|
// This function is to be used as a callback for preg_replace_callback |
|
382
|
|
|
if ($char[0]) { |
|
383
|
|
|
if ($char[0]=="_") { |
|
384
|
|
|
return "__"; |
|
385
|
|
|
} else { |
|
386
|
|
|
return "_".dechex(ord($char[0])); |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
}, |
|
390
|
|
|
$path |
|
391
|
|
|
); |
|
392
|
|
|
} |
|
393
|
|
|
return $result; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
View Code Duplication |
function unescape($path) { |
|
397
|
|
|
$result = ""; |
|
398
|
|
|
if ($path) { |
|
399
|
|
|
$result = preg_replace_callback( |
|
400
|
|
|
'/(_[0-9a-fA-F][0-9a-fA-F]|__)/', |
|
401
|
|
|
function ( $matches ) { |
|
402
|
|
|
// Two types of escaped characters can be here, the |
|
403
|
|
|
// underscore or other characters. Check for the |
|
404
|
|
|
// underscore first. |
|
405
|
|
|
|
|
406
|
|
|
$char = $matches[0]; |
|
407
|
|
|
if ($char[1] == "_") { |
|
408
|
|
|
// It is the underscore, return it as a character. |
|
409
|
|
|
return "_"; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
// Assume it is an escaped character here. Find the |
|
413
|
|
|
// numbers in hex, turn them back to decimal, get |
|
414
|
|
|
// the corresponding character and return it. |
|
415
|
|
|
|
|
416
|
|
|
return chr(hexdec(substr($char, 1, 2))); |
|
417
|
|
|
}, |
|
418
|
|
|
$path |
|
419
|
|
|
); |
|
420
|
|
|
} |
|
421
|
|
|
return $result; |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.