<?php
/*
PHP Quake 3 Library
Copyright (C) 2006-2007 Gerald Kaszuba
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class q3query {
private $rconpassword;
private $fp;
private $cmd;
private $lastcmd;
public function __construct($address, $port) {
$this->cmd = str_repeat(chr(255), 4);
$this->fp = fsockopen("udp://$address", $port, $errno, $errstr, 30);
if (!$this->fp)
die("$errstr ($errno)<br />\n");
}
public function set_rconpassword($p) {
$this->rconpassword = $p;
}
public function rcon($s) {
sleep(1);
$this->send('rcon '.$this->rconpassword.' '.$s);
}
//Returns a parsed array of the rcon status for this server
public function status(&$cur_map = 0){
$this->send('rcon '.$this->rconpassword.' status');
$response = $this->get_response();
preg_match_all('/^\s+(\S{1,2}?)\s+(\S{1,4})\s+(\S{1,3})\s+(.*)\^7\s+(\S+)\s+(\S+):(\S+)\s+(\S+)\s+(\S+)$/m', $response, $aStatus);
if ( !is_int($cur_map) ){
preg_match('/^map:\s+(.*)$/m', $response, $aMap);
$cur_map = $aMap[1];
}
return $aStatus;
}
//Returns an array of all the maps available on this server
public function maplist(){
$this->send('rcon dir maps bsp');
$response = $this->get_response();
$parts = split("---------------\n", $response);
$maplist = split("\n", $parts[1]);
return $maplist;
}
public function get_response($timeout=5) {
$s = '';
$bang = time() + $timeout;
while (!strlen($s) and time() < $bang) {
$s = $this->recv();
}
if (substr($s, 0, 4) != $this->cmd) {
}
return substr($s, 4);
}
private function send($string) {
fwrite($this->fp, $this->cmd . $string . "\n");
}
private function recv() {
return fread($this->fp, 9999);
}
}
?>