<?php
//define aettings
//this is only if you want that the users can enter ip+host by their self...
//ip of the server
if(empty($_GET['host'])){
//default server
$host = '213.230.209.202';
}else{
$host = $_GET['host'];
}
if(empty($_GET['port'])){
//querypoort of the server. always gameserver port + 1
$port = 7778;
}else{
$port = $_GET['port'];
}
//backgroud color
$background = 'white';
//no edit afher
function getInfo($host,$port) {
  if(is_int($port)){
    $sock = @fsockopen("udp://" . $host, $port, $errno, $errstr,4);
    //Check if we have a socket open, if not, display error message
  if (!$sock) {
  return '';
  }else
  {
  fputs($sock,"\\status\\\player_property\Health\\\game_property\ElapsedTime\\\game_property\RemainingTime\\");

  $gotfinal = False;
  $data = "";

  //Set starttime, for possible loop expiration, so the server doesn't get too much work.
  $starttime = Time();

  //Loop until final packet has been received.
 while(!($gotfinal == True || feof($sock))) {

  //Get data
  if(($buf = fgetc($sock)) == FALSE) {
  usleep(100); // wait for additional data? :S whatever
  }

  //Add to databuffer
  $data .= $buf;

  //Check if final item (queryid) has been received
  if (strpos($data,"final\\") != False) {
  $gotfinal = True;
  }

  //Protect webserver against massive loop.
  if ((Time() - $starttime) > 5) {
  echo "Data receiving took too long. Cancelled.";
  $gotfinal = True;
  }
  }
  //Close socket
  fclose ($sock);
  //Split chunks by \
     }
        return $data;
  }else{
  return '';
  }
}

function parseInfo($info) {
	$retval = array();
	if ($info != '') {
		$key = strtok($info, '\\');
		while ($key) {
			$val = strtok('\\');
			if ($val !== false){
			if(empty($val)){
                        $retval[$key] = "0";
                        }else{
                        $retval[$key] = $val;
                        }
			}
			else
                        {
                        break;
                        }
			$key = strtok('\\');
		}
	}
	return $retval;
}
function get_minutes($seconds){
//ut return remaining/espalated time in seconds (eg 1159) we want to read it as 19:59
	$timemins = intval($seconds / 60);
//% means modulo
	$timesecs = ($seconds % 60);

	$Reqlength = 2; //Amount of digits we need
//make the text 2 digets
	if ($Reqlength-strlen($timemins) > 0) $timemins = str_repeat("0",($Reqlength-strlen($timemins))) . $timemins;
	if ($Reqlength-strlen($timesecs) > 0) $timesecs = str_repeat("0",($Reqlength-strlen($timesecs))) . $timesecs;
	return $timemins . ":" . $timesecs;
}
//game tupe in ut wll often return as Teamgameplus. To create a better/understand  format for us
function get_true_gamename($game){
switch ($game){
//dm
case 'DeathMatchPlus':
$out = 'Death Match';
break;
//tdm
case 'TeamGamePlus':
$out = 'Team Death Match';
break;
//ctf
case 'CTFGame':
$out = 'Capture The Flag';
break;
//dom
case 'Domination':
$out = 'Domination';
break;
//as
case 'Assault':
$out = 'Assault';
break;
//LMS
case 'LastManStanding':
$out = 'Last Man Standing';
break;
default:
$out = $game;
break;
}
return $out;
}
function team($teamid,$teamids){
//team red == 0
//tram blue == 1
//team green == 2
//team yellow == 3
//spectator/none color == 255 == background collor
   $colors = array("red","blue","green","yellow");
   if ($teamid < 4)
	{
	$teamids = $colors[$teamid];
	}
return $teamids;
}
//parse info and recive the info
$stats = parseInfo(getInfo($host,$port));
//show image in the right corner
echo '<div style="float:right;">';
if(file_exists('images/'.$stats['mapname'].'.jpg')){
echo '<img src="images/'.$stats['mapname'].'.jpg" alt="'.$stats['mapname'].'" title="'.$stats['mapname'].'"/>';

}else{
echo '<img src="images/none.gif" alt="'.$stats['mapname'].'" title="'.$stats['mapname'].'"/>';
}
echo '</div>';
echo '<strong>Serverinfo</strong><br />';
echo 'Servername: '.$stats['hostname'].'<br />';
echo 'Gameserver version: '.$stats['gamever'].'<br />';
echo 'Required version: '.$stats['minnetver'].'<br />';
echo '<strong>Settings</strong><br />';
echo 'Gametype: '.get_true_gamename($stats['gametype']).'<br />';
echo 'Mapname: '.$stats['mapname'].'<br />';
echo 'Maptitle: '.$stats['maptitle'].'<br />';
echo 'Players: '.$stats['numplayers'].'/'.$stats['maxplayers'].'<br />';
echo 'Mutators: '.$stats['mutators'].'<br />';
echo 'Timelimit: '.$stats['timelimit'].' minutes<br />';
echo 'Fraglimit: '.$stats['goalteamscore'].'<br />';
echo 'Friendly Fire: '.$stats['friendlyfire'].'<br />';
echo 'Tournament: '.$stats['tournament'].'<br />';
echo 'Gamestyle: '.$stats['gamestyle'].'<br />';
echo 'Elapsed Time: '.get_minutes($stats['ElapsedTime']).'<br />';
echo 'Remaining Time: '.get_minutes($stats['RemainingTime']).'<br />';
echo '<strong>Playerlist:</strong>';
//echo table
echo '<table>
<tr><td>Player name:</td><td>Frags</td><td>Ping</td><tr>';
for($i = 0;!empty($stats['player_'.$i]);$i++){

echo '<tr style="background-color:'.team($stats['team_'.$i],$background).'"><td>'.$stats['player_'.$i].'</td><td>'.$stats['frags_'.$i].'</td><td>'.$stats['ping_'.$i].'</td></tr>';
}
echo '</table>';
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="get">
IP: <input type="text" name="host"/><br />
Queryport (Gameport + 1): <input type="text" name="port"/><br />
<input type="submit" value="query server"/>
</form>';
?>