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