Escape section names before applying regex

refs #9530
This commit is contained in:
Matthias Jentsch 2015-08-04 13:14:46 +02:00
parent ba83d6d5d9
commit d88c628b6b
2 changed files with 39 additions and 2 deletions

View File

@ -143,7 +143,7 @@ class IniEditor
if ($this->isSectionDeclaration($l)) {
return -1;
}
if (preg_match('/^\s*' . $formatted . '\[\]\s*=/', $l) === 1) {
if (preg_match('/^\s*' . preg_quote($formatted, '/') . '\[\]\s*=/', $l) === 1) {
return $line;
}
if ($this->isPropertyDeclaration($l, array_merge($key, array($index)))) {
@ -507,7 +507,7 @@ class IniEditor
private function isSectionDeclaration($lineContent, $section = null)
{
if (isset($section)) {
return preg_match('/^\s*\[\s*' . $section . '\s*[\]:]/', $lineContent) === 1;
return preg_match('/^\s*\[\s*' . preg_quote(trim($section), '/') . '\s*[\]:]/', $lineContent) === 1;
} else {
return preg_match('/^\s*\[/', $lineContent) === 1;
}

View File

@ -746,6 +746,43 @@ inkey' => 'blarg'
);
}
public function testSectionNameEscaping()
{
$config = <<<'EOD'
[section 1]
foo = 1337
[section (with special chars)]
foo = "baz"
[section/as/arbitrary/path]
foo = "nope"
[section.with.dots.in.it]
foo = "bar"
EOD;
$target = $this->writeConfigToTemporaryFile($config);
$writer = new IniWriter(
array(
'config' => Config::fromArray(
array(
'section 1' => array('foo' => 1337),
'section (with special chars)' => array('foo' => 'baz'),
'section/as/arbitrary/path' => array('foo' => 'nope'),
'section.with.dots.in.it' => array('foo' => 'bar')
)
),
'filename' => $target
)
);
$this->assertEquals(
trim($config),
trim($writer->render()),
'IniWriter does not handle special chars in section names properly.'
);
}
/**
* Write a INI-configuration string to a temporary file and return its path
*