<?php
header("Content-type:text/html;charset=utf-8");
error_reporting(0);
require_once ('../configuration/config.inc.php');
$Action = $_REQUEST["Action"];
$uName = $_POST["uName"];
$uPas = $_POST["uPas"];
$oPas = $_POST["oPas"];
$Sid = $_POST["Sid"];
$scode = strtolower($_POST["sCode"]);
$email = '123@163.com';
$qqq = $qq;
$maxnum2 = $maxnum;
if($Action == 'Register'){
    register($uName, $email, $uPas, $Sid, $scode, $qqq, $maxnum2);
    $_SESSION['accountName'] = $uName;
    $_SESSION['password'] = $uPas;
    echo '注册成功，点确定进行自动跳转。';
}
if($Action == 'Login'){
    try{
        login($uName, $uPas, $qqq);
        $_SESSION['accountName'] = $uName;
        $_SESSION['password'] = $uPas;
        echo '登陆成功，点击确定自动跳转';
    }
    catch(Exception $e){
        echo '用户名或者密码错误！请重新登录！';
        exit;
    }
}
if($Action == 'ChangePass'){
    try{
        $uName = $_SESSION['accountName'];
        change_password($uName, $oPas, $uPas);
        echo('dsdsdsf');
    }
    catch(Exception $e){
        echo '旧密码错误！请重试！';
        exit;
    }
}

if($Action == 'SignOut'){
    logout();
    echo '用户退出系统成功。';
}
function logout(){
    $_SESSION['accountName'] = '';
    $_SESSION['password'] = '';
}
function login($account, $password, $qqq){
    $ip = GetIP();
    $sql = "select  *  from account where name='" . $account . "'";
    $result = mysql_query($sql);
    $row7 = mysql_fetch_array($result);
    $sql3 = "select  *  from blockade_ip where lock_ip='" . $row7['netbar_ip'] . "'";
    $result3 = mysql_query($sql3);
    $total3 = mysql_num_rows($result3);
    if ($total3 > 0){
        echo "你的位置已经被我们锁定！挂逼滚！";
        exit();
    }
    $sql = "select  *  from blockade_ip where lock_ip='" . $ip . "'";
    $result = mysql_query($sql);
    $total = mysql_num_rows($result);
    if ($total > 0){
        echo "你所在地区禁止登录！";
        exit();
    }
    $sql = "select  *  from ip where INET_ATON('$ip') between INET_ATON(startIp) and INET_ATON(endIp)";
    $result = mysql_query($sql);
    $row8 = mysql_fetch_array($result);
    $sql = "select  *  from jzip where Country='" . $row8['Country'] . "'";
    $result = mysql_query($sql);
    $total = mysql_num_rows($result);
    $sql2 = "select  *  from baidan where name='" . $account . "'";
    $result2 = mysql_query($sql2);
    $total2 = mysql_num_rows($result2);
    if ($total > 0 and $total2 < 1){
        echo "你所在地区禁止登录，如有问题请联系：" . $qqq . ",提供你的账号申请登录即可！";
        exit();
    }
    $sql = "select * from account where name='" . $account . "' and password = '" . $password . "'";
    $result = mysql_query($sql);
    $total = mysql_num_rows($result);
    if (!$result){
        throw new Exception('登录失败！');
    }
    if ($total > 0){
        $ip = GetIP();
        $srcDataStr = date("Y-m-d H:i:s");
        $sql = "UPDATE account SET last_login_ip='$ip',last_login_time='$srcDataStr' where name='$account'";
        $result = mysql_query($sql);
        return true;
    }else{
        throw new Exception('登录失败！');
    }
}
function register($account, $email, $password, $Sid = '', $scode, $qqq, $maxnum2){
    $ip = GetIP();
    if($_SESSION["VerifyCode"] == null || $_SESSION["VerifyCode"] == "" || $scode != $_SESSION["VerifyCode"]){
        echo ('验证码错误');
        exit();
    }
    $isTure = preg_match('/^[0-9a-zA-Z]+$/', $account);
    if(!$isTure){
        exit('注册失败，帐号只能包含英文和数字');
    }
    $sql = "select * from ".$database.".account where name='" . $account . "'";
    $result = mysql_query($sql);
    $totala = mysql_num_rows($result);
    if (!$result){
        echo ('数据库表不存在或者未连接。');
        exit();
    }
    if ($totala > 0){
        echo ('账号名被占用，请重新输入');
        exit();
    }
    $sql = "select last_login_ip from ".$database.".account where last_login_ip='" . $ip . "'";
    $result = mysql_query($sql);
    $total22 = mysql_num_rows($result);
    if ($total22 >= $maxnum2){
        echo ('同一个IP不能注册过多帐号' . $qqq . '');
        exit();
    }
    $sql = "select  *  from ".$database.".ip where INET_ATON('$ip') between INET_ATON(startIp) and INET_ATON(endIp)";
    $result = mysql_query($sql);
    $row8 = mysql_fetch_array($result);
    $sql = "select  *  from ".$database.".jzip where Country='" . $row8['Country'] . "'";
    $result = mysql_query($sql);
    $totalaa = mysql_num_rows($result);
    if ($totalaa > 0){
        echo '你所在地区禁止注册，如有问题请联系：' . $qqq . '';
        exit();
    }
    $srcDataStr = date("Y-m-d H:i:s");
    $srcDataStr1 = date("YmdHis");
    $sql = "insert into ".$database.".`account` (name,password,tgnum,last_login_ip,last_login_time) values ('" . $account . "','" . $password . "','0','" . $ip . "','" . $srcDataStr . "')";
    $result = mysql_query($sql);
    if (!$result){
        echo ('注册失败！请稍候再次尝试一下！');
        exit();
    }else{
        if($Sid != ''){
            $sql = "select * from ".$database.".spread where ip='$ip' and tid='" . passport_decrypt($Sid, "nimabi") . "'";
            $result = mysql_query($sql);
            $totalcc = mysql_num_rows($result);
            if(!$totalcc > 0){
                $sql = "insert into ".$database.".spread values('" . passport_decrypt($Sid, "nimabi") . "','" . $account . "','" . $ip . "','0')";
                $result = mysql_query($sql);
                if($result){
                    $sql = "update ".$database.".account set tgnum=tgnum+1 where name='" . passport_decrypt($Sid, "nimabi") . "'";
                    $result = mysql_query($sql);
                }
            }
        }
    }
    return true;
}
function change_password($account, $old_password, $new_password){
    login($account, $old_password);
    $sql = "update ".$database.".account set password = '" . $new_password . "' where name = '" . $account . "'";
    $result = mysql_query($sql);
    if (!$result){
        echo('设置新密码失败，请重试');
        exit();
    }else{
        echo('设置新密码成功，请使用新密码登陆');
        exit();
    }
}
if($Action == 'SpreadTran'){
include_once "../configuration/config.inc.php";
if(!$_SESSION['accountName']) exit("<script> alert('请登陆后使用！');location.href='/UserCenter/Login.php';</script>");
$username=$_SESSION['accountName'];

	$sql="SELECT b.lid FROM $database.account a,$database.spread b,$database2.`players` c WHERE b.tid='".$_SESSION['accountName']."' and b.lid=a.`name` AND a.`name`=c.`account_name` and c.`level`>=".$lv." and b.islq=0";
	
    $instr = "";
    $result = mysql_query($sql,$conn);
    while($c = mysql_fetch_array($result)){
        $instr .= "'" . $c[0] . "'" . ",";
    }
    $instr .= "'eruweiouriowejhfkfgghftftytyrtu76786ghkkjsdf2384932454jdfgdf'";
    $tgnum = mysql_num_rows($result);
    $yuanbao = $tgnum * $tgyuanbao;
    if($tgnum == 0){
        //echo '推广玩家等级未达到要求，可兑换的推广数为0' ;

		echo "<script>alert('推广玩家等级未达到要求，可兑换的推广数为0');location.href=/UserCenter/;</script>"; 
        exit();
    }else{
        $sql = "update ".$database.".spread set islq=1 where lid in ($instr)";
        $result1 = mysql_query($sql,$conn);
        $sql = "update ".$database.".account set dj=dj+" . $yuanbao . " where name = '" . $_SESSION['accountName'] . "'";
        $result1 = mysql_query($sql,$conn);
        if (!$result1){
            //echo('兑换元宝失败');
			echo "<script>alert('兑换元宝失败');location.href=/UserCenter/;</script>"; 
            exit();
        }else{
        
						echo "<script>location.href = '/coon/de.php';</script>"; 
						//echo "<script> alert('兑换元宝成功！');location.href=/UserCenter/;</script>";
						//echo "<script type='text/javascript'>location.href='/coon/de.php';</script>";
            //echo "<font color=red><strong>兑换成功,登录游戏查收！</font></strong>";
            exit();
        }
    }
}

?>