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