]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/af_readability/init.php
Fix array checking method in 2 places, add array check in one location
[tt-rss.git] / plugins / af_readability / init.php
old mode 100644 (file)
new mode 100755 (executable)
index 6cdd28f..1d7c638
@@ -21,6 +21,8 @@ class Af_Readability extends Plugin {
                $host->add_hook($host::HOOK_PREFS_TAB, $this);
                $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
                $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
+
+               $host->add_filter_action($this, "action_inline", __("Inline content"));
        }
 
        function hook_prefs_tab($args) {
@@ -31,7 +33,7 @@ class Af_Readability extends Plugin {
                print_notice("Enable the plugin for specific feeds in the feed editor.");
 
                $enabled_feeds = $this->host->get($this, "enabled_feeds");
-               if (!array($enabled_feeds)) $enabled_feeds = array();
+               if (!is_array($enabled_feeds)) $enabled_feeds = array();
 
                $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
                $this->host->set($this, "enabled_feeds", $enabled_feeds);
@@ -58,7 +60,7 @@ class Af_Readability extends Plugin {
                print "<div class=\"dlgSecCont\">";
 
                $enabled_feeds = $this->host->get($this, "enabled_feeds");
-               if (!array($enabled_feeds)) $enabled_feeds = array();
+               if (!is_array($enabled_feeds)) $enabled_feeds = array();
 
                $key = array_search($feed_id, $enabled_feeds);
                $checked = $key !== FALSE ? "checked" : "";
@@ -90,17 +92,50 @@ class Af_Readability extends Plugin {
                $this->host->set($this, "enabled_feeds", $enabled_feeds);
        }
 
-       function hook_article_filter($article) {
+       function hook_article_filter_action($article, $action) {
+               return $this->process_article($article);
+       }
 
-               $enabled_feeds = $this->host->get($this, "enabled_feeds");
-               $key = array_search($article["feed"]["id"], $enabled_feeds);
-               if ($key === FALSE) return $article;
+       function process_article($article) {
 
-               if (!class_exists("Readability")) require_once(__DIR__ . "/classes/Readability.php");
+               if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
+
+               if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
+
+                       $ch = curl_init($article["link"]);
+
+                       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, true);
+                       curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
+
+                       @$result = curl_exec($ch);
+                       $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
+
+                       if (strpos($content_type, "text/html") === FALSE)
+                               return $article;
+               }
 
                $tmp = fetch_file_contents($article["link"]);
 
-               if ($tmp) {
+               if ($tmp && mb_strlen($tmp) < 65535 * 4) {
+                       $tmpdoc = new DOMDocument("1.0", "UTF-8");
+
+                       if (!$tmpdoc->loadHTML($tmp))
+                               return $article;
+
+                       if (strtolower($tmpdoc->encoding) != 'utf-8') {
+                               $tmpxpath = new DOMXPath($tmpdoc);
+
+                               foreach ($tmpxpath->query("//meta") as $elem) {
+                                       $elem->parentNode->removeChild($elem);
+                               }
+
+                               $tmp = $tmpdoc->saveHTML();
+                       }
+
                        $r = new Readability($tmp, $article["link"]);
 
                        if ($r->init()) {
@@ -129,6 +164,17 @@ class Af_Readability extends Plugin {
                }
 
                return $article;
+       }
+
+       function hook_article_filter($article) {
+
+               $enabled_feeds = $this->host->get($this, "enabled_feeds");
+               if (!is_array($enabled_feeds)) return $article;
+
+               $key = array_search($article["feed"]["id"], $enabled_feeds);
+               if ($key === FALSE) return $article;
+
+               return $this->process_article($article);
 
        }