<?
include_once("./_common.php");

if (!function_exists('convert_charset')) 
{
    /*
    -----------------------------------------------------------
        Charset 转换
    -----------------------------------------------------------
    如果可以使用iconv就直接转换
    不行则使用mb_convert_encoding 
    如果两个都不支持就没有办法运行了
    */
    function convert_charset($from_charset, $to_charset, $str) 
    {

        if( function_exists('iconv') )
            return iconv($from_charset, $to_charset, $str);
        elseif( function_exists('mb_convert_encoding') )
            return mb_convert_encoding($str, $to_charset, $from_charset);
        else
            die("Not found 'iconv' or 'mbstring' library in server.");
    }
}

header("Content-Type: text/html; charset=$g4[charset]");

$subject = strtolower($_POST['subject']);
$content = strtolower(strip_tags($_POST['content']));

//gb2312 时 $config['cf_filter'] 转换为UTF-8
if (strtolower($g4[charset]) == 'gb2312') 
{
    //$subject = convert_charset('utf-8', 'cp936', $subject);
    //$content = convert_charset('utf-8', 'cp936', $content);
    $config['cf_filter'] = convert_charset('cp936', 'utf-8', $config['cf_filter']);
}

//$filter = explode(",", strtolower(trim($config['cf_filter'])));
// strtolower 的语言变形
$filter = explode(",", trim($config['cf_filter']));
for ($i=0; $i<count($filter); $i++) 
{
    $str = $filter[$i];

    // 如果标题中查找到禁用词就停止
    $subj = "";
    $pos = strpos($subject, $str);
    if ($pos !== false) 
    {
        if (strtolower($g4[charset]) == 'gb2312') 
            $subj = convert_charset('utf-8', 'cp936', $str);//cp936 转换
        else 
            $subj = $str;
        break;
    }

    // 如果内容中找到禁用词就停止
    $cont = "";
    $pos = strpos($content, $str);
    if ($pos !== false) 
    {
        if (strtolower($g4[charset]) == 'gb2312') 
            $cont = convert_charset('utf-8', 'cp936', $str);//cp936 转换
        else 
            $cont = $str;
        break;
    }
}

die("{\"subject\":\"$subj\",\"content\":\"$cont\"}");
?>