]> git.wh0rd.org Git - tt-rss.git/blob - classes/pluginhost.php
rework update.php to use getopt; allow --task parameter
[tt-rss.git] / classes / pluginhost.php
1 <?php
2 class PluginHost {
3         private $link;
4         private $hooks = array();
5         private $plugins = array();
6         private $handlers = array();
7         private $commands = array();
8         private $storage = array();
9         private $owner_uid;
10         private $debug;
11
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;
25
26         const KIND_ALL = 1;
27         const KIND_SYSTEM = 2;
28         const KIND_USER = 3;
29
30         function __construct($link) {
31                 $this->link = $link;
32
33                 $this->storage = $_SESSION["plugin_storage"];
34
35                 if (!$this->storage) $this->storage = array();
36         }
37
38         private function register_plugin($name, $plugin) {
39                 //array_push($this->plugins, $plugin);
40                 $this->plugins[$name] = $plugin;
41         }
42
43         function get_link() {
44                 return $this->link;
45         }
46
47         function get_plugins() {
48                 return $this->plugins;
49         }
50
51         function get_plugin($name) {
52                 return $this->plugins[$name];
53         }
54
55         function run_hooks($type, $method, $args) {
56                 foreach ($this->get_hooks($type) as $hook) {
57                         $hook->$method($args);
58                 }
59         }
60
61         function add_hook($type, $sender) {
62                 if (!is_array($this->hooks[$type])) {
63                         $this->hooks[$type] = array();
64                 }
65
66                 array_push($this->hooks[$type], $sender);
67         }
68
69         function del_hook($type, $sender) {
70                 if (is_array($this->hooks[$type])) {
71                         $key = array_Search($this->hooks[$type], $sender);
72                         if ($key !== FALSE) {
73                                 unset($this->hooks[$type][$key]);
74                         }
75                 }
76         }
77
78         function get_hooks($type) {
79                 if (isset($this->hooks[$type])) {
80                         return $this->hooks[$type];
81                 } else {
82                         return array();
83                 }
84         }
85         function load_all($kind, $owner_uid = false) {
86                 $plugins = array_map("basename", glob("plugins/*"));
87                 $this->load(join(",", $plugins), $kind, $owner_uid);
88         }
89
90         function load($classlist, $kind, $owner_uid = false) {
91                 $plugins = explode(",", $classlist);
92
93                 $this->owner_uid = (int) $owner_uid;
94
95                 foreach ($plugins as $class) {
96                         $class = trim($class);
97                         $class_file = strtolower(basename($class));
98                         $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
99
100                         if (!isset($this->plugins[$class])) {
101                                 if (file_exists($file)) require_once $file;
102
103                                 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
104                                         $plugin = new $class($this);
105
106                                         switch ($kind) {
107                                         case $this::KIND_SYSTEM:
108                                                 if ($this->is_system($plugin)) {
109                                                         $plugin->init($this);
110                                                         $this->register_plugin($class, $plugin);
111                                                 }
112                                                 break;
113                                         case $this::KIND_USER:
114                                                 if (!$this->is_system($plugin)) {
115                                                         $plugin->init($this);
116                                                         $this->register_plugin($class, $plugin);
117                                                 }
118                                                 break;
119                                         case $this::KIND_ALL:
120                                                 $plugin->init($this);
121                                                 $this->register_plugin($class, $plugin);
122                                                 break;
123                                         }
124                                 }
125                         }
126                 }
127         }
128
129         function is_system($plugin) {
130                 $about = $plugin->about();
131
132                 return @$about[3];
133         }
134
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);
139
140                 if ($this->is_system($sender)) {
141                         if (!is_array($this->handlers[$handler])) {
142                                 $this->handlers[$handler] = array();
143                         }
144
145                         $this->handlers[$handler][$method] = $sender;
146                 }
147         }
148
149         function del_handler($handler, $method) {
150                 $handler = str_replace("-", "_", strtolower($handler));
151                 $method = strtolower($method);
152
153                 if ($this->is_system($sender)) {
154                         unset($this->handlers[$handler][$method]);
155                 }
156         }
157
158         function lookup_handler($handler, $method) {
159                 $handler = str_replace("-", "_", strtolower($handler));
160                 $method = strtolower($method);
161
162                 if (is_array($this->handlers[$handler])) {
163                         if (isset($this->handlers[$handler]["*"])) {
164                                 return $this->handlers[$handler]["*"];
165                         } else {
166                                 return $this->handlers[$handler][$method];
167                         }
168                 }
169
170                 return false;
171         }
172
173         function add_command($command, $description, $sender) {
174                 $command = str_replace("-", "_", strtolower($command));
175
176                 $this->commands[$command] = array("description" => $description,
177                         "class" => $sender);
178         }
179
180         function del_command($command) {
181                 $command = "-" . strtolower($command);
182
183                 unset($this->commands[$command]);
184         }
185
186         function lookup_command($command) {
187                 $command = "-" . strtolower($command);
188
189                 if (is_array($this->commands[$command])) {
190                         return $this->commands[$command]["class"];
191                 } else {
192                         return false;
193                 }
194
195                 return false;
196         }
197
198         function get_commands() {
199                 return $this->commands;
200         }
201
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);
207                         }
208                 }
209         }
210
211         function load_data($force = false) {
212                 if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force))  {
213                         $plugin = db_escape_string($plugin);
214
215                         $result = db_query($this->link, "SELECT name, content FROM ttrss_plugin_storage
216                                 WHERE owner_uid = '".$this->owner_uid."'");
217
218                         while ($line = db_fetch_assoc($result)) {
219                                 $this->storage[$line["name"]] = unserialize($line["content"]);
220                         }
221
222                         $_SESSION["plugin_storage"] = $this->storage;
223                 }
224         }
225
226         private function save_data($plugin) {
227                 if ($this->owner_uid) {
228                         $plugin = db_escape_string($plugin);
229
230                         db_query($this->link, "BEGIN");
231
232                         $result = db_query($this->link,"SELECT id FROM ttrss_plugin_storage WHERE
233                                 owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
234
235                         if (!isset($this->storage[$plugin]))
236                                 $this->storage[$plugin] = array();
237
238                         $content = db_escape_string(serialize($this->storage[$plugin]));
239
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'");
243
244                         } else {
245                                 db_query($this->link, "INSERT INTO ttrss_plugin_storage
246                                         (name,owner_uid,content) VALUES
247                                         ('$plugin','".$this->owner_uid."','$content')");
248                         }
249
250                         db_query($this->link, "COMMIT");
251                 }
252         }
253
254         function set($sender, $name, $value, $sync = true) {
255                 $idx = get_class($sender);
256
257                 if (!isset($this->storage[$idx]))
258                         $this->storage[$idx] = array();
259
260                 $this->storage[$idx][$name] = $value;
261
262                 $_SESSION["plugin_storage"] = $this->storage;
263
264                 if ($sync) $this->save_data(get_class($sender));
265         }
266
267         function get($sender, $name, $default_value = false) {
268                 $idx = get_class($sender);
269
270                 if (isset($this->storage[$idx][$name])) {
271                         return $this->storage[$idx][$name];
272                 } else {
273                         return $default_value;
274                 }
275         }
276
277         function get_all($sender) {
278                 $idx = get_class($sender);
279
280                 return $this->storage[$idx];
281         }
282
283         function clear_data($sender) {
284                 if ($this->owner_uid) {
285                         $idx = get_class($sender);
286
287                         unset($this->storage[$idx]);
288
289                         db_query($this->link, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
290                                 AND owner_uid = " . $this->owner_uid);
291
292                         $_SESSION["plugin_storage"] = $this->storage;
293                 }
294         }
295
296         function set_debug($debug) {
297                 $this->debug = $debug;
298         }
299
300         function get_debug() {
301                 return $this->debug;
302         }
303 }
304 ?>