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.

rcon.class.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?PHP
  2. class Rcon {
  3. const SERVERDATA_AUTH = 3;
  4. const SERVERDATA_EXECCOMMAND = 2;
  5. private $host, $port, $password;
  6. private $request = 0;
  7. private $socket;
  8. public function __construct($host, $port, $password) {
  9. $this->host = $host;
  10. $this->port = $port;
  11. $this->password = $password;
  12. if (($this->socket = fsockopen($this->host, $this->port)) === false) {
  13. die("Unable to create RCON connection to server!\n");
  14. }
  15. $this->send(self::SERVERDATA_AUTH, $password);
  16. $this->receive();
  17. }
  18. public function getStatus() {
  19. $this->send(self::SERVERDATA_EXECCOMMAND, 'status');
  20. $data = $this->receive();
  21. return $data['string1'];
  22. }
  23. public function execute($command) {
  24. $this->send(self::SERVERDATA_EXECCOMMAND, $command);
  25. }
  26. private function send($type, $string1, $string2 = "") {
  27. $data = pack('V3a' . (strlen($string1) + 1) . 'a' . (strlen($string2) + 1),
  28. strlen($string1 . $string2) + 10, ++$this->request, $type, $string1, $string2);
  29. fputs($this->socket, $data);
  30. }
  31. private function receive() {
  32. $size = unpack('V', fread($this->socket, 4));
  33. $size = $size[1];
  34. $data = fread($this->socket, $size);
  35. $data = unpack('Vid/Vresponse/a*string1/a*string2', $data);
  36. if ($data['id'] == -1) {
  37. throw new Exception('Authentication failed');
  38. } else if ($data['id'] < $this->request) {
  39. return $this->receive();
  40. }
  41. return $data;
  42. }
  43. private function hexdump($data) {
  44. $data = unpack('H*', $data);
  45. $data = $data[1];
  46. for ($i = 0; $i < strlen($data); $i += 2) {
  47. if ($i % 32 == 0) { echo "\n"; } else if ($i % 32 == 0) { echo ' '; }
  48. echo substr($data, $i, 2) . ' ';
  49. }
  50. echo "\n";
  51. }
  52. }
  53. ?>