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;
32 const KIND_SYSTEM
= 2;
35 function __construct($link) {
38 $this->storage
= $_SESSION["plugin_storage"];
40 if (!$this->storage
) $this->storage
= array();
43 private function register_plugin($name, $plugin) {
44 //array_push($this->plugins, $plugin);
45 $this->plugins
[$name] = $plugin;
52 function get_plugins() {
53 return $this->plugins
;
56 function get_plugin($name) {
57 return $this->plugins
[$name];
60 function run_hooks($type, $method, $args) {
61 foreach ($this->get_hooks($type) as $hook) {
62 $hook->$method($args);
66 function add_hook($type, $sender) {
67 if (!is_array($this->hooks
[$type])) {
68 $this->hooks
[$type] = array();
71 array_push($this->hooks
[$type], $sender);
74 function del_hook($type, $sender) {
75 if (is_array($this->hooks
[$type])) {
76 $key = array_Search($this->hooks
[$type], $sender);
78 unset($this->hooks
[$type][$key]);
83 function get_hooks($type) {
84 if (isset($this->hooks
[$type])) {
85 return $this->hooks
[$type];
90 function load_all($kind, $owner_uid = false) {
91 $plugins = array_map("basename", glob("plugins/*"));
92 $this->load(join(",", $plugins), $kind, $owner_uid);
95 function load($classlist, $kind, $owner_uid = false) {
96 $plugins = explode(",", $classlist);
98 $this->owner_uid
= (int) $owner_uid;
100 foreach ($plugins as $class) {
101 $class = trim($class);
102 $class_file = strtolower(basename($class));
103 $file = dirname(__FILE__
)."/../plugins/$class_file/init.php";
105 if (!isset($this->plugins
[$class])) {
106 if (file_exists($file)) require_once $file;
108 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
109 $plugin = new $class($this);
112 case $this::KIND_SYSTEM
:
113 if ($this->is_system($plugin)) {
114 $plugin->init($this);
115 $this->register_plugin($class, $plugin);
118 case $this::KIND_USER
:
119 if (!$this->is_system($plugin)) {
120 $plugin->init($this);
121 $this->register_plugin($class, $plugin);
124 case $this::KIND_ALL
:
125 $plugin->init($this);
126 $this->register_plugin($class, $plugin);
134 function is_system($plugin) {
135 $about = $plugin->about();
140 // only system plugins are allowed to modify routing
141 function add_handler($handler, $method, $sender) {
142 $handler = str_replace("-", "_", strtolower($handler));
143 $method = strtolower($method);
145 if ($this->is_system($sender)) {
146 if (!is_array($this->handlers
[$handler])) {
147 $this->handlers
[$handler] = array();
150 $this->handlers
[$handler][$method] = $sender;
154 function del_handler($handler, $method) {
155 $handler = str_replace("-", "_", strtolower($handler));
156 $method = strtolower($method);
158 if ($this->is_system($sender)) {
159 unset($this->handlers
[$handler][$method]);
163 function lookup_handler($handler, $method) {
164 $handler = str_replace("-", "_", strtolower($handler));
165 $method = strtolower($method);
167 if (is_array($this->handlers
[$handler])) {
168 if (isset($this->handlers
[$handler]["*"])) {
169 return $this->handlers
[$handler]["*"];
171 return $this->handlers
[$handler][$method];
178 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
179 $command = str_replace("-", "_", strtolower($command));
181 $this->commands
[$command] = array("description" => $description,
183 "arghelp" => $arghelp,
187 function del_command($command) {
188 $command = "-" . strtolower($command);
190 unset($this->commands
[$command]);
193 function lookup_command($command) {
194 $command = "-" . strtolower($command);
196 if (is_array($this->commands
[$command])) {
197 return $this->commands
[$command]["class"];
205 function get_commands() {
206 return $this->commands
;
209 function run_commands($args) {
210 foreach ($this->get_commands() as $command => $data) {
211 if (isset($args[$command])) {
212 $command = str_replace("-", "", $command);
213 $data["class"]->$command($args);
218 function load_data($force = false) {
219 if ($this->owner_uid
&& (!$_SESSION["plugin_storage"] ||
$force)) {
220 $plugin = db_escape_string($this->link
, $plugin);
222 $result = db_query($this->link
, "SELECT name, content FROM ttrss_plugin_storage
223 WHERE owner_uid = '".$this->owner_uid
."'");
225 while ($line = db_fetch_assoc($result)) {
226 $this->storage
[$line["name"]] = unserialize($line["content"]);
229 $_SESSION["plugin_storage"] = $this->storage
;
233 private function save_data($plugin) {
234 if ($this->owner_uid
) {
235 $plugin = db_escape_string($this->link
, $plugin);
237 db_query($this->link
, "BEGIN");
239 $result = db_query($this->link
,"SELECT id FROM ttrss_plugin_storage WHERE
240 owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
242 if (!isset($this->storage
[$plugin]))
243 $this->storage
[$plugin] = array();
245 $content = db_escape_string($this->link
, serialize($this->storage
[$plugin]));
247 if (db_num_rows($result) != 0) {
248 db_query($this->link
, "UPDATE ttrss_plugin_storage SET content = '$content'
249 WHERE owner_uid= '".$this->owner_uid
."' AND name = '$plugin'");
252 db_query($this->link
, "INSERT INTO ttrss_plugin_storage
253 (name,owner_uid,content) VALUES
254 ('$plugin','".$this->owner_uid
."','$content')");
257 db_query($this->link
, "COMMIT");
261 function set($sender, $name, $value, $sync = true) {
262 $idx = get_class($sender);
264 if (!isset($this->storage
[$idx]))
265 $this->storage
[$idx] = array();
267 $this->storage
[$idx][$name] = $value;
269 $_SESSION["plugin_storage"] = $this->storage
;
271 if ($sync) $this->save_data(get_class($sender));
274 function get($sender, $name, $default_value = false) {
275 $idx = get_class($sender);
277 if (isset($this->storage
[$idx][$name])) {
278 return $this->storage
[$idx][$name];
280 return $default_value;
284 function get_all($sender) {
285 $idx = get_class($sender);
287 return $this->storage
[$idx];
290 function clear_data($sender) {
291 if ($this->owner_uid
) {
292 $idx = get_class($sender);
294 unset($this->storage
[$idx]);
296 db_query($this->link
, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
297 AND owner_uid = " . $this->owner_uid
);
299 $_SESSION["plugin_storage"] = $this->storage
;
303 function set_debug($debug) {
304 $this->debug
= $debug;
307 function get_debug() {
311 // Plugin feed functions are *EXPERIMENTAL*!
313 // cat_id: only -1 is supported (Special)
314 function add_feed($cat_id, $title, $icon, $sender) {
315 if (!$this->feeds
[$cat_id]) $this->feeds
[$cat_id] = array();
317 $id = count($this->feeds
[$cat_id]);
319 array_push($this->feeds
[$cat_id],
320 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
325 function get_feeds($cat_id) {
326 return $this->feeds
[$cat_id];
329 // convert feed_id (e.g. -129) to pfeed_id first
330 function get_feed_handler($pfeed_id) {
331 foreach ($this->feeds
as $cat) {
332 foreach ($cat as $feed) {
333 if ($feed['id'] == $pfeed_id) {
334 return $feed['sender'];
340 static function pfeed_to_feed_id($label) {
341 return PLUGIN_FEED_BASE_INDEX
- 1 - abs($label);
344 static function feed_to_pfeed_id($feed) {
345 return PLUGIN_FEED_BASE_INDEX
- 1 +
abs($feed);