xref: /dokuwiki/inc/pluginutils.php (revision f219f385ea50864502246121ad76284b4b9f68cb)
1ee20e7d1Sandi<?php
2ee20e7d1Sandi/**
3ee20e7d1Sandi * Utilities for handling plugins
4ee20e7d1Sandi *
5ee20e7d1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee20e7d1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7ee20e7d1Sandi */
8ee20e7d1Sandi
9087b3a7fSchris// plugin related constants
10e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AdminPlugin;
113a7140a1SAndreas Gohruse dokuwiki\Extension\PluginController;
12e1d9dcc8SAndreas Gohruse dokuwiki\Extension\PluginInterface;
13e1d9dcc8SAndreas Gohr
1403c4aec3Schrisif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
1564159a61SAndreas Gohr// note that only [a-z0-9]+ is officially supported,
1664159a61SAndreas Gohr// this is only to support plugins that don't follow these conventions, too
177521090bSMichael Hamannif(!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
1810e43949SChris Smith
1910e43949SChris Smith/**
2010e43949SChris Smith * Original plugin functions, remain for backwards compatibility
2110e43949SChris Smith */
22143ff0f8SGerrit Uitslag
23143ff0f8SGerrit Uitslag/**
24143ff0f8SGerrit Uitslag * Return list of available plugins
25143ff0f8SGerrit Uitslag *
26143ff0f8SGerrit Uitslag * @param string $type type of plugins; empty string for all
27143ff0f8SGerrit Uitslag * @param bool $all; true to retrieve all, false to retrieve only enabled plugins
28143ff0f8SGerrit Uitslag * @return array with plugin names or plugin component names
29143ff0f8SGerrit Uitslag */
30db959ae3SAndreas Gohrfunction plugin_list($type='',$all=false) {
313a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
32db959ae3SAndreas Gohr    global $plugin_controller;
33db959ae3SAndreas Gohr    return $plugin_controller->getList($type,$all);
34db959ae3SAndreas Gohr}
35143ff0f8SGerrit Uitslag
36143ff0f8SGerrit Uitslag/**
37143ff0f8SGerrit Uitslag * Returns plugin object
38143ff0f8SGerrit Uitslag * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
39143ff0f8SGerrit Uitslag * otherwise an already loaded instance.
40143ff0f8SGerrit Uitslag *
41143ff0f8SGerrit Uitslag * @param  $type     string type of plugin to load
42143ff0f8SGerrit Uitslag * @param  $name     string name of the plugin to load
43143ff0f8SGerrit Uitslag * @param  $new      bool   true to return a new instance of the plugin, false to use an already loaded instance
44143ff0f8SGerrit Uitslag * @param  $disabled bool   true to load even disabled plugins
45e1d9dcc8SAndreas Gohr * @return PluginInterface|null  the plugin object or null on failure
46143ff0f8SGerrit Uitslag */
47b838050eSPiyush Mishrafunction plugin_load($type,$name,$new=false,$disabled=false) {
483a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
49db959ae3SAndreas Gohr    global $plugin_controller;
50db6f7eaeSAndreas Gohr    return $plugin_controller->load($type,$name,$new,$disabled);
51db959ae3SAndreas Gohr}
52143ff0f8SGerrit Uitslag
53143ff0f8SGerrit Uitslag/**
54143ff0f8SGerrit Uitslag * Whether plugin is disabled
55143ff0f8SGerrit Uitslag *
56143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
57e3710957SGerrit Uitslag * @return bool true disabled, false enabled
58143ff0f8SGerrit Uitslag */
59db959ae3SAndreas Gohrfunction plugin_isdisabled($plugin) {
603a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
61db959ae3SAndreas Gohr    global $plugin_controller;
62db959ae3SAndreas Gohr    return $plugin_controller->isdisabled($plugin);
63db959ae3SAndreas Gohr}
64143ff0f8SGerrit Uitslag
65143ff0f8SGerrit Uitslag/**
66143ff0f8SGerrit Uitslag * Enable the plugin
67143ff0f8SGerrit Uitslag *
68143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
69e3710957SGerrit Uitslag * @return bool true saving succeed, false saving failed
70143ff0f8SGerrit Uitslag */
71db959ae3SAndreas Gohrfunction plugin_enable($plugin) {
723a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
73db959ae3SAndreas Gohr    global $plugin_controller;
74db959ae3SAndreas Gohr    return $plugin_controller->enable($plugin);
75db959ae3SAndreas Gohr}
76143ff0f8SGerrit Uitslag
77143ff0f8SGerrit Uitslag/**
78143ff0f8SGerrit Uitslag * Disable the plugin
79143ff0f8SGerrit Uitslag *
80143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
81e3710957SGerrit Uitslag * @return bool  true saving succeed, false saving failed
82143ff0f8SGerrit Uitslag */
83db959ae3SAndreas Gohrfunction plugin_disable($plugin) {
843a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
85db959ae3SAndreas Gohr    global $plugin_controller;
86db959ae3SAndreas Gohr    return $plugin_controller->disable($plugin);
87db959ae3SAndreas Gohr}
88143ff0f8SGerrit Uitslag
89143ff0f8SGerrit Uitslag/**
90143ff0f8SGerrit Uitslag * Returns directory name of plugin
91143ff0f8SGerrit Uitslag *
92143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
93143ff0f8SGerrit Uitslag * @return string name of directory
94*f219f385SAndreas Gohr * @deprecated 2018-07-20
95143ff0f8SGerrit Uitslag */
96db959ae3SAndreas Gohrfunction plugin_directory($plugin) {
97*f219f385SAndreas Gohr    dbg_deprecated('$plugin directly');
98*f219f385SAndreas Gohr    return $plugin;
99db959ae3SAndreas Gohr}
100143ff0f8SGerrit Uitslag
101143ff0f8SGerrit Uitslag/**
102143ff0f8SGerrit Uitslag * Returns cascade of the config files
103143ff0f8SGerrit Uitslag *
104143ff0f8SGerrit Uitslag * @return array with arrays of plugin configs
105143ff0f8SGerrit Uitslag */
106b838050eSPiyush Mishrafunction plugin_getcascade() {
1073a7140a1SAndreas Gohr    /** @var $plugin_controller PluginController */
108b838050eSPiyush Mishra    global $plugin_controller;
109b838050eSPiyush Mishra    return $plugin_controller->getCascade();
110b838050eSPiyush Mishra}
111a61966c5SChristopher Smith
112a61966c5SChristopher Smith
113a61966c5SChristopher Smith/**
114a61966c5SChristopher Smith * Return the currently operating admin plugin or null
115a61966c5SChristopher Smith * if not on an admin plugin page
116a61966c5SChristopher Smith *
117a61966c5SChristopher Smith * @return Doku_Plugin_Admin
118a61966c5SChristopher Smith */
119a61966c5SChristopher Smithfunction plugin_getRequestAdminPlugin(){
120a61966c5SChristopher Smith    static $admin_plugin = false;
121a61966c5SChristopher Smith    global $ACT,$INPUT,$INFO;
122a61966c5SChristopher Smith
123a61966c5SChristopher Smith    if ($admin_plugin === false) {
124a61966c5SChristopher Smith        if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
125a61966c5SChristopher Smith            $pluginlist = plugin_list('admin');
126a61966c5SChristopher Smith            if (in_array($page, $pluginlist)) {
127a61966c5SChristopher Smith                // attempt to load the plugin
128e1d9dcc8SAndreas Gohr                /** @var $admin_plugin AdminPlugin */
129a61966c5SChristopher Smith                $admin_plugin = plugin_load('admin', $page);
130a61966c5SChristopher Smith                // verify
13164cdf779SAndreas Gohr                if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
132a61966c5SChristopher Smith                    $admin_plugin = null;
133a61966c5SChristopher Smith                    $INPUT->remove('page');
134a61966c5SChristopher Smith                    msg('For admins only',-1);
135a61966c5SChristopher Smith                }
136a61966c5SChristopher Smith            }
137a61966c5SChristopher Smith        }
138a61966c5SChristopher Smith    }
139a61966c5SChristopher Smith
140a61966c5SChristopher Smith    return $admin_plugin;
141a61966c5SChristopher Smith}
142