xref: /dokuwiki/inc/pluginutils.php (revision e1d9dcc8b460b6f029ac9c8d5d3b8d23b6e73402)
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
10*e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AdminPlugin;
11*e1d9dcc8SAndreas Gohruse dokuwiki\Extension\PluginInterface;
12*e1d9dcc8SAndreas Gohr
1303c4aec3Schrisif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
1464159a61SAndreas Gohr// note that only [a-z0-9]+ is officially supported,
1564159a61SAndreas Gohr// this is only to support plugins that don't follow these conventions, too
167521090bSMichael Hamannif(!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
1710e43949SChris Smith
1810e43949SChris Smith/**
1910e43949SChris Smith * Original plugin functions, remain for backwards compatibility
2010e43949SChris Smith */
21143ff0f8SGerrit Uitslag
22143ff0f8SGerrit Uitslag/**
23143ff0f8SGerrit Uitslag * Return list of available plugins
24143ff0f8SGerrit Uitslag *
25143ff0f8SGerrit Uitslag * @param string $type type of plugins; empty string for all
26143ff0f8SGerrit Uitslag * @param bool $all; true to retrieve all, false to retrieve only enabled plugins
27143ff0f8SGerrit Uitslag * @return array with plugin names or plugin component names
28143ff0f8SGerrit Uitslag */
29db959ae3SAndreas Gohrfunction plugin_list($type='',$all=false) {
30143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
31db959ae3SAndreas Gohr    global $plugin_controller;
32db959ae3SAndreas Gohr    return $plugin_controller->getList($type,$all);
33db959ae3SAndreas Gohr}
34143ff0f8SGerrit Uitslag
35143ff0f8SGerrit Uitslag/**
36143ff0f8SGerrit Uitslag * Returns plugin object
37143ff0f8SGerrit Uitslag * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
38143ff0f8SGerrit Uitslag * otherwise an already loaded instance.
39143ff0f8SGerrit Uitslag *
40143ff0f8SGerrit Uitslag * @param  $type     string type of plugin to load
41143ff0f8SGerrit Uitslag * @param  $name     string name of the plugin to load
42143ff0f8SGerrit Uitslag * @param  $new      bool   true to return a new instance of the plugin, false to use an already loaded instance
43143ff0f8SGerrit Uitslag * @param  $disabled bool   true to load even disabled plugins
44*e1d9dcc8SAndreas Gohr * @return PluginInterface|null  the plugin object or null on failure
45143ff0f8SGerrit Uitslag */
46b838050eSPiyush Mishrafunction plugin_load($type,$name,$new=false,$disabled=false) {
47143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
48db959ae3SAndreas Gohr    global $plugin_controller;
49db6f7eaeSAndreas Gohr    return $plugin_controller->load($type,$name,$new,$disabled);
50db959ae3SAndreas Gohr}
51143ff0f8SGerrit Uitslag
52143ff0f8SGerrit Uitslag/**
53143ff0f8SGerrit Uitslag * Whether plugin is disabled
54143ff0f8SGerrit Uitslag *
55143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
56e3710957SGerrit Uitslag * @return bool true disabled, false enabled
57143ff0f8SGerrit Uitslag */
58db959ae3SAndreas Gohrfunction plugin_isdisabled($plugin) {
59143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
60db959ae3SAndreas Gohr    global $plugin_controller;
61db959ae3SAndreas Gohr    return $plugin_controller->isdisabled($plugin);
62db959ae3SAndreas Gohr}
63143ff0f8SGerrit Uitslag
64143ff0f8SGerrit Uitslag/**
65143ff0f8SGerrit Uitslag * Enable the plugin
66143ff0f8SGerrit Uitslag *
67143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
68e3710957SGerrit Uitslag * @return bool true saving succeed, false saving failed
69143ff0f8SGerrit Uitslag */
70db959ae3SAndreas Gohrfunction plugin_enable($plugin) {
71143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
72db959ae3SAndreas Gohr    global $plugin_controller;
73db959ae3SAndreas Gohr    return $plugin_controller->enable($plugin);
74db959ae3SAndreas Gohr}
75143ff0f8SGerrit Uitslag
76143ff0f8SGerrit Uitslag/**
77143ff0f8SGerrit Uitslag * Disable the plugin
78143ff0f8SGerrit Uitslag *
79143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
80e3710957SGerrit Uitslag * @return bool  true saving succeed, false saving failed
81143ff0f8SGerrit Uitslag */
82db959ae3SAndreas Gohrfunction plugin_disable($plugin) {
83143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
84db959ae3SAndreas Gohr    global $plugin_controller;
85db959ae3SAndreas Gohr    return $plugin_controller->disable($plugin);
86db959ae3SAndreas Gohr}
87143ff0f8SGerrit Uitslag
88143ff0f8SGerrit Uitslag/**
89143ff0f8SGerrit Uitslag * Returns directory name of plugin
90143ff0f8SGerrit Uitslag *
91143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
92143ff0f8SGerrit Uitslag * @return string name of directory
93143ff0f8SGerrit Uitslag */
94db959ae3SAndreas Gohrfunction plugin_directory($plugin) {
95143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
96db959ae3SAndreas Gohr    global $plugin_controller;
97db959ae3SAndreas Gohr    return $plugin_controller->get_directory($plugin);
98db959ae3SAndreas Gohr}
99143ff0f8SGerrit Uitslag
100143ff0f8SGerrit Uitslag/**
101143ff0f8SGerrit Uitslag * Returns cascade of the config files
102143ff0f8SGerrit Uitslag *
103143ff0f8SGerrit Uitslag * @return array with arrays of plugin configs
104143ff0f8SGerrit Uitslag */
105b838050eSPiyush Mishrafunction plugin_getcascade() {
106143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
107b838050eSPiyush Mishra    global $plugin_controller;
108b838050eSPiyush Mishra    return $plugin_controller->getCascade();
109b838050eSPiyush Mishra}
110a61966c5SChristopher Smith
111a61966c5SChristopher Smith
112a61966c5SChristopher Smith/**
113a61966c5SChristopher Smith * Return the currently operating admin plugin or null
114a61966c5SChristopher Smith * if not on an admin plugin page
115a61966c5SChristopher Smith *
116a61966c5SChristopher Smith * @return Doku_Plugin_Admin
117a61966c5SChristopher Smith */
118a61966c5SChristopher Smithfunction plugin_getRequestAdminPlugin(){
119a61966c5SChristopher Smith    static $admin_plugin = false;
120a61966c5SChristopher Smith    global $ACT,$INPUT,$INFO;
121a61966c5SChristopher Smith
122a61966c5SChristopher Smith    if ($admin_plugin === false) {
123a61966c5SChristopher Smith        if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
124a61966c5SChristopher Smith            $pluginlist = plugin_list('admin');
125a61966c5SChristopher Smith            if (in_array($page, $pluginlist)) {
126a61966c5SChristopher Smith                // attempt to load the plugin
127*e1d9dcc8SAndreas Gohr                /** @var $admin_plugin AdminPlugin */
128a61966c5SChristopher Smith                $admin_plugin = plugin_load('admin', $page);
129a61966c5SChristopher Smith                // verify
13064cdf779SAndreas Gohr                if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
131a61966c5SChristopher Smith                    $admin_plugin = null;
132a61966c5SChristopher Smith                    $INPUT->remove('page');
133a61966c5SChristopher Smith                    msg('For admins only',-1);
134a61966c5SChristopher Smith                }
135a61966c5SChristopher Smith            }
136a61966c5SChristopher Smith        }
137a61966c5SChristopher Smith    }
138a61966c5SChristopher Smith
139a61966c5SChristopher Smith    return $admin_plugin;
140a61966c5SChristopher Smith}
141