]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
af_redditimgur: fix imgur single-image pages
[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 // linked albums & pages
163
164 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
165 preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
166
167 $album_content = fetch_file_contents($entry->getAttribute("href"),
168 false, false, false, false, 10);
169
170 if ($album_content) {
171 $adoc = new DOMDocument();
172 @$adoc->loadHTML($album_content);
173
174 if ($adoc) {
175 $axpath = new DOMXPath($adoc);
176 $aentries = $axpath->query("//meta[@property='og:image']");
177 $urls = array();
178
179 foreach ($aentries as $aentry) {
180
181 if (!in_array($aentry->getAttribute("content"), $urls)) {
182 $img = $doc->createElement('img');
183 $img->setAttribute("src", $aentry->getAttribute("content"));
184 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
185
186 $br = $doc->createElement('br');
187
188 $entry->parentNode->insertBefore($img, $entry);
189 $entry->parentNode->insertBefore($br, $entry);
190
191 array_push($urls, $aentry->getAttribute("content"));
192
193 $found = true;
194 }
195 }
196 }
197 }
198 }
199 }
200
201 // remove tiny thumbnails
202 if ($entry->hasAttribute("src")) {
203 if ($entry->parentNode && $entry->parentNode->parentNode) {
204 $entry->parentNode->parentNode->removeChild($entry->parentNode);
205 }
206 }
207 }
208
209 return $found;
210 }
211
212 function hook_article_filter($article) {
213
214 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
215 $doc = new DOMDocument();
216 @$doc->loadHTML($article["content"]);
217 $xpath = new DOMXPath($doc);
218
219 $found = $this->inline_stuff($article, $doc, $xpath);
220
221 if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") &&
222 mb_strlen(strip_tags($article["content"])) <= 150) {
223
224 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
225
226 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
227
228 if ($content_link &&
229 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
230 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
231 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
232
233 /* link may lead to a huge video file or whatever, we need to check content type before trying to
234 parse it which p much requires curl */
235
236 $ch = curl_init($content_link->getAttribute("href"));
237 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
238 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
239 curl_setopt($ch, CURLOPT_HEADER, true);
240 curl_setopt($ch, CURLOPT_NOBODY, true);
241 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
242 !ini_get("safe_mode") && !ini_get("open_basedir"));
243 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
244
245 @$result = curl_exec($ch);
246 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
247
248 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
249
250 $tmp = fetch_file_contents($content_link->getAttribute("href"));
251
252 if ($tmp) {
253 $r = new Readability($tmp, $content_link->getAttribute("href"));
254
255 if ($r->init()) {
256
257 $tmpxpath = new DOMXPath($r->dom);
258
259 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
260
261 foreach ($entries as $entry) {
262 if ($entry->hasAttribute("href")) {
263 $entry->setAttribute("href",
264 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
265
266 }
267
268 if ($entry->hasAttribute("src")) {
269 $entry->setAttribute("src",
270 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
271
272 }
273
274 }
275
276 $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
277
278 // prob not a very good idea (breaks wikipedia pages, etc) -
279 // inliner currently is not really fit for any random web content
280
281 //$doc = new DOMDocument();
282 //@$doc->loadHTML($article["content"]);
283 //$xpath = new DOMXPath($doc);
284 //$found = $this->inline_stuff($article, $doc, $xpath);
285 }
286 }
287 }
288 }
289
290 }
291
292 $node = $doc->getElementsByTagName('body')->item(0);
293
294 if ($node && $found) {
295 $article["content"] = $doc->saveXML($node);
296 }
297 }
298
299 return $article;
300 }
301
302 function api_version() {
303 return 2;
304 }
305
306 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
307
308 $video = $doc->createElement('video');
309 $video->setAttribute("autoplay", "1");
310 $video->setAttribute("controls", "1");
311 $video->setAttribute("loop", "1");
312
313 if ($poster_url) $video->setAttribute("poster", $poster_url);
314
315 $source = $doc->createElement('source');
316 $source->setAttribute("src", $source_stream);
317 $source->setAttribute("type", "video/mp4");
318
319 $video->appendChild($source);
320
321 $br = $doc->createElement('br');
322 $entry->parentNode->insertBefore($video, $entry);
323 $entry->parentNode->insertBefore($br, $entry);
324
325 $img = $doc->createElement('img');
326 $img->setAttribute("src",
327 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
328
329 $entry->parentNode->insertBefore($img, $entry);
330 }
331 }
332 ?>