xref: /dokuwiki/inc/Ui/Admin.php (revision 1aa4284e45888bf1247ef9ca7f3713d3bb5eacda)
1<?php
2namespace dokuwiki\Ui;
3
4/**
5 * Class Admin
6 *
7 * Displays the Admin screen
8 *
9 * @package dokuwiki\Ui
10 * @author Andreas Gohr <andi@splitbrain.org>
11 * @author Håkan Sandell <hakan.sandell@home.se>
12 */
13class Admin extends Ui {
14
15    protected $menu;
16
17    /**
18     * Display the UI element
19     *
20     * @return void
21     */
22    public function show() {
23        $this->menu = $this->getPluginList();
24        echo '<div class="ui-admin">';
25        echo p_locale_xhtml('admin');
26        $this->showSecurityCheck();
27        $this->showAdminMenu();
28        $this->showManagerMenu();
29        $this->showVersion();
30        $this->showPluginMenu();
31        echo '</div>';
32    }
33
34    /**
35     * Display the standard admin tasks
36     */
37    protected function showAdminMenu() {
38        /** @var \DokuWiki_Auth_Plugin $auth */
39        global $auth;
40        global $INFO;
41
42        if(!$INFO['isadmin']) return;
43
44        // user manager only if the auth backend supports it
45        if(!$auth || !$auth->canDo('getUsers') ) {
46            if(isset($this->menu['usermanager'])) unset($this->menu['usermanager']);
47        }
48
49        echo '<ul class="admin_tasks">';
50        foreach(array('usermanager','acl', 'extension', 'config', 'styling') as $plugin) {
51            if(!isset($this->menu[$plugin])) continue;
52            $this->showMenuItem($this->menu[$plugin]);
53            unset($this->menu[$plugin]);
54        }
55        echo '</ul>';
56    }
57
58    /**
59     * Display the standard manager tasks
60     */
61    protected function showManagerMenu() {
62        echo '<ul class="admin_tasks">';
63        foreach(array('revert','popularity') as $plugin) {
64            if(!isset($this->menu[$plugin])) continue;
65            $this->showMenuItem($this->menu[$plugin]);
66            unset($this->menu[$plugin]);
67        }
68        echo '</ul>';
69    }
70
71    /**
72     * Display all the remaining plugins
73     */
74    protected function showPluginMenu() {
75        if(!count($this->menu)) return;
76        echo p_locale_xhtml('adminplugins');
77        echo '<ul class="admin_plugins">';
78        foreach ($this->menu as $item) {
79            $this->showMenuItem($item);
80        }
81        echo '</ul>';
82    }
83
84    /**
85     * Display the DokuWiki version
86     */
87    protected function showVersion() {
88        echo '<div id="admin__version">';
89        echo getVersion();
90        echo '</div>';
91    }
92
93    /**
94     * data security check
95     *
96     * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
97     *
98     * it verifies either:
99     *   'savedir' has been moved elsewhere, or
100     *   has protection to prevent the webserver serving files from it
101     */
102    protected function showSecurityCheck() {
103        global $conf;
104        if(substr($conf['savedir'], 0, 2) !== './') return;
105        echo '<a style="border:none; float:right;"
106                href="http://www.dokuwiki.org/security#web_access_security">
107                <img src="' . DOKU_URL . $conf['savedir'] . '/security.png" alt="Your data directory seems to be protected properly."
108                onerror="this.parentNode.style.display=\'none\'" /></a>';
109    }
110
111    /**
112     * Display a single Admin menu item
113     *
114     * @param array $item
115     */
116    protected function showMenuItem($item) {
117        global $ID;
118        if(blank($item['prompt'])) return;
119        echo '<li><div class="li">';
120        echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
121        echo '<span class="icon">';
122        echo inlineSVG($item['icon']);
123        echo '</span>';
124        echo '<span class="prompt">';
125        echo $item['prompt'];
126        echo '</span>';
127        echo '</a>';
128        echo '</div></li>';
129    }
130
131    /**
132     * Build  list of admin functions from the plugins that handle them
133     *
134     * Checks the current permissions to decide on manager or admin plugins
135     *
136     * @return array list of plugins with their properties
137     */
138    protected function getPluginList() {
139        global $INFO;
140        global $conf;
141
142        $pluginlist = plugin_list('admin');
143        $menu = array();
144        foreach($pluginlist as $p) {
145            /** @var \DokuWiki_Admin_Plugin $obj */
146            if(($obj = plugin_load('admin', $p)) === null) continue;
147
148            // check permissions
149            if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
150
151            $menu[$p] = array(
152                'plugin' => $p,
153                'prompt' => $obj->getMenuText($conf['lang']),
154                'icon' => $obj->getMenuIcon(),
155                'sort' => $obj->getMenuSort(),
156            );
157        }
158
159        // sort by name, then sort
160        uasort(
161            $menu,
162            function ($a, $b) {
163                $strcmp = strcasecmp($a['prompt'], $b['prompt']);
164                if($strcmp != 0) return $strcmp;
165                if($a['sort'] == $b['sort']) return 0;
166                return ($a['sort'] < $b['sort']) ? -1 : 1;
167            }
168        );
169
170        return $menu;
171    }
172
173}
174