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