xref: /dokuwiki/inc/pluginutils.php (revision 64159a61e94d0ce680071c8890e144982c3a8cbe)
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
1003c4aec3Schrisif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11*64159a61SAndreas Gohr// note that only [a-z0-9]+ is officially supported,
12*64159a61SAndreas Gohr// this is only to support plugins that don't follow these conventions, too
137521090bSMichael Hamannif(!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
1410e43949SChris Smith
1510e43949SChris Smith/**
1610e43949SChris Smith * Original plugin functions, remain for backwards compatibility
1710e43949SChris Smith */
18143ff0f8SGerrit Uitslag
19143ff0f8SGerrit Uitslag/**
20143ff0f8SGerrit Uitslag * Return list of available plugins
21143ff0f8SGerrit Uitslag *
22143ff0f8SGerrit Uitslag * @param string $type type of plugins; empty string for all
23143ff0f8SGerrit Uitslag * @param bool $all; true to retrieve all, false to retrieve only enabled plugins
24143ff0f8SGerrit Uitslag * @return array with plugin names or plugin component names
25143ff0f8SGerrit Uitslag */
26db959ae3SAndreas Gohrfunction plugin_list($type='',$all=false) {
27143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
28db959ae3SAndreas Gohr    global $plugin_controller;
29db959ae3SAndreas Gohr    return $plugin_controller->getList($type,$all);
30db959ae3SAndreas Gohr}
31143ff0f8SGerrit Uitslag
32143ff0f8SGerrit Uitslag/**
33143ff0f8SGerrit Uitslag * Returns plugin object
34143ff0f8SGerrit Uitslag * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
35143ff0f8SGerrit Uitslag * otherwise an already loaded instance.
36143ff0f8SGerrit Uitslag *
37143ff0f8SGerrit Uitslag * @param  $type     string type of plugin to load
38143ff0f8SGerrit Uitslag * @param  $name     string name of the plugin to load
39143ff0f8SGerrit Uitslag * @param  $new      bool   true to return a new instance of the plugin, false to use an already loaded instance
40143ff0f8SGerrit Uitslag * @param  $disabled bool   true to load even disabled plugins
4139bceb98SAndreas Gohr * @return DokuWiki_PluginInterface|null  the plugin object or null on failure
42143ff0f8SGerrit Uitslag */
43b838050eSPiyush Mishrafunction plugin_load($type,$name,$new=false,$disabled=false) {
44143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
45db959ae3SAndreas Gohr    global $plugin_controller;
46db6f7eaeSAndreas Gohr    return $plugin_controller->load($type,$name,$new,$disabled);
47db959ae3SAndreas Gohr}
48143ff0f8SGerrit Uitslag
49143ff0f8SGerrit Uitslag/**
50143ff0f8SGerrit Uitslag * Whether plugin is disabled
51143ff0f8SGerrit Uitslag *
52143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
53e3710957SGerrit Uitslag * @return bool true disabled, false enabled
54143ff0f8SGerrit Uitslag */
55db959ae3SAndreas Gohrfunction plugin_isdisabled($plugin) {
56143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
57db959ae3SAndreas Gohr    global $plugin_controller;
58db959ae3SAndreas Gohr    return $plugin_controller->isdisabled($plugin);
59db959ae3SAndreas Gohr}
60143ff0f8SGerrit Uitslag
61143ff0f8SGerrit Uitslag/**
62143ff0f8SGerrit Uitslag * Enable the plugin
63143ff0f8SGerrit Uitslag *
64143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
65e3710957SGerrit Uitslag * @return bool true saving succeed, false saving failed
66143ff0f8SGerrit Uitslag */
67db959ae3SAndreas Gohrfunction plugin_enable($plugin) {
68143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
69db959ae3SAndreas Gohr    global $plugin_controller;
70db959ae3SAndreas Gohr    return $plugin_controller->enable($plugin);
71db959ae3SAndreas Gohr}
72143ff0f8SGerrit Uitslag
73143ff0f8SGerrit Uitslag/**
74143ff0f8SGerrit Uitslag * Disable the plugin
75143ff0f8SGerrit Uitslag *
76143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
77e3710957SGerrit Uitslag * @return bool  true saving succeed, false saving failed
78143ff0f8SGerrit Uitslag */
79db959ae3SAndreas Gohrfunction plugin_disable($plugin) {
80143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
81db959ae3SAndreas Gohr    global $plugin_controller;
82db959ae3SAndreas Gohr    return $plugin_controller->disable($plugin);
83db959ae3SAndreas Gohr}
84143ff0f8SGerrit Uitslag
85143ff0f8SGerrit Uitslag/**
86143ff0f8SGerrit Uitslag * Returns directory name of plugin
87143ff0f8SGerrit Uitslag *
88143ff0f8SGerrit Uitslag * @param string $plugin name of plugin
89143ff0f8SGerrit Uitslag * @return string name of directory
90143ff0f8SGerrit Uitslag */
91db959ae3SAndreas Gohrfunction plugin_directory($plugin) {
92143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
93db959ae3SAndreas Gohr    global $plugin_controller;
94db959ae3SAndreas Gohr    return $plugin_controller->get_directory($plugin);
95db959ae3SAndreas Gohr}
96143ff0f8SGerrit Uitslag
97143ff0f8SGerrit Uitslag/**
98143ff0f8SGerrit Uitslag * Returns cascade of the config files
99143ff0f8SGerrit Uitslag *
100143ff0f8SGerrit Uitslag * @return array with arrays of plugin configs
101143ff0f8SGerrit Uitslag */
102b838050eSPiyush Mishrafunction plugin_getcascade() {
103143ff0f8SGerrit Uitslag    /** @var $plugin_controller Doku_Plugin_Controller */
104b838050eSPiyush Mishra    global $plugin_controller;
105b838050eSPiyush Mishra    return $plugin_controller->getCascade();
106b838050eSPiyush Mishra}
107a61966c5SChristopher Smith
108a61966c5SChristopher Smith
109a61966c5SChristopher Smith/**
110a61966c5SChristopher Smith * Return the currently operating admin plugin or null
111a61966c5SChristopher Smith * if not on an admin plugin page
112a61966c5SChristopher Smith *
113a61966c5SChristopher Smith * @return Doku_Plugin_Admin
114a61966c5SChristopher Smith */
115a61966c5SChristopher Smithfunction plugin_getRequestAdminPlugin(){
116a61966c5SChristopher Smith    static $admin_plugin = false;
117a61966c5SChristopher Smith    global $ACT,$INPUT,$INFO;
118a61966c5SChristopher Smith
119a61966c5SChristopher Smith    if ($admin_plugin === false) {
120a61966c5SChristopher Smith        if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
121a61966c5SChristopher Smith            $pluginlist = plugin_list('admin');
122a61966c5SChristopher Smith            if (in_array($page, $pluginlist)) {
123a61966c5SChristopher Smith                // attempt to load the plugin
124a61966c5SChristopher Smith                /** @var $admin_plugin DokuWiki_Admin_Plugin */
125a61966c5SChristopher Smith                $admin_plugin = plugin_load('admin', $page);
126a61966c5SChristopher Smith                // verify
127a61966c5SChristopher Smith                if ($admin_plugin && $admin_plugin->forAdminOnly() && !$INFO['isadmin']) {
128a61966c5SChristopher Smith                    $admin_plugin = null;
129a61966c5SChristopher Smith                    $INPUT->remove('page');
130a61966c5SChristopher Smith                    msg('For admins only',-1);
131a61966c5SChristopher Smith                }
132a61966c5SChristopher Smith            }
133a61966c5SChristopher Smith        }
134a61966c5SChristopher Smith    }
135a61966c5SChristopher Smith
136a61966c5SChristopher Smith    return $admin_plugin;
137a61966c5SChristopher Smith}
138