| | |
| | | <?php |
| | | require_once('../../lib/config.inc.php'); |
| | | require_once '../../lib/config.inc.php'; |
| | | $conf['start_session'] = false; |
| | | require_once('../../lib/app.inc.php'); |
| | | require_once '../../lib/app.inc.php'; |
| | | |
| | | if($conf['demo_mode'] == true) $app->error('This function is disabled in demo mode.'); |
| | | |
| | |
| | | if( $islist ) { |
| | | $json = '[' . implode(',', array_map('__json_encode', $data) ) . ']'; |
| | | } else { |
| | | $items = Array(); |
| | | $items = array(); |
| | | foreach( $data as $key => $value ) { |
| | | $items[] = __json_encode("$key") . ':' . __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 'addcslashes' page. |
| | | // Escape non-printable or Non-ASCII characters. |
| | | // I also put the \\ character first, as suggested in comments on the 'addcslashes' 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; |