xref: /dokuwiki/inc/pluginutils.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1ee20e7d1Sandi<?php
2*d4f83172SAndreas Gohr
3ee20e7d1Sandi/**
4ee20e7d1Sandi * Utilities for handling plugins
5ee20e7d1Sandi *
6ee20e7d1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7ee20e7d1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
8ee20e7d1Sandi */
9ee20e7d1Sandi
10087b3a7fSchris// plugin related constants
11e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AdminPlugin;
123a7140a1SAndreas Gohruse dokuwiki\Extension\PluginController;
13e1d9dcc8SAndreas Gohruse dokuwiki\Extension\PluginInterface;
14e1d9dcc8SAndreas Gohr
1503c4aec3Schrisif (!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
1664159a61SAndreas Gohr// note that only [a-z0-9]+ is officially supported,
1764159a61SAndreas Gohr// this is only to support plugins that don't follow these conventions, too
187521090bSMichael Hamannif (!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
1910e43949SChris Smith
2010e43949SChris Smith/**
2110e43949SChris Smith * Original plugin functions, remain for backwards compatibility
2210e43949SChris Smith */
23143ff0f8SGerrit Uitslag
24143ff0f8SGerrit Uitslag/**
25143ff0f8SGerrit Uitslag * Return list of available plugins
26143ff0f8SGerrit Uitslag *
27143ff0f8SGerrit Uitslag * @param string $type type of plugins; empty string for all
28143ff0f8SGerrit Uitslag * @param bool $all; true to retrieve all, false to retrieve only enabled plugins
29143ff0f8SGerrit Uitslag * @return array with plugin names or plugin component names
30143ff0f8SGerrit Uitslag */
318f6611d2SSatoshi Saharafunction plugin_list($type = '', $all = false)
329c3f55f8SSatoshi Sahara{
333a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
34db959ae3SAndreas Gohr    global $plugin_controller;
359c3f55f8SSatoshi Sahara    $plugins = $plugin_controller->getList($type, $all);
369c3f55f8SSatoshi Sahara    sort($plugins, SORT_NATURAL | SORT_FLAG_CASE);
379c3f55f8SSatoshi Sahara    return $plugins;
38db959ae3SAndreas Gohr}
39143ff0f8SGerrit Uitslag
40143ff0f8SGerrit Uitslag/**
41143ff0f8SGerrit Uitslag * Returns plugin object
42143ff0f8SGerrit Uitslag * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
43143ff0f8SGerrit Uitslag * otherwise an already loaded instance.
44143ff0f8SGerrit Uitslag *
45143ff0f8SGerrit Uitslag * @param  $type     string type of plugin to load
46143ff0f8SGerrit Uitslag * @param  $name     string name of the plugin to load
47143ff0f8SGerrit Uitslag * @param  $new      bool   true to return a new instance of the plugin, false to use an already loaded instance
48143ff0f8SGerrit Uitslag * @param  $disabled bool   true to load even disabled plugins
49e1d9dcc8SAndreas Gohr * @return PluginInterface|null  the plugin object or null on failure
50143ff0f8SGerrit Uitslag */
519c3f55f8SSatoshi Saharafunction plugin_load($type, $name, $new = false, $disabled = false)
529c3f55f8SSatoshi Sahara{
533a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
54db959ae3SAndreas Gohr    global $plugin_controller;
55db6f7eaeSAndreas Gohr    return $plugin_controller->load($type, $name, $new, $disabled);
56db959ae3SAndreas Gohr}
57143ff0f8SGerrit Uitslag
58143ff0f8SGerrit Uitslag/**
59143ff0f8SGerrit Uitslag * Whether plugin is disabled
60143ff0f8SGerrit Uitslag *
61143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
62e3710957SGerrit Uitslag * @return bool true disabled, false enabled
63143ff0f8SGerrit Uitslag */
648f6611d2SSatoshi Saharafunction plugin_isdisabled($plugin)
659c3f55f8SSatoshi Sahara{
663a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
67db959ae3SAndreas Gohr    global $plugin_controller;
68c1ec88ceSAndreas Gohr    return !$plugin_controller->isEnabled($plugin);
69db959ae3SAndreas Gohr}
70143ff0f8SGerrit Uitslag
71143ff0f8SGerrit Uitslag/**
72143ff0f8SGerrit Uitslag * Enable the plugin
73143ff0f8SGerrit Uitslag *
74143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
75e3710957SGerrit Uitslag * @return bool true saving succeed, false saving failed
76143ff0f8SGerrit Uitslag */
778f6611d2SSatoshi Saharafunction plugin_enable($plugin)
789c3f55f8SSatoshi Sahara{
793a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
80db959ae3SAndreas Gohr    global $plugin_controller;
81db959ae3SAndreas Gohr    return $plugin_controller->enable($plugin);
82db959ae3SAndreas Gohr}
83143ff0f8SGerrit Uitslag
84143ff0f8SGerrit Uitslag/**
85143ff0f8SGerrit Uitslag * Disable the plugin
86143ff0f8SGerrit Uitslag *
87143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
88e3710957SGerrit Uitslag * @return bool  true saving succeed, false saving failed
89143ff0f8SGerrit Uitslag */
908f6611d2SSatoshi Saharafunction plugin_disable($plugin)
919c3f55f8SSatoshi Sahara{
923a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
93db959ae3SAndreas Gohr    global $plugin_controller;
94db959ae3SAndreas Gohr    return $plugin_controller->disable($plugin);
95db959ae3SAndreas Gohr}
96143ff0f8SGerrit Uitslag
97143ff0f8SGerrit Uitslag/**
98143ff0f8SGerrit Uitslag * Returns directory name of plugin
99143ff0f8SGerrit Uitslag *
100143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
101143ff0f8SGerrit Uitslag * @return string name of directory
102f219f385SAndreas Gohr * @deprecated 2018-07-20
103143ff0f8SGerrit Uitslag */
1048f6611d2SSatoshi Saharafunction plugin_directory($plugin)
1059c3f55f8SSatoshi Sahara{
106f219f385SAndreas Gohr    dbg_deprecated('$plugin directly');
107f219f385SAndreas Gohr    return $plugin;
108db959ae3SAndreas Gohr}
109143ff0f8SGerrit Uitslag
110143ff0f8SGerrit Uitslag/**
111143ff0f8SGerrit Uitslag * Returns cascade of the config files
112143ff0f8SGerrit Uitslag *
113143ff0f8SGerrit Uitslag * @return array with arrays of plugin configs
114143ff0f8SGerrit Uitslag */
1158f6611d2SSatoshi Saharafunction plugin_getcascade()
1169c3f55f8SSatoshi Sahara{
1173a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
118b838050eSPiyush Mishra    global $plugin_controller;
119b838050eSPiyush Mishra    return $plugin_controller->getCascade();
120b838050eSPiyush Mishra}
121a61966c5SChristopher Smith
122a61966c5SChristopher Smith
123a61966c5SChristopher Smith/**
124a61966c5SChristopher Smith * Return the currently operating admin plugin or null
125a61966c5SChristopher Smith * if not on an admin plugin page
126a61966c5SChristopher Smith *
127a61966c5SChristopher Smith * @return Doku_Plugin_Admin
128a61966c5SChristopher Smith */
1298f6611d2SSatoshi Saharafunction plugin_getRequestAdminPlugin()
1309c3f55f8SSatoshi Sahara{
131a61966c5SChristopher Smith    static $admin_plugin = false;
132a61966c5SChristopher Smith    global $ACT,$INPUT,$INFO;
133a61966c5SChristopher Smith
134a61966c5SChristopher Smith    if ($admin_plugin === false) {
135a61966c5SChristopher Smith        if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
136a61966c5SChristopher Smith            $pluginlist = plugin_list('admin');
137a61966c5SChristopher Smith            if (in_array($page, $pluginlist)) {
138a61966c5SChristopher Smith                // attempt to load the plugin
139e1d9dcc8SAndreas Gohr                /** @var $admin_plugin AdminPlugin */
140a61966c5SChristopher Smith                $admin_plugin = plugin_load('admin', $page);
141a61966c5SChristopher Smith                // verify
14264cdf779SAndreas Gohr                if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
143a61966c5SChristopher Smith                    $admin_plugin = null;
144a61966c5SChristopher Smith                    $INPUT->remove('page');
145a61966c5SChristopher Smith                    msg('For admins only', -1);
146a61966c5SChristopher Smith                }
147a61966c5SChristopher Smith            }
148a61966c5SChristopher Smith        }
149a61966c5SChristopher Smith    }
150a61966c5SChristopher Smith
151a61966c5SChristopher Smith    return $admin_plugin;
152a61966c5SChristopher Smith}
153