]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_redditimgur/init.php
af_redditimgur: add option for previous
[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) {
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                                 $matches = array();
95
96                                 if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
97                                         $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
98                                 }
99
100                                 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
101
102                                         $tmp = fetch_file_contents($entry->getAttribute("href"));
103
104                                         if ($tmp) {
105                                                 $tmpdoc = new DOMDocument();
106
107                                                 if (@$tmpdoc->loadHTML($tmp)) {
108                                                         $tmpxpath = new DOMXPath($tmpdoc);
109
110                                                         $source_meta = $tmpxpath->query("//meta[@name='twitter:player:stream' and contains(@content, '.mp4')]")->item(0);
111                                                         $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
112
113                                                         if ($source_meta) {
114                                                                 $source_stream = $source_meta->getAttribute("content");
115                                                                 $poster_url = false;
116
117                                                                 if ($source_stream) {
118
119                                                                         if ($poster_meta)
120                                                                                 $poster_url = $poster_meta->getAttribute("content");
121
122                                                                         $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
123                                                                         $found = 1;
124                                                                 }
125                                                         }
126                                                 }
127                                         }
128
129                                 }
130
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
137                                 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
138
139                                         $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
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);
145
146                                         $found = true;
147                                 }
148
149                                 $matches = array();
150                                 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
151                                         preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
152                                         preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
153                                         preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
154
155                                         $vid_id = $matches[1];
156
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");
165
166                                         $br = $doc->createElement('br');
167                                         $entry->parentNode->insertBefore($iframe, $entry);
168                                         $entry->parentNode->insertBefore($br, $entry);
169
170                                         $found = true;
171                                 }
172
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"));
176
177                                         $br = $doc->createElement('br');
178                                         $entry->parentNode->insertBefore($img, $entry);
179                                         $entry->parentNode->insertBefore($br, $entry);
180
181                                         $found = true;
182                                 }
183
184                                 // linked albums & pages
185
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)) {
188
189                                         $album_content = fetch_file_contents($entry->getAttribute("href"),
190                                                 false, false, false, false, 10);
191
192                                         if ($album_content) {
193                                                 $adoc = new DOMDocument();
194
195                                                 if (@$adoc->loadHTML($album_content)) {
196                                                         $axpath = new DOMXPath($adoc);
197                                                         $aentries = $axpath->query("//meta[@property='og:image']");
198                                                         $urls = array();
199
200                                                         foreach ($aentries as $aentry) {
201
202                                                                 $url = str_replace("?fb", "", $aentry->getAttribute("content"));
203                                                                 $check_url = basename($url);
204                                                                 $check_url = mb_substr($check_url, 0, strrpos($check_url, "."));
205
206                                                                 if (!in_array($check_url, $urls)) {
207                                                                         $img = $doc->createElement('img');
208                                                                         $img->setAttribute("src", $url);
209                                                                         $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
210
211                                                                         $br = $doc->createElement('br');
212
213                                                                         $entry->parentNode->insertBefore($img, $entry);
214                                                                         $entry->parentNode->insertBefore($br, $entry);
215
216                                                                         array_push($urls, $check_url);
217
218                                                                         $found = true;
219                                                                 }
220                                                         }
221                                                 }
222                                         }
223                                 }
224
225                                 // wtf is this even
226                                 if (preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
227                                         $img_id = $matches[1];
228
229                                         $img = $doc->createElement('img');
230                                         $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
231
232                                         $br = $doc->createElement('br');
233                                         $entry->parentNode->insertBefore($img, $entry);
234                                         $entry->parentNode->insertBefore($br, $entry);
235
236                                         $found = true;
237                                 }
238                         }
239
240                         // remove tiny thumbnails
241                         if ($entry->hasAttribute("src")) {
242                                 if ($entry->parentNode && $entry->parentNode->parentNode) {
243                                         $entry->parentNode->parentNode->removeChild($entry->parentNode);
244                                 }
245                         }
246                 }
247
248                 return $found;
249         }
250
251         function hook_article_filter($article) {
252
253                 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
254                         $doc = new DOMDocument();
255                         @$doc->loadHTML($article["content"]);
256                         $xpath = new DOMXPath($doc);
257
258                         if ($this->host->get($this, "enable_content_dupcheck")) {
259                                 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
260
261                                 if ($content_link) {
262                                         $content_href = db_escape_string($content_link->getAttribute("href"));
263                                         $entry_guid = db_escape_string($article["guid_hashed"]);
264                                         $owner_uid = $article["owner_uid"];
265
266                                         if (DB_TYPE == "pgsql") {
267                                                 $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'";
268                                         } else {
269                                                 $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)";
270                                         }
271
272                                         $result = db_query("SELECT COUNT(id) AS cid
273                                                 FROM ttrss_entries, ttrss_user_entries WHERE
274                                                         ref_id = id AND
275                                                         $interval_qpart AND
276                                                         guid != '$entry_guid' AND
277                                                         owner_uid = '$owner_uid' AND
278                                                         content LIKE '%href=\"$content_href\">[link]%'");
279
280                                         if ($result) {
281                                                 $num_found = db_fetch_result($result, 0, "cid");
282
283                                                 if ($num_found > 0) $article["force_catchup"] = true;
284                                         }
285                                 }
286                         }
287
288                         $found = $this->inline_stuff($article, $doc, $xpath);
289
290                         if (!defined('NO_CURL') && function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
291                                 mb_strlen(strip_tags($article["content"])) <= 150) {
292
293                                 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
294
295                                 if ($content_link &&
296                                         strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
297                                         strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
298                                         strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
299
300                                         /* link may lead to a huge video file or whatever, we need to check content type before trying to
301                                         parse it which p much requires curl */
302
303                                         $ch = curl_init($content_link->getAttribute("href"));
304                                         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
305                                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
306                                         curl_setopt($ch, CURLOPT_HEADER, true);
307                                         curl_setopt($ch, CURLOPT_NOBODY, true);
308                                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
309                                         curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
310
311                                         @$result = curl_exec($ch);
312                                         $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
313
314                                         if ($content_type && strpos($content_type, "text/html") !== FALSE) {
315
316                                                 $tmp = fetch_file_contents($content_link->getAttribute("href"));
317
318                                                 //_debug("tmplen: " . mb_strlen($tmp));
319
320                                                 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
321
322                                                         $r = new Readability($tmp, $content_link->getAttribute("href"));
323
324                                                         if ($r->init()) {
325
326                                                                 $tmpxpath = new DOMXPath($r->dom);
327
328                                                                 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
329
330                                                                 foreach ($entries as $entry) {
331                                                                         if ($entry->hasAttribute("href")) {
332                                                                                 $entry->setAttribute("href",
333                                                                                         rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
334
335                                                                         }
336
337                                                                         if ($entry->hasAttribute("src")) {
338                                                                                 $entry->setAttribute("src",
339                                                                                         rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
340
341                                                                         }
342
343                                                                 }
344
345                                                                 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
346
347                                                                 // prob not a very good idea (breaks wikipedia pages, etc) -
348                                                                 // inliner currently is not really fit for any random web content
349
350                                                                 //$doc = new DOMDocument();
351                                                                 //@$doc->loadHTML($article["content"]);
352                                                                 //$xpath = new DOMXPath($doc);
353                                                                 //$found = $this->inline_stuff($article, $doc, $xpath);
354                                                         }
355                                                 }
356                                         }
357                                 }
358
359                         }
360
361                         $node = $doc->getElementsByTagName('body')->item(0);
362
363                         if ($node && $found) {
364                                 $article["content"] = $doc->saveXML($node);
365                         }
366                 }
367
368                 return $article;
369         }
370
371         function api_version() {
372                 return 2;
373         }
374
375         private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
376
377                 $video = $doc->createElement('video');
378                 $video->setAttribute("autoplay", "1");
379                 $video->setAttribute("controls", "1");
380                 $video->setAttribute("loop", "1");
381
382                 if ($poster_url) $video->setAttribute("poster", $poster_url);
383
384                 $source = $doc->createElement('source');
385                 $source->setAttribute("src", $source_stream);
386                 $source->setAttribute("type", "video/mp4");
387
388                 $video->appendChild($source);
389
390                 $br = $doc->createElement('br');
391                 $entry->parentNode->insertBefore($video, $entry);
392                 $entry->parentNode->insertBefore($br, $entry);
393
394                 $img = $doc->createElement('img');
395                 $img->setAttribute("src",
396                         "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
397
398                 $entry->parentNode->insertBefore($img, $entry);
399         }
400 }
401 ?>