Esempio n. 1
0
function ajax_error_handler($error_code, $error_message, $error_file, $error_line)
{
    switch ($error_code) {
        case 2:
            // only a warning, ignore it
            break;
        case 2048:
            if (WP_BEIFEN_DEV_DEBUG == '1') {
                $result['status'] = __("Warning", WP_BEIFEN_DOMAIN);
                $result['message'] = __("A warning! Request aborted!", WP_BEIFEN_DOMAIN);
                $result['message'] .= '<br>Error Code:';
                $result['message'] .= $error_code;
                $result['message'] .= '<br>Message: ';
                $result['message'] .= $error_message;
                $result['message'] .= '<br>File: ';
                $result['message'] .= $error_file;
                $result['message'] .= '<br>Line: ';
                $result['message'] .= $error_line;
                echo custom_json_encode($result);
                die;
            }
            break;
        default:
            // some critical error, send error message and abort
            $result['status'] = __("Error", WP_BEIFEN_DOMAIN);
            $result['message'] = __("A critical error occured! Request aborted!", WP_BEIFEN_DOMAIN);
            // Display for debugging?
            $options = get_option(WP_BEIFEN_OPTIONS);
            if ($options['enable_debugging']) {
                $result['message'] .= '<br>Error Code:';
                $result['message'] .= $error_code;
                $result['message'] .= '<br>Message: ';
                $result['message'] .= $error_message;
                $result['message'] .= '<br>File: ';
                $result['message'] .= $error_file;
                $result['message'] .= '<br>Line: ';
                $result['message'] .= $error_line;
            }
            echo custom_json_encode($result);
            die;
    }
}
Esempio n. 2
0
/**
 * 由于php的json扩展自带的函数json_encode会将汉字转换成unicode码
 * 所以我们在这里用自定义的json_encode,这个函数不会将汉字转换为unicode码
 */
function custom_json_encode($a = false)
{
    if (is_null($a)) {
        return 'null';
    }
    if ($a === false) {
        return 'false';
    }
    if ($a === true) {
        return 'true';
    }
    if (is_scalar($a)) {
        if (is_float($a)) {
            // Always use "." for floats.
            return floatval(str_replace(",", ".", strval($a)));
        }
        if (is_string($a)) {
            static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\\"'));
            return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
        } else {
            return $a;
        }
    }
    $isList = true;
    for ($i = 0, reset($a); $i < count($a); $i++, next($a)) {
        if (key($a) !== $i) {
            $isList = false;
            break;
        }
    }
    $result = array();
    if ($isList) {
        foreach ($a as $v) {
            $result[] = custom_json_encode($v);
        }
        return '[' . join(',', $result) . ']';
    } else {
        foreach ($a as $k => $v) {
            $result[] = custom_json_encode($k) . ':' . custom_json_encode($v);
        }
        return '{' . join(',', $result) . '}';
    }
}