ivmpquery.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. // Class by Boylett
  3. // Released under GNU/GPL.
  4. // Designed for IVMP 0.1 Beta T4
  5. class IVMPQuery
  6. {
  7. var $socket = false;
  8. var $ip = false;
  9. var $port = false;
  10. function Query($ip,$port,&$errno,&$errstr,$timeout = 5,$gettimeout = 1)
  11. {
  12. $this->Close();
  13. $this->ip = $ip;
  14. $this->port = $port;
  15. $this->socket = @fsockopen('udp://'.$ip,$port,$errno,$errstr,$timeout);
  16. if($this->socket === false) return false;
  17. @stream_set_timeout($this->socket,$gettimeout);
  18. return true;
  19. }
  20. function Close()
  21. {
  22. if($this->socket !== false)
  23. {
  24. fclose($this->socket);
  25. $this->socket = false;
  26. }
  27. }
  28. function GetPacketData($command)
  29. {
  30. $packet = 'IVMP';
  31. $packet .= $command;
  32. return $packet;
  33. }
  34. function ServerData()
  35. {
  36. fputs($this->socket,$this->GetPacketData('i'));
  37. @fread($this->socket,5); // IVMPi
  38. $len = ord(@fread($this->socket,4));
  39. $hostname = @fread($this->socket,$len); // read hostname
  40. $players = ord(@fread($this->socket,4)); // read players
  41. $maxplayers = ord(@fread($this->socket,4)); // read max players
  42. $passworded = ord(@fread($this->socket,1)); // 1 byte for password
  43. return array(
  44. 'hostname' => $hostname,
  45. 'players' => $players,
  46. 'maxplayers' => $maxplayers,
  47. 'passworded' => (bool)$passworded
  48. );
  49. }
  50. function Players()
  51. {
  52. fputs($this->socket,$this->GetPacketData('l'));
  53. @fread($this->socket,5); // IVMPl
  54. $count = ord(@fread($this->socket,4));
  55. $arr = array();
  56. for($i = 0; $i < $count; $i++)
  57. {
  58. $id = ord(@fread($this->socket,4));
  59. $len = ord(@fread($this->socket,4));
  60. $arr[$id] = @fread($this->socket,$len);
  61. }
  62. return $arr;
  63. }
  64. }
  65. ?>