xref: /dokuwiki/inc/Ui/Admin.php (revision 836f6efbf31a2a263102aea61ef0cc5d577aa9bb)
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        $img = DOKU_URL . $conf['savedir'] .
106            '/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png';
107        echo '<a style="border:none; float:right;"
108                href="http://www.dokuwiki.org/security#web_access_security">
109                <img src="' . $img . '" alt="Your data directory seems to be protected properly."
110                onerror="this.parentNode.style.display=\'none\'" /></a>';
111    }
112
113    /**
114     * Display a single Admin menu item
115     *
116     * @param array $item
117     */
118    protected function showMenuItem($item) {
119        global $ID;
120        if(blank($item['prompt'])) return;
121        echo '<li><div class="li">';
122        echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
123        echo '<span class="icon">';
124        echo inlineSVG($item['icon']);
125        echo '</span>';
126        echo '<span class="prompt">';
127        echo $item['prompt'];
128        echo '</span>';
129        echo '</a>';
130        echo '</div></li>';
131    }
132
133    /**
134     * Build  list of admin functions from the plugins that handle them
135     *
136     * Checks the current permissions to decide on manager or admin plugins
137     *
138     * @return array list of plugins with their properties
139     */
140    protected function getPluginList() {
141        global $INFO;
142        global $conf;
143
144        $pluginlist = plugin_list('admin');
145        $menu = array();
146        foreach($pluginlist as $p) {
147            /** @var \DokuWiki_Admin_Plugin $obj */
148            if(($obj = plugin_load('admin', $p)) === null) continue;
149
150            // check permissions
151            if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
152
153            $menu[$p] = array(
154                'plugin' => $p,
155                'prompt' => $obj->getMenuText($conf['lang']),
156                'icon' => $obj->getMenuIcon(),
157                'sort' => $obj->getMenuSort(),
158            );
159        }
160
161        // sort by name, then sort
162        uasort(
163            $menu,
164            function ($a, $b) {
165                $strcmp = strcasecmp($a['prompt'], $b['prompt']);
166                if($strcmp != 0) return $strcmp;
167                if($a['sort'] == $b['sort']) return 0;
168                return ($a['sort'] < $b['sort']) ? -1 : 1;
169            }
170        );
171
172        return $menu;
173    }
174
175}
176