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