]>
Commit | Line | Data |
---|---|---|
cc85704f | 1 | <?php |
0862a602 | 2 | class Af_RedditImgur extends Plugin { |
19c73507 AD |
3 | private $host; |
4 | ||
d2a421e3 | 5 | function about() { |
7a866114 | 6 | return array(1.0, |
a95fb169 | 7 | "Inline images (and other content) in Reddit RSS feeds", |
7a866114 AD |
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); | |
b90c4468 AD |
15 | $host->add_hook($host::HOOK_PREFS_TAB, $this); |
16 | } | |
17 | ||
18 | function hook_prefs_tab($args) { | |
19 | if ($args != "prefFeeds") return; | |
20 | ||
21 | print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">"; | |
22 | ||
23 | $enable_readability = $this->host->get($this, "enable_readability"); | |
24 | $enable_readability_checked = $enable_readability ? "checked" : ""; | |
25 | ||
26 | print "<form dojoType=\"dijit.form.Form\">"; | |
27 | ||
28 | print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> | |
29 | evt.preventDefault(); | |
30 | if (this.validate()) { | |
31 | console.log(dojo.objectToQuery(this.getValues())); | |
32 | new Ajax.Request('backend.php', { | |
33 | parameters: dojo.objectToQuery(this.getValues()), | |
34 | onComplete: function(transport) { | |
35 | notify_info(transport.responseText); | |
36 | } | |
37 | }); | |
38 | //this.reset(); | |
39 | } | |
40 | </script>"; | |
41 | ||
42 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">"; | |
43 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">"; | |
44 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">"; | |
45 | ||
73dfda1d | 46 | print "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>"); |
b90c4468 AD |
47 | print "<p/>"; |
48 | ||
49 | print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\" | |
50 | $enable_readability_checked name=\"enable_readability\"> "; | |
51 | ||
52 | print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>"; | |
53 | ||
54 | print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">". | |
55 | __("Save")."</button>"; | |
56 | ||
57 | print "</form>"; | |
58 | ||
59 | print "</div>"; | |
60 | } | |
61 | ||
62 | function save() { | |
63 | $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true"; | |
4533b3ef | 64 | |
b90c4468 AD |
65 | $this->host->set($this, "enable_readability", $enable_readability); |
66 | ||
67 | echo __("Configuration saved"); | |
19c73507 AD |
68 | } |
69 | ||
6322fc68 | 70 | private function inline_stuff($article, &$doc, $xpath) { |
cc85704f | 71 | |
6322fc68 | 72 | $entries = $xpath->query('(//a[@href]|//img[@src])'); |
cc85704f | 73 | |
6322fc68 | 74 | $found = false; |
ce7d5e87 | 75 | |
6322fc68 AD |
76 | foreach ($entries as $entry) { |
77 | if ($entry->hasAttribute("href")) { | |
299aeb30 | 78 | |
6322fc68 | 79 | $matches = array(); |
5d429910 | 80 | |
5f297a5c AD |
81 | if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
82 | $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]); | |
83 | } | |
84 | ||
b8887ebb | 85 | if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
5d429910 | 86 | |
6322fc68 | 87 | $tmp = fetch_file_contents($entry->getAttribute("href")); |
5d429910 | 88 | |
6322fc68 AD |
89 | if ($tmp) { |
90 | $tmpdoc = new DOMDocument(); | |
5d429910 | 91 | |
58a44ecb | 92 | if (@$tmpdoc->loadHTML($tmp)) { |
6322fc68 | 93 | $tmpxpath = new DOMXPath($tmpdoc); |
b8887ebb | 94 | |
1f6732b2 | 95 | $source_meta = $tmpxpath->query("//meta[@name='twitter:player:stream' and contains(@content, '.mp4')]")->item(0); |
b8887ebb | 96 | $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0); |
5d429910 | 97 | |
6322fc68 AD |
98 | if ($source_meta) { |
99 | $source_stream = $source_meta->getAttribute("content"); | |
b8887ebb | 100 | $poster_url = false; |
5d429910 | 101 | |
6322fc68 | 102 | if ($source_stream) { |
b8887ebb AD |
103 | |
104 | if ($poster_meta) | |
105 | $poster_url = $poster_meta->getAttribute("content"); | |
106 | ||
107 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); | |
6322fc68 | 108 | $found = 1; |
5d429910 | 109 | } |
5d429910 | 110 | } |
6322fc68 AD |
111 | } |
112 | } | |
5d429910 | 113 | |
6322fc68 | 114 | } |
98e20510 | 115 | |
5f297a5c AD |
116 | // imgur .gif -> .gifv |
117 | if (preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) { | |
118 | $entry->setAttribute("href", | |
119 | str_replace(".gif", ".gifv", $entry->getAttribute("href"))); | |
120 | } | |
121 | ||
6322fc68 | 122 | if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) { |
5d429910 | 123 | |
6322fc68 | 124 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); |
b8887ebb AD |
125 | |
126 | if (strpos($source_stream, "i.imgur.com") !== FALSE) | |
127 | $poster_url = str_replace(".mp4", "h.jpg", $source_stream); | |
128 | ||
129 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); | |
98e20510 | 130 | |
6322fc68 AD |
131 | $found = true; |
132 | } | |
9875d717 | 133 | |
6322fc68 | 134 | $matches = array(); |
f7745af9 | 135 | if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || |
9ec9a8f9 | 136 | preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
f7745af9 | 137 | preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
6322fc68 | 138 | preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { |
3b9ca4e6 | 139 | |
6322fc68 | 140 | $vid_id = $matches[1]; |
3b9ca4e6 | 141 | |
6322fc68 AD |
142 | $iframe = $doc->createElement("iframe"); |
143 | $iframe->setAttribute("class", "youtube-player"); | |
144 | $iframe->setAttribute("type", "text/html"); | |
145 | $iframe->setAttribute("width", "640"); | |
146 | $iframe->setAttribute("height", "385"); | |
147 | $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); | |
148 | $iframe->setAttribute("allowfullscreen", "1"); | |
149 | $iframe->setAttribute("frameborder", "0"); | |
3b9ca4e6 | 150 | |
6322fc68 AD |
151 | $br = $doc->createElement('br'); |
152 | $entry->parentNode->insertBefore($iframe, $entry); | |
153 | $entry->parentNode->insertBefore($br, $entry); | |
3b9ca4e6 | 154 | |
6322fc68 AD |
155 | $found = true; |
156 | } | |
3b9ca4e6 | 157 | |
6322fc68 AD |
158 | if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) { |
159 | $img = $doc->createElement('img'); | |
160 | $img->setAttribute("src", $entry->getAttribute("href")); | |
cc85704f | 161 | |
6322fc68 AD |
162 | $br = $doc->createElement('br'); |
163 | $entry->parentNode->insertBefore($img, $entry); | |
164 | $entry->parentNode->insertBefore($br, $entry); | |
ce7d5e87 | 165 | |
6322fc68 AD |
166 | $found = true; |
167 | } | |
ce7d5e87 | 168 | |
d4ac4fc6 | 169 | // linked albums & pages |
35055d05 | 170 | |
d4ac4fc6 AD |
171 | if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) || |
172 | preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { | |
35055d05 | 173 | |
6322fc68 AD |
174 | $album_content = fetch_file_contents($entry->getAttribute("href"), |
175 | false, false, false, false, 10); | |
35055d05 | 176 | |
6322fc68 AD |
177 | if ($album_content) { |
178 | $adoc = new DOMDocument(); | |
35055d05 | 179 | |
58a44ecb | 180 | if (@$adoc->loadHTML($album_content)) { |
6322fc68 AD |
181 | $axpath = new DOMXPath($adoc); |
182 | $aentries = $axpath->query("//meta[@property='og:image']"); | |
183 | $urls = array(); | |
35055d05 | 184 | |
6322fc68 | 185 | foreach ($aentries as $aentry) { |
7ff4d1fa | 186 | |
ecc92d92 | 187 | $url = str_replace("?fb", "", $aentry->getAttribute("content")); |
a4617617 AD |
188 | $check_url = basename($url); |
189 | $check_url = mb_substr($check_url, 0, strrpos($check_url, ".")); | |
ecc92d92 | 190 | |
a4617617 | 191 | if (!in_array($check_url, $urls)) { |
6322fc68 | 192 | $img = $doc->createElement('img'); |
ecc92d92 | 193 | $img->setAttribute("src", $url); |
6322fc68 | 194 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); |
7ff4d1fa | 195 | |
6322fc68 | 196 | $br = $doc->createElement('br'); |
7ff4d1fa | 197 | |
6322fc68 AD |
198 | $entry->parentNode->insertBefore($img, $entry); |
199 | $entry->parentNode->insertBefore($br, $entry); | |
98e20510 | 200 | |
a4617617 | 201 | array_push($urls, $check_url); |
98e20510 | 202 | |
6322fc68 | 203 | $found = true; |
35055d05 AD |
204 | } |
205 | } | |
cc85704f | 206 | } |
cc85704f | 207 | } |
6322fc68 | 208 | } |
58a44ecb AD |
209 | |
210 | // wtf is this even | |
211 | if (preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { | |
212 | $img_id = $matches[1]; | |
213 | ||
214 | $img = $doc->createElement('img'); | |
215 | $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg"); | |
216 | ||
217 | $br = $doc->createElement('br'); | |
218 | $entry->parentNode->insertBefore($img, $entry); | |
219 | $entry->parentNode->insertBefore($br, $entry); | |
220 | ||
221 | $found = true; | |
222 | } | |
6322fc68 AD |
223 | } |
224 | ||
225 | // remove tiny thumbnails | |
226 | if ($entry->hasAttribute("src")) { | |
227 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
228 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
229 | } | |
230 | } | |
231 | } | |
cc85704f | 232 | |
6322fc68 AD |
233 | return $found; |
234 | } | |
b90c4468 | 235 | |
6322fc68 | 236 | function hook_article_filter($article) { |
b90c4468 | 237 | |
6322fc68 AD |
238 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { |
239 | $doc = new DOMDocument(); | |
240 | @$doc->loadHTML($article["content"]); | |
241 | $xpath = new DOMXPath($doc); | |
99bb8b3b | 242 | |
4f5204dd AD |
243 | $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); |
244 | ||
4533b3ef | 245 | $found = $this->inline_stuff($article, $doc, $xpath); |
b90c4468 | 246 | |
6475fc7e AD |
247 | if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") && |
248 | mb_strlen(strip_tags($article["content"])) <= 150) { | |
249 | ||
8b08d9d7 | 250 | if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php"); |
b90c4468 | 251 | |
12d880d7 AD |
252 | if ($content_link && |
253 | strpos($content_link->getAttribute("href"), "twitter.com") === FALSE && | |
9ec9a8f9 | 254 | strpos($content_link->getAttribute("href"), "youtube.com") === FALSE && |
12d880d7 | 255 | strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) { |
fd61fd6e | 256 | |
6475fc7e AD |
257 | /* link may lead to a huge video file or whatever, we need to check content type before trying to |
258 | parse it which p much requires curl */ | |
fd61fd6e | 259 | |
6475fc7e AD |
260 | $ch = curl_init($content_link->getAttribute("href")); |
261 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
262 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
263 | curl_setopt($ch, CURLOPT_HEADER, true); | |
264 | curl_setopt($ch, CURLOPT_NOBODY, true); | |
265 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, | |
266 | !ini_get("safe_mode") && !ini_get("open_basedir")); | |
267 | curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT); | |
fd61fd6e | 268 | |
6475fc7e AD |
269 | @$result = curl_exec($ch); |
270 | $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | |
b90c4468 | 271 | |
6475fc7e | 272 | if ($content_type && strpos($content_type, "text/html") !== FALSE) { |
fd61fd6e | 273 | |
6475fc7e | 274 | $tmp = fetch_file_contents($content_link->getAttribute("href")); |
fd61fd6e | 275 | |
4d03c5c5 AD |
276 | //_debug("tmplen: " . mb_strlen($tmp)); |
277 | ||
278 | if ($tmp && mb_strlen($tmp) < 65535 * 4) { | |
279 | ||
6475fc7e | 280 | $r = new Readability($tmp, $content_link->getAttribute("href")); |
fd61fd6e | 281 | |
6475fc7e | 282 | if ($r->init()) { |
b90c4468 | 283 | |
6475fc7e | 284 | $tmpxpath = new DOMXPath($r->dom); |
b90c4468 | 285 | |
6475fc7e | 286 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); |
6322fc68 | 287 | |
6475fc7e AD |
288 | foreach ($entries as $entry) { |
289 | if ($entry->hasAttribute("href")) { | |
290 | $entry->setAttribute("href", | |
291 | rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href"))); | |
292 | ||
293 | } | |
b90c4468 | 294 | |
6475fc7e AD |
295 | if ($entry->hasAttribute("src")) { |
296 | $entry->setAttribute("src", | |
297 | rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src"))); | |
cc85704f | 298 | |
6475fc7e | 299 | } |
6322fc68 | 300 | |
6475fc7e AD |
301 | } |
302 | ||
303 | $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; | |
304 | ||
47888b3d AD |
305 | // prob not a very good idea (breaks wikipedia pages, etc) - |
306 | // inliner currently is not really fit for any random web content | |
6475fc7e | 307 | |
47888b3d AD |
308 | //$doc = new DOMDocument(); |
309 | //@$doc->loadHTML($article["content"]); | |
310 | //$xpath = new DOMXPath($doc); | |
311 | //$found = $this->inline_stuff($article, $doc, $xpath); | |
6475fc7e | 312 | } |
6322fc68 | 313 | } |
a16d7a5d | 314 | } |
cc85704f | 315 | } |
6322fc68 AD |
316 | |
317 | } | |
318 | ||
319 | $node = $doc->getElementsByTagName('body')->item(0); | |
320 | ||
321 | if ($node && $found) { | |
322 | $article["content"] = $doc->saveXML($node); | |
323 | } | |
cc85704f AD |
324 | } |
325 | ||
326 | return $article; | |
327 | } | |
106a3de9 AD |
328 | |
329 | function api_version() { | |
330 | return 2; | |
331 | } | |
332 | ||
b8887ebb | 333 | private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) { |
5d429910 AD |
334 | |
335 | $video = $doc->createElement('video'); | |
336 | $video->setAttribute("autoplay", "1"); | |
5dcc7bf1 | 337 | $video->setAttribute("controls", "1"); |
5d429910 AD |
338 | $video->setAttribute("loop", "1"); |
339 | ||
b8887ebb AD |
340 | if ($poster_url) $video->setAttribute("poster", $poster_url); |
341 | ||
5d429910 AD |
342 | $source = $doc->createElement('source'); |
343 | $source->setAttribute("src", $source_stream); | |
344 | $source->setAttribute("type", "video/mp4"); | |
345 | ||
346 | $video->appendChild($source); | |
347 | ||
348 | $br = $doc->createElement('br'); | |
349 | $entry->parentNode->insertBefore($video, $entry); | |
350 | $entry->parentNode->insertBefore($br, $entry); | |
351 | ||
352 | $img = $doc->createElement('img'); | |
353 | $img->setAttribute("src", | |
354 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
355 | ||
356 | $entry->parentNode->insertBefore($img, $entry); | |
357 | } | |
cc85704f AD |
358 | } |
359 | ?> |