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