From 79f9bef767ba0e0a35c8bf8411bb90feac35a551 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 12 Apr 2013 08:18:43 +0400 Subject: add support for plugins adding API methods --- classes/api.php | 15 +++++++++++++-- classes/pluginhost.php | 10 ++++++++++ plugins/example_api/init.php | 31 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 plugins/example_api/init.php diff --git a/classes/api.php b/classes/api.php index aaeb86dcf..0fcd65eb3 100644 --- a/classes/api.php +++ b/classes/api.php @@ -464,8 +464,19 @@ class API extends Handler { } - function index() { - print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD')); + function index($method) { + global $pluginhost; + + $plugin = $pluginhost->get_api_method(strtolower($method)); + + if ($plugin && method_exists($plugin, $method)) { + $reply = $plugin->$method(); + + print $this->wrap($reply[0], $reply[1]); + + } else { + print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method)); + } } function shareToPublished() { diff --git a/classes/pluginhost.php b/classes/pluginhost.php index 9ae7b809e..7c6fab9a0 100644 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -7,6 +7,7 @@ class PluginHost { private $commands = array(); private $storage = array(); private $feeds = array(); + private $api_methods = array(); private $owner_uid; private $debug; @@ -347,5 +348,14 @@ class PluginHost { 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]; + } } ?> diff --git a/plugins/example_api/init.php b/plugins/example_api/init.php new file mode 100644 index 000000000..a5f34b865 --- /dev/null +++ b/plugins/example_api/init.php @@ -0,0 +1,31 @@ +link = $host->get_link(); + $this->host = $host; + + $host->add_api_method("example_testmethod", $this); + } + + function example_testmethod() { + return array(API::STATUS_OK, array("current_time" => time())); + } +} +?> -- cgit v1.2.3