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));
107 if (!is_dir(dirname(__FILE__
)."/../plugins/$class_file")) continue;
109 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
111 if (!isset($this->plugins
[$class])) {
112 if (file_exists($file)) require_once $file;
114 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
115 $plugin = new $class($this);
118 case $this::KIND_SYSTEM
:
119 if ($this->is_system($plugin)) {
120 $plugin->init($this);
121 $this->register_plugin($class, $plugin);
124 case $this::KIND_USER
:
125 if (!$this->is_system($plugin)) {
126 $plugin->init($this);
127 $this->register_plugin($class, $plugin);
130 case $this::KIND_ALL
:
131 $plugin->init($this);
132 $this->register_plugin($class, $plugin);
140 function is_system($plugin) {
141 $about = $plugin->about();
146 // only system plugins are allowed to modify routing
147 function add_handler($handler, $method, $sender) {
148 $handler = str_replace("-", "_", strtolower($handler));
149 $method = strtolower($method);
151 if ($this->is_system($sender)) {
152 if (!is_array($this->handlers
[$handler])) {
153 $this->handlers
[$handler] = array();
156 $this->handlers
[$handler][$method] = $sender;
160 function del_handler($handler, $method) {
161 $handler = str_replace("-", "_", strtolower($handler));
162 $method = strtolower($method);
164 if ($this->is_system($sender)) {
165 unset($this->handlers
[$handler][$method]);
169 function lookup_handler($handler, $method) {
170 $handler = str_replace("-", "_", strtolower($handler));
171 $method = strtolower($method);
173 if (is_array($this->handlers
[$handler])) {
174 if (isset($this->handlers
[$handler]["*"])) {
175 return $this->handlers
[$handler]["*"];
177 return $this->handlers
[$handler][$method];
184 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
185 $command = str_replace("-", "_", strtolower($command));
187 $this->commands
[$command] = array("description" => $description,
189 "arghelp" => $arghelp,
193 function del_command($command) {
194 $command = "-" . strtolower($command);
196 unset($this->commands
[$command]);
199 function lookup_command($command) {
200 $command = "-" . strtolower($command);
202 if (is_array($this->commands
[$command])) {
203 return $this->commands
[$command]["class"];
211 function get_commands() {
212 return $this->commands
;
215 function run_commands($args) {
216 foreach ($this->get_commands() as $command => $data) {
217 if (isset($args[$command])) {
218 $command = str_replace("-", "", $command);
219 $data["class"]->$command($args);
224 function load_data($force = false) {
225 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
226 $plugin = db_escape_string($this->link
, $plugin);
228 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
229 WHERE owner_uid = '".$this->owner_uid
."'");
231 while ($line = db_fetch_assoc($result)) {
232 $this->storage
[$line["name"]] = unserialize($line["content"]);
235 $_SESSION["plugin_storage"] = $this->storage
;
239 private function save_data($plugin) {
240 if ($this->owner_uid
) {
241 $plugin = db_escape_string($this->link
, $plugin);
243 db_query($this->link
, "BEGIN");
245 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
246 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
248 if (!isset($this->storage
[$plugin]))
249 $this->storage
[$plugin] = array();
251 $content = db_escape_string($this->link
, serialize($this->storage
[$plugin]));
253 if (db_num_rows($result) != 0) {
254 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
255 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
258 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
259 (name,owner_uid,content) VALUES
260 ('$plugin','".$this->owner_uid
."','$content')");
263 db_query($this->link
, "COMMIT");
267 function set($sender, $name, $value, $sync = true) {
268 $idx = get_class($sender);
270 if (!isset($this->storage
[$idx]))
271 $this->storage
[$idx] = array();
273 $this->storage
[$idx][$name] = $value;
275 $_SESSION["plugin_storage"] = $this->storage
;
277 if ($sync) $this->save_data(get_class($sender));
280 function get($sender, $name, $default_value = false) {
281 $idx = get_class($sender);
283 if (isset($this->storage
[$idx][$name])) {
284 return $this->storage
[$idx][$name];
286 return $default_value;
290 function get_all($sender) {
291 $idx = get_class($sender);
293 return $this->storage
[$idx];
296 function clear_data($sender) {
297 if ($this->owner_uid
) {
298 $idx = get_class($sender);
300 unset($this->storage
[$idx]);
302 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
303 AND owner_uid = " . $this->owner_uid
);
305 $_SESSION["plugin_storage"] = $this->storage
;
309 function set_debug($debug) {
310 $this->debug
= $debug;
313 function get_debug() {
317 // Plugin feed functions are *EXPERIMENTAL*!
319 // cat_id: only -1 is supported (Special)
320 function add_feed($cat_id, $title, $icon, $sender) {
321 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
323 $id = count($this->feeds
[$cat_id]);
325 array_push($this->feeds
[$cat_id],
326 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
331 function get_feeds($cat_id) {
332 return $this->feeds
[$cat_id];
335 // convert feed_id (e.g. -129) to pfeed_id first
336 function get_feed_handler($pfeed_id) {
337 foreach ($this->feeds
as $cat) {
338 foreach ($cat as $feed) {
339 if ($feed['id'] == $pfeed_id) {
340 return $feed['sender'];
346 static function pfeed_to_feed_id($label) {
347 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
350 static function feed_to_pfeed_id($feed) {
351 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
354 function add_api_method($name, $sender) {
355 if ($this->is_system($sender)) {
356 $this->api_methods
[strtolower($name)] = $sender;
360 function get_api_method($name) {
361 return $this->api_methods
[$name];