]> git.wh0rd.org - tt-rss.git/blob - 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
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?:\/\/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 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
94
95 if ($source_meta) {
96 $source_stream = $source_meta->getAttribute("content");
97
98 if ($source_stream) {
99 $this->handle_as_video($doc, $entry, $source_stream);
100 $found = 1;
101 }
102 }
103 }
104 }
105
106 }
107
108 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
109
110 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
111 $this->handle_as_video($doc, $entry, $source_stream);
112
113 $found = true;
114 }
115
116 $matches = array();
117 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
118 preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
119 preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
120 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
121
122 $vid_id = $matches[1];
123
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");
132
133 $br = $doc->createElement('br');
134 $entry->parentNode->insertBefore($iframe, $entry);
135 $entry->parentNode->insertBefore($br, $entry);
136
137 $found = true;
138 }
139
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"));
143
144 $br = $doc->createElement('br');
145 $entry->parentNode->insertBefore($img, $entry);
146 $entry->parentNode->insertBefore($br, $entry);
147
148 $found = true;
149 }
150
151 // links to imgur pages
152 $matches = array();
153 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
154
155 $token = $matches[2];
156
157 $album_content = fetch_file_contents($entry->getAttribute("href"),
158 false, false, false, false, 10);
159
160 if ($album_content && $token) {
161 $adoc = new DOMDocument();
162 @$adoc->loadHTML($album_content);
163
164 if ($adoc) {
165 $axpath = new DOMXPath($adoc);
166 $aentries = $axpath->query('(//img[@src])');
167
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"));
172
173 $br = $doc->createElement('br');
174
175 $entry->parentNode->insertBefore($img, $entry);
176 $entry->parentNode->insertBefore($br, $entry);
177
178 $found = true;
179
180 break;
181 }
182 }
183 }
184 }
185 }
186
187 // linked albums, ffs
188 if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
189
190 $album_content = fetch_file_contents($entry->getAttribute("href"),
191 false, false, false, false, 10);
192
193 if ($album_content) {
194 $adoc = new DOMDocument();
195 @$adoc->loadHTML($album_content);
196
197 if ($adoc) {
198 $axpath = new DOMXPath($adoc);
199 $aentries = $axpath->query("//meta[@property='og:image']");
200 $urls = array();
201
202 foreach ($aentries as $aentry) {
203
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);
208
209 $br = $doc->createElement('br');
210
211 $entry->parentNode->insertBefore($img, $entry);
212 $entry->parentNode->insertBefore($br, $entry);
213
214 array_push($urls, $aentry->getAttribute("content"));
215
216 $found = true;
217 }
218 }
219 }
220 }
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 }
231
232 return $found;
233 }
234
235 function hook_article_filter($article) {
236
237 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
238 $doc = new DOMDocument();
239 @$doc->loadHTML($article["content"]);
240 $xpath = new DOMXPath($doc);
241
242 $found = $this->inline_stuff($article, $doc, $xpath);
243
244 if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
245 mb_strlen(strip_tags($article["content"])) <= 150) {
246
247 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
248
249 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
250
251 if ($content_link &&
252 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
253 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
254 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
255
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 */
258
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);
267
268 @$result = curl_exec($ch);
269 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
270
271 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
272
273 $tmp = fetch_file_contents($content_link->getAttribute("href"));
274
275 if ($tmp) {
276 $r = new Readability($tmp, $content_link->getAttribute("href"));
277
278 if ($r->init()) {
279
280 $tmpxpath = new DOMXPath($r->dom);
281
282 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
283
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 }
290
291 if ($entry->hasAttribute("src")) {
292 $entry->setAttribute("src",
293 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
294
295 }
296
297 }
298
299 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
300
301 // prob not a very good idea (breaks wikipedia pages, etc) -
302 // inliner currently is not really fit for any random web content
303
304 //$doc = new DOMDocument();
305 //@$doc->loadHTML($article["content"]);
306 //$xpath = new DOMXPath($doc);
307 //$found = $this->inline_stuff($article, $doc, $xpath);
308 }
309 }
310 }
311 }
312
313 }
314
315 $node = $doc->getElementsByTagName('body')->item(0);
316
317 if ($node && $found) {
318 $article["content"] = $doc->saveXML($node);
319 }
320 }
321
322 return $article;
323 }
324
325 function api_version() {
326 return 2;
327 }
328
329 private function handle_as_video($doc, $entry, $source_stream) {
330
331 $video = $doc->createElement('video');
332 $video->setAttribute("autoplay", "1");
333 $video->setAttribute("controls", "1");
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 }
352 }
353 ?>