Advertisement
Guest User

PHP Script for _Zaboomafoo_

a guest
Jul 22nd, 2015
6,625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1. Place this in you index.php or whatever page you want it.
  2.  
  3. <?php require_once('onplayers.php'); ?>
  4.  
  5.  
  6. Place this in a file called "onplayers.php"
  7.  
  8. <div id="onplayers">
  9. <a><?php include_once 'status.class.php'; $status = new MinecraftServerStatus(); $response = $status->getStatus('yourserver.com'); if(!$response) { echo"Server name is offline!"; } else { echo"Players online: ".$response['players']; }; ?></a>
  10. </div>
  11.  
  12. Place this in a file called "status.class.php"
  13.  
  14. <?php
  15.  
  16. /**
  17. * Minecraft Server Status Query
  18. * @author Julian Spravil <[email protected]> https://github.com/FunnyItsElmo
  19. * @license Free to use but dont remove the author, license and copyright
  20. * @copyright © 2013 Julian Spravil
  21. */
  22. class MinecraftServerStatus {
  23.  
  24. private $timeout;
  25.  
  26. public function __construct($timeout = 2) {
  27. $this->timeout = $timeout;
  28. }
  29.  
  30. public function getStatus($host = '127.0.0.1', $port = 25565, $version = '1.7.*') {
  31.  
  32. if (substr_count($host , '.') != 4) $host = gethostbyname($host);
  33.  
  34. $serverdata = array();
  35. $serverdata['hostname'] = $host;
  36. $serverdata['version'] = false;
  37. $serverdata['protocol'] = false;
  38. $serverdata['players'] = false;
  39. $serverdata['maxplayers'] = false;
  40. $serverdata['motd'] = false;
  41. $serverdata['motd_raw'] = false;
  42. $serverdata['favicon'] = false;
  43. $serverdata['ping'] = false;
  44.  
  45. $socket = $this->connect($host, $port);
  46.  
  47. if(!$socket) {
  48. return false;
  49. }
  50.  
  51. if(preg_match('/1.7|1.8/',$version)) {
  52.  
  53. $start = microtime(true);
  54.  
  55. $handshake = pack('cccca*', hexdec(strlen($host)), 0, 0x04, strlen($host), $host).pack('nc', $port, 0x01);
  56.  
  57. socket_send($socket, $handshake, strlen($handshake), 0); //give the server a high five
  58. socket_send($socket, "\x01\x00", 2, 0);
  59. socket_read( $socket, 1 );
  60.  
  61. $ping = round((microtime(true)-$start)*1000); //calculate the high five duration
  62.  
  63. $packetlength = $this->read_packet_length($socket);
  64.  
  65. if($packetlength < 10) {
  66. return false;
  67. }
  68.  
  69. socket_read($socket, 1);
  70.  
  71. $packetlength = $this->read_packet_length($socket);
  72.  
  73. $data = socket_read($socket, $packetlength, PHP_NORMAL_READ);
  74.  
  75. if(!$data) {
  76. return false;
  77. }
  78.  
  79. $data = json_decode($data);
  80.  
  81. $serverdata['version'] = $data->version->name;
  82. $serverdata['protocol'] = $data->version->protocol;
  83. $serverdata['players'] = $data->players->online;
  84. $serverdata['maxplayers'] = $data->players->max;
  85.  
  86. $motd = $data->description;
  87. $motd = preg_replace("/(§.)/", "",$motd);
  88. $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);
  89.  
  90. $serverdata['motd'] = $motd;
  91. $serverdata['motd_raw'] = $data->description;
  92. $serverdata['favicon'] = $data->favicon;
  93. $serverdata['ping'] = $ping;
  94.  
  95. } else {
  96.  
  97. $start = microtime(true);
  98.  
  99. socket_send($socket, "\xFE\x01", 2, 0);
  100. $length = socket_recv($socket, $data, 512, 0);
  101.  
  102. $ping = round((microtime(true)-$start)*1000);//calculate the high five duration
  103.  
  104. if($length < 4 || $data[0] != "\xFF") {
  105. return false;
  106. }
  107.  
  108. $motd = "";
  109. $motdraw = "";
  110.  
  111. //Evaluate the received data
  112. if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){
  113.  
  114. $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
  115. $motd = $result[1];
  116. $motdraw = $motd;
  117.  
  118. } else {
  119.  
  120. $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
  121. foreach ($result as $key => $string) {
  122. if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
  123. $motd .= '§'.$string;
  124. }
  125. }
  126. $motdraw = $motd;
  127. }
  128.  
  129. $motd = preg_replace("/(§.)/", "", $motd);
  130. $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd); //Remove all special characters from a string
  131.  
  132. $serverdata['version'] = $result[0];
  133. $serverdata['players'] = $result[sizeof($result)-2];
  134. $serverdata['maxplayers'] = $result[sizeof($result)-1];
  135. $serverdata['motd'] = $motd;
  136. $serverdata['motd_raw'] = $motdraw;
  137. $serverdata['ping'] = $ping;
  138.  
  139. }
  140.  
  141. $this->disconnect($socket);
  142.  
  143. return $serverdata;
  144.  
  145. }
  146.  
  147. private function connect($host, $port) {
  148. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  149. if (!@socket_connect($socket, $host, $port)) {
  150. $this->disconnect($socket);
  151. return false;
  152. }
  153. return $socket;
  154. }
  155.  
  156. private function disconnect($socket) {
  157. if($socket != null) {
  158. socket_close($socket);
  159. }
  160. }
  161.  
  162. private function read_packet_length($socket) {
  163. $a = 0;
  164. $b = 0;
  165. while(true) {
  166. $c = socket_read($socket, 1);
  167. if(!$c) {
  168. return 0;
  169. }
  170. $c = Ord($c);
  171. $a |= ($c & 0x7F) << $b++ * 7;
  172. if( $b > 5 ) {
  173. return false;
  174. }
  175. if(($c & 0x80) != 128) {
  176. break;
  177. }
  178. }
  179. return $a;
  180. }
  181.  
  182. }
  183.  
  184.  
  185. /**
  186. * Minecraft Server Status Query
  187. * @author Julian Spravil <[email protected]> https://github.com/FunnyItsElmo
  188. * @license Free to use but dont remove the author, license and copyright
  189. * @copyright © 2013 Julian Spravil
  190. */
  191. class MinecraftServerStatus {
  192.  
  193. private $timeout;
  194.  
  195. public function __construct($timeout = 2) {
  196. $this->timeout = $timeout;
  197. }
  198.  
  199. public function getStatus($host = '127.0.0.1', $port = 25565, $version = '1.7.*') {
  200.  
  201. if (substr_count($host , '.') != 4) $host = gethostbyname($host);
  202.  
  203. $serverdata = array();
  204. $serverdata['hostname'] = $host;
  205. $serverdata['version'] = false;
  206. $serverdata['protocol'] = false;
  207. $serverdata['players'] = false;
  208. $serverdata['maxplayers'] = false;
  209. $serverdata['motd'] = false;
  210. $serverdata['motd_raw'] = false;
  211. $serverdata['favicon'] = false;
  212. $serverdata['ping'] = false;
  213.  
  214. $socket = $this->connect($host, $port);
  215.  
  216. if(!$socket) {
  217. return false;
  218. }
  219.  
  220. if(preg_match('/1.7|1.8/',$version)) {
  221.  
  222. $start = microtime(true);
  223.  
  224. $handshake = pack('cccca*', hexdec(strlen($host)), 0, 0x04, strlen($host), $host).pack('nc', $port, 0x01);
  225.  
  226. socket_send($socket, $handshake, strlen($handshake), 0); //give the server a high five
  227. socket_send($socket, "\x01\x00", 2, 0);
  228. socket_read( $socket, 1 );
  229.  
  230. $ping = round((microtime(true)-$start)*1000); //calculate the high five duration
  231.  
  232. $packetlength = $this->read_packet_length($socket);
  233.  
  234. if($packetlength < 10) {
  235. return false;
  236. }
  237.  
  238. socket_read($socket, 1);
  239.  
  240. $packetlength = $this->read_packet_length($socket);
  241.  
  242. $data = socket_read($socket, $packetlength, PHP_NORMAL_READ);
  243.  
  244. if(!$data) {
  245. return false;
  246. }
  247.  
  248. $data = json_decode($data);
  249.  
  250. $serverdata['version'] = $data->version->name;
  251. $serverdata['protocol'] = $data->version->protocol;
  252. $serverdata['players'] = $data->players->online;
  253. $serverdata['maxplayers'] = $data->players->max;
  254.  
  255. $motd = $data->description;
  256. $motd = preg_replace("/(§.)/", "",$motd);
  257. $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);
  258.  
  259. $serverdata['motd'] = $motd;
  260. $serverdata['motd_raw'] = $data->description;
  261. $serverdata['favicon'] = $data->favicon;
  262. $serverdata['ping'] = $ping;
  263.  
  264. } else {
  265.  
  266. $start = microtime(true);
  267.  
  268. socket_send($socket, "\xFE\x01", 2, 0);
  269. $length = socket_recv($socket, $data, 512, 0);
  270.  
  271. $ping = round((microtime(true)-$start)*1000);//calculate the high five duration
  272.  
  273. if($length < 4 || $data[0] != "\xFF") {
  274. return false;
  275. }
  276.  
  277. $motd = "";
  278. $motdraw = "";
  279.  
  280. //Evaluate the received data
  281. if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){
  282.  
  283. $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
  284. $motd = $result[1];
  285. $motdraw = $motd;
  286.  
  287. } else {
  288.  
  289. $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
  290. foreach ($result as $key => $string) {
  291. if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
  292. $motd .= '§'.$string;
  293. }
  294. }
  295. $motdraw = $motd;
  296. }
  297.  
  298. $motd = preg_replace("/(§.)/", "", $motd);
  299. $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd); //Remove all special characters from a string
  300.  
  301. $serverdata['version'] = $result[0];
  302. $serverdata['players'] = $result[sizeof($result)-2];
  303. $serverdata['maxplayers'] = $result[sizeof($result)-1];
  304. $serverdata['motd'] = $motd;
  305. $serverdata['motd_raw'] = $motdraw;
  306. $serverdata['ping'] = $ping;
  307.  
  308. }
  309.  
  310. $this->disconnect($socket);
  311.  
  312. return $serverdata;
  313.  
  314. }
  315.  
  316. private function connect($host, $port) {
  317. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  318. if (!@socket_connect($socket, $host, $port)) {
  319. $this->disconnect($socket);
  320. return false;
  321. }
  322. return $socket;
  323. }
  324.  
  325. private function disconnect($socket) {
  326. if($socket != null) {
  327. socket_close($socket);
  328. }
  329. }
  330.  
  331. private function read_packet_length($socket) {
  332. $a = 0;
  333. $b = 0;
  334. while(true) {
  335. $c = socket_read($socket, 1);
  336. if(!$c) {
  337. return 0;
  338. }
  339. $c = Ord($c);
  340. $a |= ($c & 0x7F) << $b++ * 7;
  341. if( $b > 5 ) {
  342. return false;
  343. }
  344. if(($c & 0x80) != 128) {
  345. break;
  346. }
  347. }
  348. return $a;
  349. }
  350.  
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement