<?php
define('THIS_SCRIPT', 'reg');
require_once 'global.php';

//通行证数据接口
$comeurl=$_GET['comeurl'];
empty($comeurl) AND ($comeurl = $fsetting['homepage']);
empty($comeurl) AND ($comeurl='index.php');
if(strtolower(substr($comeurl,0,7))!='http://'){
	$baseurl=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
	(false!==$spos=strrpos($baseurl,"/"))&&($baseurl=substr($baseurl,0,$spos));
	$comeurl='http://'.$baseurl."/".$comeurl;
}
if(($passport['enabledpassportclient']=='1')&&($_GET['is_rpc']!=1)){
(substr($passport['serveraddress'],-1,1)!="/") &&($passport['serveraddress'].="/");
($passport['serverregister']{0}=="/") &&($passport['serverregister']=substr($passport['serverregister'],1));
$jumpurl=$passport['serveraddress'].$passport['serverregister'];
$jumpurl.=(false!==strpos($jumpurl,'?')?'&':'?')."comeurl=".rawurldecode($comeurl);
header("location:{$jumpurl}");
exit;
}
//end 通行证数据接口

$lang = load_lang($lang, 'reg');
$stats = $lang['strings1'];
if ( $fsetting['applynewuser'] == 0 ){
	$arrNavMenu = array(
	0=>array('linktitle'=>$stats)
	);
	if ($login_che!=1){
	head(1,0,0,$arrNavMenu);
	showmsg($lang['tpl.str87']);
	footer();
	}
}


 if($action == 'usernamecheck') {
	$username = str_replace(' ', '', $username_c);
	if(!$username) {
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str1']."\", \"username\")</script>";
		exit;
	}

	$username = strip_tags($username);
	list($minlength, $maxlength) = explode(',', $fsetting['usernamelength']);
	if(strlen($username) < $minlength || strlen($username) > $maxlength) {
		$lang['lang.str2'] = str_replace(array('$minlength', '$maxlength'), array($minlength, $maxlength), $lang['lang.str2']);
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str2']."\", \"username\")</script>";
		exit;
	}
	$query = $db->query("select forum_setting from {$dv}setup");
	$rs = $db->fetch_array($query);
	$_reglist = explode("|||",$rs['forum_setting']);
	$reglimit = $_reglist[4];
	$reglimit = explode(',', $reglimit);
	foreach($reglimit as $val) {
		if(@strpos($username, $val) !== false) {
			$lang['lang.str3'] = str_replace('$val', $val, $lang['lang.str3']);
			echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str3']."\", \"username\")</script>";
			exit;
		}
	}
	if (1 == preg_match('/=|%|'.chr(32).'|\?|&|;|\||,|\'|'.chr(34).'|'.chr(9).'|'.preg_quote($lang['lang.str22']).'|\$/i',trim($username))) {
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str19']."\", \"username\")</script>";
		exit;
	}
	$charscope = '\x'.strtoupper(dechex(10)).'-\x'.dechex(ord('A')-1).'\x'.dechex(ord('Z')+1).'-\x'.dechex(ord('a')-1).'-\x'.dechex(ord('z')+1).'\x'.dechex(ord('_')).'';
	if (preg_match('/^['.$charscope.']+$/i', $username)&&!preg_match('/^\d+$/i', $username)) {
		echo "<script language=\"javascript\">parent.msgshow('".$lang['lang.str19']."', \"username\")</script>";
		exit;
	}

	require_once CACHE_PATH.'Forum_IPgame_cache.php';
	$IP_CACHE_arr=$IP_CACHE['allowip'];
	if (intval($IP_CACHE_arr['ipnumber_estop'])===1 and is_numeric($username)){
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str26']."\", \"username\")</script>";
		exit;	
	}

	$query = $db->query("SELECT COUNT(*) AS count FROM {$dv}user WHERE username='$username'", 'UNBUFFERED');
	$rs =& $db->fetch_array($query);
	if(!Empty($rs['count'])) {
		$lang['lang.str4'] = str_replace('$username', $username, $lang['lang.str4']);
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str4']."\", \"username\")</script>";
		exit;
	} else {
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str5']."@@@\", \"username\")</script>";
		exit;
	}
} elseif($action == 'emailcheck') {
	$email = str_replace(' ', '', $email);
	if(!$email) {
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str6']."\", \"email\")</script>";
		exit;
	}
	if(!ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$", $email)) {
		echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str7']."\", \"email\")</script>";
		exit;
	}
	if($fsetting['maillimited']) {
		$query = $db->query("SELECT COUNT(*) AS count FROM {$dv}user WHERE useremail='$email'", 'UNBUFFERED');
		$rs =& $db->fetch_array($query);
		if($rs['count'] > 0) {
			echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str8']."\", \"email\")</script>";
			exit;
		}
	}
	echo "<script language=\"javascript\">parent.msgshow(\"".$lang['lang.str9']."@@@\", \"email\")</script>";
	exit;
}elseif($action == 'save'){
	$iapply=$GLOBALS['iapply'];
	$arrNavMenu = array(
	0=>array('linktitle'=>$lang['strings1'],'linkurl'=>'reg.php')
	,1=>array('linktitle'=>$lang['strings4'])
	);
	head(1,0,0,$arrNavMenu);
	if($iapply != 'ok'){
		showmsg($lang['lang.str23']);//接受服务条款
	}
	else{
		reg_step2();
	}
	footer();
}else{
	$arrNavMenu = array(
	0=>array('linktitle'=>$lang['strings1'],'linkurl'=>'reg.php')
	,1=>array('linktitle'=>$lang['strings2'])
	);
	head(1,0,0,$arrNavMenu);
	reg_step1();
	footer();
}

function reg_step1(){
	global $fsetting,$lang,$action,$_DCACHE,$dv,$db,$userid,$comeurl;
	if($userid>0){
		showmsg($lang['lang.str21']);
		exit;
	}
	$lang = load_lang($lang, 'usermanager');
	$namelength = explode(',',$fsetting['usernamelength']);
	include_once CACHE_PATH.'forum_face.php';
	$mquery=$db->query("select groupname from {$dv}groupname");
	$user_shengxiao = chk_select('',$lang['template.strings11']);
	$user_blood = chk_select('','A,B,AB,O');
	$user_belief = chk_select('',$lang['template.strings16']);
	$user_occupation = chk_select('',$lang['template.strings12']);
	$user_marital = chk_select('',$lang['template.strings13']);
	$user_education = chk_select('',$lang['template.strings14']);
	$user_character = Chk_KidneyType("character",'',$lang['template.strings15']);
	include_once printout('reg');
}

function reg_step2(){
	global $dv,$db,$action,$lang,$fsetting,$userinfo,$getcode,$msetting,$character,$userid;
	$regtime = get_cookie("regtime");
	$showmsg = '';
	if ($regtime !='' && $fsetting['newusertime'] > 0){
		if (datediff("S",$regtime,gmdate("Y-m-d H:i:s")) < $fsetting['newusertime']){
			$showmsg .= str_replace('{$regtime}',$fsetting['newusertime'],$lang['strings5']). '|||';
		}
	}
	$chkpost = ChkPost();
	if ($chkpost == false){
		$showmsg .= $lang['strings6']. '|||';
	}
	$username = str_replace(' ', '', $GLOBALS['username']);
	$username = preg_replace('/\<(.+?)\>/is', '', $username);
	$namelength = strlen($username);
	list($usernameminlength,$usernamemaxlength) = explode(',',$fsetting['usernamelength']);
	if ($username == '' || $namelength > $usernamemaxlength || $namelength < $usernameminlength){
		$lang['strings7'] = str_replace('{$RegMaxLength}',$usernamemaxlength,$lang['strings7']);
		$lang['strings7'] = str_replace('{$RegMinLength}',$usernameminlength,$lang['strings7']);
		$showmsg .= $lang['strings7']. '|||';
	}
	if (IsTrueName($username) === false){
		$showmsg .= $lang['strings8']. '|||';
	}
	//时区验证部分
	//数字用户名验证部分
	//验证码验证部分
	if ( $fsetting['usergetcode'] == 1 && ($userinfo['getcode'] == '' || $userinfo['getcode'] != $getcode) ){
		$showmsg .= $lang['strings31']. '|||';
	}

	if ($fsetting['regsplitwords'] != ''){
		$fsetting['regsplitwords'] = explode(',',$fsetting['regsplitwords']);
		for ($i=0; $i<count($fsetting['regsplitwords']); $i++){
			if ($fsetting['regsplitwords'][$i] != ''){
				if (strpos($username,"{$fsetting['regsplitwords'][$i]}") === true){
					$showmsg .= $lang['strings8']. '|||';
					break;
				}
			}
		}
	}
	$sex=$GLOBALS['sex'] == 0?0:1;
	$showre = $GLOBALS['showre'] == 0 ?0:1;

	if ($fsetting['mailpassword'] == 1){
		$spassword = Createpass(8);
		$password = dv_md5($spassword);
	}else{
		$pass1 = $GLOBALS['password'];
		$pass2 = $GLOBALS['repassword'];
		if ($pass1 == '' || strlen($pass1) > 20 || strlen($pass1) < 6){
			$showmsg .= $lang['strings9']. '|||';
		}
		if ($pass2 == '' || strlen($pass2) > 20 || strlen($pass2) < 6){
			$showmsg .= $lang['strings10']. '|||';
		}
		if ($pass1 != $pass2){
			$showmsg .= $lang['strings11']. '|||';
		}else{
			$password = dv_md5($pass1);
		}
	}
	if ($GLOBALS['quesion'] == ''){
		$showmsg .= $lang['strings12']. '|||';
	}
	if ($GLOBALS['answer'] == ''){
		$showmsg .= $lang['strings13']. '|||';
	}else{
		$answer = dv_md5($GLOBALS['answer']);
	}
	if (!ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$",$GLOBALS['e_mail'])){
		$showmsg .= $lang['strings14']. '|||';
	}else{
		if ($fsetting['banmail'] != '' && $fsetting['banmail'] != '0'){
			$splituseremail = explode('|',$fsetting['banmail']);
			for ($i=0; $i<count($splituseremail); $i++){
				if (strpos($GLOBALS['e_mail'],"{$splituseremail[$i]}") === true){
					$showmsg .= $lang['strings15']. '|||';
					break;
				}
			}
		}
	}
	if ($GLOBALS['myface'] != '' && $fsetting['myfacepostlimited'] == 0){
		if ($GLOBALS['width'] == '' || !is_numeric($GLOBALS['width']) || $GLOBALS['height'] == '' || !is_numeric($GLOBALS['height'])){
			$showmsg .= $lang['strings16']. '|||';
		}elseif($GLOBALS['width'] > $fsetting['userfacemaxsize'] || $GLOBALS['height'] > $fsetting['userfacemaxsize']){
			$showmsg .= $lang['strings17']. '|||';
		}else{
			if ($fsetting['httpuserface'] == 0){
				if (strpos(strtolower($GLOBALS['myface']),"http://") === true || strpos(strtolower($GLOBALS['myface']),"www.") === true){
					$showmsg .= $lang['strings18']. '|||';
				}
			}
			$face = $GLOBALS['myface'];
		}
	}elseif($GLOBALS['face'] != ''){
		$face = $GLOBALS['face'];
	}
	$face = htmlspecialchars($face);
	$face = str_replace("|","",$face);
	if (!is_numeric($GLOBALS['oicq'])){
		$GLOBALS['oicq'] = "";
	}
	if(is_array($character)) {
		foreach($character as $val) {
			$_character .= checkreal($val).',';
		}
	} else {
		$_character = checkreal($character);
	}
	$userinfo = checkreal($GLOBALS['realname']). '|||' .$_character. '|||' .checkreal($GLOBALS['personal']). '|||' .checkreal($GLOBALS['country']). '|||' .checkreal($GLOBALS['province']). '|||' .checkreal($GLOBALS['city']). '|||' .checkreal($GLOBALS['shengxiao']). '|||' .checkreal($GLOBALS['blood']). '|||' .checkreal($GLOBALS['belief']). '|||' .checkreal($GLOBALS['occupation']). '|||' .checkreal($GLOBALS['marital']). '|||' .checkreal($GLOBALS['education']). '|||' .checkreal($GLOBALS['college']). '|||' .checkreal($GLOBALS['userphone']). '|||' .checkreal($GLOBALS['address']);
	$usersetting = checkreal($GLOBALS['setuserinfo']). '|||' .checkreal($GLOBALS['setusertrue']). '|||' .$showre;
	$userim = checkreal($GLOBALS['homepage']). '|||' .$GLOBALS['oicq']. '|||' .checkreal($GLOBALS['icq']). '|||' .checkreal($GLOBALS['msn']). '|||' .checkreal($GLOBALS['yahoo']). '|||' .checkreal($GLOBALS['aim']). '|||' .checkreal($GLOBALS['uc']);
	$userim = htmlspecialchars($userim);
	if ($showmsg != ''){
		showmsg($showmsg);
	}
	$truepassword = Createpass(16);
	$query=$db->query("Select usertitle,grouppic,usergroupid,issetting,parentgid From {$dv}usergroups Where parentgid=3 Order By minarticle Limit 1");
	if ($groups =& $db->fetch_array($query)){
		$userclass = $groups['usertitle'];
		$titlepic = $groups['grouppic'];
		$usergroupid = $groups['usergroupid'];
	}else{
		showmsg($lang['strings19']);
	}
	if ($fsetting['isnewuserlimited'] == 1){
		$usergroupid = 5;
	}
	if ($GLOBALS['forum_passport'] != '' && $fsetting['maillimited'] == 1){
		$query=$db->query("(Select userid From {$dv}user Where username='$username') union all (Select userid From {$dv}user Where useremail='$GLOBALS[e_mail]') union all (Select userid From {$dv}user Where passport='$GLOBALS[forum_passport]')");
	}elseif($GLOBALS['forum_passport'] != ''){
		$query=$db->query("(Select userid From {$dv}user Where username='$username') union all (Select userid From {$dv}user Where passport='$GLOBALS[forum_passport]')");
	}elseif($fsetting['maillimited'] == 1){
		$query=$db->query("(Select userid From {$dv}user Where username='$username') union all (Select userid From {$dv}user Where useremail='$GLOBALS[e_mail]')");
	}else{
		$query=$db->query("Select userid From {$dv}user Where username='$username'");
	}
	if ($users =& $db->fetch_array($query, array('pageSize'=>1))){
		if ($GLOBALS['forum_passport'] != '' && $fsetting['maillimited'] == 1){
			showmsg($lang['strings20']);
		}elseif($GLOBALS['forum_passport'] != ''){
			showmsg($lang['strings21']);
		}elseif($fsetting['maillimited'] == 1){
			showmsg($lang['strings22']);
		}else{
			showmsg($lang['strings23']);
		}
	}
	$birthday = trim($GLOBALS['birthday']);
	if( !isDate($birthday) ) {
		$birthday = '';
	} else {
		$birthday = dv_gmtimestamp($birthday) - getUserTimeZoneOffset();
	}
	$userjointime = time();
	$forum_user = explode(',',$fsetting['userpoint']);
	$db->query("Insert Into {$dv}user (username,userpassword,useremail,userclass,titlepic,userquesion,useranswer,truepassword,userim,usersign,userpost,usertopic,usergroupid,lockuser,usersex,userbirthday,usergroup,joindate,userface,userwidth,userheight,usertoday,userlogins,lastlogin,userwealth,userep,usercp,userinfo,usersetting,userpower,userdel,userisbest,usermoney,userticket,userfav,ischallenge,userhidden,userlastip) Values ('$username','$password','$GLOBALS[e_mail]','$userclass','$titlepic','$GLOBALS[quesion]','$answer','$truepassword','$userim','$GLOBALS[signature]',0,0,'$usergroupid',0,'$sex','$birthday','$GLOBALS[usergroup]','$userjointime','$face','$GLOBALS[width]','$GLOBALS[height]','0|0|0|0|0',1,'$userjointime','$forum_user[0]','$forum_user[5]','$forum_user[10]','$userinfo','$usersetting',0,0,0,0,0,'$lang[strings24]',0,2,'$GLOBALS[usertrueip]')");
	$query=$db->query("Select max(userid) From {$dv}user");
	$users =& $db->fetch_array($query);
	$userid = $users['max(userid)'];

	//对上传头象进行过滤与改名
	//Email通知密码
	if ($fsetting['mailpassword'] == 1){
		$fsetting['sendnewusermail'] = 1;
	}
	include_once CACHE_PATH.'mailconfig.php';
	if ( $fsetting['sendnewusermail'] == 1 && $msetting['mailmod'] != 0 ){
		$title = str_replace('{$forumname}',$fsetting['forumname'],$lang['tpl.str85']);
		if ( $fsetting['mailpassword'] == 1 ){
			$getpass = $spassword;
		}else{
			$getpass = $pass1;
		}
		$mailbody = $lang['tpl.str86'];
		$mailbody = str_replace('{$username}',$username,$mailbody);
		$mailbody = str_replace('{$password}',$getpass,$mailbody);
		$mailbody = str_replace('{$copyright}',$fsetting['copyright'],$mailbody);
		$mailbody = str_replace('{$version}','<a href="http://www.cndw.com" target=_blank>Dvbbs.Php '.$fsetting['forumversion'].'</a>',$mailbody);
		include_once INC_PATH.'sendmail.php';
		$issend = sendmail($GLOBALS['e_mail'],$title,$mailbody,"{$fsetting['forumname']} <{$fsetting['mastermail']}>");
		if ( $issend == 1 ){
			$sendmsg = $lang['strings27'];
		}else{
			$sendmsg = $lang['strings26'];
		}
	}else{
		$sendmsg = $lang['strings25'];
	}
	//发送注册短消息
	if ($fsetting['sendnewusermsg'] == 1){
		$title = $lang['strings28'].$fsetting['forumname'];
		$body = str_replace('{$forumname}',$fsetting['forumname'],$lang['strings29']);
		$db->query("Insert Into {$dv}message (incept,sender,title,content,sendtime,flag,issend) Values ('$username','$fsetting[forumname]','$title','$body','".time()."',0,1)");
		$query=$db->query("Select id From {$dv}message Order By id Desc Limit 1");
		$message =& $db->fetch_array($query);
		$usermsg = '1||' .$message['id']. '||' .$fsetting['forumname'];
		$db->query("Update {$dv}user Set usermsg='$usermsg' Where userid='$userid'");
	}
	if ($fsetting['mailpassword'] == 0 && $fsetting['isnewuserlimited'] == 0){
		$db->query("Delete From {$dv}online Where username='$GLOBALS[membername]' Or id='$GLOBALS[statuserid]'");
		$cookiedate = $GLOBALS['usercookies'];
		$joindate = gmdate('Y-m-d H:i:s');
		set_cookie("userid",$userid,$cookiedate);
		set_cookie("username",$username,$cookiedate);
		set_cookie("password",$truepassword,$cookiedate);
		set_cookie("userhidden",2,$cookiedate);
		set_cookie("joindate",$joindate,$cookiedate);
	}
	set_cookie("regtime",$joindate,86400);
	//通行证部分
	//更新缓存
	$username = htmlspecialchars($username);
	$db->query("UpDate {$dv}setup Set forum_usernum=forum_usernum+1,forum_lastuser='$username'");
	require_once INC_PATH.'BoardCache.class.php';
	$mycache =& new BoardCache($db);
	$mycache->createForumSetting();
	//成功信息
	if ($GLOBALS['goto'] == ''){
		$GLOBALS['goto'] = $fsetting['homepage'];
	}
	if ($sendmsg == ''){
		$sendmsg = $lang['strings30'];
	}else{
		$sendmsg = $lang['strings30']. '|||' .$sendmsg;
	}
	$sendmsg = str_replace('{$forumname}',$fsetting['forumname'],$sendmsg);
	$leftbar = isset($_COOKIE[$cookieprename.'setleftbar']) ? $_COOKIE[$cookieprename.'setleftbar'] : $fsetting['leftbar'];

	//通行证接口
	global $passport,$comeurl;
	if($passport['enabledpassportserver']=='1'){
		$action='login';
		include(ROOT_PATH."passport/{$passport['colientsystem']}.php");
		header("Location:{$url}");
		exit;
	}
   //end 通行证接口
   
	if($leftbar) {
		echo "<script language=\"JavaScript\">top.location.href=\"".$fsetting['homepage']."?targeturl=".urlencode($GLOBALS['goto'])."\"</script>";
		exit;
	}
	showmsg($sendmsg,1,$GLOBALS['goto']);
}

function IsTrueName($name){
	global $fsetting, $lang;
	//$name = trim($name);
	if (1 == preg_match('/\s+/i', $name)) {	//	用户名中不允许存在空白字符
		return false;
	}
	$regsplitwords = trim($fsetting['regsplitwords']);
	if ($regsplitwords !== '') {
		$regsplitwords = preg_replace('#,(?=,)#i', '', $regsplitwords);
		$len = strlen($regsplitwords);
		($len > 1) AND (',' === $regsplitwords{$len - 1}) AND ($regsplitwords = substr($regsplitwords, 0, $len - 1));
		$regsplitwords = str_replace(array(',', '/'), array('|', '\\/'), preg_quote($regsplitwords)).'|,';
		if (1 == preg_match("/{$regsplitwords}/i", $name)) {
			return false;
		}
	}
	
	if (1 !== preg_match('/^(?:[-\w]|[\x80-\xff])+$/im', $name)) {
		return false;
	}
	//简体编码的全角空白符\xa1\xa1	繁体编码的全角空白符\xa1\x40	utf-8编码的全角空白符\xe3\x80\x80
	if (1 == preg_match('/\xa1\xa1|\xa1\x40|\xe3\x80\x80/i', $name)) {
		return false;
	}

	if (1 !== preg_match('/^(?:[-\w]|(?:[\x80-\xFF][\x00-\xFF]))+$/i', $name)) {
		return false;
	}
	return true;
}
function checkreal($str){
	if (is_null($str)){return;}
	$str = str_replace('|','',$str);
	return $str;
}
//多项选取转换输出
function Chk_KidneyType($str0,$str1,$str2){
	$str2 = explode(',',$str2);
	$returnVal = array();
	$count = count($str2);
	for( $i = 0 ; $i < $count ; $i++ ){
		$val = trim($str2[$i]);
		$checked = ( strpos($str1,$val) !== false ? ' checked ' : '' );
		$br = ( ($i+1) % 5 == 0 ? '<br>' : '' );
		$returnVal[] = "<INPUT type=\"checkbox\" name=\"{$str0}[]\" value=\"{$val}\" {$checked} class=chkbox>{$val}{$br}";
	}
	if( empty($returnVal) ){
		return '';
	} else{
		return implode('',$returnVal);
	}
}//end function Chk_KidneyType

//下拉菜单转换输出
Function chk_select($str1,$str2){
	$returnVal = array();
	if( empty($str1) ){
		$returnVal[] = '<option value="" selected>...</option>';
	}
	$str2 = explode(',',$str2);
	if( !empty($str2) && is_array($str2) ){
		foreach( $str2 as $val ){
			$selected = ( $val == $str1 ? ' selected ' : '' );
			$returnVal[] = "<option value=\"{$val}\" {$selected}>{$val}</option>";
		}
	}
	if( empty($returnVal) ){
		return '';
	} else{
		return implode('',$returnVal);
	}
}//end function chk_select



?>