From 7ba046604896498ec99e8c3f80930a4e94209016 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 8 Oct 2018 15:50:02 +0200 Subject: [PATCH 1/3] config/module(en|dis)able: Utilize a form instead of a link --- application/controllers/ConfigController.php | 44 ++++++++++--- application/forms/ActionForm.php | 61 +++++++++++++++++++ application/views/scripts/config/module.phtml | 21 ++----- .../views/scripts/config/modules.phtml | 2 +- 4 files changed, 101 insertions(+), 27 deletions(-) create mode 100644 application/forms/ActionForm.php diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index 6ab8d2062..d52b3aa60 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -12,6 +12,7 @@ use Icinga\Application\Modules\Module; use Icinga\Data\ResourceFactory; use Icinga\Exception\ConfigurationError; use Icinga\Exception\NotFoundError; +use Icinga\Forms\ActionForm; use Icinga\Forms\Config\GeneralConfigForm; use Icinga\Forms\Config\ResourceConfigForm; use Icinga\Forms\Config\UserBackendConfigForm; @@ -122,7 +123,20 @@ class ConfigController extends Controller $module = new Module($app, $name, $manager->getModuleDir($name)); } + $toggleForm = new ActionForm(); + $toggleForm->setDefaults(['identifier' => $name]); + if (! $this->view->moduleData->enabled) { + $toggleForm->setAction(Url::fromPath('config/moduleenable')); + $toggleForm->setDescription(sprintf($this->translate('Enable the %s module'), $name)); + } elseif ($this->view->moduleData->loaded) { + $toggleForm->setAction(Url::fromPath('config/moduledisable')); + $toggleForm->setDescription(sprintf($this->translate('Disable the %s module'), $name)); + } else { + $toggleForm = null; + } + $this->view->module = $module; + $this->view->toggleForm = $toggleForm; $this->view->tabs = $module->getConfigTabs()->activate('info'); $this->view->moduleGitCommitId = Version::getGitHead($module->getBaseDir()); } else { @@ -137,15 +151,20 @@ class ConfigController extends Controller public function moduleenableAction() { $this->assertPermission('config/modules'); - $module = $this->getParam('name'); - $manager = Icinga::app()->getModuleManager(); - try { - $manager->enableModule($module); + + $form = new ActionForm(); + $form->setOnSuccess(function (ActionForm $form) { + $module = $form->getValue('identifier'); + Icinga::app()->getModuleManager()->enableModule($module); Notification::success(sprintf($this->translate('Module "%s" enabled'), $module)); $this->rerenderLayout()->reloadCss()->redirectNow('config/modules'); + }); + + try { + $form->handleRequest(); } catch (Exception $e) { $this->view->exceptionMessage = $e->getMessage(); - $this->view->moduleName = $module; + $this->view->moduleName = $form->getValue('name'); $this->view->action = 'enable'; $this->render('module-configuration-error'); } @@ -157,15 +176,20 @@ class ConfigController extends Controller public function moduledisableAction() { $this->assertPermission('config/modules'); - $module = $this->getParam('name'); - $manager = Icinga::app()->getModuleManager(); - try { - $manager->disableModule($module); + + $form = new ActionForm(); + $form->setOnSuccess(function (ActionForm $form) { + $module = $form->getValue('identifier'); + Icinga::app()->getModuleManager()->disableModule($module); Notification::success(sprintf($this->translate('Module "%s" disabled'), $module)); $this->rerenderLayout()->reloadCss()->redirectNow('config/modules'); + }); + + try { + $form->handleRequest(); } catch (Exception $e) { $this->view->exceptionMessage = $e->getMessage(); - $this->view->moduleName = $module; + $this->view->moduleName = $form->getValue('name'); $this->view->action = 'disable'; $this->render('module-configuration-error'); } diff --git a/application/forms/ActionForm.php b/application/forms/ActionForm.php new file mode 100644 index 000000000..def3a52e3 --- /dev/null +++ b/application/forms/ActionForm.php @@ -0,0 +1,61 @@ +icon = (string) $name; + return $this; + } + + public function init() + { + $this->setAttrib('class', 'inline'); + $this->setUidDisabled(true); + $this->setDecorators(['FormElements', 'Form']); + } + + public function createElements(array $formData) + { + $this->addElement( + 'hidden', + 'identifier', + [ + 'required' => true, + 'decorators' => ['ViewHelper'] + ] + ); + $this->addElement( + 'button', + 'btn_submit', + [ + 'escape' => false, + 'type' => 'submit', + 'class' => 'link-button spinner', + 'value' => 'btn_submit', + 'decorators' => ['ViewHelper'], + 'label' => $this->getView()->icon($this->icon), + 'title' => $this->getDescription() + ] + ); + } +} diff --git a/application/views/scripts/config/module.phtml b/application/views/scripts/config/module.phtml index 397d53a12..684c62f6d 100644 --- a/application/views/scripts/config/module.phtml +++ b/application/views/scripts/config/module.phtml @@ -18,22 +18,11 @@ translate('State') ?> - - qlink( - $this->translate('disable'), - 'config/moduledisable', - array('name' => $module->getName()), - array('title' => sprintf($this->translate('Disable the %s module'), $module->getName()), 'class' => 'action-link') - ); ?> - - - qlink( - $this->translate('enable'), - 'config/moduleenable', - array('name' => $module->getName()), - array('title' => sprintf($this->translate('Enable the %s module'), $module->getName()), 'class' => 'action-link') - ); ?> - + + + toggleForm)): ?> + toggleForm ?> + escape($this->translate('Version')) ?> diff --git a/application/views/scripts/config/modules.phtml b/application/views/scripts/config/modules.phtml index b7722ce55..b13b37841 100644 --- a/application/views/scripts/config/modules.phtml +++ b/application/views/scripts/config/modules.phtml @@ -27,7 +27,7 @@ echo $this->qlink( $module->name, - 'config/module/', + 'config/module', array('name' => $module->name), array( 'class' => 'rowaction', From 4e6b93686284cdd29c94c78eb00220692f9bfba9 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 9 Oct 2018 08:30:20 +0200 Subject: [PATCH 2/3] helpers/url: Properly escape image urls --- library/Icinga/Web/View/helpers/url.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Icinga/Web/View/helpers/url.php b/library/Icinga/Web/View/helpers/url.php index 390808e5c..0d1c83e74 100644 --- a/library/Icinga/Web/View/helpers/url.php +++ b/library/Icinga/Web/View/helpers/url.php @@ -77,7 +77,7 @@ $this->addHelperFunction('img', function ($url, $params = null, array $propertie return sprintf( '', - $view->url($url, $params), + $view->escape($view->url($url, $params)), $view->propertiesToString($properties) ); }); From 86ae8c12df6f0fbfa45bc8120365342505d0a2e3 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 9 Oct 2018 10:25:46 +0200 Subject: [PATCH 3/3] IniParser: Use mode INI_SCANNER_RAW to parse ini files --- library/Icinga/File/Ini/IniParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Icinga/File/Ini/IniParser.php b/library/Icinga/File/Ini/IniParser.php index c19f0ac88..2748f78ca 100644 --- a/library/Icinga/File/Ini/IniParser.php +++ b/library/Icinga/File/Ini/IniParser.php @@ -262,7 +262,7 @@ class IniParser } try { - $configArray = parse_ini_string($content, true); + $configArray = parse_ini_string($content, true, INI_SCANNER_RAW); } catch (ErrorException $e) { throw new ConfigurationError('Couldn\'t parse the INI file `%s\'', $path, $e); }