PluginController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. namespace dokuwiki\Extension;
  3. use dokuwiki\ErrorHandler;
  4. /**
  5. * Class to encapsulate access to dokuwiki plugins
  6. *
  7. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  8. * @author Christopher Smith <chris@jalakai.co.uk>
  9. */
  10. class PluginController
  11. {
  12. /** @var array the types of plugins DokuWiki supports */
  13. const PLUGIN_TYPES = ['auth', 'admin', 'syntax', 'action', 'renderer', 'helper', 'remote', 'cli'];
  14. protected $listByType = [];
  15. /** @var array all installed plugins and their enabled state [plugin=>enabled] */
  16. protected $masterList = [];
  17. protected $pluginCascade = ['default' => [], 'local' => [], 'protected' => []];
  18. protected $lastLocalConfigFile = '';
  19. /**
  20. * Populates the master list of plugins
  21. */
  22. public function __construct()
  23. {
  24. $this->loadConfig();
  25. $this->populateMasterList();
  26. }
  27. /**
  28. * Returns a list of available plugins of given type
  29. *
  30. * @param $type string, plugin_type name;
  31. * the type of plugin to return,
  32. * use empty string for all types
  33. * @param $all bool;
  34. * false to only return enabled plugins,
  35. * true to return both enabled and disabled plugins
  36. *
  37. * @return array of
  38. * - plugin names when $type = ''
  39. * - or plugin component names when a $type is given
  40. *
  41. * @author Andreas Gohr <andi@splitbrain.org>
  42. */
  43. public function getList($type = '', $all = false)
  44. {
  45. // request the complete list
  46. if (!$type) {
  47. return $all ? array_keys($this->masterList) : array_keys(array_filter($this->masterList));
  48. }
  49. if (!isset($this->listByType[$type]['enabled'])) {
  50. $this->listByType[$type]['enabled'] = $this->getListByType($type, true);
  51. }
  52. if ($all && !isset($this->listByType[$type]['disabled'])) {
  53. $this->listByType[$type]['disabled'] = $this->getListByType($type, false);
  54. }
  55. return $all
  56. ? array_merge($this->listByType[$type]['enabled'], $this->listByType[$type]['disabled'])
  57. : $this->listByType[$type]['enabled'];
  58. }
  59. /**
  60. * Loads the given plugin and creates an object of it
  61. *
  62. * @param $type string type of plugin to load
  63. * @param $name string name of the plugin to load
  64. * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
  65. * @param $disabled bool true to load even disabled plugins
  66. * @return PluginInterface|null the plugin object or null on failure
  67. * @author Andreas Gohr <andi@splitbrain.org>
  68. *
  69. */
  70. public function load($type, $name, $new = false, $disabled = false)
  71. {
  72. //we keep all loaded plugins available in global scope for reuse
  73. global $DOKU_PLUGINS;
  74. list($plugin, /* $component */) = $this->splitName($name);
  75. // check if disabled
  76. if (!$disabled && !$this->isEnabled($plugin)) {
  77. return null;
  78. }
  79. $class = $type . '_plugin_' . $name;
  80. try {
  81. //plugin already loaded?
  82. if (!empty($DOKU_PLUGINS[$type][$name])) {
  83. if ($new || !$DOKU_PLUGINS[$type][$name]->isSingleton()) {
  84. return class_exists($class, true) ? new $class : null;
  85. }
  86. return $DOKU_PLUGINS[$type][$name];
  87. }
  88. //construct class and instantiate
  89. if (!class_exists($class, true)) {
  90. # the plugin might be in the wrong directory
  91. $inf = confToHash(DOKU_PLUGIN . "$plugin/plugin.info.txt");
  92. if ($inf['base'] && $inf['base'] != $plugin) {
  93. msg(
  94. sprintf(
  95. "Plugin installed incorrectly. Rename plugin directory '%s' to '%s'.",
  96. hsc($plugin),
  97. hsc(
  98. $inf['base']
  99. )
  100. ), -1
  101. );
  102. } elseif (preg_match('/^' . DOKU_PLUGIN_NAME_REGEX . '$/', $plugin) !== 1) {
  103. msg(
  104. sprintf(
  105. "Plugin name '%s' is not a valid plugin name, only the characters a-z ".
  106. "and 0-9 are allowed. " .
  107. 'Maybe the plugin has been installed in the wrong directory?', hsc($plugin)
  108. ), -1
  109. );
  110. }
  111. return null;
  112. }
  113. $DOKU_PLUGINS[$type][$name] = new $class;
  114. } catch (\Throwable $e) {
  115. ErrorHandler::showExceptionMsg($e, sprintf('Failed to load plugin %s', $plugin));
  116. return null;
  117. }
  118. return $DOKU_PLUGINS[$type][$name];
  119. }
  120. /**
  121. * Whether plugin is disabled
  122. *
  123. * @param string $plugin name of plugin
  124. * @return bool true disabled, false enabled
  125. * @deprecated in favor of the more sensible isEnabled where the return value matches the enabled state
  126. */
  127. public function isDisabled($plugin)
  128. {
  129. dbg_deprecated('isEnabled()');
  130. return !$this->isEnabled($plugin);
  131. }
  132. /**
  133. * Check whether plugin is disabled
  134. *
  135. * @param string $plugin name of plugin
  136. * @return bool true enabled, false disabled
  137. */
  138. public function isEnabled($plugin)
  139. {
  140. return !empty($this->masterList[$plugin]);
  141. }
  142. /**
  143. * Disable the plugin
  144. *
  145. * @param string $plugin name of plugin
  146. * @return bool true saving succeed, false saving failed
  147. */
  148. public function disable($plugin)
  149. {
  150. if (array_key_exists($plugin, $this->pluginCascade['protected'])) return false;
  151. $this->masterList[$plugin] = 0;
  152. return $this->saveList();
  153. }
  154. /**
  155. * Enable the plugin
  156. *
  157. * @param string $plugin name of plugin
  158. * @return bool true saving succeed, false saving failed
  159. */
  160. public function enable($plugin)
  161. {
  162. if (array_key_exists($plugin, $this->pluginCascade['protected'])) return false;
  163. $this->masterList[$plugin] = 1;
  164. return $this->saveList();
  165. }
  166. /**
  167. * Returns cascade of the config files
  168. *
  169. * @return array with arrays of plugin configs
  170. */
  171. public function getCascade()
  172. {
  173. return $this->pluginCascade;
  174. }
  175. /**
  176. * Read all installed plugins and their current enabled state
  177. */
  178. protected function populateMasterList()
  179. {
  180. if ($dh = @opendir(DOKU_PLUGIN)) {
  181. $all_plugins = array();
  182. while (false !== ($plugin = readdir($dh))) {
  183. if ($plugin[0] === '.') continue; // skip hidden entries
  184. if (is_file(DOKU_PLUGIN . $plugin)) continue; // skip files, we're only interested in directories
  185. if (array_key_exists($plugin, $this->masterList) && $this->masterList[$plugin] == 0) {
  186. $all_plugins[$plugin] = 0;
  187. } elseif (array_key_exists($plugin, $this->masterList) && $this->masterList[$plugin] == 1) {
  188. $all_plugins[$plugin] = 1;
  189. } else {
  190. $all_plugins[$plugin] = 1;
  191. }
  192. }
  193. $this->masterList = $all_plugins;
  194. if (!file_exists($this->lastLocalConfigFile)) {
  195. $this->saveList(true);
  196. }
  197. }
  198. }
  199. /**
  200. * Includes the plugin config $files
  201. * and returns the entries of the $plugins array set in these files
  202. *
  203. * @param array $files list of files to include, latter overrides previous
  204. * @return array with entries of the $plugins arrays of the included files
  205. */
  206. protected function checkRequire($files)
  207. {
  208. $plugins = array();
  209. foreach ($files as $file) {
  210. if (file_exists($file)) {
  211. include_once($file);
  212. }
  213. }
  214. return $plugins;
  215. }
  216. /**
  217. * Save the current list of plugins
  218. *
  219. * @param bool $forceSave ;
  220. * false to save only when config changed
  221. * true to always save
  222. * @return bool true saving succeed, false saving failed
  223. */
  224. protected function saveList($forceSave = false)
  225. {
  226. global $conf;
  227. if (empty($this->masterList)) return false;
  228. // Rebuild list of local settings
  229. $local_plugins = $this->rebuildLocal();
  230. if ($local_plugins != $this->pluginCascade['local'] || $forceSave) {
  231. $file = $this->lastLocalConfigFile;
  232. $out = "<?php\n/*\n * Local plugin enable/disable settings\n" .
  233. " * Auto-generated through plugin/extension manager\n *\n" .
  234. " * NOTE: Plugins will not be added to this file unless there " .
  235. "is a need to override a default setting. Plugins are\n" .
  236. " * enabled by default.\n */\n";
  237. foreach ($local_plugins as $plugin => $value) {
  238. $out .= "\$plugins['$plugin'] = $value;\n";
  239. }
  240. // backup current file (remove any existing backup)
  241. if (file_exists($file)) {
  242. $backup = $file . '.bak';
  243. if (file_exists($backup)) @unlink($backup);
  244. if (!@copy($file, $backup)) return false;
  245. if ($conf['fperm']) chmod($backup, $conf['fperm']);
  246. }
  247. //check if can open for writing, else restore
  248. return io_saveFile($file, $out);
  249. }
  250. return false;
  251. }
  252. /**
  253. * Rebuild the set of local plugins
  254. *
  255. * @return array array of plugins to be saved in end($config_cascade['plugins']['local'])
  256. */
  257. protected function rebuildLocal()
  258. {
  259. //assign to local variable to avoid overwriting
  260. $backup = $this->masterList;
  261. //Can't do anything about protected one so rule them out completely
  262. $local_default = array_diff_key($backup, $this->pluginCascade['protected']);
  263. //Diff between local+default and default
  264. //gives us the ones we need to check and save
  265. $diffed_ones = array_diff_key($local_default, $this->pluginCascade['default']);
  266. //The ones which we are sure of (list of 0s not in default)
  267. $sure_plugins = array_filter($diffed_ones, array($this, 'negate'));
  268. //the ones in need of diff
  269. $conflicts = array_diff_key($local_default, $diffed_ones);
  270. //The final list
  271. return array_merge($sure_plugins, array_diff_assoc($conflicts, $this->pluginCascade['default']));
  272. }
  273. /**
  274. * Build the list of plugins and cascade
  275. *
  276. */
  277. protected function loadConfig()
  278. {
  279. global $config_cascade;
  280. foreach (array('default', 'protected') as $type) {
  281. if (array_key_exists($type, $config_cascade['plugins'])) {
  282. $this->pluginCascade[$type] = $this->checkRequire($config_cascade['plugins'][$type]);
  283. }
  284. }
  285. $local = $config_cascade['plugins']['local'];
  286. $this->lastLocalConfigFile = array_pop($local);
  287. $this->pluginCascade['local'] = $this->checkRequire(array($this->lastLocalConfigFile));
  288. if (is_array($local)) {
  289. $this->pluginCascade['default'] = array_merge(
  290. $this->pluginCascade['default'],
  291. $this->checkRequire($local)
  292. );
  293. }
  294. $this->masterList = array_merge(
  295. $this->pluginCascade['default'],
  296. $this->pluginCascade['local'],
  297. $this->pluginCascade['protected']
  298. );
  299. }
  300. /**
  301. * Returns a list of available plugin components of given type
  302. *
  303. * @param string $type plugin_type name; the type of plugin to return,
  304. * @param bool $enabled true to return enabled plugins,
  305. * false to return disabled plugins
  306. * @return array of plugin components of requested type
  307. */
  308. protected function getListByType($type, $enabled)
  309. {
  310. $master_list = $enabled
  311. ? array_keys(array_filter($this->masterList))
  312. : array_keys(array_filter($this->masterList, array($this, 'negate')));
  313. $plugins = array();
  314. foreach ($master_list as $plugin) {
  315. if (file_exists(DOKU_PLUGIN . "$plugin/$type.php")) {
  316. $plugins[] = $plugin;
  317. continue;
  318. }
  319. $typedir = DOKU_PLUGIN . "$plugin/$type/";
  320. if (is_dir($typedir)) {
  321. if ($dp = opendir($typedir)) {
  322. while (false !== ($component = readdir($dp))) {
  323. if (strpos($component, '.') === 0 || strtolower(substr($component, -4)) !== '.php') continue;
  324. if (is_file($typedir . $component)) {
  325. $plugins[] = $plugin . '_' . substr($component, 0, -4);
  326. }
  327. }
  328. closedir($dp);
  329. }
  330. }
  331. }//foreach
  332. return $plugins;
  333. }
  334. /**
  335. * Split name in a plugin name and a component name
  336. *
  337. * @param string $name
  338. * @return array with
  339. * - plugin name
  340. * - and component name when available, otherwise empty string
  341. */
  342. protected function splitName($name)
  343. {
  344. if (!isset($this->masterList[$name])) {
  345. return sexplode('_', $name, 2, '');
  346. }
  347. return array($name, '');
  348. }
  349. /**
  350. * Returns inverse boolean value of the input
  351. *
  352. * @param mixed $input
  353. * @return bool inversed boolean value of input
  354. */
  355. protected function negate($input)
  356. {
  357. return !(bool)$input;
  358. }
  359. }