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