]> git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
remove PHPMailer and related directives from config.php-dist; add pluggable Mailer...
[tt-rss.git] / classes / pluginhost.php
1 <?php
2 class PluginHost {
3 private $pdo;
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 $plugin_actions = array();
12 private $owner_uid;
13 private $debug;
14 private $last_registered;
15 private static $instance;
16
17 const API_VERSION = 2;
18
19 // Hooks marked with *1 are run in global context and available
20 // to plugins loaded in config.php only
21
22 const HOOK_ARTICLE_BUTTON = 1;
23 const HOOK_ARTICLE_FILTER = 2;
24 const HOOK_PREFS_TAB = 3;
25 const HOOK_PREFS_TAB_SECTION = 4;
26 const HOOK_PREFS_TABS = 5;
27 const HOOK_FEED_PARSED = 6;
28 const HOOK_UPDATE_TASK = 7; // *1
29 const HOOK_AUTH_USER = 8;
30 const HOOK_HOTKEY_MAP = 9;
31 const HOOK_RENDER_ARTICLE = 10;
32 const HOOK_RENDER_ARTICLE_CDM = 11;
33 const HOOK_FEED_FETCHED = 12;
34 const HOOK_SANITIZE = 13;
35 const HOOK_RENDER_ARTICLE_API = 14;
36 const HOOK_TOOLBAR_BUTTON = 15;
37 const HOOK_ACTION_ITEM = 16;
38 const HOOK_HEADLINE_TOOLBAR_BUTTON = 17;
39 const HOOK_HOTKEY_INFO = 18;
40 const HOOK_ARTICLE_LEFT_BUTTON = 19;
41 const HOOK_PREFS_EDIT_FEED = 20;
42 const HOOK_PREFS_SAVE_FEED = 21;
43 const HOOK_FETCH_FEED = 22;
44 const HOOK_QUERY_HEADLINES = 23;
45 const HOOK_HOUSE_KEEPING = 24; // *1
46 const HOOK_SEARCH = 25;
47 const HOOK_FORMAT_ENCLOSURES = 26;
48 const HOOK_SUBSCRIBE_FEED = 27;
49 const HOOK_HEADLINES_BEFORE = 28;
50 const HOOK_RENDER_ENCLOSURE = 29;
51 const HOOK_ARTICLE_FILTER_ACTION = 30;
52 const HOOK_ARTICLE_EXPORT_FEED = 31;
53 const HOOK_MAIN_TOOLBAR_BUTTON = 32;
54 const HOOK_ENCLOSURE_ENTRY = 33;
55 const HOOK_FORMAT_ARTICLE = 34;
56 const HOOK_FORMAT_ARTICLE_CDM = 35;
57 const HOOK_FEED_BASIC_INFO = 36;
58 const HOOK_SEND_LOCAL_FILE = 37;
59 const HOOK_UNSUBSCRIBE_FEED = 38;
60 const HOOK_SEND_MAIL = 39;
61
62 const KIND_ALL = 1;
63 const KIND_SYSTEM = 2;
64 const KIND_USER = 3;
65
66 function __construct() {
67 $this->pdo = Db::pdo();
68
69 $this->storage = array();
70 }
71
72 private function __clone() {
73 //
74 }
75
76 public static function getInstance() {
77 if (self::$instance == null)
78 self::$instance = new self();
79
80 return self::$instance;
81 }
82
83 private function register_plugin($name, $plugin) {
84 //array_push($this->plugins, $plugin);
85 $this->plugins[$name] = $plugin;
86 }
87
88 // needed for compatibility with API 1
89 function get_link() {
90 return false;
91 }
92
93 function get_dbh() {
94 return Db::get();
95 }
96
97 function get_pdo() {
98 return $this->pdo;
99 }
100
101 function get_plugin_names() {
102 $names = array();
103
104 foreach ($this->plugins as $p) {
105 array_push($names, get_class($p));
106 }
107
108 return $names;
109 }
110
111 function get_plugins() {
112 return $this->plugins;
113 }
114
115 function get_plugin($name) {
116 return $this->plugins[strtolower($name)];
117 }
118
119 function run_hooks($type, $method, $args) {
120 foreach ($this->get_hooks($type) as $hook) {
121 $hook->$method($args);
122 }
123 }
124
125 function add_hook($type, $sender) {
126 if (!is_array($this->hooks[$type])) {
127 $this->hooks[$type] = array();
128 }
129
130 array_push($this->hooks[$type], $sender);
131 }
132
133 function del_hook($type, $sender) {
134 if (is_array($this->hooks[$type])) {
135 $key = array_Search($sender, $this->hooks[$type]);
136 if ($key !== FALSE) {
137 unset($this->hooks[$type][$key]);
138 }
139 }
140 }
141
142 function get_hooks($type) {
143 if (isset($this->hooks[$type])) {
144 return $this->hooks[$type];
145 } else {
146 return array();
147 }
148 }
149 function load_all($kind, $owner_uid = false, $skip_init = false) {
150
151 $plugins = array_merge(glob("plugins/*"), glob("plugins.local/*"));
152 $plugins = array_filter($plugins, "is_dir");
153 $plugins = array_map("basename", $plugins);
154
155 asort($plugins);
156
157 $this->load(join(",", $plugins), $kind, $owner_uid, $skip_init);
158 }
159
160 function load($classlist, $kind, $owner_uid = false, $skip_init = false) {
161 $plugins = explode(",", $classlist);
162
163 $this->owner_uid = (int) $owner_uid;
164
165 foreach ($plugins as $class) {
166 $class = trim($class);
167 $class_file = strtolower(basename($class));
168
169 if (!is_dir(__DIR__."/../plugins/$class_file") &&
170 !is_dir(__DIR__."/../plugins.local/$class_file")) continue;
171
172 // try system plugin directory first
173 $file = __DIR__ . "/../plugins/$class_file/init.php";
174 $vendor_dir = __DIR__ . "/../plugins/$class_file/vendor";
175
176 if (!file_exists($file)) {
177 $file = __DIR__ . "/../plugins.local/$class_file/init.php";
178 $vendor_dir = __DIR__ . "/../plugins.local/$class_file/vendor";
179 }
180
181 if (!isset($this->plugins[$class])) {
182 if (file_exists($file)) require_once $file;
183
184 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
185
186 // register plugin autoloader if necessary, for namespaced classes ONLY
187 // layout corresponds to tt-rss main /vendor/author/Package/Class.php
188
189 if (file_exists($vendor_dir)) {
190 spl_autoload_register(function($class) use ($vendor_dir) {
191
192 if (strpos($class, '\\') !== FALSE) {
193 list ($namespace, $class_name) = explode('\\', $class, 2);
194
195 if ($namespace && $class_name) {
196 $class_file = "$vendor_dir/$namespace/" . str_replace('\\', '/', $class_name) . ".php";
197
198 if (file_exists($class_file))
199 require_once $class_file;
200 }
201 }
202 });
203 }
204
205 $plugin = new $class($this);
206
207 $plugin_api = $plugin->api_version();
208
209 if ($plugin_api < PluginHost::API_VERSION) {
210 user_error("Plugin $class is not compatible with current API version (need: " . PluginHost::API_VERSION . ", got: $plugin_api)", E_USER_WARNING);
211 continue;
212 }
213
214 $this->last_registered = $class;
215
216 switch ($kind) {
217 case $this::KIND_SYSTEM:
218 if ($this->is_system($plugin)) {
219 if (!$skip_init) $plugin->init($this);
220 $this->register_plugin($class, $plugin);
221 }
222 break;
223 case $this::KIND_USER:
224 if (!$this->is_system($plugin)) {
225 if (!$skip_init) $plugin->init($this);
226 $this->register_plugin($class, $plugin);
227 }
228 break;
229 case $this::KIND_ALL:
230 if (!$skip_init) $plugin->init($this);
231 $this->register_plugin($class, $plugin);
232 break;
233 }
234 }
235 }
236 }
237 }
238
239 function is_system($plugin) {
240 $about = $plugin->about();
241
242 return @$about[3];
243 }
244
245 // only system plugins are allowed to modify routing
246 function add_handler($handler, $method, $sender) {
247 $handler = str_replace("-", "_", strtolower($handler));
248 $method = strtolower($method);
249
250 if ($this->is_system($sender)) {
251 if (!is_array($this->handlers[$handler])) {
252 $this->handlers[$handler] = array();
253 }
254
255 $this->handlers[$handler][$method] = $sender;
256 }
257 }
258
259 function del_handler($handler, $method, $sender) {
260 $handler = str_replace("-", "_", strtolower($handler));
261 $method = strtolower($method);
262
263 if ($this->is_system($sender)) {
264 unset($this->handlers[$handler][$method]);
265 }
266 }
267
268 function lookup_handler($handler, $method) {
269 $handler = str_replace("-", "_", strtolower($handler));
270 $method = strtolower($method);
271
272 if (is_array($this->handlers[$handler])) {
273 if (isset($this->handlers[$handler]["*"])) {
274 return $this->handlers[$handler]["*"];
275 } else {
276 return $this->handlers[$handler][$method];
277 }
278 }
279
280 return false;
281 }
282
283 function add_command($command, $description, $sender, $suffix = "", $arghelp = "") {
284 $command = str_replace("-", "_", strtolower($command));
285
286 $this->commands[$command] = array("description" => $description,
287 "suffix" => $suffix,
288 "arghelp" => $arghelp,
289 "class" => $sender);
290 }
291
292 function del_command($command) {
293 $command = "-" . strtolower($command);
294
295 unset($this->commands[$command]);
296 }
297
298 function lookup_command($command) {
299 $command = "-" . strtolower($command);
300
301 if (is_array($this->commands[$command])) {
302 return $this->commands[$command]["class"];
303 } else {
304 return false;
305 }
306 }
307
308 function get_commands() {
309 return $this->commands;
310 }
311
312 function run_commands($args) {
313 foreach ($this->get_commands() as $command => $data) {
314 if (isset($args[$command])) {
315 $command = str_replace("-", "", $command);
316 $data["class"]->$command($args);
317 }
318 }
319 }
320
321 function load_data() {
322 if ($this->owner_uid) {
323 $sth = $this->pdo->prepare("SELECT name, content FROM ttrss_plugin_storage
324 WHERE owner_uid = ?");
325 $sth->execute([$this->owner_uid]);
326
327 while ($line = $sth->fetch()) {
328 $this->storage[$line["name"]] = unserialize($line["content"]);
329 }
330 }
331 }
332
333 private function save_data($plugin) {
334 if ($this->owner_uid) {
335 $this->pdo->beginTransaction();
336
337 $sth = $this->pdo->prepare("SELECT id FROM ttrss_plugin_storage WHERE
338 owner_uid= ? AND name = ?");
339 $sth->execute([$this->owner_uid, $plugin]);
340
341 if (!isset($this->storage[$plugin]))
342 $this->storage[$plugin] = array();
343
344 $content = serialize($this->storage[$plugin]);
345
346 if ($sth->fetch()) {
347 $sth = $this->pdo->prepare("UPDATE ttrss_plugin_storage SET content = ?
348 WHERE owner_uid= ? AND name = ?");
349 $sth->execute([(string)$content, $this->owner_uid, $plugin]);
350
351 } else {
352 $sth = $this->pdo->prepare("INSERT INTO ttrss_plugin_storage
353 (name,owner_uid,content) VALUES
354 (?, ?, ?)");
355 $sth->execute([$plugin, $this->owner_uid, (string)$content]);
356 }
357
358 $this->pdo->commit();
359 }
360 }
361
362 function set($sender, $name, $value, $sync = true) {
363 $idx = get_class($sender);
364
365 if (!isset($this->storage[$idx]))
366 $this->storage[$idx] = array();
367
368 $this->storage[$idx][$name] = $value;
369
370 if ($sync) $this->save_data(get_class($sender));
371 }
372
373 function get($sender, $name, $default_value = false) {
374 $idx = get_class($sender);
375
376 if (isset($this->storage[$idx][$name])) {
377 return $this->storage[$idx][$name];
378 } else {
379 return $default_value;
380 }
381 }
382
383 function get_all($sender) {
384 $idx = get_class($sender);
385
386 $data = $this->storage[$idx];
387
388 return $data ? $data : [];
389 }
390
391 function clear_data($sender) {
392 if ($this->owner_uid) {
393 $idx = get_class($sender);
394
395 unset($this->storage[$idx]);
396
397 $sth = $this->pdo->prepare("DELETE FROM ttrss_plugin_storage WHERE name = ?
398 AND owner_uid = ?");
399 $sth->execute([$idx, $this->owner_uid]);
400 }
401 }
402
403 function set_debug($debug) {
404 $this->debug = $debug;
405 }
406
407 function get_debug() {
408 return $this->debug;
409 }
410
411 // Plugin feed functions are *EXPERIMENTAL*!
412
413 // cat_id: only -1 is supported (Special)
414 function add_feed($cat_id, $title, $icon, $sender) {
415 if (!$this->feeds[$cat_id]) $this->feeds[$cat_id] = array();
416
417 $id = count($this->feeds[$cat_id]);
418
419 array_push($this->feeds[$cat_id],
420 array('id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon));
421
422 return $id;
423 }
424
425 function get_feeds($cat_id) {
426 return $this->feeds[$cat_id];
427 }
428
429 // convert feed_id (e.g. -129) to pfeed_id first
430 function get_feed_handler($pfeed_id) {
431 foreach ($this->feeds as $cat) {
432 foreach ($cat as $feed) {
433 if ($feed['id'] == $pfeed_id) {
434 return $feed['sender'];
435 }
436 }
437 }
438 }
439
440 static function pfeed_to_feed_id($label) {
441 return PLUGIN_FEED_BASE_INDEX - 1 - abs($label);
442 }
443
444 static function feed_to_pfeed_id($feed) {
445 return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed);
446 }
447
448 function add_api_method($name, $sender) {
449 if ($this->is_system($sender)) {
450 $this->api_methods[strtolower($name)] = $sender;
451 }
452 }
453
454 function get_api_method($name) {
455 return $this->api_methods[$name];
456 }
457
458 function add_filter_action($sender, $action_name, $action_desc) {
459 $sender_class = get_class($sender);
460
461 if (!isset($this->plugin_actions[$sender_class]))
462 $this->plugin_actions[$sender_class] = array();
463
464 array_push($this->plugin_actions[$sender_class],
465 array("action" => $action_name, "description" => $action_desc, "sender" => $sender));
466 }
467
468 function get_filter_actions() {
469 return $this->plugin_actions;
470 }
471 }