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