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