5 private $hooks = array();
6 private $plugins = array();
7 private $handlers = array();
8 private $commands = array();
9 private $storage = array();
10 private $feeds = array();
11 private $api_methods = array();
12 private $plugin_actions = array();
15 private $last_registered;
16 private static $instance;
18 const API_VERSION = 2;
20 // Hooks marked with *1 are run in global context and available
21 // to plugins loaded in config.php only
23 const HOOK_ARTICLE_BUTTON = 1;
24 const HOOK_ARTICLE_FILTER = 2;
25 const HOOK_PREFS_TAB = 3;
26 const HOOK_PREFS_TAB_SECTION = 4;
27 const HOOK_PREFS_TABS = 5;
28 const HOOK_FEED_PARSED = 6;
29 const HOOK_UPDATE_TASK = 7; // *1
30 const HOOK_AUTH_USER = 8;
31 const HOOK_HOTKEY_MAP = 9;
32 const HOOK_RENDER_ARTICLE = 10;
33 const HOOK_RENDER_ARTICLE_CDM = 11;
34 const HOOK_FEED_FETCHED = 12;
35 const HOOK_SANITIZE = 13;
36 const HOOK_RENDER_ARTICLE_API = 14;
37 const HOOK_TOOLBAR_BUTTON = 15;
38 const HOOK_ACTION_ITEM = 16;
39 const HOOK_HEADLINE_TOOLBAR_BUTTON = 17;
40 const HOOK_HOTKEY_INFO = 18;
41 const HOOK_ARTICLE_LEFT_BUTTON = 19;
42 const HOOK_PREFS_EDIT_FEED = 20;
43 const HOOK_PREFS_SAVE_FEED = 21;
44 const HOOK_FETCH_FEED = 22;
45 const HOOK_QUERY_HEADLINES = 23;
46 const HOOK_HOUSE_KEEPING = 24; // *1
47 const HOOK_SEARCH = 25;
48 const HOOK_FORMAT_ENCLOSURES = 26;
49 const HOOK_SUBSCRIBE_FEED = 27;
50 const HOOK_HEADLINES_BEFORE = 28;
51 const HOOK_RENDER_ENCLOSURE = 29;
52 const HOOK_ARTICLE_FILTER_ACTION = 30;
53 const HOOK_ARTICLE_EXPORT_FEED = 31;
54 const HOOK_MAIN_TOOLBAR_BUTTON = 32;
55 const HOOK_ENCLOSURE_ENTRY = 33;
56 const HOOK_FORMAT_ARTICLE = 34;
57 const HOOK_FORMAT_ARTICLE_CDM = 35;
58 const HOOK_FEED_BASIC_INFO = 36;
59 const HOOK_SEND_LOCAL_FILE = 37;
62 const KIND_SYSTEM = 2;
65 function __construct() {
66 $this->dbh = Db::get();
67 $this->pdo = Db::pdo();
69 $this->storage = array();
72 private function __clone() {
76 public static function getInstance() {
77 if (self::$instance == null)
78 self::$instance = new self();
80 return self::$instance;
83 private function register_plugin($name, $plugin) {
84 //array_push($this->plugins, $plugin);
85 $this->plugins[$name] = $plugin;
88 // needed for compatibility with API 1
101 function get_plugin_names() {
104 foreach ($this->plugins as $p) {
105 array_push($names, get_class($p));
111 function get_plugins() {
112 return $this->plugins;
115 function get_plugin($name) {
116 return $this->plugins[strtolower($name)];
119 function run_hooks($type, $method, $args) {
120 foreach ($this->get_hooks($type) as $hook) {
121 $hook->$method($args);
125 function add_hook($type, $sender) {
126 if (!is_array($this->hooks[$type])) {
127 $this->hooks[$type] = array();
130 array_push($this->hooks[$type], $sender);
133 function del_hook($type, $sender) {
134 if (is_array($this->hooks[$type])) {
135 $key = array_Search($sender, $this->hooks[$type]);
136 if ($key !== FALSE) {
137 unset($this->hooks[$type][$key]);
142 function get_hooks($type) {
143 if (isset($this->hooks[$type])) {
144 return $this->hooks[$type];
149 function load_all($kind, $owner_uid = false, $skip_init = false) {
151 $plugins = array_merge(glob("plugins/*"), glob("plugins.local/*"));
152 $plugins = array_filter($plugins, "is_dir");
153 $plugins = array_map("basename", $plugins);
157 $this->load(join(",", $plugins), $kind, $owner_uid, $skip_init);
160 function load($classlist, $kind, $owner_uid = false, $skip_init = false) {
161 $plugins = explode(",", $classlist);
163 $this->owner_uid = (int) $owner_uid;
165 foreach ($plugins as $class) {
166 $class = trim($class);
167 $class_file = strtolower(basename($class));
169 if (!is_dir(__DIR__."/../plugins/$class_file") &&
170 !is_dir(__DIR__."/../plugins.local/$class_file")) continue;
172 // try system plugin directory first
173 $file = __DIR__ . "/../plugins/$class_file/init.php";
175 if (!file_exists($file)) {
176 $file = __DIR__ . "/../plugins.local/$class_file/init.php";
179 if (!isset($this->plugins[$class])) {
180 if (file_exists($file)) require_once $file;
182 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
183 $plugin = new $class($this);
185 $plugin_api = $plugin->api_version();
187 if ($plugin_api < PluginHost::API_VERSION) {
188 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost::API_VERSION . ", got: $plugin_api)", E_USER_WARNING);
192 $this->last_registered = $class;
195 case $this::KIND_SYSTEM:
196 if ($this->is_system($plugin)) {
197 if (!$skip_init) $plugin->init($this);
198 $this->register_plugin($class, $plugin);
201 case $this::KIND_USER:
202 if (!$this->is_system($plugin)) {
203 if (!$skip_init) $plugin->init($this);
204 $this->register_plugin($class, $plugin);
207 case $this::KIND_ALL:
208 if (!$skip_init) $plugin->init($this);
209 $this->register_plugin($class, $plugin);
217 function is_system($plugin) {
218 $about = $plugin->about();
223 // only system plugins are allowed to modify routing
224 function add_handler($handler, $method, $sender) {
225 $handler = str_replace("-", "_", strtolower($handler));
226 $method = strtolower($method);
228 if ($this->is_system($sender)) {
229 if (!is_array($this->handlers[$handler])) {
230 $this->handlers[$handler] = array();
233 $this->handlers[$handler][$method] = $sender;
237 function del_handler($handler, $method, $sender) {
238 $handler = str_replace("-", "_", strtolower($handler));
239 $method = strtolower($method);
241 if ($this->is_system($sender)) {
242 unset($this->handlers[$handler][$method]);
246 function lookup_handler($handler, $method) {
247 $handler = str_replace("-", "_", strtolower($handler));
248 $method = strtolower($method);
250 if (is_array($this->handlers[$handler])) {
251 if (isset($this->handlers[$handler]["*"])) {
252 return $this->handlers[$handler]["*"];
254 return $this->handlers[$handler][$method];
261 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
262 $command = str_replace("-", "_", strtolower($command));
264 $this->commands[$command] = array("description" => $description,
266 "arghelp" => $arghelp,
270 function del_command($command) {
271 $command = "-" . strtolower($command);
273 unset($this->commands[$command]);
276 function lookup_command($command) {
277 $command = "-" . strtolower($command);
279 if (is_array($this->commands[$command])) {
280 return $this->commands[$command]["class"];
288 function get_commands() {
289 return $this->commands;
292 function run_commands($args) {
293 foreach ($this->get_commands() as $command => $data) {
294 if (isset($args[$command])) {
295 $command = str_replace("-", "", $command);
296 $data["class"]->$command($args);
301 function load_data() {
302 if ($this->owner_uid) {
303 $sth = $this->pdo->prepare("SELECT name, content FROM ttrss_plugin_storage
304 WHERE owner_uid = ?");
305 $sth->execute([$this->owner_uid]);
307 while ($line = $sth->fetch()) {
308 $this->storage[$line["name"]] = unserialize($line["content"]);
313 private function save_data($plugin) {
314 if ($this->owner_uid) {
315 $this->pdo->beginTransaction();
317 $sth = $this->pdo->prepare("SELECT id FROM ttrss_plugin_storage WHERE
318 owner_uid= ? AND name = ?");
319 $sth->execute([$this->owner_uid, $plugin]);
321 if (!isset($this->storage[$plugin]))
322 $this->storage[$plugin] = array();
324 $content = serialize($this->storage[$plugin]);
327 $sth = $this->pdo->prepare("UPDATE ttrss_plugin_storage SET content = ?
328 WHERE owner_uid= ? AND name = ?");
329 $sth->execute([(string)$content, $this->owner_uid, $plugin]);
332 $sth = $this->pdo->prepare("INSERT INTO ttrss_plugin_storage
333 (name,owner_uid,content) VALUES
335 $sth->execute([$plugin, $this->owner_uid, (string)$content]);
338 $this->pdo->commit();
342 function set($sender, $name, $value, $sync = true) {
343 $idx = get_class($sender);
345 if (!isset($this->storage[$idx]))
346 $this->storage[$idx] = array();
348 $this->storage[$idx][$name] = $value;
350 if ($sync) $this->save_data(get_class($sender));
353 function get($sender, $name, $default_value = false) {
354 $idx = get_class($sender);
356 if (isset($this->storage[$idx][$name])) {
357 return $this->storage[$idx][$name];
359 return $default_value;
363 function get_all($sender) {
364 $idx = get_class($sender);
366 return $this->storage[$idx];
369 function clear_data($sender) {
370 if ($this->owner_uid) {
371 $idx = get_class($sender);
373 unset($this->storage[$idx]);
375 $sth = $this->pdo->prepare("DELETE FROM ttrss_plugin_storage WHERE name = ?
377 $sth->execute([$idx, $this->owner_uid]);
381 function set_debug($debug) {
382 $this->debug = $debug;
385 function get_debug() {
389 // Plugin feed functions are *EXPERIMENTAL*!
391 // cat_id: only -1 is supported (Special)
392 function add_feed($cat_id, $title, $icon, $sender) {
393 if (!$this->feeds[$cat_id]) $this->feeds[$cat_id] = array();
395 $id = count($this->feeds[$cat_id]);
397 array_push($this->feeds[$cat_id],
398 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
403 function get_feeds($cat_id) {
404 return $this->feeds[$cat_id];
407 // convert feed_id (e.g. -129) to pfeed_id first
408 function get_feed_handler($pfeed_id) {
409 foreach ($this->feeds as $cat) {
410 foreach ($cat as $feed) {
411 if ($feed['id'] == $pfeed_id) {
412 return $feed['sender'];
418 static function pfeed_to_feed_id($label) {
419 return PLUGIN_FEED_BASE_INDEX - 1 - abs($label);
422 static function feed_to_pfeed_id($feed) {
423 return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed);
426 function add_api_method($name, $sender) {
427 if ($this->is_system($sender)) {
428 $this->api_methods[strtolower($name)] = $sender;
432 function get_api_method($name) {
433 return $this->api_methods[$name];
436 function add_filter_action($sender, $action_name, $action_desc) {
437 $sender_class = get_class($sender);
439 if (!isset($this->plugin_actions[$sender_class]))
440 $this->plugin_actions[$sender_class] = array();
442 array_push($this->plugin_actions[$sender_class],
443 array("action" => $action_name, "description" => $action_desc, "sender" => $sender));
446 function get_filter_actions() {
447 return $this->plugin_actions;