| | |
| | | if($islist){ |
| | | $json = '[' . implode(',', array_map(array($this, "json_encode"), $data) ) . ']'; |
| | | } else { |
| | | $items = Array(); |
| | | $items = array(); |
| | | foreach( $data as $key => $value ) { |
| | | $items[] = $this->json_encode("$key") . ':' . $this->json_encode($value); |
| | | } |
| | | $json = '{' . implode(',', $items) . '}'; |
| | | } |
| | | } elseif(is_string($data)){ |
| | | # Escape non-printable or Non-ASCII characters. |
| | | # I also put the \\ character first, as suggested in comments on the 'addclashes' page. |
| | | // Escape non-printable or Non-ASCII characters. |
| | | // I also put the \\ character first, as suggested in comments on the 'addclashes' page. |
| | | $string = '"'.addcslashes($data, "\\\"\n\r\t/".chr(8).chr(12)).'"'; |
| | | $json = ''; |
| | | $len = strlen($string); |
| | | # Convert UTF-8 to Hexadecimal Codepoints. |
| | | // Convert UTF-8 to Hexadecimal Codepoints. |
| | | for($i = 0; $i < $len; $i++){ |
| | | $char = $string[$i]; |
| | | $c1 = ord($char); |
| | | |
| | | # Single byte; |
| | | // Single byte; |
| | | if($c1 <128){ |
| | | $json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1); |
| | | continue; |
| | | } |
| | | |
| | | # Double byte |
| | | // Double byte |
| | | $c2 = ord($string[++$i]); |
| | | if(($c1 & 32) === 0){ |
| | | $json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128); |
| | | continue; |
| | | } |
| | | |
| | | # Triple |
| | | // Triple |
| | | $c3 = ord($string[++$i]); |
| | | if(($c1 & 16) === 0){ |
| | | $json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128)); |
| | | continue; |
| | | } |
| | | |
| | | # Quadruple |
| | | // Quadruple |
| | | $c4 = ord($string[++$i]); |
| | | if(($c1 & 8) === 0){ |
| | | $u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1; |
| | |
| | | } |
| | | } |
| | | } else { |
| | | # int, floats, bools, null |
| | | // int, floats, bools, null |
| | | $json = strtolower(var_export($data, true)); |
| | | } |
| | | return $json; |
| | |
| | | /** IDN converter wrapper. |
| | | * all converter classes should be placed in ISPC_CLASS_PATH.'/idn/' |
| | | */ |
| | | |
| | | |
| | | private function _idn_encode_decode($domain, $encode = true) { |
| | | if($domain == '') return ''; |
| | | if(preg_match('/^[0-9\.]+$/', $domain)) return $domain; // may be an ip address - anyway does not need to bee encoded |
| | |
| | | */ |
| | | |
| | | if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') { |
| | | include_once(ISPC_CLASS_PATH.'/idn/idna_convert.class.php'); |
| | | include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php'; |
| | | $this->idn_converter = new idna_convert(array('idn_version' => 2008)); |
| | | $this->idn_converter_name = 'idna_convert.class'; |
| | | } |
| | |
| | | */ |
| | | |
| | | if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') { |
| | | include_once(ISPC_CLASS_PATH.'/idn/idna_convert.class.php'); |
| | | include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php'; |
| | | $this->idn_converter = new idna_convert(array('idn_version' => 2008)); |
| | | $this->idn_converter_name = 'idna_convert.class'; |
| | | } |