Advertisement
Guest User

Untitled

a guest
Feb 19th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <?php
  2. /**
  3. * Minecraft Server Status Class
  4. * @copyright В© 2011 Nox Nebula - Patrick Kleinschmidt
  5. * @website https://github.com/NoxNebula/MC-Server-Status
  6. * @license GNU Public Licence - Version 3
  7. * @author Nox Nebula - Patrick Kleinschmidt
  8. **/
  9.  
  10. class MinecraftStatus {
  11. private $Socket, $Info, $to;
  12. public $Online, $MOTD, $CurPlayers, $MaxPlayers, $IP, $Port, $Error;
  13.  
  14. public function __construct($IP, $Port = '25565', $timeout='0.5') {
  15. $this->IP = $IP;
  16. $this->Port = $Port;
  17. $this->to = $timeout;
  18.  
  19. // Remove any protocols from serveraddress
  20. if(preg_match('/(.*):\/\//', $this->IP)) {
  21. $this->IP = preg_replace('/(.*):\/\//', '', $this->IP);
  22. }
  23. if(strpos($this->IP, '/') !== false) {
  24. $this->IP = rtrim($this->IP, '/');
  25. if(strpos($this->IP, '/') !== false) {
  26. $this->Failed();
  27. $this->Error = 'Unsupported IP/Domain format, no \'/\'s allowed';
  28. return;
  29. }
  30. }
  31. if(preg_match_all('/:/', $this->IP, $matches) > 1) {
  32. unset($matches);
  33. // IP6
  34. if(strpos($this->IP, '[') === false && strpos($this->IP, ']') === false)
  35. $this->IP = '['.$this->IP.']';
  36. } else if(strpos($this->IP, ':') !== false) {
  37. $this->Failed();
  38. $this->Error = 'Unsupported IP/Domain format';
  39. return;
  40. }
  41.  
  42. if($this->Socket = @stream_socket_client('tcp://'.$this->IP.':'.$Port, $ErrNo, $ErrStr, $this->to)) {
  43. // If IP6 remove brackets
  44. if(strpos($this->IP, '[') === 0 && strpos($this->IP, ']') === (strlen($this->IP) - 1))
  45. $this->IP = trim($this->IP, '[]');
  46.  
  47. $this->Online = true;
  48.  
  49. fwrite($this->Socket, "\xfe");
  50. $Handle = fread($this->Socket, 2048);
  51. $Handle = str_replace("\x00", '', $Handle);
  52. $Handle = substr($Handle, 2);
  53. $this->Info = explode("\xa7", $Handle); // Separate Infos
  54. unset($Handle);
  55. fclose($this->Socket);
  56.  
  57. if(sizeof($this->Info) == 3) {
  58. $this->MOTD = $this->Info[0];
  59. $this->CurPlayers = (int)$this->Info[1];
  60. $this->MaxPlayers = (int)$this->Info[2];
  61. $this->Error = false;
  62. } else if(sizeof($this->Info) > 3) { // Handle error, Minecraft don't handle this.
  63. $Temp = '';
  64. for($i = 0; $i < sizeof($this->Info) - 2; $i++) {
  65. $Temp .= ($i > 0 ? 'В§' : '').$this->Info[$i];
  66. }
  67. $this->MOTD = $Temp;
  68. $this->CurPlayers = (int)$this->Info[sizeof($this->Info) - 2];
  69. $this->MaxPlayers = (int)$this->Info[sizeof($this->Info) - 1];
  70. $this->Error = 'Faulty motd or outdated script';
  71. } else {
  72. $this->Failed();
  73. $this->Error = 'Unexpected error, cause may be an outdated script';
  74. }
  75. } else {
  76. $this->Online = false;
  77. $this->Failed();
  78. $this->Error = 'Can not reach the server';
  79. }
  80. }
  81.  
  82. public function Info() {
  83. return array(
  84. 'MOTD' => $this->MOTD,
  85. 'CurPlayers' => $this->CurPlayers,
  86. 'MaxPlayers' => $this->MaxPlayers
  87. );
  88. }
  89.  
  90. private function Failed() {
  91. $this->MOTD = false;
  92. $this->CurPlayers = false;
  93. $this->MaxPlayers = false;
  94. }
  95. }
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement