Advertisement
Guest User

Untitled

a guest
Dec 18th, 2015
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * IDK RCON Class (UDP Client)
  5.  * for sending remote server commands
  6.  *
  7.  * @author Steve Winfield
  8. **/
  9.  
  10. namespace IDK;
  11.  
  12. class RconInstance {
  13.   const RECV_WAIT_TIMEOUT_SECONDS = 3;
  14.  
  15.   private $ip;
  16.   private $port;
  17.  
  18.   public function __construct($ip, $port) {
  19.     $this->ip = $ip;
  20.     $this->port = $port;
  21.   }
  22.  
  23.   public function sendData($data) {
  24.     $data = (string) $data;
  25.  
  26.     if(!($socket = socket_create(AF_INET, SOCK_DGRAM, 0))) {
  27.       $errorcode = socket_last_error();
  28.       $errormsg = socket_strerror($errorcode);
  29.  
  30.       throw new \Exception("Couldn't create socket: [$errorcode] $errormsg");
  31.     }
  32.  
  33.     if(!socket_sendto($socket, $data, strlen($data), 0, $this->ip, $this->port)) {
  34.       $errorcode = socket_last_error();
  35.       $errormsg = socket_strerror($errorcode);
  36.  
  37.       throw new \Exception("Could not send data: [$errorcode] $errormsg");
  38.     }
  39.  
  40.     socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => self::RECV_WAIT_TIMEOUT_SECONDS, 'usec'=>0));
  41.     socket_recv($socket, $reply, 1024, MSG_WAITALL);
  42.     socket_close($socket);
  43.  
  44.     return $reply;
  45.   }
  46. }
  47.  
  48. class RconWriter {
  49.   private $data;
  50.  
  51.   public function __construct($opCode) {
  52.     $this->data = RconEncryption::encodeB64($opCode);
  53.   }
  54.  
  55.   public function pushUTF($string) {
  56.     $this->data .= RconEncryption::encodeB64(strlen($string)) . $string;
  57.     return $this;
  58.   }
  59.  
  60.   public function pushBoolean($boolean) {
  61.     $this->data .= $boolean ? "I" : "H";
  62.   }
  63.  
  64.   public function pushInteger($i) {
  65.     $this->data .= RconEncryption::encodeVL64($i);
  66.   }
  67.  
  68.   public function __toString() {
  69.     return RconEncryption::encodeB64(strlen($this->data), 3) . $this->data;
  70.   }
  71. }
  72.  
  73. class RconEncryption {
  74.   /**
  75.    * @author BurakDev
  76.   **/
  77.   public static function encodeVL64($i) {
  78.     $wf = array();
  79.     $pos = 0;
  80.     $spos = 0;
  81.     $bytes = 1;
  82.     $negativeMask = $i>=0?0:4;
  83.     $i = abs($i);
  84.     $wf[$pos++] = 64 + ($i & 3);
  85.  
  86.     for ($i >>= 2; $i != 0; $i >>= 6){
  87.       $bytes++;
  88.       $wf[$pos++] = 64 + ($i & 0x3f);
  89.     }
  90.  
  91.     $wf[$spos] = $wf[$spos] | $bytes << 3 | $negativeMask;
  92.     $str = "";
  93.  
  94.     foreach($wf as $tmp) {
  95.       $str .= chr($tmp);
  96.     }
  97.     return str_replace("\0","",$str);
  98.   }
  99.  
  100.   /**
  101.    * @author BurakDev
  102.   **/
  103.   public static function encodeB64($i, $length = 2) {
  104.     if(is_numeric($i)) {
  105.       try {
  106.         $s = "";
  107.  
  108.         for($x = 1; $x <= $length; $x++) {
  109.           $s.= chr( (64 + ($i >> 6 * ( $length - $x) & 0x3f)) );
  110.         }
  111.  
  112.         return $s;
  113.       } catch(Exception $e) {
  114.         return $e->getMessage();
  115.       }
  116.     } else {
  117.       return false;
  118.     }
  119.   }
  120. }
  121.  
  122. class InfobusQuestion extends RconWriter {
  123.   public function __construct($roomId, $question, $selections) {
  124.     parent::__construct(2);
  125.     parent::pushInteger($roomId);
  126.     parent::pushUTF($question);
  127.     parent::pushInteger(count($selections));
  128.  
  129.     foreach ($selections as $selection) {
  130.       parent::pushUTF($selection);
  131.     }
  132.   }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement