<?php
if (!defined('MODULE_FILE')) {
	Header("Location: ../../index.php");
	exit;
}
$servers='193.19.83.83:27040,cs.ippw.ru:27031,server.tytrup.ru:27015'; //Сервера через запятую
$infopl='1'; //Вывод инфы об игроках 0-выкл 1-вкл

function main_cs () {
global $servers, $infopl;

$arrserv = explode(",",$servers);
$count = count($arrserv);
$bgr='1';
head();
title("Мониторинг Серверов CS 1.6");
OpenTable();
echo <<<HTML
<style>
#boundary{
font-family:Arial,Verdana,Sans-Serif;
text-align:left;
position:relative;
width:100%;
margin:1em 0;
}

#boundary_slide{
font-family:Arial,Verdana,Sans-Serif;
text-align:left;
position:relative;
width:100%;
margin:1em 0;
}


#keytable {
width: 100%;
border-collapse:collapse;
border:1px solid #FFCA5E;
}

#keythead th {
background: #ffcaad url(images/alfahead.png) left 1px repeat-x;
height: 26px;
color: #bb3000;
font-size: 11px;
font-weight: bold;
padding: 0px 4px;
margin: 20px 0px 0px;
text-align: center;
border: 1px solid #FFCA5E;
}


#keytbody tr {
background: url(images/bg_td100.jpg) repeat-x top;
}

#keytbody tr.odd {
background: #FFF8E8 url(images/bg_td200.jpg) repeat-x;
}

#keythtd {
font-size: 10px;
line-height: 1em;
font-family: Arial, Helvetica, sans-serif;
color: #777777;
padding: 5px 7px;
border-top: 1px solid #FFCA5E;
border-right: 1px solid #DDDDDD;
border-left: 1px solid #DDDDDD;
border-bottom: 1px solid #FFCA5E;
text-align: left;
}

#keytbody a {
color: #777777;
font-weight: bold;
text-decoration: underline;
}

#keytbody a:hover {
color: #F8A704;
text-decoration: underline;
}
</style>
HTML;

for ($i = 0; $i <= $count-1; $i++) {
$data='';
$players='';
$info='';
$contentpl='';
$server = explode(":",$arrserv[$i]);
$adress=$server[0];
$port=$server[1];
$data=monitor_server($adress, $port, 'halflife', 'info');
$player=monitor_server($adress, $port, 'halflife', 'players');
if ($data['server_os']=='w') $data['server_os']='Windows';
else if ($data['server_os']=='l') $data['server_os']='Linux';

if ($data['server_type']=='d') $data['server_type']='Dedicated';
else if ($data['server_type']=='l') $data['server_type']='Listen';
if ($data) {
/*
    echo "GameMod: ".$data['gamemod']."<br />";
		echo "Network Version:".$data['version']."<br />";
		echo "ServerType: ".$data['server_type']."<br />";
		echo "ServerOs: ".$data['server_os']."<br />";
		echo "ServerBots: ".$data['server_bots']."<br /><br />";
		*/
		if($bgr=='1') {$bgcolor='class="odd"';$bgr='0';} 
    else {$bgcolor='';$bgr='1';}
		$info .="<tr $bgcolor>
		<td id=\"keythtd\">".($i+1)."</td>
		<td id=\"keythtd\">".text_filter($data['hostname'])."</td>
		<td id=\"keythtd\">".intval($data['players'])." / ".intval($data['maxplayers'])."</td>
		<td id=\"keythtd\">".text_filter($data['mapname'])."</td>
		<td id=\"keythtd\">".text_filter($data['ip']).":".text_filter($data['port'])."</td>
		<td id=\"keythtd\">".text_filter($data['gamemod'])."</td>
		<td id=\"keythtd\">ver. ".text_filter($data['version'])."</td>
		<td id=\"keythtd\">".text_filter($data['server_type'])."</td>
		<td id=\"keythtd\">".text_filter($data['server_os'])."</td>
		</tr>";
		}
		if ($player && $infopl=='1') {
		
		$exmpl = array_keys($player);
		for ($b = 1; $b <= $data['players']; $b++) {
		if(in_array($b, $exmpl)) {
		if($bgr2=='1') {$bgcolor2='class="odd"';$bgr2='0';} 
    else {$bgcolor2='';$bgr2='1';}
		$players .="<tr $bgcolor2>
		<td id=\"keythtd\">".intval($player[$b]['id'])."</td>
		<td id=\"keythtd\">".text_filter($player[$b]['name'])."</td>
		<td id=\"keythtd\">".intval($player[$b]['score'])."</td>
		<td id=\"keythtd\">".text_filter($player[$b]['time'])."</td>";
		}
		}
$contentpl .='<center><font class=\"content\"><b>Информация об игроках на сервере</b></font></center><div id="boundary_slide"><table id="keytable" align="center">';
$contentpl .='<thead id="keythead"><tr><th>#</th><th>Name</th><th>Score</th><th>Time</th></tr></thead>';
$contentpl .='<tbody id="keytbody">'.$players.'</tbody>';
$contentpl .='</table></div>';
		}
echo '<center><font class=\"content\"><b>Информация о сервере</b></font></center><div id="boundary"><table id="keytable" align="center">';
echo '<thead id="keythead"><tr><th>#</th><th>Название сервера</th><th>Игроки</th><th>Карта</th><th>Адрес</th><th>Режим</th><th>Network</th><th>Тип Сервера</th><th>ОС</th></tr></thead>';
echo '<tbody id="keytbody">'.$info.'</tbody>';
echo '</table></div>';
echo "$contentpl";

		}
		
CloseTable();
foot();
}











function monitor_server($ip, $port, $game, $request)    {
		$fp = @fsockopen("udp://$ip", $port, $errno, $errstr, 1);
		if (!$fp) return FALSE;
		stream_set_timeout($fp, 1, 0);
		stream_set_blocking($fp, true);  
	 if (($request == "settings" || $request == "players")) {
	    $challenge_code = "\xFF\xFF\xFF\xFF\x57"; 
	    fwrite($fp, $challenge_code);
	    $buffer = fread($fp, 4096);
	    if (!trim($buffer)) { fclose($fp); return FALSE; }
		$challenge_code = substr($buffer, 5, 4);
    	}
		if ($request == "info") $challenge = "\xFF\xFF\xFF\xFFTSource Engine Query\x00";
		if ($request == "players") $challenge = "\xFF\xFF\xFF\xFFU".$challenge_code;
		if ($request == "settings") $challenge = "\xFF\xFF\xFF\xFFV".$challenge_code;
		fwrite($fp, $challenge);
		$buffer = fread($fp, 4096);
		if (!$buffer) { fclose($fp); return FALSE; }     
		if ($request == "settings")	{
		$second_packet = fread($fp, 4096);
		if (strlen($second_packet) > 0)	{
		$reverse_check = dechex(ord($buffer[8]));      
		if ($reverse_check[0] == "1")	{
		$tmp = $buffer;                 
		$buffer = $second_packet;
		$second_packet = $tmp;
		}
		$buffer = substr($buffer, 13);         
		$second_packet = substr($second_packet, 9);   
		$buffer = trim($buffer.$second_packet);
		 }
		 else	$buffer = trim(substr($buffer, 4));
		 }
		 else	$buffer = trim(substr($buffer, 4)); 
		 fclose($fp);
		 if (!trim($buffer)) return FALSE;
		 
		 
		 if ($request == "info") {
		 unset($data);
		$tmp = explode("\x00", $buffer);
		$place = strlen($tmp[0].$tmp[1].$tmp[2].$tmp[3].$tmp[4]) + 5;
		$data['gamemod'] = $tmp[3];
		$data['hostname'] = $tmp[1];
		$data['mapname'] = $tmp[2];
		$data['players'] = ord($buffer[$place]);
		$data['maxplayers'] = ord($buffer[$place + 1]);
		$data['password'] = ord($buffer[$place + 5]);
		//$data['cs_players']	= $cs_players;
		$data['ip'] = $ip;
		$data['port'] = $port; 
		$data['datatype'] = $buffer[0];               // m for steam info
		$data['version'] = ord($buffer[$place + 2]); // Network Version
		$data['description'] = $tmp[4];
		$data['server_type'] = $buffer[$place + 3];      // D edicated or L isten
		$data['server_os'] = $buffer[$place + 4];      // W indows or L inux
		$data['server_secure'] = ord($tmp[14]);            // VAC
		$exmpl = array_keys($tmp);
		if(in_array("15", $exmpl)) $data['server_bots'] = ord($tmp[15]);            // Number of Bots
		else $data['server_bots']=0;
		return $data;  
		}
		
		
		if ($request == "players") {
		$player_number = 0;
		$position = 2;
		do {
		$player_number++;
		$player[$player_number]['name']='';                                  
		$player[$player_number]['id'] = ord($buffer[$position]);
		$position ++;                                             
		while($buffer[$position] != "\x00" && $position < 4000) {
		$player[$player_number]['name'] .= $buffer[$position];  
		$position ++;
		}
		$player[$player_number]['score'] = (ord($buffer[$position + 1]))
		+ (ord($buffer[$position + 2]) * 256)
		+ (ord($buffer[$position + 3]) * 65536)
		+ (ord($buffer[$position + 4]) * 16777216);
		if ($player[$player_number]['score'] > 2147483648) $player[$player_number]['score'] -= 4294967296;
		$time = substr($buffer, $position + 5, 4);               
		if (strlen($time) < 4) return FALSE;              
		   list(,$time) = unpack("f", $time);                
		    $time = mktime(0, 0, $time);                          
		    $player[$player_number]['time'] = date("H:i:s", $time);  
		    $position += 9;
		    }
		    while ($position < strlen($buffer));                   
		    return $player;
		    }

		    if ($request == "settings") {
			$tmp     = substr($buffer, 2); 
			$rawdata = explode("\x00", $tmp);
		  for($i=1; $i<count($rawdata); $i=$i+2) {
		  $rawdata[$i] = strtolower($rawdata[$i]);  
		  $setting[$rawdata[$i]] = $rawdata[$i+1];  
		  }
		    return $setting; 
		   }
 }

 switch($op) {
  default:
	main_cs();
	break;
	}
?>