From d2a421e3cbaa782748840fc19afad4ac65f044b8 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 25 Dec 2012 10:02:08 +0400 Subject: [PATCH] more work on user-selectable plugins; properly process system and user plugins --- classes/plugin.php | 15 +++++++- classes/pluginhost.php | 42 ++++++++++++++------- classes/pref/prefs.php | 6 +-- config.php-dist | 3 +- include/functions.php | 4 +- include/rssfuncs.php | 10 ++++- plugins/digest/digest.php | 8 +--- plugins/example/example.php | 4 +- plugins/example_feed/example_feed.php | 4 +- plugins/example_routing/example_routing.php | 4 +- plugins/flattr/flattr.php | 4 +- plugins/googleplus/googleplus.php | 4 +- plugins/identica/identica.php | 4 +- plugins/import_export/import_export.php | 4 +- plugins/instances/instances.php | 4 +- plugins/mail/mail.php | 4 +- plugins/note/note.php | 4 +- plugins/pinterest/pinterest.php | 4 +- plugins/pocket/pocket.php | 4 +- plugins/redditimgur/redditimgur.php | 4 +- plugins/share/share.php | 4 +- plugins/updater/updater.php | 4 +- update.php | 4 +- 23 files changed, 92 insertions(+), 60 deletions(-) diff --git a/classes/plugin.php b/classes/plugin.php index 59cb64f5..e655a206 100644 --- a/classes/plugin.php +++ b/classes/plugin.php @@ -3,9 +3,22 @@ class Plugin { private $link; private $host; - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; } + + function about() { + // version, name, description, author, is_system + return array(1.0, "plugin", "No description", "No author", false); + } + + function get_js() { + return ""; + } + + function get_prefs_js() { + return ""; + } } ?> diff --git a/classes/pluginhost.php b/classes/pluginhost.php index d8df6db4..ee56886f 100644 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -14,6 +14,10 @@ class PluginHost { const HOOK_FEED_PARSED = 6; const HOOK_UPDATE_TASK = 7; + const KIND_ALL = 1; + const KIND_SYSTEM = 2; + const KIND_USER = 3; + function __construct($link) { $this->link = $link; } @@ -65,12 +69,12 @@ class PluginHost { return array(); } } - function load_all() { + function load_all($kind) { $plugins = array_map("basename", glob("plugins/*")); - $this->load(join(",", $plugins)); + $this->load(join(",", $plugins), $kind); } - function load($classlist) { + function load($classlist, $kind) { $plugins = explode(",", $classlist); foreach ($plugins as $class) { @@ -84,14 +88,31 @@ class PluginHost { if (class_exists($class) && is_subclass_of($class, "Plugin")) { $plugin = new $class($this); - $this->register_plugin($class, $plugin); + switch ($kind) { + case $this::KIND_SYSTEM: + if ($this->is_system($plugin)) { + $plugin->init($this); + $this->register_plugin($class, $plugin); + } + break; + case $this::KIND_USER: + if (!$this->is_system($plugin)) { + $plugin->init($this); + $this->register_plugin($class, $plugin); + } + break; + case $this::KIND_ALL: + $plugin->init($this); + $this->register_plugin($class, $plugin); + break; + } } } } } function is_system($plugin) { - $about = $plugin->_about(); + $about = $plugin->about(); return @$about[3]; } @@ -134,22 +155,17 @@ class PluginHost { return false; } - // only system plugins are allowed to modify commands function add_command($command, $description, $sender) { $command = "-" . str_replace("-", "_", strtolower($command)); - if ($this->is_system($sender)) { - $this->commands[$command] = array("description" => $description, - "class" => $sender); - } + $this->commands[$command] = array("description" => $description, + "class" => $sender); } function del_command($command) { $command = "-" . strtolower($command); - if ($this->is_system($sender)) { - unset($this->commands[$command]); - } + unset($this->commands[$command]); } function lookup_command($command) { diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index bb1b44ec..ce17af27 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -670,10 +670,10 @@ class Pref_Prefs extends Handler_Protected { $user_enabled = array_map("trim", explode(",", get_pref($this->link, "_ENABLED_PLUGINS"))); $tmppluginhost = new PluginHost($link); - $tmppluginhost->load_all(); + $tmppluginhost->load_all($tmppluginhost::KIND_ALL); foreach ($tmppluginhost->get_plugins() as $name => $plugin) { - $about = $plugin->_about(); + $about = $plugin->about(); if ($about[3]) { if (in_array($name, $system_enabled)) { @@ -709,7 +709,7 @@ class Pref_Prefs extends Handler_Protected { foreach ($tmppluginhost->get_plugins() as $name => $plugin) { - $about = $plugin->_about(); + $about = $plugin->about(); if (!$about[3]) { diff --git a/config.php-dist b/config.php-dist index 28283794..52afdea1 100644 --- a/config.php-dist +++ b/config.php-dist @@ -172,7 +172,8 @@ // after login, or content encoding errors, disable it. define('PLUGINS', 'note'); - // Plugins to load. Check plugins/ directory for additional information. + // Comma-separated list of plugins to load for all users. System plugins have to be specified + // here, user plugins may be loaded per-user using Preferences/Plugins. define('FEEDBACK_URL', ''); // Displays an URL for users to provide feedback or comments regarding diff --git a/include/functions.php b/include/functions.php index 6848f14b..cd39789a 100644 --- a/include/functions.php +++ b/include/functions.php @@ -716,7 +716,7 @@ $plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid); global $pluginhost; - $pluginhost->load($plugins); + $pluginhost->load($plugins, $pluginhost::KIND_USER); } } @@ -3353,7 +3353,7 @@ global $pluginhost; $pluginhost = new PluginHost($link); - $pluginhost->load(PLUGINS); + $pluginhost->load(PLUGINS, $pluginhost::KIND_ALL); return true; } else { diff --git a/include/rssfuncs.php b/include/rssfuncs.php index 2105de33..d3286a53 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -263,7 +263,14 @@ if (!$rss->error()) { - global $pluginhost; + // We use local pluginhost here because we need to load different per-user feed plugins + $user_plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid); + + $pluginhost = new PluginHost($link); + + $pluginhost->load(PLUGINS, $pluginhost::KIND_ALL); + $pluginhost->load($plugins, $pluginhost::KIND_USER); + $pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss); if ($debug_enabled) { @@ -538,7 +545,6 @@ "tags" => $entry_tags, "author" => $entry_author); - global $pluginhost; foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_FILTER) as $plugin) { $article = $plugin->hook_article_filter($article); } diff --git a/plugins/digest/digest.php b/plugins/digest/digest.php index cb906e3c..621e4258 100644 --- a/plugins/digest/digest.php +++ b/plugins/digest/digest.php @@ -4,22 +4,18 @@ class Digest extends Plugin implements IHandler { private $link; private $host; - function _about() { + function about() { return array(1.0, "Digest mode for tt-rss (tablet friendly UI)", "fox", true); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; $host->add_handler("digest", "*", $this); - - //$host->add_handler("rpc", "digestinit", $this); - //$host->add_handler("rpc", "digestupdate", $this); - //$host->add_handler("rpc", "digestgetcontents", $this); } function index() { diff --git a/plugins/example/example.php b/plugins/example/example.php index be6a4855..eef604b4 100644 --- a/plugins/example/example.php +++ b/plugins/example/example.php @@ -6,14 +6,14 @@ class Example extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Example plugin #1", "fox", true); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/example_feed/example_feed.php b/plugins/example_feed/example_feed.php index a0d6d19c..af14d3ff 100644 --- a/plugins/example_feed/example_feed.php +++ b/plugins/example_feed/example_feed.php @@ -7,14 +7,14 @@ class Example_Feed extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Example feed plugin", "fox", true); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/example_routing/example_routing.php b/plugins/example_routing/example_routing.php index f15951e0..31c5b6f2 100644 --- a/plugins/example_routing/example_routing.php +++ b/plugins/example_routing/example_routing.php @@ -15,14 +15,14 @@ class Example_Routing extends Plugin implements IHandler { private $link; private $host; - function _about() { + function about() { return array(1.0, "Example routing plugin", "fox", true); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/flattr/flattr.php b/plugins/flattr/flattr.php index e5042632..3ab7ebd8 100644 --- a/plugins/flattr/flattr.php +++ b/plugins/flattr/flattr.php @@ -3,14 +3,14 @@ class Flattr extends Plugin { private $link; private $host; - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); } - function _about() { + function about() { return array(1.0, "Share on Flattr plugin", "Nic Honing"); diff --git a/plugins/googleplus/googleplus.php b/plugins/googleplus/googleplus.php index a100573d..11e58de2 100644 --- a/plugins/googleplus/googleplus.php +++ b/plugins/googleplus/googleplus.php @@ -3,14 +3,14 @@ class GooglePlus extends Plugin { private $link; private $host; - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); } - function _about() { + function about() { return array(1.0, "Share on Google+ plugin", "homolibere"); diff --git a/plugins/identica/identica.php b/plugins/identica/identica.php index 67ca6ae2..7d5e7131 100644 --- a/plugins/identica/identica.php +++ b/plugins/identica/identica.php @@ -3,14 +3,14 @@ class Identica extends Plugin { private $link; private $host; - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); } - function _about() { + function about() { return array(1.0, "Share on Identi.ca", "fox"); diff --git a/plugins/import_export/import_export.php b/plugins/import_export/import_export.php index 68597978..82397656 100644 --- a/plugins/import_export/import_export.php +++ b/plugins/import_export/import_export.php @@ -4,7 +4,7 @@ class Import_Export extends Plugin implements IHandler { private $link; private $host; - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; @@ -12,7 +12,7 @@ class Import_Export extends Plugin implements IHandler { $host->add_command("xml-import", "USER FILE: import articles from XML", $this); } - function _about() { + function about() { return array(1.0, "Imports and exports user data using a neutral XML format", "fox"); diff --git a/plugins/instances/instances.php b/plugins/instances/instances.php index 2836bce5..3acb163c 100644 --- a/plugins/instances/instances.php +++ b/plugins/instances/instances.php @@ -10,14 +10,14 @@ class Instances extends Plugin implements IHandler { 2 => "Invalid object received", 16 => "Access denied" ); - function _about() { + function about() { return array(1.0, "Support for linking tt-rss instances together and sharing popular feeds.", "fox", true); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/mail/mail.php b/plugins/mail/mail.php index 648148cd..ed65ca02 100644 --- a/plugins/mail/mail.php +++ b/plugins/mail/mail.php @@ -4,13 +4,13 @@ class Mail extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Adds a share article via email button", "fox"); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/note/note.php b/plugins/note/note.php index c6c1ef90..560796a6 100644 --- a/plugins/note/note.php +++ b/plugins/note/note.php @@ -3,13 +3,13 @@ class Note extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Adds support for setting article notes", "fox"); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/pinterest/pinterest.php b/plugins/pinterest/pinterest.php index c4254917..f93b62ff 100644 --- a/plugins/pinterest/pinterest.php +++ b/plugins/pinterest/pinterest.php @@ -3,13 +3,13 @@ class Pinterest extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Share article via Pinterest", "?"); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/pocket/pocket.php b/plugins/pocket/pocket.php index 6b5ccdde..b1759756 100644 --- a/plugins/pocket/pocket.php +++ b/plugins/pocket/pocket.php @@ -4,13 +4,13 @@ class Pocket extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Share article via Pocket (formerly Read It Later)", "?"); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/redditimgur/redditimgur.php b/plugins/redditimgur/redditimgur.php index 17764753..ca6554a7 100644 --- a/plugins/redditimgur/redditimgur.php +++ b/plugins/redditimgur/redditimgur.php @@ -4,13 +4,13 @@ class RedditImgur extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Inline image links in Reddit RSS feeds", "fox"); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/share/share.php b/plugins/share/share.php index 254b68bc..e1151849 100644 --- a/plugins/share/share.php +++ b/plugins/share/share.php @@ -3,13 +3,13 @@ class Share extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Share article by unique URL", "fox"); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/plugins/updater/updater.php b/plugins/updater/updater.php index 2148e3c0..1dbd5ab6 100644 --- a/plugins/updater/updater.php +++ b/plugins/updater/updater.php @@ -4,14 +4,14 @@ class Updater extends Plugin { private $link; private $host; - function _about() { + function about() { return array(1.0, "Updates tt-rss installation to latest version.", "fox", true); } - function __construct($host) { + function init($host) { $this->link = $host->get_link(); $this->host = $host; diff --git a/update.php b/update.php index 7bf6807f..2e06565a 100755 --- a/update.php +++ b/update.php @@ -260,9 +260,9 @@ if (in_array("-list-plugins", $op)) { $tmppluginhost = new PluginHost($link); - $tmppluginhost->load_all(); + $tmppluginhost->load_all($tmppluginhost::KIND_ALL); foreach ($tmppluginhost->get_plugins() as $name => $plugin) { - $about = $plugin->_about(); + $about = $plugin->about(); printf("%-60s - v%.2f (by %s)\n%s\n\n", $name, $about[0], $about[2], $about[1]);