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();
14 const HOOK_ARTICLE_BUTTON
= 1;
15 const HOOK_ARTICLE_FILTER
= 2;
16 const HOOK_PREFS_TAB
= 3;
17 const HOOK_PREFS_TAB_SECTION
= 4;
18 const HOOK_PREFS_TABS
= 5;
19 const HOOK_FEED_PARSED
= 6;
20 const HOOK_UPDATE_TASK
= 7;
21 const HOOK_AUTH_USER
= 8;
22 const HOOK_HOTKEY_MAP
= 9;
23 const HOOK_RENDER_ARTICLE
= 10;
24 const HOOK_RENDER_ARTICLE_CDM
= 11;
25 const HOOK_FEED_FETCHED
= 12;
26 const HOOK_SANITIZE
= 13;
27 const HOOK_RENDER_ARTICLE_API
= 14;
28 const HOOK_TOOLBAR_BUTTON
= 15;
29 const HOOK_ACTION_ITEM
= 16;
30 const HOOK_HEADLINE_TOOLBAR_BUTTON
= 17;
31 const HOOK_HOTKEY_INFO
= 18;
32 const HOOK_ARTICLE_LEFT_BUTTON
= 19;
35 const KIND_SYSTEM
= 2;
38 function __construct($link) {
41 $this->storage
= $_SESSION["plugin_storage"];
43 if (!$this->storage
) $this->storage
= array();
46 private function register_plugin($name, $plugin) {
47 //array_push($this->plugins, $plugin);
48 $this->plugins
[$name] = $plugin;
55 function get_plugins() {
56 return $this->plugins
;
59 function get_plugin($name) {
60 return $this->plugins
[$name];
63 function run_hooks($type, $method, $args) {
64 foreach ($this->get_hooks($type) as $hook) {
65 $hook->$method($args);
69 function add_hook($type, $sender) {
70 if (!is_array($this->hooks
[$type])) {
71 $this->hooks
[$type] = array();
74 array_push($this->hooks
[$type], $sender);
77 function del_hook($type, $sender) {
78 if (is_array($this->hooks
[$type])) {
79 $key = array_Search($this->hooks
[$type], $sender);
81 unset($this->hooks
[$type][$key]);
86 function get_hooks($type) {
87 if (isset($this->hooks
[$type])) {
88 return $this->hooks
[$type];
93 function load_all($kind, $owner_uid = false) {
94 $plugins = array_map("basename", glob("plugins/*"));
95 $this->load(join(",", $plugins), $kind, $owner_uid);
98 function load($classlist, $kind, $owner_uid = false) {
99 $plugins = explode(",", $classlist);
101 $this->owner_uid
= (int) $owner_uid;
103 foreach ($plugins as $class) {
104 $class = trim($class);
105 $class_file = strtolower(basename($class));
106 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
108 if (!isset($this->plugins
[$class])) {
109 if (file_exists($file)) require_once $file;
111 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
112 $plugin = new $class($this);
115 case $this::KIND_SYSTEM
:
116 if ($this->is_system($plugin)) {
117 $plugin->init($this);
118 $this->register_plugin($class, $plugin);
121 case $this::KIND_USER
:
122 if (!$this->is_system($plugin)) {
123 $plugin->init($this);
124 $this->register_plugin($class, $plugin);
127 case $this::KIND_ALL
:
128 $plugin->init($this);
129 $this->register_plugin($class, $plugin);
137 function is_system($plugin) {
138 $about = $plugin->about();
143 // only system plugins are allowed to modify routing
144 function add_handler($handler, $method, $sender) {
145 $handler = str_replace("-", "_", strtolower($handler));
146 $method = strtolower($method);
148 if ($this->is_system($sender)) {
149 if (!is_array($this->handlers
[$handler])) {
150 $this->handlers
[$handler] = array();
153 $this->handlers
[$handler][$method] = $sender;
157 function del_handler($handler, $method) {
158 $handler = str_replace("-", "_", strtolower($handler));
159 $method = strtolower($method);
161 if ($this->is_system($sender)) {
162 unset($this->handlers
[$handler][$method]);
166 function lookup_handler($handler, $method) {
167 $handler = str_replace("-", "_", strtolower($handler));
168 $method = strtolower($method);
170 if (is_array($this->handlers
[$handler])) {
171 if (isset($this->handlers
[$handler]["*"])) {
172 return $this->handlers
[$handler]["*"];
174 return $this->handlers
[$handler][$method];
181 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
182 $command = str_replace("-", "_", strtolower($command));
184 $this->commands
[$command] = array("description" => $description,
186 "arghelp" => $arghelp,
190 function del_command($command) {
191 $command = "-" . strtolower($command);
193 unset($this->commands
[$command]);
196 function lookup_command($command) {
197 $command = "-" . strtolower($command);
199 if (is_array($this->commands
[$command])) {
200 return $this->commands
[$command]["class"];
208 function get_commands() {
209 return $this->commands
;
212 function run_commands($args) {
213 foreach ($this->get_commands() as $command => $data) {
214 if (isset($args[$command])) {
215 $command = str_replace("-", "", $command);
216 $data["class"]->$command($args);
221 function load_data($force = false) {
222 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
223 $plugin = db_escape_string($this->link
, $plugin);
225 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
226 WHERE owner_uid = '".$this->owner_uid
."'");
228 while ($line = db_fetch_assoc($result)) {
229 $this->storage
[$line["name"]] = unserialize($line["content"]);
232 $_SESSION["plugin_storage"] = $this->storage
;
236 private function save_data($plugin) {
237 if ($this->owner_uid
) {
238 $plugin = db_escape_string($this->link
, $plugin);
240 db_query($this->link
, "BEGIN");
242 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
243 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
245 if (!isset($this->storage
[$plugin]))
246 $this->storage
[$plugin] = array();
248 $content = db_escape_string($this->link
, serialize($this->storage
[$plugin]));
250 if (db_num_rows($result) != 0) {
251 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
252 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
255 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
256 (name,owner_uid,content) VALUES
257 ('$plugin','".$this->owner_uid
."','$content')");
260 db_query($this->link
, "COMMIT");
264 function set($sender, $name, $value, $sync = true) {
265 $idx = get_class($sender);
267 if (!isset($this->storage
[$idx]))
268 $this->storage
[$idx] = array();
270 $this->storage
[$idx][$name] = $value;
272 $_SESSION["plugin_storage"] = $this->storage
;
274 if ($sync) $this->save_data(get_class($sender));
277 function get($sender, $name, $default_value = false) {
278 $idx = get_class($sender);
280 if (isset($this->storage
[$idx][$name])) {
281 return $this->storage
[$idx][$name];
283 return $default_value;
287 function get_all($sender) {
288 $idx = get_class($sender);
290 return $this->storage
[$idx];
293 function clear_data($sender) {
294 if ($this->owner_uid
) {
295 $idx = get_class($sender);
297 unset($this->storage
[$idx]);
299 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
300 AND owner_uid = " . $this->owner_uid
);
302 $_SESSION["plugin_storage"] = $this->storage
;
306 function set_debug($debug) {
307 $this->debug
= $debug;
310 function get_debug() {
314 // Plugin feed functions are *EXPERIMENTAL*!
316 // cat_id: only -1 is supported (Special)
317 function add_feed($cat_id, $title, $icon, $sender) {
318 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
320 $id = count($this->feeds
[$cat_id]);
322 array_push($this->feeds
[$cat_id],
323 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
328 function get_feeds($cat_id) {
329 return $this->feeds
[$cat_id];
332 // convert feed_id (e.g. -129) to pfeed_id first
333 function get_feed_handler($pfeed_id) {
334 foreach ($this->feeds
as $cat) {
335 foreach ($cat as $feed) {
336 if ($feed['id'] == $pfeed_id) {
337 return $feed['sender'];
343 static function pfeed_to_feed_id($label) {
344 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
347 static function feed_to_pfeed_id($feed) {
348 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
351 function add_api_method($name, $sender) {
352 if ($this->is_system($sender)) {
353 $this->api_methods
[strtolower($name)] = $sender;
357 function get_api_method($name) {
358 return $this->api_methods
[$name];