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