<?php
// +----------------------------------------------------------------------
// | 用户注册 [ 天方夜谭快速开发框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

//用户注册
if (POST('action')=='register'){
    session_start();
    header('Content-Type:application/json; charset=utf-8');
    $name = POST('name');
    $username = POST('username');
    $email = POST('email');
    $password = POST('password');
    $password2 = POST('password2');
    $qq = POST('qq');
    $code = POST('code');
    if (empty($name) || empty($username) || empty($email) || empty($password) || empty($password2) || empty($qq) || empty($code)){
        $arr = array('state'=>'no','msg'=>'所有项都必须填写！');
    }elseif (strlen($name)>18){
        $arr = array('state'=>'no','msg'=>'昵称太长！');
    }elseif (strlen($username)<5 || strlen($username)>18){
        $arr = array('state'=>'no','msg'=>'用户名不能小于5位并且不能大于18位！');
    }elseif (!preg_match("/^[a-z0-9][a-z0-9\.\_\-]+@[a-z0-9]+\.[a-z]{2,4}$/i",$email)){
        $arr = array('state'=>'no','msg'=>'邮箱地址不正确！');
    }elseif ((strlen($password)<5 || strlen($password)>18) ||strlen($password2)<5 || strlen($password2)>18){
        $arr = array('state'=>'no','msg'=>'密码不能小于5位并且不能大于18位！');
    }elseif ($password!=$password2 || $password2!=$password){
        $arr = array('state'=>'no','msg'=>'两次输入的密码不一致！');
    }elseif (strlen($qq)<5 || strlen($qq)>11){
        $arr = array('state'=>'no','msg'=>'QQ号码不正确！');
    }elseif ($code!=$_SESSION["tfyt_code"]){
        $arr = array('state'=>'no','msg'=>'请输入正确的验证码！');
    }elseif ($TFYT_DB->get_row("select username from ".PREFIX."users where username='{$username}' limit 1")){
        $arr = array('state'=>'no','msg'=>'该用户名已被注册，换一个吧！');
    }elseif ($TFYT_DB->get_row("select qq from ".PREFIX."users where qq='{$qq}' limit 1")){
        $arr = array('state'=>'no','msg'=>'该QQ号码已被注册，换一个吧！');
    }elseif ($TFYT_DB->get_row("select email from ".PREFIX."users where email='{$email}' limit 1")){
        $arr = array('state'=>'no','msg'=>'该邮箱号码已被注册，换一个吧！');
    }else{
        $only = str_rand();
        $pwd = md5Pwd($password);   //MD5
        $regip = getIp();   //IP
        $regcity = get_region(getIp()); //城市
        $regtime = date('Y-m-d h:i:s'); //时间
        $q_quota = TFYT('reg_q_quota'); //挂机配额
        $p_quota = TFYT('reg_p_quota'); //播放器配额
        $s_quota = TFYT('reg_s_quota'); //歌曲配额
        $u_quota = TFYT('reg_u_quota'); //歌曲上传配额
        $money = TFYT('reg_u_quota'); //彩虹屁
        if (
            $TFYT_DB->query("insert into ".PREFIX."users (`name`,`username`,`password`,`qq`,`email`,`regip`,`regcity`,`regtime`,`only`)values('{$name}','{$username}','{$pwd}','{$qq}','{$email}','{$regip}','{$regcity}','{$regtime}','{$only}')")
            &&
            $TFYT_DB->query("insert into ".PREFIX."users_service (`only`,`q_quota`,`p_quota`,`s_quota`,`u_quota`,`money`)values('{$only}','{$q_quota}','{$p_quota}','{$s_quota}','{$u_quota}','{$money}')")
        ){
            $arr = array('state'=>'ok','msg'=>'注册成功！您的用户名为:'.$username.'');
        }else{
            $arr = array('state'=>'no','msg'=>'系统错误，注册失败,请联系管理员修复！');
        }
    }
    exit(json_encode($arr));
}

//加载模板
Load_Template(Users.'register.php');