]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
redditimgur: don't try to readability parse youtube, add another youtube embed variant
[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
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");
19c73507
AD
70 }
71
6322fc68 72 private function inline_stuff($article, &$doc, $xpath) {
cc85704f 73
6322fc68 74 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 75
6322fc68 76 $found = false;
ce7d5e87 77
6322fc68
AD
78 foreach ($entries as $entry) {
79 if ($entry->hasAttribute("href")) {
299aeb30 80
6322fc68 81 $matches = array();
5d429910 82
6322fc68 83 if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
5d429910 84
6322fc68 85 $tmp = fetch_file_contents($entry->getAttribute("href"));
5d429910 86
6322fc68
AD
87 if ($tmp) {
88 $tmpdoc = new DOMDocument();
89 @$tmpdoc->loadHTML($tmp);
5d429910 90
6322fc68
AD
91 if ($tmpdoc) {
92 $tmpxpath = new DOMXPath($tmpdoc);
93 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
5d429910 94
6322fc68
AD
95 if ($source_meta) {
96 $source_stream = $source_meta->getAttribute("content");
5d429910 97
6322fc68
AD
98 if ($source_stream) {
99 $this->handle_as_video($doc, $entry, $source_stream);
100 $found = 1;
5d429910 101 }
5d429910 102 }
6322fc68
AD
103 }
104 }
5d429910 105
6322fc68 106 }
98e20510 107
6322fc68 108 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
5d429910 109
6322fc68
AD
110 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
111 $this->handle_as_video($doc, $entry, $source_stream);
98e20510 112
6322fc68
AD
113 $found = true;
114 }
9875d717 115
6322fc68 116 $matches = array();
f7745af9 117 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
9ec9a8f9 118 preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
f7745af9 119 preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
6322fc68 120 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
3b9ca4e6 121
6322fc68 122 $vid_id = $matches[1];
3b9ca4e6 123
6322fc68
AD
124 $iframe = $doc->createElement("iframe");
125 $iframe->setAttribute("class", "youtube-player");
126 $iframe->setAttribute("type", "text/html");
127 $iframe->setAttribute("width", "640");
128 $iframe->setAttribute("height", "385");
129 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
130 $iframe->setAttribute("allowfullscreen", "1");
131 $iframe->setAttribute("frameborder", "0");
3b9ca4e6 132
6322fc68
AD
133 $br = $doc->createElement('br');
134 $entry->parentNode->insertBefore($iframe, $entry);
135 $entry->parentNode->insertBefore($br, $entry);
3b9ca4e6 136
6322fc68
AD
137 $found = true;
138 }
3b9ca4e6 139
6322fc68
AD
140 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
141 $img = $doc->createElement('img');
142 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 143
6322fc68
AD
144 $br = $doc->createElement('br');
145 $entry->parentNode->insertBefore($img, $entry);
146 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87 147
6322fc68
AD
148 $found = true;
149 }
ce7d5e87 150
6322fc68
AD
151 // links to imgur pages
152 $matches = array();
153 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
ce7d5e87 154
6322fc68 155 $token = $matches[2];
ce7d5e87 156
6322fc68
AD
157 $album_content = fetch_file_contents($entry->getAttribute("href"),
158 false, false, false, false, 10);
ce7d5e87 159
6322fc68
AD
160 if ($album_content && $token) {
161 $adoc = new DOMDocument();
162 @$adoc->loadHTML($album_content);
ce7d5e87 163
6322fc68
AD
164 if ($adoc) {
165 $axpath = new DOMXPath($adoc);
166 $aentries = $axpath->query('(//img[@src])');
ce7d5e87 167
6322fc68
AD
168 foreach ($aentries as $aentry) {
169 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
170 $img = $doc->createElement('img');
171 $img->setAttribute("src", $aentry->getAttribute("src"));
7ff4d1fa 172
6322fc68 173 $br = $doc->createElement('br');
7ff4d1fa 174
6322fc68
AD
175 $entry->parentNode->insertBefore($img, $entry);
176 $entry->parentNode->insertBefore($br, $entry);
7ff4d1fa 177
6322fc68 178 $found = true;
35055d05 179
6322fc68 180 break;
ce7d5e87 181 }
a16d7a5d 182 }
6322fc68
AD
183 }
184 }
185 }
35055d05 186
6322fc68
AD
187 // linked albums, ffs
188 if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05 189
6322fc68
AD
190 $album_content = fetch_file_contents($entry->getAttribute("href"),
191 false, false, false, false, 10);
35055d05 192
6322fc68
AD
193 if ($album_content) {
194 $adoc = new DOMDocument();
195 @$adoc->loadHTML($album_content);
35055d05 196
6322fc68
AD
197 if ($adoc) {
198 $axpath = new DOMXPath($adoc);
199 $aentries = $axpath->query("//meta[@property='og:image']");
200 $urls = array();
35055d05 201
6322fc68 202 foreach ($aentries as $aentry) {
7ff4d1fa 203
6322fc68
AD
204 if (!in_array($aentry->getAttribute("content"), $urls)) {
205 $img = $doc->createElement('img');
206 $img->setAttribute("src", $aentry->getAttribute("content"));
207 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa 208
6322fc68 209 $br = $doc->createElement('br');
7ff4d1fa 210
6322fc68
AD
211 $entry->parentNode->insertBefore($img, $entry);
212 $entry->parentNode->insertBefore($br, $entry);
98e20510 213
6322fc68 214 array_push($urls, $aentry->getAttribute("content"));
98e20510 215
6322fc68 216 $found = true;
35055d05
AD
217 }
218 }
cc85704f 219 }
cc85704f 220 }
6322fc68
AD
221 }
222 }
223
224 // remove tiny thumbnails
225 if ($entry->hasAttribute("src")) {
226 if ($entry->parentNode && $entry->parentNode->parentNode) {
227 $entry->parentNode->parentNode->removeChild($entry->parentNode);
228 }
229 }
230 }
cc85704f 231
6322fc68
AD
232 return $found;
233 }
b90c4468 234
6322fc68 235 function hook_article_filter($article) {
b90c4468 236
6322fc68
AD
237 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
238 $doc = new DOMDocument();
239 @$doc->loadHTML($article["content"]);
240 $xpath = new DOMXPath($doc);
99bb8b3b 241
6322fc68 242 $found = $this->inline_stuff($article, $doc, $xpath);
b90c4468 243
6475fc7e
AD
244 if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
245 mb_strlen(strip_tags($article["content"])) <= 150) {
246
8b08d9d7 247 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
b90c4468 248
6322fc68 249 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
fd61fd6e 250
12d880d7
AD
251 if ($content_link &&
252 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
9ec9a8f9 253 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
12d880d7 254 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
fd61fd6e 255
6475fc7e
AD
256 /* link may lead to a huge video file or whatever, we need to check content type before trying to
257 parse it which p much requires curl */
fd61fd6e 258
6475fc7e
AD
259 $ch = curl_init($content_link->getAttribute("href"));
260 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
261 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
262 curl_setopt($ch, CURLOPT_HEADER, true);
263 curl_setopt($ch, CURLOPT_NOBODY, true);
264 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
265 !ini_get("safe_mode") && !ini_get("open_basedir"));
266 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
fd61fd6e 267
6475fc7e
AD
268 @$result = curl_exec($ch);
269 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
b90c4468 270
6475fc7e 271 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
fd61fd6e 272
6475fc7e 273 $tmp = fetch_file_contents($content_link->getAttribute("href"));
fd61fd6e 274
6475fc7e
AD
275 if ($tmp) {
276 $r = new Readability($tmp, $content_link->getAttribute("href"));
fd61fd6e 277
6475fc7e 278 if ($r->init()) {
b90c4468 279
6475fc7e 280 $tmpxpath = new DOMXPath($r->dom);
b90c4468 281
6475fc7e 282 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
6322fc68 283
6475fc7e
AD
284 foreach ($entries as $entry) {
285 if ($entry->hasAttribute("href")) {
286 $entry->setAttribute("href",
287 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
288
289 }
b90c4468 290
6475fc7e
AD
291 if ($entry->hasAttribute("src")) {
292 $entry->setAttribute("src",
293 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
cc85704f 294
6475fc7e 295 }
6322fc68 296
6475fc7e
AD
297 }
298
299 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
300
47888b3d
AD
301 // prob not a very good idea (breaks wikipedia pages, etc) -
302 // inliner currently is not really fit for any random web content
6475fc7e 303
47888b3d
AD
304 //$doc = new DOMDocument();
305 //@$doc->loadHTML($article["content"]);
306 //$xpath = new DOMXPath($doc);
307 //$found = $this->inline_stuff($article, $doc, $xpath);
6475fc7e 308 }
6322fc68 309 }
a16d7a5d 310 }
cc85704f 311 }
6322fc68
AD
312
313 }
314
315 $node = $doc->getElementsByTagName('body')->item(0);
316
317 if ($node && $found) {
318 $article["content"] = $doc->saveXML($node);
319 }
cc85704f
AD
320 }
321
322 return $article;
323 }
106a3de9
AD
324
325 function api_version() {
326 return 2;
327 }
328
5d429910
AD
329 private function handle_as_video($doc, $entry, $source_stream) {
330
331 $video = $doc->createElement('video');
332 $video->setAttribute("autoplay", "1");
5dcc7bf1 333 $video->setAttribute("controls", "1");
5d429910
AD
334 $video->setAttribute("loop", "1");
335
336 $source = $doc->createElement('source');
337 $source->setAttribute("src", $source_stream);
338 $source->setAttribute("type", "video/mp4");
339
340 $video->appendChild($source);
341
342 $br = $doc->createElement('br');
343 $entry->parentNode->insertBefore($video, $entry);
344 $entry->parentNode->insertBefore($br, $entry);
345
346 $img = $doc->createElement('img');
347 $img->setAttribute("src",
348 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
349
350 $entry->parentNode->insertBefore($img, $entry);
351 }
cc85704f
AD
352}
353?>