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