<?php
/* SOURCE ENGINE QUERY FUNCTION, requires the server ip:port */
function source_query($ip){
$cut = explode(":", $ip);
$HL2_address = $cut[0];
$HL2_port = $cut[1];
$HL2_command = "\377\377\377\377TSource Engine Query\0";
$HL2_socket = fsockopen("udp://".$HL2_address, $HL2_port, $errno, $errstr,3);
fwrite($HL2_socket, $HL2_command);
$JunkHead = fread($HL2_socket,4);
$CheckStatus = socket_get_status($HL2_socket);
if($CheckStatus["unread_bytes"] == 0)return 0;
$do = 1;
while($do){
$str = fread($HL2_socket,1);
$HL2_stats.= $str;
$status = socket_get_status($HL2_socket);
if($status["unread_bytes"] == 0){
$do = 0;
}
}
fclose($HL2_socket);
$x = 0;
while ($x <= strlen($HL2_stats)){
$x++;
$result.= substr($HL2_stats, $x, 1);
}
// ord ( string $string );
$result = str_split($result);
$info['network'] = ord($result[0]);$char = 1;
while(ord($result[$char]) != "%00"){$info['name'] .= $result[$char];$char++;}$char++;
while(ord($result[$char]) != "%00"){$info['map'] .= $result[$char];$char++;}$char++;
while(ord($result[$char]) != "%00"){$info['dir'] .= $result[$char];$char++;}$char++;
while(ord($result[$char]) != "%00"){$info['description'] .= $result[$char];$char++;}$char++;
$info['appid'] = ord($result[$char].$result[($char+1)]);$char += 2;
$info['players'] = ord($result[$char]);$char++;
$info['max'] = ord($result[$char]);$char++;
$info['bots'] = ord($result[$char]);$char++;
$info['dedicated'] = ord($result[$char]);$char++;
$info['os'] = chr(ord($result[$char]));$char++;
$info['password'] = ord($result[$char]);$char++;
$info['secure'] = ord($result[$char]);$char++;
while(ord($result[$char]) != "%00"){$info['version'] .= $result[$char];$char++;}
return $info;
}
$ip = $_GET['ip'];
$port = $_GET['port'];
$color = $_GET['color'];
if(strlen($color) == 6 )
{
$hex = sscanf($color, '%2x%2x%2x');
$r = $hex[0];
$g = $hex[1];
$b = $hex[2];
}
else
{
$hex[0] = 0;
$hex[1] = 0;
$hex[2] = 0;
}
$q = source_query($ip.':'.$port);
$servername = $q['name'];
$servermap = $q['map'];
$servermaxplayers = $q['max'];
$servertotalplayers = $q['players'];
$bg = 'bg3';
if($q['os'] == 'w')
{
$os = 'windows';
}
else
{
$os = 'linux';
}
switch ($q['appid'])
{
case 240:
$game = 'css';
break;
case 440:
$game = 'tf2';
break;
case 160:
$game = 'gmod';
break;
default:
$game = 'hl2';
break;
}
$oslogo = imagecreatefromgif('logo/'.$os.'.gif');
$gamelogo = imagecreatefromgif('logo/'.$game.'.gif');
$image = imagecreatetruecolor(400, 40);
$bg = imagecolorallocate($image, $r, $g, $b);
imagefilledrectangle($image, 0, 0, 399, 39, $bg);
if($q['password'] == '1')
{
$locklogo = imagecreatefromgif('logo/lock.gif');
imagecopymerge($image, $locklogo, 41, 21, 0, 0, 16, 16, 100);
imagedestroy($locklogo);
}
imagecopymerge($image, $oslogo, 4, 21, 0, 0, 16, 16, 100);
imagecopymerge($image, $gamelogo, 23, 21, 0, 0, 16, 16, 100);
imagedestroy($oslogo);
imagedestroy($gamelogo);
// Prepare Colours
//int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha )
$green = imagecolorallocate($image, 0, 255, 0);
$white = imagecolorallocate($image, 255, 255, 255);
// Prepare Font
$font = 'font/arial.ttf';
$bold = 'font/StencilStd.otf';
// Prepare Dimension Variables
$height = imagesy($image);
$width = imagesx($image);
$text_area_height = 0;
imagettftext($image, 10, 0, 5, 16, $white, $font, $servertotalplayers.'/'.$servermaxplayers.' - '.$servername);
imagettftext($image, 10, 0, 60, 34, $green, $font, 'IP: '.$ip.':'.$port.' Map: '.$servermap);
// array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
// Send image to browser as PNG
header("Content-type: image/png");
imagepng($image);
?>