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