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