]>
Commit | Line | Data |
---|---|---|
cc85704f | 1 | <?php |
0862a602 | 2 | class Af_RedditImgur extends Plugin { |
cc85704f | 3 | |
19c73507 AD |
4 | private $link; |
5 | private $host; | |
6 | ||
d2a421e3 | 7 | function about() { |
7a866114 AD |
8 | return array(1.0, |
9 | "Inline image links in Reddit RSS feeds", | |
10 | "fox"); | |
11 | } | |
12 | ||
d2a421e3 | 13 | function init($host) { |
19c73507 AD |
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) { | |
a16d7a5d | 21 | $owner_uid = $article["owner_uid"]; |
cc85704f | 22 | |
0b5ef30d AD |
23 | $force = false; |
24 | ||
cc85704f | 25 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { |
0b5ef30d | 26 | if (strpos($article["plugin_data"], "redditimgur,$owner_uid:") === FALSE || $force) { |
a16d7a5d AD |
27 | $doc = new DOMDocument(); |
28 | @$doc->loadHTML($article["content"]); | |
cc85704f | 29 | |
a16d7a5d AD |
30 | if ($doc) { |
31 | $xpath = new DOMXPath($doc); | |
32 | $entries = $xpath->query('(//a[@href]|//img[@src])'); | |
cc85704f | 33 | |
ce7d5e87 AD |
34 | $found = false; |
35 | ||
a16d7a5d AD |
36 | foreach ($entries as $entry) { |
37 | if ($entry->hasAttribute("href")) { | |
38 | if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) { | |
cc85704f | 39 | |
a16d7a5d AD |
40 | $img = $doc->createElement('img'); |
41 | $img->setAttribute("src", $entry->getAttribute("href")); | |
cc85704f | 42 | |
a16d7a5d | 43 | $entry->parentNode->replaceChild($img, $entry); |
ce7d5e87 AD |
44 | |
45 | $found = true; | |
46 | } | |
47 | ||
48 | // links to imgur pages | |
49 | $matches = array(); | |
35055d05 | 50 | if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
ce7d5e87 AD |
51 | |
52 | $token = $matches[1]; | |
53 | ||
54 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
55 | false, false, false, false, 10); | |
56 | ||
57 | if ($album_content && $token) { | |
58 | $adoc = new DOMDocument(); | |
59 | @$adoc->loadHTML($album_content); | |
60 | ||
61 | if ($adoc) { | |
62 | $axpath = new DOMXPath($adoc); | |
63 | $aentries = $axpath->query('(//img[@src])'); | |
64 | ||
65 | foreach ($aentries as $aentry) { | |
66 | if (preg_match("/^http:\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) { | |
67 | $img = $doc->createElement('img'); | |
68 | $img->setAttribute("src", $aentry->getAttribute("src")); | |
69 | $entry->parentNode->insertBefore($img, $entry); | |
70 | $found = true; | |
35055d05 AD |
71 | |
72 | break; | |
ce7d5e87 AD |
73 | } |
74 | } | |
75 | } | |
76 | } | |
a16d7a5d | 77 | } |
35055d05 AD |
78 | |
79 | // linked albums, ffs | |
80 | if (preg_match("/^http:\/\/imgur.com\/a\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { | |
81 | ||
82 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
83 | false, false, false, false, 10); | |
84 | ||
85 | if ($album_content) { | |
86 | $adoc = new DOMDocument(); | |
87 | @$adoc->loadHTML($album_content); | |
88 | ||
89 | if ($adoc) { | |
90 | $axpath = new DOMXPath($adoc); | |
91 | $aentries = $axpath->query("//div[@class='image']//a[@href and @class='zoom']"); | |
92 | ||
93 | foreach ($aentries as $aentry) { | |
94 | $img = $doc->createElement('img'); | |
95 | $img->setAttribute("src", $aentry->getAttribute("href")); | |
0b5ef30d | 96 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); |
35055d05 AD |
97 | $entry->parentNode->insertBefore($img, $entry); |
98 | $found = true; | |
99 | } | |
100 | } | |
101 | } | |
102 | } | |
cc85704f AD |
103 | } |
104 | ||
a16d7a5d AD |
105 | // remove tiny thumbnails |
106 | if ($entry->hasAttribute("src")) { | |
107 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
108 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
109 | } | |
cc85704f AD |
110 | } |
111 | } | |
112 | ||
a16d7a5d | 113 | $node = $doc->getElementsByTagName('body')->item(0); |
cc85704f | 114 | |
ce7d5e87 | 115 | if ($node && $found) { |
a16d7a5d | 116 | $article["content"] = $doc->saveXML($node, LIBXML_NOEMPTYTAG); |
0b5ef30d | 117 | if (!$force) $article["plugin_data"] = "redditimgur,$owner_uid:" . $article["plugin_data"]; |
a16d7a5d | 118 | } |
cc85704f | 119 | } |
3cb9cd6e AD |
120 | } else if (isset($article["stored"]["content"])) { |
121 | $article["content"] = $article["stored"]["content"]; | |
cc85704f AD |
122 | } |
123 | } | |
124 | ||
125 | return $article; | |
126 | } | |
127 | } | |
128 | ?> |