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