]>
Commit | Line | Data |
---|---|---|
cc85704f | 1 | <?php |
5a0e0392 | 2 | class RedditImgur extends Plugin { |
cc85704f | 3 | |
19c73507 AD |
4 | private $link; |
5 | private $host; | |
6 | ||
7a866114 AD |
7 | function _about() { |
8 | return array(1.0, | |
9 | "Inline image links in Reddit RSS feeds", | |
10 | "fox"); | |
11 | } | |
12 | ||
19c73507 AD |
13 | function __construct($host) { |
14 | $this->link = $host->get_link(); | |
15 | $this->host = $host; | |
16 | ||
17 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); | |
18 | } | |
19 | ||
20 | function hook_article_filter($article) { | |
cc85704f AD |
21 | |
22 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { | |
23 | if (strpos($article["content"], "i.imgur.com") !== FALSE) { | |
24 | ||
25 | $doc = new DOMDocument(); | |
26 | @$doc->loadHTML($article["content"]); | |
27 | ||
28 | if ($doc) { | |
29 | $xpath = new DOMXPath($doc); | |
30 | $entries = $xpath->query('(//a[@href]|//img[@src])'); | |
31 | ||
32 | foreach ($entries as $entry) { | |
33 | if ($entry->hasAttribute("href")) { | |
dd8c289b | 34 | if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) { |
cc85704f AD |
35 | |
36 | $img = $doc->createElement('img'); | |
37 | $img->setAttribute("src", $entry->getAttribute("href")); | |
38 | ||
39 | $entry->parentNode->replaceChild($img, $entry); | |
40 | } | |
41 | } | |
42 | ||
43 | // remove tiny thumbnails | |
44 | if ($entry->hasAttribute("src")) { | |
45 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
46 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
47 | } | |
48 | } | |
49 | } | |
50 | ||
51 | $node = $doc->getElementsByTagName('body')->item(0); | |
52 | ||
53 | if ($node) { | |
54 | $article["content"] = $doc->saveXML($node, LIBXML_NOEMPTYTAG); | |
55 | } | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | return $article; | |
61 | } | |
62 | } | |
63 | ?> |