From 0470c28f15a56bd580c5b33b3a943d8b5429a8dd Mon Sep 17 00:00:00 2001 From: Andreas Gohr <andi@splitbrain.org> Date: Sat, 21 Jan 2017 13:48:54 +0100 Subject: [PATCH] refactor Admin UI This introduces a new dokuwiki\Ui namespace and refactors the Admin screen into a Ui class. The ultimate goal is to split up the big, complex functions in inc\html.php in better maintainable classes in the Ui namespace. This is the first go at it. Others function->class conversions should follow. This also switches the icons for our base admin plugins to inline SVG. (files and styling not included, yet). --- inc/Ui/Admin.php | 172 +++++++++++++++++++++++++++++++++++++++++++++++ inc/Ui/Ui.php | 20 ++++++ inc/common.php | 2 +- inc/html.php | 140 -------------------------------------- inc/template.php | 3 +- 5 files changed, 195 insertions(+), 142 deletions(-) create mode 100644 inc/Ui/Admin.php create mode 100644 inc/Ui/Ui.php diff --git a/inc/Ui/Admin.php b/inc/Ui/Admin.php new file mode 100644 index 000000000..24a1cc57b --- /dev/null +++ b/inc/Ui/Admin.php @@ -0,0 +1,172 @@ +<?php +namespace dokuwiki\Ui; + +/** + * Class Admin + * + * Displays the Admin screen + * + * @package dokuwiki\Ui + * @author Andreas Gohr <andi@splitbrain.org> + * @author HÃ¥kan Sandell <hakan.sandell@home.se> + */ +class Admin extends Ui { + + protected $menu; + + /** + * Display the UI element + * + * @return void + */ + public function show() { + $this->menu = $this->getPluginList(); + dbg($this->menu); + + echo p_locale_xhtml('admin'); + $this->showSecurityCheck(); + $this->showAdminMenu(); + $this->showManagerMenu(); + $this->showVersion(); + $this->showPluginMenu(); + } + + /** + * Display the standard admin tasks + */ + protected function showAdminMenu() { + /** @var \DokuWiki_Auth_Plugin $auth */ + global $auth; + global $INFO; + + if(!$INFO['isadmin']) return; + + // user manager only if the auth backend supports it + if(!$auth || !$auth->canDo('getUsers') ) { + if(isset($menu['usermanager'])) unset($menu['usermanager']); + } + + echo '<ul class="admin_tasks">'; + foreach(array('usermanager','acl', 'extension', 'config', 'styling') as $plugin) { + if(!isset($this->menu[$plugin])) continue; + $this->showMenuItem($this->menu[$plugin]); + unset($this->menu[$plugin]); + } + echo '</ul>'; + } + + /** + * Display the standard manager tasks + */ + protected function showManagerMenu() { + echo '<ul class="admin_tasks">'; + foreach(array('revert','popularity') as $plugin) { + if(!isset($this->menu[$plugin])) continue; + $this->showMenuItem($this->menu[$plugin]); + unset($this->menu[$plugin]); + } + echo '</ul>'; + } + + /** + * Display all the remaining plugins + */ + protected function showPluginMenu() { + if(!count($this->menu)) return; + echo '<div class="clearer"></div>'; + echo p_locale_xhtml('adminplugins'); + echo '<ul class="admin_plugins">'; + foreach ($this->menu as $item) { + $this->showMenuItem($item); + } + echo '</ul>'; + } + + /** + * Display the DokuWiki version + */ + protected function showVersion() { + echo '<div id="admin__version">'; + echo getVersion(); + echo '</div>'; + } + + /** + * data security check + * + * simple check if the 'savedir' is relative and accessible when appended to DOKU_URL + * + * it verifies either: + * 'savedir' has been moved elsewhere, or + * has protection to prevent the webserver serving files from it + */ + protected function showSecurityCheck() { + global $conf; + if(substr($conf['savedir'], 0, 2) !== './') return; + echo '<a style="border:none; float:right;" + href="http://www.dokuwiki.org/security#web_access_security"> + <img src="' . DOKU_URL . $conf['savedir'] . '/security.png" alt="Your data directory seems to be protected properly." + onerror="this.parentNode.style.display=\'none\'" /></a>'; + } + + /** + * Display a single Admin menu item + * + * @param array $item + */ + protected function showMenuItem($item) { + global $ID; + if(blank($item['prompt'])) return; + echo '<li><div class="li">'; + echo '<span>'; + embedSVG($item['icon']); + echo '</span>'; + echo '<a href="' . wl($ID, 'do=admin&page=' . $item['plugin']) . '">'; + echo $item['prompt']; + echo '</a>'; + echo '</div></li>'; + } + + /** + * Build list of admin functions from the plugins that handle them + * + * Checks the current permissions to decide on manager or admin plugins + * + * @return array list of plugins with their properties + */ + protected function getPluginList() { + global $INFO; + global $conf; + + $pluginlist = plugin_list('admin'); + $menu = array(); + foreach($pluginlist as $p) { + /** @var \DokuWiki_Admin_Plugin $obj */ + if(($obj = plugin_load('admin', $p)) === null) continue; + + // check permissions + if($obj->forAdminOnly() && !$INFO['isadmin']) continue; + + $menu[$p] = array( + 'plugin' => $p, + 'prompt' => $obj->getMenuText($conf['lang']), + 'icon' => $obj->getMenuIcon(), + 'sort' => $obj->getMenuSort(), + ); + } + + // sort by name, then sort + uasort( + $menu, + function ($a, $b) { + $strcmp = strcasecmp($a['prompt'], $b['prompt']); + if($strcmp != 0) return $strcmp; + if($a['sort'] == $b['sort']) return 0; + return ($a['sort'] < $b['sort']) ? -1 : 1; + } + ); + + return $menu; + } + +} diff --git a/inc/Ui/Ui.php b/inc/Ui/Ui.php new file mode 100644 index 000000000..7034302c7 --- /dev/null +++ b/inc/Ui/Ui.php @@ -0,0 +1,20 @@ +<?php +namespace dokuwiki\Ui; + +/** + * Class Ui + * + * Abstrract base class for all DokuWiki screens + * + * @package dokuwiki\Ui + */ +abstract class Ui { + + /** + * Display the UI element + * + * @return void + */ + abstract public function show(); + +} diff --git a/inc/common.php b/inc/common.php index 64c6941ba..0706fe3b3 100644 --- a/inc/common.php +++ b/inc/common.php @@ -2024,7 +2024,7 @@ function stripsourcemaps(&$text){ * @param int $maxsize maximum allowed size for the SVG to be embedded * @return bool true if the file was embedded, false otherwise */ -function embedSVG($file, $maxsize = 1024) { +function embedSVG($file, $maxsize = 1024*2) { $file = trim($file); if($file === '') return false; if(!file_exists($file)) return false; diff --git a/inc/html.php b/inc/html.php index 66eb470d8..bc5f0953a 100644 --- a/inc/html.php +++ b/inc/html.php @@ -2048,146 +2048,6 @@ function html_debug(){ print '</body></html>'; } -/** - * List available Administration Tasks - * - * @author Andreas Gohr <andi@splitbrain.org> - * @author HÃ¥kan Sandell <hakan.sandell@home.se> - */ -function html_admin(){ - global $ID; - global $INFO; - global $conf; - /** @var DokuWiki_Auth_Plugin $auth */ - global $auth; - - // build menu of admin functions from the plugins that handle them - $pluginlist = plugin_list('admin'); - $menu = array(); - foreach ($pluginlist as $p) { - /** @var DokuWiki_Admin_Plugin $obj */ - if(($obj = plugin_load('admin',$p)) === null) continue; - - // check permissions - if($obj->forAdminOnly() && !$INFO['isadmin']) continue; - - $menu[$p] = array('plugin' => $p, - 'prompt' => $obj->getMenuText($conf['lang']), - 'icon' => $obj->getMenuIcon(), - 'sort' => $obj->getMenuSort(), - ); - } - - // data security check - // simple check if the 'savedir' is relative and accessible when appended to DOKU_URL - // it verifies either: - // 'savedir' has been moved elsewhere, or - // has protection to prevent the webserver serving files from it - if (substr($conf['savedir'],0,2) == './'){ - echo '<a style="border:none; float:right;" - href="http://www.dokuwiki.org/security#web_access_security"> - <img src="'.DOKU_URL.$conf['savedir'].'/security.png" alt="Your data directory seems to be protected properly." - onerror="this.parentNode.style.display=\'none\'" /></a>'; - } - - print p_locale_xhtml('admin'); - - // Admin Tasks - if($INFO['isadmin']){ - ptln('<ul class="admin_tasks">'); - - if($menu['usermanager'] && $auth && $auth->canDo('getUsers')){ - ptln(' <li class="admin_usermanager"><div class="li">'. - '<a href="'.wl($ID, array('do' => 'admin','page' => 'usermanager')).'">'. - $menu['usermanager']['prompt'].'</a></div></li>'); - } - unset($menu['usermanager']); - - if($menu['acl']){ - ptln(' <li class="admin_acl"><div class="li">'. - '<a href="'.wl($ID, array('do' => 'admin','page' => 'acl')).'">'. - $menu['acl']['prompt'].'</a></div></li>'); - } - unset($menu['acl']); - - if($menu['extension']){ - ptln(' <li class="admin_plugin"><div class="li">'. - '<a href="'.wl($ID, array('do' => 'admin','page' => 'extension')).'">'. - $menu['extension']['prompt'].'</a></div></li>'); - } - unset($menu['extension']); - - if($menu['config']){ - ptln(' <li class="admin_config"><div class="li">'. - '<a href="'.wl($ID, array('do' => 'admin','page' => 'config')).'">'. - $menu['config']['prompt'].'</a></div></li>'); - } - unset($menu['config']); - - if($menu['styling']){ - ptln(' <li class="admin_styling"><div class="li">'. - '<a href="'.wl($ID, array('do' => 'admin','page' => 'styling')).'">'. - $menu['styling']['prompt'].'</a></div></li>'); - } - unset($menu['styling']); - } - ptln('</ul>'); - - // Manager Tasks - ptln('<ul class="admin_tasks">'); - - if($menu['revert']){ - ptln(' <li class="admin_revert"><div class="li">'. - '<a href="'.wl($ID, array('do' => 'admin','page' => 'revert')).'">'. - $menu['revert']['prompt'].'</a></div></li>'); - } - unset($menu['revert']); - - if($menu['popularity']){ - ptln(' <li class="admin_popularity"><div class="li">'. - '<a href="'.wl($ID, array('do' => 'admin','page' => 'popularity')).'">'. - $menu['popularity']['prompt'].'</a></div></li>'); - } - unset($menu['popularity']); - - // print DokuWiki version: - ptln('</ul>'); - echo '<div id="admin__version">'; - echo getVersion(); - echo '</div>'; - - // print the rest as sorted list - if(count($menu)){ - // sort by name, then sort - usort( - $menu, - function ($a, $b) { - $strcmp = strcasecmp($a['prompt'], $b['prompt']); - if($strcmp != 0) return $strcmp; - if($a['sort'] == $b['sort']) return 0; - return ($a['sort'] < $b['sort']) ? -1 : 1; - } - ); - - // output the menu - ptln('<div class="clearer"></div>'); - print p_locale_xhtml('adminplugins'); - ptln('<ul class="admin_plugins">'); - foreach ($menu as $item) { - if (!$item['prompt']) continue; - ptln('<li><div class="li">'); - ptln('<div class="admin_plugin_icon">'); - embedSVG($item['icon']); - ptln('</div>'); - ptln('<div class="admin_plugin_name"><a href="'.wl($ID, 'do=admin&page='.$item['plugin']).'">'); - ptln($item['prompt']); - ptln('</a></div>'); - ptln('</div></li>'); - } - ptln('</ul>'); - } -} - /** * Form to request a new password for an existing account * diff --git a/inc/template.php b/inc/template.php index 1e0af91c8..ca13b4616 100644 --- a/inc/template.php +++ b/inc/template.php @@ -262,7 +262,8 @@ function tpl_admin() { if($INFO['prependTOC']) tpl_toc(); $plugin->html(); } else { - html_admin(); + $admin = new dokuwiki\Ui\Admin(); + $admin->show(); } return true; } -- GitLab