<?php

include_once ("functions.php");

// In diesem Bereich sind die einzigsten Änderungen die am Script vorgenommen werden müssen
// ****************************************************************************************

// Serverparameter:

$server_ip = "78.143.44.223";	// Server IP
$server_port = "28960"; 		// Query Port

// ****************************************************************************************
// Ab hier bitte nichts mehr ändern, außer man weiß was man tut und will das Script an das eigene Design anpassen:

// Verbindungsaufbau
$server_ip = "udp://" . $server_ip;
$serverip = substr($server_ip, 6);
$connect = fsockopen($server_ip, $server_port, $errno, $errstr, 30);

if ($connect) {

  socket_set_timeout ($connect, 1, 000000);
  $send = "ÿÿÿÿgetstatus\n";
  $debug1 = fputs($connect, $send);
  $debug2 = fwrite ($connect, $send);
  $output = fread ($connect, 1);
  
  if (! empty ($output)) {
  
    do {
	
      $status_pre = socket_get_status ($connect);
      $output = $output . fread ($connect, 1);
      $status_post = socket_get_status ($connect);
    } while ($status_pre['unread_bytes'] != $status_post['unread_bytes']);
  };
  
  // Verbindung schliessen
  fclose($connect);

  // Verarbeite Daten
  $output = explode ("\\", $output);
  if (in_array("sv_maxclients", $output)) {$max_index = array_search ("sv_maxclients", $output);$max_clients = $output[$max_index+1];}
  if (in_array("sv_privateClients", $output)) {$max_index = array_search ("sv_privateClients", $output);$privat = $output[$max_index+1];}
  if (in_array("mapname", $output)) {$max_index = array_search ("mapname", $output);$mapname = $output[$max_index+1];}
  if (in_array("sv_hostname", $output)) {$max_index = array_search ("sv_hostname", $output);$hostname = $output[$max_index+1];}
  if (in_array("g_gametype", $output)) {$max_index = array_search ("g_gametype", $output);$gametype = $output[$max_index+1];}
  if (in_array("sv_punkbuster", $output)) {$max_index = array_search ("sv_punkbuster", $output);$punkbuster = $output[$max_index+1];}
  if (in_array("sv_pure", $output)) {$max_index = array_search ("sv_pure", $output);$pure = $output[$max_index+1];}
  if (in_array("sv_voice", $output)) {$max_index = array_search ("sv_voice", $output);$voice = $output[$max_index+1];}
  if (in_array("g_antilag", $output)) {$max_index = array_search ("g_antilag", $output);$alag = $output[$max_index+1];}
  if (in_array("pswrd", $output)) {$max_index = array_search ("pswrd", $output);$passwort = $output[$max_index+1];}
  if (in_array("_Location", $output)) { $max_index = array_search ("_Location", $output);$Standort = $output[$max_index+1];}
  if (in_array("_Admin", $output)) { $max_index = array_search ("_Admin", $output);$admin = $output[$max_index+1];}
  if (in_array("_Website", $output)) { $max_index = array_search ("_Website", $output);$Web = $output[$max_index+1];}
  if (in_array("_Host", $output)) { $max_index = array_search ("_Host", $output);$Host = $output[$max_index+1];}
  if (in_array("_Email", $output)) { $max_index = array_search ("_Email", $output);$EMail = $output[$max_index+1];}
  if (in_array("_IRC", $output)) { $max_index = array_search ("_IRC", $output);$IRC = $output[$max_index+1]; }
  if (in_array("_ICQ", $output)) { $max_index = array_search ("_ICQ", $output);$ICQ = $output[$max_index+1]; }
  if (in_array("gamename", $output)) {$max_index = array_search ("gamename", $output);$gamename = $output[$max_index+1];}
  if (in_array("shortversion", $output)) {$max_index = array_search ("shortversion", $output);$Version = $output[$max_index+1];}
  if (in_array("protocol", $output)) {$max_index = array_search ("protocol", $output);$protokol = $output[$max_index+1];}
  if (in_array("sv_maxPing", $output)) {$max_index = array_search ("sv_maxPing", $output);$maxPing = $output[$max_index+1];}
  if (in_array("sv_minPing", $output)) {$max_index = array_search ("sv_minPing", $output);$minPing = $output[$max_index+1];}
  if (in_array("_Mod", $output)) { $max_index = array_search ("_Mod", $output); $Mod = $output[$max_index+1];} else $Mod = "---";
  if (in_array("fs_game", $output)) {$max_index = array_search ("fs_game", $output);$fsgame = $output[$max_index+1];} else $fsgame = "---";

  // Gamename Short
  if ($gamename == "Call of Duty") $gamename_short = "CoD";
  if ($gamename == "CoD:United Offensive") $gamename_short = "CoD UO";
  if ($gamename == "Call of Duty 2") $gamename_short = "CoD 2";
  if ($gamename == "Call of Duty 4") $gamename_short = "CoD 4 MW";  
  if ($gamename == "Call of Duty: World at War") $gamename_short = "CoD WW";

  // Ermittlung der Version CoD WW
  if ($protokol == 94) $Version_codww = "1.1";
  else if ($protokol == 95) $Version_codww = "1.2";
  else if ($protokol == 96) $Version_codww = "1.3";
  else if ($protokol == 97) $Version_codww = "1.4";
  else if ($protokol == 99) $Version_codww = "1.5";
  else if ($protokol == 100) $Version_codww = "1.6";
  else if ($protokol == 101) $Version_codww = "1.7";

  // Gametypes
  if ($gametype == "re") {$gametype = "Retrieval";$gametype_short = "RE";}
  if ($gametype == "dm") {$gametype = "Deathmatch";$gametype_short = "DM";}
  if ($gametype == "sd") {$gametype = "Suchen und Zerstören";$gametype_short = "SD";} 
  if ($gametype == "tdm") {$gametype = "Team Deathmatch";$gametype_short = "TDM";}
  if ($gametype == "war") {$gametype = "Team Deathmatch";$gametype_short = "TDM";}
  if ($gametype == "bel") {$gametype = "Hinter feindl. Linien";$gametype_short = "BEL";}
  if ($gametype == "hq") {$gametype = "Hauptquartier";$gametype_short = "HQ";}
  if ($gametype == "dom") {$gametype = "Domination";$gametype_short = "DOM";}
  if ($gametype == "ctf") {$gametype = "Capture the Flag";$gametype_short = "CTF";}
  if ($gametype == "bas") {$gametype = "Base Assault";$gametype_short = "BAS";}
  if ($gametype == "koth") {$gametype = "Hauptquartier";$gametype_short = "KOTH";}
  if ($gametype == "sab") {$gametype = "Sabotage";$gametype_short = "SAB";}
  if ($gametype == "twar") {$gametype = "Krieg";$gametype_short = "TWAR";}
  if ($gametype == "ctfb") {$gametype = "Capture the Flag back";$gametype_short = "CTFB";}
  if ($gametype == "ch") {$gametype = "Capture and hold";$gametype_short = "CH";}
  if ($gametype == "htf") {$gametype = "Hold the Flag";$gametype_short = "HTF";}
  if ($gametype == "vip") {$gametype = "V.I.P.";$gametype_short = "VIP";}
  if ($gametype == "wrz") {$gametype = "Warzone";$gametype_short = "WRZ";}
  if ($gametype == "vid") {$gametype = "Very important Droid";$gametype_short = "VID";}
  
  // Erweiterte PB Abfrage
  $pb_sig = "livestats/bilder/pb/$serverip-$server_port.png";

  if (file_exists($pb_sig)) {

	$pb_stream = 1;
  } 

  else {

	$pb_stream = 0;
  }

  if ($punkbuster == 1 && $pb_stream == 0) $pb = "aktiv";
  else if ($punkbuster == 1 && $pb_stream == 1) $pb = "aktiv & streaming";
  else if ($punkbuster == 0) $pb = "inaktiv";
    
  $last_value = count($output) - 1;
  $players_string = $output[$last_value];
  $players_string = explode("\n", $players_string);
  
  for ($i = 1; $i != count($players_string); $i++) {
  
	$split = explode(" ", $players_string[$i], 3);
	$playerlist[$i-1]['score'] = $split[0];
	$playerlist[$i-1]['ping'] = $split[1];
	$playerlist[$i-1]['player'] = $split[2];

  }
	  
  @usort($playerlist, "cmp");
	$players = count($playerlist) - 1;

  // Maps für CoD
  if ($gamename_short == "CoD") {
  
	include_once ("livestats/maps_cod.php");
	
	$mappic_folder = "cod";
	$GameIcon_small = "cod_icon.gif";
	$GameIcon_big = "cod_font.png";
  
	if ($Maps[$mapname] || "") {
  
	  $mapnamelong = $Maps[$mapname];
	}
  
	if ($mapnamelong == "") {
  
	  $mapnamelong = $mapname;
	}
  }
  
  // Maps für CoD UO
  else if ($gamename_short == "CoD UO") {
  
	include_once ("livestats/maps_coduo.php");
	
	$mappic_folder = "coduo";
	$GameIcon_small = "coduo_icon.gif";
	$GameIcon_big = "coduo_font.png";
  
	if ($Maps[$mapname] || "") {
  
	  $mapnamelong = $Maps[$mapname];
	}
  
	if ($mapnamelong == "") {
  
	  $mapnamelong = $mapname;
	}
  }
  
  // Maps für CoD 2
  else if ($gamename_short == "CoD 2") {
  
	include_once ("livestats/maps_cod2.php");
	
	$mappic_folder = "cod2";
	$GameIcon_small = "cod2_icon.gif";
	$GameIcon_big = "cod2_font.png";
  
	if ($Maps[$mapname] || "") {
  
	  $mapnamelong = $Maps[$mapname];
	}
  
	if ($mapnamelong == "") {
  
	  $mapnamelong = $mapname;
	}
  }
  
  // Maps für CoD 4 MW
  else if ($gamename_short == "CoD 4 MW") {
  
	include_once ("livestats/maps_cod4.php");
	
	$mappic_folder = "cod4";
	$GameIcon_small = "cod4_icon.gif";
	$GameIcon_big = "cod4_font.png";
  
	if ($Maps[$mapname] || "") {
  
	  $mapnamelong = $Maps[$mapname];
	}
  
	if ($mapnamelong == "") {
  
	  $mapnamelong = $mapname;
	}
  }
  
  // Maps für CoD WW
  else if ($gamename_short == "CoD WW") {
  
	include_once ("livestats/maps_codww.php");
	
	$mappic_folder = "codww";
	$GameIcon_small = "codww_icon.gif";
	$GameIcon_big = "codww_font.png";
  
	if ($Maps[$mapname] || "") {
  
	  $mapnamelong = $Maps[$mapname];
	}
  
	if ($mapnamelong == "") {
  
	  $mapnamelong = $mapname;
	}
  }
  
  // Ist ein Mappic vorhanden?
  $mappic = "livestats/bilder/small/$mappic_folder/$mapname.jpg";

  if (file_exists($mappic)) {

	$mappic_small = $mapname.".jpg";
  } 

  // Sonst nehme unknown.jpg
  else {

	$mappic_small = "unknown.jpg";
  }
  
  $img_offline = "livestats/bilder/general/offline.png";
}

// Server online
if ($max_clients){
?>
<table border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
      <td valign="top">
      <table border="0" cellspacing="0" cellpadding="0">
       <tr>
        <td class="backtds">
	     <table border="0" cellspacing="0" cellpadding="0">
          <tr>
           <td ><strong>Server:</strong><br/>
                <div class="sBoxAbs"><?=farben($hostname)?></div>
           </td>
          </tr>
          <?php if ($gamename_short != "CoD WW"){ ?>
		  <tr>
           <td ><strong>Game:</strong><br/>&nbsp;
           <img style="vertical-align: middle;" src="livestats/bilder/general/<?php echo $GameIcon_small ?>" width="16" height="16">  <?php echo $gamename_short ?> (v <?php echo $Version?>)</td>
          </tr>
		  <?php }else{ ?>
          <tr>
           <td ><strong>Game:</strong><br/>&nbsp;
           <img style="vertical-align: middle;" src="livestats/bilder/general/<?php echo $GameIcon_small ?>" width="16" height="16"> <?php echo $gamename_short ?> (v <?php echo $Version_codww ?>)</td>
          </tr>
		  <?php }?>
          <tr>
           <td ><strong>IP Adresse:</strong><br/>&nbsp;
		   <a href="hlsw://<?=$serverip?>:<?=$server_port?>"><?=$serverip?>:<?=$server_port?></a>
          </tr>
          <tr>
           <td ><strong>Mod:</strong><br/>&nbsp;
		   <?=farben($Mod)?></a>
          </tr>
          <tr>
           <td ><strong>Player:</strong><br/>&nbsp;
           <?=$players?> / <?=$max_clients - $privat?> (+<?=$privat?>)</td>
          </tr>
          <tr>
           <td ><strong>Gametype:</strong><br/>&nbsp;
           <?=$gametype?></td>
          </tr>
          <tr>
           <td ><strong>Punkbuster:</strong><br/>&nbsp;
           <?=$pb?></td>
          </tr>
          <tr>
            <td><strong>Serverinfo:<br/>&nbsp;
            <a href="index.php?site=server">Hier klicken</a>
          </td>
       </tr>
        </table>
	</td>
       </tr>
	   	   <tr>
         <td height="10">&nbsp;</td>
       </tr>
       <tr>
        <td class="backtds"><img src="livestats/bilder/small/<?php echo $mappic_folder?>/<?php echo $mappic_small ?>" width="130" height="90"><br></td>
       </tr>
       <tr>
	    <td ><strong>Map:</strong><br/>
		  <div class="sBoxAbs"><?=$mapnamelong?></div>
		</td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
<?
}
// Server offline
else {
?>
<table border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
      <td valign="top">
      <table border="0" cellspacing="0" cellpadding="0">
       <tr>
        <td class="backtds">
	     <table border="0" cellspacing="0" cellpadding="0">
          <tr>
           <td ><strong>Server:</strong><br/>&nbsp;
            <font color="#ff0000">Server ist Offline</font>
           </td>
          </tr>
          <tr>
           <td ><strong>Game:</strong><br/>&nbsp;
           ---</td>
          </tr>
          <tr>
           <td ><strong>IP Adresse:</strong><br/>&nbsp;
		   <a href="hlsw://<?=$serverip?>:<?=$server_port?>"><?=$serverip?>:<?=$server_port?></a>
          </tr>
          <tr>
           <td ><strong>Mod:</strong><br/>&nbsp;
		   ---</a>
          </tr>
          <tr>
           <td ><strong>Player:</strong><br/>&nbsp;
           xx / xx (+x)</td>
          </tr>
          <tr>
           <td ><strong>Gametype:</strong><br/>&nbsp;
           ---</td>
          </tr>
          <tr>
           <td ><strong>Punkbuster:</strong><br/>&nbsp;
           <?=$pb?></td>
          </tr>
          <tr>
            <td><strong>Serverinfo:<br/>&nbsp;
            <a href="index.php?site=server">Hier klicken</a>
          </td>
       </tr>
        </table>
	</td>
       </tr>
	   	   <tr>
         <td height="10">&nbsp;</td>
       </tr>
       <tr>
        <td <img src="<?=$img_offline?>" width="130" height="90" border="0"><br></td>
       </tr>
       <tr>
	    <td ><strong>Map:</strong><br/>&nbsp;
		  ---
		</td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
<?
}
?>