<?php
include 'header.php';
include '__unique_config_no_deploy__.php';

//define('DB_NAME', 'game_hs');   //v1
define('DB_NAME', 'game_hs_v2');   //v2

// 공용 아이템 UID 상수
const GOLD_ITEM_UID = 50008;
const CRYSTAL_ITEM_UID = 50009;

function get_ip()
{
    switch(true)
    {
        case (!empty($_SERVER['HTTP_X_REAL_IP'])) : return $_SERVER['HTTP_X_REAL_IP'];
        case (!empty($_SERVER['HTTP_CLIENT_IP'])) : return $_SERVER['HTTP_CLIENT_IP'];
        case (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) : return $_SERVER['HTTP_X_FORWARDED_FOR'];
        case (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) : return $_SERVER['HTTP_CF_CONNECTING_IP'];
        default : return $_SERVER['REMOTE_ADDR'];
    }
}


function CheckValidatedServerIP()
{
	//dev 서버는 무조건 return true;  dev 서버는 호스트로 테스트를 많이 하기 때문.
	//return false;
	return true;
	
	/* $ip = get_ip();//$_SERVER['REMOTE_ADDR']; 

	// 허용 아이피
	$iplist = array(
                "52.231.15.71",  // dev 1
				"20.41.87.34", // game 01
				"20.194.26.75", //game 03
				"52.231.96.229"); // game 02
		
		 foreach ($iplist as $value) 
		 { 
			if (strpos($ip, $value) === 0) return true;
			else continue; 

		 }
  
	
	return false; */
}

function CheckServerVersion($client_version)
{
	 $server_version = "0.17.0";

	$client_ver_string = explode('.', $client_version );
	$server_ver_string = explode('.',$server_version);
	if($client_ver_string[0] < $server_ver_string[0] || $client_ver_string[1] < $server_ver_string[1])
	{
		$response = "VERSION_FAILED"; 
		$notice = "Please update new version";
		
		$isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
		(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
		($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
	 
		echo json_encode(array('result'=>$response,'notice'=>$notice));
		return false;
	}
	return true;
}

function CheckServerMaintenance()
{
	$server_check = true;   //서버 점검 완료
	//$server_check = false; //서버 점검증
	
	if ($server_check == false)
	{
		$response = "MAINTENANCE_CHECK_PROGRESSING";
		$title = "Server is being checked";
		$notice = "Server is being checked";
		
		$isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
		(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
		($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
		
		echo json_encode(array('result'=>$response,'title'=>$title,'notice'=>$notice));
	}
	return $server_check;
}

include 'functions.php';

// function AlreadyExistsItem($dbhandle,$char_uid, $item_uid)
// {
	// $return =false;
	
	// $strQuery = "select count(*) from owned_item where character_uid=".$char_uid." and item_uid=".$item_uid;
	// if($query_result = mysqli_query($dbhandle, $strQuery)){
		// $data = mysqli_fetch_row($query_result);
		// if( $data[0] > 0 )
		// {
			// $return = true;
		// }
		// mysqli_free_result($query_result); 
	// }
	
	// return $return;
// }

// function UpdateSkillBookCount($dbhandle, $char_uid, $book_inven_uid, $book_update_count)
// {
	// if($book_update_count >0)
		// $query = "update owned_item set count=".$book_update_count." where owned_item_uid=".$book_inven_uid." and character_uid=".$char_uid;
	// else	
		// $query = "delete from owned_item where owned_item_uid=".$book_inven_uid." and character_uid=".$char_uid;

	// //Query Result
	// $result = @mysqli_query($dbhandle,$query);
	// if($result)
	// {
		// return true;
	// }
	
	// return false;
// }


// function getFromUrl($url, $method = 'GET')
// {   
    // // Initialize
    // $info   = parse_url($url);
    // $req    = '';
    // $data   = '';
    // $line   = '';
    // $agent  = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)';
    // $linebreak  = "\r\n";
    // $headPassed = false;
 
    // // Setting Protocol
    // switch($info['scheme'] = strtoupper($info['scheme']))
    // {
        // case 'HTTP':
            // $info['port']   = 80;
            // break;
 
        // case 'HTTPS':
            // $info['ssl']    = 'ssl://';
            // $info['port']   = 443;
            // break;
 
        // default:
            // return false;
    // }
 
    // // Setting Path
    // if(!$info['path'])
    // {
        // $info['path'] = '/';
    // }
 
    // // Setting Request Header
    // switch($method = strtoupper($method))
    // {
        // case 'GET':
            // if($info['query'])
            // {
                // $info['path'] .= '?' . $info['query'];
            // }
 
            // $req .= 'GET ' . $info['path'] . ' HTTP/1.1' . $linebreak;
            // $req .= 'Host: ' . $info['host'] . $linebreak;
            // $req .= 'User-Agent: ' . $agent . $linebreak;
            // $req .= 'Referer: ' . $url . $linebreak;
            // $req .= 'Connection: Close' . $linebreak . $linebreak;
            // break;
 
        // case 'POST':
            // $req .= 'POST ' . $info['path'] . ' HTTP/1.1' . $linebreak;
            // $req .= 'Host: ' . $info['host'] . $linebreak;
            // $req .= 'User-Agent: ' . $agent . $linebreak; 
            // $req .= 'Referer: ' . $url . $linebreak;
            // $req .= 'Content-Type: application/x-www-form-urlencoded'.$linebreak; 
            // $req .= 'Content-Length: '. strlen($info['query']) . $linebreak;
            // $req .= 'Connection: Close' . $linebreak . $linebreak;
            // $req .= $info['query']; 
            // break;
    // }
 
    // // Socket Open
    // $fsock  = @fsockopen($info['ssl'] . $info['host'], $info['port']);
    // if ($fsock)
    // {
        // fwrite($fsock, $req);
        // while(!feof($fsock))
        // {
            // $line = fgets($fsock, 128);
            // if($line == "\r\n" && !$headPassed)
            // {
                // $headPassed = true;
                // continue;
            // }
            // if($headPassed)
            // {
                // $data .= $line;
            // }
        // }
        // fclose($fsock);
    // }
 
    // return $data;
// }

// function CheckTicket($platform, $account_id, $ticket)
// {
	// if ($platform === "steam")
	// {
		// if (CheckSteamTicket($ticket))
		// {
			// return true;
		// }
		// else
		// {
			// return false;
		// }
	// }
	// else if ($platform === "oculus")
	// {
		// if (CheckOculusTicket($ticket, $account_id))
		// {
			// return true;
		// }
		// else
		// {
			// return false;
		// }
	// }
	// else if ($platform === "quest")
	// {
		// //quest는 아직 인증을 할 수 없으므로 true를 반환한다.
		// return true;
	// }
	
	// return false;
// }


// function CheckSteamTicket($ticket)
// {
	// $appid = "696960";
	// $key = "97E44C8D71A80DAD981DCB66DC684403";
	
	// $url = "https://api.steampowered.com/ISteamUserAuth/AuthenticateUserTicket/v1/?key=".$key."&appid=".$appid."&ticket=".$ticket ;
	
	// // {"response":{"params":{"result":"OK","steamid":"123456","ownersteamid":"123456","vacbanned":false,"publisherbanned":false}}}
	// $json = json_decode(getFromUrl($url), true);
	// $result = $json['response']['params']['result'];
	
	// if ($result === "OK")
	// {
		// return true;
	// }
	// else
	// {
		// return false;
	// }
// }

// function CheckOculusTicket($ticket, $id)
// {
	// $url = 'https://graph.oculus.com/user_nonce_validate'; //주소셋팅
	// $postfields["access_token"] = "OC|2909051379138471|40fd4b4995279ad89236666d6f6c9c77";
	// $postfields["user_id"] = $id; 
	// $postfields["nonce"] = $ticket;

	// $ch = curl_init(); //curl 로딩

	// curl_setopt($ch, CURLOPT_URL, $url);
	// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	// curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
	// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
	// curl_setopt($ch, CURLOPT_HEADER, 0); 
	// curl_setopt($ch, CURLOPT_POST, true);

	// $curl_result = curl_exec ($ch); // curl 실행 및 결과값 저장
	// curl_close ($ch); // curl 종료
	
	// $json_result = json_decode($curl_result); //{"is_valid":true} or {"is_valid":false}
	// $is_valid = $json_result->is_valid;
	
	// if ($is_valid)
	// {
		// return true;
	// }
	// else
	// {
		// return false;
	// }
// }
	
// //사용하지 말것
// function CheckServerVersion($client_version)
// {
	 // $server_version = "0.2.1";
	 
	 
	// if($client_version == '1')
	// {
		// $response = "VERSION_FAILED";
		// $error = "Please update new version";
		// $new_login_key = "";
		
		// $isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
		// (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
		// ($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
	 
		// echo json_encode(array('result'=>$response,'login_key'=>$new_login_key,'error'=>$error));
		// return;
	// }

	// $client_ver_string = explode('.', $client_version );
	// $server_ver_string = explode('.',$server_version);
	// if($client_ver_string[0] < $server_ver_string[0] || $client_ver_string[1] < $server_ver_string[1])
	// {
		// $response = "VERSION_FAILED";
		// $error = "Please update new version";
		// $new_login_key = "";
		
		// $isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
		// (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
		// ($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
	 
		// echo json_encode(array('result'=>$response,'login_key'=>$new_login_key,'error'=>$error));
		// return;
	// }
 
// }

?>