ProfesseurCraft

Rcon.class.php

Oct 3rd, 2013
3,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.81 KB | None | 0 0
  1. <?php
  2. /*
  3. RCON remote console class, modified for minecraft compability by Tehbeard.
  4.  
  5. !!!YOU MUST CONFIGURE RCON ON YOUR MINECRAFT SERVER FOR THIS TO WORK
  6. AT TIME OF WRITING ONLY 1.9pr4+ HAVE BUILTIN RCON SUPPORT!!!
  7.  
  8. Example Code:
  9. ============
  10. $r = new rcon("ip",port,"mot de passe"); //create rcon object for server on the rcon port with a specific password
  11. if($r->Auth()){ //Connect and attempt to authenticate
  12. {
  13.   $r->rconCommand("say Saving in 10 seconds!"); //send a command
  14.   sleep(10);
  15.   $r->rconCommand("save-all"); //send a command
  16.   $r->rconCommand("say Save complete!");//send a command
  17.   echo $r->rconCommand("list");//send a command, echo returned value
  18.   }
  19. }
  20. ============
  21.  
  22. Based upon the following work:
  23. [<<<
  24.     Basic CS:S Rcon class by Freman.  (V1.00)
  25.     ----------------------------------------------
  26.     Ok, it's a completely working class now with with multi-packet responses
  27.  
  28.     Contact: printf("%s%s%s%s%s%s%s%s%s%d%s%s%s","rc","on",chr(46),"cl","ass",chr(64),"pri","ya",chr(46),2,"y",chr(46),"net")
  29.  
  30.     Behaviour I've noticed:
  31.         rcon is not returning the packet id.
  32. >>>]
  33. */
  34.  
  35. define("SERVERDATA_EXECCOMMAND",2);
  36. define("SERVERDATA_AUTH",3);
  37.  
  38. class RCon {
  39.     var $Password;
  40.     var $Host;
  41.     var $Port = 27015;
  42.     var $_Sock = null;
  43.     var $_Id = 0;
  44.  
  45.     function RCon ($Host,$Port,$Password) {
  46.         $this->Password = $Password;
  47.         $this->Host = $Host;
  48.         $this->Port = $Port;
  49.         $this->_Sock = @fsockopen($this->Host,$this->Port, $errno, $errstr, 30) or
  50.                 die("Unable to open socket: $errstr ($errno)\n");
  51.         $this->_Set_Timeout($this->_Sock,2,500);
  52.         }
  53.  
  54.     function Auth () {
  55.         $PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);
  56.  
  57.         // Real response (id: -1 = failure)
  58.         $ret = $this->_PacketRead();
  59.         //var_dump($ret);
  60.         if ($ret[0]['ID'] == -1) {
  61.             return false;
  62.         }
  63.         return true;
  64.     }
  65.  
  66.     function _Set_Timeout(&$res,$s,$m=0) {
  67.         if (version_compare(phpversion(),'4.3.0','<')) {
  68.             return socket_set_timeout($res,$s,$m);
  69.         }
  70.         return stream_set_timeout($res,$s,$m);
  71.     }
  72.  
  73.     function _Write($cmd, $s1='', $s2='') {
  74.         // Get and increment the packet id
  75.         $id = ++$this->_Id;
  76.  
  77.         // Put our packet together
  78.         $data = pack("VV",$id,$cmd).$s1.chr(0).$s2.chr(0);
  79.  
  80.         // Prefix the packet size
  81.         $data = pack("V",strlen($data)).$data;
  82.  
  83.         // Send packet
  84.         fwrite($this->_Sock,$data,strlen($data));
  85.  
  86.         // In case we want it later we'll return the packet id
  87.         return $id;
  88.     }
  89.  
  90.     function _PacketRead() {
  91.         //Declare the return array
  92.         $retarray = array();
  93.         //Fetch the packet size
  94.         while ($size = @fread($this->_Sock,4)) {
  95.             $size = unpack('V1Size',$size);
  96.             //Work around valve breaking the protocol
  97.             if ($size["Size"] > 4096) {
  98.                 //pad with 8 nulls
  99.                 $packet = "\x00\x00\x00\x00\x00\x00\x00\x00".fread($this->_Sock,4096);
  100.             } else {
  101.                 //Read the packet back
  102.                 $packet = fread($this->_Sock,$size["Size"]);
  103.             }
  104.             array_push($retarray,unpack("V1ID/V1Response/a*S1/a*S2",$packet));
  105.         }
  106.         return $retarray;
  107.     }
  108.  
  109.     function Read() {
  110.         $Packets = $this->_PacketRead();
  111.  
  112.         foreach($Packets as $pack) {
  113.             if (isset($ret[$pack['ID']])) {
  114.                 $ret[$pack['ID']]['S1'] .= $pack['S1'];
  115.                 $ret[$pack['ID']]['S2'] .= $pack['S1'];
  116.             } else {
  117.                 $ret[$pack['ID']] = array(
  118.                     'Response' => $pack['Response'],
  119.                     'S1' => $pack['S1'],
  120.                     'S2' => $pack['S2'],
  121.                 );
  122.             }
  123.         }
  124.         return $ret;
  125.     }
  126.  
  127.     function sendCommand($Command) {
  128.         //$Command = '"'.trim(str_replace(' ','" "', $Command)).'"';
  129.         //$Command="stop";
  130.         $this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
  131.     }
  132.  
  133.     function rconCommand($Command) {
  134.         $this->sendcommand($Command);
  135.  
  136.         $ret = $this->Read();
  137.  
  138.         //ATM: Source servers don't return the request id, but if they fix this the code below should read as
  139.         // return $ret[$this->_Id]['S1'];
  140.         return $ret[$this->_Id]['S1'];
  141.     }
  142. }
  143. ?>
Advertisement
Add Comment
Please, Sign In to add comment