]> git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
do not try to load plugins which aren't directories
[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
107 if (!is_dir(dirname(__FILE__)."/../plugins/$class_file")) continue;
108
109 $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
110
111 if (!isset($this->plugins[$class])) {
112 if (file_exists($file)) require_once $file;
113
114 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
115 $plugin = new $class($this);
116
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 }
135 }
136 }
137 }
138 }
139
140 function is_system($plugin) {
141 $about = $plugin->about();
142
143 return @$about[3];
144 }
145
146 // only system plugins are allowed to modify routing
147 function add_handler($handler, $method, $sender) {
148 $handler = str_replace("-", "_", strtolower($handler));
149 $method = strtolower($method);
150
151 if ($this->is_system($sender)) {
152 if (!is_array($this->handlers[$handler])) {
153 $this->handlers[$handler] = array();
154 }
155
156 $this->handlers[$handler][$method] = $sender;
157 }
158 }
159
160 function del_handler($handler, $method) {
161 $handler = str_replace("-", "_", strtolower($handler));
162 $method = strtolower($method);
163
164 if ($this->is_system($sender)) {
165 unset($this->handlers[$handler][$method]);
166 }
167 }
168
169 function lookup_handler($handler, $method) {
170 $handler = str_replace("-", "_", strtolower($handler));
171 $method = strtolower($method);
172
173 if (is_array($this->handlers[$handler])) {
174 if (isset($this->handlers[$handler]["*"])) {
175 return $this->handlers[$handler]["*"];
176 } else {
177 return $this->handlers[$handler][$method];
178 }
179 }
180
181 return false;
182 }
183
184 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
185 $command = str_replace("-", "_", strtolower($command));
186
187 $this->commands[$command] = array("description" => $description,
188 "suffix" => $suffix,
189 "arghelp" => $arghelp,
190 "class" => $sender);
191 }
192
193 function del_command($command) {
194 $command = "-" . strtolower($command);
195
196 unset($this->commands[$command]);
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) {
217 if (isset($args[$command])) {
218 $command = str_replace("-", "", $command);
219 $data["class"]->$command($args);
220 }
221 }
222 }
223
224 function load_data($force = false) {
225 if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) {
226 $plugin = db_escape_string($this->link, $plugin);
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) {
241 $plugin = db_escape_string($this->link, $plugin);
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
251 $content = db_escape_string($this->link, serialize($this->storage[$plugin]));
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
280 function get($sender, $name, $default_value = false) {
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 }
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 }
308
309 function set_debug($debug) {
310 $this->debug = $debug;
311 }
312
313 function get_debug() {
314 return $this->debug;
315 }
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
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 }
363 }
364 ?>