Unsupported PHP app for analysing and displaying stats for Team Fortress 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.class.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?PHP
  2. class Server {
  3. private $host, $port;
  4. private $socket;
  5. public function __construct($host, $port) {
  6. $this->host = $host;
  7. $this->port = $port;
  8. $this->socket = fsockopen('udp://' . $this->host, $this->port);
  9. stream_set_timeout($this->socket, 5);
  10. }
  11. public function getInfo() {
  12. $this->send(pack('LAA*x', -1, 'T', 'Source Engine Query'));
  13. return $this->receiveInfo();
  14. }
  15. private function send($data) {
  16. fwrite($this->socket, $data);
  17. }
  18. private function readUntilNull() {
  19. do {
  20. $data .= $read = fread($this->socket, 1);
  21. $info = stream_get_meta_data($this->socket);
  22. if ($info['timed_out']) {
  23. throw new Exception('Socket time out');
  24. }
  25. } while ($read !== false && $read !== "\0");
  26. return $data;
  27. }
  28. private function receiveInfo() {
  29. $res = unpack('Lheader/ctype/cversion/a*name', $this->readUntilNull());
  30. $res = array_merge($res, unpack('a*map', $this->readUntilNull()));
  31. $res = array_merge($res, unpack('a*game_dir', $this->readUntilNull()));
  32. $res = array_merge($res, unpack('a*game_name', $this->readUntilNull()));
  33. $res = array_merge($res, unpack('vappid/cplayers/cmaxplayers/cbots/cdedicated/cos/cpassword/csecure', fread($this->socket, 9)));
  34. $res = array_merge($res, unpack('a*game_version', $this->readUntilNull()));
  35. return $res;
  36. }
  37. private function hexdump($data) {
  38. $data = unpack('H*', $data);
  39. $data = $data[1];
  40. for ($i = 0; $i < strlen($data); $i += 2) {
  41. if ($i % 32 == 0) { echo "\n"; } else if ($i % 32 == 0) { echo ' '; }
  42. echo substr($data, $i, 2) . ' ';
  43. }
  44. echo "\n";
  45. }
  46. }
  47. ?>