diff --git a/e107_handlers/core_functions.php b/e107_handlers/core_functions.php index e27d6aef7..853d7f8e9 100644 --- a/e107_handlers/core_functions.php +++ b/e107_handlers/core_functions.php @@ -572,7 +572,16 @@ class e_array { if($mode === 'json') { - return json_encode($ArrayData, JSON_PRETTY_PRINT); + //todo discuss - move to e_parse::toJSON() ? + $encoded = json_encode($ArrayData, JSON_PRETTY_PRINT); + if(json_last_error() === JSON_ERROR_UTF8) + { + $ArrayData = e107::getParser()->toUTF8($ArrayData); + $encoded = json_encode($ArrayData, JSON_PRETTY_PRINT); + //todo log + } + + return $encoded; } $Array = var_export($ArrayData, true); @@ -586,6 +595,8 @@ class e_array { } + + /** * @DEPRECATED - Backwards Compatible. Use e107::serialize() instead; * @param array $ArrayData array to be stored @@ -628,7 +639,7 @@ class e_array { * * @param string $systemLocationFile relative to e_SYSTEM file path (without the extension) * @param string $extension [optional] file extension, default is 'php' - * @return array or false when file not found (or on error) + * @return array|false false when file not found (or on error) */ public function load($systemLocationFile, $extension = 'php') { @@ -650,7 +661,7 @@ class e_array { * * @param string $systemLocationFile relative to e_SYSTEM file path (without the extension) * @param string $extension [optional] file extension, default is 'php' - * @return array or false when file not found (or on error) + * @return array|false when file not found (or on error) */ public function store($array, $systemLocationFile, $extension = 'php') { diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 44881aa9d..f228d098c 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -2120,7 +2120,36 @@ class e_parse extends e_parser } + /** + * @param $mixed + * @return array|false|string + */ + public function toUTF8($mixed) + { + if(is_array($mixed)) + { + foreach($mixed as $k => $v) + { + unset($mixed[$k]); + $mixed[$this->toUTF8($k)] = $this->toUTF8($v); + } + } + elseif(is_object($mixed)) + { + $objVars = get_object_vars($mixed); + foreach($objVars as $key => $value) + { + $mixed->$key = $this->toUTF8($value); + } + } + elseif(is_string($mixed)) + { + return iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($mixed)); + } + + return $mixed; + } function toASCII($text) diff --git a/e107_handlers/plugin_class.php b/e107_handlers/plugin_class.php index 53bbf2b5b..38baf0347 100644 --- a/e107_handlers/plugin_class.php +++ b/e107_handlers/plugin_class.php @@ -759,6 +759,13 @@ class e_plugin $cacheSet = e107::serialize($arr,'json'); + if(empty($cacheSet)) + { + $error = json_last_error_msg(); + e107::getMessage()->addDebug("Plugin Cache JSON encoding is failing! (".__METHOD__.") Line: ".__LINE__); + e107::getMessage()->addDebug("JSON Error: ".$error); + } + e107::getCache()->set($cacheTag,$cacheSet,true,true,true); $this->_data = $arr; diff --git a/e107_tests/tests/unit/e_arrayTest.php b/e107_tests/tests/unit/e_arrayTest.php index 7e8008e87..cf0a09f28 100644 --- a/e107_tests/tests/unit/e_arrayTest.php +++ b/e107_tests/tests/unit/e_arrayTest.php @@ -108,16 +108,15 @@ $data = array ( $this->assertArrayHasKey('TITLE', $actual); - // case sitePrefs - // $string_6 = $this->getSitePrefExample(); - // $actual = $this->arrObj->unserialize($string_6); - - $tests = array( - 0 => array('string' => $this->getSitePrefExample(), + 0 => array( + 'string' => $this->getSitePrefExample(), 'expected' => array('email_password' => '$2y$10$IpizFx.gp5USl98SLXwwbeod3SYF3M3raAQX0y01ETexzoutvdyWW' ) ), - + 1 => array( + 'string' => "{\n \"hello\": \"h\u00e9ll\u00f2 w\u00f2rld\"\n}", + 'expected' => array('hello'=>'héllò wòrld') + ), ); @@ -167,5 +166,13 @@ $data = array ( $expected5 = "{\n \"hello\": \"world\"\n}"; $this->assertEquals($expected5,$result5); + $pref6 = array('hello'=> mb_convert_encoding('héllò wòrld', 'ISO-8859-1')); + $result6 = $this->arrObj->serialize($pref6,'json'); + $expected6 = "{\n \"hello\": \"h\u00e9ll\u00f2 w\u00f2rld\"\n}"; + + $this->assertEquals($expected6,$result6); + + + } }