nbtc971
2010-10-06 00:34:54
Does anyone have a fully working server status script after the most recent update to HL2DM? I have managed to get mine partly working, as in it pulls in the server information such as map name, server name, number players, etc. But the getPlayers function isn't working any longer. I can't seem to get it working.
Any help is appreciated.
Code: Select all
define('INFO_RESPONSE_HL1',0x6D);
define('CHALLENGE_RESPONSE',0x41);
include('inc_hexdump.php');
include_once('inc_system-functions.php');
class CServerInfo
{
var $raw;
function getInfo($address,$port)
{
$ret = array();
$s = fsockopen("udp://".$address,$port,$errno,$errstring,1);
//if (!$s) echo "$errstring ($errno)<br />\n";
stream_set_timeout($s,1);
if(!fwrite($s,"\xFF\xFF\xFF\xFFTSource Engine Query\x00",39))
{
echo "Error Writing Data. $errstring ($errno)";
}
$packet = fread($s,1024);
$parr = explode("\x00",substr($packet,6));
$ret['hostname'] = $parr[0];
$ret['map'] = $parr[1];
$ret['gamename'] = $parr[2];
$ret['gamedesc'] = $parr[3];
$packet = substr($packet,6+strlen($ret['hostname'])+1+strlen($ret['map'])+1+strlen($ret['gamename'])+1+strlen($ret['gamedesc'])+1+2);
$ret['numplayers'] = ord($packet[0]);
$ret['maxplayers'] = ord($packet[1]);
$ret['botcount'] = ord($packet[2]);
$ret['dedicated'] = ord($packet[3]);
$ret['os'] = $packet[4];
$ret['password'] = ord($packet[5]);
$ret['secure'] = $packet[6];
$_SESSION['getInfo.' . $address . '.' . $port] = $ret;
return $ret;
}
function getPlayers($address,$port)
{
set_magic_quotes_runtime(0);
$ret = array();
$s = fsockopen("udp://".$address,$port,$errno,$errstring,1);
stream_set_timeout($s,1); // 1 second timeout on read/write operations
fwrite($s,"\xFF\xFF\xFF\xFF\x57"); //Get challenge #
$packet = fread($s,1024);
$chalId = $this->getChallenge($packet);
echo $chalId;
fwrite($s,"\xFF\xFF\xFF\xFF\x55".$chalId);
$packet = fread($s,2048);
if(empty($packet))
{
return $this->getPlayersProxy($address, $port);
}
$packet = substr($packet,5);
$nump = ord($packet[0]);
$packet = substr($packet,1);
$this->raw = $packet;
for ($i=0;$i<$nump;$i++)
{
$temp = array();
$temp['index'] = $this->_getbyte();
$temp['name'] = $this->_getnullstr();
$temp['kills'] = $this->_getlong();
$temp['time'] = SecondsToString((int)$this->_getfloat(),true);
if(!empty($temp['name']))
array_push($ret,$temp);
}
array_qsort($ret, 'kills', SORT_DESC);
return $ret;
}
function getChallenge($packet)
{
if(isset($packet[4]) && ord($packet[4]) == CHALLENGE_RESPONSE)
{
return substr($packet,5);
}
return "\xFF\xFF\xFF\xFF";
/*
socket_set_timeout($this->sock, 2);
fwrite($this->sock, "\xFF\xFF\xFF\xFF\x57");
$challenge = fread($this->sock, 4096);
$challenge = substr($challenge, 5, 4);
return $challenge;
*/
}
/*
Proxy functions are used when there is a problem with blocked ports,
or firewalls like with some webhosts.
*/
function getInfoProxy($address,$port)
{
if(!(isset($address) && isset($port)))
return false;
$ret = array();
$info = @file_get_contents(SERVER_QUERY . "?ip=" . $address . "&port=" .$port."&type=info");
if(strstr($info, "Page not found"))
return false;
if($info)
{
$inf = explode("\t", $info);
$ret['hostname'] = $inf[0];
$ret['numplayers'] = $inf[1];
$ret['maxplayers'] = $inf[2];
$ret['map'] = $inf[3];
$_SESSION['getInfo.' . $address . '.' . $port] = $ret;
return $ret;
}
else
return false;
}
function getPlayersProxy($address,$port)
{
if(!(isset($address) && isset($port)))
return false;
$ret = array();
$players = @file_get_contents(SERVER_QUERY . "?ip=" . $address . "&port=" .$port."&type=players");
if(strstr($players, "Page not found"))
return false;
if($players)
{
$plr = explode("\n", $players);
foreach($plr AS $player)
{
$items = explode("\t", $player);
if(!isset($items[1])) {
continue;
}
$row = array();
$row['index'] = $items[0];
$row['name'] = $items[1];
$row['kills'] = $items[2];
$row['time'] = $items[3];
array_push($ret, $row);
}
return $ret;
}
else
return false;
}
function _getnullstr()
{
if (empty($this->raw))
return '';
$end = strpos($this->raw, "\0");
$str = substr($this->raw, 0, $end);
$this->raw = substr($this->raw, $end+1);
return $str;
}
function _getchar()
{
return sprintf("%c", $this->_getbyte());
}
function _getbyte()
{
$byte = substr($this->raw, 0, 1);
$this->raw = substr($this->raw, 1);
return ord($byte);
}
function _getshort()
{
$lo = $this->_getbyte();
$hi = $this->_getbyte();
$short = ($hi << 8) | $lo;
return $short;
}
function _getlong()
{
$lo = $this->_getshort();
$hi = $this->_getshort();
$long = ($hi << 16) | $lo;
return $long;
}
function _getfloat()
{
$f = @unpack("f1float", $this->raw);
$this->raw = substr($this->raw, 4);
return $f['float'];
}
}