10470c28fSAndreas Gohr<?php 2*e2d055f5SAndreas Gohr 30470c28fSAndreas Gohrnamespace dokuwiki\Ui; 40470c28fSAndreas Gohr 579a2d784SGerrit Uitslaguse dokuwiki\Extension\AdminPlugin; 6*e2d055f5SAndreas Gohruse dokuwiki\Extension\PluginInterface; 72d85e841SAndreas Gohruse dokuwiki\Utf8\Sort; 82d85e841SAndreas Gohr 90470c28fSAndreas Gohr/** 100470c28fSAndreas Gohr * Class Admin 110470c28fSAndreas Gohr * 120470c28fSAndreas Gohr * Displays the Admin screen 130470c28fSAndreas Gohr * 140470c28fSAndreas Gohr * @package dokuwiki\Ui 150470c28fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 160470c28fSAndreas Gohr * @author Håkan Sandell <hakan.sandell@home.se> 170470c28fSAndreas Gohr */ 18*e2d055f5SAndreas Gohrclass Admin extends Ui 19*e2d055f5SAndreas Gohr{ 200470c28fSAndreas Gohr 21*e2d055f5SAndreas Gohr protected $forAdmins = ['usermanager', 'acl', 'extension', 'config', 'logviewer', 'styling']; 22*e2d055f5SAndreas Gohr protected $forManagers = ['revert', 'popularity']; 2364cdf779SAndreas Gohr /** @var array[] */ 240470c28fSAndreas Gohr protected $menu; 250470c28fSAndreas Gohr 260470c28fSAndreas Gohr /** 270470c28fSAndreas Gohr * Display the UI element 280470c28fSAndreas Gohr * 290470c28fSAndreas Gohr * @return void 300470c28fSAndreas Gohr */ 31*e2d055f5SAndreas Gohr public function show() 32*e2d055f5SAndreas Gohr { 330470c28fSAndreas Gohr $this->menu = $this->getPluginList(); 345d2e38cbSAndreas Gohr echo '<div class="ui-admin">'; 350470c28fSAndreas Gohr echo p_locale_xhtml('admin'); 36052e1c84SAndreas Gohr 3764cdf779SAndreas Gohr $this->showMenu('admin'); 3864cdf779SAndreas Gohr $this->showMenu('manager'); 39052e1c84SAndreas Gohr $this->showSecurityCheck(); 400470c28fSAndreas Gohr $this->showVersion(); 4164cdf779SAndreas Gohr $this->showMenu('other'); 42713faa94SAndreas Gohr echo '</div>'; 430470c28fSAndreas Gohr } 440470c28fSAndreas Gohr 450470c28fSAndreas Gohr /** 4664cdf779SAndreas Gohr * Show the given menu of available plugins 4764cdf779SAndreas Gohr * 4864cdf779SAndreas Gohr * @param string $type admin|manager|other 490470c28fSAndreas Gohr */ 50*e2d055f5SAndreas Gohr protected function showMenu($type) 51*e2d055f5SAndreas Gohr { 5264cdf779SAndreas Gohr if (!$this->menu[$type]) return; 530470c28fSAndreas Gohr 5464cdf779SAndreas Gohr if ($type === 'other') { 550470c28fSAndreas Gohr echo p_locale_xhtml('adminplugins'); 5664cdf779SAndreas Gohr $class = 'admin_plugins'; 5764cdf779SAndreas Gohr } else { 5864cdf779SAndreas Gohr $class = 'admin_tasks'; 5964cdf779SAndreas Gohr } 6064cdf779SAndreas Gohr 6164cdf779SAndreas Gohr echo "<ul class=\"$class\">"; 6264cdf779SAndreas Gohr foreach ($this->menu[$type] as $item) { 630470c28fSAndreas Gohr $this->showMenuItem($item); 640470c28fSAndreas Gohr } 650470c28fSAndreas Gohr echo '</ul>'; 660470c28fSAndreas Gohr } 670470c28fSAndreas Gohr 680470c28fSAndreas Gohr /** 690470c28fSAndreas Gohr * Display the DokuWiki version 700470c28fSAndreas Gohr */ 71*e2d055f5SAndreas Gohr protected function showVersion() 72*e2d055f5SAndreas Gohr { 730470c28fSAndreas Gohr echo '<div id="admin__version">'; 740470c28fSAndreas Gohr echo getVersion(); 750470c28fSAndreas Gohr echo '</div>'; 760470c28fSAndreas Gohr } 770470c28fSAndreas Gohr 780470c28fSAndreas Gohr /** 790470c28fSAndreas Gohr * data security check 800470c28fSAndreas Gohr * 810470c28fSAndreas Gohr * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL 820470c28fSAndreas Gohr * 830470c28fSAndreas Gohr * it verifies either: 840470c28fSAndreas Gohr * 'savedir' has been moved elsewhere, or 850470c28fSAndreas Gohr * has protection to prevent the webserver serving files from it 86052e1c84SAndreas Gohr * 87052e1c84SAndreas Gohr * The actual check is carried out via JavaScript. See behaviour.js 880470c28fSAndreas Gohr */ 89*e2d055f5SAndreas Gohr protected function showSecurityCheck() 90*e2d055f5SAndreas Gohr { 910470c28fSAndreas Gohr global $conf; 920470c28fSAndreas Gohr if (substr($conf['savedir'], 0, 2) !== './') return; 9364159a61SAndreas Gohr $img = DOKU_URL . $conf['savedir'] . 9464159a61SAndreas Gohr '/dont-panic-if-you-see-this-in-your-logs-it-means-your-directory-permissions-are-correct.png'; 95d3f829c2SAndreas Gohr echo '<div id="security__check" data-src="' . $img . '"></div>'; 960470c28fSAndreas Gohr } 970470c28fSAndreas Gohr 980470c28fSAndreas Gohr /** 990470c28fSAndreas Gohr * Display a single Admin menu item 1000470c28fSAndreas Gohr * 1010470c28fSAndreas Gohr * @param array $item 1020470c28fSAndreas Gohr */ 103*e2d055f5SAndreas Gohr protected function showMenuItem($item) 104*e2d055f5SAndreas Gohr { 1050470c28fSAndreas Gohr global $ID; 1060470c28fSAndreas Gohr if (blank($item['prompt'])) return; 1070470c28fSAndreas Gohr echo '<li><div class="li">'; 108220b8a20SAndreas Gohr echo '<a href="' . wl($ID, 'do=admin&page=' . $item['plugin']) . '">'; 109220b8a20SAndreas Gohr echo '<span class="icon">'; 1104cd2074fSAndreas Gohr echo inlineSVG($item['icon']); 1110470c28fSAndreas Gohr echo '</span>'; 112220b8a20SAndreas Gohr echo '<span class="prompt">'; 1130470c28fSAndreas Gohr echo $item['prompt']; 114220b8a20SAndreas Gohr echo '</span>'; 1150470c28fSAndreas Gohr echo '</a>'; 1160470c28fSAndreas Gohr echo '</div></li>'; 1170470c28fSAndreas Gohr } 1180470c28fSAndreas Gohr 1190470c28fSAndreas Gohr /** 1200470c28fSAndreas Gohr * Build list of admin functions from the plugins that handle them 1210470c28fSAndreas Gohr * 1220470c28fSAndreas Gohr * Checks the current permissions to decide on manager or admin plugins 1230470c28fSAndreas Gohr * 1240470c28fSAndreas Gohr * @return array list of plugins with their properties 1250470c28fSAndreas Gohr */ 126*e2d055f5SAndreas Gohr protected function getPluginList() 127*e2d055f5SAndreas Gohr { 1280470c28fSAndreas Gohr global $conf; 1290470c28fSAndreas Gohr 1300470c28fSAndreas Gohr $pluginlist = plugin_list('admin'); 13164cdf779SAndreas Gohr $menu = ['admin' => [], 'manager' => [], 'other' => []]; 13264cdf779SAndreas Gohr 1330470c28fSAndreas Gohr foreach ($pluginlist as $p) { 13479a2d784SGerrit Uitslag /** @var AdminPlugin $obj */ 135*e2d055f5SAndreas Gohr if (!($obj = plugin_load('admin', $p)) instanceof PluginInterface) continue; 1360470c28fSAndreas Gohr 1370470c28fSAndreas Gohr // check permissions 13864cdf779SAndreas Gohr if (!$obj->isAccessibleByCurrentUser()) continue; 1390470c28fSAndreas Gohr 14064cdf779SAndreas Gohr if (in_array($p, $this->forAdmins, true)) { 14164cdf779SAndreas Gohr $type = 'admin'; 14264cdf779SAndreas Gohr } elseif (in_array($p, $this->forManagers, true)) { 14364cdf779SAndreas Gohr $type = 'manager'; 14464cdf779SAndreas Gohr } else { 14564cdf779SAndreas Gohr $type = 'other'; 14664cdf779SAndreas Gohr } 14764cdf779SAndreas Gohr 148*e2d055f5SAndreas Gohr $menu[$type][$p] = [ 1490470c28fSAndreas Gohr 'plugin' => $p, 1500470c28fSAndreas Gohr 'prompt' => $obj->getMenuText($conf['lang']), 1510470c28fSAndreas Gohr 'icon' => $obj->getMenuIcon(), 152*e2d055f5SAndreas Gohr 'sort' => $obj->getMenuSort() 153*e2d055f5SAndreas Gohr ]; 1540470c28fSAndreas Gohr } 1550470c28fSAndreas Gohr 1560470c28fSAndreas Gohr // sort by name, then sort 15764cdf779SAndreas Gohr uasort($menu['admin'], [$this, 'menuSort']); 15864cdf779SAndreas Gohr uasort($menu['manager'], [$this, 'menuSort']); 15964cdf779SAndreas Gohr uasort($menu['other'], [$this, 'menuSort']); 1600470c28fSAndreas Gohr 1610470c28fSAndreas Gohr return $menu; 1620470c28fSAndreas Gohr } 1630470c28fSAndreas Gohr 16464cdf779SAndreas Gohr /** 16564cdf779SAndreas Gohr * Custom sorting for admin menu 16664cdf779SAndreas Gohr * 16764cdf779SAndreas Gohr * We sort alphabetically first, then by sort value 16864cdf779SAndreas Gohr * 16964cdf779SAndreas Gohr * @param array $a 17064cdf779SAndreas Gohr * @param array $b 17164cdf779SAndreas Gohr * @return int 17264cdf779SAndreas Gohr */ 173*e2d055f5SAndreas Gohr protected function menuSort($a, $b) 174*e2d055f5SAndreas Gohr { 1752d85e841SAndreas Gohr $strcmp = Sort::strcmp($a['prompt'], $b['prompt']); 17664cdf779SAndreas Gohr if ($strcmp != 0) return $strcmp; 177*e2d055f5SAndreas Gohr return $a['sort'] <=> $b['sort']; 17864cdf779SAndreas Gohr } 1790470c28fSAndreas Gohr} 180