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