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->dbh
= Db
::get();
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
95 function get_plugin_names() {
98 foreach ($this->plugins
as $p) {
99 array_push($names, get_class($p));
105 function get_plugins() {
106 return $this->plugins
;
109 function get_plugin($name) {
110 return $this->plugins
[strtolower($name)];
113 function run_hooks($type, $method, $args) {
114 foreach ($this->get_hooks($type) as $hook) {
115 $hook->$method($args);
119 function add_hook($type, $sender) {
120 if (!is_array($this->hooks
[$type])) {
121 $this->hooks
[$type] = array();
124 array_push($this->hooks
[$type], $sender);
127 function del_hook($type, $sender) {
128 if (is_array($this->hooks
[$type])) {
129 $key = array_Search($sender, $this->hooks
[$type]);
130 if ($key !== FALSE) {
131 unset($this->hooks
[$type][$key]);
136 function get_hooks($type) {
137 if (isset($this->hooks
[$type])) {
138 return $this->hooks
[$type];
143 function load_all($kind, $owner_uid = false, $skip_init = false) {
145 $plugins = array_merge(glob("plugins/*"), glob("plugins.local/*"));
146 $plugins = array_filter($plugins, "is_dir");
147 $plugins = array_map("basename", $plugins);
151 $this->load(join(",", $plugins), $kind, $owner_uid, $skip_init);
154 function load($classlist, $kind, $owner_uid = false, $skip_init = false) {
155 $plugins = explode(",", $classlist);
157 $this->owner_uid
= (int) $owner_uid;
159 foreach ($plugins as $class) {
160 $class = trim($class);
161 $class_file = strtolower(basename($class));
163 if (!is_dir(__DIR__
."/../plugins/$class_file") &&
164 !is_dir(__DIR__
."/../plugins.local/$class_file")) continue;
166 // try system plugin directory first
167 $file = __DIR__
. "/../plugins/$class_file/init.php";
169 if (!file_exists($file)) {
170 $file = __DIR__
. "/../plugins.local/$class_file/init.php";
173 if (!isset($this->plugins
[$class])) {
174 if (file_exists($file)) require_once $file;
176 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
177 $plugin = new $class($this);
179 $plugin_api = $plugin->api_version();
181 if ($plugin_api < PluginHost
::API_VERSION
) {
182 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost
::API_VERSION
. ", got: $plugin_api)", E_USER_WARNING
);
186 $this->last_registered
= $class;
189 case $this::KIND_SYSTEM
:
190 if ($this->is_system($plugin)) {
191 if (!$skip_init) $plugin->init($this);
192 $this->register_plugin($class, $plugin);
195 case $this::KIND_USER
:
196 if (!$this->is_system($plugin)) {
197 if (!$skip_init) $plugin->init($this);
198 $this->register_plugin($class, $plugin);
201 case $this::KIND_ALL
:
202 if (!$skip_init) $plugin->init($this);
203 $this->register_plugin($class, $plugin);
211 function is_system($plugin) {
212 $about = $plugin->about();
217 // only system plugins are allowed to modify routing
218 function add_handler($handler, $method, $sender) {
219 $handler = str_replace("-", "_", strtolower($handler));
220 $method = strtolower($method);
222 if ($this->is_system($sender)) {
223 if (!is_array($this->handlers
[$handler])) {
224 $this->handlers
[$handler] = array();
227 $this->handlers
[$handler][$method] = $sender;
231 function del_handler($handler, $method, $sender) {
232 $handler = str_replace("-", "_", strtolower($handler));
233 $method = strtolower($method);
235 if ($this->is_system($sender)) {
236 unset($this->handlers
[$handler][$method]);
240 function lookup_handler($handler, $method) {
241 $handler = str_replace("-", "_", strtolower($handler));
242 $method = strtolower($method);
244 if (is_array($this->handlers
[$handler])) {
245 if (isset($this->handlers
[$handler]["*"])) {
246 return $this->handlers
[$handler]["*"];
248 return $this->handlers
[$handler][$method];
255 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
256 $command = str_replace("-", "_", strtolower($command));
258 $this->commands
[$command] = array("description" => $description,
260 "arghelp" => $arghelp,
264 function del_command($command) {
265 $command = "-" . strtolower($command);
267 unset($this->commands
[$command]);
270 function lookup_command($command) {
271 $command = "-" . strtolower($command);
273 if (is_array($this->commands
[$command])) {
274 return $this->commands
[$command]["class"];
282 function get_commands() {
283 return $this->commands
;
286 function run_commands($args) {
287 foreach ($this->get_commands() as $command => $data) {
288 if (isset($args[$command])) {
289 $command = str_replace("-", "", $command);
290 $data["class"]->$command($args);
295 function load_data() {
296 if ($this->owner_uid
) {
297 $result = $this->dbh
->query("SELECT name, content FROM ttrss_plugin_storage
298 WHERE owner_uid = '".$this->owner_uid
."'");
300 while ($line = $this->dbh
->fetch_assoc($result)) {
301 $this->storage
[$line["name"]] = unserialize($line["content"]);
306 private function save_data($plugin) {
307 if ($this->owner_uid
) {
308 $plugin = $this->dbh
->escape_string($plugin);
310 $this->dbh
->query("BEGIN");
312 $result = $this->dbh
->query("SELECT id FROM ttrss_plugin_storage WHERE
313 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
315 if (!isset($this->storage
[$plugin]))
316 $this->storage
[$plugin] = array();
318 $content = $this->dbh
->escape_string(serialize($this->storage
[$plugin]),
321 if ($this->dbh
->num_rows($result) != 0) {
322 $this->dbh
->query("UPDATE ttrss_plugin_storage SET content = '$content'
323 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
326 $this->dbh
->query("INSERT INTO ttrss_plugin_storage
327 (name,owner_uid,content) VALUES
328 ('$plugin','".$this->owner_uid
."','$content')");
331 $this->dbh
->query("COMMIT");
335 function set($sender, $name, $value, $sync = true) {
336 $idx = get_class($sender);
338 if (!isset($this->storage
[$idx]))
339 $this->storage
[$idx] = array();
341 $this->storage
[$idx][$name] = $value;
343 if ($sync) $this->save_data(get_class($sender));
346 function get($sender, $name, $default_value = false) {
347 $idx = get_class($sender);
349 if (isset($this->storage
[$idx][$name])) {
350 return $this->storage
[$idx][$name];
352 return $default_value;
356 function get_all($sender) {
357 $idx = get_class($sender);
359 return $this->storage
[$idx];
362 function clear_data($sender) {
363 if ($this->owner_uid
) {
364 $idx = get_class($sender);
366 unset($this->storage
[$idx]);
368 $this->dbh
->query("DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
369 AND owner_uid = " . $this->owner_uid
);
373 function set_debug($debug) {
374 $this->debug
= $debug;
377 function get_debug() {
381 // Plugin feed functions are *EXPERIMENTAL*!
383 // cat_id: only -1 is supported (Special)
384 function add_feed($cat_id, $title, $icon, $sender) {
385 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
387 $id = count($this->feeds
[$cat_id]);
389 array_push($this->feeds
[$cat_id],
390 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
395 function get_feeds($cat_id) {
396 return $this->feeds
[$cat_id];
399 // convert feed_id (e.g. -129) to pfeed_id first
400 function get_feed_handler($pfeed_id) {
401 foreach ($this->feeds
as $cat) {
402 foreach ($cat as $feed) {
403 if ($feed['id'] == $pfeed_id) {
404 return $feed['sender'];
410 static function pfeed_to_feed_id($label) {
411 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
414 static function feed_to_pfeed_id($feed) {
415 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
418 function add_api_method($name, $sender) {
419 if ($this->is_system($sender)) {
420 $this->api_methods
[strtolower($name)] = $sender;
424 function get_api_method($name) {
425 return $this->api_methods
[$name];
428 function add_filter_action($sender, $action_name, $action_desc) {
429 $sender_class = get_class($sender);
431 if (!isset($this->plugin_actions
[$sender_class]))
432 $this->plugin_actions
[$sender_class] = array();
434 array_push($this->plugin_actions
[$sender_class],
435 array("action" => $action_name, "description" => $action_desc, "sender" => $sender));
438 function get_filter_actions() {
439 return $this->plugin_actions
;