From d8a1d2a25b2247e5a63f5b0ab7f0bd9423a217e5 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 27 Dec 2012 16:55:25 +0400 Subject: [PATCH] add experimental key/value storage for plugins --- classes/pluginhost.php | 86 ++++++++++++++++++++++++++++++++++++- include/functions.php | 3 +- plugins/example/example.php | 15 +++++-- 3 files changed, 99 insertions(+), 5 deletions(-) diff --git a/classes/pluginhost.php b/classes/pluginhost.php index d97dfa666..e43b39f9d 100644 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -1,10 +1,17 @@ link = $link; + + $this->storage = $_SESSION["plugin_storage"]; + + if (!$this->storage) $this->storage = array(); } private function register_plugin($name, $plugin) { @@ -75,9 +86,11 @@ class PluginHost { $this->load(join(",", $plugins), $kind); } - function load($classlist, $kind) { + function load($classlist, $kind, $owner_uid = false) { $plugins = explode(",", $classlist); + $this->owner_uid = (int) $owner_uid; + foreach ($plugins as $class) { $class = trim($class); $class_file = strtolower(basename($class)); @@ -194,5 +207,76 @@ class PluginHost { } } + function load_data($force = false) { + if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) { + $plugin = db_escape_string($plugin); + + $result = db_query($this->link, "SELECT name, content FROM ttrss_plugin_storage + WHERE owner_uid = '".$this->owner_uid."'"); + + while ($line = db_fetch_assoc($result)) { + $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($plugin); + + db_query($this->link, "BEGIN"); + + $result = db_query($this->link,"SELECT id FROM ttrss_plugin_storage WHERE + owner_uid= '".$this->owner_uid."' AND name = '$plugin'"); + + if (!isset($this->storage[$plugin])) + $this->storage[$plugin] = array(); + + $content = db_escape_string(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'"); + + } else { + db_query($this->link, "INSERT INTO ttrss_plugin_storage + (name,owner_uid,content) VALUES + ('$plugin','".$this->owner_uid."','$content')"); + } + + db_query($this->link, "COMMIT"); + } + } + + function set($sender, $name, $value, $sync = true) { + $idx = get_class($sender); + + if (!isset($this->storage[$idx])) + $this->storage[$idx] = array(); + + $this->storage[$idx][$name] = $value; + + $_SESSION["plugin_storage"] = $this->storage; + + if ($sync) $this->save_data(get_class($sender)); + } + + function get($sender, $name, $default_value) { + $idx = get_class($sender); + + if (isset($this->storage[$idx][$name])) { + return $this->storage[$idx][$name]; + } else { + return $default_value; + } + } + + function get_all($sender) { + $idx = get_class($sender); + + return $this->storage[$idx]; + } } ?> diff --git a/include/functions.php b/include/functions.php index f6ef7c2b3..b382b4069 100644 --- a/include/functions.php +++ b/include/functions.php @@ -727,7 +727,8 @@ $plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid); global $pluginhost; - $pluginhost->load($plugins, $pluginhost::KIND_USER); + $pluginhost->load($plugins, $pluginhost::KIND_USER, $owner_uid); + $pluginhost->load_data(); } } diff --git a/plugins/example/example.php b/plugins/example/example.php index eef604b4f..f3788ae8c 100644 --- a/plugins/example/example.php +++ b/plugins/example/example.php @@ -23,7 +23,9 @@ class Example extends Plugin { function save() { $example_value = db_escape_string($_POST["example_value"]); - echo "Value set to $example_value (not really)"; + $this->host->set($this, "example", $example_value); + + echo "Value set to $example_value"; } function get_prefs_js() { @@ -35,6 +37,13 @@ class Example extends Plugin { print "
"; + print "
"; + +// print_r($this->host->set($this, "example", rand(0,100))); +// print_r($this->host->get_all($this)); + + $value = $this->host->get($this, "example"); + print "
"; print ""; @@ -58,7 +67,7 @@ class Example extends Plugin { print ""; print ""; - print ""; + print ""; print "
".__("Sample value")."
";