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