<?php
include 'common.php';


function InsertSpeedHackLog($account_id, $login_key, $char_uid, $hack_type, $hack_value, $zone_id)
{
	$dbhandle = DB_Connect();
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");
	
	
	//if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	//{
	//	PrintLoginKeyError();
	//	return;
	//}
	
	$log_time = date('Y-m-d H:i:s');
	
	$insert_query = "INSERT INTO speed_hack_log (user_id, character_uid, hack_type, hack_value, zone_id, log_time) VALUES ('".$account_id."',".$char_uid.",".$hack_type.",".$hack_value.",".$zone_id.",'".$log_time."')";

	//Query Result
	$result = @mysqli_query($dbhandle,$insert_query);
	$response = "LOG_FAILED";
	$error = "";
	if($result)
	{
		$response = "LOG_OK";
		// don't use for upate/insert query, because these query result is true/false, not mysqli_result object
		//mysqli_free_result($result); 
		
		//$error = $insert_query ;
	}
	else
	{
		
		$error = mysqli_error($dbhandle);
		//$error = $insert_query ;
	}
	
	 mysqli_close($dbhandle);
 
    $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));
}


$account_id = $_GET['account_id'];
$login_key = $_GET['login_key'];
$char_uid = $_GET['char_uid'];
$hack_type = $_GET['hack_type'];
$hack_value = $_GET['hack_value'];
$zone_id = $_GET['zone_id'];



if(CheckValidatedServerIP())
{
	
	InsertSpeedHackLog($account_id, $login_key, $char_uid, $hack_type, $hack_value, $zone_id);
	

}
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	));
}

?>