<?php
//error_reporting(E_ALL);
//ini_set('display_errors', '1');

include("include/database.php");

$database->dailyFunctions();

if(!isset($_GET['universalunreal']) || (isset($_GET['universalunreal']) && $_GET['universalunreal'] != "true"))
{
	header("Location: index.php");
	die();
}

// some data verification
if(isset($_GET['operation']) and $_GET['operation'] != "universelogin")
{
	$universeid = $database->urlSafeDecode($_GET['universeid']);
	$universepass = $_GET['universepass'];
	
	//verify that this request is legit when it's not a login attempt
	if($universeid != UNIVERSE_ID || $universepass != UNIVERSE_PASS)
	{
		die();
	}	
	
	// verify that server requesting access is in database list
	if(!verify_server(mysql_real_escape_string($_GET['thisserver'])))
	{
		die();
	}
}

if(isset($_GET['operation']) and $_GET['operation'] == "universelogin")
{
	$universeid = $database->urlSafeDecode($_GET['universeid']);
	$universepass = $_GET['universepass'];
	$fromserver = $_GET['thisserver'];
	$gamesession = $_GET['gamesession'];
	$version = $_GET['version'];
	
	$fromserver = mysql_real_escape_string($fromserver);
	$gamesession = mysql_real_escape_string($gamesession);

	if($version != BUILD)
	{
		echo "!!!return-data!!!&universe-server-login#version#".BUILD."#".$database->urlSafeReplace($database->getUpdateLocation())."&";
	}
	else if($universeid == UNIVERSE_ID && $universepass == UNIVERSE_PASS && verify_server($fromserver))
	{
		$allowLookupLocal = "0";
		$allowLookupUniversal = "0";
		if($database->UTCheckLocalLookupAllowed($fromserver))
		{
			$allowLookupLocal = "1";
		}
		if($database->UTCheckUniversalLookupAllowed($fromserver))
		{
			$allowLookupUniversal = "1";
		}
		echo "!!!return-data!!!&universe-server-login#accepted#".$allowLookupLocal."#".$allowLookupUniversal."&<!--EOL-->";
		ob_flush();
		flush();
		$database->UTStartNewMatch($fromserver,$gamesession);
	}
	else
	{
		echo "!!!return-data!!!&universe-server-login#denied&";
	}
}
else if(isset($_GET['operation']) and $_GET['operation'] == "login")
{
	// username, password, playername
	$username = $_GET['username'];
	$password = $_GET['password'];
	$playername = $_GET['playername'];
	$fromserver = $_GET['thisserver'];
	$clientid = $_GET['clientid'];
	$playerip = $_GET['playerip'];
	$collectplayerdata = $_GET['collectplayerdata'];
	
	$username = mysql_real_escape_string($username);
	$password = mysql_real_escape_string($password);
	$fromserver = mysql_real_escape_string($fromserver);
	$playername = mysql_real_escape_string($playername);
	$clientid = mysql_real_escape_string($clientid);
	$playerip = mysql_real_escape_string($playerip);

	if($collectplayerdata && LOG_PLAYER_DATA)
	{
		$database->UTRecordPlayerData($clientid,$playername,$playerip,$database->UTRetrieveNameByURL($fromserver));
	}
	$database->UTAttemptPlayerLogin($username,$password,$playername,$fromserver);
}
else if(isset($_GET['operation']) and $_GET['operation'] == "registerplayer")
{
	if(!LOG_PLAYER_DATA)
	{
		die();
	}
	
	$playername = $_GET['playername'];
	$fromserver = $_GET['thisserver'];
	$clientid = $_GET['clientid'];
	$playerip = $_GET['playerip'];

	$fromserver = mysql_real_escape_string($fromserver);
	$playername = mysql_real_escape_string($playername);
	$clientid = mysql_real_escape_string($clientid);
	$playerip = mysql_real_escape_string($playerip);

	$database->UTRecordPlayerData($clientid,$playername,$playerip,$database->UTRetrieveNameByURL($fromserver));
}
else if(isset($_GET['operation']) and $_GET['operation'] == "nexgenaction")
{
	if(!LOG_PLAYER_DATA)
	{
		die();
	}
	
	$fromserver = $_GET['thisserver'];
	$performerid = $_GET['performerid'];
	$performername = $_GET['performername'];
	$performerip = $_GET['performerip'];
	$receiverid = $_GET['receiverid'];
	$receivername = $_GET['receivername'];
	$receiverip = $_GET['receiverip'];
	$information = $_GET['information'];

	$fromserver = mysql_real_escape_string($fromserver);
	$performerid = mysql_real_escape_string($performerid);
	$performername = mysql_real_escape_string($performername);
	$performerip = mysql_real_escape_string($performerip);
	$receiverid = mysql_real_escape_string($receiverid);
	$receivername = mysql_real_escape_string($receivername);
	$receiverip = mysql_real_escape_string($receiverip);
	$information = mysql_real_escape_string($information);

	$database->UTRecordPlayerActions($performerid,$performername,$performerip,$receiverid,$receivername,$receiverip,$information,$database->UTRetrieveNameByURL($fromserver));
}
else if(isset($_GET['operation']) and $_GET['operation'] == "nexgenpm")
{
	$senderplayername = $_GET['senderplayername'];
	$senderuniversename = $_GET['senderuniversename'];
	$receiverplayername = $_GET['receiverplayername'];
	$receiveruniversename = $_GET['receiveruniversename'];
	$message = $_GET['message'];

	$fromID = 0;
	$toID = 0;
	
	if(strpos($senderuniversename,"PLAYERID:") === false) // use Universe Name
	{
		$from = $senderuniversename;
		$fromID = $database->getIDFromName($database->urlSafeDecode($senderuniversename));
	}
	else
	{
		$from = $senderplayername;
	}
		
	if(strpos($receiveruniversename,"PLAYERID:") === false) // use Universe Name
	{
		$to = $receiveruniversename;
		$toID = $database->getIDFromName($database->urlSafeDecode($receiveruniversename));
	}
	else
	{
		$to = $receiverplayername;
	}

	$to = mysql_real_escape_string($to);
	$from = mysql_real_escape_string($from);
	$message = mysql_real_escape_string($message);

	$database->savePMHistory($fromID,$toID,$from . " to " . $to . " (via Nexgen)",$message);
}
else if(isset($_GET['operation']) and $_GET['operation'] == "buddyaction")
{
	$value = $_GET['value'];
	$buddyid = $_GET['buddyid'];
	$username = $_GET['username'];

	$buddyid = mysql_real_escape_string($buddyid);
	$username = mysql_real_escape_string($username);
	$value = mysql_real_escape_string($value);

	$database->doBuddyAcceptDeny($database->getIDFromName($username),$buddyid,$value);
}
else if(isset($_GET['operation']) and $_GET['operation'] == "logout")
{
	$username = $_GET['username'];
	$fromserver = $_GET['thisserver'];
	
	$username = mysql_real_escape_string($username);
	$fromserver = mysql_real_escape_string($fromserver);

	$database->UTAttemptPlayerLogout($username,$fromserver);
}
else if(isset($_GET['operation']) and $_GET['operation'] == "returnbriefinfo")
{
	$fromserver = $_GET['thisserver'];
	$map = $_GET['map'];
	$numplayers = $_GET['numplayers'];
	$maxplayers = $_GET['maxplayers'];
	$players = $_GET['players'];
	
	$fromserver = mysql_real_escape_string($fromserver);
	$map = mysql_real_escape_string($map);
	$numplayers = mysql_real_escape_string($numplayers);
	$maxplayers = mysql_real_escape_string($maxplayers);
	$players = mysql_real_escape_string($players);

	$database->UTReturnBriefInfo($fromserver, $map, $numplayers, $maxplayers, $players);
}
else if(isset($_GET['operation']) and $_GET['operation'] == "briefinfo")
{
	$database->UTGetBriefInfo();
	$database->UTCheckBriefInfo();
}
else if(isset($_GET['operation']) and $_GET['operation'] == "sendpm")
{
	$fromserver = $_GET['thisserver'];
	$to = $_GET['to'];
	$from = $_GET['from'];
	$msg = $_GET['msg'];
	
	$fromserver = mysql_real_escape_string($fromserver);
	$to = mysql_real_escape_string($to);
	$from = mysql_real_escape_string($from);
	$msg = mysql_real_escape_string($msg);

	$database->UTSendPM($fromserver, $to, $from, $msg);
}
else if(isset($_GET['operation']) and $_GET['operation'] == "query")
{
	query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['server']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::sendmatchinfo:::".$_GET['thisserver'].":::".$_GET['username']."&");
}
else if(isset($_GET['operation']) and $_GET['operation'] == "queryspecial")
{
	if(strtolower($_GET['server']) == "servers")
	{
		query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['thisserver']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivematchinfo:::servers:::".$_GET['username'].":::".$database->UTRetrieveServerListString()."&");
	}
	else
	{
		$server = $database->UTRetrieveConnectToByName(mysql_real_escape_string($_GET['server']));
		if($server == "255.255.255.255")
		{
			query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['thisserver']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivematchinfo:::notfound:::".$_GET['username'].":::_:::_:::_&");
		}
		else
		{
			query_url("http://".$server."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::sendmatchinfo:::".$_GET['thisserver'].":::".$_GET['username']."&");
		}
	}
}
else if(isset($_GET['operation']) and $_GET['operation'] == "gotherespecial")
{
	if(strtolower($_GET['server']) == "servers")
	{
		query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['thisserver']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivematchinfo:::servers:::".$_GET['username'].":::".$database->UTRetrieveServerListString()."&");
	}
	else
	{
		$server = $database->UTRetrieveURLByName(mysql_real_escape_string($_GET['server']));
		if($server == "255.255.255.255")
		{
			query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['thisserver']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivematchinfo:::notfound:::".$_GET['username'].":::_:::_:::_&");
		}
		else
		{
			query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['thisserver']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivegothereinfo:::".$_GET['username'].":::".$server."&");
		}
	}
}
else if(isset($_GET['operation']) and $_GET['operation'] == "whoat")
{
	if(strtolower($_GET['server']) == "servers")
	{
		query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['thisserver']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivematchinfo:::servers:::".$_GET['username'].":::".$database->UTRetrieveServerListString()."&");
	}
	else
	{
		$server = $database->UTRetrieveConnectToByName(mysql_real_escape_string($_GET['server']));
		if($server == "255.255.255.255")
		{
			query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['thisserver']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivematchinfo:::notfound:::".$_GET['username'].":::_:::_:::_&");
		}
		else
		{
			query_url("http://".$server."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::sendwhoatinfo:::".$_GET['thisserver'].":::".$_GET['username']."&");
		}
	}
}
else if(isset($_GET['operation']) and $_GET['operation'] == "lookup")
{
	$fromserver = $_GET['thisserver'];
	$clientid = $_GET['clientid'];
	$scope = $_GET['scope'];
	$username = $_GET['username'];
	$segment = $_GET['segment'];
	
	$fromserver = mysql_real_escape_string($fromserver);
	$clientid = mysql_real_escape_string($clientid);
	$scope = mysql_real_escape_string($scope);
	$username = mysql_real_escape_string($username);
	
	if(strtolower($scope) == "local" && !$database->UTCheckLocalLookupAllowed($fromserver))
	{
		die();
	}
	else if(strtolower($scope) == "universal" && !$database->UTCheckUniversalLookupAllowed($fromserver))
	{
		die();
	}

	$database->UTPlayerLookup($fromserver, $scope, $clientid, $username, $segment);
}
else if(isset($_GET['operation']) and $_GET['operation'] == "returnquerydata")
{
	query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['returnto']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivematchinfo:::".$_GET['thisserver'].":::".$_GET['username'].":::".$_GET['map'].":::".$_GET['numplayers'].":::".$_GET['maxplayers']."&");
}
else if(isset($_GET['operation']) and $_GET['operation'] == "returnwhoatdata")
{
	query_url("http://".$database->UTRetrieveConnectToByURL(mysql_real_escape_string($_GET['returnto']))."/?universalunreal&".$database->urlSafeReplace(UNIVERSE_ID).":::".UNIVERSE_PASS.":::receivewhoatinfo:::".$_GET['thisserver'].":::".$_GET['username'].":::".$_GET['players']."&");
}

function query_url($url)
{
	$timeout = 2;

	if(QUERY_METHOD == 0)
	{
		$old = @ini_set('default_socket_timeout', $timeout);
		$file = @fopen($url, 'r');
		@ini_set('default_socket_timeout', $old);
		@stream_set_timeout($file, $timeout);
		@stream_set_blocking($file, 0);
	}
	else if(QUERY_METHOD == 1)
	{
		$curl = curl_init();
		curl_setopt ($curl, CURLOPT_URL, $url);
		curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
		curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
		$html = curl_exec ($curl);
		curl_close ($curl);
	}
}

function verify_server($server)
{
	global $database;

	if(strlen($database->UTRetrieveNameByURL($server)) > 0)
	{
		return true;
	}
	return false;
}

?>