]>
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")) { | |
299aeb30 | 31 | |
5d429910 AD |
32 | $matches = array(); |
33 | ||
34 | if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { | |
35 | ||
36 | $tmp = fetch_file_contents($entry->getAttribute("href")); | |
37 | ||
38 | if ($tmp) { | |
39 | $tmpdoc = new DOMDocument(); | |
40 | @$tmpdoc->loadHTML($tmp); | |
41 | ||
42 | if ($tmpdoc) { | |
43 | $tmpxpath = new DOMXPath($tmpdoc); | |
44 | $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0); | |
45 | ||
46 | if ($source_meta) { | |
47 | $source_stream = $source_meta->getAttribute("content"); | |
48 | ||
49 | if ($source_stream) { | |
50 | $this->handle_as_video($doc, $entry, $source_stream); | |
51 | $found = 1; | |
52 | } | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
57 | } | |
58 | ||
9875d717 | 59 | if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) { |
60 | ||
5d429910 | 61 | /*$video = $doc->createElement('video'); |
1ae46c50 AD |
62 | $video->setAttribute("autoplay", "1"); |
63 | $video->setAttribute("loop", "1"); | |
299aeb30 | 64 | |
1ae46c50 AD |
65 | $source = $doc->createElement('source'); |
66 | $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href"))); | |
67 | $source->setAttribute("type", "video/mp4"); | |
299aeb30 | 68 | |
1ae46c50 AD |
69 | $video->appendChild($source); |
70 | ||
71 | $br = $doc->createElement('br'); | |
72 | $entry->parentNode->insertBefore($video, $entry); | |
73 | $entry->parentNode->insertBefore($br, $entry); | |
74 | ||
98e20510 AD |
75 | $img = $doc->createElement('img'); |
76 | $img->setAttribute("src", | |
77 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
78 | ||
5d429910 AD |
79 | $entry->parentNode->insertBefore($img, $entry);*/ |
80 | ||
81 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); | |
82 | $this->handle_as_video($doc, $entry, $source_stream); | |
98e20510 | 83 | |
1ae46c50 | 84 | $found = true; |
299aeb30 | 85 | } |
9875d717 | 86 | |
5f58daa2 AD |
87 | if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) { |
88 | $img = $doc->createElement('img'); | |
a16d7a5d | 89 | $img->setAttribute("src", $entry->getAttribute("href")); |
cc85704f | 90 | |
7ff4d1fa AD |
91 | $br = $doc->createElement('br'); |
92 | $entry->parentNode->insertBefore($img, $entry); | |
93 | $entry->parentNode->insertBefore($br, $entry); | |
ce7d5e87 AD |
94 | |
95 | $found = true; | |
96 | } | |
97 | ||
98 | // links to imgur pages | |
99 | $matches = array(); | |
248c5a6a | 100 | if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
ce7d5e87 | 101 | |
1af9f54f | 102 | $token = $matches[2]; |
ce7d5e87 AD |
103 | |
104 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
105 | false, false, false, false, 10); | |
106 | ||
107 | if ($album_content && $token) { | |
108 | $adoc = new DOMDocument(); | |
109 | @$adoc->loadHTML($album_content); | |
110 | ||
111 | if ($adoc) { | |
112 | $axpath = new DOMXPath($adoc); | |
113 | $aentries = $axpath->query('(//img[@src])'); | |
114 | ||
115 | foreach ($aentries as $aentry) { | |
e44ea762 | 116 | if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) { |
ce7d5e87 AD |
117 | $img = $doc->createElement('img'); |
118 | $img->setAttribute("src", $aentry->getAttribute("src")); | |
7ff4d1fa AD |
119 | |
120 | $br = $doc->createElement('br'); | |
121 | ||
ce7d5e87 | 122 | $entry->parentNode->insertBefore($img, $entry); |
7ff4d1fa AD |
123 | $entry->parentNode->insertBefore($br, $entry); |
124 | ||
ce7d5e87 | 125 | $found = true; |
35055d05 AD |
126 | |
127 | break; | |
ce7d5e87 AD |
128 | } |
129 | } | |
130 | } | |
131 | } | |
a16d7a5d | 132 | } |
35055d05 AD |
133 | |
134 | // linked albums, ffs | |
09897321 | 135 | if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { |
35055d05 AD |
136 | |
137 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
138 | false, false, false, false, 10); | |
139 | ||
140 | if ($album_content) { | |
141 | $adoc = new DOMDocument(); | |
142 | @$adoc->loadHTML($album_content); | |
143 | ||
144 | if ($adoc) { | |
145 | $axpath = new DOMXPath($adoc); | |
47cdc58c | 146 | $aentries = $axpath->query("//meta[@property='og:image']"); |
98e20510 | 147 | $urls = array(); |
35055d05 AD |
148 | |
149 | foreach ($aentries as $aentry) { | |
7ff4d1fa | 150 | |
98e20510 AD |
151 | if (!in_array($aentry->getAttribute("content"), $urls)) { |
152 | $img = $doc->createElement('img'); | |
153 | $img->setAttribute("src", $aentry->getAttribute("content")); | |
154 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); | |
7ff4d1fa | 155 | |
98e20510 | 156 | $br = $doc->createElement('br'); |
7ff4d1fa | 157 | |
98e20510 AD |
158 | $entry->parentNode->insertBefore($img, $entry); |
159 | $entry->parentNode->insertBefore($br, $entry); | |
160 | ||
161 | array_push($urls, $aentry->getAttribute("content")); | |
162 | ||
163 | $found = true; | |
164 | } | |
35055d05 AD |
165 | } |
166 | } | |
167 | } | |
168 | } | |
cc85704f AD |
169 | } |
170 | ||
a16d7a5d AD |
171 | // remove tiny thumbnails |
172 | if ($entry->hasAttribute("src")) { | |
173 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
174 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
175 | } | |
cc85704f AD |
176 | } |
177 | } | |
178 | ||
a16d7a5d | 179 | $node = $doc->getElementsByTagName('body')->item(0); |
cc85704f | 180 | |
ce7d5e87 | 181 | if ($node && $found) { |
cc38c8e5 | 182 | $article["content"] = $doc->saveXML($node); |
a16d7a5d | 183 | } |
cc85704f | 184 | } |
cc85704f AD |
185 | } |
186 | ||
187 | return $article; | |
188 | } | |
106a3de9 AD |
189 | |
190 | function api_version() { | |
191 | return 2; | |
192 | } | |
193 | ||
5d429910 AD |
194 | private function handle_as_video($doc, $entry, $source_stream) { |
195 | ||
196 | $video = $doc->createElement('video'); | |
197 | $video->setAttribute("autoplay", "1"); | |
5dcc7bf1 | 198 | $video->setAttribute("controls", "1"); |
5d429910 AD |
199 | $video->setAttribute("loop", "1"); |
200 | ||
201 | $source = $doc->createElement('source'); | |
202 | $source->setAttribute("src", $source_stream); | |
203 | $source->setAttribute("type", "video/mp4"); | |
204 | ||
205 | $video->appendChild($source); | |
206 | ||
207 | $br = $doc->createElement('br'); | |
208 | $entry->parentNode->insertBefore($video, $entry); | |
209 | $entry->parentNode->insertBefore($br, $entry); | |
210 | ||
211 | $img = $doc->createElement('img'); | |
212 | $img->setAttribute("src", | |
213 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
214 | ||
215 | $entry->parentNode->insertBefore($img, $entry); | |
216 | } | |
cc85704f AD |
217 | } |
218 | ?> |