From fed4f950bff954fab6c328176b8ce9fcdabfcfff Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sat, 6 Feb 2016 14:57:16 +0900 Subject: [PATCH 1/2] Fix encoding of numeric arrays in XMLRPC->JSON conversion --- classes/display/JSONDisplayHandler.php | 43 ++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/classes/display/JSONDisplayHandler.php b/classes/display/JSONDisplayHandler.php index 7ed3bf2fd..2c41eca6c 100644 --- a/classes/display/JSONDisplayHandler.php +++ b/classes/display/JSONDisplayHandler.php @@ -9,14 +9,53 @@ class JSONDisplayHandler * @param ModuleObject $oModule the module object * @return string */ - function toDoc(&$oModule) + public function toDoc($oModule) { $variables = $oModule->getVariables(); $variables['error'] = $oModule->getError(); $variables['message'] = $oModule->getMessage(); + + if (Context::getRequestMethod() === 'XMLRPC') + { + $temp = array(); + foreach ($variables as $key => $value) + { + if (self::_isNumericArray($value)) + { + $temp[$key] = array_values($value); + } + else + { + $temp[$key] = $value; + } + } + $variables = $temp; + } + return json_encode($variables); } - + + /** + * Check if an array only has numeric keys. + * + * @param array $array + * @return bool + */ + protected static function _isNumericArray($array) + { + if (!is_array($array) || !count($array)) + { + return false; + } + foreach ($array as $key => $value) + { + if (intval($key) != $key) + { + return false; + } + } + return true; + } } /* End of file JSONDisplayHandler.class.php */ /* Location: ./classes/display/JSONDisplayHandler.class.php */ From e2199f19ec024a70179eef88e5991436d920e5b9 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sat, 6 Feb 2016 15:02:49 +0900 Subject: [PATCH 2/2] Apply numeric array conversion to every JSON request as well --- classes/display/JSONDisplayHandler.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/classes/display/JSONDisplayHandler.php b/classes/display/JSONDisplayHandler.php index 2c41eca6c..24d8c81b1 100644 --- a/classes/display/JSONDisplayHandler.php +++ b/classes/display/JSONDisplayHandler.php @@ -15,24 +15,20 @@ class JSONDisplayHandler $variables['error'] = $oModule->getError(); $variables['message'] = $oModule->getMessage(); - if (Context::getRequestMethod() === 'XMLRPC') + $temp = array(); + foreach ($variables as $key => $value) { - $temp = array(); - foreach ($variables as $key => $value) + if (self::_isNumericArray($value)) { - if (self::_isNumericArray($value)) - { - $temp[$key] = array_values($value); - } - else - { - $temp[$key] = $value; - } + $temp[$key] = array_values($value); + } + else + { + $temp[$key] = $value; } - $variables = $temp; } - return json_encode($variables); + return json_encode($temp); } /**