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