X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=classes%2Fpluginhost.php;h=7e3fb08abbcfdf021ba02541c3f159f7b6173edd;hb=HEAD;hp=f56343c5f34615794ba27dd61087d80b4a95e351;hpb=53955264441e178afd894791de734189ed13fb78;p=tt-rss.git diff --git a/classes/pluginhost.php b/classes/pluginhost.php old mode 100644 new mode 100755 index f56343c5..7e3fb08a --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -1,6 +1,6 @@ dbh = Db::get(); + $this->pdo = Db::pdo(); $this->storage = array(); } @@ -90,7 +90,11 @@ class PluginHost { } function get_dbh() { - return $this->dbh; + return Db::get(); + } + + function get_pdo() { + return $this->pdo; } function get_plugin_names() { @@ -166,15 +170,37 @@ class PluginHost { // 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(); @@ -276,8 +302,6 @@ class PluginHost { } else { return false; } - - return false; } function get_commands() { @@ -295,10 +319,11 @@ class PluginHost { 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"]); } } @@ -306,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(); } } @@ -357,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) { @@ -366,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)