<?php
include("../conn.php");
include("../common.php");

if (ubo($_POST[btn])) {
    $name = ubo($_POST[name]);
    $pass = ubo($_POST[pass]);
    $code = ubo($_POST[code]);
    function checkPhoneNumber($phone_number)
    {

        $g = "/^1[34578]\d{9}$/";
        $g2 = "/^19[89]\d{8}$/";
        $g3 = "/^166\d{8}$/";
        if (preg_match($g, $phone_number)) {
            return true;
        } else if (preg_match($g2, $phone_number)) {
            return true;
        } else if (preg_match($g3, $phone_number)) {
            return true;
        }

        return false;
    }

    if ($name == null) {
        $err = '<p class="error" style="color: red !important;">手机号不能为空</p>';
    } else if (!checkPhoneNumber($name)) {
        $err = '<p class="error" style="color: red !important;">手机号格式校验失败</p>';
    } else {
        $sql = mysql_query("SELECT count(1) FROM user where uid='" . $name . "'");
        $bs = mysql_fetch_array($sql)[0];
        if ($bs > 0) {
            $err = '<p class="error" style="color: red !important;">该手机号已存在</p>';
        } else if ($pass == null) {
            $err = '<p class="error" style="color: red !important;">密码不能为空</p>';
        }else if($code==null){
            $err = '<p class="error" style="color: red !important;">邀请码不能为空</p>';
        }else{
            $sql = mysql_query("SELECT count(1) FROM zhucema where ma='" . $code . "'");
            $count = mysql_fetch_array($sql)[0];
            if($count<=0){
                $err = '<p class="error" style="color: red !important;">邀请码不存在</p>';
            }else {
                $time=date("Y-m-d H:i:s",time());
				$max = mysqli_fetch_row(mysqli_query($conn, "select max(uid) from user"))[0];
				$max =(int)max;
				$max++;
                mysql_query("INSERT INTO `ysdaili`.`user`(`uid`, `recommend`, `bindtime`, `welfare`,'account') VALUES ('".$max."', '".$code."', '".date("Y-m-d H:i:s",time())."', 0,'".$phone_number."');");
                echo "<script>alert('注册成功！\n您的账号是：".$phone_number."  \n 密码是：".$pass."\n请妥善保存！')</script>";
            }
        }
    }

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>
    <title>魔域代理系统</title>
    <meta name="keywords" content="独家开发"/>
    <link rel="stylesheet" type="text/css" href="../daili/newui/login/login.css" media="all"/>
    <link rel="shortcut icon" href="../daili/favicon.ico"/>
    <style>
        .bgimg{
            background: url(newui/login/xz.jpg) no-repeat;
        }
        .error{
            font-size: 20px;
        }
    </style>
</head>
<body ] class="bgimg">
<div class="loginbox" style="background-color: transparent">
    <form action="" method="post">
        <div class="huan"><p class="go">游戏账号注册</p></div>
        <input type="text" name="name" autocomplete="off" class="username text" value="<?php echo $name ?>"
               placeholder="手机号 将做为游戏登录账号" autofocus/>
			   <p style="margin-top:-20px;padding-bottom:10px ;color:red">请牢记手机号，可用于找回密码，忘记概不负责</p>
        <input type="text" name="pass" autocomplete="off" class="text password" value="<?php echo $pass ?>"
               placeholder="密码"/>
			           <br>
        <input type="text" name="code" autocomplete="off" class="text password" value="<?php echo $code ?>"
               placeholder="邀请码 请向群管理索要"/>
        <?php echo $err ?>
        <input type="submit" value="注册" name="btn" class="btn"/>
</div>
</body>
</html>