Guest User

Untitled

a guest
Mar 30th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. abstract class Console {
  2.     public abstract function exec($command);
  3. }
  4.  
  5. class Soap extends Console {
  6.     private $uri;
  7.     private $client;
  8.    
  9.     public function __construct($uri, $host, $port, $username, $password) {
  10.         $this->uri = $uri;
  11.         $this->client = new SoapClient(NULL, array(
  12.             'location' => 'http://'.$host.':'.$port.'/',
  13.             'uri' => $uri,
  14.             'style' => SOAP_RPC,
  15.             'login' => $username,
  16.             'password' => $password,
  17.             'encoding' => 'utf-8',
  18.             'connection_timeout' => 5
  19.         ));
  20.     }
  21.  
  22.     public function exec($command) {
  23.         return $this->client->executeCommand(new SoapParam($command, 'command'));
  24.     }
  25. }
  26.  
  27. class Rcon extends Console {
  28.     private $timeout;
  29.  
  30.     private $socket;
  31.  
  32.     private $authorized;
  33.     private $last_response;
  34.  
  35.     const PACKET_AUTHORIZE = 5;
  36.     const PACKET_COMMAND = 6;
  37.  
  38.     const SERVERDATA_AUTH = 3;
  39.     const SERVERDATA_AUTH_RESPONSE = 2;
  40.     const SERVERDATA_EXECCOMMAND = 2;
  41.     const SERVERDATA_RESPONSE_VALUE = 0;
  42.  
  43.     public function __construct($host, $port, $password) {
  44.         $this->password = $password;
  45.         $this->socket = fsockopen($host, $port, $errno, $errstr, 5);
  46.  
  47.         if (!$this->socket)
  48.             throw new Exception($errstr);
  49.  
  50.         stream_set_timeout($this->socket, 5, 0);
  51.         $auth = $this->authorize();
  52.  
  53.         if (!$auth)
  54.             throw new Exception('Can\'t auth');
  55.     }
  56.  
  57.     public function exec($command) {
  58.         $this->write_packet(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command);
  59.  
  60.         $response_packet = $this->read_packet();
  61.         if ($response_packet['id'] == Rcon::PACKET_COMMAND && $response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) {
  62.             $this->last_response = $response_packet['body'];
  63.             return $response_packet['body'];
  64.         } else {
  65.             throw new Exception('Can\'t read response');
  66.         }
  67.     }
  68.  
  69.     private function authorize() {
  70.         $this->write_packet(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password);
  71.         $response_packet = $this->read_packet();
  72.  
  73.         if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE || $response_packet['id'] == Rcon::PACKET_AUTHORIZE) {
  74.             $this->authorized = true;
  75.             return true;
  76.         }
  77.  
  78.         return false;
  79.     }
  80.  
  81.     private function write_packet($packet_id, $packet_type, $packet_body) {
  82.         $packet = pack("VV", $packet_id, $packet_type);
  83.         $packet = $packet.$packet_body."\x00";
  84.         $packet = $packet."\x00";
  85.         $packet_size = strlen($packet);
  86.         $packet = pack("V", $packet_size).$packet;
  87.         fwrite($this->socket, $packet, strlen($packet));
  88.     }
  89.  
  90.     private function read_packet()
  91.     {
  92.         $size_data = fread($this->socket, 4);
  93.         $size_pack = unpack("V1size", $size_data);
  94.         $size = $size_pack['size'];
  95.  
  96.         $packet_data = fread($this->socket, $size);
  97.         $packet_pack = unpack("V1id/V1type/a*body", $packet_data);
  98.  
  99.         return $packet_pack;
  100.     }
  101. }
  102.  
  103. class Api {
  104.     private $config;
  105.     private $wowEmulators = array(
  106.         'trinitycore' => 'TC',
  107.         'skyfire' => 'SF',
  108.         'mangos' => 'MaNGOS',
  109.         'oregon' => 'Oregon',
  110.         'mythcore' => 'MC'
  111.     );
  112.    
  113.     public function __construct($config) {
  114.         $this->config = $config;
  115.     }
  116.    
  117.     public function run($params) {
  118.         if(!array_key_exists('command', $params) || !array_key_exists('hash', $params))
  119.             $this->error('Bad Request');
  120.        
  121.         $command = trim(urldecode($params['command']));
  122.         $hash = trim(urldecode($params['hash']));
  123.  
  124.         if(strtolower(md5(trim($this->config['secret']).':'.$command)) !== strtolower($hash))
  125.             $this->error('Wrong hash');
  126.        
  127.         try {
  128.             if($this->config['game'] == 'wow')
  129.                 $console = $this->createWowConsole();
  130.             else if($this->config['game'] == 'minecraft')
  131.                 $console = $this->createMinecraftConsole();
  132.        
  133.             echo $console->exec($command);
  134.         } catch(Exception $e) {
  135.             $this->error($e->getMessage()."\n\n".$e->getTraceAsString());
  136.         }
  137.     }
  138.    
  139.     private function createWowConsole() {
  140.         $uri = 'urn:'.$this->wowEmulators[$this->config['package']];
  141.        
  142.         return new Soap($uri, $this->config['host'], $this->config['port'], $this->config['username'], $this->config['password']);
  143.     }
  144.    
  145.     private function createMinecraftConsole() {
  146.         return new Rcon($this->config['host'], $this->config['port'], $this->config['password']);
  147.     }
  148.    
  149.     private function error($message) {
  150.         echo $message.' SERVERDONATE_ERROR';
  151.         exit;
  152.     }
  153. }
  154.  
  155. header('Content-type: application/json; charset=utf-8');
  156.  
  157. if (get_magic_quotes_gpc()) {
  158.     $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  159.     while (list($key, $val) = each($process)) {
  160.         foreach ($val as $k => $v) {
  161.             unset($process[$key][$k]);
  162.             if (is_array($v)) {
  163.                 $process[$key][stripslashes($k)] = $v;
  164.                 $process[] = &$process[$key][stripslashes($k)];
  165.             } else {
  166.                 $process[$key][stripslashes($k)] = stripslashes($v);
  167.             }
  168.         }
  169.     }
  170.     unset($process);
  171. }
  172.  
  173. $api = new Api($config);
  174. $api->run($_POST);
Add Comment
Please, Sign In to add comment