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