1<?php
2namespace dokuwiki\Ui;
3
4use dokuwiki\Extension\AdminPlugin;
5use dokuwiki\Utf8\Sort;
6
7/**
8 * Class Admin
9 *
10 * Displays the Admin screen
11 *
12 * @package dokuwiki\Ui
13 * @author Andreas Gohr <andi@splitbrain.org>
14 * @author Håkan Sandell <hakan.sandell@home.se>
15 */
16class Admin extends Ui {
17
18    protected $forAdmins = array('usermanager', 'acl', 'extension', 'config', 'logviewer', 'styling');
19    protected $forManagers = array('revert', 'popularity');
20    /** @var array[] */
21    protected $menu;
22
23    /**
24     * Display the UI element
25     *
26     * @return void
27     */
28    public function show() {
29        $this->menu = $this->getPluginList();
30        echo '<div class="ui-admin">';
31        echo p_locale_xhtml('admin');
32
33        $this->showMenu('admin');
34        $this->showMenu('manager');
35        $this->showSecurityCheck();
36        $this->showVersion();
37        $this->showMenu('other');
38        echo '</div>';
39    }
40
41    /**
42     * Show the given menu of available plugins
43     *
44     * @param string $type admin|manager|other
45     */
46    protected function showMenu($type) {
47        if (!$this->menu[$type]) return;
48
49        if ($type === 'other') {
50            echo p_locale_xhtml('adminplugins');
51            $class = 'admin_plugins';
52        } else {
53            $class = 'admin_tasks';
54        }
55
56        echo "<ul class=\"$class\">";
57        foreach ($this->menu[$type] as $item) {
58            $this->showMenuItem($item);
59        }
60        echo '</ul>';
61    }
62
63    /**
64     * Display the DokuWiki version
65     */
66    protected function showVersion() {
67        echo '<div id="admin__version">';
68        echo getVersion();
69        echo '</div>';
70    }
71
72    /**
73     * data security check
74     *
75     * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
76     *
77     * it verifies either:
78     *   'savedir' has been moved elsewhere, or
79     *   has protection to prevent the webserver serving files from it
80     *
81     * The actual check is carried out via JavaScript. See behaviour.js
82     */
83    protected function showSecurityCheck() {
84        global $conf;
85        if(substr($conf['savedir'], 0, 2) !== './') return;
86        $img = DOKU_URL . $conf['savedir'] .
87            '/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png';
88        echo '<div id="security__check" data-src="' . $img . '"></div>';
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 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