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