From b55c55da943f4ccd564d48fe6b218fbdada98c1a Mon Sep 17 00:00:00 2001 From: Stefan Giehl Date: Wed, 9 Nov 2022 09:19:54 +0100 Subject: [PATCH] Avoid escaping of characters other than " (#21) * only add slashed to " * more tests * fix reading --- src/IniReader.php | 14 +++++++++++--- src/IniWriter.php | 2 +- tests/BaseIniReaderTest.php | 19 +++++++++++++++++++ tests/IniWriterTest.php | 4 ++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/IniReader.php b/src/IniReader.php index 595b4f8..dafe6e7 100644 --- a/src/IniReader.php +++ b/src/IniReader.php @@ -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) { @@ -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, ";")) { @@ -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) { diff --git a/src/IniWriter.php b/src/IniWriter.php index 4b84e49..0caf49c 100644 --- a/src/IniWriter.php +++ b/src/IniWriter.php @@ -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 . '"'; } diff --git a/tests/BaseIniReaderTest.php b/tests/BaseIniReaderTest.php index 8e6ef2c..ac2d6b5 100644 --- a/tests/BaseIniReaderTest.php +++ b/tests/BaseIniReaderTest.php @@ -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 \' ']], + ]; + } } diff --git a/tests/IniWriterTest.php b/tests/IniWriterTest.php index 2e50029..5af6a7a 100644 --- a/tests/IniWriterTest.php +++ b/tests/IniWriterTest.php @@ -25,7 +25,7 @@ public function test_writeToString() 'int' => 10, 'float' => 10.3, 'array' => array( - 'string', + 'string with !"§$%&&§%/\'(%/(=', 10.3, true, false, @@ -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