示例#1
0
/** Emit a JSON document.
*
*	Typically this is the last command in your document, but it doesn't have to be.
*
*	@param Array $out Response to output
*	@param Boolean $allow_jsonp Should JSON-P Callbacks be allowed? (Default: true)
**/
function json_Emit($out, $allow_jsonp = true)
{
    $prefix = "";
    $suffix = "";
    // By default, PHP will make '/' slashes in to '\/'. These flags fix that //
    $out_format = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
    // If 'pretty' mode (i.e. readable) //
    if (isset($_GET['pretty'])) {
        $out_format |= JSON_PRETTY_PRINT;
    }
    // JSON-P //
    if (isset($_GET['callback'])) {
        if ($allow_jsonp) {
            $callback = $_GET['callback'];
            if (json_IsValidJSONPCallback($callback)) {
                $prefix = $callback . "(";
                $suffix = ");";
            } else {
                $out = json_NewErrorResponse(400, "Invalid JSON-P Callback");
            }
        } else {
            $out = json_NewErrorResponse(401, "JSON-P Unavailable");
        }
    }
    // Debug Info //
    if (defined('CMW_PHP_DEBUG') && isset($_GET['debug'])) {
        $out['debug'] = [];
        if (isset($_SERVER['PATH_INFO'])) {
            $out['debug']['url'] = $_SERVER['PATH_INFO'];
        }
        if (getenv('REDIRECT_URL')) {
            $out['debug']['redirect_url'] = getenv('REDIRECT_URL');
        }
        if (getenv('REDIRECT_QUERY_STRING')) {
            $out['debug']['redirect_query'] = getenv('REDIRECT_QUERY_STRING');
        }
    }
    // Output the Page //
    header('Content-Type: application/json');
    echo $prefix, str_replace('</', '<\\/', json_encode($out, $out_format)), $suffix;
}
示例#2
0
/** Emit a JSON document.
*
*	Typically this is the last command in your document, but it doesn't have to be.
*
*	@param Array $out Response to output
*	@param Boolean $allow_jsonp Should JSON-P Callbacks be allowed? (Default: true)
**/
function json_Emit($out, $allow_jsonp = true)
{
    $prefix = "";
    $suffix = "";
    // By default, PHP will make '/' slashes in to '\/'. These flags fix that //
    $out_format = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
    // If 'pretty' mode (i.e. readable) //
    if (isset($_GET['pretty'])) {
        $out_format |= JSON_PRETTY_PRINT;
    }
    // JSON-P //
    if (isset($_GET['callback'])) {
        if ($allow_jsonp) {
            $callback = $_GET['callback'];
            if (json_IsValidJSONPCallback($callback)) {
                $prefix = $callback . "(";
                $suffix = ");";
            } else {
                $out = json_NewErrorResponse(400, "Invalid JSON-P Callback");
            }
        } else {
            $out = json_NewErrorResponse(401, "JSON-P Unavailable");
        }
    }
    // Debug Info //
    if (defined('CMW_PHP_DEBUG') && isset($_GET['debug'])) {
        $out['debug'] = [];
        if (isset($GLOBALS['_CORE_SCRIPT_TIMER'])) {
            $out['debug']['execute_time'] = core_MicrotimeToString($GLOBALS['_CORE_SCRIPT_TIMER']);
        }
        if (function_exists('db_GetQueryCount')) {
            $out['debug']['db_queries'] = db_GetQueryCount();
        }
        if (function_exists('cache_GetReads')) {
            $out['debug']['cache_reads'] = cache_GetReads();
            $out['debug']['cache_writes'] = cache_GetWrites();
        }
        if (function_exists('opcache_is_script_cached')) {
            // Technically, this is checking if "json.php" isn't cached //
            if (!opcache_is_script_cached(__FILE__)) {
                $out['debug']['opcache'] = "disabled";
            }
        } else {
            $out['debug']['opcache'] = "unavailable";
        }
        if (isset($_SERVER['PATH_INFO'])) {
            $out['debug']['url'] = $_SERVER['PATH_INFO'];
        }
        if (getenv('REDIRECT_URL')) {
            $out['debug']['redirect_url'] = getenv('REDIRECT_URL');
        }
        if (getenv('REDIRECT_QUERY_STRING')) {
            $out['debug']['redirect_query'] = getenv('REDIRECT_QUERY_STRING');
        }
    }
    // Output the Page //
    header('Content-Type: application/json');
    header("Cache-Control: no-cache, no-store, must-revalidate");
    // HTTP 1.1.
    //header("Pragma: no-cache"); // HTTP 1.0.
    //header("Expires: 0"); // Proxies.
    echo $prefix, str_replace('</', '<\\/', json_encode($out, $out_format)), $suffix;
}