]> git.wh0rd.org - tt-rss.git/blame - classes/pluginhost.php
Merge pull request #340 from dzaikos/master
[tt-rss.git] / classes / pluginhost.php
CommitLineData
19c73507
AD
1<?php
2class PluginHost {
6322ac79 3 private $dbh;
19c73507
AD
4 private $hooks = array();
5 private $plugins = array();
8dcb2b47 6 private $handlers = array();
73f28fe9 7 private $commands = array();
d8a1d2a2 8 private $storage = array();
a413f53e 9 private $feeds = array();
79f9bef7 10 private $api_methods = array();
d8a1d2a2 11 private $owner_uid;
be178857 12 private $debug;
726bd48e 13 private $last_registered;
1ffe3391 14 private static $instance;
19c73507 15
106a3de9 16 const API_VERSION = 2;
ddf28801 17
19c73507
AD
18 const HOOK_ARTICLE_BUTTON = 1;
19 const HOOK_ARTICLE_FILTER = 2;
6065f3ad 20 const HOOK_PREFS_TAB = 3;
699daf58 21 const HOOK_PREFS_TAB_SECTION = 4;
6cbe53c9 22 const HOOK_PREFS_TABS = 5;
4412b877 23 const HOOK_FEED_PARSED = 6;
41b82aa4 24 const HOOK_UPDATE_TASK = 7;
0f28f81f 25 const HOOK_AUTH_USER = 8;
e218c5f5 26 const HOOK_HOTKEY_MAP = 9;
84d952f1
AD
27 const HOOK_RENDER_ARTICLE = 10;
28 const HOOK_RENDER_ARTICLE_CDM = 11;
017401dd 29 const HOOK_FEED_FETCHED = 12;
e9b86f0a 30 const HOOK_SANITIZE = 13;
b6604c96 31 const HOOK_RENDER_ARTICLE_API = 14;
ceb78471
AD
32 const HOOK_TOOLBAR_BUTTON = 15;
33 const HOOK_ACTION_ITEM = 16;
34 const HOOK_HEADLINE_TOOLBAR_BUTTON = 17;
47854200 35 const HOOK_HOTKEY_INFO = 18;
ccb2b8dd 36 const HOOK_ARTICLE_LEFT_BUTTON = 19;
057177eb 37 const HOOK_PREFS_EDIT_FEED = 20;
8cefe38a 38 const HOOK_PREFS_SAVE_FEED = 21;
ee65bef4 39 const HOOK_FETCH_FEED = 22;
891e36f5 40 const HOOK_QUERY_HEADLINES = 23;
8e470220 41 const HOOK_HOUSE_KEEPING = 24;
19c73507 42
d2a421e3
AD
43 const KIND_ALL = 1;
44 const KIND_SYSTEM = 2;
45 const KIND_USER = 3;
46
48ed517e 47 function __construct() {
1ffe3391 48 $this->dbh = Db::get();
d8a1d2a2 49
d48398e6 50 $this->storage = array();
19c73507
AD
51 }
52
1ffe3391
AD
53 private function __clone() {
54 //
55 }
56
57 public static function getInstance() {
58 if (self::$instance == null)
59 self::$instance = new self();
60
61 return self::$instance;
62 }
63
19c73507
AD
64 private function register_plugin($name, $plugin) {
65 //array_push($this->plugins, $plugin);
66 $this->plugins[$name] = $plugin;
67 }
68
106a3de9 69 // needed for compatibility with API 1
726bd48e 70 function get_link() {
106a3de9 71 return false;
726bd48e
AD
72 }
73
6322ac79
AD
74 function get_dbh() {
75 return $this->dbh;
19c73507
AD
76 }
77
78 function get_plugins() {
79 return $this->plugins;
80 }
81
82 function get_plugin($name) {
83 return $this->plugins[$name];
84 }
85
6065f3ad
AD
86 function run_hooks($type, $method, $args) {
87 foreach ($this->get_hooks($type) as $hook) {
88 $hook->$method($args);
89 }
90 }
91
19c73507
AD
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) {
19b3992b
AD
110 if (isset($this->hooks[$type])) {
111 return $this->hooks[$type];
112 } else {
113 return array();
114 }
19c73507 115 }
5d9abb1e 116 function load_all($kind, $owner_uid = false) {
7a866114 117 $plugins = array_map("basename", glob("plugins/*"));
5d9abb1e 118 $this->load(join(",", $plugins), $kind, $owner_uid);
7a866114 119 }
19c73507 120
d8a1d2a2 121 function load($classlist, $kind, $owner_uid = false) {
19c73507
AD
122 $plugins = explode(",", $classlist);
123
d8a1d2a2
AD
124 $this->owner_uid = (int) $owner_uid;
125
19c73507
AD
126 foreach ($plugins as $class) {
127 $class = trim($class);
8dcb2b47 128 $class_file = strtolower(basename($class));
b8c7f835
AD
129
130 if (!is_dir(dirname(__FILE__)."/../plugins/$class_file")) continue;
131
e938b1de 132 $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
19c73507 133
de612e7a
AD
134 if (!isset($this->plugins[$class])) {
135 if (file_exists($file)) require_once $file;
19c73507 136
de612e7a
AD
137 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
138 $plugin = new $class($this);
19c73507 139
ddf28801
AD
140 $plugin_api = $plugin->api_version();
141
142 if ($plugin_api < PluginHost::API_VERSION) {
143 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost::API_VERSION . ", got: $plugin_api)", E_USER_WARNING);
144 continue;
145 }
146
726bd48e
AD
147 $this->last_registered = $class;
148
d2a421e3
AD
149 switch ($kind) {
150 case $this::KIND_SYSTEM:
151 if ($this->is_system($plugin)) {
152 $plugin->init($this);
153 $this->register_plugin($class, $plugin);
154 }
155 break;
156 case $this::KIND_USER:
157 if (!$this->is_system($plugin)) {
158 $plugin->init($this);
159 $this->register_plugin($class, $plugin);
160 }
161 break;
162 case $this::KIND_ALL:
163 $plugin->init($this);
164 $this->register_plugin($class, $plugin);
165 break;
166 }
de612e7a 167 }
19c73507
AD
168 }
169 }
170 }
171
de612e7a 172 function is_system($plugin) {
d2a421e3 173 $about = $plugin->about();
de612e7a
AD
174
175 return @$about[3];
176 }
177
178 // only system plugins are allowed to modify routing
8dcb2b47 179 function add_handler($handler, $method, $sender) {
6cbe53c9 180 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
181 $method = strtolower($method);
182
de612e7a
AD
183 if ($this->is_system($sender)) {
184 if (!is_array($this->handlers[$handler])) {
185 $this->handlers[$handler] = array();
186 }
8dcb2b47 187
de612e7a
AD
188 $this->handlers[$handler][$method] = $sender;
189 }
8dcb2b47
AD
190 }
191
6f7798b6 192 function del_handler($handler, $method, $sender) {
6cbe53c9 193 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
194 $method = strtolower($method);
195
de612e7a
AD
196 if ($this->is_system($sender)) {
197 unset($this->handlers[$handler][$method]);
198 }
8dcb2b47
AD
199 }
200
201 function lookup_handler($handler, $method) {
6cbe53c9 202 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
203 $method = strtolower($method);
204
205 if (is_array($this->handlers[$handler])) {
6cbe53c9
AD
206 if (isset($this->handlers[$handler]["*"])) {
207 return $this->handlers[$handler]["*"];
208 } else {
209 return $this->handlers[$handler][$method];
210 }
8dcb2b47
AD
211 }
212
213 return false;
214 }
73f28fe9 215
4cf0f9a9 216 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
764555ff 217 $command = str_replace("-", "_", strtolower($command));
73f28fe9 218
d2a421e3 219 $this->commands[$command] = array("description" => $description,
4cf0f9a9
AD
220 "suffix" => $suffix,
221 "arghelp" => $arghelp,
d2a421e3 222 "class" => $sender);
73f28fe9
AD
223 }
224
225 function del_command($command) {
226 $command = "-" . strtolower($command);
227
d2a421e3 228 unset($this->commands[$command]);
73f28fe9
AD
229 }
230
231 function lookup_command($command) {
232 $command = "-" . strtolower($command);
233
234 if (is_array($this->commands[$command])) {
235 return $this->commands[$command]["class"];
236 } else {
237 return false;
238 }
239
240 return false;
241 }
242
243 function get_commands() {
244 return $this->commands;
245 }
246
247 function run_commands($args) {
248 foreach ($this->get_commands() as $command => $data) {
764555ff 249 if (isset($args[$command])) {
73f28fe9
AD
250 $command = str_replace("-", "", $command);
251 $data["class"]->$command($args);
252 }
253 }
254 }
255
d8a1d2a2 256 function load_data($force = false) {
d48398e6 257 if ($this->owner_uid) {
d9c85e0f 258 $result = $this->dbh->query("SELECT name, content FROM ttrss_plugin_storage
d8a1d2a2
AD
259 WHERE owner_uid = '".$this->owner_uid."'");
260
d9c85e0f 261 while ($line = $this->dbh->fetch_assoc($result)) {
d8a1d2a2
AD
262 $this->storage[$line["name"]] = unserialize($line["content"]);
263 }
d8a1d2a2
AD
264 }
265 }
266
267 private function save_data($plugin) {
268 if ($this->owner_uid) {
d9c85e0f 269 $plugin = $this->dbh->escape_string($plugin);
d8a1d2a2 270
d9c85e0f 271 $this->dbh->query("BEGIN");
d8a1d2a2 272
d9c85e0f 273 $result = $this->dbh->query("SELECT id FROM ttrss_plugin_storage WHERE
d8a1d2a2
AD
274 owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
275
276 if (!isset($this->storage[$plugin]))
277 $this->storage[$plugin] = array();
278
14c84904
AD
279 $content = $this->dbh->escape_string(serialize($this->storage[$plugin]),
280 false);
d8a1d2a2 281
d9c85e0f
AD
282 if ($this->dbh->num_rows($result) != 0) {
283 $this->dbh->query("UPDATE ttrss_plugin_storage SET content = '$content'
d8a1d2a2
AD
284 WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
285
286 } else {
d9c85e0f 287 $this->dbh->query("INSERT INTO ttrss_plugin_storage
d8a1d2a2
AD
288 (name,owner_uid,content) VALUES
289 ('$plugin','".$this->owner_uid."','$content')");
290 }
291
d9c85e0f 292 $this->dbh->query("COMMIT");
d8a1d2a2
AD
293 }
294 }
295
296 function set($sender, $name, $value, $sync = true) {
297 $idx = get_class($sender);
298
299 if (!isset($this->storage[$idx]))
300 $this->storage[$idx] = array();
301
302 $this->storage[$idx][$name] = $value;
303
d8a1d2a2
AD
304 if ($sync) $this->save_data(get_class($sender));
305 }
306
5d9abb1e 307 function get($sender, $name, $default_value = false) {
d8a1d2a2
AD
308 $idx = get_class($sender);
309
310 if (isset($this->storage[$idx][$name])) {
311 return $this->storage[$idx][$name];
312 } else {
313 return $default_value;
314 }
315 }
316
317 function get_all($sender) {
318 $idx = get_class($sender);
319
320 return $this->storage[$idx];
321 }
5d9abb1e
AD
322
323 function clear_data($sender) {
324 if ($this->owner_uid) {
325 $idx = get_class($sender);
326
327 unset($this->storage[$idx]);
328
d9c85e0f 329 $this->dbh->query("DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
5d9abb1e 330 AND owner_uid = " . $this->owner_uid);
5d9abb1e
AD
331 }
332 }
be178857
AD
333
334 function set_debug($debug) {
335 $this->debug = $debug;
336 }
337
338 function get_debug() {
339 return $this->debug;
340 }
a413f53e
AD
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
79f9bef7
AD
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 }
19c73507
AD
388}
389?>