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($dbh) {
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));
106 if (!is_dir(dirname(__FILE__
)."/../plugins/$class_file")) continue;
108 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
110 if (!isset($this->plugins
[$class])) {
111 if (file_exists($file)) require_once $file;
113 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
114 $plugin = new $class($this);
117 case $this::KIND_SYSTEM
:
118 if ($this->is_system($plugin)) {
119 $plugin->init($this);
120 $this->register_plugin($class, $plugin);
123 case $this::KIND_USER
:
124 if (!$this->is_system($plugin)) {
125 $plugin->init($this);
126 $this->register_plugin($class, $plugin);
129 case $this::KIND_ALL
:
130 $plugin->init($this);
131 $this->register_plugin($class, $plugin);
139 function is_system($plugin) {
140 $about = $plugin->about();
145 // only system plugins are allowed to modify routing
146 function add_handler($handler, $method, $sender) {
147 $handler = str_replace("-", "_", strtolower($handler));
148 $method = strtolower($method);
150 if ($this->is_system($sender)) {
151 if (!is_array($this->handlers
[$handler])) {
152 $this->handlers
[$handler] = array();
155 $this->handlers
[$handler][$method] = $sender;
159 function del_handler($handler, $method) {
160 $handler = str_replace("-", "_", strtolower($handler));
161 $method = strtolower($method);
163 if ($this->is_system($sender)) {
164 unset($this->handlers
[$handler][$method]);
168 function lookup_handler($handler, $method) {
169 $handler = str_replace("-", "_", strtolower($handler));
170 $method = strtolower($method);
172 if (is_array($this->handlers
[$handler])) {
173 if (isset($this->handlers
[$handler]["*"])) {
174 return $this->handlers
[$handler]["*"];
176 return $this->handlers
[$handler][$method];
183 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
184 $command = str_replace("-", "_", strtolower($command));
186 $this->commands
[$command] = array("description" => $description,
188 "arghelp" => $arghelp,
192 function del_command($command) {
193 $command = "-" . strtolower($command);
195 unset($this->commands
[$command]);
198 function lookup_command($command) {
199 $command = "-" . strtolower($command);
201 if (is_array($this->commands
[$command])) {
202 return $this->commands
[$command]["class"];
210 function get_commands() {
211 return $this->commands
;
214 function run_commands($args) {
215 foreach ($this->get_commands() as $command => $data) {
216 if (isset($args[$command])) {
217 $command = str_replace("-", "", $command);
218 $data["class"]->$command($args);
223 function load_data($force = false) {
224 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
225 $plugin = db_escape_string( $plugin);
227 $result = db_query( "SELECT name, content FROM ttrss_plugin_storage
228 WHERE owner_uid = '".$this->owner_uid
."'");
230 while ($line = db_fetch_assoc($result)) {
231 $this->storage
[$line["name"]] = unserialize($line["content"]);
234 $_SESSION["plugin_storage"] = $this->storage
;
238 private function save_data($plugin) {
239 if ($this->owner_uid
) {
240 $plugin = db_escape_string( $plugin);
244 $result = db_query("SELECT id FROM ttrss_plugin_storage WHERE
245 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
247 if (!isset($this->storage
[$plugin]))
248 $this->storage
[$plugin] = array();
250 $content = db_escape_string( serialize($this->storage
[$plugin]));
252 if (db_num_rows($result) != 0) {
253 db_query( "UPDATE ttrss_plugin_storage SET content = '$content'
254 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
257 db_query( "INSERT INTO ttrss_plugin_storage
258 (name,owner_uid,content) VALUES
259 ('$plugin','".$this->owner_uid
."','$content')");
266 function set($sender, $name, $value, $sync = true) {
267 $idx = get_class($sender);
269 if (!isset($this->storage
[$idx]))
270 $this->storage
[$idx] = array();
272 $this->storage
[$idx][$name] = $value;
274 $_SESSION["plugin_storage"] = $this->storage
;
276 if ($sync) $this->save_data(get_class($sender));
279 function get($sender, $name, $default_value = false) {
280 $idx = get_class($sender);
282 if (isset($this->storage
[$idx][$name])) {
283 return $this->storage
[$idx][$name];
285 return $default_value;
289 function get_all($sender) {
290 $idx = get_class($sender);
292 return $this->storage
[$idx];
295 function clear_data($sender) {
296 if ($this->owner_uid
) {
297 $idx = get_class($sender);
299 unset($this->storage
[$idx]);
301 db_query( "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
302 AND owner_uid = " . $this->owner_uid
);
304 $_SESSION["plugin_storage"] = $this->storage
;
308 function set_debug($debug) {
309 $this->debug
= $debug;
312 function get_debug() {
316 // Plugin feed functions are *EXPERIMENTAL*!
318 // cat_id: only -1 is supported (Special)
319 function add_feed($cat_id, $title, $icon, $sender) {
320 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
322 $id = count($this->feeds
[$cat_id]);
324 array_push($this->feeds
[$cat_id],
325 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
330 function get_feeds($cat_id) {
331 return $this->feeds
[$cat_id];
334 // convert feed_id (e.g. -129) to pfeed_id first
335 function get_feed_handler($pfeed_id) {
336 foreach ($this->feeds
as $cat) {
337 foreach ($cat as $feed) {
338 if ($feed['id'] == $pfeed_id) {
339 return $feed['sender'];
345 static function pfeed_to_feed_id($label) {
346 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
349 static function feed_to_pfeed_id($feed) {
350 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);
353 function add_api_method($name, $sender) {
354 if ($this->is_system($sender)) {
355 $this->api_methods
[strtolower($name)] = $sender;
359 function get_api_method($name) {
360 return $this->api_methods
[$name];