<?
// ------------------------------------------------------------------
// SAN ANDREAS: MULTI THEFT AUTO GAME SERVER QUERY CLASS
// ------------------------------------------------------------------
// Written by: Bogdan Zarchievici <hide@address.com>
// Version: 1.0.0.a
// Revision: Tuesday, February 07, 2006 at 10:44:39 PM
// ------------------------------------------------------------------
// Comments: works with any PHP version greater than 4.1.0.
// ------------------------------------------------------------------
// Special thanks to: Alexandru Burdulea and all of you who have been
// actively interested in the development of this class.
// ------------------------------------------------------------------
// For more details on MTA:SA please visit:
// http://www.mtasa.com
// http://multitheftauto.com/blog
// ------------------------------------------------------------------
class san_andreas
{ var $timeout=10; // packet TTL in seconds
//
// MAIN FUNCTION OF THE CLASS (CONSTRUCTOR)
//
function san_andreas($ip,$port)
{ $player_count=0;
# (begin) socket code
$socket=socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array('sec'=>$this->timeout,'usec'=>0));
if (!$socket) return; $packet=null;
if (!socket_sendto($socket,chr(115),chr(49),0x100,$ip,$port+123)) return;
if (!socket_recvfrom($socket,$raw,4096,0,$client['ip'],$client['port'])) return;
socket_close($socket);
# (end) socket code
# (begin) packet parsing code
$server=substr($raw,0,strlen($raw)-strlen(strchr($raw,chr(63))));
$players=explode(chr(63),strchr($raw,chr(63)));
foreach (range(1,22) as $code)
{ $server=str_replace(chr($code),chr(0),$server);
$players=str_replace(chr($code),chr(0),$players);
}
for ($i=0; $i<(count($server)); $i++) $chunk=explode(chr(0),$server);
# (end) packet parsing code
# (begin) fill server array with results
$this->results['server']['hostname']=explode(chr(7),str_replace($port.$chunk[2][5],"",$chunk[2]));
$this->results['server']['hostname']=$this->results['server']['hostname'][0];
$this->results['server']['gametype']=$chunk[3];
$this->results['server']['gamemod']=$chunk[4];
$this->results['server']['version']=$chunk[5];
$this->results['server']['max_players']=$chunk[8];
$this->results['server']['players']=0;
# (end) fill server array with results
# (begin) fill players array with results
for ($i=1; $i<count($players); $i++)
{ $player_info=explode(chr(0),$players[$i]);
$this->results['players'][$player_count]['player']=$player_info[1];
$this->results['players'][$player_count]['status']='playing';
$this->results['players'][$player_count]['ping']=$player_info[5];
$player_count++;
}
if (isset($this->results['players']) && count($this->results['players'])>0) $this->results['server']['players']=count($this->results['players']);
# (end) fill players array with results
unset($this->timeout);
}
}
?>