<?php

require './include/common.inc.php';

if($HS['enableserverpassport'])
{
	$registerurl = $HS['passport_serverurl'].$HS['passport_registerurl'];
	if($PHP_QUERYSTRING) $registerurl .= strpos($registerurl, '?') ? '&'.$PHP_QUERYSTRING : '?'.$PHP_QUERYSTRING;
	header('location:'.$registerurl);
	exit;
}

if($_userid) showmessage($LANG['not_allow_register_repeat']);
if(!$MOD['enableregister']) showmessage($LANG['sorry_new_register_not_allowed']);

if(!isset($forward)) $forward = $PHP_REFERER;
if(!isset($action)) $action = '';


if($dosubmit)
{
	checkcode($checkcodestr, $MOD['enablecheckcodeofreg'], $PHP_REFERER);
	if(is_badword($username)) showmessage($LANG['username_not_accord_with_critizen']);
	if(strlen($password)<4 || strlen($password)>20) showmessage($LANG['password_not_less_than_4_longer_than_20']);
	if(!is_email($email)) showmessage($LANG['input_valid_email']);

	if($member->exists()) showmessage("$username ".$LANG['have_registered']);

	if($MOD['enablemultiregperemail'] == 0 && $member->email_exists($email)) showmessage("$email ".$LANG['have_used_change_one_email']);

	$idtype = $s_card_n;
	$idcard = $card_n;
	$myanswer = $t_ans_n;
	$memberinfo = array('username'=>$username, 'password'=>$password,'email'=>$email,'groupid'=>6,'idtype'=>$idtype,'idcard'=>$idcard,"question"=>$s_q_n,"answer"=>$myanswer,"truename"=>$truename);

	if($userid = $member->register($memberinfo))
	{
//推广部份
    if ($bill)
    {
    $bill = base64_decode($bill);	

		if(is_numeric($bill))
		{
		  $biller = $db->get_one("SELECT username FROM ".TABLE_MEMBER." WHERE userid=$bill");
		  if($biller)
		  {
			 
			  $db->query("INSERT INTO ".TABLE_BILL." (`userid`,`biller`,`ip`,`addtime`,`tgname`) VALUES('$bill','$biller[username]','$PHP_IP','$PHP_TIME','$username')");
		  }
		}
}
//推广结束
		//自动激活分区
		$_userid = $userid;
		$_username = $username;
		//doSub($gameid, $passowrd);
		doSub($gameid, $password);

		$info = $member->login($password, 0);
		$forward = $PHP_SITEURL.'game/index.php';
		if($HS['enablepassport'])
		{
			$action = 'login';
			extract($info);
			require MOD_ROOT.'/passport/'.$HS['passport_file'].'.php';
			header('location:'.$url);
			exit;
		}
		showmessage('恭喜您，注册成功！', $forward);
		//showmessage('恭喜您，注册成功！下一步请激活游戏分区。', $forward);
	}
	else
	{
    $member->logout();
		showmessage($LANG['register_fail']);
	}
}
elseif($action == 'checkuser')
{
	if(strtolower($CONFIG['charset']) != 'utf-8' && preg_match("/^([\s\S]*?)([\x81-\xfe][\x40-\xfe])([\s\S]*?)/", $username))
	{
		include HS_ROOT.'/include/charset.func.php';
		$username = convert_encoding('utf-8', $CONFIG['charset'], $username);
        $member->set_username($username);
	}
	if(strlen($username) < 5 || strlen($username) > 12)
	{
		echo 1;
	}
	elseif($member->is_badword($username))
	{
		echo 2;
	}
	elseif($member->is_reg($username))
	{
		echo 3;
	}
	elseif($member->ban_name($username))
	{
		echo 4;
	}
	elseif(!ctype_alnum($username))
	{
	  echo 5;
	}
	else
	{
		echo 0;
	}
}
elseif($action == 'checkemail')
{
	if(!is_email($email))
	{
		echo 1;
	}
	elseif(!$MOD['enablemultiregperemail'] && $member->email_exists($email))
	{
		echo 2;
	}
	else
	{
		echo 0;
	}
}
elseif($action == 'register')
{

	$head['title'] = $LANG['member_register'];
	$head['keywords'] = $LANG['member_register'];
	$head['description'] = $LANG['member_register'];

    //if(!isset($forward) || $forward == '') $forward = $PHP_SITEURL;
	
	$result = $db->query("SELECT * FROM ".TABLE_GAMESERVER." ORDER BY id");
	while($r = $db->fetch_array($result))
	{
		$servers[] = $r;
	}
    
    include template('member', 'register');
}
else
{
header('location:?action=register');
}



function doSub($id,$gpass)
{
global $db,$_userid,$_username;
if (!$id) return "错误的参数";
$rs = $db->get_one("SELECT * FROM ".TABLE_GAMESERVER." WHERE id='$id'");
@extract($rs);
if(!$dataattr) return "错误类型:unset data_attrib，请联系管理员！";
if($dataattr == 'mssql')
{
if(!extension_loaded('mssql')) return "错误类型:unloaded mssql，请联系管理员！";
include HS_ROOT.'/include/db_'.$dataattr.'.class.php';
$mssqlco = @mssql_connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
if(!$mssqlco) return "错误类型:connect mssql error，请联系管理员！";
$sqldb = new db_mssql;
$sqldb->connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
}
elseif($dataattr == 'mysql')
{
$msyqlco = @mysql_connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
if(!$msyqlco) return "错误类型:connect mysql error，请联系管理员！";
$sqldb = new db_mysql;
$sqldb->connect($serverip.':'.$port,$loginname,$loginpass,$dataacc);
}
else
{
return "错误类型:data_attrib error，请联系管理员！";
}
$spwd = hsmd5($gpass);
$checkuser = $sqldb->get_one("SELECT password,user_priv FROM user WHERE username='$_username'");
if($checkuser)
{
if ($checkuser['password'] == $spwd &&$checkuser['user_priv'] == "1")
{
$area = $db->get_one("SELECT area FROM ".TABLE_MEMBER_INFO." WHERE userid='$_userid'");
if(!$area['area'] ||$area['area'] =='')
{
$newarea = $id;
}else{
$newarea = $area['area'].','.$id;
}
$db->query("UPDATE ".TABLE_MEMBER_INFO." SET area='$newarea' WHERE userid='$_userid'");
if($db->affected_rows())
{
return "true";
exit;
}else{
return "错误类型:update area error，请联系管理员！";
}
}else{
$query = "UPDATE user SET password='$spwd',user_priv='1' WHERE username='$_username'";
}
}
else
{
$query = "INSERT INTO user (username,password,user_priv) VALUES ('$_username','$spwd','1')";
}
$sqldb->query($query);
if($sqldb->affected_rows())
{
$area = $db->get_one("SELECT area FROM ".TABLE_MEMBER_INFO." WHERE userid='$_userid'");
if(!$area['area'] ||$area['area'] =='')
{
$newarea = $id;
}else{
$newarea = $area['area'].','.$id;
}
$db->query("UPDATE ".TABLE_MEMBER_INFO." SET area='$newarea' WHERE userid='$_userid'");
if($db->affected_rows())
{
return "true";
exit;
}else{
return "错误类型:update area error，请联系管理员！";
}
}else{
return "错误类型:unknow insert error，请联系管理员！";
}
}
?>