]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
add some print_checkbox/print_button calls; rename some plugin preference pane titles
[tt-rss.git] / plugins / af_redditimgur / init.php
1 <?php
2 class Af_RedditImgur extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
7 "Inline images (and other content) in Reddit RSS feeds",
8 "fox");
9 }
10
11 function flags() {
12 return array("needs_curl" => true);
13 }
14
15 function init($host) {
16 $this->host = $host;
17
18 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
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 dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Reddit content settings (af_redditimgur)')."\">";
26
27 $enable_readability = $this->host->get($this, "enable_readability");
28 $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
29
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
46 print_hidden("op", "pluginhandler");
47 print_hidden("method", "save");
48 print_hidden("plugin", "af_redditimgur");
49
50 print "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
51 print "<p/>";
52
53 print_checkbox("enable_readability", $enable_readability);
54 print "&nbsp;<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
55
56 print "<br/>";
57
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"));
61 print "</form>";
62
63 print "</div>";
64 }
65
66 function save() {
67 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
68 $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]) == "true";
69
70 $this->host->set($this, "enable_readability", $enable_readability, false);
71 $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck);
72
73 echo __("Configuration saved");
74 }
75
76 private function inline_stuff($article, &$doc, $xpath, $debug = false) {
77
78 $entries = $xpath->query('(//a[@href]|//img[@src])');
79
80 $found = false;
81
82 foreach ($entries as $entry) {
83 if ($entry->hasAttribute("href")) {
84
85 _debug("processing href: " . $entry->getAttribute("href"), $debug);
86
87 $matches = array();
88
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();
100 if ($tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
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
116 if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
117 $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
118 }
119
120 if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
121
122 _debug("Handling as Gfycat", $debug);
123
124 $tmp = fetch_file_contents($entry->getAttribute("href"));
125
126 if ($tmp) {
127 $tmpdoc = new DOMDocument();
128
129 if (@$tmpdoc->loadHTML($tmp)) {
130 $tmpxpath = new DOMXPath($tmpdoc);
131
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);
134
135 if ($source_node && $poster_node) {
136 $source_stream = $source_node->getAttribute("src");
137 $poster_url = $poster_node->getAttribute("poster");
138
139 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
140 $found = 1;
141 }
142 }
143 }
144 }
145
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
172 // imgur .gif -> .gifv
173 if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
174 _debug("Handling as imgur gif (->gifv)", $debug);
175
176 $entry->setAttribute("href",
177 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
178 }
179
180 if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) {
181 _debug("Handling as imgur gifv", $debug);
182
183 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
184
185 if (strpos($source_stream, "imgur.com") !== FALSE)
186 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
187
188 $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug);
189
190 $found = true;
191 }
192
193 $matches = array();
194 if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
195 preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
196 preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
197 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
198
199 $vid_id = $matches[1];
200
201 _debug("Handling as youtube: $vid_id", $debug);
202
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");
211
212 $br = $doc->createElement('br');
213 $entry->parentNode->insertBefore($iframe, $entry);
214 $entry->parentNode->insertBefore($br, $entry);
215
216 $found = true;
217 }
218
219 if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) ||
220 mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== FALSE ||
221 mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== FALSE) {
222
223 _debug("Handling as a picture", $debug);
224
225 $img = $doc->createElement('img');
226 $img->setAttribute("src", $entry->getAttribute("href"));
227
228 $br = $doc->createElement('br');
229 $entry->parentNode->insertBefore($img, $entry);
230 $entry->parentNode->insertBefore($br, $entry);
231
232 $found = true;
233 }
234
235 // linked albums & pages
236
237 if (!$found && preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
238 preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
239
240 _debug("Handling as an imgur page/album/gallery", $debug);
241
242 $album_content = fetch_file_contents($entry->getAttribute("href"),
243 false, false, false, false, 10);
244
245 if ($album_content) {
246 $adoc = new DOMDocument();
247
248 if (@$adoc->loadHTML($album_content)) {
249 $axpath = new DOMXPath($adoc);
250
251 $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src] | //div[@class='video-elements']/source)");
252 $urls = [];
253
254 foreach ($aentries as $aentry) {
255
256 $url = $aentry->getAttribute("src");
257
258 if (!in_array($url, $urls)) {
259
260 if ($aentry->tagName == "img") {
261
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
272 if (strpos($url, "imgur.com") !== FALSE)
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 }
280
281 array_push($urls, $url);
282
283 $found = true;
284 }
285
286 }
287
288 if ($debug) print_r($urls);
289 }
290 }
291 }
292
293 // wtf is this even
294 if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
295 $img_id = $matches[1];
296
297 _debug("handling as gyazo: $img_id", $debug);
298
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 }
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 }
317
318 return $found;
319 }
320
321 function hook_article_filter($article) {
322
323 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
324 $doc = new DOMDocument();
325 @$doc->loadHTML($article["content"]);
326 $xpath = new DOMXPath($doc);
327
328 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
329
330 if ($this->host->get($this, "enable_content_dupcheck")) {
331
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"];
336
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 }
342
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]%'");
350
351 if ($result) {
352 $num_found = db_fetch_result($result, 0, "cid");
353
354 if ($num_found > 0) $article["force_catchup"] = true;
355 }
356 }
357 }
358
359 $found = $this->inline_stuff($article, $doc, $xpath);
360
361 $node = $doc->getElementsByTagName('body')->item(0);
362
363 if ($node && $found) {
364 $article["content"] = $doc->saveXML($node);
365 } else if ($content_link) {
366 $article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath);
367 }
368 }
369
370 return $article;
371 }
372
373 function api_version() {
374 return 2;
375 }
376
377 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) {
378
379 _debug("handle_as_video: $source_stream", $debug);
380
381 $video = $doc->createElement('video');
382 $video->setAttribute("autoplay", "1");
383 $video->setAttribute("controls", "1");
384 $video->setAttribute("loop", "1");
385
386 if ($poster_url) $video->setAttribute("poster", $poster_url);
387
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 }
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
416 $found = $this->inline_stuff([], $doc, $xpath, true);
417
418 print "Inline result: $found\n";
419
420 if (!$found) {
421 print "\nReadability result:\n";
422
423 $article = $this->readability([], $url, $doc, $xpath, true);
424
425 print_r($article);
426 } else {
427 print "\nResulting HTML:\n";
428
429 print $doc->saveHTML();
430 }
431 }
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 }
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 }
512 }
513 ?>