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