]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_redditimgur/init.php
7eafec68265ead2ac707549e4a84ade6d5109a41
[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 init($host) {
12                 $this->host = $host;
13
14                 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
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
26                 print "<form dojoType=\"dijit.form.Form\">";
27
28                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
29                         evt.preventDefault();
30                         if (this.validate()) {
31                                 console.log(dojo.objectToQuery(this.getValues()));
32                                 new Ajax.Request('backend.php', {
33                                         parameters: dojo.objectToQuery(this.getValues()),
34                                         onComplete: function(transport) {
35                                                 notify_info(transport.responseText);
36                                         }
37                                 });
38                                 //this.reset();
39                         }
40                         </script>";
41
42                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
43                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
44                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">";
45
46                 print "<h3>" . __("Global settings") . "</h3>";
47
48                 print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
49                 print "<p/>";
50
51                 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
52                         $enable_readability_checked name=\"enable_readability\">&nbsp;";
53
54                 print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
55
56                 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
57                         __("Save")."</button>";
58
59                 print "</form>";
60
61                 print "</div>";
62         }
63
64         function save() {
65                 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
66
67                 $this->host->set($this, "enable_readability", $enable_readability);
68
69                 echo __("Configuration saved");
70         }
71
72         private function inline_stuff($article, &$doc, $xpath) {
73
74                 $entries = $xpath->query('(//a[@href]|//img[@src])');
75
76                 $found = false;
77
78                 foreach ($entries as $entry) {
79                         if ($entry->hasAttribute("href")) {
80
81                                 $matches = array();
82
83                                 if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
84
85                                         $tmp = fetch_file_contents($entry->getAttribute("href"));
86
87                                         if ($tmp) {
88                                                 $tmpdoc = new DOMDocument();
89                                                 @$tmpdoc->loadHTML($tmp);
90
91                                                 if ($tmpdoc) {
92                                                         $tmpxpath = new DOMXPath($tmpdoc);
93                                                         $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
94
95                                                         if ($source_meta) {
96                                                                 $source_stream = $source_meta->getAttribute("content");
97
98                                                                 if ($source_stream) {
99                                                                         $this->handle_as_video($doc, $entry, $source_stream);
100                                                                         $found = 1;
101                                                                 }
102                                                         }
103                                                 }
104                                         }
105
106                                 }
107
108                                 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
109
110                                         $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
111                                         $this->handle_as_video($doc, $entry, $source_stream);
112
113                                         $found = true;
114                                 }
115
116                                 $matches = array();
117                                 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
118                                         preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
119                                         preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
120
121                                         $vid_id = $matches[1];
122
123                                         $iframe = $doc->createElement("iframe");
124                                         $iframe->setAttribute("class", "youtube-player");
125                                         $iframe->setAttribute("type", "text/html");
126                                         $iframe->setAttribute("width", "640");
127                                         $iframe->setAttribute("height", "385");
128                                         $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
129                                         $iframe->setAttribute("allowfullscreen", "1");
130                                         $iframe->setAttribute("frameborder", "0");
131
132                                         $br = $doc->createElement('br');
133                                         $entry->parentNode->insertBefore($iframe, $entry);
134                                         $entry->parentNode->insertBefore($br, $entry);
135
136                                         $found = true;
137                                 }
138
139                                 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
140                                         $img = $doc->createElement('img');
141                                         $img->setAttribute("src", $entry->getAttribute("href"));
142
143                                         $br = $doc->createElement('br');
144                                         $entry->parentNode->insertBefore($img, $entry);
145                                         $entry->parentNode->insertBefore($br, $entry);
146
147                                         $found = true;
148                                 }
149
150                                 // links to imgur pages
151                                 $matches = array();
152                                 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
153
154                                         $token = $matches[2];
155
156                                         $album_content = fetch_file_contents($entry->getAttribute("href"),
157                                                 false, false, false, false, 10);
158
159                                         if ($album_content && $token) {
160                                                 $adoc = new DOMDocument();
161                                                 @$adoc->loadHTML($album_content);
162
163                                                 if ($adoc) {
164                                                         $axpath = new DOMXPath($adoc);
165                                                         $aentries = $axpath->query('(//img[@src])');
166
167                                                         foreach ($aentries as $aentry) {
168                                                                 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
169                                                                         $img = $doc->createElement('img');
170                                                                         $img->setAttribute("src", $aentry->getAttribute("src"));
171
172                                                                         $br = $doc->createElement('br');
173
174                                                                         $entry->parentNode->insertBefore($img, $entry);
175                                                                         $entry->parentNode->insertBefore($br, $entry);
176
177                                                                         $found = true;
178
179                                                                         break;
180                                                                 }
181                                                         }
182                                                 }
183                                         }
184                                 }
185
186                                 // linked albums, ffs
187                                 if (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                                                 @$adoc->loadHTML($album_content);
195
196                                                 if ($adoc) {
197                                                         $axpath = new DOMXPath($adoc);
198                                                         $aentries = $axpath->query("//meta[@property='og:image']");
199                                                         $urls = array();
200
201                                                         foreach ($aentries as $aentry) {
202
203                                                                 if (!in_array($aentry->getAttribute("content"), $urls)) {
204                                                                         $img = $doc->createElement('img');
205                                                                         $img->setAttribute("src", $aentry->getAttribute("content"));
206                                                                         $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
207
208                                                                         $br = $doc->createElement('br');
209
210                                                                         $entry->parentNode->insertBefore($img, $entry);
211                                                                         $entry->parentNode->insertBefore($br, $entry);
212
213                                                                         array_push($urls, $aentry->getAttribute("content"));
214
215                                                                         $found = true;
216                                                                 }
217                                                         }
218                                                 }
219                                         }
220                                 }
221                         }
222
223                         // remove tiny thumbnails
224                         if ($entry->hasAttribute("src")) {
225                                 if ($entry->parentNode && $entry->parentNode->parentNode) {
226                                         $entry->parentNode->parentNode->removeChild($entry->parentNode);
227                                 }
228                         }
229                 }
230
231                 return $found;
232         }
233
234         function hook_article_filter($article) {
235
236                 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
237                         $doc = new DOMDocument();
238                         @$doc->loadHTML($article["content"]);
239                         $xpath = new DOMXPath($doc);
240
241                         $found = $this->inline_stuff($article, $doc, $xpath);
242
243                         if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
244                                 mb_strlen(strip_tags($article["content"])) <= 150) {
245
246                                 if (!class_exists("Readability")) require_once(__DIR__ . "/classes/Readability.php");
247
248                                 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
249
250                                 if ($content_link && strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
251
252                                         /* link may lead to a huge video file or whatever, we need to check content type before trying to
253                                         parse it which p much requires curl */
254
255                                         $ch = curl_init($content_link->getAttribute("href"));
256                                         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
257                                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
258                                         curl_setopt($ch, CURLOPT_HEADER, true);
259                                         curl_setopt($ch, CURLOPT_NOBODY, true);
260                                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
261                                                 !ini_get("safe_mode") && !ini_get("open_basedir"));
262                                         curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
263
264                                         @$result = curl_exec($ch);
265                                         $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
266
267                                         if ($content_type && strpos($content_type, "text/html") !== FALSE) {
268
269                                                 $tmp = fetch_file_contents($content_link->getAttribute("href"));
270
271                                                 if ($tmp) {
272                                                         $r = new Readability($tmp, $content_link->getAttribute("href"));
273
274                                                         if ($r->init()) {
275                                                                 //$article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
276
277                                                                 $tmpxpath = new DOMXPath($r->dom);
278
279                                                                 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
280
281                                                                 foreach ($entries as $entry) {
282                                                                         if ($entry->hasAttribute("href")) {
283                                                                                 $entry->setAttribute("href",
284                                                                                         rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
285
286                                                                         }
287
288                                                                         if ($entry->hasAttribute("src")) {
289                                                                                 $entry->setAttribute("src",
290                                                                                         rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
291
292                                                                         }
293
294                                                                 }
295
296                                                                 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
297
298                                                                 $doc = new DOMDocument();
299                                                                 @$doc->loadHTML($article["content"]);
300                                                                 $xpath = new DOMXPath($doc);
301
302                                                                 $found = $this->inline_stuff($article, $doc, $xpath);
303                                                         }
304                                                 }
305                                         }
306                                 }
307
308                         }
309
310                         $node = $doc->getElementsByTagName('body')->item(0);
311
312                         if ($node && $found) {
313                                 $article["content"] = $doc->saveXML($node);
314                         }
315                 }
316
317                 return $article;
318         }
319
320         function api_version() {
321                 return 2;
322         }
323
324         private function handle_as_video($doc, $entry, $source_stream) {
325
326                 $video = $doc->createElement('video');
327                 $video->setAttribute("autoplay", "1");
328                 $video->setAttribute("controls", "1");
329                 $video->setAttribute("loop", "1");
330
331                 $source = $doc->createElement('source');
332                 $source->setAttribute("src", $source_stream);
333                 $source->setAttribute("type", "video/mp4");
334
335                 $video->appendChild($source);
336
337                 $br = $doc->createElement('br');
338                 $entry->parentNode->insertBefore($video, $entry);
339                 $entry->parentNode->insertBefore($br, $entry);
340
341                 $img = $doc->createElement('img');
342                 $img->setAttribute("src",
343                         "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
344
345                 $entry->parentNode->insertBefore($img, $entry);
346         }
347 }
348 ?>