Advertisement
Guest User

Untitled

a guest
Aug 20th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class CServerRcon
  3. {
  4.   private $password;
  5.   private $_sock = null;
  6.   private $_id = 0;
  7.   private $isfsock = true;
  8.  
  9.   const SERVERDATA_EXECCOMMAND = 02;
  10.   const SERVERDATA_AUTH = 03;
  11.   const SERVERDATA_RESPONSE_VALUE = 00;
  12.   const SERVERDATA_AUTH_RESPONSE = 02;
  13.  
  14.   function CServerRcon ($address, $port, $password)
  15.   {
  16.     $this->password = $password;
  17.  
  18.     try
  19.     {
  20.       if (defined('BIND_IP') && function_exists('socket_create') && function_exists('socket_bind'))
  21.       {
  22.         $this->isfsock = false;
  23.         $this->_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  24.     if (!this->_sock) {
  25.         throw new Exception('Socket error ' . socket_strerror(socket_last_error()));
  26.     }
  27.         socket_set_option($this->_sock, SOL_SOCKET, SO_REUSEADDR, 1);
  28.         socket_bind($this->_sock, BIND_IP);
  29.  
  30.         if (!socket_connect($this->_sock, $address, $port)) {
  31.         throw new Exception('Socket error ' . socket_strerror(socket_last_error()));
  32.     }
  33.    
  34.         socket_set_option($this->_sock, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>2, "usec"=>0));
  35.         socket_set_option($this->_sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>2, "usec"=>0));
  36.       }
  37.       else
  38.       {
  39.         $this->_sock = @fsockopen($address, $port, $errno, $errstr, 2);
  40.     if (!$this->_sock) {
  41.         throw new Exception('Socket error ' . $errno . ' ' . $errstr);
  42.     }
  43.         stream_set_timeout($this->_sock, 2);
  44.       }
  45.     }
  46.     catch (Exception $err) {
  47.        echo $err->getMessage() . '<br/>'; // for debug only
  48.     }
  49.   }
  50.    
  51.   public function Auth ()
  52.   {
  53.     $PackID = $this->_Write(CServerRcon::SERVERDATA_AUTH,$this->password);
  54.     $ret = $this->_PacketRead();
  55.  
  56.     return (isset($ret[1]['ID']) && $ret[1]['ID'] == -1)?0:1;
  57.   }
  58.  
  59.   private function _Write($cmd, $s1='', $s2='')
  60.   {
  61.     $id = ++$this->_id;
  62.     $data = pack("VV",$id,$cmd).$s1.chr(0).$s2.chr(0);
  63.     $data = pack("V",strlen($data)).$data;
  64.  
  65.     if ($this->isfsock)
  66.       fwrite($this->_sock, $data, strlen($data));
  67.     else
  68.       socket_write($this->_sock, $data, strlen($data));
  69.  
  70.     return $id;
  71.   }
  72.  
  73.   private function _sock_read($size)
  74.   {
  75.     if ($this->isfsock)
  76.       return @fread($this->_sock, $size);
  77.     else
  78.       return socket_read($this->_sock, $size);
  79.   }
  80.  
  81.   private function _PacketRead()
  82.   {
  83.     $retarray = array();
  84.  
  85.     while ($size = $this->_sock_read(4))
  86.     {
  87.       $size = unpack('V1Size',$size);
  88.  
  89.       if ($size["Size"] > 4096)
  90.         $packet = "\x00\x00\x00\x00\x00\x00\x00\x00".$this->_sock_read(4096);
  91.       else
  92.         $packet = $this->_sock_read($size["Size"]);
  93.  
  94.       array_push($retarray,unpack("V1ID/V1Reponse/a*S1/a*S2",$packet));
  95.     }
  96.  
  97.     return $retarray;
  98.   }
  99.  
  100.   public function Read()
  101.   {
  102.     $Packets = $this->_PacketRead();
  103.  
  104.     foreach($Packets as $pack)
  105.     {
  106.       if (isset($ret[$pack['ID']]))
  107.       {
  108.         $ret[$pack['ID']]['S1'] .= $pack['S1'];
  109.         $ret[$pack['ID']]['S2'] .= $pack['S1'];
  110.       }
  111.       else
  112.       {
  113.         $ret[$pack['ID']] = array('Reponse' => $pack['Reponse'],
  114.                                   'S1' => $pack['S1'],
  115.                                   'S2' =>   $pack['S2'],);
  116.       }
  117.     }
  118.  
  119.     return $ret;
  120.   }
  121.  
  122.   public function sendCommand($command)
  123.   {
  124.     //$command = '"'.trim(str_replace(' ','" "', $command)).'"';
  125.     $this->_Write(CServerRcon::SERVERDATA_EXECCOMMAND,$command,'');
  126.   }
  127.  
  128.   public function rconCommand($command)
  129.   {
  130.       $this->sendCommand($command);
  131.       $ret = $this->Read();
  132.       return $ret[2]['S1'];
  133.   }
  134. }
  135.  
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement