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();
11 private $plugin_actions = array();
14 private $last_registered;
15 private static $instance;
17 const API_VERSION
= 2;
19 // Hooks marked with *1 are run in global context and available
20 // to plugins loaded in config.php only
22 const HOOK_ARTICLE_BUTTON
= 1;
23 const HOOK_ARTICLE_FILTER
= 2;
24 const HOOK_PREFS_TAB
= 3;
25 const HOOK_PREFS_TAB_SECTION
= 4;
26 const HOOK_PREFS_TABS
= 5;
27 const HOOK_FEED_PARSED
= 6;
28 const HOOK_UPDATE_TASK
= 7; // *1
29 const HOOK_AUTH_USER
= 8;
30 const HOOK_HOTKEY_MAP
= 9;
31 const HOOK_RENDER_ARTICLE
= 10;
32 const HOOK_RENDER_ARTICLE_CDM
= 11;
33 const HOOK_FEED_FETCHED
= 12;
34 const HOOK_SANITIZE
= 13;
35 const HOOK_RENDER_ARTICLE_API
= 14;
36 const HOOK_TOOLBAR_BUTTON
= 15;
37 const HOOK_ACTION_ITEM
= 16;
38 const HOOK_HEADLINE_TOOLBAR_BUTTON
= 17;
39 const HOOK_HOTKEY_INFO
= 18;
40 const HOOK_ARTICLE_LEFT_BUTTON
= 19;
41 const HOOK_PREFS_EDIT_FEED
= 20;
42 const HOOK_PREFS_SAVE_FEED
= 21;
43 const HOOK_FETCH_FEED
= 22;
44 const HOOK_QUERY_HEADLINES
= 23;
45 const HOOK_HOUSE_KEEPING
= 24; // *1
46 const HOOK_SEARCH
= 25;
47 const HOOK_FORMAT_ENCLOSURES
= 26;
48 const HOOK_SUBSCRIBE_FEED
= 27;
49 const HOOK_HEADLINES_BEFORE
= 28;
50 const HOOK_RENDER_ENCLOSURE
= 29;
51 const HOOK_ARTICLE_FILTER_ACTION
= 30;
52 const HOOK_ARTICLE_EXPORT_FEED
= 31;
53 const HOOK_MAIN_TOOLBAR_BUTTON
= 32;
54 const HOOK_ENCLOSURE_ENTRY
= 33;
55 const HOOK_FORMAT_ARTICLE
= 34;
56 const HOOK_FORMAT_ARTICLE_CDM
= 35;
57 const HOOK_FEED_BASIC_INFO
= 36;
58 const HOOK_SEND_LOCAL_FILE
= 37;
61 const KIND_SYSTEM
= 2;
64 function __construct() {
65 $this->pdo
= Db
::pdo();
67 $this->storage
= array();
70 private function __clone() {
74 public static function getInstance() {
75 if (self
::$instance == null)
76 self
::$instance = new self();
78 return self
::$instance;
81 private function register_plugin($name, $plugin) {
82 //array_push($this->plugins, $plugin);
83 $this->plugins
[$name] = $plugin;
86 // needed for compatibility with API 1
99 function get_plugin_names() {
102 foreach ($this->plugins
as $p) {
103 array_push($names, get_class($p));
109 function get_plugins() {
110 return $this->plugins
;
113 function get_plugin($name) {
114 return $this->plugins
[strtolower($name)];
117 function run_hooks($type, $method, $args) {
118 foreach ($this->get_hooks($type) as $hook) {
119 $hook->$method($args);
123 function add_hook($type, $sender) {
124 if (!is_array($this->hooks
[$type])) {
125 $this->hooks
[$type] = array();
128 array_push($this->hooks
[$type], $sender);
131 function del_hook($type, $sender) {
132 if (is_array($this->hooks
[$type])) {
133 $key = array_Search($sender, $this->hooks
[$type]);
134 if ($key !== FALSE) {
135 unset($this->hooks
[$type][$key]);
140 function get_hooks($type) {
141 if (isset($this->hooks
[$type])) {
142 return $this->hooks
[$type];
147 function load_all($kind, $owner_uid = false, $skip_init = false) {
149 $plugins = array_merge(glob("plugins/*"), glob("plugins.local/*"));
150 $plugins = array_filter($plugins, "is_dir");
151 $plugins = array_map("basename", $plugins);
155 $this->load(join(",", $plugins), $kind, $owner_uid, $skip_init);
158 function load($classlist, $kind, $owner_uid = false, $skip_init = false) {
159 $plugins = explode(",", $classlist);
161 $this->owner_uid
= (int) $owner_uid;
163 foreach ($plugins as $class) {
164 $class = trim($class);
165 $class_file = strtolower(basename($class));
167 if (!is_dir(__DIR__
."/../plugins/$class_file") &&
168 !is_dir(__DIR__
."/../plugins.local/$class_file")) continue;
170 // try system plugin directory first
171 $file = __DIR__
. "/../plugins/$class_file/init.php";
173 if (!file_exists($file)) {
174 $file = __DIR__
. "/../plugins.local/$class_file/init.php";
177 if (!isset($this->plugins
[$class])) {
178 if (file_exists($file)) require_once $file;
180 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
181 $plugin = new $class($this);
183 $plugin_api = $plugin->api_version();
185 if ($plugin_api < PluginHost
::API_VERSION
) {
186 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost
::API_VERSION
. ", got: $plugin_api)", E_USER_WARNING
);
190 $this->last_registered
= $class;
193 case $this::KIND_SYSTEM
:
194 if ($this->is_system($plugin)) {
195 if (!$skip_init) $plugin->init($this);
196 $this->register_plugin($class, $plugin);
199 case $this::KIND_USER
:
200 if (!$this->is_system($plugin)) {
201 if (!$skip_init) $plugin->init($this);
202 $this->register_plugin($class, $plugin);
205 case $this::KIND_ALL
:
206 if (!$skip_init) $plugin->init($this);
207 $this->register_plugin($class, $plugin);
215 function is_system($plugin) {
216 $about = $plugin->about();
221 // only system plugins are allowed to modify routing
222 function add_handler($handler, $method, $sender) {
223 $handler = str_replace("-", "_", strtolower($handler));
224 $method = strtolower($method);
226 if ($this->is_system($sender)) {
227 if (!is_array($this->handlers
[$handler])) {
228 $this->handlers
[$handler] = array();
231 $this->handlers
[$handler][$method] = $sender;
235 function del_handler($handler, $method, $sender) {
236 $handler = str_replace("-", "_", strtolower($handler));
237 $method = strtolower($method);
239 if ($this->is_system($sender)) {
240 unset($this->handlers
[$handler][$method]);
244 function lookup_handler($handler, $method) {
245 $handler = str_replace("-", "_", strtolower($handler));
246 $method = strtolower($method);
248 if (is_array($this->handlers
[$handler])) {
249 if (isset($this->handlers
[$handler]["*"])) {
250 return $this->handlers
[$handler]["*"];
252 return $this->handlers
[$handler][$method];
259 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
260 $command = str_replace("-", "_", strtolower($command));
262 $this->commands
[$command] = array("description" => $description,
264 "arghelp" => $arghelp,
268 function del_command($command) {
269 $command = "-" . strtolower($command);
271 unset($this->commands
[$command]);
274 function lookup_command($command) {
275 $command = "-" . strtolower($command);
277 if (is_array($this->commands
[$command])) {
278 return $this->commands
[$command]["class"];
284 function get_commands() {
285 return $this->commands
;
288 function run_commands($args) {
289 foreach ($this->get_commands() as $command => $data) {
290 if (isset($args[$command])) {
291 $command = str_replace("-", "", $command);
292 $data["class"]->$command($args);
297 function load_data() {
298 if ($this->owner_uid
) {
299 $sth = $this->pdo
->prepare("SELECT name, content FROM ttrss_plugin_storage
300 WHERE owner_uid = ?");
301 $sth->execute([$this->owner_uid
]);
303 while ($line = $sth->fetch()) {
304 $this->storage
[$line["name"]] = unserialize($line["content"]);
309 private function save_data($plugin) {
310 if ($this->owner_uid
) {
311 $this->pdo
->beginTransaction();
313 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_plugin_storage WHERE
314 owner_uid= ? AND name = ?");
315 $sth->execute([$this->owner_uid
, $plugin]);
317 if (!isset($this->storage
[$plugin]))
318 $this->storage
[$plugin] = array();
320 $content = serialize($this->storage
[$plugin]);
323 $sth = $this->pdo
->prepare("UPDATE ttrss_plugin_storage SET content = ?
324 WHERE owner_uid= ? AND name = ?");
325 $sth->execute([(string)$content, $this->owner_uid
, $plugin]);
328 $sth = $this->pdo
->prepare("INSERT INTO ttrss_plugin_storage
329 (name,owner_uid,content) VALUES
331 $sth->execute([$plugin, $this->owner_uid
, (string)$content]);
334 $this->pdo
->commit();
338 function set($sender, $name, $value, $sync = true) {
339 $idx = get_class($sender);
341 if (!isset($this->storage
[$idx]))
342 $this->storage
[$idx] = array();
344 $this->storage
[$idx][$name] = $value;
346 if ($sync) $this->save_data(get_class($sender));
349 function get($sender, $name, $default_value = false) {
350 $idx = get_class($sender);
352 if (isset($this->storage
[$idx][$name])) {
353 return $this->storage
[$idx][$name];
355 return $default_value;
359 function get_all($sender) {
360 $idx = get_class($sender);
362 return $this->storage
[$idx];
365 function clear_data($sender) {
366 if ($this->owner_uid
) {
367 $idx = get_class($sender);
369 unset($this->storage
[$idx]);
371 $sth = $this->pdo
->prepare("DELETE FROM ttrss_plugin_storage WHERE name = ?
373 $sth->execute([$idx, $this->owner_uid
]);
377 function set_debug($debug) {
378 $this->debug
= $debug;
381 function get_debug() {
385 // Plugin feed functions are *EXPERIMENTAL*!
387 // cat_id: only -1 is supported (Special)
388 function add_feed($cat_id, $title, $icon, $sender) {
389 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
391 $id = count($this->feeds
[$cat_id]);
393 array_push($this->feeds
[$cat_id],
394 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
399 function get_feeds($cat_id) {
400 return $this->feeds
[$cat_id];
403 // convert feed_id (e.g. -129) to pfeed_id first
404 function get_feed_handler($pfeed_id) {
405 foreach ($this->feeds
as $cat) {
406 foreach ($cat as $feed) {
407 if ($feed['id'] == $pfeed_id) {
408 return $feed['sender'];
414 static function pfeed_to_feed_id($label) {
415 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
418 static function feed_to_pfeed_id($feed) {
419 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
422 function add_api_method($name, $sender) {
423 if ($this->is_system($sender)) {
424 $this->api_methods
[strtolower($name)] = $sender;
428 function get_api_method($name) {
429 return $this->api_methods
[$name];
432 function add_filter_action($sender, $action_name, $action_desc) {
433 $sender_class = get_class($sender);
435 if (!isset($this->plugin_actions
[$sender_class]))
436 $this->plugin_actions
[$sender_class] = array();
438 array_push($this->plugin_actions
[$sender_class],
439 array("action" => $action_name, "description" => $action_desc, "sender" => $sender));
442 function get_filter_actions() {
443 return $this->plugin_actions
;