pluginutils.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Utilities for handling plugins
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <andi@splitbrain.org>
  7. */
  8. // plugin related constants
  9. use dokuwiki\Extension\AdminPlugin;
  10. use dokuwiki\Extension\PluginController;
  11. use dokuwiki\Extension\PluginInterface;
  12. if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  13. // note that only [a-z0-9]+ is officially supported,
  14. // this is only to support plugins that don't follow these conventions, too
  15. if(!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
  16. /**
  17. * Original plugin functions, remain for backwards compatibility
  18. */
  19. /**
  20. * Return list of available plugins
  21. *
  22. * @param string $type type of plugins; empty string for all
  23. * @param bool $all; true to retrieve all, false to retrieve only enabled plugins
  24. * @return array with plugin names or plugin component names
  25. */
  26. function plugin_list($type='',$all=false)
  27. {
  28. /** @var $plugin_controller PluginController */
  29. global $plugin_controller;
  30. $plugins = $plugin_controller->getList($type,$all);
  31. sort($plugins, SORT_NATURAL|SORT_FLAG_CASE);
  32. return $plugins;
  33. }
  34. /**
  35. * Returns plugin object
  36. * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
  37. * otherwise an already loaded instance.
  38. *
  39. * @param $type string type of plugin to load
  40. * @param $name string name of the plugin to load
  41. * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
  42. * @param $disabled bool true to load even disabled plugins
  43. * @return PluginInterface|null the plugin object or null on failure
  44. */
  45. function plugin_load($type,$name,$new=false,$disabled=false)
  46. {
  47. /** @var $plugin_controller PluginController */
  48. global $plugin_controller;
  49. return $plugin_controller->load($type,$name,$new,$disabled);
  50. }
  51. /**
  52. * Whether plugin is disabled
  53. *
  54. * @param string $plugin name of plugin
  55. * @return bool true disabled, false enabled
  56. */
  57. function plugin_isdisabled($plugin)
  58. {
  59. /** @var $plugin_controller PluginController */
  60. global $plugin_controller;
  61. return !$plugin_controller->isEnabled($plugin);
  62. }
  63. /**
  64. * Enable the plugin
  65. *
  66. * @param string $plugin name of plugin
  67. * @return bool true saving succeed, false saving failed
  68. */
  69. function plugin_enable($plugin)
  70. {
  71. /** @var $plugin_controller PluginController */
  72. global $plugin_controller;
  73. return $plugin_controller->enable($plugin);
  74. }
  75. /**
  76. * Disable the plugin
  77. *
  78. * @param string $plugin name of plugin
  79. * @return bool true saving succeed, false saving failed
  80. */
  81. function plugin_disable($plugin)
  82. {
  83. /** @var $plugin_controller PluginController */
  84. global $plugin_controller;
  85. return $plugin_controller->disable($plugin);
  86. }
  87. /**
  88. * Returns directory name of plugin
  89. *
  90. * @param string $plugin name of plugin
  91. * @return string name of directory
  92. * @deprecated 2018-07-20
  93. */
  94. function plugin_directory($plugin)
  95. {
  96. dbg_deprecated('$plugin directly');
  97. return $plugin;
  98. }
  99. /**
  100. * Returns cascade of the config files
  101. *
  102. * @return array with arrays of plugin configs
  103. */
  104. function plugin_getcascade()
  105. {
  106. /** @var $plugin_controller PluginController */
  107. global $plugin_controller;
  108. return $plugin_controller->getCascade();
  109. }
  110. /**
  111. * Return the currently operating admin plugin or null
  112. * if not on an admin plugin page
  113. *
  114. * @return Doku_Plugin_Admin
  115. */
  116. function plugin_getRequestAdminPlugin()
  117. {
  118. static $admin_plugin = false;
  119. global $ACT,$INPUT,$INFO;
  120. if ($admin_plugin === false) {
  121. if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
  122. $pluginlist = plugin_list('admin');
  123. if (in_array($page, $pluginlist)) {
  124. // attempt to load the plugin
  125. /** @var $admin_plugin AdminPlugin */
  126. $admin_plugin = plugin_load('admin', $page);
  127. // verify
  128. if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
  129. $admin_plugin = null;
  130. $INPUT->remove('page');
  131. msg('For admins only',-1);
  132. }
  133. }
  134. }
  135. }
  136. return $admin_plugin;
  137. }