function toJSON($arr) { $json_str = ""; if (is_array($arr)) { $pure_array = true; $array_length = count($arr); for ($i = 0; $i < $array_length; $i++) { if (!isset($arr[$i])) { $pure_array = false; break; } } if ($pure_array) { $json_str = "["; $temp = array(); for ($i = 0; $i < $array_length; $i++) { $temp[] = sprintf("%s", toJSON($arr[$i])); } $json_str .= implode(",", $temp); $json_str .= "]"; } else { $json_str = "{"; $temp = array(); foreach ($arr as $key => $value) { $temp[] = sprintf("\"%s\":%s", $key, toJSON($value)); } $json_str .= implode(",", $temp); $json_str .= "}"; } } else { if (is_string($arr)) { $json_str = "\"" . json_encode_string($arr) . "\""; } else { if (is_numeric($arr)) { $json_str = $arr; } else { if (is_null($arr)) { $json_str = 'null'; } else { $json_str = "\"" . json_encode_string($arr) . "\""; } } } } return $json_str; }
/** * This function is required for older PHP servers to provide equivalent * behavior with json_encode($subject) which is not available prior to PHP 5.2 * (JSON: JavaScript Over Network; used for serialized JS objects) * * @param $subject mixed; scalar or array (indexed or associative) * @return JSON-encoded string */ function php_json_encode(&$subject) { $result = ""; if (is_array($subject)) { $indexed = true; $array_length = count($subject); for ($i = 0; $i < $array_length; $i++) { if (!array_key_exists($i, $subject)) { $indexed = false; break; } } if ($indexed) { $result = "["; $buffer = array(); for ($i = 0; $i < $array_length; $i++) { $buffer[] = sprintf("%s", php_json_encode($subject[$i])); } $result .= implode(",", $buffer); $result .= "]"; } else { $result = "{"; $buffer = array(); foreach ($subject as $key => $value) { $buffer[] = sprintf("\"%s\":%s", $key, php_json_encode($value)); } $result .= implode(",", $buffer); $result .= "}"; } } else { if (is_numeric($subject)) { $result = $subject; } else { if (is_object($subject)) { $result = php_json_encode(get_object_vars($subject)); } else { $result = "\"" . json_encode_string($subject) . "\""; } } } return $result; }
preg_replace('#:[0-9]+$#', '', $_SERVER['HTTP_HOST']) . ($http_port === 80 || $http_port === 443 ? '' : ( ':' . $_SERVER['SERVER_PORT'] )) . dirname($_SERVER['SCRIPT_NAME']). '/' . $locationFile ), ');'; exit; } else { $response = array('error' => 'Failed to rename the temporary file'); } } } if(is_array($tmp) && isset($tmp['location']) && file_exists($tmp['location'])) { //remove temporary file if an error occurred unlink($tmp['location']); } //errors setHeaders(true);//no-cache remove_old_files(); echo $param_callback, '(', json_encode_string( 'error: html2canvas-proxy-php: ' . $response['error'] ), ');';
$response = array('error' => 'Download the file was made, but there was some problem and now the file is empty, try again'); } else { $response['mime'] = str_replace(array('windows-bmp', 'ms-bmp'), 'bmp', str_replace('jpeg', 'jpg', str_replace('xhtml+xml', 'xhtml', str_replace(array('image/', 'text/', 'application/'), '', $response['mime'])))); $locationFile = preg_replace('#[.][0-9_]+$#', '.' . $response['mime'], $tmp['location']); if (file_exists($locationFile)) { unlink($locationFile); } if (rename($tmp['location'], $locationFile)) { //success $tmp = $response = null; //set cache setHeaders(false); remove_old_files(); echo $param_callback, '(', json_encode_string(($http_port === 443 ? 'https://' : 'http://') . preg_replace('#:[0-9]+$#', '', $_SERVER['HTTP_HOST']) . ($http_port === 80 || $http_port === 443 ? '' : ':' . $_SERVER['SERVER_PORT']) . dirname($_SERVER['SCRIPT_NAME']) . '/' . $locationFile), ');'; exit; } else { $response = array('error' => 'Failed to rename the temporary file'); } } } } if (is_array($tmp) && isset($tmp['location']) && file_exists($tmp['location'])) { //remove temporary file if an error occurred unlink($tmp['location']); } //errors setHeaders(true); //no-cache remove_old_files(); echo $param_callback, '(', json_encode_string('error: html2canvas-proxy-php: ' . $response['error']), ');';
function json_encode($var, $obj = FALSE) { #-- prepare JSON string $json = ""; #-- add array entries if (is_array($var) || ($obj = is_object($var))) { #-- check if array is associative if (!$obj) { foreach ((array) $var as $i => $v) { if (!is_int($i)) { $obj = 1; break; } } } #-- concat invidual entries foreach ((array) $var as $i => $v) { $json .= ($json ? "," : "") . ($obj ? "\"{$i}\":" : "") . json_encode($v); // value } #-- enclose into braces or brackets $json = $obj ? "{" . $json . "}" : "[" . $json . "]"; } 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); $var = json_encode_string($var); $json = '"' . $var . '"'; //@COMPAT: for fully-fully-compliance $var = preg_replace("/[\000-\037]/", "", $var); } elseif (is_bool($var)) { $json = $var ? "true" : "false"; } elseif ($var === NULL) { $json = "null"; } elseif (is_int($var) || is_float($var)) { $json = "{$var}"; } else { trigger_error("json_encode: don't know what a '" . gettype($var) . "' is.", E_USER_ERROR); } #-- done return $json; }
function out_json($array_data = array()) { $array_temp = array(); foreach ($array_data as $k => $v) { $array_temp[] = '"' . json_encode_string($k) . '":"' . json_encode_string($v) . '"'; } $string_temp = join(",\r\n\t", $array_temp); die("{\r\n\t" . $string_temp . "\r\n}"); }