-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
751 lines (688 loc) · 34.4 KB
/
index.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
<?php
define('DEBUG', false);
define('MIN_DOWNLOAD_DELAY', 0.12);
define('MAX_DOWNLOAD_DELAY', 0.94);
define('USE_WEBPAGE_CACHING', true);
libxml_use_internal_errors(true);
function allElementsAreArrays($array) {
foreach ($array as $val) {
if (!is_array($val)) {
return false;
}
}
return true;
}
function wrapInArray($arg) {
if (!is_array($arg)) {
return array($arg);
} else {
return $arg;
}
}
function map($traversable, $fn)
{
$result = array();
foreach ($traversable as $element) {
$result[] = $fn($element);
}
return $result;
}
function chainArrays($array) {
$result = array();
foreach ($array as $val) {
if (is_array($val)) {
$result = array_merge($result, chainArrays($val));
} else {
array_push($result, $val);
}
}
return $result;
}
function display_xml_error($error) {
$return = "";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "Warning $error->code: ";
break;
case LIBXML_ERR_ERROR:
$return .= "Error $error->code: ";
break;
case LIBXML_ERR_FATAL:
$return .= "Fatal Error $error->code: ";
break;
}
$return .= trim($error->message);
return $return;
}
class TagNotFoundException extends Exception {
public function __construct($tag_name)
{
parent::__construct("No tags with name " . $tag_name, 400);
}
}
class RSSEditor
{
/**
* @var string|null Plain source code of document
*/
protected $plainText = null;
/**
* @var DOMDocument XML document as valid DOMDocument object
*/
protected $xml;
/**
* Check pair matching of two objects.
*
* @param $firstObject mixed
* @param $secondObject mixed
*
* @throws InvalidArgumentException Only one of two arguments is array or arrays do not have same length
*/
protected function checkPairArgumentsMatching($firstObject, $secondObject) {
if (is_array($firstObject)) {
if (!is_array($secondObject) || count($firstObject) !== count($secondObject)) {
throw new InvalidArgumentException("both arrays must have equal lengths");
}
} else {
if (is_array($secondObject)) {
throw new InvalidArgumentException("both objects must be arrays or not at the same time");
}
}
}
/**
* Apply action with passing two arguments.
* If arguments are arrays with same length then apply action for each pair of these element by index.
* Otherwise apply only single time with arguments.
*
* @param $action callable
* @param $arguments mixed Single argument or array of arguments for $action
*/
protected function applyAction($action, $arguments) {
if (is_array($arguments)) {
if (allElementsAreArrays($arguments)) {
foreach ($arguments as $args) {
call_user_func_array($action, $args);
}
} else {
foreach ($arguments as $arg) {
call_user_func($action, $arg);
}
}
} else {
call_user_func($action, $arguments);
}
}
/**
* Rename all tags with given name (without attributes and children)
*
* @param $oldTagName string Current name of the tags
* @param $newTagName string New name for the tags
*
* @throws TagNotFoundException if no tags with $oldTagName are found
*/
protected function renameUnifiedTags($oldTagName, $newTagName) {
$nodes = iterator_to_array($this->xml->getElementsByTagName($oldTagName));
if (count($nodes) == 0) {
throw new TagNotFoundException($oldTagName);
};
foreach ($nodes as $node) {
$newNode = $this->xml->createElement($newTagName);
$newNode->nodeValue = htmlspecialchars($node->nodeValue);
$node->parentNode->appendChild($newNode);
$node->parentNode->removeChild($node);
};
}
/**
* Remove all tags with given name (without children)
*
* @param $tagName string|string[] Name of the tag
*
* @throws TagNotFoundException if no tags with name $tagName are found
*/
protected function removeUnifiedTags($tagName) {
$nodes = iterator_to_array($this->xml->getElementsByTagName($tagName));
if (count($nodes) == 0) {
throw new TagNotFoundException($tagName);
};
foreach ($nodes as $node) {
$node->parentNode->removeChild($node);
}
}
/**
* Split content of the tags with given name using <br> CDATA block (without attributes)
*
* @param $tagName string Name of the old tag
*
* @throws TagNotFoundException if no tags with name $tagName are found
*/
protected function insertBreaksIntoUnifiedTags($tagName) {
$nodes = $this->xml->getElementsByTagName($tagName);
if ($nodes->length == 0) {
throw new TagNotFoundException($tagName);
}
foreach ($nodes as $node) {
$result = preg_replace('/([\r\n]|&#x[Dd];| )+(\s+([\r\n]|&#x[Dd];| )+)*/u', '<br/>', $node->nodeValue);
if ($node->childNodes->item(0)->nodeType === XML_CDATA_SECTION_NODE) {
$node->replaceChild($this->xml->createCDATASection($result), $node->childNodes->item(0));
} else {
$node->nodeValue = htmlspecialchars($result);
}
}
}
/**
* Mark content of the tags with given name as CDATA block (without attributes)
*
* @param $tagName string Name of the old tag
*
* @throws TagNotFoundException if no tags with name $tagName are found
*/
protected function cdataUnifiedTags($tagName) {
$nodes = $this->xml->getElementsByTagName($tagName);
if ($nodes->length == 0) {
throw new TagNotFoundException($tagName);
}
foreach ($nodes as $node) {
$CDATASection = $this->xml->createCDATASection($node->nodeValue);
$node->nodeValue = '';
$node->appendChild($CDATASection);
}
}
/**
* Replace each match of given regular expression using given replace rule (regular expression in PCRE notation).
* Modifier 'u' is used for documents processing with UTF-8 encoding.
*
* @param $replaceFrom string Regex to find matches.
* @param $replaceTo string Regex to replace found matches.
* @param $tagName string Node name which regex is applied in.
* @param $isCaseSensitive string If false then modifier 'i' is applied (searching without case sensitive) else case sensitive searching.
*
* @throws TagNotFoundException if no tags with name $tagName are found
*/
protected function replaceContentInHomonymousTags($replaceFrom, $replaceTo, $tagName, $isCaseSensitive) {
$nodes = $this->xml->getElementsByTagName($tagName);
if ($nodes->length == 0) {
throw new TagNotFoundException($tagName);
}
$replaceFrom = str_replace('/', '\/', $replaceFrom);
foreach ($nodes as $node) {
if ($node->childNodes->length <= 2) {
foreach($node->childNodes as $childNode) {
if ($childNode->nodeType === XML_TEXT_NODE || $childNode->nodeType === XML_CDATA_SECTION_NODE) {
$result = preg_replace($isCaseSensitive ? "/$replaceFrom/u" : "/$replaceFrom/iu",
$replaceTo, $childNode->nodeValue);
if ($childNode->nodeType === XML_TEXT_NODE) {
$childNode->nodeValue = htmlspecialchars($result);
} else {
$childNode->nodeValue = '';
$childNode->appendChild($this->xml->createCDATASection($result));
}
}
}
}
}
}
/**
* @param $url string
* @param $context resource Stream context
* @param $replaceAmpToSymbol boolean replace each '&' to '&' symbol
* @param $htmlEntitiesToNumeric boolean replace all HTML entities to its numeric representation
* @param $addNamespace string add additional RSS-namespace(s)
* @param $need_webpage_caching boolean whether the necessity to cache downloaded web-pages
*
* @throws Exception if cannot download RSS feed or cannot generate valid XML from RSS feed
*/
public function __construct($url, $context, $replaceAmpToSymbol,
$htmlEntitiesToNumeric, $addNamespace,
$need_webpage_caching = USE_WEBPAGE_CACHING) {
$this->plainText = @file_get_contents($url, FILE_TEXT, $context);
if (defined('DEBUG') && DEBUG) {
file_put_contents('last_raw_rss.xml', join(PHP_EOL, $http_response_header) . PHP_EOL . $this->plainText);
}
if (!isset($http_response_header) || !preg_match('@HTTP/\d+.\d+ 200 OK@', $http_response_header[0])) {
if (isset($http_response_header)) {
$status = ": {$http_response_header[0]}";
} else {
$status = "";
}
throw new Exception("Cannot download RSS feed from URL '{$url}'{$status}", 500);
}
if (!$this->plainText) {
throw new Exception("Cannot download RSS feed from URL '{$url}'", 500);
}
if ($replaceAmpToSymbol) {
$this->plainText = preg_replace('/&(?=[a-z]{2})/', "&", $this->plainText, FILE_TEXT);
}
// replace & into code if it is not encoded symbol
$this->plainText = preg_replace("/&(?!([a-z]+|#[0-9]+);)/", "&", $this->plainText);
if ($htmlEntitiesToNumeric) {
$this->htmlEntitiesToNumeric();
}
if (!is_null($addNamespace)) {
$this->plainText = str_replace('<rss', '<rss ' . $addNamespace, $this->plainText);
}
if (defined('DEBUG') && DEBUG) {
file_put_contents('last_preprocessed_rss.xml', join(PHP_EOL, $http_response_header) . PHP_EOL . $this->plainText);
}
$this->xml = new DOMDocument();
if (!$this->xml->loadXML($this->plainText)) {
$errors = libxml_get_errors();
throw new Exception("Cannot form valid XML: "
. join(PHP_EOL, array_map('display_xml_error', $errors)),
500);
}
$this->entry_webpages = array();
$this->use_webpage_caching = $need_webpage_caching;
header("Content-type: text/xml; charset={$this->xml->encoding}");
}
/**
* Convert HTML entities to its numeric representation for supporting in XML
*/
protected function htmlEntitiesToNumeric() {
$htmlEntities = array('"', '<', '>', 'Œ', 'œ', 'Š', 'š', 'Ÿ', 'ˆ', '˜', ' ', ' ', ' ', '‌', '‍', '‎', '‏', '–', '—', '‘', '’', '‚', '“', '”', '„', '†', '‡', '‰', '‹', '›', '€', 'ƒ', 'Α', 'Β', 'Γ', 'Δ', 'Ε', 'Ζ', 'Η', 'Θ', 'Ι', 'Κ', 'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Π', 'Ρ', 'Σ', 'Τ', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω', 'α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'π', 'ρ', 'ς', 'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω', 'ϑ', 'ϒ', 'ϖ', '•', '…', '′', '″', '‾', '⁄', '℘', 'ℑ', 'ℜ', '™', 'ℵ', '←', '↑', '→', '↓', '↔', '↵', '⇐', '⇑', '⇒', '⇓', '⇔', '∀', '∂', '∃', '∅', '∇', '∈', '∉', '∋', '∏', '∑', '−', '∗', '√', '∝', '∞', '∠', '∧', '∨', '∩', '∪', '∫', '∴', '∼', '≅', '≈', '≠', '≡', '≤', '≥', '⊂', '⊃', '⊄', '⊆', '⊇', '⊕', '⊗', '⊥', '⋅', '⌈', '⌉', '⌊', '⌋', '⟨', '⟩', '◊', '♠', '♣', '♥', '♦', ' ', '¡', '¢', '£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
$htmlEntitiesCode = array('"', '<', '>', 'Œ', 'œ', 'Š', 'š', 'Ÿ', 'ˆ', '˜', ' ', ' ', ' ', '‌', '‍', '‎', '‏', '–', '—', '‘', '’', '‚', '“', '”', '„', '†', '‡', '‰', '‹', '›', '€', 'ƒ', 'Α', 'Β', 'Γ', 'Δ', 'Ε', 'Ζ', 'Η', 'Θ', 'Ι', 'Κ', 'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Π', 'Ρ', 'Σ', 'Τ', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω', 'α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'π', 'ρ', 'ς', 'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω', 'ϑ', 'ϒ', 'ϖ', '•', '…', '′', '″', '‾', '⁄', '℘', 'ℑ', 'ℜ', '™', 'ℵ', '←', '↑', '→', '↓', '↔', '↵', '⇐', '⇑', '⇒', '⇓', '⇔', '∀', '∂', '∃', '∅', '∇', '∈', '∉', '∋', '∏', '∑', '−', '∗', '√', '∝', '∞', '∠', '∧', '∨', '∩', '∪', '∫', '∴', '∼', '≅', '≈', '≠', '≡', '≤', '≥', '⊂', '⊃', '⊄', '⊆', '⊇', '⊕', '⊗', '⊥', '⋅', '⌈', '⌉', '⌊', '⌋', '〈', '〉', '◊', '♠', '♣', '♥', '♦', ' ', '¡', '¢', '£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ');
$this->plainText = str_replace($htmlEntities, $htmlEntitiesCode, $this->plainText);
}
/**
* Rename all tags with given name (without attributes and children)
*
* @param $oldTagName string|string[] Current name of the tags
* @param $newTagName string|string[] New name for the tags
*
* @throws InvalidArgumentException if pair(s) of $oldTagName and $newTagName does not match
*/
public function renameTags($oldTagName, $newTagName) {
$oldTagName = wrapInArray($oldTagName);
$newTagName = wrapInArray($newTagName);
try {
$this->checkPairArgumentsMatching($oldTagName, $newTagName);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException("Invalid rename_from/rename_to parameters: " . $e->getMessage(), 400);
}
$this->applyAction(array($this, 'renameUnifiedTags'),
array_map(function($ind) use ($oldTagName, $newTagName)
{return array($oldTagName[$ind], $newTagName[$ind]);},
range(0, count($oldTagName)-1)));
}
/**
* Remove tags with given name
*
* @param $tagName string
*/
public function removeTags($tagName) {
$this->applyAction(array($this, 'removeUnifiedTags'), $tagName);
}
/**
* Insert <br/> instead of line breaks in the content of tags with given name
*
* @param $tagName string
*/
public function insertBreaksIntoTags($tagName)
{
$this->applyAction(array($this, 'insertBreaksIntoUnifiedTags'), $tagName);
}
/**
* Wrap content of the tags with given name using CDATA
*
* @param $tagName string
*/
public function cdataTags($tagName)
{
$this->applyAction(array($this, 'cdataUnifiedTags'), $tagName);
}
/**
* Split the content of the tags with given name into tags with the same name
* that contains lower cased words from source content that is extracted using CamelCase rules
*
* Example:
* before
* <tag>CamelCaseSTRING</tag>
* after
* <tag>camel</tag><tag>case</tag><tag>string</tag>
*
* @param $tagName string
*
* @throws TagNotFoundException if no tags with name $tagName are found
*/
public function splitCamelCase($tagName) {
$nodes = iterator_to_array($this->xml->getElementsByTagName($tagName));
if (count($nodes) == 0) {
throw new TagNotFoundException($tagName);
}
foreach ($nodes as $node) {
$matches = array();
preg_match_all("/((?:^|[A-ZА-Я])[a-zа-я]+|[A-ZА-Я]+(?=[А-Я]|$))/u", $node->nodeValue, $matches);
foreach ($matches[0] as $match) {
$newNode = $this->xml->createElement($tagName);
$newNode->appendChild($this->xml->createCDATASection(mb_strtolower($match)));
$node->parentNode->appendChild($newNode);
}
$node->parentNode->removeChild($node);
}
}
/**
* Content replacing based on regular expressions in PCRE notation.
* Replace each match of given regular expression using given replace rule.
* For documents with UTF-8 encoding.
*
* @param $replaceFrom string|string[] Regex to find matches.
* @param $replaceTo string|string[] Regex to replace found matches.
* @param $replaceInTag string|string[] Node name which regex is applied in.
* @param $isCaseSensitive string|string[] If false then modifier 'i' is applied (searching without case sensitive) else case sensitive searching.
*
* @throws InvalidArgumentException if pair(s) of $oldTagName and $newTagName does not match
*/
public function replaceContent($replaceFrom, $replaceTo, $replaceInTag, $isCaseSensitive) {
$replaceFrom = wrapInArray($replaceFrom);
$replaceTo = wrapInArray($replaceTo);
try {
$this->checkPairArgumentsMatching($replaceFrom, $replaceTo);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException("Invalid replace_from/replace_to parameters: " . $e->getMessage(), 400);
}
if (is_array($replaceInTag)) {
try {
$this->checkPairArgumentsMatching($replaceFrom, $replaceInTag);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException("Invalid replace_from/replace_to/replace_in parameters: " . $e->getMessage(), 400);
}
} else {
$replaceInTag = array_fill(0, count($replaceFrom), $replaceInTag);
}
if (is_array($isCaseSensitive)) {
try {
$this->checkPairArgumentsMatching($replaceFrom, $isCaseSensitive);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException("Invalid replace_from/replace_to/replace_sens parameters: " . $e->getMessage(), 400);
}
} else {
$isCaseSensitive = array_fill(0, count($replaceFrom), $isCaseSensitive);
}
$this->applyAction(array($this, 'replaceContentInHomonymousTags'),
array_map(function($ind) use ($replaceFrom, $replaceTo, $replaceInTag, $isCaseSensitive)
{ return array($replaceFrom[$ind], $replaceTo[$ind], $replaceInTag[$ind], $isCaseSensitive[$ind]);},
range(0, count($replaceFrom)-1)));
}
public function filter($include=array(), $exclude=array()) {
$include = wrapInArray($include);
$exclude = wrapInArray($exclude);
$items = iterator_to_array($this->xml->getElementsByTagName("item"));
foreach ($items as $feed_item) {
$title = $feed_item->getElementsByTagName('title');
$title = ($title->length > 0) ? $title->item(0)->nodeValue : '';
$description = $feed_item->getElementsByTagName('description');
$description = ($description->length > 0) ? $description->item(0)->nodeValue : '';
$categories = $feed_item->getElementsByTagName('category');
if ($categories->length > 0) {
$categories = join(' ', array_map(function ($cat) {return $cat->nodeValue;},
iterator_to_array($categories)));
} else {
$categories = '';
}
$leave = true;
if ($include) {
$leave = false;
foreach ($include as $user_regex) {
$regex = '/' . preg_replace("/(?<!\\\)\//u", "\\/", $user_regex) . '/iu';
$title_matched = preg_match($regex, $title);
$description_matched = preg_match($regex, $description);
$categories_matched = preg_match($regex, $categories);
if ($title_matched === false || $description_matched === false || $categories_matched === false) {
throw new InvalidArgumentException("Invalid regular expression: {$user_regex}");
}
if ($title_matched || $description_matched || $categories_matched) {
$leave = true;
break;
}
}
}
if ($exclude && $leave) {
foreach ($exclude as $user_regex) {
$regex = '/' . preg_replace("/(?<!\\\)\//u", "\\/", $user_regex) . '/iu';
$title_matched = preg_match($regex, $title);
$description_matched = preg_match($regex, $description);
$categories_matched = preg_match($regex, $categories);
if ($title_matched === false || $description_matched === false || $categories_matched === false) {
throw new InvalidArgumentException("Invalid regular expression: {$user_regex}");
}
if ($title_matched || $description_matched || $categories_matched) {
$leave = false;
break;
}
}
}
if (!$leave) {
$feed_item->parentNode->removeChild($feed_item);
}
}
}
/**
* Add categories to each feed item from its web-page ('link' element) using XPath expression
* that extract list of category names from web-page.
* If feed item does not have link then this item is ignored.
*
* @param $feedItem DOMNode Feed item node
* @param $xpath string XPath 1.0 expression that extracts category names
* @return DOMNodeList Extracted data using XPath expression
*
* @throws TagNotFoundException if feed item does not contain 'link' element
* @throws InvalidArgumentException if invalid XPath 1.0 expression is passed
*/
protected function xpathQuery($feedItem, $xpath) {
$entry_link = $feedItem->getElementsByTagName("link");
if ($entry_link->length == 0) {
throw new TagNotFoundException("link");
}
$entry_link = $entry_link->item(0)->nodeValue;
if ($this->use_webpage_caching && isset($this->entry_webpages[$entry_link])) {
$entry_doc = $this->entry_webpages[$entry_link];
} else {
$entry_page = new DOMDocument();
@$entry_page->loadHTMLFile($entry_link);
$entry_doc = new DOMXPath($entry_page);
if ($this->use_webpage_caching) {
$this->entry_webpages[$entry_link] = $entry_doc;
}
}
$result = @($entry_doc->query($xpath));
if ($result === false) {
throw new InvalidArgumentException("Invalid XPath expression < {$xpath} >: " . error_get_last()['message']);
}
return $result;
}
/**
* Add categories to each feed item from its web-page ('link' element) using XPath expression
* that extract list of category names from web-page.
* If some feed item does not have link then this item is ignored.
*
* @param $xpath string XPath 1.0 expression that extracts category names
*/
public function addCategory($xpath) {
foreach ($this->xml->getElementsByTagName("item") as $feed_item) {
try {
$categories = $this->xpathQuery($feed_item, $xpath);
sleep(mt_rand() / mt_getrandmax() * (MAX_DOWNLOAD_DELAY - MIN_DOWNLOAD_DELAY) + MIN_DOWNLOAD_DELAY);
} catch (TagNotFoundException $exc) {
continue;
}
$entry_categories = map($feed_item->getElementsByTagName("category"),
function($cat) {return trim($cat->nodeValue);});
foreach ($categories as $category) {
$category_name = trim($category->nodeValue, " \t\n\r\0\x0B,;. ");
if (!in_array($category_name, $entry_categories)) {
$category_element = $feed_item->appendChild($this->xml->createElement("category"));
$category_element->appendChild($this->xml->createCDATASection($category_name));
}
}
}
}
/**
* Replace description of each feed item with its web-page content (with URL from 'link' element)
* that extracted using XPath expression.
* If some feed item does not have link then this item is ignored.
*
* @param $xpath string XPath 1.0 expression that extracts category names
*/
public function replaceDescription($xpath) {
foreach ($this->xml->getElementsByTagName("item") as $feed_item) {
try {
$paragraphs = $this->xpathQuery($feed_item, $xpath);
sleep(mt_rand() / mt_getrandmax() * (MAX_DOWNLOAD_DELAY - MIN_DOWNLOAD_DELAY) + MIN_DOWNLOAD_DELAY);
} catch (TagNotFoundException $exc) {
continue;
}
$description = implode(PHP_EOL, map($paragraphs, function ($par) {return trim($par->C14N());}));
$description_elements = $feed_item->getElementsByTagName("description");
foreach ($description_elements as $description_element) {
$feed_item->removeChild($description_element);
}
$description_element = $feed_item->appendChild($this->xml->createElement("description"));
$description_element->appendChild($this->xml->createCDATASection($description));
}
}
/**
* Extend description of each feed item with its web-page content (with URL from 'link' element)
* that extracted using XPath expression.
* If some feed item does not have link then this item is ignored.
*
* @param $xpath string XPath 1.0 expression that extracts category names
*/
public function extendDescription($xpath) {
foreach ($this->xml->getElementsByTagName("item") as $feed_item) {
try {
$paragraphs = $this->xpathQuery($feed_item, $xpath);
} catch (TagNotFoundException $exc) {
continue;
}
$description = implode(PHP_EOL, map($paragraphs, function ($par) {return trim($par->C14N());}));
$description_elements = $feed_item->getElementsByTagName("description");
if ($description_elements->length > 0) {
$description_element = $description_elements->item(0);
$src_description = $description_elements->item(0)->nodeValue;
$feed_item->removeChild($description_element);
$description = $src_description . PHP_EOL . $description;
}
$description_element = $feed_item->appendChild($this->xml->createElement("description"));
$description_element->appendChild($this->xml->createCDATASection($description));
}
}
/**
* Get current XML as a plain text
*
* @return string current XML as a plain text
*/
public function getXMLAsText()
{
return $this->xml->saveXML();
}
}
try {
$opts = array(
'http' => array(
'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20130401 Firefox/21.0',
)
);
$outer_encoding = mb_internal_encoding();
if (!mb_internal_encoding("UTF-8")) {
throw new Exception("Cannot set encoding UTF-8 for multibyte strings", 500);
}
$context = stream_context_create($opts);
libxml_set_streams_context($context);
$allowed_parameters = array(
'url',
'amp',
'add_namespace',
'remove',
'break',
'split',
'cdata',
'add_category',
'replace_description',
'extend_description',
array('rename_from', 'rename_to'),
array('replace_from', 'replace_to', 'replace_in'),
'replace_sens',
'include',
'exclude'
);
$downloading_parameters = array(
'add_category',
'replace_description',
'extend_description',
);
if (!isset($_GET['url'])) {
throw new InvalidArgumentException("No url of RSS for processing", 400);
}
$url = $_GET['url'];
$passed_params = array_keys($_GET);
$matched_params = preg_grep('/index[._]php$/u', $passed_params);
if (!empty($matched_params)) {
unset($_GET[$matched_params[0]]);
}
$all_allowed_parameters = chainArrays($allowed_parameters);
$unregistered_parameters = array_diff(array_keys($_GET), $all_allowed_parameters);
if (!empty($unregistered_parameters)) {
throw new Exception("Unknown parameters: " . implode(', ', $unregistered_parameters)
. ". Supported parameters: " . implode(', ', $all_allowed_parameters), 400);
}
if (!(isset($_GET['amp']) ||
isset($_GET['add_namespace']) ||
isset($_GET['remove']) ||
isset($_GET['break']) ||
isset($_GET['split']) ||
isset($_GET['cdata']) ||
isset($_GET['add_category']) ||
isset($_GET['replace_description']) ||
isset($_GET['extend_description'])) &&
((isset($_GET['rename_from']) || isset($_GET['rename_to'])) &&
(!isset($_GET['rename_from']) || !isset($_GET['rename_to'])) ||
(isset($_GET['replace_from']) || isset($_GET['replace_to']) || isset($_GET['replace_in'])) &&
(!isset($_GET['replace_from']) || !isset($_GET['replace_to']) || !isset($_GET['replace_in'])))
) {
throw new InvalidArgumentException("Incomplete parameters (must be rename_from and rename_to OR remove " .
"OR replace_description OR extend_description OR break OR cdata " .
"OR replace_from and replace_to and replace_in OR addCategory");
}
$feed = new RSSEditor($url, $context,
isset($_GET['amp']), true, @$_GET['add_namespace'],
USE_WEBPAGE_CACHING && count(array_intersect(array_keys($_GET), $downloading_parameters)) > 1);
if (isset($_GET['remove'])) {
$feed->removeTags($_GET['remove']);
}
if (isset($_GET['rename_from']) && isset($_GET['rename_to'])) {
$feed->renameTags($_GET['rename_from'], $_GET['rename_to']);
}
if (isset($_GET['split'])) {
$feed->splitCamelCase($_GET['split']);
}
if (isset($_GET['replace_description'])) {
$feed->replaceDescription($_GET['replace_description']);
}
if (isset($_GET['extend_description'])) {
$feed->extendDescription($_GET['extend_description']);
}
if (isset($_GET['break'])) {
$feed->insertBreaksIntoTags($_GET['break']);
}
if (isset($_GET['cdata'])) {
$feed->cdataTags($_GET['cdata']);
}
if (isset($_GET['replace_from']) && isset($_GET['replace_to']) && isset($_GET['replace_in'])) {
$feed->replaceContent($_GET['replace_from'], $_GET['replace_to'],
$_GET['replace_in'], isset($_GET['replace_sens']));
}
if (isset($_GET['add_category'])) {
$feed->addCategory($_GET['add_category']);
}
if (isset($_GET['include']) || isset($_GET['exclude'])) {
$include = isset($_GET['include']) ? $_GET['include'] : array();
$exclude= isset($_GET['exclude']) ? $_GET['exclude'] : array();
$feed->filter($include, $exclude);
}
echo $feed->getXMLAsText();
mb_internal_encoding($outer_encoding);
} catch (Exception $exc) {
header("HTTP/1.1 {$exc->getCode()} {$exc->getMessage()}");
header("Content-type: text/plain; charset=UTF-8");
error_log($exc);
die($exc->getMessage());
}