ServerStatus/hidden/csgo_query.php

113 lines
2.6 KiB
PHP

<?php
// error_reporting(0);
/* SOURCE ENGINE QUERY FUNCTION, requires the server ip:port */
class ByteBuffer
{
public $data = array(), $pointer;
function __construct($data)
{
$this->data = str_split($data);
$this->pointer = 0;
}
public function ReadByte()
{
return ord($this->data[$this->pointer++]);
}
public function ReadShort()
{
return (ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8));
}
public function ReadInt()
{
return (ord($this->data[$this->pointer++]) | (ord($this->data[$this->pointer++])<<8) | (ord($this->data[$this->pointer++])<<16) | (ord($this->data[$this->pointer++])<<24));
}
public function ReadChar()
{
return $this->data[$this->pointer++];
}
public function ReadString()
{
$string = '';
while ($this->data[$this->pointer] != "\0")
{
$string .= $this->data[$this->pointer];
$this->pointer++;
}
$this->pointer++;
return $string;
}
}
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);
}
$result = urlencode($result); // the output
return $result;
}
/* FORMAT SOURCE ENGINE QUERY (assumes the query's results were urlencode()'ed!) */
function format_source_query($string)
{
$string = str_replace('%07','',$string);
$string = str_replace("%00","|||",$string);
$sinfo = urldecode($string);
$sinfo = explode('|||',$sinfo);
$info['hostname'] = $sinfo[0];
$info['map'] = $sinfo[1];
$info['game'] = $sinfo[2];
if ($info['game'] == 'garrysmod') { $info['game'] = "Garry's Mod"; }
elseif ($info['game'] == 'cstrike') { $info['game'] = "Counter-Strike: Source"; }
elseif ($info['game'] == 'dod') { $info['game'] = "Day of Defeat: Source"; }
$info['gamemode'] = $sinfo[3];
$buffer = new ByteBuffer($string);
$info['players'] = $buffer->ReadByte();
$info['maxplayers'] = $buffer->ReadByte();
return $info;
}
?>