]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
7414656d51b7cbfdea2b310cb1a4302baf622e66
2 class Af_RedditImgur
extends Plugin
{
7 "Inline images (and other content) in Reddit RSS feeds",
12 return array("needs_curl" => true);
15 function init($host) {
18 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
19 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
22 function hook_prefs_tab($args) {
23 if ($args != "prefFeeds") return;
25 print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">";
27 $enable_readability = $this->host
->get($this, "enable_readability");
28 $enable_readability_checked = $enable_readability ?
"checked" : "";
30 $enable_content_dupcheck = $this->host
->get($this, "enable_content_dupcheck");
31 $enable_content_dupcheck_checked = $enable_content_dupcheck ?
"checked" : "";
33 print "<form dojoType=\"dijit.form.Form\">";
35 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
37 if (this.validate()) {
38 console.log(dojo.objectToQuery(this.getValues()));
39 new Ajax.Request('backend.php', {
40 parameters: dojo.objectToQuery(this.getValues()),
41 onComplete: function(transport) {
42 notify_info(transport.responseText);
49 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
50 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
51 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">";
53 print "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
56 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
57 $enable_readability_checked name=\"enable_readability\"> ";
59 print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
63 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_content_dupcheck\"
64 $enable_content_dupcheck_checked name=\"enable_content_dupcheck\"> ";
66 print "<label for=\"enable_content_dupcheck\">" . __("Enable additional duplicate checking") . "</label>";
67 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
68 __("Save")."</button>";
76 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
77 $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]) == "true";
79 $this->host
->set($this, "enable_readability", $enable_readability, false);
80 $this->host
->set($this, "enable_content_dupcheck", $enable_content_dupcheck);
82 echo __("Configuration saved");
85 private function inline_stuff($article, &$doc, $xpath, $debug = false) {
87 $entries = $xpath->query('(//a[@href]|//img[@src])');
91 foreach ($entries as $entry) {
92 if ($entry->hasAttribute("href")) {
94 _debug("processing href: " . $entry->getAttribute("href"), $debug);
98 if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
99 $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
102 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
104 _debug("Handling as Gfycat", $debug);
106 $tmp = fetch_file_contents($entry->getAttribute("href"));
109 $tmpdoc = new DOMDocument();
111 if (@$tmpdoc->loadHTML($tmp)) {
112 $tmpxpath = new DOMXPath($tmpdoc);
114 $source_meta = $tmpxpath->query("//meta[@name='twitter:player:stream' and contains(@content, '.mp4')]")->item(0);
115 $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
118 $source_stream = $source_meta->getAttribute("content");
121 if ($source_stream) {
124 $poster_url = $poster_meta->getAttribute("content");
126 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
135 // imgur .gif -> .gifv
136 if (preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
137 _debug("Handling as imgur gif (->gifv)", $debug);
139 $entry->setAttribute("href",
140 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
143 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
144 _debug("Handling as imgur gifv", $debug);
146 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
148 if (strpos($source_stream, "i.imgur.com") !== FALSE)
149 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
151 $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug);
157 if (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
158 preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
159 preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
160 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
162 $vid_id = $matches[1];
164 _debug("Handling as youtube: $vid_id", $debug);
166 $iframe = $doc->createElement("iframe");
167 $iframe->setAttribute("class", "youtube-player");
168 $iframe->setAttribute("type", "text/html");
169 $iframe->setAttribute("width", "640");
170 $iframe->setAttribute("height", "385");
171 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
172 $iframe->setAttribute("allowfullscreen", "1");
173 $iframe->setAttribute("frameborder", "0");
175 $br = $doc->createElement('br');
176 $entry->parentNode
->insertBefore($iframe, $entry);
177 $entry->parentNode
->insertBefore($br, $entry);
182 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) ||
183 mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== FALSE ||
184 mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== FALSE) {
186 _debug("Handling as a picture", $debug);
188 $img = $doc->createElement('img');
189 $img->setAttribute("src", $entry->getAttribute("href"));
191 $br = $doc->createElement('br');
192 $entry->parentNode
->insertBefore($img, $entry);
193 $entry->parentNode
->insertBefore($br, $entry);
198 // linked albums & pages
200 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
201 preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
203 _debug("Handling as an imgur page/album/gallery", $debug);
205 $album_content = fetch_file_contents($entry->getAttribute("href"),
206 false, false, false, false, 10);
208 if ($album_content) {
209 $adoc = new DOMDocument();
211 if (@$adoc->loadHTML($album_content)) {
212 $axpath = new DOMXPath($adoc);
214 /*$aentries = $axpath->query("//meta[@property='og:image']");
217 foreach ($aentries as $aentry) {
219 _debug("og:image content=" . $aentry->getAttribute("content"), $debug);
221 $url = str_replace("?fb", "", $aentry->getAttribute("content"));
222 $check_url = basename($url);
223 $check_url = mb_substr($check_url, 0, strrpos($check_url, "."));
225 if (!in_array($check_url, $urls)) {
226 $img = $doc->createElement('img');
227 $img->setAttribute("src", $url);
228 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
230 $br = $doc->createElement('br');
232 $entry->parentNode->insertBefore($img, $entry);
233 $entry->parentNode->insertBefore($br, $entry);
235 array_push($urls, $check_url);
241 //if ($debug) print_r($album_content);
243 $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src] | //div[@class='video-elements']/source)");
246 foreach ($aentries as $aentry) {
248 $url = $aentry->getAttribute("src");
250 if (!in_array($url, $urls)) {
252 if ($aentry->tagName
== "img") {
254 $img = $doc->createElement('img');
255 $img->setAttribute("src", $url);
256 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
258 $br = $doc->createElement('br');
260 $entry->parentNode
->insertBefore($img, $entry);
261 $entry->parentNode
->insertBefore($br, $entry);
262 } else if ($aentry->tagName
== "source") {
264 if (strpos($url, "i.imgur.com") !== FALSE)
265 $poster_url = str_replace(".mp4", "h.jpg", $url);
269 $this->handle_as_video($doc, $entry, $url, $poster_url);
273 array_push($urls, $url);
280 if ($debug) print_r($urls);
286 if (preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
287 $img_id = $matches[1];
289 _debug("handling as gyazo: $img_id", $debug);
291 $img = $doc->createElement('img');
292 $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
294 $br = $doc->createElement('br');
295 $entry->parentNode
->insertBefore($img, $entry);
296 $entry->parentNode
->insertBefore($br, $entry);
302 // remove tiny thumbnails
303 if ($entry->hasAttribute("src")) {
304 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
305 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
313 function hook_article_filter($article) {
315 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
316 $doc = new DOMDocument();
317 @$doc->loadHTML($article["content"]);
318 $xpath = new DOMXPath($doc);
320 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
322 if ($this->host
->get($this, "enable_content_dupcheck")) {
325 $content_href = db_escape_string($content_link->getAttribute("href"));
326 $entry_guid = db_escape_string($article["guid_hashed"]);
327 $owner_uid = $article["owner_uid"];
329 if (DB_TYPE
== "pgsql") {
330 $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'";
332 $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)";
335 $result = db_query("SELECT COUNT(id) AS cid
336 FROM ttrss_entries, ttrss_user_entries WHERE
339 guid != '$entry_guid' AND
340 owner_uid = '$owner_uid' AND
341 content LIKE '%href=\"$content_href\">[link]%'");
344 $num_found = db_fetch_result($result, 0, "cid");
346 if ($num_found > 0) $article["force_catchup"] = true;
351 $found = $this->inline_stuff($article, $doc, $xpath);
353 if (!defined('NO_CURL') && function_exists("curl_init") && !$found && $this->host
->get($this, "enable_readability") &&
354 mb_strlen(strip_tags($article["content"])) <= 150) {
356 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
359 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
360 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
361 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
363 /* link may lead to a huge video file or whatever, we need to check content type before trying to
364 parse it which p much requires curl */
366 $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
368 $content_type = $this->get_content_type($content_link->getAttribute("href"), $useragent_compat);
370 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
372 $tmp = fetch_file_contents(array("url" => $content_link->getAttribute("href"),
373 "useragent" => $useragent_compat));
375 //_debug("tmplen: " . mb_strlen($tmp));
377 if ($tmp && mb_strlen($tmp) < 1024 * 250) {
379 $r = new Readability($tmp, $content_link->getAttribute("href"));
383 $tmpxpath = new DOMXPath($r->dom
);
385 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
387 foreach ($entries as $entry) {
388 if ($entry->hasAttribute("href")) {
389 $entry->setAttribute("href",
390 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
394 if ($entry->hasAttribute("src")) {
395 $entry->setAttribute("src",
396 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
402 $article["content"] = $r->articleContent
->innerHTML
. "<hr/>" . $article["content"];
404 // prob not a very good idea (breaks wikipedia pages, etc) -
405 // inliner currently is not really fit for any random web content
407 //$doc = new DOMDocument();
408 //@$doc->loadHTML($article["content"]);
409 //$xpath = new DOMXPath($doc);
410 //$found = $this->inline_stuff($article, $doc, $xpath);
418 $node = $doc->getElementsByTagName('body')->item(0);
420 if ($node && $found) {
421 $article["content"] = $doc->saveXML($node);
428 function api_version() {
432 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) {
434 _debug("handle_as_video: $source_stream", $debug);
436 $video = $doc->createElement('video');
437 $video->setAttribute("autoplay", "1");
438 $video->setAttribute("controls", "1");
439 $video->setAttribute("loop", "1");
441 if ($poster_url) $video->setAttribute("poster", $poster_url);
443 $source = $doc->createElement('source');
444 $source->setAttribute("src", $source_stream);
445 $source->setAttribute("type", "video/mp4");
447 $video->appendChild($source);
449 $br = $doc->createElement('br');
450 $entry->parentNode
->insertBefore($video, $entry);
451 $entry->parentNode
->insertBefore($br, $entry);
453 $img = $doc->createElement('img');
454 $img->setAttribute("src",
455 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
457 $entry->parentNode
->insertBefore($img, $entry);
461 $url = htmlspecialchars($_REQUEST["url"]);
463 header("Content-type: text/plain");
467 $doc = new DOMDocument();
468 @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
469 $xpath = new DOMXPath($doc);
471 print "Inline result: " . $this->inline_stuff([], $doc, $xpath, true) . "\n";
473 print "\nResulting HTML:\n";
475 print $doc->saveHTML();
479 private function get_content_type($url, $useragent = SELF_USER_AGENT
) {
480 $content_type = false;
482 if (function_exists("curl_init") && !defined("NO_CURL")) {
483 $ch = curl_init($url);
484 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
485 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
486 curl_setopt($ch, CURLOPT_HEADER
, true);
487 curl_setopt($ch, CURLOPT_NOBODY
, true);
488 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, !ini_get("open_basedir"));
489 curl_setopt($ch, CURLOPT_USERAGENT
, $useragent);
491 @$result = curl_exec($ch);
492 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
495 return $content_type;