4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
8 private $storage = array();
12 const HOOK_ARTICLE_BUTTON = 1;
13 const HOOK_ARTICLE_FILTER = 2;
14 const HOOK_PREFS_TAB = 3;
15 const HOOK_PREFS_TAB_SECTION = 4;
16 const HOOK_PREFS_TABS = 5;
17 const HOOK_FEED_PARSED = 6;
18 const HOOK_UPDATE_TASK = 7;
19 const HOOK_AUTH_USER = 8;
20 const HOOK_HOTKEY_MAP = 9;
21 const HOOK_RENDER_ARTICLE = 10;
22 const HOOK_RENDER_ARTICLE_CDM = 11;
23 const HOOK_FEED_FETCHED = 12;
24 const HOOK_SANITIZE = 13;
27 const KIND_SYSTEM = 2;
30 function __construct($link) {
33 $this->storage = $_SESSION["plugin_storage"];
35 if (!$this->storage) $this->storage = array();
38 private function register_plugin($name, $plugin) {
39 //array_push($this->plugins, $plugin);
40 $this->plugins[$name] = $plugin;
47 function get_plugins() {
48 return $this->plugins;
51 function get_plugin($name) {
52 return $this->plugins[$name];
55 function run_hooks($type, $method, $args) {
56 foreach ($this->get_hooks($type) as $hook) {
57 $hook->$method($args);
61 function add_hook($type, $sender) {
62 if (!is_array($this->hooks[$type])) {
63 $this->hooks[$type] = array();
66 array_push($this->hooks[$type], $sender);
69 function del_hook($type, $sender) {
70 if (is_array($this->hooks[$type])) {
71 $key = array_Search($this->hooks[$type], $sender);
73 unset($this->hooks[$type][$key]);
78 function get_hooks($type) {
79 if (isset($this->hooks[$type])) {
80 return $this->hooks[$type];
85 function load_all($kind, $owner_uid = false) {
86 $plugins = array_map("basename", glob("plugins/*"));
87 $this->load(join(",", $plugins), $kind, $owner_uid);
90 function load($classlist, $kind, $owner_uid = false) {
91 $plugins = explode(",", $classlist);
93 $this->owner_uid = (int) $owner_uid;
95 foreach ($plugins as $class) {
96 $class = trim($class);
97 $class_file = strtolower(basename($class));
98 $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
100 if (!isset($this->plugins[$class])) {
101 if (file_exists($file)) require_once $file;
103 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
104 $plugin = new $class($this);
107 case $this::KIND_SYSTEM:
108 if ($this->is_system($plugin)) {
109 $plugin->init($this);
110 $this->register_plugin($class, $plugin);
113 case $this::KIND_USER:
114 if (!$this->is_system($plugin)) {
115 $plugin->init($this);
116 $this->register_plugin($class, $plugin);
119 case $this::KIND_ALL:
120 $plugin->init($this);
121 $this->register_plugin($class, $plugin);
129 function is_system($plugin) {
130 $about = $plugin->about();
135 // only system plugins are allowed to modify routing
136 function add_handler($handler, $method, $sender) {
137 $handler = str_replace("-", "_", strtolower($handler));
138 $method = strtolower($method);
140 if ($this->is_system($sender)) {
141 if (!is_array($this->handlers[$handler])) {
142 $this->handlers[$handler] = array();
145 $this->handlers[$handler][$method] = $sender;
149 function del_handler($handler, $method) {
150 $handler = str_replace("-", "_", strtolower($handler));
151 $method = strtolower($method);
153 if ($this->is_system($sender)) {
154 unset($this->handlers[$handler][$method]);
158 function lookup_handler($handler, $method) {
159 $handler = str_replace("-", "_", strtolower($handler));
160 $method = strtolower($method);
162 if (is_array($this->handlers[$handler])) {
163 if (isset($this->handlers[$handler]["*"])) {
164 return $this->handlers[$handler]["*"];
166 return $this->handlers[$handler][$method];
173 function add_command($command, $description, $sender) {
174 $command = str_replace("-", "_", strtolower($command));
176 $this->commands[$command] = array("description" => $description,
180 function del_command($command) {
181 $command = "-" . strtolower($command);
183 unset($this->commands[$command]);
186 function lookup_command($command) {
187 $command = "-" . strtolower($command);
189 if (is_array($this->commands[$command])) {
190 return $this->commands[$command]["class"];
198 function get_commands() {
199 return $this->commands;
202 function run_commands($args) {
203 foreach ($this->get_commands() as $command => $data) {
204 if (isset($args[$command])) {
205 $command = str_replace("-", "", $command);
206 $data["class"]->$command($args);
211 function load_data($force = false) {
212 if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) {
213 $plugin = db_escape_string($plugin);
215 $result = db_query($this->link, "SELECT name, content FROM ttrss_plugin_storage
216 WHERE owner_uid = '".$this->owner_uid."'");
218 while ($line = db_fetch_assoc($result)) {
219 $this->storage[$line["name"]] = unserialize($line["content"]);
222 $_SESSION["plugin_storage"] = $this->storage;
226 private function save_data($plugin) {
227 if ($this->owner_uid) {
228 $plugin = db_escape_string($plugin);
230 db_query($this->link, "BEGIN");
232 $result = db_query($this->link,"SELECT id FROM ttrss_plugin_storage WHERE
233 owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
235 if (!isset($this->storage[$plugin]))
236 $this->storage[$plugin] = array();
238 $content = db_escape_string(serialize($this->storage[$plugin]));
240 if (db_num_rows($result) != 0) {
241 db_query($this->link, "UPDATE ttrss_plugin_storage SET content = '$content'
242 WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
245 db_query($this->link, "INSERT INTO ttrss_plugin_storage
246 (name,owner_uid,content) VALUES
247 ('$plugin','".$this->owner_uid."','$content')");
250 db_query($this->link, "COMMIT");
254 function set($sender, $name, $value, $sync = true) {
255 $idx = get_class($sender);
257 if (!isset($this->storage[$idx]))
258 $this->storage[$idx] = array();
260 $this->storage[$idx][$name] = $value;
262 $_SESSION["plugin_storage"] = $this->storage;
264 if ($sync) $this->save_data(get_class($sender));
267 function get($sender, $name, $default_value = false) {
268 $idx = get_class($sender);
270 if (isset($this->storage[$idx][$name])) {
271 return $this->storage[$idx][$name];
273 return $default_value;
277 function get_all($sender) {
278 $idx = get_class($sender);
280 return $this->storage[$idx];
283 function clear_data($sender) {
284 if ($this->owner_uid) {
285 $idx = get_class($sender);
287 unset($this->storage[$idx]);
289 db_query($this->link, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
290 AND owner_uid = " . $this->owner_uid);
292 $_SESSION["plugin_storage"] = $this->storage;
296 function set_debug($debug) {
297 $this->debug = $debug;
300 function get_debug() {