4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
8 private $storage = array();
9 private $feeds = array();
13 const HOOK_ARTICLE_BUTTON
= 1;
14 const HOOK_ARTICLE_FILTER
= 2;
15 const HOOK_PREFS_TAB
= 3;
16 const HOOK_PREFS_TAB_SECTION
= 4;
17 const HOOK_PREFS_TABS
= 5;
18 const HOOK_FEED_PARSED
= 6;
19 const HOOK_UPDATE_TASK
= 7;
20 const HOOK_AUTH_USER
= 8;
21 const HOOK_HOTKEY_MAP
= 9;
22 const HOOK_RENDER_ARTICLE
= 10;
23 const HOOK_RENDER_ARTICLE_CDM
= 11;
24 const HOOK_FEED_FETCHED
= 12;
25 const HOOK_SANITIZE
= 13;
26 const HOOK_RENDER_ARTICLE_API
= 14;
27 const HOOK_TOOLBAR_BUTTON
= 15;
28 const HOOK_ACTION_ITEM
= 16;
29 const HOOK_HEADLINE_TOOLBAR_BUTTON
= 17;
30 const HOOK_HOTKEY_INFO
= 18;
31 const HOOK_ARTICLE_LEFT_BUTTON
= 19;
34 const KIND_SYSTEM
= 2;
37 function __construct($link) {
40 $this->storage
= $_SESSION["plugin_storage"];
42 if (!$this->storage
) $this->storage
= array();
45 private function register_plugin($name, $plugin) {
46 //array_push($this->plugins, $plugin);
47 $this->plugins
[$name] = $plugin;
54 function get_plugins() {
55 return $this->plugins
;
58 function get_plugin($name) {
59 return $this->plugins
[$name];
62 function run_hooks($type, $method, $args) {
63 foreach ($this->get_hooks($type) as $hook) {
64 $hook->$method($args);
68 function add_hook($type, $sender) {
69 if (!is_array($this->hooks
[$type])) {
70 $this->hooks
[$type] = array();
73 array_push($this->hooks
[$type], $sender);
76 function del_hook($type, $sender) {
77 if (is_array($this->hooks
[$type])) {
78 $key = array_Search($this->hooks
[$type], $sender);
80 unset($this->hooks
[$type][$key]);
85 function get_hooks($type) {
86 if (isset($this->hooks
[$type])) {
87 return $this->hooks
[$type];
92 function load_all($kind, $owner_uid = false) {
93 $plugins = array_map("basename", glob("plugins/*"));
94 $this->load(join(",", $plugins), $kind, $owner_uid);
97 function load($classlist, $kind, $owner_uid = false) {
98 $plugins = explode(",", $classlist);
100 $this->owner_uid
= (int) $owner_uid;
102 foreach ($plugins as $class) {
103 $class = trim($class);
104 $class_file = strtolower(basename($class));
105 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
107 if (!isset($this->plugins
[$class])) {
108 if (file_exists($file)) require_once $file;
110 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
111 $plugin = new $class($this);
114 case $this::KIND_SYSTEM
:
115 if ($this->is_system($plugin)) {
116 $plugin->init($this);
117 $this->register_plugin($class, $plugin);
120 case $this::KIND_USER
:
121 if (!$this->is_system($plugin)) {
122 $plugin->init($this);
123 $this->register_plugin($class, $plugin);
126 case $this::KIND_ALL
:
127 $plugin->init($this);
128 $this->register_plugin($class, $plugin);
136 function is_system($plugin) {
137 $about = $plugin->about();
142 // only system plugins are allowed to modify routing
143 function add_handler($handler, $method, $sender) {
144 $handler = str_replace("-", "_", strtolower($handler));
145 $method = strtolower($method);
147 if ($this->is_system($sender)) {
148 if (!is_array($this->handlers
[$handler])) {
149 $this->handlers
[$handler] = array();
152 $this->handlers
[$handler][$method] = $sender;
156 function del_handler($handler, $method) {
157 $handler = str_replace("-", "_", strtolower($handler));
158 $method = strtolower($method);
160 if ($this->is_system($sender)) {
161 unset($this->handlers
[$handler][$method]);
165 function lookup_handler($handler, $method) {
166 $handler = str_replace("-", "_", strtolower($handler));
167 $method = strtolower($method);
169 if (is_array($this->handlers
[$handler])) {
170 if (isset($this->handlers
[$handler]["*"])) {
171 return $this->handlers
[$handler]["*"];
173 return $this->handlers
[$handler][$method];
180 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
181 $command = str_replace("-", "_", strtolower($command));
183 $this->commands
[$command] = array("description" => $description,
185 "arghelp" => $arghelp,
189 function del_command($command) {
190 $command = "-" . strtolower($command);
192 unset($this->commands
[$command]);
195 function lookup_command($command) {
196 $command = "-" . strtolower($command);
198 if (is_array($this->commands
[$command])) {
199 return $this->commands
[$command]["class"];
207 function get_commands() {
208 return $this->commands
;
211 function run_commands($args) {
212 foreach ($this->get_commands() as $command => $data) {
213 if (isset($args[$command])) {
214 $command = str_replace("-", "", $command);
215 $data["class"]->$command($args);
220 function load_data($force = false) {
221 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
222 $plugin = db_escape_string($this->link
, $plugin);
224 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
225 WHERE owner_uid = '".$this->owner_uid
."'");
227 while ($line = db_fetch_assoc($result)) {
228 $this->storage
[$line["name"]] = unserialize($line["content"]);
231 $_SESSION["plugin_storage"] = $this->storage
;
235 private function save_data($plugin) {
236 if ($this->owner_uid
) {
237 $plugin = db_escape_string($this->link
, $plugin);
239 db_query($this->link
, "BEGIN");
241 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
242 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
244 if (!isset($this->storage
[$plugin]))
245 $this->storage
[$plugin] = array();
247 $content = db_escape_string($this->link
, serialize($this->storage
[$plugin]));
249 if (db_num_rows($result) != 0) {
250 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
251 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
254 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
255 (name,owner_uid,content) VALUES
256 ('$plugin','".$this->owner_uid
."','$content')");
259 db_query($this->link
, "COMMIT");
263 function set($sender, $name, $value, $sync = true) {
264 $idx = get_class($sender);
266 if (!isset($this->storage
[$idx]))
267 $this->storage
[$idx] = array();
269 $this->storage
[$idx][$name] = $value;
271 $_SESSION["plugin_storage"] = $this->storage
;
273 if ($sync) $this->save_data(get_class($sender));
276 function get($sender, $name, $default_value = false) {
277 $idx = get_class($sender);
279 if (isset($this->storage
[$idx][$name])) {
280 return $this->storage
[$idx][$name];
282 return $default_value;
286 function get_all($sender) {
287 $idx = get_class($sender);
289 return $this->storage
[$idx];
292 function clear_data($sender) {
293 if ($this->owner_uid
) {
294 $idx = get_class($sender);
296 unset($this->storage
[$idx]);
298 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
299 AND owner_uid = " . $this->owner_uid
);
301 $_SESSION["plugin_storage"] = $this->storage
;
305 function set_debug($debug) {
306 $this->debug
= $debug;
309 function get_debug() {
313 // Plugin feed functions are *EXPERIMENTAL*!
315 // cat_id: only -1 is supported (Special)
316 function add_feed($cat_id, $title, $icon, $sender) {
317 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
319 $id = count($this->feeds
[$cat_id]);
321 array_push($this->feeds
[$cat_id],
322 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
327 function get_feeds($cat_id) {
328 return $this->feeds
[$cat_id];
331 // convert feed_id (e.g. -129) to pfeed_id first
332 function get_feed_handler($pfeed_id) {
333 foreach ($this->feeds
as $cat) {
334 foreach ($cat as $feed) {
335 if ($feed['id'] == $pfeed_id) {
336 return $feed['sender'];
342 static function pfeed_to_feed_id($label) {
343 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
346 static function feed_to_pfeed_id($feed) {
347 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);