]>
Commit | Line | Data |
---|---|---|
cc85704f AD |
1 | <?php |
2 | class Filter_RedditImgur { | |
3 | ||
4 | function filter_article($article) { | |
5 | ||
6 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { | |
7 | if (strpos($article["content"], "i.imgur.com") !== FALSE) { | |
8 | ||
9 | $doc = new DOMDocument(); | |
10 | @$doc->loadHTML($article["content"]); | |
11 | ||
12 | if ($doc) { | |
13 | $xpath = new DOMXPath($doc); | |
14 | $entries = $xpath->query('(//a[@href]|//img[@src])'); | |
15 | ||
16 | foreach ($entries as $entry) { | |
17 | if ($entry->hasAttribute("href")) { | |
dd8c289b | 18 | if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) { |
cc85704f AD |
19 | |
20 | $img = $doc->createElement('img'); | |
21 | $img->setAttribute("src", $entry->getAttribute("href")); | |
22 | ||
23 | $entry->parentNode->replaceChild($img, $entry); | |
24 | } | |
25 | } | |
26 | ||
27 | // remove tiny thumbnails | |
28 | if ($entry->hasAttribute("src")) { | |
29 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
30 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
31 | } | |
32 | } | |
33 | } | |
34 | ||
35 | $node = $doc->getElementsByTagName('body')->item(0); | |
36 | ||
37 | if ($node) { | |
38 | $article["content"] = $doc->saveXML($node, LIBXML_NOEMPTYTAG); | |
39 | } | |
40 | } | |
41 | } | |
42 | } | |
43 | ||
44 | return $article; | |
45 | } | |
46 | } | |
47 | ?> |