1
0

MinecraftRcon.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. class MinecraftRcon
  25. {
  26. /*
  27. * Class written by xPaw
  28. *
  29. * Website: http://xpaw.ru
  30. * GitHub: https://github.com/xPaw/PHP-Minecraft-Query
  31. *
  32. * Protocol: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
  33. */
  34. // Sending
  35. const SERVERDATA_EXECCOMMAND = 2;
  36. const SERVERDATA_AUTH = 3;
  37. // Receiving
  38. const SERVERDATA_RESPONSE_VALUE = 0;
  39. const SERVERDATA_AUTH_RESPONSE = 2;
  40. private $Socket;
  41. private $RequestId;
  42. public function __destruct( )
  43. {
  44. $this->Disconnect( );
  45. }
  46. public function Connect( $Ip, $Port = 25575, $Password, $Timeout = 3 )
  47. {
  48. $this->RequestId = 0;
  49. if( $this->Socket = FSockOpen( $Ip, (int)$Port ) )
  50. {
  51. Socket_Set_TimeOut( $this->Socket, $Timeout );
  52. if( !$this->Auth( $Password ) )
  53. {
  54. $this->Disconnect( );
  55. }
  56. else
  57. {
  58. return true;
  59. }
  60. }
  61. }
  62. public function Disconnect( )
  63. {
  64. if( $this->Socket )
  65. {
  66. FClose( $this->Socket );
  67. $this->Socket = null;
  68. }
  69. }
  70. public function Command( $String )
  71. {
  72. if( !$this->WriteData( self :: SERVERDATA_EXECCOMMAND, $String ) )
  73. {
  74. return false;
  75. }
  76. $Data = $this->ReadData( );
  77. if( $Data[ 'RequestId' ] < 1 || $Data[ 'Response' ] != self :: SERVERDATA_RESPONSE_VALUE )
  78. {
  79. return false;
  80. }
  81. return $Data[ 'String' ];
  82. }
  83. private function Auth( $Password )
  84. {
  85. if( !$this->WriteData( self :: SERVERDATA_AUTH, $Password ) )
  86. {
  87. return false;
  88. }
  89. $Data = $this->ReadData( );
  90. return $Data[ 'RequestId' ] > -1 && $Data[ 'Response' ] == self :: SERVERDATA_AUTH_RESPONSE;
  91. }
  92. private function ReadData( )
  93. {
  94. $Packet = Array( );
  95. $Size = FRead( $this->Socket, 4 );
  96. $Size = UnPack( 'V1Size', $Size );
  97. $Size = $Size[ 'Size' ];
  98. // TODO: Add multiple packets (Source)
  99. $Packet = FRead( $this->Socket, $Size );
  100. $Packet = UnPack( 'V1RequestId/V1Response/a*String/a*String2', $Packet );
  101. return $Packet;
  102. }
  103. private function WriteData( $Command, $String = "" )
  104. {
  105. // Pack the packet together
  106. $Data = Pack( 'VV', $this->RequestId++, $Command ) . $String . "\x00\x00\x00";
  107. // Prepend packet length
  108. $Data = Pack( 'V', StrLen( $Data ) ) . $Data;
  109. $Length = StrLen( $Data );
  110. return $Length === FWrite( $this->Socket, $Data, $Length );
  111. }
  112. }
  113. ?>