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