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