diff options
Diffstat (limited to 'libs/upgradephp')
-rw-r--r-- | libs/upgradephp/upgrade.php | 376 |
1 files changed, 191 insertions, 185 deletions
diff --git a/libs/upgradephp/upgrade.php b/libs/upgradephp/upgrade.php index 90677cf8c8..57204a5547 100644 --- a/libs/upgradephp/upgrade.php +++ b/libs/upgradephp/upgrade.php @@ -106,75 +106,78 @@ if(!defined('E_USER_DEPRECATED')) { define('E_USER_DEPRECATED', 16384); } * @param $var mixed PHP variable/array/object * @return string transformed into JSON equivalent */ -if (!function_exists("json_encode")) { - function json_encode($var, /*emu_args*/$obj=FALSE) { - - #-- handle locale differences - $locale = localeconv(); +function _json_encode($var, /*emu_args*/$obj=FALSE) { - #-- prepare JSON string - $json = ""; - - #-- add array entries - if (is_array($var) || ($obj=is_object($var))) { - - #-- check if array is associative - if (!$obj) { - $expect = 0; - foreach ((array)$var as $i=>$v) { - if (!is_int($i) || $i !== $expect++) { - $obj = 1; - break; - } - } - } + #-- handle locale differences + $locale = localeconv(); - #-- concat invidual entries + #-- prepare JSON string + $json = ""; + + #-- add array entries + if (is_array($var) || ($obj=is_object($var))) { + + #-- check if array is associative + if (!$obj) { + $expect = 0; foreach ((array)$var as $i=>$v) { - $json .= ($json !== '' ? "," : "") // comma separators - . ($obj ? ("\"$i\":") : "") // assoc prefix - . (json_encode($v)); // value + if (!is_int($i) || $i !== $expect++) { + $obj = 1; + break; + } } - - #-- enclose into braces or brackets - $json = $obj ? "{".$json."}" : "[".$json."]"; } - #-- strings need some care - elseif (is_string($var)) { - if (!utf8_decode($var)) { - $var = utf8_encode($var); - } - $var = str_replace(array("\\", "\"", "/", "\b", "\f", "\n", "\r", "\t"), array("\\\\", '\"', "\\/", "\\b", "\\f", "\\n", "\\r", "\\t"), $var); - $json = '"' . $var . '"'; - //@COMPAT: for fully-fully-compliance $var = preg_replace("/[\000-\037]/", "", $var); + #-- concat invidual entries + foreach ((array)$var as $i=>$v) { + $json .= ($json !== '' ? "," : "") // comma separators + . ($obj ? ("\"$i\":") : "") // assoc prefix + . (json_encode($v)); // value } - #-- basic types - elseif (is_bool($var)) { - $json = $var ? "true" : "false"; - } - elseif ($var === NULL) { - $json = "null"; - } - elseif (is_int($var)) { - $json = "$var"; - } - elseif (is_float($var)) { - $json = str_replace( - array($locale['mon_thousands_sep'], $locale['mon_decimal_point']), - array('', '.'), - $var - ); - } + #-- enclose into braces or brackets + $json = $obj ? "{".$json."}" : "[".$json."]"; + } - #-- something went wrong - else { - trigger_error("json_encode: don't know what a '" .gettype($var). "' is.", E_USER_ERROR); + #-- strings need some care + elseif (is_string($var)) { + if (!utf8_decode($var)) { + $var = utf8_encode($var); } - - #-- done - return($json); + $var = str_replace(array("\\", "\"", "/", "\b", "\f", "\n", "\r", "\t"), array("\\\\", '\"', "\\/", "\\b", "\\f", "\\n", "\\r", "\\t"), $var); + $json = '"' . $var . '"'; + //@COMPAT: for fully-fully-compliance $var = preg_replace("/[\000-\037]/", "", $var); + } + + #-- basic types + elseif (is_bool($var)) { + $json = $var ? "true" : "false"; + } + elseif ($var === NULL) { + $json = "null"; + } + elseif (is_int($var)) { + $json = "$var"; + } + elseif (is_float($var)) { + $json = str_replace( + array($locale['mon_thousands_sep'], $locale['mon_decimal_point']), + array('', '.'), + $var + ); + } + + #-- something went wrong + else { + trigger_error("json_encode: don't know what a '" .gettype($var). "' is.", E_USER_ERROR); + } + + #-- done + return($json); +} +if (!function_exists("json_encode")) { + function json_encode($var, /*emu_args*/$obj=FALSE) { + return _json_encode($var); } } @@ -198,161 +201,164 @@ if (!function_exists("json_encode")) { * @param $assoc bool (optional) if outer shell should be decoded as object always * @return mixed parsed into PHP variable/array/object */ -if (!function_exists("json_decode")) { - function json_decode($json, $assoc=FALSE, /*emu_args*/$n=0,$state=0,$waitfor=0) { +function _json_decode($json, $assoc=FALSE, /*emu_args*/$n=0,$state=0,$waitfor=0) { - #-- result var - $val = NULL; - static $lang_eq = array("true" => TRUE, "false" => FALSE, "null" => NULL); - static $str_eq = array("n"=>"\012", "r"=>"\015", "\\"=>"\\", '"'=>'"', "f"=>"\f", "b"=>"\b", "t"=>"\t", "/"=>"/"); + #-- result var + $val = NULL; + static $lang_eq = array("true" => TRUE, "false" => FALSE, "null" => NULL); + static $str_eq = array("n"=>"\012", "r"=>"\015", "\\"=>"\\", '"'=>'"', "f"=>"\f", "b"=>"\b", "t"=>"\t", "/"=>"/"); - #-- flat char-wise parsing - for (/*n*/; $n<strlen($json); /*n*/) { - $c = $json[$n]; + #-- flat char-wise parsing + for (/*n*/; $n<strlen($json); /*n*/) { + $c = $json[$n]; - #-= in-string - if ($state==='"') { + #-= in-string + if ($state==='"') { - if ($c == '\\') { - $c = $json[++$n]; - // simple C escapes - if (isset($str_eq[$c])) { - $val .= $str_eq[$c]; - } + if ($c == '\\') { + $c = $json[++$n]; + // simple C escapes + if (isset($str_eq[$c])) { + $val .= $str_eq[$c]; + } - // here we transform \uXXXX Unicode (always 4 nibbles) references to UTF-8 - elseif ($c == "u") { - // read just 16bit (therefore value can't be negative) - $hex = hexdec( substr($json, $n+1, 4) ); - $n += 4; - // Unicode ranges - if ($hex < 0x80) { // plain ASCII character - $val .= chr($hex); - } - elseif ($hex < 0x800) { // 110xxxxx 10xxxxxx - $val .= chr(0xC0 + $hex>>6) . chr(0x80 + $hex&63); - } - elseif ($hex <= 0xFFFF) { // 1110xxxx 10xxxxxx 10xxxxxx - $val .= chr(0xE0 + $hex>>12) . chr(0x80 + ($hex>>6)&63) . chr(0x80 + $hex&63); - } - // other ranges, like 0x1FFFFF=0xF0, 0x3FFFFFF=0xF8 and 0x7FFFFFFF=0xFC do not apply + // here we transform \uXXXX Unicode (always 4 nibbles) references to UTF-8 + elseif ($c == "u") { + // read just 16bit (therefore value can't be negative) + $hex = hexdec( substr($json, $n+1, 4) ); + $n += 4; + // Unicode ranges + if ($hex < 0x80) { // plain ASCII character + $val .= chr($hex); } - - // no escape, just a redundant backslash - //@COMPAT: we could throw an exception here - else { - $val .= "\\" . $c; + elseif ($hex < 0x800) { // 110xxxxx 10xxxxxx + $val .= chr(0xC0 + $hex>>6) . chr(0x80 + $hex&63); + } + elseif ($hex <= 0xFFFF) { // 1110xxxx 10xxxxxx 10xxxxxx + $val .= chr(0xE0 + $hex>>12) . chr(0x80 + ($hex>>6)&63) . chr(0x80 + $hex&63); } + // other ranges, like 0x1FFFFF=0xF0, 0x3FFFFFF=0xF8 and 0x7FFFFFFF=0xFC do not apply } - // end of string - elseif ($c == '"') { - $state = 0; + // no escape, just a redundant backslash + //@COMPAT: we could throw an exception here + else { + $val .= "\\" . $c; } + } - // yeeha! a single character found!!!!1! - else/*if (ord($c) >= 32)*/ { //@COMPAT: specialchars check - but native json doesn't do it? - $val .= $c; - } + // end of string + elseif ($c == '"') { + $state = 0; } - #-> end of sub-call (array/object) - elseif ($waitfor && (strpos($waitfor, $c) !== false)) { - return array($val, $n); // return current value and state + // yeeha! a single character found!!!!1! + else/*if (ord($c) >= 32)*/ { //@COMPAT: specialchars check - but native json doesn't do it? + $val .= $c; } - - #-= in-array - elseif ($state===']') { - list($v, $n) = json_decode($json, 0, $n, 0, ",]"); - $val[] = $v; - if ($json[$n] == "]") { return array($val, $n); } + } + + #-> end of sub-call (array/object) + elseif ($waitfor && (strpos($waitfor, $c) !== false)) { + return array($val, $n); // return current value and state + } + + #-= in-array + elseif ($state===']') { + list($v, $n) = json_decode($json, 0, $n, 0, ",]"); + $val[] = $v; + if ($json[$n] == "]") { return array($val, $n); } + } + + #-= in-object + elseif ($state==='}') { + list($i, $n) = json_decode($json, 0, $n, 0, ":"); // this allowed non-string indicies + list($v, $n) = json_decode($json, $assoc, $n+1, 0, ",}"); + $val[$i] = $v; + if ($json[$n] == "}") { return array($val, $n); } + } + + #-- looking for next item (0) + else { + + #-> whitespace + if (preg_match("/\s/", $c)) { + // skip } - #-= in-object - elseif ($state==='}') { - list($i, $n) = json_decode($json, 0, $n, 0, ":"); // this allowed non-string indicies - list($v, $n) = json_decode($json, $assoc, $n+1, 0, ",}"); - $val[$i] = $v; - if ($json[$n] == "}") { return array($val, $n); } + #-> string begin + elseif ($c == '"') { + $state = '"'; } - #-- looking for next item (0) - else { - - #-> whitespace - if (preg_match("/\s/", $c)) { - // skip + #-> object + elseif ($c == "{") { + list($val, $n) = json_decode($json, $assoc, $n+1, '}', "}"); + if ($val && $n && !$assoc) { + $obj = new stdClass(); + foreach ($val as $i=>$v) { + $obj->{$i} = $v; + } + $val = $obj; + unset($obj); } + } + #-> array + elseif ($c == "[") { + list($val, $n) = json_decode($json, $assoc, $n+1, ']', "]"); + } - #-> string begin - elseif ($c == '"') { - $state = '"'; - } + #-> comment + elseif (($c == "/") && ($json[$n+1]=="*")) { + // just find end, skip over + ($n = strpos($json, "*/", $n+1)) or ($n = strlen($json)); + } - #-> object - elseif ($c == "{") { - list($val, $n) = json_decode($json, $assoc, $n+1, '}', "}"); - if ($val && $n && !$assoc) { - $obj = new stdClass(); - foreach ($val as $i=>$v) { - $obj->{$i} = $v; - } - $val = $obj; - unset($obj); - } + #-> numbers + elseif (preg_match("#^(-?\d+(?:\.\d+)?)(?:[eE]([-+]?\d+))?#", substr($json, $n), $uu)) { + $val = $uu[1]; + $n += strlen($uu[0]) - 1; + if (strpos($val, ".")) { // float + $val = (float)$val; } - #-> array - elseif ($c == "[") { - list($val, $n) = json_decode($json, $assoc, $n+1, ']', "]"); + elseif ($val[0] == "0") { // oct + $val = octdec($val); } - - #-> comment - elseif (($c == "/") && ($json[$n+1]=="*")) { - // just find end, skip over - ($n = strpos($json, "*/", $n+1)) or ($n = strlen($json)); + else { + $val = (int)$val; } - - #-> numbers - elseif (preg_match("#^(-?\d+(?:\.\d+)?)(?:[eE]([-+]?\d+))?#", substr($json, $n), $uu)) { - $val = $uu[1]; - $n += strlen($uu[0]) - 1; - if (strpos($val, ".")) { // float - $val = (float)$val; - } - elseif ($val[0] == "0") { // oct - $val = octdec($val); - } - else { - $val = (int)$val; - } - // exponent? - if (isset($uu[2])) { - $val *= pow(10, (int)$uu[2]); - } + // exponent? + if (isset($uu[2])) { + $val *= pow(10, (int)$uu[2]); } + } - #-> boolean or null - elseif (preg_match("#^(true|false|null)\b#", substr($json, $n), $uu)) { - $val = $lang_eq[$uu[1]]; - $n += strlen($uu[1]) - 1; - } + #-> boolean or null + elseif (preg_match("#^(true|false|null)\b#", substr($json, $n), $uu)) { + $val = $lang_eq[$uu[1]]; + $n += strlen($uu[1]) - 1; + } - #-- parsing error - else { - // PHPs native json_decode() breaks here usually and QUIETLY - trigger_error("json_decode: error parsing '$c' at position $n", E_USER_WARNING); - return $waitfor ? array(NULL, 1<<30) : NULL; - } + #-- parsing error + else { + // PHPs native json_decode() breaks here usually and QUIETLY + trigger_error("json_decode: error parsing '$c' at position $n", E_USER_WARNING); + return $waitfor ? array(NULL, 1<<30) : NULL; + } - }//state - - #-- next char - if ($n === NULL) { return NULL; } - $n++; - }//for + }//state + + #-- next char + if ($n === NULL) { return NULL; } + $n++; + }//for - #-- final result - return ($val); + #-- final result + return ($val); +} +if (!function_exists("json_decode")) { + function json_decode($json, $assoc=FALSE) { + return _json_decode($json, $assoc); } } |