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