<?php  
include 'common.php';

function EchoRespone($return, $responseMsg)
{
	$response = array('result'=>$return,'error'=>$responseMsg);
    
 
    $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($response);
}

function BlockCharacter($character_name, $gm_account_id, $gm_login_key)
{
    $dbhandle = DB_Connect();
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
    or die("Could not select db");
	
	if(!CheckLoginKey($dbhandle, $gm_account_id, $gm_login_key))
	{
		PrintLoginKeyError();
		mysqli_close($dbhandle);
		return;
	}
	
    
    $strQuery = "select * from character_list where character_name='".$character_name."'";
    if($query_result = mysqli_query($dbhandle, $strQuery))
    {
        $data = mysqli_fetch_row($query_result);
		mysqli_free_result($query_result); 
		$cnt = is_null($data) ? 0 : count($data);
		if($cnt > 0)
		{			
			$update_query = "update character_list set block=1 where character_name='".$character_name."'";
			//Query Result
			$result = @mysqli_query($dbhandle,$update_query);
			if($result)
			{
                EchoRespone("BLOCK_CHARACTER_OK", $character_name);
			}
			else
			{
                $error = mysqli_error($dbhandle);
                EchoRespone("BLOCK_CHARACTER_FAILED", $error);
			}
        }
        else
        {
            EchoRespone("BLOCK_CHARACTER_FAILED", "NoExistCharacter");    
        }
    }
    else
    {
        //DB오류
		$error = mysqli_error($dbhandle);
		EchoRespone("BLOCK_CHARACTER_FAILED", $error);
    }

    mysqli_close($dbhandle);
}



if(CheckValidatedServerIP())
{
	BlockCharacter($_GET['character_name'], $_GET['gm_account_id'], $_GET['gm_login_key']);
}
else
{
	$response = "INVALIDATED_SERVER";
	$error = "CheckValidatedServerIP ERROR";
	$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,'error'=>$error	));
}

?>