[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/Ui/ -> Admin.php (source)

   1  <?php
   2  namespace dokuwiki\Ui;
   3  
   4  use dokuwiki\Utf8\Sort;
   5  
   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 = array('usermanager', 'acl', 'extension', 'config', 'styling');
  18      protected $forManagers = array('revert', 'popularity');
  19      /** @var array[] */
  20      protected $menu;
  21  
  22      /**
  23       * Display the UI element
  24       *
  25       * @return void
  26       */
  27      public function show() {
  28          $this->menu = $this->getPluginList();
  29          echo '<div class="ui-admin">';
  30          echo p_locale_xhtml('admin');
  31          $this->showSecurityCheck();
  32          $this->showMenu('admin');
  33          $this->showMenu('manager');
  34          $this->showVersion();
  35          $this->showMenu('other');
  36          echo '</div>';
  37      }
  38  
  39      /**
  40       * Show the given menu of available plugins
  41       *
  42       * @param string $type admin|manager|other
  43       */
  44      protected function showMenu($type) {
  45          if (!$this->menu[$type]) return;
  46  
  47          if ($type === 'other') {
  48              echo p_locale_xhtml('adminplugins');
  49              $class = 'admin_plugins';
  50          } else {
  51              $class = 'admin_tasks';
  52          }
  53  
  54          echo "<ul class=\"$class\">";
  55          foreach ($this->menu[$type] as $item) {
  56              $this->showMenuItem($item);
  57          }
  58          echo '</ul>';
  59      }
  60  
  61      /**
  62       * Display the DokuWiki version
  63       */
  64      protected function showVersion() {
  65          echo '<div id="admin__version">';
  66          echo getVersion();
  67          echo '</div>';
  68      }
  69  
  70      /**
  71       * data security check
  72       *
  73       * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
  74       *
  75       * it verifies either:
  76       *   'savedir' has been moved elsewhere, or
  77       *   has protection to prevent the webserver serving files from it
  78       */
  79      protected function showSecurityCheck() {
  80          global $conf;
  81          if(substr($conf['savedir'], 0, 2) !== './') 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 '<a style="border:none; float:right;"
  85                  href="http://www.dokuwiki.org/security#web_access_security">
  86                  <img src="' . $img . '" alt="Your data directory seems to be protected properly."
  87                  onerror="this.parentNode.style.display=\'none\'" /></a>';
  88      }
  89  
  90      /**
  91       * Display a single Admin menu item
  92       *
  93       * @param array $item
  94       */
  95      protected function showMenuItem($item) {
  96          global $ID;
  97          if(blank($item['prompt'])) return;
  98          echo '<li><div class="li">';
  99          echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
 100          echo '<span class="icon">';
 101          echo inlineSVG($item['icon']);
 102          echo '</span>';
 103          echo '<span class="prompt">';
 104          echo $item['prompt'];
 105          echo '</span>';
 106          echo '</a>';
 107          echo '</div></li>';
 108      }
 109  
 110      /**
 111       * Build  list of admin functions from the plugins that handle them
 112       *
 113       * Checks the current permissions to decide on manager or admin plugins
 114       *
 115       * @return array list of plugins with their properties
 116       */
 117      protected function getPluginList() {
 118          global $conf;
 119  
 120          $pluginlist = plugin_list('admin');
 121          $menu = ['admin' => [], 'manager' => [], 'other' => []];
 122  
 123          foreach($pluginlist as $p) {
 124              /** @var \dokuwiki\Extension\AdminPlugin $obj */
 125              if(($obj = plugin_load('admin', $p)) === null) continue;
 126  
 127              // check permissions
 128              if (!$obj->isAccessibleByCurrentUser()) continue;
 129  
 130              if (in_array($p, $this->forAdmins, true)) {
 131                  $type = 'admin';
 132              } elseif (in_array($p, $this->forManagers, true)){
 133                  $type = 'manager';
 134              } else {
 135                  $type = 'other';
 136              }
 137  
 138              $menu[$type][$p] = array(
 139                  'plugin' => $p,
 140                  'prompt' => $obj->getMenuText($conf['lang']),
 141                  'icon' => $obj->getMenuIcon(),
 142                  'sort' => $obj->getMenuSort(),
 143              );
 144          }
 145  
 146          // sort by name, then sort
 147          uasort($menu['admin'], [$this, 'menuSort']);
 148          uasort($menu['manager'], [$this, 'menuSort']);
 149          uasort($menu['other'], [$this, 'menuSort']);
 150  
 151          return $menu;
 152      }
 153  
 154      /**
 155       * Custom sorting for admin menu
 156       *
 157       * We sort alphabetically first, then by sort value
 158       *
 159       * @param array $a
 160       * @param array $b
 161       * @return int
 162       */
 163      protected function menuSort($a, $b) {
 164          $strcmp = Sort::strcmp($a['prompt'], $b['prompt']);
 165          if($strcmp != 0) return $strcmp;
 166          if($a['sort'] === $b['sort']) return 0;
 167          return ($a['sort'] < $b['sort']) ? -1 : 1;
 168      }
 169  }