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{
32    /** @var $plugin_controller PluginController */
33    global $plugin_controller;
34    $plugins = $plugin_controller->getList($type,$all);
35    sort($plugins, SORT_NATURAL|SORT_FLAG_CASE);
36    return $plugins;
37}
38
39/**
40 * Returns plugin object
41 * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
42 * otherwise an already loaded instance.
43 *
44 * @param  $type     string type of plugin to load
45 * @param  $name     string name of the plugin to load
46 * @param  $new      bool   true to return a new instance of the plugin, false to use an already loaded instance
47 * @param  $disabled bool   true to load even disabled plugins
48 * @return PluginInterface|null  the plugin object or null on failure
49 */
50function plugin_load($type,$name,$new=false,$disabled=false)
51{
52    /** @var $plugin_controller PluginController */
53    global $plugin_controller;
54    return $plugin_controller->load($type,$name,$new,$disabled);
55}
56
57/**
58 * Whether plugin is disabled
59 *
60 * @param string $plugin name of plugin
61 * @return bool true disabled, false enabled
62 */
63function plugin_isdisabled($plugin)
64{
65    /** @var $plugin_controller PluginController */
66    global $plugin_controller;
67    return !$plugin_controller->isEnabled($plugin);
68}
69
70/**
71 * Enable the plugin
72 *
73 * @param string $plugin name of plugin
74 * @return bool true saving succeed, false saving failed
75 */
76function plugin_enable($plugin)
77{
78    /** @var $plugin_controller PluginController */
79    global $plugin_controller;
80    return $plugin_controller->enable($plugin);
81}
82
83/**
84 * Disable the plugin
85 *
86 * @param string $plugin name of plugin
87 * @return bool  true saving succeed, false saving failed
88 */
89function plugin_disable($plugin)
90{
91    /** @var $plugin_controller PluginController */
92    global $plugin_controller;
93    return $plugin_controller->disable($plugin);
94}
95
96/**
97 * Returns directory name of plugin
98 *
99 * @param string $plugin name of plugin
100 * @return string name of directory
101 * @deprecated 2018-07-20
102 */
103function plugin_directory($plugin)
104{
105    dbg_deprecated('$plugin directly');
106    return $plugin;
107}
108
109/**
110 * Returns cascade of the config files
111 *
112 * @return array with arrays of plugin configs
113 */
114function plugin_getcascade()
115{
116    /** @var $plugin_controller PluginController */
117    global $plugin_controller;
118    return $plugin_controller->getCascade();
119}
120
121
122/**
123 * Return the currently operating admin plugin or null
124 * if not on an admin plugin page
125 *
126 * @return Doku_Plugin_Admin
127 */
128function plugin_getRequestAdminPlugin()
129{
130    static $admin_plugin = false;
131    global $ACT,$INPUT,$INFO;
132
133    if ($admin_plugin === false) {
134        if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
135            $pluginlist = plugin_list('admin');
136            if (in_array($page, $pluginlist)) {
137                // attempt to load the plugin
138                /** @var $admin_plugin AdminPlugin */
139                $admin_plugin = plugin_load('admin', $page);
140                // verify
141                if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
142                    $admin_plugin = null;
143                    $INPUT->remove('page');
144                    msg('For admins only',-1);
145                }
146            }
147        }
148    }
149
150    return $admin_plugin;
151}
152