]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
af_redditimgur: add crude dupe checking based on content links
[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
d2a421e3 11 function init($host) {
19c73507
AD
12 $this->host = $host;
13
14 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
b90c4468
AD
15 $host->add_hook($host::HOOK_PREFS_TAB, $this);
16 }
17
18 function hook_prefs_tab($args) {
19 if ($args != "prefFeeds") return;
20
21 print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">";
22
23 $enable_readability = $this->host->get($this, "enable_readability");
24 $enable_readability_checked = $enable_readability ? "checked" : "";
25
4f5204dd
AD
26 $enable_dupecheck = $this->host->get($this, "enable_dupecheck");
27 $enable_dupecheck_checked = $enable_dupecheck ? "checked" : "";
28
b90c4468
AD
29 print "<form dojoType=\"dijit.form.Form\">";
30
31 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
32 evt.preventDefault();
33 if (this.validate()) {
34 console.log(dojo.objectToQuery(this.getValues()));
35 new Ajax.Request('backend.php', {
36 parameters: dojo.objectToQuery(this.getValues()),
37 onComplete: function(transport) {
38 notify_info(transport.responseText);
39 }
40 });
41 //this.reset();
42 }
43 </script>";
44
45 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
46 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
47 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">";
48
49 print "<h3>" . __("Global settings") . "</h3>";
50
51 print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
52 print "<p/>";
53
54 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
55 $enable_readability_checked name=\"enable_readability\">&nbsp;";
56
57 print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
58
4f5204dd
AD
59 print "<br/>";
60
61 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_dupecheck\"
62 $enable_dupecheck_checked name=\"enable_dupecheck\">&nbsp;";
63
64 print "<label for=\"enable_dupecheck\">" . __("Mark duplicates as read using content links") . "</label>";
65
b90c4468
AD
66 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
67 __("Save")."</button>";
68
69 print "</form>";
70
71 print "</div>";
72 }
73
74 function save() {
75 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
4f5204dd
AD
76 $enable_dupecheck = checkbox_to_sql_bool($_POST["enable_dupecheck"]) == "true";
77
b90c4468 78 $this->host->set($this, "enable_readability", $enable_readability);
4f5204dd 79 $this->host->set($this, "enable_dupecheck", $enable_dupecheck);
b90c4468
AD
80
81 echo __("Configuration saved");
19c73507
AD
82 }
83
6322fc68 84 private function inline_stuff($article, &$doc, $xpath) {
cc85704f 85
6322fc68 86 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 87
6322fc68 88 $found = false;
ce7d5e87 89
6322fc68
AD
90 foreach ($entries as $entry) {
91 if ($entry->hasAttribute("href")) {
299aeb30 92
6322fc68 93 $matches = array();
5d429910 94
5f297a5c
AD
95 if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
96 $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
97 }
98
b8887ebb 99 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
5d429910 100
6322fc68 101 $tmp = fetch_file_contents($entry->getAttribute("href"));
5d429910 102
6322fc68
AD
103 if ($tmp) {
104 $tmpdoc = new DOMDocument();
105 @$tmpdoc->loadHTML($tmp);
5d429910 106
6322fc68
AD
107 if ($tmpdoc) {
108 $tmpxpath = new DOMXPath($tmpdoc);
b8887ebb 109
6322fc68 110 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
b8887ebb 111 $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
5d429910 112
6322fc68
AD
113 if ($source_meta) {
114 $source_stream = $source_meta->getAttribute("content");
b8887ebb 115 $poster_url = false;
5d429910 116
6322fc68 117 if ($source_stream) {
b8887ebb
AD
118
119 if ($poster_meta)
120 $poster_url = $poster_meta->getAttribute("content");
121
122 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
6322fc68 123 $found = 1;
5d429910 124 }
5d429910 125 }
6322fc68
AD
126 }
127 }
5d429910 128
6322fc68 129 }
98e20510 130
5f297a5c
AD
131 // imgur .gif -> .gifv
132 if (preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
133 $entry->setAttribute("href",
134 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
135 }
136
6322fc68 137 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
5d429910 138
6322fc68 139 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
b8887ebb
AD
140
141 if (strpos($source_stream, "i.imgur.com") !== FALSE)
142 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
143
144 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
98e20510 145
6322fc68
AD
146 $found = true;
147 }
9875d717 148
6322fc68 149 $matches = array();
f7745af9 150 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
9ec9a8f9 151 preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
f7745af9 152 preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
6322fc68 153 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
3b9ca4e6 154
6322fc68 155 $vid_id = $matches[1];
3b9ca4e6 156
6322fc68
AD
157 $iframe = $doc->createElement("iframe");
158 $iframe->setAttribute("class", "youtube-player");
159 $iframe->setAttribute("type", "text/html");
160 $iframe->setAttribute("width", "640");
161 $iframe->setAttribute("height", "385");
162 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
163 $iframe->setAttribute("allowfullscreen", "1");
164 $iframe->setAttribute("frameborder", "0");
3b9ca4e6 165
6322fc68
AD
166 $br = $doc->createElement('br');
167 $entry->parentNode->insertBefore($iframe, $entry);
168 $entry->parentNode->insertBefore($br, $entry);
3b9ca4e6 169
6322fc68
AD
170 $found = true;
171 }
3b9ca4e6 172
6322fc68
AD
173 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
174 $img = $doc->createElement('img');
175 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 176
6322fc68
AD
177 $br = $doc->createElement('br');
178 $entry->parentNode->insertBefore($img, $entry);
179 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87 180
6322fc68
AD
181 $found = true;
182 }
ce7d5e87 183
d4ac4fc6 184 // linked albums & pages
35055d05 185
d4ac4fc6
AD
186 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
187 preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05 188
6322fc68
AD
189 $album_content = fetch_file_contents($entry->getAttribute("href"),
190 false, false, false, false, 10);
35055d05 191
6322fc68
AD
192 if ($album_content) {
193 $adoc = new DOMDocument();
194 @$adoc->loadHTML($album_content);
35055d05 195
6322fc68
AD
196 if ($adoc) {
197 $axpath = new DOMXPath($adoc);
198 $aentries = $axpath->query("//meta[@property='og:image']");
199 $urls = array();
35055d05 200
6322fc68 201 foreach ($aentries as $aentry) {
7ff4d1fa 202
ecc92d92
AD
203 $url = str_replace("?fb", "", $aentry->getAttribute("content"));
204
205 if (!in_array($url, $urls)) {
6322fc68 206 $img = $doc->createElement('img');
ecc92d92 207 $img->setAttribute("src", $url);
6322fc68 208 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa 209
6322fc68 210 $br = $doc->createElement('br');
7ff4d1fa 211
6322fc68
AD
212 $entry->parentNode->insertBefore($img, $entry);
213 $entry->parentNode->insertBefore($br, $entry);
98e20510 214
ecc92d92 215 array_push($urls, $url);
98e20510 216
6322fc68 217 $found = true;
35055d05
AD
218 }
219 }
cc85704f 220 }
cc85704f 221 }
6322fc68
AD
222 }
223 }
224
225 // remove tiny thumbnails
226 if ($entry->hasAttribute("src")) {
227 if ($entry->parentNode && $entry->parentNode->parentNode) {
228 $entry->parentNode->parentNode->removeChild($entry->parentNode);
229 }
230 }
231 }
cc85704f 232
6322fc68
AD
233 return $found;
234 }
b90c4468 235
6322fc68 236 function hook_article_filter($article) {
b90c4468 237
6322fc68
AD
238 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
239 $doc = new DOMDocument();
240 @$doc->loadHTML($article["content"]);
241 $xpath = new DOMXPath($doc);
99bb8b3b 242
4f5204dd
AD
243 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
244
245 if ($content_link && $this->host->get($this, "enable_dupecheck")) {
246
247 if (DB_TYPE == "pgsql") {
248 $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
249 } else {
250 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
251 }
252
253 $content_href = db_escape_string($content_link->getAttribute("href"));
254 $owner_uid = $article["owner_uid"];
255 $guid = db_escape_string($article["guid_hashed"]);
256
257 $result = db_query("SELECT id FROM ttrss_entries, ttrss_user_entries
258 WHERE
259 content LIKE '%$content_href%' AND
260 $date_qpart AND
261 ref_id = id AND
262 owner_uid = $owner_uid
263 AND guid != '$guid'
264 LIMIT 1");
265
266 if (db_num_rows($result) != 0) {
267 $found = true;
268 $article["force_catchup"] = true;
269 }
270 }
271
272 if (!$found) $found = $this->inline_stuff($article, $doc, $xpath);
b90c4468 273
6475fc7e
AD
274 if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
275 mb_strlen(strip_tags($article["content"])) <= 150) {
276
8b08d9d7 277 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
b90c4468 278
12d880d7
AD
279 if ($content_link &&
280 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
9ec9a8f9 281 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
12d880d7 282 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
fd61fd6e 283
6475fc7e
AD
284 /* link may lead to a huge video file or whatever, we need to check content type before trying to
285 parse it which p much requires curl */
fd61fd6e 286
6475fc7e
AD
287 $ch = curl_init($content_link->getAttribute("href"));
288 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
289 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
290 curl_setopt($ch, CURLOPT_HEADER, true);
291 curl_setopt($ch, CURLOPT_NOBODY, true);
292 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
293 !ini_get("safe_mode") && !ini_get("open_basedir"));
294 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
fd61fd6e 295
6475fc7e
AD
296 @$result = curl_exec($ch);
297 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
b90c4468 298
6475fc7e 299 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
fd61fd6e 300
6475fc7e 301 $tmp = fetch_file_contents($content_link->getAttribute("href"));
fd61fd6e 302
6475fc7e
AD
303 if ($tmp) {
304 $r = new Readability($tmp, $content_link->getAttribute("href"));
fd61fd6e 305
6475fc7e 306 if ($r->init()) {
b90c4468 307
6475fc7e 308 $tmpxpath = new DOMXPath($r->dom);
b90c4468 309
6475fc7e 310 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
6322fc68 311
6475fc7e
AD
312 foreach ($entries as $entry) {
313 if ($entry->hasAttribute("href")) {
314 $entry->setAttribute("href",
315 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
316
317 }
b90c4468 318
6475fc7e
AD
319 if ($entry->hasAttribute("src")) {
320 $entry->setAttribute("src",
321 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
cc85704f 322
6475fc7e 323 }
6322fc68 324
6475fc7e
AD
325 }
326
327 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
328
47888b3d
AD
329 // prob not a very good idea (breaks wikipedia pages, etc) -
330 // inliner currently is not really fit for any random web content
6475fc7e 331
47888b3d
AD
332 //$doc = new DOMDocument();
333 //@$doc->loadHTML($article["content"]);
334 //$xpath = new DOMXPath($doc);
335 //$found = $this->inline_stuff($article, $doc, $xpath);
6475fc7e 336 }
6322fc68 337 }
a16d7a5d 338 }
cc85704f 339 }
6322fc68
AD
340
341 }
342
343 $node = $doc->getElementsByTagName('body')->item(0);
344
345 if ($node && $found) {
346 $article["content"] = $doc->saveXML($node);
347 }
cc85704f
AD
348 }
349
350 return $article;
351 }
106a3de9
AD
352
353 function api_version() {
354 return 2;
355 }
356
b8887ebb 357 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
5d429910
AD
358
359 $video = $doc->createElement('video');
360 $video->setAttribute("autoplay", "1");
5dcc7bf1 361 $video->setAttribute("controls", "1");
5d429910
AD
362 $video->setAttribute("loop", "1");
363
b8887ebb
AD
364 if ($poster_url) $video->setAttribute("poster", $poster_url);
365
5d429910
AD
366 $source = $doc->createElement('source');
367 $source->setAttribute("src", $source_stream);
368 $source->setAttribute("type", "video/mp4");
369
370 $video->appendChild($source);
371
372 $br = $doc->createElement('br');
373 $entry->parentNode->insertBefore($video, $entry);
374 $entry->parentNode->insertBefore($br, $entry);
375
376 $img = $doc->createElement('img');
377 $img->setAttribute("src",
378 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
379
380 $entry->parentNode->insertBefore($img, $entry);
381 }
cc85704f
AD
382}
383?>