tronds

Simple Quake 3 RCON Class

Feb 19th, 2013
339
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. <?php
  2. class Q3Query
  3.     {
  4.     const packet_prefix = "\xff\xff\xff\xff";
  5.    
  6.     public function remoteConsole ($Ip, $Port = 28960, $Pw = "Test", $Msg = "status")
  7.     {
  8.     try{
  9.             if(($this->Socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === FALSE){
  10.                 throw new Exception('Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL);
  11.             }
  12.             socket_set_option($this->Socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>1, 'usec'=>0));
  13.             socket_set_option($this->Socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>1, 'usec'=>0));
  14.            
  15.             $msg = self::packet_prefix . "rcon \"" . $Pw . "\" " . $Msg;
  16.             $msglen = strlen($msg);
  17.             if(@socket_sendto($this->Socket, $msg, $msglen, 0, $Ip, $Port) === FALSE){
  18.                 throw new Exception('An error occured while writing to the server.');
  19.             }
  20.  
  21.  
  22.             if((@socket_recvfrom($this->Socket, $data, 8129, 0, $Ip, $Port)) === FALSE){
  23.                 throw new Exception('Gameserver timed out.');
  24.             }
  25.  
  26.             socket_close($this->Socket);
  27.             $this->data = $data;
  28.     } catch (Exception $e) {
  29.             echo 'Caught exception: ',  $e->getMessage(), "\n";
  30.             return(-1);
  31.     }
  32.    
  33.     }
  34.     public function getStatus ($Ip, $Port = 28960)
  35.     {
  36.     try{
  37.         if(($this->Socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === FALSE){
  38.             throw new Exception('Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL);
  39.         }
  40.         socket_set_option($this->Socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>1, 'usec'=>0));
  41.         socket_set_option($this->Socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>1, 'usec'=>0));
  42.        
  43.         $msg = self::packet_prefix . "getstatus\n";
  44.         $msglen = strlen($msg);
  45.         if(@socket_sendto($this->Socket, $msg, $msglen, 0, $Ip, $Port) === FALSE){
  46.             throw new Exception('Gameserver timed out.');
  47.         }
  48.  
  49.  
  50.         if((@socket_recvfrom($this->Socket, $data, 8129, 0, $Ip, $Port)) === FALSE){
  51.             throw new Exception('An error occured while reading from the server.');
  52.         }
  53.  
  54.         socket_close($this->Socket);
  55.         $this->data = $data;
  56.     } catch (Exception $e) {
  57.         echo 'Caught exception: ',  $e->getMessage(), "\n";
  58.         return(-1);
  59.     }
  60.        
  61.     }
  62.    
  63.    
  64.     public function getStatusStructure($data)
  65.     {
  66.     /*Divides the response into an array
  67.     [0] = prefix, [1] = variables with values (separated by "\"), [2] and greater = playerinfo*/
  68.     $splitdata = explode("\n", $data);
  69.    
  70.     $vardata = explode("\\", $splitdata[1]);
  71.     $this->vararray=array();
  72.     $varlength = count($vardata);
  73.         //First element in vardata is a whitespace, skipping it
  74.         for($key=1, $value=2; $key<$varlength, $value<$varlength;$key=$key+2, $value=$value+2){
  75.             $this->vararray[$vardata[$key]] = $vardata[$value];
  76.         }
  77.    
  78.     $this->playerarray = array();
  79.         for($x=2, $c = 0;$x < (count($splitdata)-1); $x++, $c++){//Last element is a whitespace
  80.             $player = explode(" ", $splitdata[$x]);
  81.             $name = implode(" ", array_slice($player, 2)); //This is just silly ._.
  82.             /*
  83.             name = $player[>=2]
  84.             ping = $player[1];
  85.             score = $player[0];*/
  86.             $this->playerarray[$c] = array("name" => $name, "score" => $player[0], "ping" => $player[1]);
  87.         }
  88.     }
  89.     public function remoteConsoleStructure($data){
  90.  
  91.     $this->splitdata = explode("\n", $data);
  92.  
  93.     }
  94.     }?>
Advertisement
Comments
  • Comment was deleted
Add Comment
Please, Sign In to add comment