<?php
/**
* Mudder通行证反响接口--服务端
* @author moufer<moufer@163.com>
* @copyright wa666.cn
* @version $Id: mudder_passport_server.php 2 2008-1-31 22:23 $
*/

define('IN_PASSPORT', TRUE);

require_once '../include/common.inc.php';

$mud_authkey = $_config['authkey'];
$mud_cookiepre = $cookiepre;

$keys = array('username','email','action','signhash');
foreach($keys as $val) {
    $$val='';
}

$senddata = base64_decode($senddata); 
$datas = explode('&', $senddata);
foreach($datas as $val) {
    $param = explode('=', $val);
    if(in_array($param[0], $keys) && isset($param[0])) {
        ${$param[0]} = urldecode($param[1]);
    }
}

if($action!='loginout') {

    if(!$username) {
        echo '用户名为空。';
        exit;
    } elseif(!is_safeusername($username)) {
        echo '用户名存在非法字符串。';
        exit;
    } elseif(strlen($username) > 16) {
        echo '用户名长度不能超过16位。';
        exit;
    }
    $tmphash = substr(md5($username.$mud_authkey), 0, 24);
    if($tmphash != $signhash) {
        echo '通行证验证失败。';
        exit;
    }

}

if($action == 'login') {
    $row = $db->get_one("SELECT uid FROM {$dbpre}members WHERE username='$username'");
    if($row) {
        $uid = $row['uid'];
        $db->query("UPDATE {$dbpre}members SET logintime='$timestamp',loginip='$onlineip',logincount=logincount+1 WHERE uid='{$uid}'");
    } else {
        $md5pw = md5(random(10));
        if(!$db->query("INSERT INTO {$dbpre}members (email,username,password,regdate,logintime,loginip,logincount,groupid) VALUES ('$email','$username','$md5pw','$timestamp','$timestamp','$onlineip','1','10')")) {
            $error = '注册失败!';
        } else {
            $uid = $db->insert_id();
            $spacename = $username.'的个人空间';
            $spacedescribe = "读万卷书，行万里路。";
            $db->query("INSERT INTO {$dbpre}spaces(uid,space_styleid,spacename,spacedescribe,pageview) VALUES('$uid','{$_config['space_styleid']}','$spacename','$spacedescribe','0')");
            if($_config['salutatory']) {
                $subject = '欢迎注册'.$_config['sitename'].'会员';
                $content = "敬爱的会员：$username\r\n欢迎您注册成为".$_config['sitename']."会员。\r\n\r\n".$_config['sitename']."\r\n".date("Y-m-d H:i:s",$timestamp);
                $db->query("INSERT INTO {$dbpre}pmsgs(senduid,recvuid,content,subject,posttime,new) VALUES('0','$uid','$content','$subject','$timestamp','1')");
            }
        }
    }
    echo $error ? $error : 'mudder_ok_'.$uid;
    exit;
} elseif($action == 'reg') {
    $md5pw = md5(random(10));
    if(!$db->query("INSERT INTO {$dbpre}members (email,username,password,regdate,logintime,loginip,logincount,groupid) VALUES ('$email','$username','$md5pw','$timestamp','$timestamp','$onlineip','1','10')")) {
        $error = '注册失败!';
    } else {
        $uid = $db->insert_id();
        $spacename = $username.'的个人空间';
        $spacedescribe = "读万卷书，行万里路。";
        $db->query("INSERT INTO {$dbpre}spaces(uid,space_styleid,spacename,spacedescribe,pageview) VALUES('$uid','{$_config['space_styleid']}','$spacename','$spacedescribe','0')");
        if($_config['salutatory']) {
            $subject = '欢迎注册'.$_config['sitename'].'会员';
            $content = "敬爱的会员：$username\r\n欢迎您注册成为".$_config['sitename']."会员。\r\n\r\n".$_config['sitename']."\r\n".date("Y-m-d H:i:s",$timestamp);
            $db->query("INSERT INTO {$dbpre}pmsgs(senduid,recvuid,content,subject,posttime,new) VALUES('0','$uid','$content','$subject','$timestamp','1')");
        }
    }
    echo $error ? $error : 'mudder_ok_'.$uid;
    exit;
} elseif($action == 'logout') {
    echo 'mudder_ok_0';
    exit;
} else {
    echo '未定义操作。';
    exit;
}

function is_safeusername($string) {
    global $_config;
    $censorwords = $_config['censoruser'] ? explode("\r\n", $_config['censoruser']) : array();
    if($censorwords) foreach($censorwords as $censor) {
        $preg = "/".str_replace("*", ".*?", $censor)."/is";
        if(preg_match($preg, $string)) return false;
    }
	$guestexp = '\xA1\xA1|^Guest|^\xD3\xCE\xBF\xCD|\xB9\x43\xAB\xC8';
	return !preg_match("/^\s*$|^c:\\con\\con$|[%,\*\"\s\t\<\>\&]|$guestexp/is", $string);
}
?>
