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