<?php
//--------------- 註冊函數 ---------------

//驗證會員組是否可註冊
function CheckMemberGroupCanReg($groupid){
	global $empire,$dbtbpre;
	$groupid=(int)$groupid;
	$r=$empire->fetch1("select groupid from {$dbtbpre}enewsmembergroup where groupid='$groupid' and canreg=1");
	if(empty($r['groupid']))
	{
		printerror('ErrorUrl','',1);
	}
}

//驗證註冊時間
function eCheckIpRegTime($ip,$time){
	global $empire,$dbtbpre;
	if(empty($time))
	{
		return '';
	}
	$ip=RepPostVar($ip);
	$uaddr=$empire->fetch1("select userid from {$dbtbpre}enewsmemberadd where regip='$ip' order by userid desc".do_dblimit_one());
	if(empty($uaddr['userid']))
	{
		return '';
	}
	$ur=$empire->fetch1("select ".eReturnSelectMemberF('userid,registertime')." from ".eReturnMemberTable()." where ".egetmf('userid')."='".$uaddr['userid']."'".do_dblimit_one());
	if(empty($ur['userid']))
	{
		return '';
	}
	$registertime=eReturnMemberIntRegtime($ur['registertime']);
	if(time()-$registertime<=$time*3600)
	{
		printerror('RegisterReIpError','',1);
	}
}

//用戶註冊
function register($add){
	global $empire,$dbtbpre,$public_r,$ecms_config,$level_r;
	//關閉註冊
	if($public_r['register_ok'])
	{
		printerror('CloseRegister','',1);
	}
	//驗證時間段允許操作
	eCheckTimeCloseDo('reg');
	//驗證IP
	eCheckAccessDoIp('register');
	if(!empty($ecms_config['member']['registerurl']))
	{
		Header("Location:".$ecms_config['member']['registerurl']);
		exit();
    }
	//已經登陸不能註冊
	if(getcvar('mluserid'))
	{
		printerror('LoginToRegister','',1);
	}
	CheckCanPostUrl();//驗證來源
	$username=trim($add['username']);
	$password=trim($add['password']);
	$username=RepPostVar($username);
	$password=RepPostVar($password);
	$email=addslashes(RepPostStr($add['email']));
	$email=RepPostVar($email);
	$phno=RepPostVar($add['phno']);
	if(!$username||!$password)
	{
		printerror("EmptyMember","history.go(-1)",1);
	}
	if($public_r['regmustef']!=3)
	{
		if($public_r['regmustef']==1)//郵箱必填
		{
			if(!$email)
			{
				printerror('EmptyMemberEmail','history.go(-1)',1);
			}
		}
		elseif($public_r['regmustef']==2)//手機必填
		{
			if(!$phno)
			{
				printerror('EmptyMemberPhno','history.go(-1)',1);
			}
		}
		else//郵箱和手機必填
		{
			if(!$email||!$phno)
			{
				printerror('EmptyMemberMF','history.go(-1)',1);
			}
		}
	}
	$tobind=(int)$add['tobind'];
	//驗證碼
	$keyvname='checkregkey';
	if($public_r['regkey_ok'])
	{
		ecmsCheckShowKey($keyvname,$add['key'],1);
	}
	$user_groupid=eReturnMemberDefGroupid();
	$groupid=(int)$add['groupid'];
	$groupid=empty($groupid)?$user_groupid:$groupid;
	$groupid=(int)$groupid;
	CheckMemberGroupCanReg($groupid);
	//認證碼
	if($level_r[$groupid]['regps'])
	{
		if('dg'.$level_r[$groupid]['regps']!='dg'.$add['mg_regps'])
		{
			printerror('FailMgSetPs','',1);
		}
	}
	//IP
	$regip=egetip();
	$eipf=egetipfrom();
	$regipport=egetipport();
	//用戶字數
	$pr=$empire->fetch1("select min_userlen,max_userlen,min_passlen,max_passlen,regretime,regclosewords,regemailonly,regphonly from {$dbtbpre}enewspublic".do_dblimit_one());
	$userlen=strlen($username);
	if($userlen<$pr['min_userlen']||$userlen>$pr['max_userlen'])
	{
		printerror('FaiUserlen','',1);
	}
	//密碼字數
	$passlen=strlen($password);
	if($passlen<$pr['min_passlen']||$passlen>$pr['max_passlen'])
	{
		printerror('FailPasslen','',1);
	}
	if($add['repassword']!==$password)
	{
		printerror('NotRepassword','',1);
	}
	if($email&&!chemail($email))
	{
		printerror('EmailFail','',1);
	}
	if($phno&&!echphno($phno))
	{
		printerror('PhnoFail','',1);
	}
	if(strstr($username,'|')||strstr($username,'*'))
	{
		printerror('NotSpeWord','',1);
	}
	//同一IP註冊
	eCheckIpRegTime($regip,$pr['regretime']);
	//保留用戶
	toCheckCloseWord($username,$pr['regclosewords'],'RegHaveCloseword');
	$username=RepPostStr($username);
	//重複用戶
	$num=$empire->gettotal("select count(*) as total from ".eReturnMemberTable()." where ".egetmf('username')."='$username'".do_dblimit_cone());
	if($num)
	{
		printerror('ReUsername','',1);
	}
	//重複郵箱
	if($pr['regemailonly'])
	{
		$num=$empire->gettotal("select count(*) as total from ".eReturnMemberTable()." where ".egetmf('email')."='$email'".do_dblimit_cone());
		if($num>=$pr['regemailonly'])
		{
			printerror('ReEmailFail','',1);
		}
	}
	//重複手機
	if($pr['regphonly'])
	{
		$num=$empire->gettotal("select count(*) as total from ".eReturnMemberTable()." where ".egetmf('phno')."='$phno'".do_dblimit_cone());
		if($num>=$pr['regphonly'])
		{
			printerror('RePhnoFail','',1);
		}
	}
	//註冊時間
	$lasttime=time();
	$registertime=eReturnAddMemberRegtime();
	$rnd=make_password(EcmsRandInt(20,36));//產生隨機密碼
	$rndt=make_password(EcmsRandInt(20,32));//取得隨機密碼2
	$userkey=eReturnMemberUserKey();
	//密碼
	$truepassword=$password;
	$salt=eReturnMemberSalt();
	$password=eDoMemberPw($password,$salt);
	//審核
	$checked=ReturnGroupChecked($groupid);
	if($checked&&$public_r['regacttype'])
	{
		$checked=0;
	}
	$checked=(int)$checked;
	//驗證附加表必填項
	$mr['add_filepass']=ReturnTranFilepass();
	$fid=GetMemberFormId($groupid);
	$member_r=ReturnDoMemberF($fid,$add,$mr,0,$username);

	$sql=$empire->updatesql("insert into ".eReturnMemberTable()."(".eReturnInsertMemberF('username,password,rnd,email,registertime,groupid,userfen,userdate,money,zgroupid,havemsg,checked,salt,userkey,isot,phno,upic,mustdo,rndt').") values('$username','$password','$rnd','$email','$registertime','$groupid','".$public_r['reggetfen']."','0','0','0','0','$checked','$salt','$userkey','1','$phno',0,0,'$rndt');","ins");
	//取得userid
	$userid=$empire->lastid(eReturnMemberTable(),egetmf('userid'));
	//附加表
	$addr=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$userid'");
	if(!$addr['userid'])
	{
		$spacestyleid=ReturnGroupSpaceStyleid($groupid);
		$sql1=$empire->updatesql("insert into {$dbtbpre}enewsmemberadd(userid,spacestyleid,regip,lasttime,lastip,loginnum,regipport,lastipport,eipf".$member_r[0].") values('$userid','$spacestyleid','$regip','$lasttime','$regip','1','$regipport','$regipport','$eipf'".$member_r[1].");","ins");
	}
	//更新附件
	UpdateTheFileOther(6,$userid,$mr['add_filepass'],'member');
	ecmsEmptyShowKey($keyvname);//清空驗證碼
	//綁定帳號
	if($tobind)
	{
		MemberConnect_BindUser($userid);
	}
	if($sql)
	{
		//郵箱激活
		if($checked==0&&$public_r['regacttype']==1)
		{
			include('class/member_actfun.php');
			SendActUserEmail($userid,$username,$email);
		}
		//審核
		if($checked==0)
		{
			$location=DoingReturnUrl("../../",$_POST['ecmsfrom']);
			printerror("RegisterSuccessCheck",$location,1);
		}
		$logincookie=0;
		if($ecms_config['member']['regcookietime'])
		{
			$logincookie=time()+$ecms_config['member']['regcookietime'];
		}
		$r=$empire->fetch1("select ".eReturnSelectMemberF('*')." from ".eReturnMemberTable()." where ".egetmf('userid')."='$userid'".do_dblimit_one());
		$set1=esetcookie("mlusername",$username,$logincookie);
		$set2=esetcookie("mluserid",$userid,$logincookie);
		$set3=esetcookie("mlgroupid",$groupid,$logincookie);
		$set4=esetcookie("mlrnd",$rnd,$logincookie);
		$set6=esetcookie("mlrndt",$rndt,$logincookie);
		//驗證符
		qGetLoginAuthstr($userid,$username,$rnd,$groupid,$logincookie);
		//登錄附加cookie
		AddLoginCookie($r);
		$location="../member/cp/";
		$returnurl=getcvar('returnurl');
		if($returnurl&&!strstr($returnurl,"e/member/iframe")&&!strstr($returnurl,"e/member/register")&&!strstr($returnurl,"enews=exit"))
		{
			$location=CheckCanUseDm($returnurl);
		}
		$set5=esetcookie("returnurl","");
		//易通行系統
		DoEpassport('reg',$userid,$username,$truepassword,$salt,$email,$groupid,$registertime);
		$location=DoingReturnUrl($location,$_POST['ecmsfrom']);
		printerror("RegisterSuccess",$location,1);
	}
	else
	{printerror("DbError","history.go(-1)",1);}
}
?>