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