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