tronds icon

Simple Quake 3 RCON Class

tronds | PRO | 02/19/13 09:03:03 AM UTC | 0 ⭐ | 230 👁️ | Never ⏰ | []
PHP |

3.08 KB

|

None

|

0 👍

/

0 👎

<?php
class Q3Query
    {
    const packet_prefix = "\xff\xff\xff\xff";
    
    public function remoteConsole ($Ip, $Port = 28960, $Pw = "Test", $Msg = "status")
    {
    try{
            if(($this->Socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === FALSE){
                throw new Exception('Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL);
            }
            socket_set_option($this->Socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>1, 'usec'=>0));
            socket_set_option($this->Socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>1, 'usec'=>0));
            
            $msg = self::packet_prefix . "rcon \"" . $Pw . "\" " . $Msg;
            $msglen = strlen($msg);
            if(@socket_sendto($this->Socket, $msg, $msglen, 0, $Ip, $Port) === FALSE){
                throw new Exception('An error occured while writing to the server.');
            }
 
 
            if((@socket_recvfrom($this->Socket, $data, 8129, 0, $Ip, $Port)) === FALSE){
                throw new Exception('Gameserver timed out.');
            }
 
            socket_close($this->Socket);
            $this->data = $data;
    } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
            return(-1);
    }
    
    }
    public function getStatus ($Ip, $Port = 28960)
    {
    try{
        if(($this->Socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === FALSE){
            throw new Exception('Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL);
        }
        socket_set_option($this->Socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>1, 'usec'=>0));
        socket_set_option($this->Socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>1, 'usec'=>0));
        
        $msg = self::packet_prefix . "getstatus\n";
        $msglen = strlen($msg);
        if(@socket_sendto($this->Socket, $msg, $msglen, 0, $Ip, $Port) === FALSE){
            throw new Exception('Gameserver timed out.');
        }
 
 
        if((@socket_recvfrom($this->Socket, $data, 8129, 0, $Ip, $Port)) === FALSE){
            throw new Exception('An error occured while reading from the server.');
        }
 
        socket_close($this->Socket);
        $this->data = $data;
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
        return(-1);
    }
        
    }
    
    
    public function getStatusStructure($data)
    {
    /*Divides the response into an array
    [0] = prefix, [1] = variables with values (separated by "\"), [2] and greater = playerinfo*/
    $splitdata = explode("\n", $data);
    
    $vardata = explode("\\", $splitdata[1]);
    $this->vararray=array();
    $varlength = count($vardata);
        //First element in vardata is a whitespace, skipping it
        for($key=1, $value=2; $key<$varlength, $value<$varlength;$key=$key+2, $value=$value+2){
            $this->vararray[$vardata[$key]] = $vardata[$value];
        }
    
    $this->playerarray = array();
        for($x=2, $c = 0;$x < (count($splitdata)-1); $x++, $c++){//Last element is a whitespace
            $player = explode(" ", $splitdata[$x]);
            $name = implode(" ", array_slice($player, 2)); //This is just silly ._.
            /*
            name = $player[>=2]
            ping = $player[1];
            score = $player[0];*/
            $this->playerarray[$c] = array("name" => $name, "score" => $player[0], "ping" => $player[1]);
        }
    }
    public function remoteConsoleStructure($data){
 
    $this->splitdata = explode("\n", $data);
 
    }
    }?>

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎