<?php
include 'common.php';
include 'server_exchange_common.php';

function GetRegisteredItemUids($account_id, $character_uid, $login_key)
{
	$dbhandle = DB_Connect();
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");

	$return = "GET_REGISTERED_ITEM_UIDS_FAILED";
	$error = "";
	$dataArray = array();

	if (CheckExchangeIsMaintenance($dbhandle))
	{
		$return = "EXCHANGE_UNDER_MAINTENANCE";
		$response = array('result'=>$return,'error'=>$error,'list'=>$dataArray);
	}
	else
	{
		$now = new DateTime('now', new DateTimeZone('UTC'));
		$strNow = $now->format('Y-m-d H:i:s');
		$twoDaysAgo = date('Y-m-d H:i:s', strtotime($strNow." -2 days"));

		$query = "SELECT DISTINCT item_uid FROM exchange_item WHERE is_cancel=0 AND register_date>='".$twoDaysAgo."'";

		if ($result = mysqli_query($dbhandle, $query))
		{
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			{
				array_push($dataArray, $row["item_uid"]);
			}
			mysqli_free_result($result);
		}

		$return = "GET_REGISTERED_ITEM_UIDS_OK";
		$response = array('result'=>$return,'error'=>"",'list'=>$dataArray);
	}

	$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);

	mysqli_close($dbhandle);
}

$account_id = $_GET['account_id'];
$character_uid = $_GET['character_uid'];
$login_key = $_GET['login_key'];

if(CheckValidatedServerIP())
{
	GetRegisteredItemUids($account_id, $character_uid, $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));
}

?>
