<?php
include 'common.php';

function CheckIsMaster($dbhandle, $guild_uid, $character_uid)
{
	$stmt = $dbhandle->prepare("SELECT master_uid FROM guild_info WHERE guild_uid=?");
	$stmt->bind_param("i", $guild_uid);
	$stmt->execute();
	$result = $stmt->get_result();
	if ($result)
	{
		if($row = $result->fetch_assoc())
		{
			$master_uid = $row["master_uid"];
			mysqli_stmt_free_result($stmt);

			if ($character_uid == $master_uid)
			{
				return true;
			}
			else
			{
				$return = "NOT_HAVE_PERMISSION";
				$error = "";
				$response = array('result'=>$return,'error'=>$error);
				EchoResponse($response);

				mysqli_close($dbhandle);
				exit;
			}
		}
		else
		{
			$return = "NO_EXIST_GUILD";
			$error = "";
			$response = array('result'=>$return,'error'=>$error);
			EchoResponse($response);

			mysqli_close($dbhandle);
			exit;
		}
	}
	else
	{
		$return = "DB_ERROR";
		$error = mysqli_error($dbhandle);
		$response = array('result'=>$return,'error'=>$error);
		EchoResponse($response);

		mysqli_close($dbhandle);
		exit;
	}
}

function EditNotice($guild_uid, $account_id, $character_uid, $notice, $login_key)
{
	//Connect DB
	$dbhandle = DB_Connect();
	
	
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");

	mysqli_query($dbhandle,"set session character_set_connection=utf8;");
	mysqli_query($dbhandle,"set session character_set_results=utf8;");
	mysqli_query($dbhandle,"set session character_set_client=utf8;");

	if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	{
		PrintLoginKeyError();
		mysqli_close($dbhandle);
		return;
	}

	if (CheckIsMaster($dbhandle, $guild_uid, $character_uid))
	{
		$stmt = $dbhandle->prepare("UPDATE guild_insidedata SET notice=? WHERE guild_uid=?");
		$stmt->bind_param("si", $notice, $guild_uid);
		$result = $stmt->execute();
		$stmt->close();
		
		if($result)
		{
			$return = "GUILD_EDIT_NOTICE_OK";
			$error = "";
			$response = array('result'=>$return,'error'=>$error);
			EchoResponse($response);
		}
		else
		{
			$return = "DB_ERROR";
			$error = mysqli_error($dbhandle);
			$response = array('result'=>$return,'error'=>$error);
			EchoResponse($response);
		}
	}
		
	mysqli_close($dbhandle);
}
function EchoResponse($response)
{
	$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);
}


$json = json_decode(file_get_contents('php://input'));
$guild_uid = $json->guild_uid;
$account_id = $json->account_id;
$character_uid = $json->character_uid;
$notice = $json->notice;
$login_key = $json->login_key;

if(CheckValidatedServerIP())
{
	EditNotice($guild_uid, $account_id, $character_uid, $notice, $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	));
}


?>