<?php
//header('Content-Type: text/html; charset=windows-1251');
header('Content-type: text/html; charset=utf-8');

// Настройки
$ROOT = 'http://bf.mxc.ru/';
$DOMAIN = 'http://bf.mxc.ru/';
$GAMESERVER = '88.151.184.23'; // Для монитора
$GAMEPORT = '29900'; // Для монитора

// Порт для мониторинга сервера!
$port = $GAMEPORT; // $port = '29900';
$port1 = '29901';
$port2 = '29902';
$port3 = '29903';
$port4 = '29904';
$port5 = '29905';
$port6 = '29906';

// IP Адрес сервера!
$server = $GAMESERVER;
//$server1 = '88.151.184.23'; // - не проверялась такая функция!
//$server2 = 'bf2.sibnet.ru';
//$server3 = '';
//$server4 = '';
//$server5 = '';
//$server6 = '';

// Обновления странитцы
$refreshrate = '600';

//
//$statsonline = $lang_statsonline;
//$statsoffline = $lang_statsoffline;

function monitor ($server, $port) {
GLOBAL $refreshrate, $GAMESERVER, $GAMEPORT, $ROOT;

//URL to Images folder. No trailing slash.
$images = $ROOT;

//Border Color of the Stats
$bordercolor = '#000000';

//Body Color of inside the stats table
$bodycolor = '#5C5F52';

//Top and Bottom Cell Colors
$topbottomcell = '#383C33';

//Page Background Color
$backgroundcolor = '#3C3A36';

//Page Refresh Rate in Seconds
$refreshrate = '600';

//Font Color
$fontcolor = '#FFFFFF';

//Number of Ping to be know as a Lagger, Anything above this number will be marked
$lagger = '120';

//Insert Image URL if using your own Logo in the stats, or leave blank if not. Do not make your image wider than 800
//pixels or it will make the table look funny :)
$titlepic = '';

//If Using your own logo, specify alignment (left, center, right)
$align = 'left';

//If Using your own logo, specify color behind your Logo
$titlepicbg = '#BAB3A1';

//Colors for Laggers, Admin, and Teamkiller
$laggercolor = '#999999';
$admincolor = '#CC6600';
$teamkillercolor = '#E4FF4D';

//Colors for Player Count if Server is Full or if Server has an Open Slot
$servernotfullcolor = 'yellow';
$serverfullcolor = '#FB0A04';

//Message to display when server times out or is down
$timeoutmessage = 'Server is currently down or changing maps';

//online\offline
$online = '<img src='.$images.'/site-images/online.png />';
$offline = '<img src='.$images.'/site-images/offline.png />';

if (!$_GET['ip']) {
	$server=$server;
} else {
	$server=$_GET['ip'];
}

if (!$_GET['port']) {
	$port=$port;
} else {
	$port=$_GET['port'];
}

$sock = @fsockopen("udp://".$server, $port);
@socket_set_timeout($sock, 0, 800000);

$queryString = "\xFE\xFD\x00\x10\x20\x30\x40\xFF\xFF\xFF\x01";
@fwrite($sock, $queryString);

//сервер в сети!
$end=false;
$i=1;
while(!$end) {
	$bytes = @fread($sock, 1);
	$status = @socket_get_status($sock);
	$length = $status['unread_bytes'];

	if($length>0) {
		$Info[$i] = $bytes.fread($sock, $length);

		preg_match("/splitnum(...)/is",$Info[$i],$regs);
		$String = $regs[1];

		$num = ord(substr($String,1,1));

		if($num == 128 || $num == 0) {
			$Display[1] = $Info[$i];
		}

		if ($num == 129 || $num == 1) {
			$Display[2] = $Info[$i];
		}

		if ($num == 130) {
			$Display[3] = $Info[$i];
		}
	}

	if($length==0) {
		$end=true;
	}
	$i++;
}
//сервер в сети!

@fclose($sock);

  $Info = $Display[1].$Display[2].$Display[3];
if(!$Info) {

echo("<br>$offline $timeoutmessage<br>");

}
else {
$output = str_replace("\\","",$Info);
$changeChr = chr(0);
$output = str_replace($changeChr, "\\", $output);
//print $output;
$rules = "x".substr($output,0,strpos($output,"\\\\".chr(1)));
$players = "\\".substr($output,strpos($output,"\\\\".chr(1))+3);

$p3 = strpos($players,"\\\\".chr(2));

if(!$p3) {
	$p3 = strpos($players,"\\\\team_t");
}
if(!$p3) {
	$p3 = strpos($players,"\Ѓteam_t");
}

$players = substr($players,0,$p3);

$players = str_replace("\\ 0@splitnum\Ѓ","",$players);
$players = str_replace("\\ 0@splitnum\\Ѓ","",$players);
                        //\\ 0@splitnum\Ѓteam_t
$players = str_replace(" 0@splitnum\\","",$players);
$players = str_replace(" 0@splitnum\\‚","",$players);

//Parse Rules
$rule_temp = substr($rules,1);
$rule_temp = str_replace("Ђ","\\",$rule_temp);
$rules_arr = explode("\\",$rule_temp);
$rules_count = count($rules_arr);


for($i=0;$i<$rules_count/2;$i++) {

	$r1[$i] = $rules_arr[$i*2];
	$r2[$i] = $rules_arr[($i*2)+1];
	$rule[$r1[$i]] = $r2[$i];

}

$tags = explode("\\",$players);

for ($p = 0; $p < $rule[numplayers]; $p ++) {

		$data[] = array('name' => $player["player_"][$p], 'score' => $player["score_"][$p], 'deaths' => $player["deaths_"][$p], 'ping' => $player["ping_"][$p], 'team' => $player["team_"][$p], 'kills' => $player["skill_"][$p], 'pid' => $player["pid_"][$p]);
}

if($rule[numplayers]==$rule[maxplayers]) {

	$currentplayers = "<font color=".$serverfullcolor." class=nc>".$rule[numplayers]."</font>";
	$maxplayers = "<font color=".$serverfullcolor." class=nc>".$rule[maxplayers]."</font>";

} elseif ($rule[numplayers] > 0 && $rule[numplayers]!=$rule[maxplayers]) {

	$currentplayers = "<font color=".$servernotfullcolor." class=nc>".$rule[numplayers]."</font>";
	$maxplayers = "<font color=".$servernotfullcolor." class=nc>".$rule[maxplayers]."</font>";

} else {

	$currentplayers = "<font class=ab>".$rule[numplayers]."</font>";
	$maxplayers = "<font class=ab>".$rule[maxplayers]."</font>";

}

echo("<br>$online $rule[hostname] <br> <b>$rule[mapname]</b> - <b>$currentplayers</b><b>/</b><b>$maxplayers</b><br>");


if (!$rule[numplayers]){
}

} //else

} //function

//echo("<br/><b>BATTLEFIELD 2 Vehicles</b>");
monitor($server, $port);
monitor($server, $port1);
//echo("<br/><b>BATTLEFIELD 2 Infantry</b>");
monitor($server, $port2);
monitor($server, $port3);
//echo("<br/><b>BATTLEFIELD 2 Special Forces Vehicles</b>");
monitor($server, $port4);
monitor($server, $port5);
//echo("<br/><b>BATTLEFIELD 2 Special Forces Infantry</b>");
monitor($server, $port6);
monitor($server, $port7);

?>