<?php
	header("Content-type:text/html;charset=utf-8");
	include("../include/conn_1.php");
	include("../include/function.php");
	include("../include/public.php");

	session_start();
	
	//忘记密码
	if ($action=="forgot"){

		$username =  trim($username);
		$mobile   =  trim($mobile);
		$code     =  trim($code);

		if ($code!=$_SESSION["code_2"]) {
			die(json_encode(array('info'=>"验证码错误",'status'=>"n")));
		}

		$sql_lgn = "select * from {$db_prefix}member where username='".$username."' and mobile='".$mobile."'";
		$rs_lgn=$db->get_one($sql_lgn);
		if(!$rs_lgn){
			die(json_encode(array('info'=>"手机号码与登录名不匹配",'status'=>"n")));
		}

		//匹配，修改密码，发送，记录日志
		$passW = getRandChar();	//获取6位密码
		$pwd   = mymd5($passW);
		$sql_pwd = "update  {$db_prefix}member set pwd = '{$pwd}'   where  mobile='".$mobile."' and username='".$username."'";
		$rs_pwd=$db->query($sql_pwd);
		if($rs_pwd){
			//插入记录
			$sql_in_log = "insert into {$db_prefix}member_forgot_logs(`username`,`mobile`,`ip`,`posttime`,`type`) value('{$username}','{$mobile}','".getip()."','".time()."','pwd')";
			$db->query($sql_in_log);
			//发送记录
			$CONTENT = mobileTemp('forgot',$passW);
			$isSend  = sendMobile($mobile,$CONTENT,'forgot');
			if($isSend['status'] == "y"){
				die(json_encode(array('info'=>"重置成功",'status'=>"y")));
			}else{
				die(json_encode(array('info'=>$isSend['info'],'status'=>"n")));
			}
		}else{
			die(json_encode(array('info'=>"密码重置错误，请重新修改",'status'=>"n")));
		}
	}

	//注册账号验证
	if ($action=="username"){

		$username =  trim($username);
		if(strlen($username) < 2 && strlen($username) > 32){
			die(json_encode(array('info'=>"您输入的账号位数有误",'status'=>"n")));
		}

		//if(!inject_check($username)){
		//	die(json_encode(array('info'=>"您输入的账号格式有误",'status'=>"n")));
		//}

		//判断是否存在
		$sql_lgn = "select * from {$db_prefix}member where username='".$username."'";
		$rs_lgn=$db->get_one($sql_lgn);
		if($rs_lgn){
			die(json_encode(array('info'=>"您输入的账号已经存在",'status'=>"n")));
		}else{
			die(json_encode(array('info'=>"通过",'status'=>"y")));
		}
	}

	//注册手机验证
	if ($action=="mobile"){

		$mobile =  trim($param);
		if(strlen($mobile) !== 11){
			die(json_encode(array('info'=>"您输入的手机位数不对",'status'=>"n")));
		}


		// $search ='/^13[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/';
		// if(!preg_match($search,$mobile)) {
		//    die(json_encode(array('info'=>"您输入的手机格式有误",'status'=>"n")));
		// }
		//判断是否存在
		$sql_lgn = "select * from {$db_prefix}member where mobile='".$mobile."'";
		$rs_lgn=$db->get_one($sql_lgn);
		if($rs_lgn){
			die(json_encode(array('info'=>"您输入的手机号码已经存在",'status'=>"n")));
		}else{
			die(json_encode(array('info'=>"通过",'status'=>"y")));
		}
	}

	//验证码
	if ($action=="Code"){

		if ($code!=$_SESSION["code_2"]) {
			die(json_encode(array('info'=>"验证码输入有误，请重新输入",'status'=>"n")));
		}
		//发送短信
		$PHONE   = trim($tel);
		$sql_lgn = "select * from {$db_prefix}member where mobile='".$PHONE."'";
		$rs_lgn=$db->get_one($sql_lgn);
		if($rs_lgn){
			die(json_encode(array('info'=>"手机号已被注册，请更换手机号",'status'=>"n")));
		}
		//判断每天只能发10次
		$sql_send = "select count(*) as count from {$db_prefix}member_send_mobile where to_days(`posttime`) = to_days(now()) and mobile='".$PHONE."'";
		$rs_send = $db->get_one($sql_send);
		if((int)$rs_send['count'] >= 10){
			die(json_encode(array('info'=>"今天发送数量已经上限",'status'=>"n")));
		}

		$number  = randStr();
		$_SESSION['code_mobile'] = $number;
		$CONTENT = mobileTemp('regcode',$number);
		$isSend  = sendMobile($PHONE,$CONTENT,'code_reg');
		if($isSend['status'] == 'y'){
			die(json_encode(array('data'=>'','info'=>"短信验证码已发送至您的手机",'status'=>"y")));
		}else{
			die(json_encode(array('data'=>'','info'=>$isSend['info'],'status'=>"n")));
		}
	}

	//短信验证码
	if ($action=="mobileCode"){

		if ($mcode!=$_SESSION["code_mobile"]) {
			die(json_encode(array('info'=>"你输入的短信验证码错误",'status'=>"n")));
		}
		die(json_encode(array('info'=>"通过",'status'=>"y")));
	}


	//注册
	if($action="reg"){

		$tel   = $phone;
		$mima  = $password;
		$mcode = $code;

		if(!$tel  || !$mima){
			die(json_encode(array('info'=>"网络异常，请重新提交",'status'=>"n")));
		}

		$username =  trim($username);

		if ($mcode!=$_SESSION["code_mobile"]) {
			die(json_encode(array('info'=>"你输入的短信验证码错误",'status'=>"n")));
		}
		
		if(strlen($username) < 2 && strlen($username) > 32){
			die(json_encode(array('info'=>"您输入的账号位数有误",'status'=>"n")));
		}

		//if(!inject_check($username)){
		//	die(json_encode(array('info'=>"您输入的账号格式有误",'status'=>"n")));
		//}


		//判断是否存在
		$sql_lgn = "select * from {$db_prefix}member where username='".$username."'";
		$rs_lgn=$db->get_one($sql_lgn);
		if($rs_lgn){
			die(json_encode(array('info'=>"您输入的账号已经存在",'status'=>"n")));
		}

		$mobile =  trim($tel);
		if(strlen($mobile) !== 11){
			die(json_encode(array('info'=>"您输入的手机位数不对",'status'=>"n")));
		}

		//$search ='/^(1(([35][0-9])|(47)|[8][0126789]))\d{8}$/';
		//if(!preg_match($search,$mobile)) {
		//   die(json_encode(array('info'=>"您输入的手机格式有误",'status'=>"n")));
		//}

		if($yaoqingren == $username){
			die(json_encode(array('info'=>"邀请人不能为自己账号",'status'=>"n")));
		}

		//判断邀请人是否存在
		$sql_lgn2 = "select * from {$db_prefix}member where username='".$yaoqingren."'";
		$rs_lgn=$db->get_one($sql_lgn);
		if($rs_lgn){
			die(json_encode(array('info'=>"你输入的邀请人不存在",'status'=>"n")));
		}


		//判断是否存在
		$sql_lgn2 = "select * from {$db_prefix}member where mobile='".$mobile."'";
		$rs_lgn=$db->get_one($sql_lgn);
		if($rs_lgn){
			die(json_encode(array('info'=>"您输入的手机号码已经存在",'status'=>"n")));
		}

		
		//注册送积分
		$giveAmount = configW('XiaXianReg');
		if($giveAmount !== 0){
			//0金额不开启
			$amount = $giveAmount;
			$ordernumber = ordernumber(); //获取订单号
			

			//记录充值记录
			$sql_lgn_ins = "insert into {$db_prefix}member_order(`username`,`ordernumber`,`amount`,`memo`,`type`,`status`,`ip`,`posttime`,`paytime`) value('{$username}','{$ordernumber}','{$amount}','新手礼包','优惠活动',1,'".getip()."','".time()."','".time()."')";
			$db->query($sql_lgn_ins);
			//发送短信息
			$compnayN = configW('webname');
			$title = "尊敬的".$username."会员您好！恭喜您注册成为".$compnayN."的会员，获得新手礼包：".$amount."元";
			$content = "获得新手礼包：".$amount;
			$sql_lgn_msg = "insert into {$db_prefix}member_msg(`username`,`title`,`content`,`from_name`,`posttime`,`status`) value('{$username}','{$title}','{$content}','system','".time()."',0)";
			$db->query($sql_lgn_msg);
		}else{
			$amount = 0;
		}

		//插入数据
		$pwd = mymd5($mima);
		$yaoqingren =  trim($yaoqingren);
		$sql_lgn_ins = "insert into {$db_prefix}member(`username`,`pwd`,`paypwd`,`mobile`,`email`,`inviter`,`ip`,`posttime`,`ismobile`,`level`,`amount`,`qq`) value('{$username}','{$pwd}','{$pwd}','{$tel}','{$youxiang}','{$yaoqingren}','".getip()."','".time()."',1,1,'{$amount}','{$qq}')";
		$result = $db->query($sql_lgn_ins);
		if($result){
			die(json_encode(array('info'=>"恭喜您注册成功！返回登录页面中...",'status'=>"y")));
		}else{
			die(json_encode(array('info'=>"注册失败,请重新注册",'status'=>"n")));
		}
	}

?>