diff --git a/common/framework/config.php b/common/framework/config.php index 0d1ab7ef3..93f1eb265 100644 --- a/common/framework/config.php +++ b/common/framework/config.php @@ -136,7 +136,7 @@ class Config } // Save the main config file. - $buff = 'ftp_info); $warning = '// DO NOT EDIT THIS FILE. CHANGES WILL NOT BE APPLIED.' . "\n" . '// TO CHANGE RHYMIX SYSTEM CONFIGURATION, EDIT config.php INSTEAD.'; - $buff = '\s+array\(\n/', "=> array(\n", $value); - $value = preg_replace('/array\(\s*\n\s*\)/', 'array()', $value); - $value = preg_replace_callback('/\n(\x20+)/', function($m) { - return "\n" . str_repeat("\t", intval(strlen($m[1]) / 2)); - }, $value); - $value = preg_replace('/\n(\t+)[0-9]+ => /', "\n\$1", $value); - return $value; - } - else - { - return var_export($value, true); - } - } } diff --git a/common/framework/debug.php b/common/framework/debug.php index 04836b6b5..81e4326ff 100644 --- a/common/framework/debug.php +++ b/common/framework/debug.php @@ -274,7 +274,7 @@ class Debug if (config('debug.write_error_log') === 'all' && self::isEnabledForCurrentUser()) { $log_entry = str_replace("\0", '', sprintf('Rhymix Debug: %s in %s on line %d', - var_export($message, true), $entry->file, $entry->line)); + var_export_clean($message), $entry->file, $entry->line)); error_log($log_entry); } } diff --git a/common/framework/parsers/langparser.php b/common/framework/parsers/langparser.php index b2185ce38..8d667ba44 100644 --- a/common/framework/parsers/langparser.php +++ b/common/framework/parsers/langparser.php @@ -82,18 +82,18 @@ class LangParser { foreach ($subvalue as $subsubkey => $subsubvalue) { - $buff .= '$lang->' . $key . "['$subkey']['$subsubkey']" . ' = ' . var_export($subsubvalue, true) . ";\n"; + $buff .= '$lang->' . $key . "['$subkey']['$subsubkey']" . ' = ' . var_export_clean($subsubvalue) . ";\n"; } } else { - $buff .= '$lang->' . $key . "['$subkey']" . ' = ' . var_export($subvalue, true) . ";\n"; + $buff .= '$lang->' . $key . "['$subkey']" . ' = ' . var_export_clean($subvalue) . ";\n"; } } } else { - $buff .= '$lang->' . $key . ' = ' . var_export($value, true) . ";\n"; + $buff .= '$lang->' . $key . ' = ' . var_export_clean($value) . ";\n"; } } Storage::write($output_filename, $buff); diff --git a/common/framework/storage.php b/common/framework/storage.php index 4ac59397d..95fca7db1 100644 --- a/common/framework/storage.php +++ b/common/framework/storage.php @@ -337,7 +337,7 @@ class Storage { $comment = "/* $comment */\n"; } - return self::write($filename, '<' . '?php ' . $comment . 'return unserialize(' . var_export(serialize($data), true) . ');'); + return self::write($filename, '<' . '?php ' . $comment . 'return ' . var_export_clean($data) . ';'); } /** diff --git a/common/functions.php b/common/functions.php index 078c280e5..7e6da87a6 100644 --- a/common/functions.php +++ b/common/functions.php @@ -409,6 +409,22 @@ function url2path($url) return Rhymix\Framework\URL::toServerPath($url); } +/** + * This function fixes some of the issues with var_export() so that the result executes cleanly. + * + * @param mixed $var + * @return string + */ +function var_export_clean($var) +{ + $result = var_export($var, true); + if (version_compare(PHP_VERSION, '7.3', '<')) + { + $result = preg_replace('/stdClass::__set_state\((?=array\(\n)/', '(object)(', $result); + } + return $result; +} + /** * Convert hexadecimal color codes to an array of R, G, B values. * This function can handle both 6-digit and 3-digit notations, optionally prefixed with '#'. diff --git a/common/tpl/debug_comment.html b/common/tpl/debug_comment.html index e3213c2ba..b4f6d60b3 100644 --- a/common/tpl/debug_comment.html +++ b/common/tpl/debug_comment.html @@ -43,7 +43,7 @@ Debug Entries { if (is_scalar($entry->message)) { - $entry->message = var_export($entry->message, true); + $entry->message = var_export_clean($entry->message); } else { diff --git a/tests/unit/framework/ConfigTest.php b/tests/unit/framework/ConfigTest.php index b9ef136d2..52d372aa7 100644 --- a/tests/unit/framework/ConfigTest.php +++ b/tests/unit/framework/ConfigTest.php @@ -18,9 +18,5 @@ class ConfigTest extends \Codeception\TestCase\Test Rhymix\Framework\Config::set('foo.bar', $rand = mt_rand()); $this->assertEquals(array('bar' => $rand), Rhymix\Framework\Config::get('foo')); $this->assertEquals($rand, Rhymix\Framework\Config::get('foo.bar')); - - $var = array('foo' => 'bar'); - $serialized = "array(\n\t'foo' => 'bar',\n)"; - $this->assertEquals($serialized, Rhymix\Framework\Config::serialize($var)); } } diff --git a/tests/unit/functions/FunctionsTest.php b/tests/unit/functions/FunctionsTest.php index d2ee628c0..745692a90 100644 --- a/tests/unit/functions/FunctionsTest.php +++ b/tests/unit/functions/FunctionsTest.php @@ -103,6 +103,21 @@ class FunctionsTest extends \Codeception\TestCase\Test $this->assertEquals('Rhymix ^~', base64_decode_urlsafe('Umh5bWl4IF5-')); } + public function testVarExportClean() + { + $obj = new stdClass; + $obj->foo = 42; + $obj->bar = 7; + if (version_compare(PHP_VERSION, '7.3', '<')) + { + $this->assertEquals("(object)(array(\n 'foo' => 42,\n 'bar' => 7,\n))", var_export_clean($obj)); + } + else + { + $this->assertEquals("(object) array(\n 'foo' => 42,\n 'bar' => 7,\n)", var_export_clean($obj)); + } + } + public function testHex2Rgb2Hex() { $this->assertEquals(array(128, 128, 128), hex2rgb('808080'));