diff --git a/library/Icinga/File/Ini/IniEditor.php b/library/Icinga/File/Ini/IniEditor.php index 6ee19fa74..e1a7984d8 100644 --- a/library/Icinga/File/Ini/IniEditor.php +++ b/library/Icinga/File/Ini/IniEditor.php @@ -433,6 +433,9 @@ class IniEditor */ private function formatKey(array $key) { + foreach ($key as $i => $separator) { + $key[$i] = $this->sanitize($separator); + } return implode($this->nestSeparator, $key); } @@ -599,7 +602,7 @@ class IniEditor } /** - * Prepare a value for INe + * Prepare a value for INI * * @param $value The value of the string * @@ -613,10 +616,12 @@ class IniEditor return $value; } elseif (is_bool($value)) { return ($value ? 'true' : 'false'); - } elseif (strpos($value, '"') === false) { - return '"' . $value . '"'; - } else { - return '"' . str_replace('"', '\"', $value) . '"'; } + return '"' . str_replace('"', '\"', $this->sanitize($value)) . '"'; + } + + private function sanitize($value) + { + return str_replace('\n', '', $value); } } diff --git a/library/Icinga/File/Ini/IniWriter.php b/library/Icinga/File/Ini/IniWriter.php index 0db8be139..c9b4b3c20 100644 --- a/library/Icinga/File/Ini/IniWriter.php +++ b/library/Icinga/File/Ini/IniWriter.php @@ -44,45 +44,6 @@ class IniWriter extends Zend_Config_Writer_FileAbstract parent::__construct($options); } - /** - * Find all keys containing dots and convert it to a nested configuration - * - * Ensure that configurations with the same ini representation the have - * similarly nested Zend_Config objects. The configuration may be altered - * during that process. - * - * @param Zend_Config $config The configuration to normalize - * @return Zend_Config The normalized config - */ - private function normalizeKeys(Zend_Config $config) - { - foreach ($config as $key => $value) { - if (preg_match('/\./', $key) > 0) { - // remove old key - unset ($config->$key); - - // insert new key - $nests = explode('.', $key); - $current = $config; - $i = 0; - for (; $i < count($nests) - 1; $i++) { - if (! isset($current->{$nests[$i]})) { - // configuration key doesn't exist, create a new nesting level - $current->{$nests[$i]} = new Zend_Config (array(), true); - } - // move to next nesting level - $current = $current->{$nests[$i]}; - } - // reached last nesting level, insert value - $current->{$nests[$i]} = $value; - } - if ($value instanceof Zend_Config) { - $config->$key = $this->normalizeKeys ($value); - } - } - return $config; - } - /** * Render the Zend_Config into a config file string * @@ -96,16 +57,6 @@ class IniWriter extends Zend_Config_Writer_FileAbstract $oldconfig = new Zend_Config(array()); } - // create an internal copy of the given configuration, since the user of this class - // won't expect that a configuration will ever be altered during - // the rendering process. - $extends = $this->_config->getExtends(); - $this->_config = new Zend_Config ($this->_config->toArray(), true); - foreach ($extends as $extending => $extended) { - $this->_config->setExtend($extending, $extended); - } - $this->_config = $this->normalizeKeys($this->_config); - $newconfig = $this->_config; $editor = new IniEditor(file_get_contents($this->_filename), $this->options); $this->diffConfigs($oldconfig, $newconfig, $editor); diff --git a/test/php/library/Icinga/File/Ini/IniWriterTest.php b/test/php/library/Icinga/File/Ini/IniWriterTest.php index 53bd750d8..1b9580fb3 100644 --- a/test/php/library/Icinga/File/Ini/IniWriterTest.php +++ b/test/php/library/Icinga/File/Ini/IniWriterTest.php @@ -29,6 +29,30 @@ class IniWriterTest extends BaseTestCase unlink($this->tempFile2); } + public function testWhetherPointInSectionIsNotNormalized() + { + $writer = new IniWriter( + array( + 'config' => new Config( + array( + 'section' => array( + 'foo.bar' => 1337 + ), + 'section.with.multiple.dots' => array( + 'some more' => array( + 'nested stuff' => 'With more values' + ) + ) + ) + ), + 'filename' => $this->tempFile + ) + ); + $writer->write(); + $config = Config::fromIni($this->tempFile)->toArray(); + $this->assertTrue(array_key_exists('section.with.multiple.dots', $config), 'Section names not normalized'); + } + public function testWhetherSimplePropertiesAreInsertedInEmptyFiles() { $this->markTestSkipped('Implementation has changed. Section-less properties are not supported anymore'); @@ -702,44 +726,6 @@ EOD; ); } - public function testKeyNormalization() - { - $normalKeys = new IniWriter( - array ( - 'config' => new Config(array ( - 'foo' => 'bar', - 'nest' => array ( - 'nested' => array ( - 'stuff' => 'nested configuration element' - ) - ), - 'preserving' => array ( - 'ini' => array( - 'writer' => 'n' - ), - 'foo' => 'this should not be overwritten' - ) - )), - 'filename' => $this->tempFile - ) - - ); - - $nestedKeys = new IniWriter( - array ( - 'config' => new Config(array ( - 'foo' => 'bar', - 'nest.nested.stuff' => 'nested configuration element', - 'preserving.ini.writer' => 'n', - 'preserving.foo' => 'this should not be overwritten' - )), - 'filename' => $this->tempFile2 - ) - - ); - $this->assertEquals($normalKeys->render(), $nestedKeys->render()); - } - /** * Write a INI-configuration string to a temporary file and return it's path *