2014-08-29 06:25:25 -04:00
|
|
|
<?php
|
2016-02-08 09:41:00 -05:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-08-29 06:25:25 -04:00
|
|
|
|
2014-11-14 04:57:14 -05:00
|
|
|
namespace Icinga\Forms;
|
2014-08-29 06:25:25 -04:00
|
|
|
|
|
|
|
|
use Exception;
|
2014-11-04 08:48:24 -05:00
|
|
|
use Zend_Form_Decorator_Abstract;
|
2015-11-26 09:34:08 -05:00
|
|
|
use Icinga\Application\Config;
|
2019-07-12 04:24:40 -04:00
|
|
|
use Icinga\Application\Hook\ConfigFormEventsHook;
|
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-08-29 06:25:25 -04:00
|
|
|
use Icinga\Web\Form;
|
2015-11-13 10:14:11 -05:00
|
|
|
use Icinga\Web\Notification;
|
2014-08-29 06:25:25 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Form base-class providing standard functionality for configuration forms
|
|
|
|
|
*/
|
|
|
|
|
class ConfigForm extends Form
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The configuration to work with
|
|
|
|
|
*
|
|
|
|
|
* @var Config
|
|
|
|
|
*/
|
|
|
|
|
protected $config;
|
|
|
|
|
|
2015-11-26 09:34:08 -05:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*
|
|
|
|
|
* Values from subforms are directly added to the returned values array instead of being grouped by the subforms'
|
|
|
|
|
* names.
|
|
|
|
|
*/
|
|
|
|
|
public function getValues($suppressArrayNotation = false)
|
|
|
|
|
{
|
|
|
|
|
$values = parent::getValues($suppressArrayNotation);
|
|
|
|
|
foreach (array_keys($this->_subForms) as $name) {
|
|
|
|
|
// Zend returns values from subforms grouped by their names, but we want them flat
|
|
|
|
|
$values = array_merge($values, $values[$name]);
|
|
|
|
|
unset($values[$name]);
|
|
|
|
|
}
|
|
|
|
|
return $values;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-29 06:25:25 -04:00
|
|
|
/**
|
|
|
|
|
* Set the configuration to use when populating the form or when saving the user's input
|
|
|
|
|
*
|
|
|
|
|
* @param Config $config The configuration to use
|
|
|
|
|
*
|
2015-04-07 08:23:26 -04:00
|
|
|
* @return $this
|
2014-08-29 06:25:25 -04:00
|
|
|
*/
|
2014-09-08 07:31:25 -04:00
|
|
|
public function setIniConfig(Config $config)
|
2014-08-29 06:25:25 -04:00
|
|
|
{
|
|
|
|
|
$this->config = $config;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 04:24:07 -04:00
|
|
|
public function isValid($formData)
|
|
|
|
|
{
|
|
|
|
|
$valid = parent::isValid($formData);
|
|
|
|
|
|
|
|
|
|
if ($valid && ConfigFormEventsHook::runIsValid($this) === false) {
|
|
|
|
|
foreach (ConfigFormEventsHook::getLastErrors() as $msg) {
|
|
|
|
|
$this->error($msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$valid = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $valid;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 10:14:11 -05:00
|
|
|
public function onSuccess()
|
|
|
|
|
{
|
|
|
|
|
$sections = array();
|
2017-11-09 03:07:15 -05:00
|
|
|
foreach (static::transformEmptyValuesToNull($this->getValues()) as $sectionAndPropertyName => $value) {
|
2015-11-13 10:14:11 -05:00
|
|
|
list($section, $property) = explode('_', $sectionAndPropertyName, 2);
|
|
|
|
|
$sections[$section][$property] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($sections as $section => $config) {
|
2022-11-02 10:34:01 -04:00
|
|
|
if ($this->isEmptyConfig($config)) {
|
|
|
|
|
$this->config->removeSection($section);
|
|
|
|
|
} else {
|
|
|
|
|
$this->config->setSection($section, $config);
|
|
|
|
|
}
|
2015-11-13 10:14:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->save()) {
|
|
|
|
|
Notification::success($this->translate('New configuration has successfully been stored'));
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-07-12 04:24:07 -04:00
|
|
|
|
|
|
|
|
if (ConfigFormEventsHook::runOnSuccess($this) === false) {
|
|
|
|
|
Notification::error($this->translate(
|
|
|
|
|
'Configuration successfully stored. Though, one or more module hooks failed to run.'
|
|
|
|
|
. ' See logs for details'
|
|
|
|
|
));
|
|
|
|
|
}
|
2015-11-13 10:14:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function onRequest()
|
|
|
|
|
{
|
|
|
|
|
$values = array();
|
|
|
|
|
foreach ($this->config as $section => $properties) {
|
|
|
|
|
foreach ($properties as $name => $value) {
|
|
|
|
|
$values[$section . '_' . $name] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->populate($values);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-29 06:25:25 -04:00
|
|
|
/**
|
|
|
|
|
* Persist the current configuration to disk
|
|
|
|
|
*
|
|
|
|
|
* If an error occurs the user is shown a view describing the issue and displaying the raw INI configuration.
|
|
|
|
|
*
|
|
|
|
|
* @return bool Whether the configuration could be persisted
|
|
|
|
|
*/
|
|
|
|
|
public function save()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2015-09-16 04:58:57 -04:00
|
|
|
$this->writeConfig($this->config);
|
2019-02-01 04:58:35 -05:00
|
|
|
} catch (ConfigurationError $e) {
|
|
|
|
|
$this->addError($e->getMessage());
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-08-29 06:25:25 -04:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
$this->addDecorator('ViewScript', array(
|
2014-09-09 07:22:51 -04:00
|
|
|
'viewModule' => 'default',
|
2014-08-29 06:25:25 -04:00
|
|
|
'viewScript' => 'showConfiguration.phtml',
|
|
|
|
|
'errorMessage' => $e->getMessage(),
|
2015-01-30 03:32:08 -05:00
|
|
|
'configString' => $this->config,
|
2014-11-04 08:48:24 -05:00
|
|
|
'filePath' => $this->config->getConfigFile(),
|
|
|
|
|
'placement' => Zend_Form_Decorator_Abstract::PREPEND
|
2014-08-29 06:25:25 -04:00
|
|
|
));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-16 04:58:57 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write the configuration to disk
|
|
|
|
|
*
|
|
|
|
|
* @param Config $config
|
|
|
|
|
*/
|
|
|
|
|
protected function writeConfig(Config $config)
|
|
|
|
|
{
|
|
|
|
|
$config->saveIni();
|
|
|
|
|
}
|
2016-12-01 04:54:09 -05:00
|
|
|
|
2022-11-02 10:34:01 -04:00
|
|
|
/**
|
|
|
|
|
* Get whether the given config is empty or has only empty values
|
|
|
|
|
*
|
|
|
|
|
* @param array|Config $config
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function isEmptyConfig($config)
|
|
|
|
|
{
|
|
|
|
|
if ($config instanceof Config) {
|
|
|
|
|
$config = $config->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($config as $value) {
|
|
|
|
|
if ($value !== null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 04:54:09 -05:00
|
|
|
/**
|
|
|
|
|
* Transform all empty values of the given array to null
|
|
|
|
|
*
|
|
|
|
|
* @param array $values
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function transformEmptyValuesToNull(array $values)
|
|
|
|
|
{
|
2017-11-09 03:07:15 -05:00
|
|
|
array_walk($values, function (&$v) {
|
|
|
|
|
if ($v === '' || $v === false || $v === array()) {
|
|
|
|
|
$v = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $values;
|
2016-12-01 04:54:09 -05:00
|
|
|
}
|
2014-08-29 06:25:25 -04:00
|
|
|
}
|