]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
af_redditimgur: make sure content_link is defined even if content dupcheck is disabled
[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" : "";
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
AD
61 print "<br/>";
62
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
5f297a5c
AD
98 if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
99 $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
100 }
101
b8887ebb 102 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
5d429910 103
ec482d28
AD
104 _debug("Handling as Gfycat", $debug);
105
6322fc68 106 $tmp = fetch_file_contents($entry->getAttribute("href"));
5d429910 107
6322fc68
AD
108 if ($tmp) {
109 $tmpdoc = new DOMDocument();
5d429910 110
58a44ecb 111 if (@$tmpdoc->loadHTML($tmp)) {
6322fc68 112 $tmpxpath = new DOMXPath($tmpdoc);
b8887ebb 113
1f6732b2 114 $source_meta = $tmpxpath->query("//meta[@name='twitter:player:stream' and contains(@content, '.mp4')]")->item(0);
b8887ebb 115 $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
5d429910 116
6322fc68
AD
117 if ($source_meta) {
118 $source_stream = $source_meta->getAttribute("content");
b8887ebb 119 $poster_url = false;
5d429910 120
6322fc68 121 if ($source_stream) {
b8887ebb
AD
122
123 if ($poster_meta)
124 $poster_url = $poster_meta->getAttribute("content");
125
126 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
6322fc68 127 $found = 1;
5d429910 128 }
5d429910 129 }
6322fc68
AD
130 }
131 }
5d429910 132
6322fc68 133 }
98e20510 134
5f297a5c
AD
135 // imgur .gif -> .gifv
136 if (preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
ec482d28
AD
137 _debug("Handling as imgur gif (->gifv)", $debug);
138
5f297a5c
AD
139 $entry->setAttribute("href",
140 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
141 }
142
6322fc68 143 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
ec482d28 144 _debug("Handling as imgur gifv", $debug);
5d429910 145
6322fc68 146 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
b8887ebb
AD
147
148 if (strpos($source_stream, "i.imgur.com") !== FALSE)
149 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
150
ec482d28 151 $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug);
98e20510 152
6322fc68
AD
153 $found = true;
154 }
9875d717 155
6322fc68 156 $matches = array();
0a361964
AD
157 if (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
158 preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
159 preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
6322fc68 160 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
3b9ca4e6 161
6322fc68 162 $vid_id = $matches[1];
3b9ca4e6 163
ec482d28
AD
164 _debug("Handling as youtube: $vid_id", $debug);
165
6322fc68
AD
166 $iframe = $doc->createElement("iframe");
167 $iframe->setAttribute("class", "youtube-player");
168 $iframe->setAttribute("type", "text/html");
169 $iframe->setAttribute("width", "640");
170 $iframe->setAttribute("height", "385");
171 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
172 $iframe->setAttribute("allowfullscreen", "1");
173 $iframe->setAttribute("frameborder", "0");
3b9ca4e6 174
6322fc68
AD
175 $br = $doc->createElement('br');
176 $entry->parentNode->insertBefore($iframe, $entry);
177 $entry->parentNode->insertBefore($br, $entry);
3b9ca4e6 178
6322fc68
AD
179 $found = true;
180 }
3b9ca4e6 181
6322fc68 182 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
ec482d28
AD
183 _debug("Handling as a picture", $debug);
184
6322fc68
AD
185 $img = $doc->createElement('img');
186 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 187
6322fc68
AD
188 $br = $doc->createElement('br');
189 $entry->parentNode->insertBefore($img, $entry);
190 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87 191
6322fc68
AD
192 $found = true;
193 }
ce7d5e87 194
d4ac4fc6 195 // linked albums & pages
35055d05 196
d4ac4fc6 197 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
8b2a52c6 198 preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05 199
c80175bd 200 _debug("Handling as an imgur page/album/gallery", $debug);
ec482d28 201
6322fc68
AD
202 $album_content = fetch_file_contents($entry->getAttribute("href"),
203 false, false, false, false, 10);
35055d05 204
6322fc68
AD
205 if ($album_content) {
206 $adoc = new DOMDocument();
35055d05 207
58a44ecb 208 if (@$adoc->loadHTML($album_content)) {
6322fc68 209 $axpath = new DOMXPath($adoc);
ec482d28
AD
210
211 /*$aentries = $axpath->query("//meta[@property='og:image']");
6322fc68 212 $urls = array();
35055d05 213
6322fc68 214 foreach ($aentries as $aentry) {
7ff4d1fa 215
ec482d28
AD
216 _debug("og:image content=" . $aentry->getAttribute("content"), $debug);
217
ecc92d92 218 $url = str_replace("?fb", "", $aentry->getAttribute("content"));
a4617617
AD
219 $check_url = basename($url);
220 $check_url = mb_substr($check_url, 0, strrpos($check_url, "."));
ecc92d92 221
a4617617 222 if (!in_array($check_url, $urls)) {
6322fc68 223 $img = $doc->createElement('img');
ecc92d92 224 $img->setAttribute("src", $url);
6322fc68 225 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa 226
6322fc68 227 $br = $doc->createElement('br');
7ff4d1fa 228
6322fc68
AD
229 $entry->parentNode->insertBefore($img, $entry);
230 $entry->parentNode->insertBefore($br, $entry);
98e20510 231
a4617617 232 array_push($urls, $check_url);
98e20510 233
6322fc68 234 $found = true;
35055d05 235 }
ec482d28
AD
236 } */
237
c80175bd
AD
238 //if ($debug) print_r($album_content);
239
240 $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src])");
ec482d28
AD
241 $urls = [];
242
243 foreach ($aentries as $aentry) {
244
245 $url = $aentry->getAttribute("src");
246
247 if (!in_array($url, $urls)) {
248 $img = $doc->createElement('img');
249 $img->setAttribute("src", $url);
250 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
251
252 $br = $doc->createElement('br');
253
254 $entry->parentNode->insertBefore($img, $entry);
255 $entry->parentNode->insertBefore($br, $entry);
256
257 array_push($urls, $url);
258
259 $found = true;
260 }
261
35055d05 262 }
ec482d28
AD
263
264 if ($debug) print_r($urls);
cc85704f 265 }
cc85704f 266 }
6322fc68 267 }
58a44ecb
AD
268
269 // wtf is this even
270 if (preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
271 $img_id = $matches[1];
272
ec482d28
AD
273 _debug("handling as gyazo: $img_id", $debug);
274
58a44ecb
AD
275 $img = $doc->createElement('img');
276 $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
277
278 $br = $doc->createElement('br');
279 $entry->parentNode->insertBefore($img, $entry);
280 $entry->parentNode->insertBefore($br, $entry);
281
282 $found = true;
283 }
6322fc68
AD
284 }
285
286 // remove tiny thumbnails
287 if ($entry->hasAttribute("src")) {
288 if ($entry->parentNode && $entry->parentNode->parentNode) {
289 $entry->parentNode->parentNode->removeChild($entry->parentNode);
290 }
291 }
292 }
cc85704f 293
6322fc68
AD
294 return $found;
295 }
b90c4468 296
6322fc68 297 function hook_article_filter($article) {
b90c4468 298
6322fc68
AD
299 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
300 $doc = new DOMDocument();
301 @$doc->loadHTML($article["content"]);
302 $xpath = new DOMXPath($doc);
99bb8b3b 303
582ff3cf
AD
304 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
305
891b77f9 306 if ($this->host->get($this, "enable_content_dupcheck")) {
902b1ac7 307
891b77f9
AD
308 if ($content_link) {
309 $content_href = db_escape_string($content_link->getAttribute("href"));
310 $entry_guid = db_escape_string($article["guid_hashed"]);
311 $owner_uid = $article["owner_uid"];
902b1ac7 312
891b77f9
AD
313 if (DB_TYPE == "pgsql") {
314 $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'";
315 } else {
316 $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)";
317 }
902b1ac7 318
891b77f9
AD
319 $result = db_query("SELECT COUNT(id) AS cid
320 FROM ttrss_entries, ttrss_user_entries WHERE
321 ref_id = id AND
322 $interval_qpart AND
323 guid != '$entry_guid' AND
324 owner_uid = '$owner_uid' AND
325 content LIKE '%href=\"$content_href\">[link]%'");
902b1ac7 326
891b77f9
AD
327 if ($result) {
328 $num_found = db_fetch_result($result, 0, "cid");
902b1ac7 329
891b77f9
AD
330 if ($num_found > 0) $article["force_catchup"] = true;
331 }
902b1ac7 332 }
891b77f9 333 }
4f5204dd 334
4533b3ef 335 $found = $this->inline_stuff($article, $doc, $xpath);
b90c4468 336
aa03bac4 337 if (!defined('NO_CURL') && function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
6475fc7e
AD
338 mb_strlen(strip_tags($article["content"])) <= 150) {
339
8b08d9d7 340 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
b90c4468 341
12d880d7
AD
342 if ($content_link &&
343 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
9ec9a8f9 344 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
12d880d7 345 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
fd61fd6e 346
6475fc7e
AD
347 /* link may lead to a huge video file or whatever, we need to check content type before trying to
348 parse it which p much requires curl */
fd61fd6e 349
6475fc7e
AD
350 $ch = curl_init($content_link->getAttribute("href"));
351 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
352 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
353 curl_setopt($ch, CURLOPT_HEADER, true);
354 curl_setopt($ch, CURLOPT_NOBODY, true);
4c467026 355 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
6475fc7e 356 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
fd61fd6e 357
6475fc7e
AD
358 @$result = curl_exec($ch);
359 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
b90c4468 360
6475fc7e 361 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
fd61fd6e 362
6475fc7e 363 $tmp = fetch_file_contents($content_link->getAttribute("href"));
fd61fd6e 364
4d03c5c5
AD
365 //_debug("tmplen: " . mb_strlen($tmp));
366
367 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
368
6475fc7e 369 $r = new Readability($tmp, $content_link->getAttribute("href"));
fd61fd6e 370
6475fc7e 371 if ($r->init()) {
b90c4468 372
6475fc7e 373 $tmpxpath = new DOMXPath($r->dom);
b90c4468 374
6475fc7e 375 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
6322fc68 376
6475fc7e
AD
377 foreach ($entries as $entry) {
378 if ($entry->hasAttribute("href")) {
379 $entry->setAttribute("href",
380 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
381
382 }
b90c4468 383
6475fc7e
AD
384 if ($entry->hasAttribute("src")) {
385 $entry->setAttribute("src",
386 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
cc85704f 387
6475fc7e 388 }
6322fc68 389
6475fc7e
AD
390 }
391
392 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
393
47888b3d
AD
394 // prob not a very good idea (breaks wikipedia pages, etc) -
395 // inliner currently is not really fit for any random web content
6475fc7e 396
47888b3d
AD
397 //$doc = new DOMDocument();
398 //@$doc->loadHTML($article["content"]);
399 //$xpath = new DOMXPath($doc);
400 //$found = $this->inline_stuff($article, $doc, $xpath);
6475fc7e 401 }
6322fc68 402 }
a16d7a5d 403 }
cc85704f 404 }
6322fc68
AD
405
406 }
407
408 $node = $doc->getElementsByTagName('body')->item(0);
409
410 if ($node && $found) {
411 $article["content"] = $doc->saveXML($node);
412 }
cc85704f
AD
413 }
414
415 return $article;
416 }
106a3de9
AD
417
418 function api_version() {
419 return 2;
420 }
421
ec482d28
AD
422 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) {
423
424 _debug("handle_as_video: $source_stream", $debug);
5d429910
AD
425
426 $video = $doc->createElement('video');
427 $video->setAttribute("autoplay", "1");
5dcc7bf1 428 $video->setAttribute("controls", "1");
5d429910
AD
429 $video->setAttribute("loop", "1");
430
b8887ebb
AD
431 if ($poster_url) $video->setAttribute("poster", $poster_url);
432
5d429910
AD
433 $source = $doc->createElement('source');
434 $source->setAttribute("src", $source_stream);
435 $source->setAttribute("type", "video/mp4");
436
437 $video->appendChild($source);
438
439 $br = $doc->createElement('br');
440 $entry->parentNode->insertBefore($video, $entry);
441 $entry->parentNode->insertBefore($br, $entry);
442
443 $img = $doc->createElement('img');
444 $img->setAttribute("src",
445 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
446
447 $entry->parentNode->insertBefore($img, $entry);
448 }
ec482d28
AD
449
450 function testurl() {
451 $url = htmlspecialchars($_REQUEST["url"]);
452
453 header("Content-type: text/plain");
454
455 print "URL: $url\n";
456
457 $doc = new DOMDocument();
458 @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
459 $xpath = new DOMXPath($doc);
460
461 print "Inline result: " . $this->inline_stuff([], $doc, $xpath, true) . "\n";
462
463 print "\nResulting HTML:\n";
464
465 print $doc->saveHTML();
466
467 }
cc85704f
AD
468}
469?>