Remove characters in writePHPData() comment that could cause syntax error

This commit is contained in:
Kijin Sung 2023-09-05 02:07:21 +09:00
parent 206f6e706a
commit 0f1ea1dbc2
2 changed files with 10 additions and 3 deletions

View file

@ -363,15 +363,18 @@ class Storage
{
if ($comment !== null)
{
$comment = "/* $comment */\n";
$comment = '/* ' . preg_replace_callback('/[\x00-\x1f\xff*?<>\/\\\\\'"]/', function($match) {
return rawurlencode($match[0]);
}, $comment) . " */\n";
}
if ($serialize)
{
$content = '<' . '?php ' . $comment . 'return unserialize(' . var_export(serialize($data), true) . ');';
$content = '<' . '?php ' . $comment . 'return unserialize(' . var_export(serialize($data), true) . ');' . PHP_EOL;
}
else
{
$content = '<' . '?php ' . $comment . 'return ' . var_export($data, true) . ';';
$content = '<' . '?php ' . $comment . 'return ' . var_export($data, true) . ';' . PHP_EOL;
}
return self::write($filename, $content);
}

View file

@ -198,9 +198,13 @@ class StorageTest extends \Codeception\TestCase\Test
{
$testfile = \RX_BASEDIR . 'tests/_output/test.php';
$data = array('foo' => 'bar', 'baz' => array('rhymix' => '\'"special\\chars' . chr(0) . chr(255), 'test' => 'wow'));
$comment = 'Hello world */' . PHP_EOL . '?><?php return; ?>';
$this->assertTrue(Rhymix\Framework\Storage::writePHPData($testfile, $data));
$this->assertEquals($data, Rhymix\Framework\Storage::readPHPData($testfile));
$this->assertTrue(Rhymix\Framework\Storage::writePHPData($testfile, $data, $comment));
$this->assertEquals($data, Rhymix\Framework\Storage::readPHPData($testfile));
}
public function testCopy()