]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
af_redditimgur: try to handle mobile imgur albums
[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 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
30 $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
31 $enable_content_dupcheck_checked = $enable_content_dupcheck ? "checked" : "";
32
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
53 print "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
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
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>";
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";
77 $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]) == "true";
78
79 $this->host->set($this, "enable_readability", $enable_readability, false);
80 $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck);
81
82 echo __("Configuration saved");
83 }
84
85 private function inline_stuff($article, &$doc, $xpath, $debug = false) {
86
87 $entries = $xpath->query('(//a[@href]|//img[@src])');
88
89 $found = false;
90
91 foreach ($entries as $entry) {
92 if ($entry->hasAttribute("href")) {
93
94 _debug("processing href: " . $entry->getAttribute("href"), $debug);
95
96 $matches = array();
97
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
102 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
103
104 _debug("Handling as Gfycat", $debug);
105
106 $tmp = fetch_file_contents($entry->getAttribute("href"));
107
108 if ($tmp) {
109 $tmpdoc = new DOMDocument();
110
111 if (@$tmpdoc->loadHTML($tmp)) {
112 $tmpxpath = new DOMXPath($tmpdoc);
113
114 $source_meta = $tmpxpath->query("//meta[@name='twitter:player:stream' and contains(@content, '.mp4')]")->item(0);
115 $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
116
117 if ($source_meta) {
118 $source_stream = $source_meta->getAttribute("content");
119 $poster_url = false;
120
121 if ($source_stream) {
122
123 if ($poster_meta)
124 $poster_url = $poster_meta->getAttribute("content");
125
126 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
127 $found = 1;
128 }
129 }
130 }
131 }
132
133 }
134
135 // imgur .gif -> .gifv
136 if (preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
137 _debug("Handling as imgur gif (->gifv)", $debug);
138
139 $entry->setAttribute("href",
140 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
141 }
142
143 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
144 _debug("Handling as imgur gifv", $debug);
145
146 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
147
148 if (strpos($source_stream, "i.imgur.com") !== FALSE)
149 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
150
151 $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug);
152
153 $found = true;
154 }
155
156 $matches = array();
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) ||
160 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
161
162 $vid_id = $matches[1];
163
164 _debug("Handling as youtube: $vid_id", $debug);
165
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");
174
175 $br = $doc->createElement('br');
176 $entry->parentNode->insertBefore($iframe, $entry);
177 $entry->parentNode->insertBefore($br, $entry);
178
179 $found = true;
180 }
181
182 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
183 _debug("Handling as a picture", $debug);
184
185 $img = $doc->createElement('img');
186 $img->setAttribute("src", $entry->getAttribute("href"));
187
188 $br = $doc->createElement('br');
189 $entry->parentNode->insertBefore($img, $entry);
190 $entry->parentNode->insertBefore($br, $entry);
191
192 $found = true;
193 }
194
195 // linked albums & pages
196
197 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
198 preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
199
200 _debug("Handling as an imgur page/album/gallery", $debug);
201
202 $album_content = fetch_file_contents($entry->getAttribute("href"),
203 false, false, false, false, 10);
204
205 if ($album_content) {
206 $adoc = new DOMDocument();
207
208 if (@$adoc->loadHTML($album_content)) {
209 $axpath = new DOMXPath($adoc);
210
211 /*$aentries = $axpath->query("//meta[@property='og:image']");
212 $urls = array();
213
214 foreach ($aentries as $aentry) {
215
216 _debug("og:image content=" . $aentry->getAttribute("content"), $debug);
217
218 $url = str_replace("?fb", "", $aentry->getAttribute("content"));
219 $check_url = basename($url);
220 $check_url = mb_substr($check_url, 0, strrpos($check_url, "."));
221
222 if (!in_array($check_url, $urls)) {
223 $img = $doc->createElement('img');
224 $img->setAttribute("src", $url);
225 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
226
227 $br = $doc->createElement('br');
228
229 $entry->parentNode->insertBefore($img, $entry);
230 $entry->parentNode->insertBefore($br, $entry);
231
232 array_push($urls, $check_url);
233
234 $found = true;
235 }
236 } */
237
238 //if ($debug) print_r($album_content);
239
240 $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src])");
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
262 }
263
264 if ($debug) print_r($urls);
265 }
266 }
267 }
268
269 // wtf is this even
270 if (preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
271 $img_id = $matches[1];
272
273 _debug("handling as gyazo: $img_id", $debug);
274
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 }
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 }
293
294 return $found;
295 }
296
297 function hook_article_filter($article) {
298
299 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
300 $doc = new DOMDocument();
301 @$doc->loadHTML($article["content"]);
302 $xpath = new DOMXPath($doc);
303
304 if ($this->host->get($this, "enable_content_dupcheck")) {
305 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
306
307 if ($content_link) {
308 $content_href = db_escape_string($content_link->getAttribute("href"));
309 $entry_guid = db_escape_string($article["guid_hashed"]);
310 $owner_uid = $article["owner_uid"];
311
312 if (DB_TYPE == "pgsql") {
313 $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'";
314 } else {
315 $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)";
316 }
317
318 $result = db_query("SELECT COUNT(id) AS cid
319 FROM ttrss_entries, ttrss_user_entries WHERE
320 ref_id = id AND
321 $interval_qpart AND
322 guid != '$entry_guid' AND
323 owner_uid = '$owner_uid' AND
324 content LIKE '%href=\"$content_href\">[link]%'");
325
326 if ($result) {
327 $num_found = db_fetch_result($result, 0, "cid");
328
329 if ($num_found > 0) $article["force_catchup"] = true;
330 }
331 }
332 }
333
334 $found = $this->inline_stuff($article, $doc, $xpath);
335
336 if (!defined('NO_CURL') && function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
337 mb_strlen(strip_tags($article["content"])) <= 150) {
338
339 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
340
341 if ($content_link &&
342 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
343 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
344 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
345
346 /* link may lead to a huge video file or whatever, we need to check content type before trying to
347 parse it which p much requires curl */
348
349 $ch = curl_init($content_link->getAttribute("href"));
350 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
351 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
352 curl_setopt($ch, CURLOPT_HEADER, true);
353 curl_setopt($ch, CURLOPT_NOBODY, true);
354 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
355 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
356
357 @$result = curl_exec($ch);
358 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
359
360 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
361
362 $tmp = fetch_file_contents($content_link->getAttribute("href"));
363
364 //_debug("tmplen: " . mb_strlen($tmp));
365
366 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
367
368 $r = new Readability($tmp, $content_link->getAttribute("href"));
369
370 if ($r->init()) {
371
372 $tmpxpath = new DOMXPath($r->dom);
373
374 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
375
376 foreach ($entries as $entry) {
377 if ($entry->hasAttribute("href")) {
378 $entry->setAttribute("href",
379 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
380
381 }
382
383 if ($entry->hasAttribute("src")) {
384 $entry->setAttribute("src",
385 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
386
387 }
388
389 }
390
391 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
392
393 // prob not a very good idea (breaks wikipedia pages, etc) -
394 // inliner currently is not really fit for any random web content
395
396 //$doc = new DOMDocument();
397 //@$doc->loadHTML($article["content"]);
398 //$xpath = new DOMXPath($doc);
399 //$found = $this->inline_stuff($article, $doc, $xpath);
400 }
401 }
402 }
403 }
404
405 }
406
407 $node = $doc->getElementsByTagName('body')->item(0);
408
409 if ($node && $found) {
410 $article["content"] = $doc->saveXML($node);
411 }
412 }
413
414 return $article;
415 }
416
417 function api_version() {
418 return 2;
419 }
420
421 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) {
422
423 _debug("handle_as_video: $source_stream", $debug);
424
425 $video = $doc->createElement('video');
426 $video->setAttribute("autoplay", "1");
427 $video->setAttribute("controls", "1");
428 $video->setAttribute("loop", "1");
429
430 if ($poster_url) $video->setAttribute("poster", $poster_url);
431
432 $source = $doc->createElement('source');
433 $source->setAttribute("src", $source_stream);
434 $source->setAttribute("type", "video/mp4");
435
436 $video->appendChild($source);
437
438 $br = $doc->createElement('br');
439 $entry->parentNode->insertBefore($video, $entry);
440 $entry->parentNode->insertBefore($br, $entry);
441
442 $img = $doc->createElement('img');
443 $img->setAttribute("src",
444 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
445
446 $entry->parentNode->insertBefore($img, $entry);
447 }
448
449 function testurl() {
450 $url = htmlspecialchars($_REQUEST["url"]);
451
452 header("Content-type: text/plain");
453
454 print "URL: $url\n";
455
456 $doc = new DOMDocument();
457 @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
458 $xpath = new DOMXPath($doc);
459
460 print "Inline result: " . $this->inline_stuff([], $doc, $xpath, true) . "\n";
461
462 print "\nResulting HTML:\n";
463
464 print $doc->saveHTML();
465
466 }
467 }
468 ?>