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