<?
/*
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.
*/
/*
Improvements to get_response Copyright (C) 2011 Dakota Schneider.
get_response() may be distributed within GPL works, so long as
this message remains intact. Happy coding!
*/
class q3query {
private $rconpassword;
private $fp;
private $cmd;
private $lastcmd; // unused
private $debugmode = false;
public function __construct($address, $port) {
$this->cmd = str_repeat(chr(255), 4);
$this->fp = fsockopen("udp://$address", $port, $errno, $errstr, 1);
stream_set_timeout($this->fp, 1);
if (!$this->fp) {
echo "Q3QUERY - __construct() ".$errstr." (".$errno.")\n";
}
}
public function set_debugmode($mode) {
$this->debugmode = $mode;
}
public function set_rconpassword($p) {
$this->rconpassword = $p;
}
public function rcon($s) {
$rcon_rcon = 'rcon '.$this->rconpassword.' '.$s;
if ($this->debugmode) { echo 'Q3QUERY - rcon() Sending:\n' . $rcon_rcon . '\n'; }
$this->send($rcon_rcon);
}
public function status() {
$rcon_status = 'rcon '.$this->rconpassword." status";
if ($this->debugmode) { echo 'Q3QUERY - status() Sending:\n'.$rcon_status.'\n'; }
$this->send($rcon_status);
}
/*
public function get_response($timeout=1) {
$s = '';
$bang = time() + $timeout;
while (!strlen($s) and time() < $bang) {
$s = $this->recv();
}
if (substr($s, 0, 4) != $this->cmd) {
}
return substr($s, 4);
}
*/
public function get_response($timeout=1) {
$s = '';
$count = 0;
$bang = time() + $timeout;
while (time() < $bang) {
if ($count == 0) {
// first time, include header
$c = $this->recv();
if (!empty($c)) {
if (substr($this->cmd, 0)) {
if ($this->debugmode) { echo "Q3QUERY - get_response() First packet header match.\n"; }
// header matches, carry on
// header: '\xff\xff\xff\xffprint\n'
$bang = time() + $timeout;
$s = $s . $c;
$count = $count + 1;
} else {
// header does not match, stop?
}
}
} else {
$c = $this->recv();
if (!empty($c)) {
if (substr($this->cmd, 0)) {
if ($this->debugmode) { echo "Q3QUERY - get_response() Packet header match.\n"; }
// header matches, carry on
// header: '\xff\xff\xff\xffprint\n'
$bang = time() + $timeout;
$s = $s . substr($c, strpos($c, 'print\n') + 10);
} else {
// header does not match, stop?
}
}
}
}
// if (substr($s, 0, 4) != $this->cmd) {
// }
return substr($s, 4);
}
private function send($string) {
$rcon_send = $this->cmd . $string . "\n";
fwrite($this->fp, $rcon_send);
if ($this->debugmode) { echo "Q3QUERY - send() wrote:\n".$rcon_send.'\n'; }
}
private function recv() {
if (!$this->fp) { return FALSE; }
$fread = fread($this->fp, 9999);
return $fread;
if (empty($fread)) { $fread = "EMPTY"; }
if ($this->debugmode) { echo "Q3QUERY - recv() read:\n".$fread."\n"; }
}
}
?>