Example #1
0
/**
 * 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.
 *
 * @param string $message
 *     The error message to return.
 */
function phorum_ajax_error($message)
{
    $message = phorum_api_json_convert_to_utf8($message);
    header("HTTP/1.1 500 Phorum Ajax error");
    header("Status: 500 Phorum Ajax error");
    header("Content-Type: text/plain; charset=UTF-8");
    print $message;
    exit(1);
}
Example #2
0
function phorum_api_json_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 $k => $v) {
            $new[phorum_api_json_convert_to_utf8($k)] = phorum_api_json_convert_to_utf8($v);
        }
        $var = $new;
    } elseif (is_object($var)) {
        $vars = get_class_vars(get_class($var));
        foreach ($vars as $property => $value) {
            $var->{$property} = phorum_api_json_convert_to_utf8($value);
        }
    } elseif (is_string($var)) {
        // Fix for characters that do not survive the UTF-8 conversion somehow.
        $var = strtr($var, $cp1252_map);
        // Convert to UTF-8.
        $var = iconv($PHORUM['DATA']['CHARSET'], 'UTF-8', $var);
    }
    return $var;
}