Admin.php 4.7 KB

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