4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
8 private $storage = array();
9 private $feeds = array();
10 private $api_methods = array();
13 private $last_registered;
14 private static $instance;
16 const API_VERSION
= 2;
18 const HOOK_ARTICLE_BUTTON
= 1;
19 const HOOK_ARTICLE_FILTER
= 2;
20 const HOOK_PREFS_TAB
= 3;
21 const HOOK_PREFS_TAB_SECTION
= 4;
22 const HOOK_PREFS_TABS
= 5;
23 const HOOK_FEED_PARSED
= 6;
24 const HOOK_UPDATE_TASK
= 7;
25 const HOOK_AUTH_USER
= 8;
26 const HOOK_HOTKEY_MAP
= 9;
27 const HOOK_RENDER_ARTICLE
= 10;
28 const HOOK_RENDER_ARTICLE_CDM
= 11;
29 const HOOK_FEED_FETCHED
= 12;
30 const HOOK_SANITIZE
= 13;
31 const HOOK_RENDER_ARTICLE_API
= 14;
32 const HOOK_TOOLBAR_BUTTON
= 15;
33 const HOOK_ACTION_ITEM
= 16;
34 const HOOK_HEADLINE_TOOLBAR_BUTTON
= 17;
35 const HOOK_HOTKEY_INFO
= 18;
36 const HOOK_ARTICLE_LEFT_BUTTON
= 19;
37 const HOOK_PREFS_EDIT_FEED
= 20;
38 const HOOK_PREFS_SAVE_FEED
= 21;
39 const HOOK_FETCH_FEED
= 22;
40 const HOOK_QUERY_HEADLINES
= 23;
41 const HOOK_HOUSE_KEEPING
= 24;
44 const KIND_SYSTEM
= 2;
47 function __construct() {
48 $this->dbh
= Db
::get();
50 $this->storage
= array();
53 private function __clone() {
57 public static function getInstance() {
58 if (self
::$instance == null)
59 self
::$instance = new self();
61 return self
::$instance;
64 private function register_plugin($name, $plugin) {
65 //array_push($this->plugins, $plugin);
66 $this->plugins
[$name] = $plugin;
69 // needed for compatibility with API 1
78 function get_plugins() {
79 return $this->plugins
;
82 function get_plugin($name) {
83 return $this->plugins
[$name];
86 function run_hooks($type, $method, $args) {
87 foreach ($this->get_hooks($type) as $hook) {
88 $hook->$method($args);
92 function add_hook($type, $sender) {
93 if (!is_array($this->hooks
[$type])) {
94 $this->hooks
[$type] = array();
97 array_push($this->hooks
[$type], $sender);
100 function del_hook($type, $sender) {
101 if (is_array($this->hooks
[$type])) {
102 $key = array_Search($this->hooks
[$type], $sender);
103 if ($key !== FALSE) {
104 unset($this->hooks
[$type][$key]);
109 function get_hooks($type) {
110 if (isset($this->hooks
[$type])) {
111 return $this->hooks
[$type];
116 function load_all($kind, $owner_uid = false) {
117 $plugins = array_map("basename", glob("plugins/*"));
118 $this->load(join(",", $plugins), $kind, $owner_uid);
121 function load($classlist, $kind, $owner_uid = false) {
122 $plugins = explode(",", $classlist);
124 $this->owner_uid
= (int) $owner_uid;
126 foreach ($plugins as $class) {
127 $class = trim($class);
128 $class_file = strtolower(basename($class));
130 if (!is_dir(dirname(__FILE__
)."/../plugins/$class_file")) continue;
132 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
134 if (!isset($this->plugins
[$class])) {
135 if (file_exists($file)) require_once $file;
137 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
138 $plugin = new $class($this);
140 $plugin_api = $plugin->api_version();
142 if ($plugin_api < PluginHost
::API_VERSION
) {
143 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost
::API_VERSION
. ", got: $plugin_api)", E_USER_WARNING
);
147 $this->last_registered
= $class;
150 case $this::KIND_SYSTEM
:
151 if ($this->is_system($plugin)) {
152 $plugin->init($this);
153 $this->register_plugin($class, $plugin);
156 case $this::KIND_USER
:
157 if (!$this->is_system($plugin)) {
158 $plugin->init($this);
159 $this->register_plugin($class, $plugin);
162 case $this::KIND_ALL
:
163 $plugin->init($this);
164 $this->register_plugin($class, $plugin);
172 function is_system($plugin) {
173 $about = $plugin->about();
178 // only system plugins are allowed to modify routing
179 function add_handler($handler, $method, $sender) {
180 $handler = str_replace("-", "_", strtolower($handler));
181 $method = strtolower($method);
183 if ($this->is_system($sender)) {
184 if (!is_array($this->handlers
[$handler])) {
185 $this->handlers
[$handler] = array();
188 $this->handlers
[$handler][$method] = $sender;
192 function del_handler($handler, $method, $sender) {
193 $handler = str_replace("-", "_", strtolower($handler));
194 $method = strtolower($method);
196 if ($this->is_system($sender)) {
197 unset($this->handlers
[$handler][$method]);
201 function lookup_handler($handler, $method) {
202 $handler = str_replace("-", "_", strtolower($handler));
203 $method = strtolower($method);
205 if (is_array($this->handlers
[$handler])) {
206 if (isset($this->handlers
[$handler]["*"])) {
207 return $this->handlers
[$handler]["*"];
209 return $this->handlers
[$handler][$method];
216 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
217 $command = str_replace("-", "_", strtolower($command));
219 $this->commands
[$command] = array("description" => $description,
221 "arghelp" => $arghelp,
225 function del_command($command) {
226 $command = "-" . strtolower($command);
228 unset($this->commands
[$command]);
231 function lookup_command($command) {
232 $command = "-" . strtolower($command);
234 if (is_array($this->commands
[$command])) {
235 return $this->commands
[$command]["class"];
243 function get_commands() {
244 return $this->commands
;
247 function run_commands($args) {
248 foreach ($this->get_commands() as $command => $data) {
249 if (isset($args[$command])) {
250 $command = str_replace("-", "", $command);
251 $data["class"]->$command($args);
256 function load_data($force = false) {
257 if ($this->owner_uid
) {
258 $result = $this->dbh
->query("SELECT name, content FROM ttrss_plugin_storage
259 WHERE owner_uid = '".$this->owner_uid
."'");
261 while ($line = $this->dbh
->fetch_assoc($result)) {
262 $this->storage
[$line["name"]] = unserialize($line["content"]);
267 private function save_data($plugin) {
268 if ($this->owner_uid
) {
269 $plugin = $this->dbh
->escape_string($plugin);
271 $this->dbh
->query("BEGIN");
273 $result = $this->dbh
->query("SELECT id FROM ttrss_plugin_storage WHERE
274 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
276 if (!isset($this->storage
[$plugin]))
277 $this->storage
[$plugin] = array();
279 $content = $this->dbh
->escape_string(serialize($this->storage
[$plugin]),
282 if ($this->dbh
->num_rows($result) != 0) {
283 $this->dbh
->query("UPDATE ttrss_plugin_storage SET content = '$content'
284 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
287 $this->dbh
->query("INSERT INTO ttrss_plugin_storage
288 (name,owner_uid,content) VALUES
289 ('$plugin','".$this->owner_uid
."','$content')");
292 $this->dbh
->query("COMMIT");
296 function set($sender, $name, $value, $sync = true) {
297 $idx = get_class($sender);
299 if (!isset($this->storage
[$idx]))
300 $this->storage
[$idx] = array();
302 $this->storage
[$idx][$name] = $value;
304 if ($sync) $this->save_data(get_class($sender));
307 function get($sender, $name, $default_value = false) {
308 $idx = get_class($sender);
310 if (isset($this->storage
[$idx][$name])) {
311 return $this->storage
[$idx][$name];
313 return $default_value;
317 function get_all($sender) {
318 $idx = get_class($sender);
320 return $this->storage
[$idx];
323 function clear_data($sender) {
324 if ($this->owner_uid
) {
325 $idx = get_class($sender);
327 unset($this->storage
[$idx]);
329 $this->dbh
->query("DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
330 AND owner_uid = " . $this->owner_uid
);
334 function set_debug($debug) {
335 $this->debug
= $debug;
338 function get_debug() {
342 // Plugin feed functions are *EXPERIMENTAL*!
344 // cat_id: only -1 is supported (Special)
345 function add_feed($cat_id, $title, $icon, $sender) {
346 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
348 $id = count($this->feeds
[$cat_id]);
350 array_push($this->feeds
[$cat_id],
351 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
356 function get_feeds($cat_id) {
357 return $this->feeds
[$cat_id];
360 // convert feed_id (e.g. -129) to pfeed_id first
361 function get_feed_handler($pfeed_id) {
362 foreach ($this->feeds
as $cat) {
363 foreach ($cat as $feed) {
364 if ($feed['id'] == $pfeed_id) {
365 return $feed['sender'];
371 static function pfeed_to_feed_id($label) {
372 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
375 static function feed_to_pfeed_id($feed) {
376 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
379 function add_api_method($name, $sender) {
380 if ($this->is_system($sender)) {
381 $this->api_methods
[strtolower($name)] = $sender;
385 function get_api_method($name) {
386 return $this->api_methods
[$name];