Skip to content
Snippets Groups Projects
Admin.php 4.76 KiB
Newer Older
Andreas Gohr's avatar
Andreas Gohr committed
<?php
namespace dokuwiki\Ui;

/**
 * Class Admin
 *
 * Displays the Admin screen
 *
 * @package dokuwiki\Ui
 * @author Andreas Gohr <andi@splitbrain.org>
 * @author Håkan Sandell <hakan.sandell@home.se>
 */
class Admin extends Ui {

    protected $forAdmins = array('usermanager', 'acl', 'extension', 'config', 'styling');
    protected $forManagers = array('revert', 'popularity');
    /** @var array[] */
Andreas Gohr's avatar
Andreas Gohr committed
    protected $menu;

    /**
     * Display the UI element
     *
     * @return void
     */
    public function show() {
        $this->menu = $this->getPluginList();
        echo '<div class="ui-admin">';
Andreas Gohr's avatar
Andreas Gohr committed
        echo p_locale_xhtml('admin');
        $this->showSecurityCheck();
        $this->showMenu('admin');
        $this->showMenu('manager');
Andreas Gohr's avatar
Andreas Gohr committed
        $this->showVersion();
        $this->showMenu('other');
        echo '</div>';
Andreas Gohr's avatar
Andreas Gohr committed
    }

    /**
     * Show the given menu of available plugins
     *
     * @param string $type admin|manager|other
Andreas Gohr's avatar
Andreas Gohr committed
     */
    protected function showMenu($type) {
        if (!$this->menu[$type]) return;

        if ($type === 'other') {
            echo p_locale_xhtml('adminplugins');
            $class = 'admin_plugins';
        } else {
            $class = 'admin_tasks';
Andreas Gohr's avatar
Andreas Gohr committed
        }

        echo "<ul class=\"$class\">";
        foreach ($this->menu[$type] as $item) {
Andreas Gohr's avatar
Andreas Gohr committed
            $this->showMenuItem($item);
        }
        echo '</ul>';
    }

    /**
     * Display the DokuWiki version
     */
    protected function showVersion() {
        echo '<div id="admin__version">';
        echo getVersion();
        echo '</div>';
    }

    /**
     * data security check
     *
     * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
     *
     * it verifies either:
     *   'savedir' has been moved elsewhere, or
     *   has protection to prevent the webserver serving files from it
     */
    protected function showSecurityCheck() {
        global $conf;
        if(substr($conf['savedir'], 0, 2) !== './') return;
        echo '<a style="border:none; float:right;"
                href="http://www.dokuwiki.org/security#web_access_security">
                <img src="' . DOKU_URL . $conf['savedir'] .
                '/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png" 
                alt="Your data directory seems to be protected properly."
Andreas Gohr's avatar
Andreas Gohr committed
                onerror="this.parentNode.style.display=\'none\'" /></a>';
    }

    /**
     * Display a single Admin menu item
     *
     * @param array $item
     */
    protected function showMenuItem($item) {
        global $ID;
        if(blank($item['prompt'])) return;
        echo '<li><div class="li">';
Andreas Gohr's avatar
Andreas Gohr committed
        echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
        echo '<span class="icon">';
        echo inlineSVG($item['icon']);
Andreas Gohr's avatar
Andreas Gohr committed
        echo '</span>';
Andreas Gohr's avatar
Andreas Gohr committed
        echo '<span class="prompt">';
Andreas Gohr's avatar
Andreas Gohr committed
        echo $item['prompt'];
Andreas Gohr's avatar
Andreas Gohr committed
        echo '</span>';
Andreas Gohr's avatar
Andreas Gohr committed
        echo '</a>';
        echo '</div></li>';
    }

    /**
     * Build  list of admin functions from the plugins that handle them
     *
     * Checks the current permissions to decide on manager or admin plugins
     *
     * @return array list of plugins with their properties
     */
    protected function getPluginList() {
        global $conf;

        $pluginlist = plugin_list('admin');
        $menu = ['admin' => [], 'manager' => [], 'other' => []];

Andreas Gohr's avatar
Andreas Gohr committed
        foreach($pluginlist as $p) {
            /** @var \DokuWiki_Admin_Plugin $obj */
            if (($obj = plugin_load('admin', $p)) === null) continue;
Andreas Gohr's avatar
Andreas Gohr committed

            // check permissions
            if (!$obj->isAccessibleByCurrentUser()) continue;

            if (in_array($p, $this->forAdmins, true)) {
                $type = 'admin';
            } elseif (in_array($p, $this->forManagers, true)){
                $type = 'manager';
            } else {
                $type = 'other';
            }
Andreas Gohr's avatar
Andreas Gohr committed

            $menu[$type][$p] = array(
Andreas Gohr's avatar
Andreas Gohr committed
                'plugin' => $p,
                'prompt' => $obj->getMenuText($conf['lang']),
                'icon' => $obj->getMenuIcon(),
                'sort' => $obj->getMenuSort(),
            );
        }

        // sort by name, then sort
        uasort($menu['admin'], [$this, 'menuSort']);
        uasort($menu['manager'], [$this, 'menuSort']);
        uasort($menu['other'], [$this, 'menuSort']);
Andreas Gohr's avatar
Andreas Gohr committed

        return $menu;
    }

    /**
     * Custom sorting for admin menu
     *
     * We sort alphabetically first, then by sort value
     *
     * @param array $a
     * @param array $b
     * @return int
     */
    protected function menuSort ($a, $b) {
        $strcmp = strcasecmp($a['prompt'], $b['prompt']);
        if($strcmp != 0) return $strcmp;
        if($a['sort'] === $b['sort']) return 0;
        return ($a['sort'] < $b['sort']) ? -1 : 1;
    }
Andreas Gohr's avatar
Andreas Gohr committed
}