Skip to content

Commit

Permalink
Avoid escaping of characters other than " (#21)
Browse files Browse the repository at this point in the history
* only add slashed to "

* more tests

* fix reading
  • Loading branch information
sgiehl authored Nov 9, 2022
1 parent 14c8cf5 commit b55c55d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/IniReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function readComments($filename)
continue;
}

list($key, $value) = explode('=', $line, 2);
[$key, $value] = explode('=', $line, 2);

$key = trim($key);
if (strpos($key, '[]') === strlen($key) - 2) {
Expand Down Expand Up @@ -249,7 +249,7 @@ private function readWithAlternativeImplementation($ini)
}

// Key-value pair
list($key, $value) = explode('=', $line, 2);
[$key, $value] = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
if (strstr($value, ";")) {
Expand Down Expand Up @@ -284,7 +284,15 @@ private function readWithAlternativeImplementation($ini)
}

if (is_string($value)) {
$value = trim($value, "'\"");
if (preg_match('/^"(.*)"$/', $value)) {
$value = preg_replace('/^"(.*)"$/', '$1', $value);
$value = str_replace('\"', '"', $value);
} elseif (preg_match("/^'(.*)'$/", $value)) {
$value = preg_replace("/^'(.*)'$/", '$1', $value);
$value = str_replace("\'", "'", $value);
} else {
$value = trim($value, "'\"");
}
}

if ($i == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/IniWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private function encodeValue($value)
if (is_string($value)) {
// remove any quotes w/ newlines after it since INI parsing will consider it the end of the string
$value = preg_replace('/\"[\n\r]/', "\n", $value);
$value = addslashes($value);
$value = addcslashes($value, '"');
return '"' . $value . '"';
}

Expand Down
19 changes: 19 additions & 0 deletions tests/BaseIniReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,23 @@ public function test_readBoolKeys()

self::assertEquals($expected, $result);
}

/**
* @dataProvider getSpecialCharsAndEscapingTests
*/
public function test_readSpecialCharsAndEscaping($in, $out)
{
self::assertSame($out, $this->reader->readString($in));
}

public function getSpecialCharsAndEscapingTests()
{
return [
['key = "test \" test"', ['key' => 'test " test']],
['key = "test \" \' test"', ['key' => 'test " \' test']],
['key = "test \" \\\' test"', ['key' => 'test " \\\' test']],
['key = "\' test"', ['key' => '\' test']],
['key = "test \' "', ['key' => 'test \' ']],
];
}
}
4 changes: 2 additions & 2 deletions tests/IniWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function test_writeToString()
'int' => 10,
'float' => 10.3,
'array' => array(
'string',
'string with !"§$%&&§%/\'(%/(=',
10.3,
true,
false,
Expand All @@ -42,7 +42,7 @@ public function test_writeToString()
bool_false = 0
int = 10
float = 10.3
array[] = "string"
array[] = "string with !\"§$%&&§%/'(%/(="
array[] = 10.3
array[] = 1
array[] = 0
Expand Down

0 comments on commit b55c55d

Please sign in to comment.