diff --git a/library/Icinga/File/Ini/Dom/Directive.php b/library/Icinga/File/Ini/Dom/Directive.php index cdf4108b8..4279a5f71 100644 --- a/library/Icinga/File/Ini/Dom/Directive.php +++ b/library/Icinga/File/Ini/Dom/Directive.php @@ -157,11 +157,10 @@ class Directive { $str = trim($str); $str = str_replace('\\', '\\\\', $str); - $str = str_replace('"', '\\"', $str); + $str = str_replace('"', '\"', $str); + $str = str_replace("\r", '\r', $str); + $str = str_replace("\n", '\n', $str); - // line breaks in the value should always match the current system EOL sequence - // to assure editable configuration files - $str = preg_replace("/(\r\n)|(\n)/", PHP_EOL, $str); return $str; } } diff --git a/library/Icinga/File/Ini/IniParser.php b/library/Icinga/File/Ini/IniParser.php index 063f1994b..6be0dec15 100644 --- a/library/Icinga/File/Ini/IniParser.php +++ b/library/Icinga/File/Ini/IniParser.php @@ -284,8 +284,8 @@ class IniParser */ protected static function unescapeSectionName($str) { - $str = str_replace('\\"', '"', $str); - $str = str_replace('\\;', ';', $str); + $str = str_replace('\"', '"', $str); + $str = str_replace('\;', ';', $str); return str_replace('\\\\', '\\', $str); } @@ -299,8 +299,11 @@ class IniParser */ protected static function unescapeOptionValue($str) { - $str = str_replace('\\"', '"', $str); + $str = str_replace('\n', "\n", $str); + $str = str_replace('\r', "\r", $str); + $str = str_replace('\"', '"', $str); + $str = str_replace('\\\\', '\\', $str); - return str_replace('\\\\', '\\', $str); + return $str; } } diff --git a/test/php/library/Icinga/File/Ini/IniParserTest.php b/test/php/library/Icinga/File/Ini/IniParserTest.php index d186d41f1..7de2e1abe 100644 --- a/test/php/library/Icinga/File/Ini/IniParserTest.php +++ b/test/php/library/Icinga/File/Ini/IniParserTest.php @@ -132,7 +132,42 @@ EOD; $this->assertEquals( 2, count(explode("\n", $doc->getSection('section')->getDirective('key1')->getValue())), - 'IniParser does not recognize multi-line values' + 'IniParser::parseIni does not recognize multi-line values' + ); + + (new IniWriter(Config::fromArray([ + 'section' => [ + 'key1' => "with some\nnewline in the value" + ] + ]), $this->tempFile))->write(); + + $doc = IniParser::parseIniFile($this->tempFile); + $this->assertEquals( + 2, + count(explode("\n", $doc->getSection('section')->get('key1'))), + 'IniParser::parseIniFile does not recognize multi-line values' + ); + } + + public function testEnvironmentVariableResolutionDoesNotWork() + { + (new IniWriter(Config::fromArray([ + 'section' => [ + 'key1' => '${PATH}_${APACHE_RUN_DIR}', + 'key2' => '${APACHE_RUN_USER}' + ] + ]), $this->tempFile))->write(); + + $configObject = IniParser::parseIniFile($this->tempFile); + $this->assertEquals( + '${PATH}_${APACHE_RUN_DIR}', + $configObject->getSection('section')->get('key1'), + 'IniParser::parseIniFile does resolve environment variables' + ); + $this->assertEquals( + '${APACHE_RUN_USER}', + $configObject->getSection('section')->get('key2'), + 'IniParser::parseIniFile does resolve environment variables' ); } } diff --git a/test/php/library/Icinga/File/Ini/IniWriterTest.php b/test/php/library/Icinga/File/Ini/IniWriterTest.php index b7481c8c8..c3fb6df1f 100644 --- a/test/php/library/Icinga/File/Ini/IniWriterTest.php +++ b/test/php/library/Icinga/File/Ini/IniWriterTest.php @@ -275,9 +275,14 @@ inkey' => 'blarg' ); $rendered = $writer->render(); + $this->assertRegExp( + '~linebreak\\\\nin line~', + $rendered, + 'newlines in values are not escaped' + ); $this->assertEquals( + 4, count(explode("\n", $rendered)), - 5, 'generated config should not contain more than three line breaks' ); }