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