PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/banned/include/steam_condenser/steam/servers/SourceServer.php

http://globalban-spanish.googlecode.com/
PHP | 92 lines | 50 code | 14 blank | 28 comment | 2 complexity | 00ab66ac88988bdc453946bcf232b2fc MD5 | raw file
  1. <?php
  2. /**
  3. * This code is free software; you can redistribute it and/or modify it under
  4. * the terms of the new BSD License.
  5. *
  6. * Copyright (c) 2008-2011, Sebastian Staudt
  7. *
  8. * @author Sebastian Staudt
  9. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  10. * @package Steam Condenser (PHP)
  11. * @subpackage SourceServer
  12. */
  13. require_once STEAM_CONDENSER_PATH . 'InetAddress.php';
  14. require_once STEAM_CONDENSER_PATH . 'exceptions/RCONNoAuthException.php';
  15. require_once STEAM_CONDENSER_PATH . 'steam/packets/rcon/RCONAuthRequest.php';
  16. require_once STEAM_CONDENSER_PATH . 'steam/packets/rcon/RCONAuthResponse.php';
  17. require_once STEAM_CONDENSER_PATH . 'steam/packets/rcon/RCONExecRequest.php';
  18. require_once STEAM_CONDENSER_PATH . 'steam/servers/GameServer.php';
  19. require_once STEAM_CONDENSER_PATH . 'steam/sockets/RCONSocket.php';
  20. require_once STEAM_CONDENSER_PATH . 'steam/sockets/SourceSocket.php';
  21. /**
  22. * @package Steam Condenser (PHP)
  23. * @subpackage SourceServer
  24. * @todo Server has to recognize incoming packets
  25. */
  26. class SourceServer extends GameServer
  27. {
  28. /**
  29. * @var long
  30. */
  31. private $rconRequestId;
  32. /**
  33. * Splits the player status obtained with "rcon status"
  34. * @param String $playerStatus
  35. * @return String[]
  36. */
  37. public function splitPlayerStatus($playerStatus) {
  38. preg_match('%# *(\d+) +"(.*)" +(.*)%', $playerStatus, $playerData);
  39. array_shift($playerData);
  40. $morePlayerData = preg_split('/\s+/', array_pop($playerData));
  41. $playerData = array_merge($playerData, $morePlayerData);
  42. unset($playerData[3]);
  43. return array_values($playerData);
  44. }
  45. /**
  46. * @param InetAddress $serverIP
  47. * @param int $portNumber The listening port of the server, defaults to 27015
  48. */
  49. public function __construct(InetAddress $ipAddress, $portNumber = 27015) {
  50. parent::__construct($portNumber);
  51. $this->rconSocket = new RCONSocket($ipAddress, $portNumber);
  52. $this->socket = new SourceSocket($ipAddress, $portNumber);
  53. }
  54. public function rconAuth($password) {
  55. $this->rconRequestId = rand(0, pow(2, 16));
  56. $this->rconSocket->send(new RCONAuthRequest($this->rconRequestId, $password));
  57. $this->rconSocket->getReply();
  58. $reply = $this->rconSocket->getReply();
  59. return $reply->getRequestId() == $this->rconRequestId;
  60. }
  61. public function rconExec($command) {
  62. $this->rconSocket->send(new RCONExecRequest($this->rconRequestId, $command));
  63. $this->rconSocket->send(new RCONExecRequest($this->rconRequestId, null));
  64. do {
  65. $responsePacket = $this->rconSocket->getReply();
  66. if($responsePacket instanceof RCONAuthResponse) {
  67. throw new RCONNoAuthException();
  68. }
  69. $responsePackets[] = $responsePacket;
  70. } while(strlen($responsePacket->getResponse()) > 0);
  71. $response = '';
  72. foreach($responsePackets as $packet) {
  73. $response .= $packet->getResponse();
  74. }
  75. return trim($response);
  76. }
  77. }
  78. ?>