xref: /dokuwiki/inc/Ui/Admin.php (revision 0470c28f15a56bd580c5b33b3a943d8b5429a8dd)
1*0470c28fSAndreas Gohr<?php
2*0470c28fSAndreas Gohrnamespace dokuwiki\Ui;
3*0470c28fSAndreas Gohr
4*0470c28fSAndreas Gohr/**
5*0470c28fSAndreas Gohr * Class Admin
6*0470c28fSAndreas Gohr *
7*0470c28fSAndreas Gohr * Displays the Admin screen
8*0470c28fSAndreas Gohr *
9*0470c28fSAndreas Gohr * @package dokuwiki\Ui
10*0470c28fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
11*0470c28fSAndreas Gohr * @author Håkan Sandell <hakan.sandell@home.se>
12*0470c28fSAndreas Gohr */
13*0470c28fSAndreas Gohrclass Admin extends Ui {
14*0470c28fSAndreas Gohr
15*0470c28fSAndreas Gohr    protected $menu;
16*0470c28fSAndreas Gohr
17*0470c28fSAndreas Gohr    /**
18*0470c28fSAndreas Gohr     * Display the UI element
19*0470c28fSAndreas Gohr     *
20*0470c28fSAndreas Gohr     * @return void
21*0470c28fSAndreas Gohr     */
22*0470c28fSAndreas Gohr    public function show() {
23*0470c28fSAndreas Gohr        $this->menu = $this->getPluginList();
24*0470c28fSAndreas Gohr        dbg($this->menu);
25*0470c28fSAndreas Gohr
26*0470c28fSAndreas Gohr        echo p_locale_xhtml('admin');
27*0470c28fSAndreas Gohr        $this->showSecurityCheck();
28*0470c28fSAndreas Gohr        $this->showAdminMenu();
29*0470c28fSAndreas Gohr        $this->showManagerMenu();
30*0470c28fSAndreas Gohr        $this->showVersion();
31*0470c28fSAndreas Gohr        $this->showPluginMenu();
32*0470c28fSAndreas Gohr    }
33*0470c28fSAndreas Gohr
34*0470c28fSAndreas Gohr    /**
35*0470c28fSAndreas Gohr     * Display the standard admin tasks
36*0470c28fSAndreas Gohr     */
37*0470c28fSAndreas Gohr    protected function showAdminMenu() {
38*0470c28fSAndreas Gohr        /** @var \DokuWiki_Auth_Plugin $auth */
39*0470c28fSAndreas Gohr        global $auth;
40*0470c28fSAndreas Gohr        global $INFO;
41*0470c28fSAndreas Gohr
42*0470c28fSAndreas Gohr        if(!$INFO['isadmin']) return;
43*0470c28fSAndreas Gohr
44*0470c28fSAndreas Gohr        // user manager only if the auth backend supports it
45*0470c28fSAndreas Gohr        if(!$auth || !$auth->canDo('getUsers') ) {
46*0470c28fSAndreas Gohr            if(isset($menu['usermanager'])) unset($menu['usermanager']);
47*0470c28fSAndreas Gohr        }
48*0470c28fSAndreas Gohr
49*0470c28fSAndreas Gohr        echo '<ul class="admin_tasks">';
50*0470c28fSAndreas Gohr        foreach(array('usermanager','acl', 'extension', 'config', 'styling') as $plugin) {
51*0470c28fSAndreas Gohr            if(!isset($this->menu[$plugin])) continue;
52*0470c28fSAndreas Gohr            $this->showMenuItem($this->menu[$plugin]);
53*0470c28fSAndreas Gohr            unset($this->menu[$plugin]);
54*0470c28fSAndreas Gohr        }
55*0470c28fSAndreas Gohr        echo '</ul>';
56*0470c28fSAndreas Gohr    }
57*0470c28fSAndreas Gohr
58*0470c28fSAndreas Gohr    /**
59*0470c28fSAndreas Gohr     * Display the standard manager tasks
60*0470c28fSAndreas Gohr     */
61*0470c28fSAndreas Gohr    protected function showManagerMenu() {
62*0470c28fSAndreas Gohr        echo '<ul class="admin_tasks">';
63*0470c28fSAndreas Gohr        foreach(array('revert','popularity') as $plugin) {
64*0470c28fSAndreas Gohr            if(!isset($this->menu[$plugin])) continue;
65*0470c28fSAndreas Gohr            $this->showMenuItem($this->menu[$plugin]);
66*0470c28fSAndreas Gohr            unset($this->menu[$plugin]);
67*0470c28fSAndreas Gohr        }
68*0470c28fSAndreas Gohr        echo '</ul>';
69*0470c28fSAndreas Gohr    }
70*0470c28fSAndreas Gohr
71*0470c28fSAndreas Gohr    /**
72*0470c28fSAndreas Gohr     * Display all the remaining plugins
73*0470c28fSAndreas Gohr     */
74*0470c28fSAndreas Gohr    protected function showPluginMenu() {
75*0470c28fSAndreas Gohr        if(!count($this->menu)) return;
76*0470c28fSAndreas Gohr        echo '<div class="clearer"></div>';
77*0470c28fSAndreas Gohr        echo p_locale_xhtml('adminplugins');
78*0470c28fSAndreas Gohr        echo '<ul class="admin_plugins">';
79*0470c28fSAndreas Gohr        foreach ($this->menu as $item) {
80*0470c28fSAndreas Gohr            $this->showMenuItem($item);
81*0470c28fSAndreas Gohr        }
82*0470c28fSAndreas Gohr        echo '</ul>';
83*0470c28fSAndreas Gohr    }
84*0470c28fSAndreas Gohr
85*0470c28fSAndreas Gohr    /**
86*0470c28fSAndreas Gohr     * Display the DokuWiki version
87*0470c28fSAndreas Gohr     */
88*0470c28fSAndreas Gohr    protected function showVersion() {
89*0470c28fSAndreas Gohr        echo '<div id="admin__version">';
90*0470c28fSAndreas Gohr        echo getVersion();
91*0470c28fSAndreas Gohr        echo '</div>';
92*0470c28fSAndreas Gohr    }
93*0470c28fSAndreas Gohr
94*0470c28fSAndreas Gohr    /**
95*0470c28fSAndreas Gohr     * data security check
96*0470c28fSAndreas Gohr     *
97*0470c28fSAndreas Gohr     * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
98*0470c28fSAndreas Gohr     *
99*0470c28fSAndreas Gohr     * it verifies either:
100*0470c28fSAndreas Gohr     *   'savedir' has been moved elsewhere, or
101*0470c28fSAndreas Gohr     *   has protection to prevent the webserver serving files from it
102*0470c28fSAndreas Gohr     */
103*0470c28fSAndreas Gohr    protected function showSecurityCheck() {
104*0470c28fSAndreas Gohr        global $conf;
105*0470c28fSAndreas Gohr        if(substr($conf['savedir'], 0, 2) !== './') return;
106*0470c28fSAndreas Gohr        echo '<a style="border:none; float:right;"
107*0470c28fSAndreas Gohr                href="http://www.dokuwiki.org/security#web_access_security">
108*0470c28fSAndreas Gohr                <img src="' . DOKU_URL . $conf['savedir'] . '/security.png" alt="Your data directory seems to be protected properly."
109*0470c28fSAndreas Gohr                onerror="this.parentNode.style.display=\'none\'" /></a>';
110*0470c28fSAndreas Gohr    }
111*0470c28fSAndreas Gohr
112*0470c28fSAndreas Gohr    /**
113*0470c28fSAndreas Gohr     * Display a single Admin menu item
114*0470c28fSAndreas Gohr     *
115*0470c28fSAndreas Gohr     * @param array $item
116*0470c28fSAndreas Gohr     */
117*0470c28fSAndreas Gohr    protected function showMenuItem($item) {
118*0470c28fSAndreas Gohr        global $ID;
119*0470c28fSAndreas Gohr        if(blank($item['prompt'])) return;
120*0470c28fSAndreas Gohr        echo '<li><div class="li">';
121*0470c28fSAndreas Gohr        echo '<span>';
122*0470c28fSAndreas Gohr        embedSVG($item['icon']);
123*0470c28fSAndreas Gohr        echo '</span>';
124*0470c28fSAndreas Gohr        echo '<a href="' . wl($ID, 'do=admin&amp;page=' . $item['plugin']) . '">';
125*0470c28fSAndreas Gohr        echo $item['prompt'];
126*0470c28fSAndreas Gohr        echo '</a>';
127*0470c28fSAndreas Gohr        echo '</div></li>';
128*0470c28fSAndreas Gohr    }
129*0470c28fSAndreas Gohr
130*0470c28fSAndreas Gohr    /**
131*0470c28fSAndreas Gohr     * Build  list of admin functions from the plugins that handle them
132*0470c28fSAndreas Gohr     *
133*0470c28fSAndreas Gohr     * Checks the current permissions to decide on manager or admin plugins
134*0470c28fSAndreas Gohr     *
135*0470c28fSAndreas Gohr     * @return array list of plugins with their properties
136*0470c28fSAndreas Gohr     */
137*0470c28fSAndreas Gohr    protected function getPluginList() {
138*0470c28fSAndreas Gohr        global $INFO;
139*0470c28fSAndreas Gohr        global $conf;
140*0470c28fSAndreas Gohr
141*0470c28fSAndreas Gohr        $pluginlist = plugin_list('admin');
142*0470c28fSAndreas Gohr        $menu = array();
143*0470c28fSAndreas Gohr        foreach($pluginlist as $p) {
144*0470c28fSAndreas Gohr            /** @var \DokuWiki_Admin_Plugin $obj */
145*0470c28fSAndreas Gohr            if(($obj = plugin_load('admin', $p)) === null) continue;
146*0470c28fSAndreas Gohr
147*0470c28fSAndreas Gohr            // check permissions
148*0470c28fSAndreas Gohr            if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
149*0470c28fSAndreas Gohr
150*0470c28fSAndreas Gohr            $menu[$p] = array(
151*0470c28fSAndreas Gohr                'plugin' => $p,
152*0470c28fSAndreas Gohr                'prompt' => $obj->getMenuText($conf['lang']),
153*0470c28fSAndreas Gohr                'icon' => $obj->getMenuIcon(),
154*0470c28fSAndreas Gohr                'sort' => $obj->getMenuSort(),
155*0470c28fSAndreas Gohr            );
156*0470c28fSAndreas Gohr        }
157*0470c28fSAndreas Gohr
158*0470c28fSAndreas Gohr        // sort by name, then sort
159*0470c28fSAndreas Gohr        uasort(
160*0470c28fSAndreas Gohr            $menu,
161*0470c28fSAndreas Gohr            function ($a, $b) {
162*0470c28fSAndreas Gohr                $strcmp = strcasecmp($a['prompt'], $b['prompt']);
163*0470c28fSAndreas Gohr                if($strcmp != 0) return $strcmp;
164*0470c28fSAndreas Gohr                if($a['sort'] == $b['sort']) return 0;
165*0470c28fSAndreas Gohr                return ($a['sort'] < $b['sort']) ? -1 : 1;
166*0470c28fSAndreas Gohr            }
167*0470c28fSAndreas Gohr        );
168*0470c28fSAndreas Gohr
169*0470c28fSAndreas Gohr        return $menu;
170*0470c28fSAndreas Gohr    }
171*0470c28fSAndreas Gohr
172*0470c28fSAndreas Gohr}
173