xref: /dokuwiki/inc/pluginutils.php (revision 4af692c25adeba403c19bd84342f212d90051447)
1<?php
2/**
3 * Utilities for handling plugins
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9// plugin related constants
10use dokuwiki\Extension\AdminPlugin;
11use dokuwiki\Extension\PluginController;
12use dokuwiki\Extension\PluginInterface;
13
14if(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15// note that only [a-z0-9]+ is officially supported,
16// this is only to support plugins that don't follow these conventions, too
17if(!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
18
19/**
20 * Original plugin functions, remain for backwards compatibility
21 */
22
23/**
24 * Return list of available plugins
25 *
26 * @param string $type type of plugins; empty string for all
27 * @param bool $all; true to retrieve all, false to retrieve only enabled plugins
28 * @return array with plugin names or plugin component names
29 */
30function plugin_list($type='',$all=false) {
31    /** @var $plugin_controller PluginController */
32    global $plugin_controller;
33    return $plugin_controller->getList($type,$all);
34}
35
36/**
37 * Returns plugin object
38 * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
39 * otherwise an already loaded instance.
40 *
41 * @param  $type     string type of plugin to load
42 * @param  $name     string name of the plugin to load
43 * @param  $new      bool   true to return a new instance of the plugin, false to use an already loaded instance
44 * @param  $disabled bool   true to load even disabled plugins
45 * @return PluginInterface|null  the plugin object or null on failure
46 */
47function plugin_load($type,$name,$new=false,$disabled=false) {
48    /** @var $plugin_controller PluginController */
49    global $plugin_controller;
50    return $plugin_controller->load($type,$name,$new,$disabled);
51}
52
53/**
54 * Whether plugin is disabled
55 *
56 * @param string $plugin name of plugin
57 * @return bool true disabled, false enabled
58 */
59function plugin_isdisabled($plugin) {
60    /** @var $plugin_controller PluginController */
61    global $plugin_controller;
62    return $plugin_controller->isdisabled($plugin);
63}
64
65/**
66 * Enable the plugin
67 *
68 * @param string $plugin name of plugin
69 * @return bool true saving succeed, false saving failed
70 */
71function plugin_enable($plugin) {
72    /** @var $plugin_controller PluginController */
73    global $plugin_controller;
74    return $plugin_controller->enable($plugin);
75}
76
77/**
78 * Disable the plugin
79 *
80 * @param string $plugin name of plugin
81 * @return bool  true saving succeed, false saving failed
82 */
83function plugin_disable($plugin) {
84    /** @var $plugin_controller PluginController */
85    global $plugin_controller;
86    return $plugin_controller->disable($plugin);
87}
88
89/**
90 * Returns directory name of plugin
91 *
92 * @param string $plugin name of plugin
93 * @return string name of directory
94 * @deprecated 2018-07-20
95 */
96function plugin_directory($plugin) {
97    dbg_deprecated('$plugin directly');
98    return $plugin;
99}
100
101/**
102 * Returns cascade of the config files
103 *
104 * @return array with arrays of plugin configs
105 */
106function plugin_getcascade() {
107    /** @var $plugin_controller PluginController */
108    global $plugin_controller;
109    return $plugin_controller->getCascade();
110}
111
112
113/**
114 * Return the currently operating admin plugin or null
115 * if not on an admin plugin page
116 *
117 * @return Doku_Plugin_Admin
118 */
119function plugin_getRequestAdminPlugin(){
120    static $admin_plugin = false;
121    global $ACT,$INPUT,$INFO;
122
123    if ($admin_plugin === false) {
124        if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
125            $pluginlist = plugin_list('admin');
126            if (in_array($page, $pluginlist)) {
127                // attempt to load the plugin
128                /** @var $admin_plugin AdminPlugin */
129                $admin_plugin = plugin_load('admin', $page);
130                // verify
131                if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
132                    $admin_plugin = null;
133                    $INPUT->remove('page');
134                    msg('For admins only',-1);
135                }
136            }
137        }
138    }
139
140    return $admin_plugin;
141}
142