Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Console {
- public abstract function exec($command);
- }
- class Soap extends Console {
- private $uri;
- private $client;
- public function __construct($uri, $host, $port, $username, $password) {
- $this->uri = $uri;
- $this->client = new SoapClient(NULL, array(
- 'location' => 'http://'.$host.':'.$port.'/',
- 'uri' => $uri,
- 'style' => SOAP_RPC,
- 'login' => $username,
- 'password' => $password,
- 'encoding' => 'utf-8',
- 'connection_timeout' => 5
- ));
- }
- public function exec($command) {
- return $this->client->executeCommand(new SoapParam($command, 'command'));
- }
- }
- class Rcon extends Console {
- private $timeout;
- private $socket;
- private $authorized;
- private $last_response;
- const PACKET_AUTHORIZE = 5;
- const PACKET_COMMAND = 6;
- const SERVERDATA_AUTH = 3;
- const SERVERDATA_AUTH_RESPONSE = 2;
- const SERVERDATA_EXECCOMMAND = 2;
- const SERVERDATA_RESPONSE_VALUE = 0;
- public function __construct($host, $port, $password) {
- $this->password = $password;
- $this->socket = fsockopen($host, $port, $errno, $errstr, 5);
- if (!$this->socket)
- throw new Exception($errstr);
- stream_set_timeout($this->socket, 5, 0);
- $auth = $this->authorize();
- if (!$auth)
- throw new Exception('Can\'t auth');
- }
- public function exec($command) {
- $this->write_packet(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command);
- $response_packet = $this->read_packet();
- if ($response_packet['id'] == Rcon::PACKET_COMMAND && $response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) {
- $this->last_response = $response_packet['body'];
- return $response_packet['body'];
- } else {
- throw new Exception('Can\'t read response');
- }
- }
- private function authorize() {
- $this->write_packet(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password);
- $response_packet = $this->read_packet();
- if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE || $response_packet['id'] == Rcon::PACKET_AUTHORIZE) {
- $this->authorized = true;
- return true;
- }
- return false;
- }
- private function write_packet($packet_id, $packet_type, $packet_body) {
- $packet = pack("VV", $packet_id, $packet_type);
- $packet = $packet.$packet_body."\x00";
- $packet = $packet."\x00";
- $packet_size = strlen($packet);
- $packet = pack("V", $packet_size).$packet;
- fwrite($this->socket, $packet, strlen($packet));
- }
- private function read_packet()
- {
- $size_data = fread($this->socket, 4);
- $size_pack = unpack("V1size", $size_data);
- $size = $size_pack['size'];
- $packet_data = fread($this->socket, $size);
- $packet_pack = unpack("V1id/V1type/a*body", $packet_data);
- return $packet_pack;
- }
- }
- class Api {
- private $config;
- private $wowEmulators = array(
- 'trinitycore' => 'TC',
- 'skyfire' => 'SF',
- 'mangos' => 'MaNGOS',
- 'oregon' => 'Oregon',
- 'mythcore' => 'MC'
- );
- public function __construct($config) {
- $this->config = $config;
- }
- public function run($params) {
- if(!array_key_exists('command', $params) || !array_key_exists('hash', $params))
- $this->error('Bad Request');
- $command = trim(urldecode($params['command']));
- $hash = trim(urldecode($params['hash']));
- if(strtolower(md5(trim($this->config['secret']).':'.$command)) !== strtolower($hash))
- $this->error('Wrong hash');
- try {
- if($this->config['game'] == 'wow')
- $console = $this->createWowConsole();
- else if($this->config['game'] == 'minecraft')
- $console = $this->createMinecraftConsole();
- echo $console->exec($command);
- } catch(Exception $e) {
- $this->error($e->getMessage()."\n\n".$e->getTraceAsString());
- }
- }
- private function createWowConsole() {
- $uri = 'urn:'.$this->wowEmulators[$this->config['package']];
- return new Soap($uri, $this->config['host'], $this->config['port'], $this->config['username'], $this->config['password']);
- }
- private function createMinecraftConsole() {
- return new Rcon($this->config['host'], $this->config['port'], $this->config['password']);
- }
- private function error($message) {
- echo $message.' SERVERDONATE_ERROR';
- exit;
- }
- }
- header('Content-type: application/json; charset=utf-8');
- if (get_magic_quotes_gpc()) {
- $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
- while (list($key, $val) = each($process)) {
- foreach ($val as $k => $v) {
- unset($process[$key][$k]);
- if (is_array($v)) {
- $process[$key][stripslashes($k)] = $v;
- $process[] = &$process[$key][stripslashes($k)];
- } else {
- $process[$key][stripslashes($k)] = stripslashes($v);
- }
- }
- }
- unset($process);
- }
- $api = new Api($config);
- $api->run($_POST);
Add Comment
Please, Sign In to add comment