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