<?php
/*
0.1 >> 0.2
FF  >>  00
255 >>   0

0.2-0.1
*/

//Grün: #00FF00
//Gelb: #FFFF00
//Rot: #FF0000

/*

WENN ping < 50
DANN echo 'grün';
SONST
	WENN ping > 200
	DANN echo 'rot';
	SONST
		WENN ping >=50 UND < 100
		DANN echo dechex(((ping-50*2)/100)*255) . "FF00";
		SONST
			WENN ping >= 100
			DANN echo "FF" . 255-((ping-100)/100*255)
			DANN echo "FF" . dechex(255-(ping/100)*255) . "00";

50ms: #00FF00
100ms: #FFFF00
200ms: #FF0000

if(!$ping) echo "<font color=\"grey\"><i><b>No Connection</b></i></font>";
		else if($ping < 0.1) echo "<font color=\"green\"><b>".round($ping, 3) * 1000 ."ms</b></font>";
		else if($ping < 0.15) echo "<font color=\"orange\"><b>".round($ping, 3) * 1000 ."ms</b></font>";
		else echo "<font color=\"red\"><b>".round($ping, 3) * 1000 ."ms</b></font>";break;
*/
if(isset($_GET["ip"]) && isset($_GET["action"])){
$ip = $_GET["ip"];// $action = $_GET["action"];
function port($ziel,$port){ if(fsockopen("tcp://".$ziel,$port,$errno,$errstr,"1"))return true; else return false; }
$on = "<font color=\"green\">Online</font>"; $off = "<font color=\"red\">Offline</font>";
switch ($action = $_GET["action"]) {
	case 0:
		function ping($host) {
			$package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67";
			$socket = socket_create(AF_INET, SOCK_RAW, 1);
			socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0));
			socket_connect($socket, $host, null);
			list($start_usec, $start_sec) = explode(" ", microtime());
			$start_time = ((float) $start_usec + (float) $start_sec);
			socket_send($socket, $package, strlen($package), 0);
			if(@socket_read($socket, 255)) {
			list($end_usec, $end_sec) = explode(" ", microtime());
			$end_time = ((float) $end_usec + (float) $end_sec);
			$total_time = $end_time - $start_time;
			return $total_time;
		} else { return false; } socket_close($socket); }
		$ping = @ping($ip);
		//$ping = 0.04;
		$pingstring = "\"><b>".round($ping, 3) * 1000 ."ms</b></font>";
		echo "<font color=\"";
		$ausgabe = "";
		if(!$ping) echo "grey\"><i><b>No Connection</b></i></font>";
		else if($ping < 0.05) 	$ausgabe = "#00FF00$pingstring";
		else if($ping > 0.2) 	$ausgabe = "#FF0000$pingstring";
		else if($ping >= 0.05 && $ping < 0.1){
			$ausgabe = dechex((($ping - 0.05)*20)*255) . "FF00$pingstring";
			if(((($ping - 0.05)*20)*255) <= 15) $ausgabe = "#0" . $ausgabe;
			else $ausgabe = "#" . $ausgabe;
			}
		else {
			$ausgabe = dechex((0.2-$ping)*2550) . "0$pingstring";
			if(((0.2-$ping)*2550) <= 15) $ausgabe = "#FF0" . $ausgabe;
			else $ausgabe =  $ausgabe = "#FF" . $ausgabe;		
			}
		echo $ausgabe;
		
		break;
	case 1: //Apache
		if(@port($ip, 80))echo $on;else echo $off;break;
	case 2: //Login Server
		if(@port($ip, 9010))echo $on;else echo $off;break;
	case 3: //World Server
		if(@port($ip, 9110))echo $on;else echo $off;break;
	case 4: //Zone 1
		if(@port($ip, 9120))echo $on;else echo $off;break;
	case 5: //Zone 2
		if(@port($ip, 9122))echo $on;else echo $off;break;
	case 6: //Zone 3
		if(@port($ip, 9124))echo $on;else echo $off;break;			
	case 7: //FTP
		if(@port($ip, 21))echo $on;else echo $off;break;
	case 8: //How many people are playing?
		include 'settings.php';
		if(@mssql_connect( $servername . "\\" . $dbname , $dbuser, $dbpass ) != false) $online = mssql_fetch_row(mssql_query("SELECT TOP 1 nCount FROM StatisticsData..tSameTimeChar ORDER BY nIndex DESC"));
		if($online[0] > -1)echo $online[0]; else echo 0; break;
}} ?>