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