/web/wikidocs/inc/plugincontroller.class.php

https://github.com/horaciocaine/top · PHP · 168 lines · 102 code · 27 blank · 39 comment · 35 complexity · 7691c2c45162accff0bdcbfb68141a3a MD5 · raw file

  1. <?php
  2. /**
  3. * Class to encapsulate access to dokuwiki plugins
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Christopher Smith <chris@jalakai.co.uk>
  7. */
  8. // plugin related constants
  9. if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  10. class Doku_Plugin_Controller {
  11. var $list_enabled = array();
  12. var $list_disabled = array();
  13. var $list_bytype = array();
  14. function Doku_Plugin_Controller() {
  15. $this->_populateMasterList();
  16. }
  17. /**
  18. * Returns a list of available plugins of given type
  19. *
  20. * @param $type string, plugin_type name;
  21. * the type of plugin to return,
  22. * use empty string for all types
  23. * @param $all bool;
  24. * false to only return enabled plugins,
  25. * true to return both enabled and disabled plugins
  26. *
  27. * @return array of plugin names
  28. *
  29. * @author Andreas Gohr <andi@splitbrain.org>
  30. */
  31. function getList($type='',$all=false){
  32. // request the complete list
  33. if (!$type) {
  34. return $all ? array_merge($this->list_enabled,$this->list_disabled) : $this->list_enabled;
  35. }
  36. if (!isset($this->list_bytype[$type]['enabled'])) {
  37. $this->list_bytype[$type]['enabled'] = $this->_getListByType($type,true);
  38. }
  39. if ($all && !isset($this->list_bytype[$type]['disabled'])) {
  40. $this->list_bytype[$type]['disabled'] = $this->_getListByType($type,false);
  41. }
  42. return $all ? array_merge($this->list_bytype[$type]['enabled'],$this->list_bytype[$type]['disabled']) : $this->list_bytype[$type]['enabled'];
  43. }
  44. /**
  45. * Loads the given plugin and creates an object of it
  46. *
  47. * @author Andreas Gohr <andi@splitbrain.org>
  48. *
  49. * @param $type string type of plugin to load
  50. * @param $name string name of the plugin to load
  51. * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
  52. * @return objectreference the plugin object or null on failure
  53. */
  54. function &load($type,$name,$new=false){
  55. //we keep all loaded plugins available in global scope for reuse
  56. global $DOKU_PLUGINS;
  57. //plugin already loaded?
  58. if(!empty($DOKU_PLUGINS[$type][$name])){
  59. if ($new && !$DOKU_PLUGINS[$type][$name]->isSingleton()) {
  60. $class = $type.'_plugin_'.$name;
  61. return class_exists($class) ? new $class : null;
  62. } else {
  63. return $DOKU_PLUGINS[$type][$name];
  64. }
  65. }
  66. //try to load the wanted plugin file
  67. list($plugin,$component) = $this->_splitName($name);
  68. $dir = $this->get_directory($plugin);
  69. $file = $component ? "$type/$component.php" : "$type.php";
  70. if (!include_once(DOKU_PLUGIN."$dir/$file")) {
  71. return null;
  72. }
  73. //construct class and instantiate
  74. $class = $type.'_plugin_'.$name;
  75. if (!class_exists($class)) return null;
  76. $DOKU_PLUGINS[$type][$name] = new $class;
  77. return $DOKU_PLUGINS[$type][$name];
  78. }
  79. function isdisabled($plugin) {
  80. return (array_search($plugin, $this->list_enabled) === false);
  81. }
  82. function enable($plugin) {
  83. if (array_search($plugin, $this->list_disabled) !== false) {
  84. return @unlink(DOKU_PLUGIN.$plugin.'/disabled');
  85. }
  86. return false;
  87. }
  88. function disable($plugin) {
  89. if (array_search($plugin, $this->list_enabled) !== false) {
  90. return @touch(DOKU_PLUGIN.$plugin.'/disabled');
  91. }
  92. return false;
  93. }
  94. function get_directory($plugin) {
  95. return $plugin;
  96. }
  97. function _populateMasterList() {
  98. if ($dh = opendir(DOKU_PLUGIN)) {
  99. while (false !== ($plugin = readdir($dh))) {
  100. if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
  101. if (is_file(DOKU_PLUGIN.$plugin)) continue;
  102. if (substr($plugin,-9) == '.disabled') {
  103. // the plugin was disabled by rc2009-01-26
  104. // disabling mechanism was changed back very soon again
  105. // to keep everything simple we just skip the plugin completely
  106. }elseif(@file_exists(DOKU_PLUGIN.$plugin.'/disabled')){
  107. $this->list_disabled[] = $plugin;
  108. } else {
  109. $this->list_enabled[] = $plugin;
  110. }
  111. }
  112. }
  113. }
  114. function _getListByType($type, $enabled) {
  115. $master_list = $enabled ? $this->list_enabled : $this->list_disabled;
  116. $plugins = array();
  117. foreach ($master_list as $plugin) {
  118. $dir = $this->get_directory($plugin);
  119. if (@file_exists(DOKU_PLUGIN."$dir/$type.php")){
  120. $plugins[] = $plugin;
  121. } else {
  122. if ($dp = @opendir(DOKU_PLUGIN."$dir/$type/")) {
  123. while (false !== ($component = readdir($dp))) {
  124. if (substr($component,0,1) == '.' || strtolower(substr($component, -4)) != ".php") continue;
  125. if (is_file(DOKU_PLUGIN."$dir/$type/$component")) {
  126. $plugins[] = $plugin.'_'.substr($component, 0, -4);
  127. }
  128. }
  129. closedir($dp);
  130. }
  131. }
  132. }
  133. return $plugins;
  134. }
  135. function _splitName($name) {
  136. if (array_search($name, $this->list_enabled + $this->list_disabled) === false) {
  137. return explode('_',$name,2);
  138. }
  139. return array($name,'');
  140. }
  141. }