Advertisement
Guest User

Untitled

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