]> git.wh0rd.org - tt-rss.git/blobdiff - classes/pluginhost.php
pngcrush.sh
[tt-rss.git] / classes / pluginhost.php
old mode 100644 (file)
new mode 100755 (executable)
index 19f8b56..7e3fb08
@@ -1,6 +1,6 @@
 <?php
 class PluginHost {
-       private $dbh;
+       private $pdo;
        private $hooks = array();
        private $plugins = array();
        private $handlers = array();
@@ -8,20 +8,23 @@ class PluginHost {
        private $storage = array();
        private $feeds = array();
        private $api_methods = array();
+       private $plugin_actions = array();
        private $owner_uid;
-       private $debug;
        private $last_registered;
        private static $instance;
 
        const API_VERSION = 2;
 
+       // Hooks marked with *1 are run in global context and available
+       // to plugins loaded in config.php only
+
        const HOOK_ARTICLE_BUTTON = 1;
        const HOOK_ARTICLE_FILTER = 2;
        const HOOK_PREFS_TAB = 3;
        const HOOK_PREFS_TAB_SECTION = 4;
        const HOOK_PREFS_TABS = 5;
        const HOOK_FEED_PARSED = 6;
-       const HOOK_UPDATE_TASK = 7;
+       const HOOK_UPDATE_TASK = 7; // *1
        const HOOK_AUTH_USER = 8;
        const HOOK_HOTKEY_MAP = 9;
        const HOOK_RENDER_ARTICLE = 10;
@@ -38,14 +41,29 @@ class PluginHost {
        const HOOK_PREFS_SAVE_FEED = 21;
        const HOOK_FETCH_FEED = 22;
        const HOOK_QUERY_HEADLINES = 23;
-       const HOOK_HOUSE_KEEPING = 24;
+       const HOOK_HOUSE_KEEPING = 24; // *1
+       const HOOK_SEARCH = 25;
+       const HOOK_FORMAT_ENCLOSURES = 26;
+       const HOOK_SUBSCRIBE_FEED = 27;
+       const HOOK_HEADLINES_BEFORE = 28;
+       const HOOK_RENDER_ENCLOSURE = 29;
+       const HOOK_ARTICLE_FILTER_ACTION = 30;
+       const HOOK_ARTICLE_EXPORT_FEED = 31;
+       const HOOK_MAIN_TOOLBAR_BUTTON = 32;
+       const HOOK_ENCLOSURE_ENTRY = 33;
+       const HOOK_FORMAT_ARTICLE = 34;
+       const HOOK_FORMAT_ARTICLE_CDM = 35;
+       const HOOK_FEED_BASIC_INFO = 36;
+       const HOOK_SEND_LOCAL_FILE = 37;
+       const HOOK_UNSUBSCRIBE_FEED = 38;
+       const HOOK_SEND_MAIL = 39;
 
        const KIND_ALL = 1;
        const KIND_SYSTEM = 2;
        const KIND_USER = 3;
 
        function __construct() {
-               $this->dbh = Db::get();
+               $this->pdo = Db::pdo();
 
                $this->storage = array();
        }
@@ -72,7 +90,11 @@ class PluginHost {
        }
 
        function get_dbh() {
-               return $this->dbh;
+               return Db::get();
+       }
+
+       function get_pdo() {
+               return $this->pdo;
        }
 
        function get_plugin_names() {
@@ -90,7 +112,7 @@ class PluginHost {
        }
 
        function get_plugin($name) {
-               return $this->plugins[$name];
+               return $this->plugins[strtolower($name)];
        }
 
        function run_hooks($type, $method, $args) {
@@ -123,12 +145,18 @@ class PluginHost {
                        return array();
                }
        }
-       function load_all($kind, $owner_uid = false) {
-               $plugins = array_map("basename", glob("plugins/*"));
-               $this->load(join(",", $plugins), $kind, $owner_uid);
+       function load_all($kind, $owner_uid = false, $skip_init = false) {
+
+               $plugins = array_merge(glob("plugins/*"), glob("plugins.local/*"));
+               $plugins = array_filter($plugins, "is_dir");
+               $plugins = array_map("basename", $plugins);
+
+               asort($plugins);
+
+               $this->load(join(",", $plugins), $kind, $owner_uid, $skip_init);
        }
 
-       function load($classlist, $kind, $owner_uid = false) {
+       function load($classlist, $kind, $owner_uid = false, $skip_init = false) {
                $plugins = explode(",", $classlist);
 
                $this->owner_uid = (int) $owner_uid;
@@ -137,14 +165,42 @@ class PluginHost {
                        $class = trim($class);
                        $class_file = strtolower(basename($class));
 
-                       if (!is_dir(dirname(__FILE__)."/../plugins/$class_file")) continue;
+                       if (!is_dir(__DIR__."/../plugins/$class_file") &&
+                                       !is_dir(__DIR__."/../plugins.local/$class_file")) continue;
 
-                       $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
+                       // try system plugin directory first
+                       $file = __DIR__ . "/../plugins/$class_file/init.php";
+                       $vendor_dir = __DIR__ . "/../plugins/$class_file/vendor";
+
+                       if (!file_exists($file)) {
+                               $file = __DIR__ . "/../plugins.local/$class_file/init.php";
+                               $vendor_dir = __DIR__ . "/../plugins.local/$class_file/vendor";
+                       }
 
                        if (!isset($this->plugins[$class])) {
                                if (file_exists($file)) require_once $file;
 
                                if (class_exists($class) && is_subclass_of($class, "Plugin")) {
+
+                                       // register plugin autoloader if necessary, for namespaced classes ONLY
+                                       // layout corresponds to tt-rss main /vendor/author/Package/Class.php
+
+                                       if (file_exists($vendor_dir)) {
+                                               spl_autoload_register(function($class) use ($vendor_dir) {
+
+                                                       if (strpos($class, '\\') !== FALSE) {
+                                                               list ($namespace, $class_name) = explode('\\', $class, 2);
+
+                                                               if ($namespace && $class_name) {
+                                                                       $class_file = "$vendor_dir/$namespace/" . str_replace('\\', '/', $class_name) . ".php";
+
+                                                                       if (file_exists($class_file))
+                                                                               require_once $class_file;
+                                                               }
+                                                       }
+                                               });
+                                       }
+
                                        $plugin = new $class($this);
 
                                        $plugin_api = $plugin->api_version();
@@ -159,18 +215,18 @@ class PluginHost {
                                        switch ($kind) {
                                        case $this::KIND_SYSTEM:
                                                if ($this->is_system($plugin)) {
-                                                       $plugin->init($this);
+                                                       if (!$skip_init) $plugin->init($this);
                                                        $this->register_plugin($class, $plugin);
                                                }
                                                break;
                                        case $this::KIND_USER:
                                                if (!$this->is_system($plugin)) {
-                                                       $plugin->init($this);
+                                                       if (!$skip_init) $plugin->init($this);
                                                        $this->register_plugin($class, $plugin);
                                                }
                                                break;
                                        case $this::KIND_ALL:
-                                               $plugin->init($this);
+                                               if (!$skip_init) $plugin->init($this);
                                                $this->register_plugin($class, $plugin);
                                                break;
                                        }
@@ -246,8 +302,6 @@ class PluginHost {
                } else {
                        return false;
                }
-
-               return false;
        }
 
        function get_commands() {
@@ -263,12 +317,13 @@ class PluginHost {
                }
        }
 
-       function load_data($force = false) {
+       function load_data() {
                if ($this->owner_uid)  {
-                       $result = $this->dbh->query("SELECT name, content FROM ttrss_plugin_storage
-                               WHERE owner_uid = '".$this->owner_uid."'");
+                       $sth = $this->pdo->prepare("SELECT name, content FROM ttrss_plugin_storage
+                               WHERE owner_uid = ?");
+                       $sth->execute([$this->owner_uid]);
 
-                       while ($line = $this->dbh->fetch_assoc($result)) {
+                       while ($line = $sth->fetch()) {
                                $this->storage[$line["name"]] = unserialize($line["content"]);
                        }
                }
@@ -276,30 +331,30 @@ class PluginHost {
 
        private function save_data($plugin) {
                if ($this->owner_uid) {
-                       $plugin = $this->dbh->escape_string($plugin);
+                       $this->pdo->beginTransaction();
 
-                       $this->dbh->query("BEGIN");
-
-                       $result = $this->dbh->query("SELECT id FROM ttrss_plugin_storage WHERE
-                               owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
+                       $sth = $this->pdo->prepare("SELECT id FROM ttrss_plugin_storage WHERE
+                               owner_uid= ? AND name = ?");
+                       $sth->execute([$this->owner_uid, $plugin]);
 
                        if (!isset($this->storage[$plugin]))
                                $this->storage[$plugin] = array();
 
-                       $content = $this->dbh->escape_string(serialize($this->storage[$plugin]),
-                               false);
+                       $content = serialize($this->storage[$plugin]);
 
-                       if ($this->dbh->num_rows($result) != 0) {
-                               $this->dbh->query("UPDATE ttrss_plugin_storage SET content = '$content'
-                                       WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
+                       if ($sth->fetch()) {
+                               $sth = $this->pdo->prepare("UPDATE ttrss_plugin_storage SET content = ?
+                                       WHERE owner_uid= ? AND name = ?");
+                               $sth->execute([(string)$content, $this->owner_uid, $plugin]);
 
                        } else {
-                               $this->dbh->query("INSERT INTO ttrss_plugin_storage
+                               $sth = $this->pdo->prepare("INSERT INTO ttrss_plugin_storage
                                        (name,owner_uid,content) VALUES
-                                       ('$plugin','".$this->owner_uid."','$content')");
+                                       (?, ?, ?)");
+                               $sth->execute([$plugin, $this->owner_uid, (string)$content]);
                        }
 
-                       $this->dbh->query("COMMIT");
+                       $this->pdo->commit();
                }
        }
 
@@ -327,7 +382,9 @@ class PluginHost {
        function get_all($sender) {
                $idx = get_class($sender);
 
-               return $this->storage[$idx];
+               $data = $this->storage[$idx];
+
+               return $data ? $data : [];
        }
 
        function clear_data($sender) {
@@ -336,19 +393,12 @@ class PluginHost {
 
                        unset($this->storage[$idx]);
 
-                       $this->dbh->query("DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
-                               AND owner_uid = " . $this->owner_uid);
+                       $sth = $this->pdo->prepare("DELETE FROM ttrss_plugin_storage WHERE name = ?
+                               AND owner_uid = ?");
+                       $sth->execute([$idx, $this->owner_uid]);
                }
        }
 
-       function set_debug($debug) {
-               $this->debug = $debug;
-       }
-
-       function get_debug() {
-               return $this->debug;
-       }
-
        // Plugin feed functions are *EXPERIMENTAL*!
 
        // cat_id: only -1 is supported (Special)
@@ -395,5 +445,18 @@ class PluginHost {
        function get_api_method($name) {
                return $this->api_methods[$name];
        }
+
+       function add_filter_action($sender, $action_name, $action_desc) {
+               $sender_class = get_class($sender);
+
+               if (!isset($this->plugin_actions[$sender_class]))
+                       $this->plugin_actions[$sender_class] = array();
+
+               array_push($this->plugin_actions[$sender_class],
+                       array("action" => $action_name, "description" => $action_desc, "sender" => $sender));
+       }
+
+       function get_filter_actions() {
+               return $this->plugin_actions;
+       }
 }
-?>