]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
remove some redundant php closing tags
[tt-rss.git] / plugins / af_redditimgur / init.php
CommitLineData
cc85704f 1<?php
0862a602 2class 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
41245888
AD
11 function flags() {
12 return array("needs_curl" => true);
13 }
14
d2a421e3 15 function init($host) {
19c73507
AD
16 $this->host = $host;
17
18 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
b90c4468
AD
19 $host->add_hook($host::HOOK_PREFS_TAB, $this);
20 }
21
22 function hook_prefs_tab($args) {
23 if ($args != "prefFeeds") return;
24
dc8bd8a6 25 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Reddit content settings (af_redditimgur)')."\">";
b90c4468
AD
26
27 $enable_readability = $this->host->get($this, "enable_readability");
891b77f9 28 $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
8788698b 29
b90c4468
AD
30 print "<form dojoType=\"dijit.form.Form\">";
31
32 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
33 evt.preventDefault();
34 if (this.validate()) {
35 console.log(dojo.objectToQuery(this.getValues()));
36 new Ajax.Request('backend.php', {
37 parameters: dojo.objectToQuery(this.getValues()),
38 onComplete: function(transport) {
39 notify_info(transport.responseText);
40 }
41 });
42 //this.reset();
43 }
44 </script>";
45
328118d1
AD
46 print_hidden("op", "pluginhandler");
47 print_hidden("method", "save");
48 print_hidden("plugin", "af_redditimgur");
b90c4468 49
73dfda1d 50 print "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
b90c4468
AD
51 print "<p/>";
52
dc8bd8a6
AD
53 print_checkbox("enable_readability", $enable_readability);
54 print "&nbsp;<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
b90c4468 55
891b77f9 56 print "<br/>";
8788698b 57
dc8bd8a6
AD
58 print_checkbox("enable_content_dupcheck", $enable_content_dupcheck);
59 print "&nbsp;<label for=\"enable_content_dupcheck\">" . __("Enable additional duplicate checking") . "</label>";
60 print "<p>"; print_button("submit", __("Save"));
b90c4468
AD
61 print "</form>";
62
63 print "</div>";
64 }
65
66 function save() {
67 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
891b77f9 68 $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]) == "true";
4533b3ef 69
891b77f9
AD
70 $this->host->set($this, "enable_readability", $enable_readability, false);
71 $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck);
b90c4468
AD
72
73 echo __("Configuration saved");
19c73507
AD
74 }
75
ec482d28 76 private function inline_stuff($article, &$doc, $xpath, $debug = false) {
cc85704f 77
6322fc68 78 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 79
6322fc68 80 $found = false;
ce7d5e87 81
6322fc68
AD
82 foreach ($entries as $entry) {
83 if ($entry->hasAttribute("href")) {
299aeb30 84
ec482d28
AD
85 _debug("processing href: " . $entry->getAttribute("href"), $debug);
86
6322fc68 87 $matches = array();
5d429910 88
8788698b
AD
89 if (preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) {
90 _debug("handling as twitter: " . $matches[1] . " " . $matches[2], $debug);
91
92 $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=" . urlencode($entry->getAttribute("href")));
93
94 if ($oembed_result) {
95 $oembed_result = json_decode($oembed_result, true);
96
97 if ($oembed_result && isset($oembed_result["html"])) {
98
99 $tmp = new DOMDocument();
48007463 100 if ($tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
8788698b
AD
101 $p = $doc->createElement("p");
102
103 $p->appendChild($doc->importNode(
104 $tmp->getElementsByTagName("blockquote")->item(0), TRUE));
105
106 $br = $doc->createElement('br');
107 $entry->parentNode->insertBefore($p, $entry);
108 $entry->parentNode->insertBefore($br, $entry);
109
110 $found = 1;
111 }
112 }
113 }
114 }
115
43db5b99 116 if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
5f297a5c
AD
117 $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
118 }
119
43db5b99 120 if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
5d429910 121
ec482d28
AD
122 _debug("Handling as Gfycat", $debug);
123
6322fc68 124 $tmp = fetch_file_contents($entry->getAttribute("href"));
5d429910 125
6322fc68
AD
126 if ($tmp) {
127 $tmpdoc = new DOMDocument();
5d429910 128
58a44ecb 129 if (@$tmpdoc->loadHTML($tmp)) {
6322fc68 130 $tmpxpath = new DOMXPath($tmpdoc);
b8887ebb 131
46506d3f
AD
132 $source_node = $tmpxpath->query("//video[contains(@class,'share-video')]//source[contains(@src, '.mp4')]")->item(0);
133 $poster_node = $tmpxpath->query("//video[contains(@class,'share-video') and @poster]")->item(0);
5d429910 134
46506d3f
AD
135 if ($source_node && $poster_node) {
136 $source_stream = $source_node->getAttribute("src");
137 $poster_url = $poster_node->getAttribute("poster");
5d429910 138
46506d3f
AD
139 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
140 $found = 1;
5d429910 141 }
6322fc68
AD
142 }
143 }
6322fc68 144 }
98e20510 145
60e97d9e
AD
146 if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) {
147
148 _debug("Handling as Streamable", $debug);
149
150 $tmp = fetch_file_contents($entry->getAttribute("href"));
151
152 if ($tmp) {
153 $tmpdoc = new DOMDocument();
154
155 if (@$tmpdoc->loadHTML($tmp)) {
156 $tmpxpath = new DOMXPath($tmpdoc);
157
158 $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0);
159 $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0);
160
161 if ($source_node && $poster_node) {
162 $source_stream = $source_node->getAttribute("src");
163 $poster_url = $poster_node->getAttribute("poster");
164
165 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
166 $found = 1;
167 }
168 }
169 }
170 }
171
5f297a5c 172 // imgur .gif -> .gifv
43db5b99 173 if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
ec482d28
AD
174 _debug("Handling as imgur gif (->gifv)", $debug);
175
5f297a5c
AD
176 $entry->setAttribute("href",
177 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
178 }
179
8c395462 180 if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) {
ec482d28 181 _debug("Handling as imgur gifv", $debug);
5d429910 182
6322fc68 183 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
b8887ebb 184
49048482 185 if (strpos($source_stream, "imgur.com") !== FALSE)
b8887ebb
AD
186 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
187
ec482d28 188 $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug);
98e20510 189
6322fc68
AD
190 $found = true;
191 }
9875d717 192
6322fc68 193 $matches = array();
43db5b99 194 if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
0a361964
AD
195 preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
196 preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
6322fc68 197 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
3b9ca4e6 198
6322fc68 199 $vid_id = $matches[1];
3b9ca4e6 200
ec482d28
AD
201 _debug("Handling as youtube: $vid_id", $debug);
202
6322fc68
AD
203 $iframe = $doc->createElement("iframe");
204 $iframe->setAttribute("class", "youtube-player");
205 $iframe->setAttribute("type", "text/html");
206 $iframe->setAttribute("width", "640");
207 $iframe->setAttribute("height", "385");
208 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
209 $iframe->setAttribute("allowfullscreen", "1");
210 $iframe->setAttribute("frameborder", "0");
3b9ca4e6 211
6322fc68
AD
212 $br = $doc->createElement('br');
213 $entry->parentNode->insertBefore($iframe, $entry);
214 $entry->parentNode->insertBefore($br, $entry);
3b9ca4e6 215
6322fc68
AD
216 $found = true;
217 }
3b9ca4e6 218
43db5b99 219 if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) ||
90e45935
AD
220 mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== FALSE ||
221 mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== FALSE) {
eb95d1bd 222
ec482d28
AD
223 _debug("Handling as a picture", $debug);
224
6322fc68
AD
225 $img = $doc->createElement('img');
226 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 227
6322fc68
AD
228 $br = $doc->createElement('br');
229 $entry->parentNode->insertBefore($img, $entry);
230 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87 231
6322fc68
AD
232 $found = true;
233 }
ce7d5e87 234
d4ac4fc6 235 // linked albums & pages
35055d05 236
43db5b99 237 if (!$found && preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
8b2a52c6 238 preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05 239
c80175bd 240 _debug("Handling as an imgur page/album/gallery", $debug);
ec482d28 241
6322fc68
AD
242 $album_content = fetch_file_contents($entry->getAttribute("href"),
243 false, false, false, false, 10);
35055d05 244
6322fc68
AD
245 if ($album_content) {
246 $adoc = new DOMDocument();
35055d05 247
58a44ecb 248 if (@$adoc->loadHTML($album_content)) {
6322fc68 249 $axpath = new DOMXPath($adoc);
ec482d28 250
a6fde6c9 251 $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src] | //div[@class='video-elements']/source)");
ec482d28
AD
252 $urls = [];
253
254 foreach ($aentries as $aentry) {
255
256 $url = $aentry->getAttribute("src");
257
258 if (!in_array($url, $urls)) {
ec482d28 259
a6fde6c9 260 if ($aentry->tagName == "img") {
ec482d28 261
a6fde6c9
AD
262 $img = $doc->createElement('img');
263 $img->setAttribute("src", $url);
264 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
265
266 $br = $doc->createElement('br');
267
268 $entry->parentNode->insertBefore($img, $entry);
269 $entry->parentNode->insertBefore($br, $entry);
270 } else if ($aentry->tagName == "source") {
271
49048482 272 if (strpos($url, "imgur.com") !== FALSE)
a6fde6c9
AD
273 $poster_url = str_replace(".mp4", "h.jpg", $url);
274 else
275 $poster_url = "";
276
277 $this->handle_as_video($doc, $entry, $url, $poster_url);
278
279 }
ec482d28
AD
280
281 array_push($urls, $url);
282
283 $found = true;
284 }
285
35055d05 286 }
ec482d28
AD
287
288 if ($debug) print_r($urls);
cc85704f 289 }
cc85704f 290 }
6322fc68 291 }
58a44ecb
AD
292
293 // wtf is this even
43db5b99 294 if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
58a44ecb
AD
295 $img_id = $matches[1];
296
ec482d28
AD
297 _debug("handling as gyazo: $img_id", $debug);
298
58a44ecb
AD
299 $img = $doc->createElement('img');
300 $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
301
302 $br = $doc->createElement('br');
303 $entry->parentNode->insertBefore($img, $entry);
304 $entry->parentNode->insertBefore($br, $entry);
305
306 $found = true;
307 }
6322fc68
AD
308 }
309
310 // remove tiny thumbnails
311 if ($entry->hasAttribute("src")) {
312 if ($entry->parentNode && $entry->parentNode->parentNode) {
313 $entry->parentNode->parentNode->removeChild($entry->parentNode);
314 }
315 }
316 }
cc85704f 317
6322fc68
AD
318 return $found;
319 }
b90c4468 320
6322fc68 321 function hook_article_filter($article) {
b90c4468 322
6322fc68
AD
323 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
324 $doc = new DOMDocument();
325 @$doc->loadHTML($article["content"]);
326 $xpath = new DOMXPath($doc);
99bb8b3b 327
582ff3cf
AD
328 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
329
891b77f9 330 if ($this->host->get($this, "enable_content_dupcheck")) {
902b1ac7 331
891b77f9
AD
332 if ($content_link) {
333 $content_href = db_escape_string($content_link->getAttribute("href"));
334 $entry_guid = db_escape_string($article["guid_hashed"]);
335 $owner_uid = $article["owner_uid"];
902b1ac7 336
891b77f9
AD
337 if (DB_TYPE == "pgsql") {
338 $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'";
339 } else {
340 $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)";
341 }
902b1ac7 342
891b77f9
AD
343 $result = db_query("SELECT COUNT(id) AS cid
344 FROM ttrss_entries, ttrss_user_entries WHERE
345 ref_id = id AND
346 $interval_qpart AND
347 guid != '$entry_guid' AND
348 owner_uid = '$owner_uid' AND
349 content LIKE '%href=\"$content_href\">[link]%'");
902b1ac7 350
891b77f9
AD
351 if ($result) {
352 $num_found = db_fetch_result($result, 0, "cid");
902b1ac7 353
891b77f9
AD
354 if ($num_found > 0) $article["force_catchup"] = true;
355 }
902b1ac7 356 }
891b77f9 357 }
4f5204dd 358
4533b3ef 359 $found = $this->inline_stuff($article, $doc, $xpath);
b90c4468 360
6322fc68
AD
361 $node = $doc->getElementsByTagName('body')->item(0);
362
363 if ($node && $found) {
364 $article["content"] = $doc->saveXML($node);
e487e92d
AD
365 } else if ($content_link) {
366 $article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath);
6322fc68 367 }
cc85704f
AD
368 }
369
370 return $article;
371 }
106a3de9
AD
372
373 function api_version() {
374 return 2;
375 }
376
ec482d28
AD
377 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) {
378
379 _debug("handle_as_video: $source_stream", $debug);
5d429910
AD
380
381 $video = $doc->createElement('video');
382 $video->setAttribute("autoplay", "1");
5dcc7bf1 383 $video->setAttribute("controls", "1");
5d429910
AD
384 $video->setAttribute("loop", "1");
385
b8887ebb
AD
386 if ($poster_url) $video->setAttribute("poster", $poster_url);
387
5d429910
AD
388 $source = $doc->createElement('source');
389 $source->setAttribute("src", $source_stream);
390 $source->setAttribute("type", "video/mp4");
391
392 $video->appendChild($source);
393
394 $br = $doc->createElement('br');
395 $entry->parentNode->insertBefore($video, $entry);
396 $entry->parentNode->insertBefore($br, $entry);
397
398 $img = $doc->createElement('img');
399 $img->setAttribute("src",
400 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
401
402 $entry->parentNode->insertBefore($img, $entry);
403 }
ec482d28
AD
404
405 function testurl() {
406 $url = htmlspecialchars($_REQUEST["url"]);
407
408 header("Content-type: text/plain");
409
410 print "URL: $url\n";
411
412 $doc = new DOMDocument();
413 @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
414 $xpath = new DOMXPath($doc);
415
e487e92d
AD
416 $found = $this->inline_stuff([], $doc, $xpath, true);
417
418 print "Inline result: $found\n";
419
420 if (!$found) {
421 print "\nReadability result:\n";
ec482d28 422
e487e92d 423 $article = $this->readability([], $url, $doc, $xpath, true);
ec482d28 424
e487e92d
AD
425 print_r($article);
426 } else {
427 print "\nResulting HTML:\n";
ec482d28 428
e487e92d
AD
429 print $doc->saveHTML();
430 }
ec482d28 431 }
90e45935
AD
432
433 private function get_content_type($url, $useragent = SELF_USER_AGENT) {
434 $content_type = false;
435
436 if (function_exists("curl_init") && !defined("NO_CURL")) {
437 $ch = curl_init($url);
438 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
439 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
440 curl_setopt($ch, CURLOPT_HEADER, true);
441 curl_setopt($ch, CURLOPT_NOBODY, true);
442 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
443 curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
444
445 @$result = curl_exec($ch);
446 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
447 }
448
449 return $content_type;
450 }
e487e92d
AD
451
452 private function readability($article, $url, $doc, $xpath, $debug = false) {
453
454 if (!defined('NO_CURL') && function_exists("curl_init") && $this->host->get($this, "enable_readability") &&
455 mb_strlen(strip_tags($article["content"])) <= 150) {
456
457 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
458
459 if ($url &&
460 strpos($url, "twitter.com") === FALSE &&
461 strpos($url, "youtube.com") === FALSE &&
462 strpos($url, "reddit.com") === FALSE) {
463
464 /* link may lead to a huge video file or whatever, we need to check content type before trying to
465 parse it which p much requires curl */
466
467 $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
468
469 $content_type = $this->get_content_type($url, $useragent_compat);
470
471 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
472
473 $tmp = fetch_file_contents(array("url" => $url,
474 "useragent" => $useragent_compat));
475
476 if ($debug) _debug("tmplen: " . mb_strlen($tmp));
477
478 if ($tmp && mb_strlen($tmp) < 1024 * 500) {
479
480 $r = new Readability($tmp, $url);
481
482 if ($r->init()) {
483
484 $tmpxpath = new DOMXPath($r->dom);
485
486 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
487
488 foreach ($entries as $entry) {
489 if ($entry->hasAttribute("href")) {
490 $entry->setAttribute("href",
491 rewrite_relative_url($url, $entry->getAttribute("href")));
492
493 }
494
495 if ($entry->hasAttribute("src")) {
496 $entry->setAttribute("src",
497 rewrite_relative_url($url, $entry->getAttribute("src")));
498
499 }
500
501 }
502
503 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
504 }
505 }
506 }
507 }
508 }
509
510 return $article;
511 }
cc85704f
AD
512}
513?>