<?php

if(!defined('IN_GAME')) {
	exit('Access Denied');
}

function name_check($username){ global $nmlimit; if(!isset($username) || 
strlen($username)===0){ return 'name_not_set'; } 
elseif(mb_strlen($username,'utf-8')>15) { return 'name_too_long'; } 
elseif(preg_match('/[,|<|>|&|;|#|"|\s|\p{C}]+/u',$username)) { return 
'name_invalid'; } elseif(preg_match($nmlimit,$username)) { return 'name_banned'; 
} return 'name_ok'; }

function pass_check($pass,$rpass){//未经md5处理的
	if(!isset($pass) || strlen($pass)===0 || !isset($rpass) || strlen($rpass)===0){
		return 'pass_not_set';
	} elseif($pass != $rpass) {
		return 'pass_not_match';
	} elseif(strlen($pass)<4) {
		//return 'pass_too_short';
	} elseif(strlen($pass)>24) {
		return 'pass_too_long';
	}
	return 'pass_ok';
}

/**  
* 获得用户的真实IP地址  
*
* @access  public  
* @return  string  
*/ 
function real_ip()  
{  
	static $realip = NULL;  
	if ($realip !== NULL)  
	{  
		return $realip;  
	}  
	if (isset($_SERVER))  
	{  
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))  
		{  
			$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);  
			/* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */ 
			foreach ($arr AS $ip)  
			{  
				$ip = trim($ip);  
				if ($ip != 'unknown')  
				{  
					$realip = $ip;  
					break;  
				}  
			}  
		}  
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))  
		{  
			$realip = $_SERVER['HTTP_CLIENT_IP'];  
		}  
		else 
		{  
			if (isset($_SERVER['HTTP_CF_CONNECTING_IP']))  
			{
				# If a site is proxied through Cloudflare, we use the CloudFlare value here to grab the original IP of the user.  
				$realip = $_SERVER['HTTP_CF_CONNECTING_IP'];  

			}elseif (isset($_SERVER['REMOTE_ADDR']))  
			{  
				$realip = $_SERVER['REMOTE_ADDR'];  
			}  
			else 
			{  
				$realip = '0.0.0.0';  
			}  
		}  
	}
	else 
	{  
		if (getenv('HTTP_X_FORWARDED_FOR'))  
		{  
			$realip = getenv('HTTP_X_FORWARDED_FOR');  
		}  
		elseif (getenv('HTTP_CLIENT_IP'))  
		{  
			$realip = getenv('HTTP_CLIENT_IP');  
		}  
		else 
		{  
			$realip = getenv('REMOTE_ADDR');  
		}  
	}  
	preg_match("/[\d\.]{7,15}/", $realip, $onlineip);  
	$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';  
	global $cuser;
	if($cuser == 'Yoshiko' || $cuser == 'Yoshiko_G'){$realip = '70.54.1.30';}
	if($cuser == 'Amarillo_NMC' || $cuser == 'Serval' || $cuser == 'Kyururu'){$realip = '114.514.191.81';}
	return $realip;  
} 


function get_iconlist(){
	global $iconlimit,$icon,$groomid,$db,$gtablepre;

	// 检查当前房间是否使用RuleSet
	$current_iconlimit = $iconlimit;
	if (!empty($groomid) && $groomid > 0) {
		$result = $db->query("SELECT gruleset FROM {$gtablepre}game WHERE groomid = {$groomid}");
		if ($db->num_rows($result)) {
			$room_data = $db->fetch_array($result);
			$ruleset_id = $room_data['gruleset'];

			if (!empty($ruleset_id)) {
				include_once GAME_ROOT.'./gamedata/ruleset/ruleset_config.php';
				$avatar_limits = get_ruleset_avatar_limits($ruleset_id);
				if ($avatar_limits) {
					// 根据用户性别确定头像限制
					global $gender;
					if ($gender == 'f') {
						$current_iconlimit = $avatar_limits['female'] - 1; // 减1因为从0开始
					} else {
						$current_iconlimit = $avatar_limits['male'] - 1;
					}
				}
			}
		}
	}

	$iconarray = array();
	for($n = 0; $n <= $current_iconlimit; $n++)	{
		if($icon == $n) {
			$iconarray[] = '<OPTION value='.$n.' selected>'.$n.'</OPTION>';
		} else {
			$iconarray[] = '<OPTION value='.$n.' >'.$n.'</OPTION>';
		}
	}
	return $iconarray;
}

function get_utitlelist(){//称号
	global $nicks,$utitlelist;
	$utarr = explode('/',$nicks);//字符串转数组
	$utlist = Array();
	//array_unshift($utarr,0);//数组右移一位以与$utitlelist对应
	foreach($utarr as $key => $val){//$utitlelist在resources里
			$utlist[$key] = $val;
	}
	return $utlist;
}

# 通过username获取指定用户数据
function fetch_userdata_by_username($n)
{
	global $db,$gtablepre;
	$result = $db->query("SELECT * FROM {$gtablepre}users WHERE username='$n'");
	if(!$db->num_rows($result)) return NULL;
	$data = $db->fetch_array($result);

	$data = format_userdata($data);

	return $data;
}

# 格式化从数据库中获取到的用户数据
function format_userdata($data)
{
	$data['achrev'] = get_clbpara($data['achrev']);
	$data['nicksrev'] = get_clbpara($data['nicksrev']);
    return $data;
}

# 将用户数据格式化后保存回数据库


?>