<?
function kiemtra_dodai($value,$info,$length_start,$length_end) 
{
	include("config.php");
	global $ContentUpdate;
	if (strlen($value)<$length_start || strlen($value)>$length_end)
	{
		$Content= "$info phải có độ dài từ $length_start tới $length_end kí tự.";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function kiemtra_null($value,$info) 
{
	include("config.php");
	global $ContentUpdate;
	if (!isset($value) OR $value=="" OR strlen($value)==0)
	{
		$Content= "$info không được để trống. ";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function kiemtra_so($value,$info)
{
	include("config.php");
	global $ContentUpdate;
	if (preg_match("/[^0-9$]/", $value))
	{
		$Content= "$info chỉ được dùng ký tự số 0-9.";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function kiemtra_chu_so($value,$info)
{
	include("config.php");
	global $ContentUpdate;
	if (preg_match("/[^a-zA-Z0-9_$]/", $value))
	{
		$Content= "$info chỉ được dùng ký tự a-z,0-9.";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function kiemtra_chu_so_($value,$info) {
	include("config.php");
	global $ContentUpdate;
	if (preg_match("/[^a-zA-Z0-9_$]/", $value)){
    	$Content= "$info chỉ được dùng ký tự a-z,0-9,_";
    	echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function kiemtra_chu_so_dot($value,$info) {
	include("config.php");
	global $ContentUpdate;
	if (preg_match("/[^a-zA-Z0-9.$,]/", $value)){
    	$Content= "$info chỉ được dùng ký tự a-z,0-9,.";
    	echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}

function kiemtra_email($email)
{
	include("config.php");
	global $ContentUpdate;
	if (preg_match("/[^a-zA-Z0-9_@.$]/", $email))
	{
    	$Content= "Email không được dùng ký tự đặc biệt.";
    	echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
	elseif (!preg_match("/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[\.a-z]{2,6}$/", $email)){
		$Content= "Email không đúng định dạng.";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function kiemtra_phone($phone)
{
	include("config.php");
	global $ContentUpdate;
	$DauSo=substr($phone,0,2);
	$LengthPhone=strlen($phone);
	if(!($DauSo=='01' && $LengthPhone==11 || $DauSo=='09' && $LengthPhone==10) && preg_match("/[^0-9$]/", $phone))
	{
		$Content= "Số điện thoại không đúng";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function checklogin_access($login,$sid)
{
	include("config.php");
	global $ContentUpdate;
	$query_checklogin = "SELECT sid FROM MEMB_INFO WHERE memb___id='$login'";
	$result_checklogin = mssql_query($query_checklogin);
	$checklogin =mssql_fetch_row($result_checklogin);
	if($sid != $checklogin[0])
	{
		$Content= "session_other";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
		
	}
}
function checkacc_exist($login)
{
	include("config.php");
	global $ContentUpdate;
	$sql_username_check = mssql_query("SELECT memb___id FROM MEMB_INFO WHERE memb___id='$login' COLLATE SQL_Latin1_General_CP1_CS_AS"); 
	$username_check = mssql_num_rows($sql_username_check);
	if ($username_check <= 0)
	{ 
 		$Content= "Tài khoản $login không tồn tại.";
 		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function checkacc_use($user)
{
	include("config.php");
	global $ContentUpdate;
	$sql_username_check = mssql_query("SELECT memb___id FROM MEMB_INFO WHERE memb___id='$user' COLLATE SQL_Latin1_General_CP1_CI_AS"); 
	$username_check = mssql_num_rows($sql_username_check);
	if ($username_check != 0)
	{
		$Content= "Tên truy cập này đã được sử dụng";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function checkphone_use($phone)
{
	include("config.php");
	global $ContentUpdate;
	$sql_phone_check = mssql_query("SELECT Phone_No FROM MEMB_INFO WHERE Phone_No='$phone'"); 
	$phone_check = mssql_num_rows($sql_phone_check);
	if ($phone_check != 0)
	{
		$Content= "Số điện thoại này đã được sử dụng";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function checkemail_use($email)
{
	include("config.php");
	global $ContentUpdate;
	$sql_email_check = mssql_query("SELECT Email_Address FROM MEMB_INFO WHERE Email_Address='$email'"); 
	$email_check = mssql_num_rows($sql_email_check);
	if ($email_check != 0)
	{
		$Content= "Email này đã được sử dụng";
		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function check_loggame($login)
{
	include("config.php");
	global $ContentUpdate;
	if ($login != 'admin')
	{
		$sql_loggame_check = mssql_query("SELECT * FROM MEMB_STAT WHERE memb___id='$login'"); 
		$loggame_check = mssql_num_rows($sql_loggame_check);
		if ($loggame_check < 1)
		{ 
	 		$Content= "Bạn phải đăng nhập vào game ít nhất một lần."; 
	 		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
		}
	}
}
function check_numchar($login)
{
	include("config.php");
	global $ContentUpdate;
	if ($login != 'admin')
	{
		$sql_numchar_check = mssql_query("SELECT * FROM character WHERE accountid='$login'"); 
		$numchar_check = mssql_num_rows($sql_numchar_check);
		if ($numchar_check < 1)
		{ 
	 		$Content= "Bạn phải tạo ít nhất một nhân vật."; 
	 		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
		}
	}
}
function check_block_acc($login)
{
	include("config.php");
	global $ContentUpdate;
	$sql_blockacc_check = mssql_query("SELECT Block_End_Time,bloc_code FROM MEMB_INFO WHERE memb___id='$login'"); 
	$blockacc_check = mssql_fetch_row($sql_blockacc_check);
	if($blockacc_check[1] != 0)
	{
		if($timestamp<$blockacc_check[0])
		{
   			$Content= "Tài khoản của bạn bị khóa tới ".date('H:i d-m-Y',$blockacc_check[0]).".";
   			echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
   		}
   		else
   		{
   			mssql_query("Update memb_info set bloc_code=0 WHERE memb___id='$login'");
   			mssql_query("Update character set ctlcode=0 WHERE accountid='$login'");
   		}
   	}
}
function check_pass($login,$pass)
{
	include("config.php");
	global $ContentUpdate;
	$check_pass_query = mssql_query("SELECT * FROM MEMB_INFO WHERE memb___id='$login' COLLATE SQL_Latin1_General_CP1_CS_AS AND PassCap1='$pass'");
	$check_pass = mssql_num_rows($check_pass_query);
	if( $check_pass == 0 )
	{
	 	$Content= "Mật khẩu cấp 1 không đúng."; 
	 	echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function check_pass2($login,$pass2)
{
	include("config.php");
	global $ContentUpdate;
	$sql_pw_check = mssql_query("SELECT * FROM MEMB_INFO WHERE PassCap2='$pass2' and memb___id='$login' COLLATE SQL_Latin1_General_CP1_CS_AS"); 
	$pw_check = mssql_num_rows($sql_pw_check);
	if ($pw_check <= 0)
	{ 
 		$Content= "Mật khẩu cấp 2 không đúng."; 
 		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
 	}
}
function check_char($login,$name)
{
	include("config.php");
	global $ContentUpdate;
	$sql_name_check = mssql_query("SELECT Name FROM Character WHERE Name='$name' and AccountID = '$login'"); 
	$name_check = mssql_num_rows($sql_name_check);
	if ($name_check <= 0)
	{ 
   		$Content= "IsCharacterNotExist"; 
   		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function check_block_char($login,$name)
{
	include("config.php");
	global $ContentUpdate;
	$sql_block_check = mssql_query("SELECT Name FROM Character WHERE Name='$name' and AccountID='$login' and CtlCode='1' and Status_UyThac<>'1'"); 
	$block_check = mssql_num_rows($sql_block_check);
	if ($block_check > 0)
	{ 
   		$Content= "isBlockChar"; 
   		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
 	}
}
function check_online($login)
{
	include("config.php");
	global $ContentUpdate;
	$sql_online_check = mssql_query("SELECT * FROM MEMB_STAT WHERE memb___id='$login' AND ConnectStat='1'");
	$online_check = mssql_num_rows($sql_online_check);
	if ($online_check > 0)
	{ 
   		$Content= "isOnline"; 
   		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function check_timeout($login)
{
	include("config.php");
	global $ContentUpdate;
	$sql_timeout_check = mssql_query("SELECT DisconnectTM FROM MEMB_STAT WHERE memb___id='$login'");
	$timeout_check = mssql_fetch_row($sql_timeout_check);
	$time_out = strtotime($timeout_check[0]);
	$time_now = $timestamp;
	$time_chenh = $time_now - $time_out;

	if ($time_chenh < 5)
	{ 
		$time_wait = 5 - $time_chenh;
   		$Content= "isTimeOut|$time_wait"; 
   		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function check_doinv($login,$name)
{
	include("config.php");
	global $ContentUpdate;
	$sql_doinv_check = mssql_query("SELECT * FROM AccountCharacter WHERE Id='$login' AND GameIDC='$name'");
	$doinv_check = mssql_num_rows($sql_doinv_check);
	if ($doinv_check > 0)
	{ 
   		$Content= "isNoChangeCharacter"; 
   		echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function check_GM($login)
{
	include("config.php");
	global $gm;;
	$sql_gm_check = mssql_query("SELECT * FROM Character WHERE AccountID='$login' and (CtlCode=8 or CtlCode=32)"); 
	$gm_check = mssql_num_rows($sql_gm_check);
	if ($gm_check <= 0)
	{ 
 		$gm = 'NoGM';
	}
	else $gm = 'isGM';
}
function check_ip($login,$ip)
{
	include("config.php");
	global $ContentUpdate;
	include("config/config_chucnang.ini");
	if($use_ip_protect)
	{
		$query_checkip = mssql_query("SELECT IP_Secure FROM MEMB_INFO WHERE memb___id='$login'");
		$checkip =mssql_fetch_row($query_checkip);
		if(!empty($checkip[0])) {
			$ip_array=explode(',',$checkip[0]);
			if(!in_array($ip,$ip_array)){
				$Content= "ip_other";
				echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
			}
		}
	}
	else
	{
		mssql_query("update memb_info set IP_Secure=null where IP_Secure is not null");
	}
}

function sosanh($a,$b,$infoa,$infob)
{
	include("config.php");
	global $ContentUpdate;
	if($a!=$b)
	{
    	$Content= "$infoa không giống với $infob.";
    	echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}
function sosanhtrung($a,$b,$infoa,$infob)
{
	include("config.php");
	global $ContentUpdate;
	if($a==$b)
	{
    	$Content= "$infoa không được giống với $infob.";
    	echo "<Response>".$Content."</Response>".$ContentUpdate;exit();
	}
}

?>