]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/af_readability/init.php
Merge branch 'master' of git.tt-rss.org:git/tt-rss into pdo-experimental
[tt-rss.git] / plugins / af_readability / init.php
index 97acb4375391a251dd98f549a3f0a5c2fe3cc5df..04673000a19240f56512f1e15050df6c62bd2d81 100755 (executable)
@@ -1,6 +1,7 @@
 <?php
 class Af_Readability extends Plugin {
 
+       /* @var PluginHost $host */
        private $host;
 
        function about() {
@@ -9,8 +10,16 @@ class Af_Readability extends Plugin {
                        "fox");
        }
 
+       function flags() {
+               return array("needs_curl" => true);
+       }
+
        function save() {
-               //
+               $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
+
+               $this->host->set($this, "enable_share_anything", $enable_share_anything);
+
+               echo __("Data saved.");
        }
 
        function init($host)
@@ -28,16 +37,42 @@ class Af_Readability extends Plugin {
        function hook_prefs_tab($args) {
                if ($args != "prefFeeds") return;
 
-               print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_readability settings')."\">";
+               print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
 
                print_notice("Enable the plugin for specific feeds in the feed editor.");
 
+               print "<form dojoType=\"dijit.form.Form\">";
+
+               print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
+                       evt.preventDefault();
+                       if (this.validate()) {
+                               console.log(dojo.objectToQuery(this.getValues()));
+                               new Ajax.Request('backend.php', {
+                                       parameters: dojo.objectToQuery(this.getValues()),
+                                       onComplete: function(transport) {
+                                               notify_info(transport.responseText);
+                                       }
+                               });
+                               //this.reset();
+                       }
+                       </script>";
+
+               print_hidden("op", "pluginhandler");
+               print_hidden("method", "save");
+               print_hidden("plugin", "af_readability");
+
+               $enable_share_anything = $this->host->get($this, "enable_share_anything");
+
+               print_checkbox("enable_share_anything", $enable_share_anything);
+               print "&nbsp;<label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
+
+               print "<p>"; print_button("submit", __("Save"));
+               print "</form>";
+
                $enabled_feeds = $this->host->get($this, "enabled_feeds");
-               if (!array($enabled_feeds)) {
-                       $enabled_feeds = array();
-               } else {
-                       $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
-               }
+               if (!is_array($enabled_feeds)) $enabled_feeds = array();
+
+               $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
                $this->host->set($this, "enabled_feeds", $enabled_feeds);
 
                if (count($enabled_feeds) > 0) {
@@ -49,7 +84,7 @@ class Af_Readability extends Plugin {
                                        "<img src='images/pub_set.png'
                                                style='vertical-align : middle'> <a href='#'
                                                onclick='editFeed($f)'>".
-                                       getFeedTitle($f) . "</a></li>";
+                                       Feeds::getFeedTitle($f) . "</a></li>";
                        }
                        print "</ul>";
                }
@@ -62,7 +97,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" : "";
@@ -78,7 +113,7 @@ class Af_Readability extends Plugin {
                $enabled_feeds = $this->host->get($this, "enabled_feeds");
                if (!is_array($enabled_feeds)) $enabled_feeds = array();
 
-               $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
+               $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
                $key = array_search($feed_id, $enabled_feeds);
 
                if ($enable) {
@@ -94,17 +129,19 @@ class Af_Readability extends Plugin {
                $this->host->set($this, "enabled_feeds", $enabled_feeds);
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function hook_article_filter_action($article, $action) {
                return $this->process_article($article);
        }
 
-       function process_article($article) {
-
+       public function extract_content($url) {
                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"]);
+                       $ch = curl_init($url);
 
                        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@@ -113,20 +150,25 @@ class Af_Readability extends Plugin {
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                        curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
 
-                       @$result = curl_exec($ch);
+                       @curl_exec($ch);
                        $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
 
                        if (strpos($content_type, "text/html") === FALSE)
-                               return $article;
+                               return false;
+
+                       $effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
                }
 
-               $tmp = fetch_file_contents($article["link"]);
+               $tmp = fetch_file_contents($url);
 
-               if ($tmp && mb_strlen($tmp) < 65535 * 4) {
+               if ($tmp && mb_strlen($tmp) < 1024 * 500) {
                        $tmpdoc = new DOMDocument("1.0", "UTF-8");
 
-                       if (!$tmpdoc->loadHTML($tmp))
-                               return $article;
+                       if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
+                               return false;
+
+                       if (!isset($effective_url))
+                               $effective_url = $url;
 
                        if (strtolower($tmpdoc->encoding) != 'utf-8') {
                                $tmpxpath = new DOMXPath($tmpdoc);
@@ -138,10 +180,9 @@ class Af_Readability extends Plugin {
                                $tmp = $tmpdoc->saveHTML();
                        }
 
-                       $r = new Readability($tmp, $article["link"]);
+                       $r = new Readability($tmp, $url);
 
                        if ($r->init()) {
-
                                $tmpxpath = new DOMXPath($r->dom);
 
                                $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
@@ -149,28 +190,41 @@ class Af_Readability extends Plugin {
                                foreach ($entries as $entry) {
                                        if ($entry->hasAttribute("href")) {
                                                $entry->setAttribute("href",
-                                                       rewrite_relative_url($article["link"], $entry->getAttribute("href")));
+                                                               rewrite_relative_url($effective_url, $entry->getAttribute("href")));
 
                                        }
 
                                        if ($entry->hasAttribute("src")) {
                                                $entry->setAttribute("src",
-                                                       rewrite_relative_url($article["link"], $entry->getAttribute("src")));
+                                                               rewrite_relative_url($effective_url, $entry->getAttribute("src")));
 
                                        }
 
                                }
 
-                               $article["content"] = $r->articleContent->innerHTML;
+                               return $r->articleContent->innerHTML;
                        }
                }
 
+               return false;
+       }
+
+       function process_article($article) {
+
+               $extracted_content = $this->extract_content($article["link"]);
+
+               if ($extracted_content) {
+                       $article["content"] = $extracted_content;
+               }
+
                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;
 
@@ -185,14 +239,13 @@ class Af_Readability extends Plugin {
        private function filter_unknown_feeds($enabled_feeds) {
                $tmp = array();
 
-               if (!empty($enabled_feeds)) {
-                       foreach ($enabled_feeds as $feed) {
+               foreach ($enabled_feeds as $feed) {
 
-                               $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
+                       $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
+                       $sth->execute([$feed, $_SESSION['uid']]);
 
-                               if (db_num_rows($result) != 0) {
-                                       array_push($tmp, $feed);
-                               }
+                       if ($row = $sth->fetch()) {
+                               array_push($tmp, $feed);
                        }
                }
 
@@ -200,4 +253,3 @@ class Af_Readability extends Plugin {
        }
 
 }
-?>