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 */ 308f6611d2SSatoshi Saharafunction plugin_list($type='',$all=false) 319c3f55f8SSatoshi Sahara{ 323a7140a1SAndreas Gohr /** @var $plugin_controller PluginController */ 33db959ae3SAndreas Gohr global $plugin_controller; 349c3f55f8SSatoshi Sahara $plugins = $plugin_controller->getList($type,$all); 359c3f55f8SSatoshi Sahara sort($plugins, SORT_NATURAL|SORT_FLAG_CASE); 369c3f55f8SSatoshi Sahara return $plugins; 37db959ae3SAndreas Gohr} 38143ff0f8SGerrit Uitslag 39143ff0f8SGerrit Uitslag/** 40143ff0f8SGerrit Uitslag * Returns plugin object 41143ff0f8SGerrit Uitslag * Returns only new instances of a plugin when $new is true or if plugin is not Singleton, 42143ff0f8SGerrit Uitslag * otherwise an already loaded instance. 43143ff0f8SGerrit Uitslag * 44143ff0f8SGerrit Uitslag * @param $type string type of plugin to load 45143ff0f8SGerrit Uitslag * @param $name string name of the plugin to load 46143ff0f8SGerrit Uitslag * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance 47143ff0f8SGerrit Uitslag * @param $disabled bool true to load even disabled plugins 48e1d9dcc8SAndreas Gohr * @return PluginInterface|null the plugin object or null on failure 49143ff0f8SGerrit Uitslag */ 509c3f55f8SSatoshi Saharafunction plugin_load($type,$name,$new=false,$disabled=false) 519c3f55f8SSatoshi Sahara{ 523a7140a1SAndreas Gohr /** @var $plugin_controller PluginController */ 53db959ae3SAndreas Gohr global $plugin_controller; 54db6f7eaeSAndreas Gohr return $plugin_controller->load($type,$name,$new,$disabled); 55db959ae3SAndreas Gohr} 56143ff0f8SGerrit Uitslag 57143ff0f8SGerrit Uitslag/** 58143ff0f8SGerrit Uitslag * Whether plugin is disabled 59143ff0f8SGerrit Uitslag * 60143ff0f8SGerrit Uitslag * @param string $plugin name of plugin 61e3710957SGerrit Uitslag * @return bool true disabled, false enabled 62143ff0f8SGerrit Uitslag */ 638f6611d2SSatoshi Saharafunction plugin_isdisabled($plugin) 649c3f55f8SSatoshi Sahara{ 653a7140a1SAndreas Gohr /** @var $plugin_controller PluginController */ 66db959ae3SAndreas Gohr global $plugin_controller; 67*c1ec88ceSAndreas Gohr return !$plugin_controller->isEnabled($plugin); 68db959ae3SAndreas Gohr} 69143ff0f8SGerrit Uitslag 70143ff0f8SGerrit Uitslag/** 71143ff0f8SGerrit Uitslag * Enable the plugin 72143ff0f8SGerrit Uitslag * 73143ff0f8SGerrit Uitslag * @param string $plugin name of plugin 74e3710957SGerrit Uitslag * @return bool true saving succeed, false saving failed 75143ff0f8SGerrit Uitslag */ 768f6611d2SSatoshi Saharafunction plugin_enable($plugin) 779c3f55f8SSatoshi Sahara{ 783a7140a1SAndreas Gohr /** @var $plugin_controller PluginController */ 79db959ae3SAndreas Gohr global $plugin_controller; 80db959ae3SAndreas Gohr return $plugin_controller->enable($plugin); 81db959ae3SAndreas Gohr} 82143ff0f8SGerrit Uitslag 83143ff0f8SGerrit Uitslag/** 84143ff0f8SGerrit Uitslag * Disable the plugin 85143ff0f8SGerrit Uitslag * 86143ff0f8SGerrit Uitslag * @param string $plugin name of plugin 87e3710957SGerrit Uitslag * @return bool true saving succeed, false saving failed 88143ff0f8SGerrit Uitslag */ 898f6611d2SSatoshi Saharafunction plugin_disable($plugin) 909c3f55f8SSatoshi Sahara{ 913a7140a1SAndreas Gohr /** @var $plugin_controller PluginController */ 92db959ae3SAndreas Gohr global $plugin_controller; 93db959ae3SAndreas Gohr return $plugin_controller->disable($plugin); 94db959ae3SAndreas Gohr} 95143ff0f8SGerrit Uitslag 96143ff0f8SGerrit Uitslag/** 97143ff0f8SGerrit Uitslag * Returns directory name of plugin 98143ff0f8SGerrit Uitslag * 99143ff0f8SGerrit Uitslag * @param string $plugin name of plugin 100143ff0f8SGerrit Uitslag * @return string name of directory 101f219f385SAndreas Gohr * @deprecated 2018-07-20 102143ff0f8SGerrit Uitslag */ 1038f6611d2SSatoshi Saharafunction plugin_directory($plugin) 1049c3f55f8SSatoshi Sahara{ 105f219f385SAndreas Gohr dbg_deprecated('$plugin directly'); 106f219f385SAndreas Gohr return $plugin; 107db959ae3SAndreas Gohr} 108143ff0f8SGerrit Uitslag 109143ff0f8SGerrit Uitslag/** 110143ff0f8SGerrit Uitslag * Returns cascade of the config files 111143ff0f8SGerrit Uitslag * 112143ff0f8SGerrit Uitslag * @return array with arrays of plugin configs 113143ff0f8SGerrit Uitslag */ 1148f6611d2SSatoshi Saharafunction plugin_getcascade() 1159c3f55f8SSatoshi Sahara{ 1163a7140a1SAndreas Gohr /** @var $plugin_controller PluginController */ 117b838050eSPiyush Mishra global $plugin_controller; 118b838050eSPiyush Mishra return $plugin_controller->getCascade(); 119b838050eSPiyush Mishra} 120a61966c5SChristopher Smith 121a61966c5SChristopher Smith 122a61966c5SChristopher Smith/** 123a61966c5SChristopher Smith * Return the currently operating admin plugin or null 124a61966c5SChristopher Smith * if not on an admin plugin page 125a61966c5SChristopher Smith * 126a61966c5SChristopher Smith * @return Doku_Plugin_Admin 127a61966c5SChristopher Smith */ 1288f6611d2SSatoshi Saharafunction plugin_getRequestAdminPlugin() 1299c3f55f8SSatoshi Sahara{ 130a61966c5SChristopher Smith static $admin_plugin = false; 131a61966c5SChristopher Smith global $ACT,$INPUT,$INFO; 132a61966c5SChristopher Smith 133a61966c5SChristopher Smith if ($admin_plugin === false) { 134a61966c5SChristopher Smith if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') { 135a61966c5SChristopher Smith $pluginlist = plugin_list('admin'); 136a61966c5SChristopher Smith if (in_array($page, $pluginlist)) { 137a61966c5SChristopher Smith // attempt to load the plugin 138e1d9dcc8SAndreas Gohr /** @var $admin_plugin AdminPlugin */ 139a61966c5SChristopher Smith $admin_plugin = plugin_load('admin', $page); 140a61966c5SChristopher Smith // verify 14164cdf779SAndreas Gohr if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) { 142a61966c5SChristopher Smith $admin_plugin = null; 143a61966c5SChristopher Smith $INPUT->remove('page'); 144a61966c5SChristopher Smith msg('For admins only',-1); 145a61966c5SChristopher Smith } 146a61966c5SChristopher Smith } 147a61966c5SChristopher Smith } 148a61966c5SChristopher Smith } 149a61966c5SChristopher Smith 150a61966c5SChristopher Smith return $admin_plugin; 151a61966c5SChristopher Smith} 152