コード例 #1
0
ファイル: json.php プロジェクト: samuell/Core
/**
 * Encode a PHP variable into a JSON structure.
 *
 * JSON data is always UTF-8. In case Phorum is not using UTF-8 as the
 * default charset, the data is converted to UTF-8 automatically.
 *
 * @param mixed $var
 *     The PHP variable to encode.
 *
 * @return string
 *     The JSON encoded representation of the PHP variable.
 */
function phorum_api_json_encode($var)
{
    global $PHORUM;
    if (strtoupper($PHORUM['DATA']['CHARSET']) != 'UTF-8') {
        $var = phorum_api_charset_convert_to_utf8($var);
    }
    return json_encode($var);
}
コード例 #2
0
ファイル: charset.php プロジェクト: samuell/Core
/**
 * A helper function that converts a PHP variable from the active
 * Phorum charset (stored in $PHORUM["DATA"]["CHARSET"]) to UTF-8.
 *
 * @param mixed $var
 *     The variable to convert to UTF-8.
 *
 * @return mixed
 *     The converted variable.
 */
function phorum_api_charset_convert_to_utf8($var)
{
    global $PHORUM;
    // Don't convert if Phorum is in UTF-8 mode already.
    if (strtoupper($PHORUM['DATA']['CHARSET']) == 'UTF-8') {
        return $var;
    }
    // This character map is used to fix differences between ISO-8859-1 and
    // Windows-1252. The 1252 characters sometimes get in messages when users
    // cut-and-paste from Word and for some reason these are not handled
    // by the iconv() conversion. Thanks to Aidan Kehoe for posting this
    // map in the PHP manual pages (http://www.php.net/utf8_encode).
    static $cp1252_map = array("€" => "€", "‚" => "‚", "ƒ" => "ƒ", "„" => "„", "…" => "…", "†" => "†", "‡" => "‡", "ˆ" => "ˆ", "‰" => "‰", "Š" => "Š", "‹" => "‹", "Œ" => "Œ", "Ž" => "Ž", "‘" => "‘", "’" => "’", "“" => "“", "”" => "”", "•" => "•", "–" => "–", "—" => "—", "˜" => "˜", "™" => "™", "š" => "š", "›" => "›", "œ" => "œ", "ž" => "ž", "Ÿ" => "Ÿ");
    if (is_array($var)) {
        $new = array();
        foreach ($var as $key => $value) {
            $key = iconv($PHORUM['DATA']['CHARSET'], 'UTF-8', $key);
            if (is_string($value)) {
                $value = strtr($value, $cp1252_map);
                $value = iconv($PHORUM['DATA']['CHARSET'], 'UTF-8', $value);
            } elseif (is_array($value) || is_object($value)) {
                $value = phorum_api_charset_convert_to_utf8($value);
            }
            $new[$key] = $value;
        }
        $var = $new;
    } elseif (is_object($var)) {
        $var = clone $var;
        $vars = get_object_vars($var);
        foreach ($vars as $property => $value) {
            if (is_string($value)) {
                $value = strtr($value, $cp1252_map);
                $value = iconv($PHORUM['DATA']['CHARSET'], 'UTF-8', $value);
            } elseif (is_array($value) || is_object($value)) {
                $value = phorum_api_charset_convert_to_utf8($value);
            }
            $var->{$property} = $value;
        }
    } elseif (is_string($var)) {
        $var = strtr($var, $cp1252_map);
        $var = iconv($PHORUM['DATA']['CHARSET'], 'UTF-8', $var);
    }
    return $var;
}
コード例 #3
0
ファイル: ajax.php プロジェクト: samuell/Core
/**
 * Return an Ajax error to the caller.
 *
 * This will send an error (500 HTTP status code) message to the client,
 * using UTF-8 as the character set.
 *
 * When JSONP is used for communicating to Phorum's Ajax script, then
 * a JSONP callback is done with {error: "error message"} as the
 * response data. The calling client will have to handle this error.
 *
 * @param string $message
 *     The error message to return.
 */
function phorum_ajax_error($message)
{
    global $PHORUM;
    if ($PHORUM['ajax_jsonp'] === NULL) {
        header("HTTP/1.1 500 Phorum Ajax error");
        header("Status: 500 Phorum Ajax error");
        header("Content-Type: text/plain; charset=UTF-8");
        print phorum_api_charset_convert_to_utf8($message);
    } else {
        $return = phorum_api_json_encode(array('error' => $message));
        print $PHORUM['ajax_jsonp'] . "({$return});";
    }
    exit(1);
}