]> git.wh0rd.org Git - tt-rss.git/blob - classes/pluginhost.php
pluginhost: allow loading user plugins from plugins.local
[tt-rss.git] / classes / pluginhost.php
1 <?php
2 class PluginHost {
3         private $dbh;
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();
11         private $owner_uid;
12         private $debug;
13         private $last_registered;
14         private static $instance;
15
16         const API_VERSION = 2;
17
18         const HOOK_ARTICLE_BUTTON = 1;
19         const HOOK_ARTICLE_FILTER = 2;
20         const HOOK_PREFS_TAB = 3;
21         const HOOK_PREFS_TAB_SECTION = 4;
22         const HOOK_PREFS_TABS = 5;
23         const HOOK_FEED_PARSED = 6;
24         const HOOK_UPDATE_TASK = 7;
25         const HOOK_AUTH_USER = 8;
26         const HOOK_HOTKEY_MAP = 9;
27         const HOOK_RENDER_ARTICLE = 10;
28         const HOOK_RENDER_ARTICLE_CDM = 11;
29         const HOOK_FEED_FETCHED = 12;
30         const HOOK_SANITIZE = 13;
31         const HOOK_RENDER_ARTICLE_API = 14;
32         const HOOK_TOOLBAR_BUTTON = 15;
33         const HOOK_ACTION_ITEM = 16;
34         const HOOK_HEADLINE_TOOLBAR_BUTTON = 17;
35         const HOOK_HOTKEY_INFO = 18;
36         const HOOK_ARTICLE_LEFT_BUTTON = 19;
37         const HOOK_PREFS_EDIT_FEED = 20;
38         const HOOK_PREFS_SAVE_FEED = 21;
39         const HOOK_FETCH_FEED = 22;
40         const HOOK_QUERY_HEADLINES = 23;
41         const HOOK_HOUSE_KEEPING = 24;
42         const HOOK_SEARCH = 25;
43         const HOOK_FORMAT_ENCLOSURES = 26;
44         const HOOK_SUBSCRIBE_FEED = 27;
45         const HOOK_HEADLINES_BEFORE = 28;
46         const HOOK_RENDER_ENCLOSURE = 29;
47
48         const KIND_ALL = 1;
49         const KIND_SYSTEM = 2;
50         const KIND_USER = 3;
51
52         function __construct() {
53                 $this->dbh = Db::get();
54
55                 $this->storage = array();
56         }
57
58         private function __clone() {
59                 //
60         }
61
62         public static function getInstance() {
63                 if (self::$instance == null)
64                         self::$instance = new self();
65
66                 return self::$instance;
67         }
68
69         private function register_plugin($name, $plugin) {
70                 //array_push($this->plugins, $plugin);
71                 $this->plugins[$name] = $plugin;
72         }
73
74         // needed for compatibility with API 1
75         function get_link() {
76                 return false;
77         }
78
79         function get_dbh() {
80                 return $this->dbh;
81         }
82
83         function get_plugin_names() {
84                 $names = array();
85
86                 foreach ($this->plugins as $p) {
87                         array_push($names, get_class($p));
88                 }
89
90                 return $names;
91         }
92
93         function get_plugins() {
94                 return $this->plugins;
95         }
96
97         function get_plugin($name) {
98                 return $this->plugins[$name];
99         }
100
101         function run_hooks($type, $method, $args) {
102                 foreach ($this->get_hooks($type) as $hook) {
103                         $hook->$method($args);
104                 }
105         }
106
107         function add_hook($type, $sender) {
108                 if (!is_array($this->hooks[$type])) {
109                         $this->hooks[$type] = array();
110                 }
111
112                 array_push($this->hooks[$type], $sender);
113         }
114
115         function del_hook($type, $sender) {
116                 if (is_array($this->hooks[$type])) {
117                         $key = array_Search($sender, $this->hooks[$type]);
118                         if ($key !== FALSE) {
119                                 unset($this->hooks[$type][$key]);
120                         }
121                 }
122         }
123
124         function get_hooks($type) {
125                 if (isset($this->hooks[$type])) {
126                         return $this->hooks[$type];
127                 } else {
128                         return array();
129                 }
130         }
131         function load_all($kind, $owner_uid = false) {
132                 $plugins = array_map("basename", array_filter(glob("plugins/*"), "is_dir"));
133
134                 if (is_dir("plugins.local")) {
135                         $plugins = array_merge($plugins, array_map("basename",
136                                 array_filter(glob("plugins.local/*"), "is_dir")));
137                 }
138
139                 asort($plugins);
140
141                 $this->load(join(",", $plugins), $kind, $owner_uid);
142         }
143
144         function load($classlist, $kind, $owner_uid = false) {
145                 $plugins = explode(",", $classlist);
146
147                 $this->owner_uid = (int) $owner_uid;
148
149                 foreach ($plugins as $class) {
150                         $class = trim($class);
151                         $class_file = strtolower(basename($class));
152
153                         if (!is_dir(__DIR__."/../plugins/$class_file") &&
154                                         !is_dir(__DIR__."/../plugins.local/$class_file")) continue;
155
156                         // try system plugin directory first
157                         $file = __DIR__ . "/../plugins/$class_file/init.php";
158
159                         if (!file_exists($file)) {
160                                 $file = __DIR__ . "/../plugins.local/$class_file/init.php";
161                         }
162
163                         if (!isset($this->plugins[$class])) {
164                                 if (file_exists($file)) require_once $file;
165
166                                 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
167                                         $plugin = new $class($this);
168
169                                         $plugin_api = $plugin->api_version();
170
171                                         if ($plugin_api < PluginHost::API_VERSION) {
172                                                 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost::API_VERSION . ", got: $plugin_api)", E_USER_WARNING);
173                                                 continue;
174                                         }
175
176                                         $this->last_registered = $class;
177
178                                         switch ($kind) {
179                                         case $this::KIND_SYSTEM:
180                                                 if ($this->is_system($plugin)) {
181                                                         $plugin->init($this);
182                                                         $this->register_plugin($class, $plugin);
183                                                 }
184                                                 break;
185                                         case $this::KIND_USER:
186                                                 if (!$this->is_system($plugin)) {
187                                                         $plugin->init($this);
188                                                         $this->register_plugin($class, $plugin);
189                                                 }
190                                                 break;
191                                         case $this::KIND_ALL:
192                                                 $plugin->init($this);
193                                                 $this->register_plugin($class, $plugin);
194                                                 break;
195                                         }
196                                 }
197                         }
198                 }
199         }
200
201         function is_system($plugin) {
202                 $about = $plugin->about();
203
204                 return @$about[3];
205         }
206
207         // only system plugins are allowed to modify routing
208         function add_handler($handler, $method, $sender) {
209                 $handler = str_replace("-", "_", strtolower($handler));
210                 $method = strtolower($method);
211
212                 if ($this->is_system($sender)) {
213                         if (!is_array($this->handlers[$handler])) {
214                                 $this->handlers[$handler] = array();
215                         }
216
217                         $this->handlers[$handler][$method] = $sender;
218                 }
219         }
220
221         function del_handler($handler, $method, $sender) {
222                 $handler = str_replace("-", "_", strtolower($handler));
223                 $method = strtolower($method);
224
225                 if ($this->is_system($sender)) {
226                         unset($this->handlers[$handler][$method]);
227                 }
228         }
229
230         function lookup_handler($handler, $method) {
231                 $handler = str_replace("-", "_", strtolower($handler));
232                 $method = strtolower($method);
233
234                 if (is_array($this->handlers[$handler])) {
235                         if (isset($this->handlers[$handler]["*"])) {
236                                 return $this->handlers[$handler]["*"];
237                         } else {
238                                 return $this->handlers[$handler][$method];
239                         }
240                 }
241
242                 return false;
243         }
244
245         function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
246                 $command = str_replace("-", "_", strtolower($command));
247
248                 $this->commands[$command] = array("description" => $description,
249                         "suffix" => $suffix,
250                         "arghelp" => $arghelp,
251                         "class" => $sender);
252         }
253
254         function del_command($command) {
255                 $command = "-" . strtolower($command);
256
257                 unset($this->commands[$command]);
258         }
259
260         function lookup_command($command) {
261                 $command = "-" . strtolower($command);
262
263                 if (is_array($this->commands[$command])) {
264                         return $this->commands[$command]["class"];
265                 } else {
266                         return false;
267                 }
268
269                 return false;
270         }
271
272         function get_commands() {
273                 return $this->commands;
274         }
275
276         function run_commands($args) {
277                 foreach ($this->get_commands() as $command => $data) {
278                         if (isset($args[$command])) {
279                                 $command = str_replace("-", "", $command);
280                                 $data["class"]->$command($args);
281                         }
282                 }
283         }
284
285         function load_data($force = false) {
286                 if ($this->owner_uid)  {
287                         $result = $this->dbh->query("SELECT name, content FROM ttrss_plugin_storage
288                                 WHERE owner_uid = '".$this->owner_uid."'");
289
290                         while ($line = $this->dbh->fetch_assoc($result)) {
291                                 $this->storage[$line["name"]] = unserialize($line["content"]);
292                         }
293                 }
294         }
295
296         private function save_data($plugin) {
297                 if ($this->owner_uid) {
298                         $plugin = $this->dbh->escape_string($plugin);
299
300                         $this->dbh->query("BEGIN");
301
302                         $result = $this->dbh->query("SELECT id FROM ttrss_plugin_storage WHERE
303                                 owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
304
305                         if (!isset($this->storage[$plugin]))
306                                 $this->storage[$plugin] = array();
307
308                         $content = $this->dbh->escape_string(serialize($this->storage[$plugin]),
309                                 false);
310
311                         if ($this->dbh->num_rows($result) != 0) {
312                                 $this->dbh->query("UPDATE ttrss_plugin_storage SET content = '$content'
313                                         WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
314
315                         } else {
316                                 $this->dbh->query("INSERT INTO ttrss_plugin_storage
317                                         (name,owner_uid,content) VALUES
318                                         ('$plugin','".$this->owner_uid."','$content')");
319                         }
320
321                         $this->dbh->query("COMMIT");
322                 }
323         }
324
325         function set($sender, $name, $value, $sync = true) {
326                 $idx = get_class($sender);
327
328                 if (!isset($this->storage[$idx]))
329                         $this->storage[$idx] = array();
330
331                 $this->storage[$idx][$name] = $value;
332
333                 if ($sync) $this->save_data(get_class($sender));
334         }
335
336         function get($sender, $name, $default_value = false) {
337                 $idx = get_class($sender);
338
339                 if (isset($this->storage[$idx][$name])) {
340                         return $this->storage[$idx][$name];
341                 } else {
342                         return $default_value;
343                 }
344         }
345
346         function get_all($sender) {
347                 $idx = get_class($sender);
348
349                 return $this->storage[$idx];
350         }
351
352         function clear_data($sender) {
353                 if ($this->owner_uid) {
354                         $idx = get_class($sender);
355
356                         unset($this->storage[$idx]);
357
358                         $this->dbh->query("DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
359                                 AND owner_uid = " . $this->owner_uid);
360                 }
361         }
362
363         function set_debug($debug) {
364                 $this->debug = $debug;
365         }
366
367         function get_debug() {
368                 return $this->debug;
369         }
370
371         // Plugin feed functions are *EXPERIMENTAL*!
372
373         // cat_id: only -1 is supported (Special)
374         function add_feed($cat_id, $title, $icon, $sender) {
375                 if (!$this->feeds[$cat_id]) $this->feeds[$cat_id] = array();
376
377                 $id = count($this->feeds[$cat_id]);
378
379                 array_push($this->feeds[$cat_id],
380                         array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
381
382                 return $id;
383         }
384
385         function get_feeds($cat_id) {
386                 return $this->feeds[$cat_id];
387         }
388
389         // convert feed_id (e.g. -129) to pfeed_id first
390         function get_feed_handler($pfeed_id) {
391                 foreach ($this->feeds as $cat) {
392                         foreach ($cat as $feed) {
393                                 if ($feed['id'] == $pfeed_id) {
394                                         return $feed['sender'];
395                                 }
396                         }
397                 }
398         }
399
400         static function pfeed_to_feed_id($label) {
401                 return PLUGIN_FEED_BASE_INDEX - 1 - abs($label);
402         }
403
404         static function feed_to_pfeed_id($feed) {
405                 return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed);
406         }
407
408         function add_api_method($name, $sender) {
409                 if ($this->is_system($sender)) {
410                         $this->api_methods[strtolower($name)] = $sender;
411                 }
412         }
413
414         function get_api_method($name) {
415                 return $this->api_methods[$name];
416         }
417 }
418 ?>