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