xref: /dokuwiki/inc/Ui/Admin.php (revision d3f829c2e865411af7ac548cd180da21c39771f2)
10470c28fSAndreas Gohr<?php
20470c28fSAndreas Gohrnamespace dokuwiki\Ui;
30470c28fSAndreas Gohr
42d85e841SAndreas Gohruse dokuwiki\Utf8\Sort;
52d85e841SAndreas Gohr
60470c28fSAndreas Gohr/**
70470c28fSAndreas Gohr * Class Admin
80470c28fSAndreas Gohr *
90470c28fSAndreas Gohr * Displays the Admin screen
100470c28fSAndreas Gohr *
110470c28fSAndreas Gohr * @package dokuwiki\Ui
120470c28fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
130470c28fSAndreas Gohr * @author Håkan Sandell <hakan.sandell@home.se>
140470c28fSAndreas Gohr */
150470c28fSAndreas Gohrclass Admin extends Ui {
160470c28fSAndreas Gohr
1770cc2cbfSAndreas Gohr    protected $forAdmins = array('usermanager', 'acl', 'extension', 'config', 'logviewer', 'styling');
1864cdf779SAndreas Gohr    protected $forManagers = array('revert', 'popularity');
1964cdf779SAndreas Gohr    /** @var array[] */
200470c28fSAndreas Gohr    protected $menu;
210470c28fSAndreas Gohr
220470c28fSAndreas Gohr    /**
230470c28fSAndreas Gohr     * Display the UI element
240470c28fSAndreas Gohr     *
250470c28fSAndreas Gohr     * @return void
260470c28fSAndreas Gohr     */
270470c28fSAndreas Gohr    public function show() {
280470c28fSAndreas Gohr        $this->menu = $this->getPluginList();
295d2e38cbSAndreas Gohr        echo '<div class="ui-admin">';
300470c28fSAndreas Gohr        echo p_locale_xhtml('admin');
31052e1c84SAndreas Gohr
3264cdf779SAndreas Gohr        $this->showMenu('admin');
3364cdf779SAndreas Gohr        $this->showMenu('manager');
34052e1c84SAndreas Gohr        $this->showSecurityCheck();
350470c28fSAndreas Gohr        $this->showVersion();
3664cdf779SAndreas Gohr        $this->showMenu('other');
37713faa94SAndreas Gohr        echo '</div>';
380470c28fSAndreas Gohr    }
390470c28fSAndreas Gohr
400470c28fSAndreas Gohr    /**
4164cdf779SAndreas Gohr     * Show the given menu of available plugins
4264cdf779SAndreas Gohr     *
4364cdf779SAndreas Gohr     * @param string $type admin|manager|other
440470c28fSAndreas Gohr     */
4564cdf779SAndreas Gohr    protected function showMenu($type) {
4664cdf779SAndreas Gohr        if (!$this->menu[$type]) return;
470470c28fSAndreas Gohr
4864cdf779SAndreas Gohr        if ($type === 'other') {
490470c28fSAndreas Gohr            echo p_locale_xhtml('adminplugins');
5064cdf779SAndreas Gohr            $class = 'admin_plugins';
5164cdf779SAndreas Gohr        } else {
5264cdf779SAndreas Gohr            $class = 'admin_tasks';
5364cdf779SAndreas Gohr        }
5464cdf779SAndreas Gohr
5564cdf779SAndreas Gohr        echo "<ul class=\"$class\">";
5664cdf779SAndreas Gohr        foreach ($this->menu[$type] as $item) {
570470c28fSAndreas Gohr            $this->showMenuItem($item);
580470c28fSAndreas Gohr        }
590470c28fSAndreas Gohr        echo '</ul>';
600470c28fSAndreas Gohr    }
610470c28fSAndreas Gohr
620470c28fSAndreas Gohr    /**
630470c28fSAndreas Gohr     * Display the DokuWiki version
640470c28fSAndreas Gohr     */
650470c28fSAndreas Gohr    protected function showVersion() {
660470c28fSAndreas Gohr        echo '<div id="admin__version">';
670470c28fSAndreas Gohr        echo getVersion();
680470c28fSAndreas Gohr        echo '</div>';
690470c28fSAndreas Gohr    }
700470c28fSAndreas Gohr
710470c28fSAndreas Gohr    /**
720470c28fSAndreas Gohr     * data security check
730470c28fSAndreas Gohr     *
740470c28fSAndreas Gohr     * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
750470c28fSAndreas Gohr     *
760470c28fSAndreas Gohr     * it verifies either:
770470c28fSAndreas Gohr     *   'savedir' has been moved elsewhere, or
780470c28fSAndreas Gohr     *   has protection to prevent the webserver serving files from it
79052e1c84SAndreas Gohr     *
80052e1c84SAndreas Gohr     * The actual check is carried out via JavaScript. See behaviour.js
810470c28fSAndreas Gohr     */
820470c28fSAndreas Gohr    protected function showSecurityCheck() {
830470c28fSAndreas Gohr        global $conf;
840470c28fSAndreas Gohr        if(substr($conf['savedir'], 0, 2) !== './') return;
8564159a61SAndreas Gohr        $img = DOKU_URL . $conf['savedir'] .
8664159a61SAndreas Gohr            '/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png';
87*d3f829c2SAndreas Gohr        echo '<div id="security__check" data-src="' . $img . '"></div>';
880470c28fSAndreas Gohr    }
890470c28fSAndreas Gohr
900470c28fSAndreas Gohr    /**
910470c28fSAndreas Gohr     * Display a single Admin menu item
920470c28fSAndreas Gohr     *
930470c28fSAndreas Gohr     * @param array $item
940470c28fSAndreas Gohr     */
950470c28fSAndreas Gohr    protected function showMenuItem($item) {
960470c28fSAndreas Gohr        global $ID;
970470c28fSAndreas Gohr        if(blank($item['prompt'])) return;
980470c28fSAndreas Gohr        echo '<li><div class="li">';
99220b8a20SAndreas Gohr        echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
100220b8a20SAndreas Gohr        echo '<span class="icon">';
1014cd2074fSAndreas Gohr        echo inlineSVG($item['icon']);
1020470c28fSAndreas Gohr        echo '</span>';
103220b8a20SAndreas Gohr        echo '<span class="prompt">';
1040470c28fSAndreas Gohr        echo $item['prompt'];
105220b8a20SAndreas Gohr        echo '</span>';
1060470c28fSAndreas Gohr        echo '</a>';
1070470c28fSAndreas Gohr        echo '</div></li>';
1080470c28fSAndreas Gohr    }
1090470c28fSAndreas Gohr
1100470c28fSAndreas Gohr    /**
1110470c28fSAndreas Gohr     * Build  list of admin functions from the plugins that handle them
1120470c28fSAndreas Gohr     *
1130470c28fSAndreas Gohr     * Checks the current permissions to decide on manager or admin plugins
1140470c28fSAndreas Gohr     *
1150470c28fSAndreas Gohr     * @return array list of plugins with their properties
1160470c28fSAndreas Gohr     */
1170470c28fSAndreas Gohr    protected function getPluginList() {
1180470c28fSAndreas Gohr        global $conf;
1190470c28fSAndreas Gohr
1200470c28fSAndreas Gohr        $pluginlist = plugin_list('admin');
12164cdf779SAndreas Gohr        $menu = ['admin' => [], 'manager' => [], 'other' => []];
12264cdf779SAndreas Gohr
1230470c28fSAndreas Gohr        foreach($pluginlist as $p) {
124e1d9dcc8SAndreas Gohr            /** @var \dokuwiki\Extension\AdminPlugin $obj */
1250470c28fSAndreas Gohr            if(($obj = plugin_load('admin', $p)) === null) continue;
1260470c28fSAndreas Gohr
1270470c28fSAndreas Gohr            // check permissions
12864cdf779SAndreas Gohr            if (!$obj->isAccessibleByCurrentUser()) continue;
1290470c28fSAndreas Gohr
13064cdf779SAndreas Gohr            if (in_array($p, $this->forAdmins, true)) {
13164cdf779SAndreas Gohr                $type = 'admin';
13264cdf779SAndreas Gohr            } elseif (in_array($p, $this->forManagers, true)){
13364cdf779SAndreas Gohr                $type = 'manager';
13464cdf779SAndreas Gohr            } else {
13564cdf779SAndreas Gohr                $type = 'other';
13664cdf779SAndreas Gohr            }
13764cdf779SAndreas Gohr
13864cdf779SAndreas Gohr            $menu[$type][$p] = array(
1390470c28fSAndreas Gohr                'plugin' => $p,
1400470c28fSAndreas Gohr                'prompt' => $obj->getMenuText($conf['lang']),
1410470c28fSAndreas Gohr                'icon' => $obj->getMenuIcon(),
1420470c28fSAndreas Gohr                'sort' => $obj->getMenuSort(),
1430470c28fSAndreas Gohr            );
1440470c28fSAndreas Gohr        }
1450470c28fSAndreas Gohr
1460470c28fSAndreas Gohr        // sort by name, then sort
14764cdf779SAndreas Gohr        uasort($menu['admin'], [$this, 'menuSort']);
14864cdf779SAndreas Gohr        uasort($menu['manager'], [$this, 'menuSort']);
14964cdf779SAndreas Gohr        uasort($menu['other'], [$this, 'menuSort']);
1500470c28fSAndreas Gohr
1510470c28fSAndreas Gohr        return $menu;
1520470c28fSAndreas Gohr    }
1530470c28fSAndreas Gohr
15464cdf779SAndreas Gohr    /**
15564cdf779SAndreas Gohr     * Custom sorting for admin menu
15664cdf779SAndreas Gohr     *
15764cdf779SAndreas Gohr     * We sort alphabetically first, then by sort value
15864cdf779SAndreas Gohr     *
15964cdf779SAndreas Gohr     * @param array $a
16064cdf779SAndreas Gohr     * @param array $b
16164cdf779SAndreas Gohr     * @return int
16264cdf779SAndreas Gohr     */
16364cdf779SAndreas Gohr    protected function menuSort($a, $b) {
1642d85e841SAndreas Gohr        $strcmp = Sort::strcmp($a['prompt'], $b['prompt']);
16564cdf779SAndreas Gohr        if($strcmp != 0) return $strcmp;
16664cdf779SAndreas Gohr        if($a['sort'] === $b['sort']) return 0;
16764cdf779SAndreas Gohr        return ($a['sort'] < $b['sort']) ? -1 : 1;
16864cdf779SAndreas Gohr    }
1690470c28fSAndreas Gohr}
170