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        echo '<div id="admin__version">';
73        echo getVersion();
74        echo '</div>';
75    }
76
77    /**
78     * data security check
79     *
80     * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
81     *
82     * it verifies either:
83     *   'savedir' has been moved elsewhere, or
84     *   has protection to prevent the webserver serving files from it
85     *
86     * The actual check is carried out via JavaScript. See behaviour.js
87     */
88    protected function showSecurityCheck()
89    {
90        global $conf;
91        if (!str_starts_with($conf['savedir'], './')) return;
92        $img = DOKU_URL . $conf['savedir'] .
93            '/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png';
94        echo '<div id="security__check" data-src="' . $img . '"></div>';
95    }
96
97    /**
98     * Display a single Admin menu item
99     *
100     * @param array $item
101     */
102    protected function showMenuItem($item)
103    {
104        global $ID;
105        if (blank($item['prompt'])) return;
106        echo '<li><div class="li">';
107        echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
108        echo '<span class="icon">';
109        echo inlineSVG($item['icon']);
110        echo '</span>';
111        echo '<span class="prompt">';
112        echo $item['prompt'];
113        echo '</span>';
114        echo '</a>';
115        echo '</div></li>';
116    }
117
118    /**
119     * Build  list of admin functions from the plugins that handle them
120     *
121     * Checks the current permissions to decide on manager or admin plugins
122     *
123     * @return array list of plugins with their properties
124     */
125    protected function getPluginList()
126    {
127        global $conf;
128
129        $pluginlist = plugin_list('admin');
130        $menu = ['admin' => [], 'manager' => [], 'other' => []];
131
132        foreach ($pluginlist as $p) {
133            /** @var AdminPlugin $obj */
134            if (!($obj = plugin_load('admin', $p)) instanceof PluginInterface) continue;
135
136            // check permissions
137            if (!$obj->isAccessibleByCurrentUser()) continue;
138            if (!$obj->showInMenu()) 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