Guest User

Untitled

a guest
Aug 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @Author: xISRAPILx
  5. */
  6. class Query{
  7.  
  8. /** @var string */
  9. private $address;
  10.  
  11. /** @var int */
  12. private $port;
  13.  
  14. /** @var resource */
  15. private $socket;
  16.  
  17. /**
  18. * Query constructor.
  19. *
  20. * @param string $address
  21. * @param int $port
  22. */
  23. public function __construct(string $address, int $port){
  24. $this->address = $address;
  25. $this->port = $port;
  26.  
  27. $this->connect();
  28. }
  29.  
  30. public function __destruct(){
  31. fclose($this->socket);
  32. }
  33.  
  34. /**
  35. * @return bool
  36. */
  37. public function connect() : bool{
  38. $this->socket = fsockopen("udp://".$this->address, $this->port, $error_id, $error, 4);
  39.  
  40. return !$this->socket ? false : true;
  41. }
  42.  
  43. /**
  44. * Получение базовой информации с сервера(Онлайн, название, игроковой режим и тд.).
  45. *
  46. * @return array|null
  47. */
  48. public function getInformation() : ?array{
  49. if(!fwrite($this->socket, $this->createBasePacket("i")))
  50. return null;
  51.  
  52. if(!fread($this->socket, 11))
  53. return null;
  54.  
  55. $information["password"] = (bool) Binary::readByte(fread($this->socket, 1)); //Статус пароля сервера
  56. $information["players"] = Binary::readLShort(fread($this->socket, 2)); //Кол-во игроков онлайн
  57. $information["max_players"] = Binary::readLShort(fread($this->socket, 2)); //Максимальное количество игроков.
  58. $information["hostname"] = Binary::readLInt(fread($this->socket, 4)); //Длина названия сервера
  59. $information["hostname"] = fread($this->socket, $information["hostname"]); //Само название сервера
  60. $information["gamemode"] = Binary::readLInt(fread($this->socket, 4)); //Длина игрового режима сервера
  61. $information["gamemode"] = fread($this->socket, $information["gamemode"]); //Сам игроков режим сервера
  62. $information["language"] = Binary::readLInt(fread($this->socket, 4)); //Длина языка сервера
  63. $information["language"] = fread($this->socket, $information["language"]); //Сам язык сервера
  64.  
  65. return $information;
  66. }
  67.  
  68. /**
  69. * @param string $opcode
  70. *
  71. * @return string
  72. */
  73. public function createBasePacket(string $opcode = "i") : string{
  74. $address = explode(".", $this->address);
  75.  
  76. $packet = "SAMP";
  77. $packet .= Binary::writeByte($address[0]);
  78. $packet .= Binary::writeByte($address[1]);
  79. $packet .= Binary::writeByte($address[2]);
  80. $packet .= Binary::writeByte($address[3]);
  81. $packet .= Binary::writeLShort($this->port);
  82. $packet .= $opcode;
  83.  
  84. return $packet;
  85. }
  86.  
  87. /**
  88. * @return string
  89. */
  90. public function getAddress() : string{
  91. return $this->address;
  92. }
  93.  
  94. /**
  95. * @param string $address
  96. */
  97. public function setAddress(string $address) : void{
  98. fclose($this->socket);
  99. $this->address = $address;
  100. $this->connect();
  101. }
  102.  
  103. /**
  104. * @return int
  105. */
  106. public function getPort() : int{
  107. return $this->port;
  108. }
  109.  
  110. /**
  111. * @param int $port
  112. */
  113. public function setPort(int $port) : void{
  114. $this->port = $port;
  115. }
  116. }
  117.  
  118. class Binary{
  119.  
  120. /**
  121. * @param string $value
  122. *
  123. * @return int
  124. */
  125. public static function readByte(string $value) : int{
  126. return ord($value{0});
  127. }
  128.  
  129. /**
  130. * @param int $value
  131. *
  132. * @return string
  133. */
  134. public static function writeByte(int $value) : string{
  135. return chr($value);
  136. }
  137.  
  138. /**
  139. * @param string $value
  140. *
  141. * @return int
  142. */
  143. public static function readLShort(string $value) : int{
  144. return unpack("v", $value)[1];
  145. }
  146.  
  147. /**
  148. * @param int $value
  149. *
  150. * @return string
  151. */
  152. public static function writeLShort(int $value) : string{
  153. return pack("v", $value);
  154. }
  155.  
  156. /**
  157. * @param string $value
  158. *
  159. * @return int
  160. */
  161. public static function readLInt(string $value) : int{
  162. return unpack("V", $value)[1] << 32 >> 32;
  163. }
  164.  
  165. /**
  166. * @param int $value
  167. *
  168. * @return string
  169. */
  170. public static function writeLInt(int $value) : string{
  171. return pack("V", $value);
  172. }
  173. }
Add Comment
Please, Sign In to add comment