Created
April 6, 2013 11:52
-
-
Save MiLk/5325855 to your computer and use it in GitHub Desktop.
# include/class/Minecraft.class.php
# FujiCraft CMS http://www.fujicraft.com/ Remove support of Minecraft protocol 1.4.7
Add support of Minecraft protocol 1.5.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Minecraft { | |
| private $Socket, $Info; | |
| public $Online, $version, $MOTD, $CurPlayers, $MaxPlayers, $IP, $Port, $Error; | |
| public function __construct($IP, $Port = '25565') { | |
| $this->IP = $IP; | |
| $this->Port = $Port; | |
| // Remove any protocols from serveraddress | |
| if (preg_match('/(.*):\/\//', $this->IP)) { | |
| $this->IP = preg_replace('/(.*):\/\//', '', $this->IP); | |
| } | |
| if (strpos($this->IP, '/') !== false) { | |
| $this->IP = rtrim($this->IP, '/'); | |
| if (strpos($this->IP, '/') !== false) { | |
| $this->Failed(); | |
| $this->Error = 'Unsupported IP/Domain format, no \'/\'s allowed'; | |
| return; | |
| } | |
| } | |
| if (preg_match_all('/:/', $this->IP, $matches) > 1) { | |
| unset($matches); | |
| // IP6 | |
| if (strpos($this->IP, '[') === false && strpos($this->IP, ']') === false) | |
| $this->IP = '[' . $this->IP . ']'; | |
| } else if (strpos($this->IP, ':') !== false) { | |
| $this->Failed(); | |
| $this->Error = 'Unsupported IP/Domain format'; | |
| return; | |
| } | |
| if ($this->Socket = @stream_socket_client('tcp://' . $this->IP . ':' . $Port, $ErrNo, $ErrStr, 1)) { | |
| // If IP6 remove brackets | |
| if (strpos($this->IP, '[') === 0 && strpos($this->IP, ']') === (strlen($this->IP) - 1)) | |
| $this->IP = trim($this->IP, '[]'); | |
| $this->Online = true; | |
| fwrite($this->Socket, "\xfe"); | |
| $Handle = fread($this->Socket, 2048); | |
| $Handle = explode("\x00\x00",$Handle); | |
| $Handle = str_replace("\x00", '', $Handle); | |
| $this->Info = $Handle; // Separate Infos | |
| unset($Handle); | |
| fclose($this->Socket); | |
| if (sizeof($this->Info) == 6) { | |
| $this->version = $this->Info[2]; | |
| $this->MOTD = $this->Info[3]; | |
| $this->CurPlayers = (int) $this->Info[4]; | |
| $this->MaxPlayers = (int) $this->Info[5]; | |
| $this->Error = false; | |
| } else { | |
| $this->Failed(); | |
| $this->Error = 'Unexpected error, cause may be an outdated script'; | |
| } | |
| } else { | |
| $this->Online = false; | |
| $this->Failed(); | |
| $this->Error = 'Can not reach the server'; | |
| } | |
| } | |
| public function Info() { | |
| return array( | |
| 'MOTD' => $this->MOTD, | |
| 'CurPlayers' => $this->CurPlayers, | |
| 'MaxPlayers' => $this->MaxPlayers | |
| ); | |
| } | |
| private function Failed() { | |
| $this->MOTD = false; | |
| $this->CurPlayers = false; | |
| $this->MaxPlayers = false; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment