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