You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

179 lines
4.7 KiB

  1. <?php
  2. namespace dokuwiki\Ui;
  3. use dokuwiki\Extension\AdminPlugin;
  4. use dokuwiki\Extension\PluginInterface;
  5. use dokuwiki\Utf8\Sort;
  6. /**
  7. * Class Admin
  8. *
  9. * Displays the Admin screen
  10. *
  11. * @package dokuwiki\Ui
  12. * @author Andreas Gohr <andi@splitbrain.org>
  13. * @author Håkan Sandell <hakan.sandell@home.se>
  14. */
  15. class Admin extends Ui
  16. {
  17. protected $forAdmins = ['usermanager', 'acl', 'extension', 'config', 'logviewer', 'styling'];
  18. protected $forManagers = ['revert', 'popularity'];
  19. /** @var array[] */
  20. protected $menu;
  21. /**
  22. * Display the UI element
  23. *
  24. * @return void
  25. */
  26. public function show()
  27. {
  28. $this->menu = $this->getPluginList();
  29. echo '<div class="ui-admin">';
  30. echo p_locale_xhtml('admin');
  31. $this->showMenu('admin');
  32. $this->showMenu('manager');
  33. $this->showSecurityCheck();
  34. $this->showVersion();
  35. $this->showMenu('other');
  36. echo '</div>';
  37. }
  38. /**
  39. * Show the given menu of available plugins
  40. *
  41. * @param string $type admin|manager|other
  42. */
  43. protected function showMenu($type)
  44. {
  45. if (!$this->menu[$type]) return;
  46. if ($type === 'other') {
  47. echo p_locale_xhtml('adminplugins');
  48. $class = 'admin_plugins';
  49. } else {
  50. $class = 'admin_tasks';
  51. }
  52. echo "<ul class=\"$class\">";
  53. foreach ($this->menu[$type] as $item) {
  54. $this->showMenuItem($item);
  55. }
  56. echo '</ul>';
  57. }
  58. /**
  59. * Display the DokuWiki version
  60. */
  61. protected function showVersion()
  62. {
  63. echo '<div id="admin__version">';
  64. echo getVersion();
  65. echo '</div>';
  66. }
  67. /**
  68. * data security check
  69. *
  70. * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
  71. *
  72. * it verifies either:
  73. * 'savedir' has been moved elsewhere, or
  74. * has protection to prevent the webserver serving files from it
  75. *
  76. * The actual check is carried out via JavaScript. See behaviour.js
  77. */
  78. protected function showSecurityCheck()
  79. {
  80. global $conf;
  81. if (!str_starts_with($conf['savedir'], './')) return;
  82. $img = DOKU_URL . $conf['savedir'] .
  83. '/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png';
  84. echo '<div id="security__check" data-src="' . $img . '"></div>';
  85. }
  86. /**
  87. * Display a single Admin menu item
  88. *
  89. * @param array $item
  90. */
  91. protected function showMenuItem($item)
  92. {
  93. global $ID;
  94. if (blank($item['prompt'])) return;
  95. echo '<li><div class="li">';
  96. echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
  97. echo '<span class="icon">';
  98. echo inlineSVG($item['icon']);
  99. echo '</span>';
  100. echo '<span class="prompt">';
  101. echo $item['prompt'];
  102. echo '</span>';
  103. echo '</a>';
  104. echo '</div></li>';
  105. }
  106. /**
  107. * Build list of admin functions from the plugins that handle them
  108. *
  109. * Checks the current permissions to decide on manager or admin plugins
  110. *
  111. * @return array list of plugins with their properties
  112. */
  113. protected function getPluginList()
  114. {
  115. global $conf;
  116. $pluginlist = plugin_list('admin');
  117. $menu = ['admin' => [], 'manager' => [], 'other' => []];
  118. foreach ($pluginlist as $p) {
  119. /** @var AdminPlugin $obj */
  120. if (!($obj = plugin_load('admin', $p)) instanceof PluginInterface) continue;
  121. // check permissions
  122. if (!$obj->isAccessibleByCurrentUser()) continue;
  123. if (in_array($p, $this->forAdmins, true)) {
  124. $type = 'admin';
  125. } elseif (in_array($p, $this->forManagers, true)) {
  126. $type = 'manager';
  127. } else {
  128. $type = 'other';
  129. }
  130. $menu[$type][$p] = [
  131. 'plugin' => $p,
  132. 'prompt' => $obj->getMenuText($conf['lang']),
  133. 'icon' => $obj->getMenuIcon(),
  134. 'sort' => $obj->getMenuSort()
  135. ];
  136. }
  137. // sort by name, then sort
  138. uasort($menu['admin'], [$this, 'menuSort']);
  139. uasort($menu['manager'], [$this, 'menuSort']);
  140. uasort($menu['other'], [$this, 'menuSort']);
  141. return $menu;
  142. }
  143. /**
  144. * Custom sorting for admin menu
  145. *
  146. * We sort alphabetically first, then by sort value
  147. *
  148. * @param array $a
  149. * @param array $b
  150. * @return int
  151. */
  152. protected function menuSort($a, $b)
  153. {
  154. $strcmp = Sort::strcmp($a['prompt'], $b['prompt']);
  155. if ($strcmp != 0) return $strcmp;
  156. return $a['sort'] <=> $b['sort'];
  157. }
  158. }