X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=classes%2Fpluginhost.php;h=f40705dd10694a2cd53b43a1e475747ab0cfe84f;hb=57932e183745bada9c6183056597cb5276f68d10;hp=0ef17b77e31359024d394959a15e01cc7e7c8233;hpb=62a1f9899ebfc099720a75a7ff0476c86034d45d;p=tt-rss.git diff --git a/classes/pluginhost.php b/classes/pluginhost.php old mode 100644 new mode 100755 index 0ef17b77..f40705dd --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -1,13 +1,23 @@ link = $link; + function __construct() { + $this->pdo = Db::pdo(); - $this->storage = $_SESSION["plugin_storage"]; + $this->storage = array(); + } - if (!$this->storage) $this->storage = array(); + private function __clone() { + // + } + + public static function getInstance() { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance; } private function register_plugin($name, $plugin) { @@ -41,8 +85,27 @@ class PluginHost { $this->plugins[$name] = $plugin; } + // needed for compatibility with API 1 function get_link() { - return $this->link; + return false; + } + + function get_dbh() { + return Db::get(); + } + + function get_pdo() { + return $this->pdo; + } + + function get_plugin_names() { + $names = array(); + + foreach ($this->plugins as $p) { + array_push($names, get_class($p)); + } + + return $names; } function get_plugins() { @@ -50,7 +113,7 @@ class PluginHost { } function get_plugin($name) { - return $this->plugins[$name]; + return $this->plugins[strtolower($name)]; } function run_hooks($type, $method, $args) { @@ -69,7 +132,7 @@ class PluginHost { function del_hook($type, $sender) { if (is_array($this->hooks[$type])) { - $key = array_Search($this->hooks[$type], $sender); + $key = array_Search($sender, $this->hooks[$type]); if ($key !== FALSE) { unset($this->hooks[$type][$key]); } @@ -83,12 +146,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; @@ -96,29 +165,69 @@ class PluginHost { foreach ($plugins as $class) { $class = trim($class); $class_file = strtolower(basename($class)); - $file = dirname(__FILE__)."/../plugins/$class_file/init.php"; + + if (!is_dir(__DIR__."/../plugins/$class_file") && + !is_dir(__DIR__."/../plugins.local/$class_file")) continue; + + // 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(); + + if ($plugin_api < PluginHost::API_VERSION) { + user_error("Plugin $class is not compatible with current API version (need: " . PluginHost::API_VERSION . ", got: $plugin_api)", E_USER_WARNING); + continue; + } + + $this->last_registered = $class; + 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; } @@ -147,7 +256,7 @@ class PluginHost { } } - function del_handler($handler, $method) { + function del_handler($handler, $method, $sender) { $handler = str_replace("-", "_", strtolower($handler)); $method = strtolower($method); @@ -171,10 +280,12 @@ class PluginHost { return false; } - function add_command($command, $description, $sender) { + function add_command($command, $description, $sender, $suffix = "", $arghelp = "") { $command = str_replace("-", "_", strtolower($command)); $this->commands[$command] = array("description" => $description, + "suffix" => $suffix, + "arghelp" => $arghelp, "class" => $sender); } @@ -192,8 +303,6 @@ class PluginHost { } else { return false; } - - return false; } function get_commands() { @@ -209,46 +318,44 @@ class PluginHost { } } - function load_data($force = false) { - if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) { - $plugin = db_escape_string($this->link, $plugin); + function load_data() { + if ($this->owner_uid) { + $sth = $this->pdo->prepare("SELECT name, content FROM ttrss_plugin_storage + WHERE owner_uid = ?"); + $sth->execute([$this->owner_uid]); - $result = db_query($this->link, "SELECT name, content FROM ttrss_plugin_storage - WHERE owner_uid = '".$this->owner_uid."'"); - - while ($line = db_fetch_assoc($result)) { + while ($line = $sth->fetch()) { $this->storage[$line["name"]] = unserialize($line["content"]); } - - $_SESSION["plugin_storage"] = $this->storage; } } private function save_data($plugin) { if ($this->owner_uid) { - $plugin = db_escape_string($this->link, $plugin); - - db_query($this->link, "BEGIN"); + $this->pdo->beginTransaction(); - $result = db_query($this->link,"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 = db_escape_string($this->link, serialize($this->storage[$plugin])); + $content = serialize($this->storage[$plugin]); - if (db_num_rows($result) != 0) { - db_query($this->link, "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 { - db_query($this->link, "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]); } - db_query($this->link, "COMMIT"); + $this->pdo->commit(); } } @@ -260,8 +367,6 @@ class PluginHost { $this->storage[$idx][$name] = $value; - $_SESSION["plugin_storage"] = $this->storage; - if ($sync) $this->save_data(get_class($sender)); } @@ -278,7 +383,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) { @@ -287,10 +394,9 @@ class PluginHost { unset($this->storage[$idx]); - db_query($this->link, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx' - AND owner_uid = " . $this->owner_uid); - - $_SESSION["plugin_storage"] = $this->storage; + $sth = $this->pdo->prepare("DELETE FROM ttrss_plugin_storage WHERE name = ? + AND owner_uid = ?"); + $sth->execute([$idx, $this->owner_uid]); } } @@ -301,5 +407,65 @@ class PluginHost { function get_debug() { return $this->debug; } + + // Plugin feed functions are *EXPERIMENTAL*! + + // cat_id: only -1 is supported (Special) + function add_feed($cat_id, $title, $icon, $sender) { + if (!$this->feeds[$cat_id]) $this->feeds[$cat_id] = array(); + + $id = count($this->feeds[$cat_id]); + + array_push($this->feeds[$cat_id], + array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon)); + + return $id; + } + + function get_feeds($cat_id) { + return $this->feeds[$cat_id]; + } + + // convert feed_id (e.g. -129) to pfeed_id first + function get_feed_handler($pfeed_id) { + foreach ($this->feeds as $cat) { + foreach ($cat as $feed) { + if ($feed['id'] == $pfeed_id) { + return $feed['sender']; + } + } + } + } + + static function pfeed_to_feed_id($label) { + return PLUGIN_FEED_BASE_INDEX - 1 - abs($label); + } + + static function feed_to_pfeed_id($feed) { + return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed); + } + + function add_api_method($name, $sender) { + if ($this->is_system($sender)) { + $this->api_methods[strtolower($name)] = $sender; + } + } + + 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; + } } -?>