Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - <?php
 - /**
 - * Název: MobilniPlatby.cz - PHP script pro zaslání RCON příkazů na MineCraft server po odeslání prémiové SMS
 - * Autor: Mysteria - http://www.mysty.cz
 - * Verze: 1.0.0-beta2
 - * Poslední update: 27. 7. 2014
 - * Více informací & download: http://forum.fakaheda.eu/viewtopic.php?f=85&t=20682#p134652
 - */
 - $settings = (object)[ // Nastavení serverového RCONu
 - 'ip' => '93.91.250.110',
 - 'port' => 27880,
 - 'password' => '*********'
 - ];
 - $messages = (object)[ // Nastavení chybových SMS zpráv
 - 'sms_bad_price' => 'Litujeme, ale poslali jste SMS za :REALPRICE:Kc misto SMS za :PRICE: Kc.',
 - 'sms_not_found' => 'Litujeme, ale poslali jste SMS, ktera nebyla automaticky rozpoznana. Kontaktujte prosim administratory.'
 - ];
 - $levels = [ // Nastavení SMS levelů
 - [
 - 'price' => 50, // Cena SMSky
 - 'text' => 'PCDVIP :NICK:', // Text SMSky, :NICK: bude nahrazen skutečným nickem uvedeným v SMS, musí být unikátní v rámci všech platebních levelů
 - 'answer' => 'Dekujeme za zakoupeni VIP na mesic za 50Kc.', // Odpověď na správně zaslanou SMS
 - 'commands' => [ // RCON příkazy vykonané po úspěšném zpracování SMSky, :NICK: bude nahrazen skutečným nickem uvedeným v SMS
 - 'pex user :NICK: group add VIP * 2592000',
 - 'rg addmember -w Flat vipDoly :NICK:',
 - 'bc &6&lHrac&e :NICK: &6&lpodporil server koupi &eVIP &6&ona mesic'
 - ]
 - ], [
 - 'price' => 99,
 - 'text' => 'PCVIP :NICK:',
 - 'answer' => 'Dekujeme za zakoupeni VIP za 99Kc.',
 - 'commands' => [
 - 'pex user :NICK: group add VIP',
 - 'rg addmember -w Flat vipDoly :NICK:',
 - 'bc &6&lHrac&e :NICK: &6&lpodporil server koupi &eVIP'
 - ]
 - ], [
 - 'price' => 199,
 - 'text' => 'PCPLUS :NICK:',
 - 'answer' => 'Dekujeme za zakoupeni VIP+ za 199Kc.',
 - 'commands' => [
 - 'pex user :NICK: group add VIP',
 - 'rg addmember -w Flat vipDoly :NICK:',
 - 'bc &6&lHrac&e :NICK: &6&lpodporil server koupi &dVIP+'
 - ]
 - ], [
 - 'price' => 399,
 - 'text' => 'PCSPONZOR :NICK:',
 - 'answer' => 'Dekujeme za zakoupeni Sponzora za 399Kc.',
 - 'commands' => [
 - 'pex user :NICK: group add VIP',
 - 'rg addmember -w Flat vipDoly :NICK:',
 - 'bc &6&lHrac&e :NICK: &6&lse stal &5Sponzorem&6&l!!!'
 - ]
 - ]
 - ];
 - /* Do not make any changes below this line if you don't know what are you doing! */
 - $rcon = new Rcon($settings->ip, $settings->port, $settings->password, 3);
 - if (!$rcon->connect()) exit('Cannot connect to server RCON. Please check your settings.');
 - $url = (object)filter_input_array(INPUT_GET);
 - try {
 - if (isset($url->shortcode)) {
 - $url->sms = trim($url->sms);
 - if (mb_strlen($url->shortcode) === 7) {
 - $realPrice = (int)substr($url->shortcode, -2, 2);
 - foreach ($levels as $level) {
 - $level = (object)$level;
 - if ($level->price >= 100) continue;
 - $realNick = explode(' ', $url->sms)[array_search(':NICK:', explode(' ', $level->text))];
 - if (strtolower($url->sms) === strtolower(str_replace(':NICK:', $realNick, $level->text))) {
 - if ($realPrice === $level->price) {
 - foreach ($level->commands as $command) $rcon->send_command(str_replace(':NICK:', $realNick, $command));
 - File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč byla úspěšně zpracována." . PHP_EOL);
 - header('Content-type:text/plain');
 - header('Content-length:' . mb_strlen($level->answer));
 - exit($level->answer);
 - } else {
 - File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč nebyla zpracována, protože měla mít cenu {$level->price}Kč." . PHP_EOL);
 - $answer = str_replace(':PRICE:', $level->price, str_replace(':REALPRICE:', $realPrice, $messages->sms_bad_price));
 - header('Content-type:text/plain');
 - header('Content-length:' . mb_strlen($answer));
 - exit($answer);
 - }
 - }
 - }
 - File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva s obsahem '$url->sms' s cenou {$realPrice}Kč nebyla zpracována, protože nebyla rozpoznána." . PHP_EOL);
 - header('Content-type:text/plain');
 - header('Content-length:' . mb_strlen($messages->sms_not_found));
 - exit($messages->sms_not_found);
 - } else {
 - foreach ($levels as $level) {
 - $level = (object)$level;
 - if ($level->price <= 99) continue;
 - $realNick = explode(' ', $url->sms)[array_search(':NICK:', explode(' ', $level->text))];
 - if (strtolower($url->sms) === strtolower(str_replace(':NICK:', $realNick, $level->text))) {
 - $file = "./database/$url->id.txt";
 - $commands = NULL;
 - foreach ($level->commands as $command) $commands .= str_replace(':NICK:', $realNick, $command) . ';;;';
 - File::write($file, $commands);
 - $level->answer .= ";90333$level->price";
 - File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] SMS zpráva ID $url->id s obsahem '$url->sms' s cenou {$level->price}Kč byla úspěšně zpracována." . PHP_EOL);
 - header('Content-type:text/plain');
 - header('Content-length:' . mb_strlen($level->answer));
 - exit($level->answer);
 - }
 - }
 - File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva ID $url->id s obsahem '$url->sms' s cenou {$level->price}Kč nebyla zpracována, protože nebyla rozpoznána." . PHP_EOL);
 - header('Content-type:text/plain');
 - header('Content-length:' . mb_strlen($messages->sms_not_found));
 - exit("$messages->sms_not_found;FREE90333199");
 - }
 - } elseif (isset($url->request)) {
 - if ($url->status === 'DELIVERED') {
 - $file = "./database/$url->request.txt";
 - if (is_array($commands = explode(';;;', File::read($file)))) foreach ($commands as $command) $rcon->send_command($command);
 - File::remove($file);
 - File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] Doručenka SMS zprávy ID $url->request byla úspěšně zpracována." . PHP_EOL);
 - } else File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] Doručenka SMS zprávy ID $url->request nebyla zpracována, protože přišla se statusem $url->status ($url->message)." . PHP_EOL);
 - header('HTTP/1.1 204 No Content');
 - exit;
 - }
 - } catch (\FileException $e) { exit($e->getMessage()); }
 - class File {
 - public static function read($source, $mode = 'r') {
 - if (file_exists($source)) {
 - if ($file = fopen($source, $mode)) {
 - if ($content = fread($file, filesize($source))) {
 - if (fclose($file)) {
 - return $content;
 - } else throw new \FileException("Cannot close file $source.");
 - } else throw new \FileException("Cannot read file $source.");
 - } else throw new \FileException("Cannot open file $source.");
 - } else throw new \FileException("Cannot find file $source.");
 - }
 - public static function write($destination, $content, $mode = 'a') {
 - if (!is_dir(dirname($destination))) if (!mkdir(dirname($destination), 0777, TRUE)) throw new \FileException("Cannot create path $destination.");
 - if ($file = fopen($destination, $mode)) {
 - if (fwrite($file, $content)) {
 - if (fclose($file)) {
 - return TRUE;
 - } else throw new \FileException("Cannot close file $destination.");
 - } else throw new \FileException("Cannot write file $destination.");
 - } else throw new \FileException("Cannot find file $destination.");
 - }
 - public static function remove($source) {
 - if (file_exists($source)) {
 - if (unlink($source)) {
 - return TRUE;
 - } else throw new \FileException("Cannot remove file $source.");
 - } else throw new \FileException("Cannot find file $source.");
 - }
 - }
 - class FileException extends \Exception { }
 - class Rcon {
 - private $host;
 - private $port;
 - private $password;
 - 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, $timeout) {
 - $this->host = $host;
 - $this->port = $port;
 - $this->password = $password;
 - $this->timeout = $timeout;
 - }
 - public function get_response() {
 - return $this->last_response;
 - }
 - public function connect() {
 - $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
 - if (!$this->socket) {
 - $this->last_response = $errstr;
 - return false;
 - }
 - //set timeout
 - stream_set_timeout($this->socket, 3, 0);
 - //authorize
 - $auth = $this->authorize();
 - if ($auth) {
 - return true;
 - }
 - return false;
 - }
 - public function disconnect() {
 - if ($this->socket) {
 - fclose($this->socket);
 - }
 - }
 - public function is_connected() {
 - return $this->authorized;
 - }
 - public function send_command($command) {
 - if (!$this->is_connected()) return false;
 - // send command packet.
 - $this->write_packet(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command);
 - // get response.
 - $response_packet = $this->read_packet();
 - if ($response_packet['id'] == Rcon::PACKET_COMMAND) {
 - if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) {
 - $this->last_response = $response_packet['body'];
 - return $response_packet['body'];
 - }
 - }
 - return false;
 - }
 - 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) {
 - if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) {
 - $this->authorized = true;
 - return true;
 - }
 - }
 - $this->disconnect();
 - return false;
 - }
 - /**
 - * Writes a packet to the socket stream..
 - */
 - private function write_packet($packet_id, $packet_type, $packet_body) {
 - /*
 - Size 32-bit little-endian Signed Integer Varies, see below.
 - ID 32-bit little-endian Signed Integer Varies, see below.
 - Type 32-bit little-endian Signed Integer Varies, see below.
 - Body Null-terminated ASCII String Varies, see below.
 - Empty String Null-terminated ASCII String 0x00
 - */
 - //create packet
 - $packet = pack("VV", $packet_id, $packet_type);
 - $packet = $packet . $packet_body . "\x00";
 - $packet = $packet . "\x00";
 - // get packet size.
 - $packet_size = strlen($packet);
 - // attach size to packet.
 - $packet = pack("V", $packet_size) . $packet;
 - // write packet.
 - fwrite($this->socket, $packet, strlen($packet));
 - }
 - private function read_packet() {
 - //get packet size.
 - $size_data = fread($this->socket, 4);
 - $size_pack = unpack("V1size", $size_data);
 - $size = $size_pack['size'];
 - // if size is > 4096, the response will be in multiple packets.
 - // this needs to be address. get more info about multi-packet responses
 - // from the RCON protocol specification at
 - // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
 - // currently, this script does not support multi-packet responses.
 - $packet_data = fread($this->socket, $size);
 - $packet_pack = unpack("V1id/V1type/a*body", $packet_data);
 - return $packet_pack;
 - }
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment