]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/af_redditimgur/init.php
basic tweet embedding using oembed
[tt-rss.git] / plugins / af_redditimgur / init.php
index 8243b94c43db1b70bdd9b78727b9a33ddc248268..e283b8cec409b3cabda4a3ab1e845b6ac972f86e 100755 (executable)
@@ -29,7 +29,7 @@ class Af_RedditImgur extends Plugin {
 
                $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
                $enable_content_dupcheck_checked = $enable_content_dupcheck ? "checked" : "";
-               
+
                print "<form dojoType=\"dijit.form.Form\">";
 
                print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
@@ -59,7 +59,7 @@ class Af_RedditImgur extends Plugin {
                print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
 
                print "<br/>";
-               
+
                print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_content_dupcheck\"
                        $enable_content_dupcheck_checked name=\"enable_content_dupcheck\">&nbsp;";
 
@@ -95,6 +95,34 @@ class Af_RedditImgur extends Plugin {
 
                                $matches = array();
 
+
+                               if (preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) {
+                                       _debug("handling as twitter: " . $matches[1] . " " . $matches[2], $debug);
+
+                                       $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=" . urlencode($entry->getAttribute("href")));
+
+                                       if ($oembed_result) {
+                                               $oembed_result = json_decode($oembed_result, true);
+
+                                               if ($oembed_result && isset($oembed_result["html"])) {
+
+                                                       $tmp = new DOMDocument();
+                                                       if ($tmp->loadHTML($oembed_result["html"])) {
+                                                               $p = $doc->createElement("p");
+
+                                                               $p->appendChild($doc->importNode(
+                                                                       $tmp->getElementsByTagName("blockquote")->item(0), TRUE));
+
+                                                               $br = $doc->createElement('br');
+                                                               $entry->parentNode->insertBefore($p, $entry);
+                                                               $entry->parentNode->insertBefore($br, $entry);
+
+                                                               $found = 1;
+                                                       }
+                                               }
+                                       }
+                               }
+
                                if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
                                        $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
                                }
@@ -179,7 +207,10 @@ class Af_RedditImgur extends Plugin {
                                        $found = true;
                                }
 
-                               if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
+                               if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) ||
+                                       mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== FALSE ||
+                                       mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== FALSE) {
+
                                        _debug("Handling as a picture", $debug);
 
                                        $img = $doc->createElement('img');
@@ -237,7 +268,7 @@ class Af_RedditImgur extends Plugin {
 
                                                        //if ($debug) print_r($album_content);
 
-                                                       $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src])");
+                                                       $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src] | //div[@class='video-elements']/source)");
                                                        $urls = [];
 
                                                        foreach ($aentries as $aentry) {
@@ -245,14 +276,27 @@ class Af_RedditImgur extends Plugin {
                                                                $url = $aentry->getAttribute("src");
 
                                                                if (!in_array($url, $urls)) {
-                                                                       $img = $doc->createElement('img');
-                                                                       $img->setAttribute("src", $url);
-                                                                       $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
 
-                                                                       $br = $doc->createElement('br');
+                                                                       if ($aentry->tagName == "img") {
 
-                                                                       $entry->parentNode->insertBefore($img, $entry);
-                                                                       $entry->parentNode->insertBefore($br, $entry);
+                                                                               $img = $doc->createElement('img');
+                                                                               $img->setAttribute("src", $url);
+                                                                               $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
+
+                                                                               $br = $doc->createElement('br');
+
+                                                                               $entry->parentNode->insertBefore($img, $entry);
+                                                                               $entry->parentNode->insertBefore($br, $entry);
+                                                                       } else if ($aentry->tagName == "source") {
+
+                                                                               if (strpos($url, "i.imgur.com") !== FALSE)
+                                                                                       $poster_url = str_replace(".mp4", "h.jpg", $url);
+                                                                               else
+                                                                                       $poster_url = "";
+
+                                                                               $this->handle_as_video($doc, $entry, $url, $poster_url);
+
+                                                                       }
 
                                                                        array_push($urls, $url);
 
@@ -347,24 +391,18 @@ class Af_RedditImgur extends Plugin {
                                        /* link may lead to a huge video file or whatever, we need to check content type before trying to
                                        parse it which p much requires curl */
 
-                                       $ch = curl_init($content_link->getAttribute("href"));
-                                       curl_setopt($ch, CURLOPT_TIMEOUT, 5);
-                                       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-                                       curl_setopt($ch, CURLOPT_HEADER, true);
-                                       curl_setopt($ch, CURLOPT_NOBODY, true);
-                                       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
-                                       curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
+                                       $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
 
-                                       @$result = curl_exec($ch);
-                                       $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
+                                       $content_type = $this->get_content_type($content_link->getAttribute("href"), $useragent_compat);
 
                                        if ($content_type && strpos($content_type, "text/html") !== FALSE) {
 
-                                               $tmp = fetch_file_contents($content_link->getAttribute("href"));
+                                               $tmp = fetch_file_contents(array("url" => $content_link->getAttribute("href"),
+                                                       "useragent" => $useragent_compat));
 
                                                //_debug("tmplen: " . mb_strlen($tmp));
 
-                                               if ($tmp && mb_strlen($tmp) < 65535 * 4) {
+                                               if ($tmp && mb_strlen($tmp) < 1024 * 250) {
 
                                                        $r = new Readability($tmp, $content_link->getAttribute("href"));
 
@@ -465,5 +503,24 @@ class Af_RedditImgur extends Plugin {
                print $doc->saveHTML();
 
        }
+
+       private function get_content_type($url, $useragent = SELF_USER_AGENT) {
+               $content_type = false;
+
+               if (function_exists("curl_init") && !defined("NO_CURL")) {
+                       $ch = curl_init($url);
+                       curl_setopt($ch, CURLOPT_TIMEOUT, 5);
+                       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+                       curl_setopt($ch, CURLOPT_HEADER, true);
+                       curl_setopt($ch, CURLOPT_NOBODY, true);
+                       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
+                       curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
+
+                       @$result = curl_exec($ch);
+                       $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
+               }
+
+               return $content_type;
+       }
 }
 ?>