Guest User

shop.php

a guest
Aug 20th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  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.  
  25.         socket_set_option($this->_sock, SOL_SOCKET, SO_REUSEADDR, 1);
  26.         socket_bind($this->_sock, BIND_IP);
  27.  
  28.         socket_connect($this->_sock, $address, $port);
  29.  
  30.         socket_set_option($this->_sock, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>2, "usec"=>0));
  31.         socket_set_option($this->_sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>2, "usec"=>0));
  32.       }
  33.       else
  34.       {
  35.         $this->_sock = @fsockopen($address, $port, $errno, $errstr, 2);
  36.         stream_set_timeout($this->_sock, 2);
  37.       }
  38.     }
  39.     catch (Exception $err) { }
  40.   }
  41.    
  42.   public function Auth ()
  43.   {
  44.     $PackID = $this->_Write(CServerRcon::SERVERDATA_AUTH,$this->password);
  45.     $ret = $this->_PacketRead();
  46.  
  47.     return (isset($ret[1]['ID']) && $ret[1]['ID'] == -1)?0:1;
  48.   }
  49.  
  50.   private function _Write($cmd, $s1='', $s2='')
  51.   {
  52.     $id = ++$this->_id;
  53.     $data = pack("VV",$id,$cmd).$s1.chr(0).$s2.chr(0);
  54.     $data = pack("V",strlen($data)).$data;
  55.  
  56.     if ($this->isfsock)
  57.       fwrite($this->_sock, $data, strlen($data));
  58.     else
  59.       socket_write($this->_sock, $data, strlen($data));
  60.  
  61.     return $id;
  62.   }
  63.  
  64.   private function _sock_read($size)
  65.   {
  66.     if ($this->isfsock)
  67.       return @fread($this->_sock, $size);
  68.     else
  69.       return socket_read($this->_sock, $size);
  70.   }
  71.  
  72.   private function _PacketRead()
  73.   {
  74.     $retarray = array();
  75.  
  76.     while ($size = $this->_sock_read(4))
  77.     {
  78.       $size = unpack('V1Size',$size);
  79.  
  80.       if ($size["Size"] > 4096)
  81.         $packet = "\x00\x00\x00\x00\x00\x00\x00\x00".$this->_sock_read(4096);
  82.       else
  83.         $packet = $this->_sock_read($size["Size"]);
  84.  
  85.       array_push($retarray,unpack("V1ID/V1Reponse/a*S1/a*S2",$packet));
  86.     }
  87.  
  88.     return $retarray;
  89.   }
  90.  
  91.   public function Read()
  92.   {
  93.     $Packets = $this->_PacketRead();
  94.  
  95.     foreach($Packets as $pack)
  96.     {
  97.       if (isset($ret[$pack['ID']]))
  98.       {
  99.         $ret[$pack['ID']]['S1'] .= $pack['S1'];
  100.         $ret[$pack['ID']]['S2'] .= $pack['S1'];
  101.       }
  102.       else
  103.       {
  104.         $ret[$pack['ID']] = array('Reponse' => $pack['Reponse'],
  105.                                   'S1' => $pack['S1'],
  106.                                   'S2' =>   $pack['S2'],);
  107.       }
  108.     }
  109.  
  110.     return $ret;
  111.   }
  112.  
  113.   public function sendCommand($command)
  114.   {
  115.     //$command = '"'.trim(str_replace(' ','" "', $command)).'"';
  116.     $this->_Write(CServerRcon::SERVERDATA_EXECCOMMAND,$command,'');
  117.   }
  118.  
  119.   public function rconCommand($command)
  120.   {
  121.       $this->sendCommand($command);
  122.       $ret = $this->Read();
  123.       return $ret[2]['S1'];
  124.   }
  125. }
  126.  
  127. ?>
Advertisement
Add Comment
Please, Sign In to add comment