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