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