-
Notifications
You must be signed in to change notification settings - Fork 14
/
RssExtender.php
537 lines (481 loc) · 14.3 KB
/
RssExtender.php
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
//
// RssExtender.php
//
// Author:
// Lars Formella <ich@larsformella.de>
//
// Copyright (c) 2012 Lars Formella
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
require_once(__DIR__ . "/Feed.php");
if(file_exists(__DIR__ . '/vendor/autoload.php'))
{
require_once(__DIR__ . '/vendor/autoload.php');
$readability_enabled = true;
}
else
{
$readability_enabled = false;
}
class RssExtender
{
private $temporaryFolder = "";
private $configFolder = "";
private $contentNames = array("description", "summary", "atom_content", "content", "content:encoded");
private $itemNames = array("item", "entry");
private $timeNames = array("date", "updated", "pubDate");
public function __construct ()
{
$this->temporaryFolder = __DIR__ . "/tmp";
$this->configFolder = __DIR__ . "/config";
// make $readability_enabled available to $this
global $readability_enabled;
$this->readability_enabled = $readability_enabled;
}
/**
* @return Feed[]
*/
public function getFeeds ()
{
$feeds = array();
if ($handle = opendir($this->configFolder))
{
while (false !== ($file = readdir($handle)))
{
if (is_file($this->configFolder . "/" . $file) && $file != "DEFAULT.php" && substr($file, 0, 1) != ".")
{
$config = array();
require($this->configFolder . "/" . $file);
$feed = new Feed($config);
$feed->name = substr($file, 0, -4);
$feeds[] = $feed;
}
}
closedir($handle);
}
return $feeds;
}
/**
* @param $feedName
*
* @return Feed
*/
public function getFeed ($feedName)
{
if (preg_match("[^a-z0-9_-.]", $feedName) > 0)
{
return null;
}
if ($this->readability_enabled && preg_match("/^https?:\/\//", $feedName) > 0)
{
if ($this->readability_enabled && preg_match("#https?:\/\/#", $feedName) == 1)
{
// URL!!! Use readability with autogenerated configuration
$config = array();
$config['content'] = array("//", 0);
$config['url'] = $feedName;
$feed = new Feed($config);
$name = $feedName;
$name = preg_replace("#https?:\/\/#", "", $name);
$name = preg_replace("#\/#", "_", $name);
$feed->name = $name;
return $feed;
}
else
{
return null;
}
}
$feed = null;
if (is_file($this->configFolder . "/" . $feedName . ".php"))
{
$config = array();
require($this->configFolder . "/" . $feedName . ".php");
$feed = new Feed($config);
$feed->name = $feedName;
}
return $feed;
}
/**
* Gets a feed and returns the output
*
* @param Feed $feed
* @param bool $useCache
*
* @return string
*/
public function getFeedContent (Feed $feed, $useCache = true)
{
if (!is_dir($this->temporaryFolder))
{
mkdir($this->temporaryFolder);
if (!is_dir($this->temporaryFolder))
{
// ok just disable caching
$useCache = false;
}
}
$folder = $this->temporaryFolder . "/" . $feed->name;
if (!is_dir($folder))
{
mkdir($folder);
if (!is_dir($folder))
{
// ok just disable caching
$useCache = false;
}
}
$feedContent = $this->getContentOfUrl($feed->url);
if (! $feedContent)
{
return "<span style='font: #ff0000'>WARNING! Cannot open feed url: ". $feed->url . "!</span>";
}
$DOMDocument = new DOMDocument;
$DOMDocument->strictErrorChecking = false;
@$DOMDocument->loadXML($feedContent);
$lastTime = $this->traverseDomAndExtendItemsAndGetEarliestChangedTime($feed, $DOMDocument->childNodes, $useCache);
$content = @$DOMDocument->saveXML();
// clear old temporary files
if ($handle = opendir($folder))
{
while (false !== ($file = readdir($handle)))
{
$filePath = $folder . "/" . $file;
if (is_file($filePath) && filemtime($filePath) < $lastTime)
{
@unlink($filePath);
}
}
closedir($handle);
}
if (strlen($content) < 50)
{
$content .= "<span style='font: #ff0000'>WARNING! Your feed content is almost empty!</span>";
}
return $this->clearWhitespaces($content);
}
/**
* Traverse through the DOM and look out for items
*
* @param Feed $feed
* @param DOMNodeList $nodeList
* @param bool $useCache
* @return int
*/
private function traverseDomAndExtendItemsAndGetEarliestChangedTime (Feed $feed, DOMNodeList $nodeList, $useCache)
{
$time = 0;
foreach ($nodeList as $domElement)
{
if (substr($domElement->nodeName, 0, 1) != '#')
{
if (in_array($domElement->nodeName, $this->itemNames))
{
$currentTime = $this->extendItemAndGetChangedTime($feed, $domElement, $useCache);
$time = $currentTime > 0 ? $currentTime : $time;
}
if ($domElement->firstChild)
{
$currentTime = $this->traverseDomAndExtendItemsAndGetEarliestChangedTime($feed, $domElement->childNodes, $useCache);
$time = $currentTime > 0 ? $currentTime : $time;
}
}
}
return $time;
}
/**
* Extends one item with the full content of the specified link
*
* @param Feed $feed
* @param DOMElement $domElement
* @param bool $useCache
* @return int
*/
private function extendItemAndGetChangedTime (Feed $feed, DOMElement $domElement, $useCache)
{
/** @var $contentNodes DOMElement[] */
$contentNodes = array();
$link = "";
$time = 0;
foreach ($domElement->childNodes as $child)
{
/** @var $child DOMElement */
$name = $child->nodeName;
$value = $child->nodeValue;
if ($name == "link")
{
$link = $value;
if ($link == "")
{
$link = $child->getAttribute("href");
}
}
else if (in_array($name, $this->contentNames))
{
$contentNodes[] = $child;
}
else if (in_array($name, $this->timeNames))
{
$time = strtotime($value);
}
}
$filteredContent = $this->getFilteredContentOfUrl($feed, $link, $useCache, $time);
foreach ($contentNodes as $contentNode)
{
// replace old with new content
if ($filteredContent != "")
{
$newNode = $contentNode->ownerDocument->createElement($contentNode->nodeName);
$newNode->setAttribute("type", "html");
$textNode = $contentNode->ownerDocument->createTextNode($filteredContent);
$newNode->appendChild($textNode);
foreach ($contentNode->attributes as $attrName => $attrNode)
{
@$newNode->setAttribute($attrName, $attrNode);
}
$domElement->replaceChild($newNode, $contentNode);
}
// append warning!
else
{
@$contentNode->nodeValue .= "\n\n<br /><br /><span style='font: #ff0000'>WARNING! Your Rss-Extender rules returned an empty string for link: " . $link . "</span>";
}
}
return $time;
}
/**
* Get the content of the url and corrects it on forwards
*
* @param string $url
*
* @return string
*/
private function getContentOfUrl(&$url)
{
if (function_exists('curl_version'))
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, "rss-extender 0.6");
$content = curl_exec($curl);
$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
}
else if (ini_get('allow_url_fopen'))
{
$originalUrlParts = parse_url($url);
$context = stream_context_create(array('http' => array('header' => "Host: ".$originalUrlParts["host"]."\r\nUser-Agent: rss-extender 0.6", 'follow_location' => true, 'max_redirects' => '3')));
$content = @file_get_contents($url, false, $context);
if (empty($content))
{
return false;
}
foreach ($http_response_header as $header)
{
if (stripos($header, "location:", 0) === 0)
{
$originalUrlParts = parse_url($url);
$url = trim(substr($header, 9));
$newUrlParts = parse_url($url);
if (!isset($newUrlParts["host"]))
{
$url = isset($newUrlParts["scheme"]) ? $newUrlParts["scheme"] : $originalUrlParts["scheme"];
$url .= "://" . $originalUrlParts["host"];
if (isset($newUrlParts["user"]) && isset($newUrlParts["pass"]))
{
$url .= $newUrlParts["user"] . ":" . $newUrlParts["pass"] . "@";
}
if (isset($newUrlParts["path"]))
{
$url .= $newUrlParts["path"];
}
if (isset($newUrlParts["query"]))
{
$url .= "?" . $newUrlParts["query"];
}
if (isset($newUrlParts["fragment"]))
{
$url .= "#" . $newUrlParts["fragment"];
}
}
$content = $this->getContentOfUrl($url);
}
}
}
else
{
$content = "<span style='font: #ff0000'>WARNING! curl and allow_url_open are deactivated / disabled.</span>";
}
return $content;
}
/**
* @param Feed $feed
* @param string $url
* @param bool $useCache
* @param int $time
*
* @return string
*/
public function getFilteredContentOfUrl(Feed $feed, $url, $useCache, $time)
{
$baseUrl = $feed->baseUrl;
if ($baseUrl == "")
{
$parseUrl = parse_url($url);
$baseUrl = $parseUrl["scheme"] . "://" . $parseUrl["host"] . "/";
}
$file = $this->temporaryFolder . "/" . $feed->name . "/" . md5($url);
$originalContent = false;
// get the article from cache
if ($useCache && is_file($file) && filesize($file) > 0 && filemtime($file) == $time)
{
$originalContent = file_get_contents($file);
}
// load it from web
if (! $originalContent)
{
$originalContent = $this->getContentOfUrl($url);
if ($handle = @fopen($file, "w"))
{
fwrite($handle, $originalContent);
fclose($handle);
touch($file, $time);
}
}
if (! $originalContent)
{
return "<span style='font: #ff0000'>WARNING! Cannot open url: " . $url . "</span>";
}
// filter for main content
$filteredContent = "";
preg_match_all($feed->contentRegex, $originalContent, $match);
foreach ($match[$feed->contentRegexPosition] as $val)
{
$filteredContent .= $val;
}
// emtpy => use readability
if ($filteredContent == "" && $this->readability_enabled)
{
$filteredContent = $this->getFilteredContentOfUrlWithReadability($originalContent);
}
$filteredContent = $this->enhanceContent($feed, $url, $baseUrl, $filteredContent, true);
// empty -> use readability
if ($filteredContent == "" && $this->readability_enabled)
{
$filteredContent = $this->getFilteredContentOfUrlWithReadability($originalContent);
$filteredContent = $this->enhanceContent($feed, $url, $baseUrl, $filteredContent, false);
}
// multiple page splitting routine
if ($feed->contentSplitRegex != "")
{
preg_match($feed->contentSplitRegex, $originalContent, $match);
if (isset($match[$feed->contentSplitRegexPosition]) && $match[$feed->contentSplitRegexPosition])
{
$filteredContent = preg_replace($feed->contentSplitRegex, "", $filteredContent);
$filteredContent .= "\n<br><hr><br>\n";
$next_page = $match[$feed->contentSplitRegexPosition];
$filteredContent .= $this->getFilteredContentOfUrl($feed, $baseUrl . $next_page, $useCache, $time);
}
}
return $this->clearWhitespaces($filteredContent);
}
/**
* @param string $html
*
* return string
*/
private function getFilteredContentOfUrlWithReadability($html)
{
$configuration = new andreskrey\Readability\Configuration();
// remove scripts
$configuration->setSummonCthulhu(true);
$configuration->setArticleByLine(true);
$readability = new andreskrey\Readability\Readability($configuration);
try
{
$readability->parse($html);
$filteredContent = "";
// FORMAT: MAIN IMAGE -- CONTENT -- OTHER IMAGES -- Readability note
$image_path = parse_url($readability->getImage(), PHP_URL_PATH);
$image_path = preg_replace("/jpg.*/i", "jpg", $image_path);
$image_path = preg_replace("/_.*/i", "", $image_path);
$content_no_meta = $readability->getContent();
$content_no_meta = preg_replace("#<meta[^>]*><\/meta>#Uis", "", $content_no_meta);
if ($image_path != "" && stristr($content_no_meta, $image_path) === FALSE)
{
$filteredContent .= "<img src=\"";
$filteredContent .= $readability->getImage();
$filteredContent .= "\" />";
}
$filteredContent .= "<h1>" . $readability->getTitle() . "</h1>";
if ($readability->getAuthor() != "" )
{
$filteredContent .= "<h2>By " . $readability->getAuthor() . "</h2>";
}
$filteredContent .= $content_no_meta;
foreach ($readability->getImages() as $image)
{
if ($image != $readability->getImage() && stristr($content_no_meta, $image) === FALSE)
{
$filteredContent .= "<img src=\"";
$filteredContent .= $image;
$filteredContent .= "\" />";
}
}
$filteredContent .= "<br><span style='font: #ff0000'>Generated by <a href='https://github.com/andreskrey/readability.php'>Readability.php</a>.</span>";
return $filteredContent;
}
catch (ParseException $e)
{
return sprintf('Readability Error: %s', $e->getMessage());
}
}
/**
* @param Feed $feed
* @param string $url
* @param string $filteredContent
* @param bool $doSearchReplace
*
* @return string
*/
private function enhanceContent(Feed $feed, $url, $baseUrl, $filteredContent, $doSearchReplace)
{
// image relative to absolute paths
$imagePath = substr($url, 0, strrpos($url, "/"));
$filteredContent = preg_replace("/(<(img|IMG)[^>]*[\s]+src[\s]*=[\s]*(\"|'))(\/)([^\"']+)(\"|')/i", "$1" . $baseUrl . "/$5$3", $filteredContent);
$filteredContent = preg_replace("/(<(img|IMG)[^>]*[\s]+src[\s]*=[\s]*(\"|'))([^\"':]+)(\"|')/i", "$1" . $imagePath . "/$4$3", $filteredContent);
// links relative to absolute paths
$filteredContent = preg_replace("/([\s]+href[\s]*=[\s]*(\"|'))(\/)([^\"']+)(\"|')/i", "$1" . $baseUrl . "/$4$2", $filteredContent);
// search and replace stuff
if ($doSearchReplace && is_array($feed->searchContent) && count($feed->searchContent) > 0)
{
$filteredContent = preg_replace($feed->searchContent, $feed->replaceContent, $filteredContent);
}
return $filteredContent;
}
/**
* @param string $content
*
* @return string
*/
private function clearWhitespaces ($content)
{
return trim($content, "\n\r\t");
}
}