06-04-2012, 08:50 AM
(This post was last modified: 06-04-2012, 08:50 AM by Chemicstry.)
I've updated this script to remove those hacky things and added some more functionality. I had to invent php byte buffer as I couldn't find anything similar lol.
Anyway here it is:
Anyway here it is:
Code:
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 = gethostbyname($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;
}
$HL2_stats = '';
$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);
$result = '';
$x = 0;
while ($x <= strlen($HL2_stats))
{
$x++;
$result.= substr($HL2_stats, $x, 1);
}
return $result;
}
function format_source_query($string)
{
$buffer = new ByteBuffer($string);
$info['version'] = $buffer->ReadByte();
$info['hostname'] = $buffer->ReadString();
$info['map'] = $buffer->ReadString();
$info['game'] = $buffer->ReadString();
$info['gamedesc'] = $buffer->ReadString();
$info['appid'] = $buffer->ReadShort();
$info['players'] = $buffer->ReadByte();
$info['maxplayers'] = $buffer->ReadByte();
$info['bots'] = $buffer->ReadByte();
$info['dedicated'] = $buffer->ReadChar(); // l - listen, d - dedicated, p - sourcetv
$info['os'] = $buffer->ReadChar(); // l - linux, w - windows
$info['password'] = $buffer->ReadByte(); // true/false
$info['vac'] = $buffer->ReadByte(); // true/false
$info['gameversion'] = $buffer->ReadString();
return $info;
}
$query = source_query($address);
$response = format_source_query($query);