Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $settings = (object)[
- 'ip' => '151.80.47.171',
- 'port' => 25575,
- 'password' => 'acsforever'
- ];
- $messages = (object)[
- 'sms_bad_price' => 'Litujeme, ale poslali jste platnou sms na spatne cislo, prekontrolujte udaje.;FREE6674',
- 'sms_not_found' => 'Litujeme, ale poslali jste neplatnou sms, prosime zkontrolujte si tvar sms.;FREE6674'
- ];
- $levels = (object) [
- [
- 'price' => 3,
- 'answer' => 'Dekujeme za zakoupeni IronVIP za 3 eura, Tve vyhody budou automaticky aktivovany do 5 min. s pozdravem Terranet.cz;88770300',
- 'type' => 'IVIP',
- 'commands' => [
- 'pex user :NICK: group set VIP',
- 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &f&lIronVIP&d&l, Děkujeme',
- 'case givekey :NICK: VIP 1',
- 'eco give :NICK: 10000'
- ]
- ], [
- 'price' => 4,
- 'answer' => 'Dekujeme za zakoupeni SuperBedny za 4 eura. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770400',
- 'type' => 'BEDNA',
- 'commands' => [
- 'case givekey :NICK: SuperBedna 1',
- 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil SuperBednu, Děkujeme'
- ]
- ], [
- 'price' => 2,
- 'answer' => 'Dekujeme za zakoupeni IronVIP za 2 eura na 30 dni. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770200',
- 'type' => 'TIVIP',
- 'commands' => [
- 'pex user :NICK: group add VIP "" 2628000',
- 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &f&lIronVIP&d&l na 30 dni, Děkujeme',
- 'case givekey :NICK: VIP 1',
- 'eco give :NICK: 10000'
- ]
- ], [
- 'price' => 4,
- 'answer' => 'Dekujeme za zakoupeni GoldVIP za 4 eur na 30 dni. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770400',
- 'type' => 'TGVIP',
- 'commands' => [
- 'pex user :NICK: group add EVIP "" 2628000',
- 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &6&lGoldVIP&d&l na 30 dni, Děkujeme',
- 'case givekey :NICK: VIP 1',
- 'eco give :NICK: 25000'
- ]
- ], [
- 'price' => 6,
- 'answer' => 'Dekujeme za zakoupeni DiamondVIP za 6 eur na 30 dni. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770600',
- 'type' => 'TDVIP',
- 'commands' => [
- 'pex user :NICK: group add GM "" 2628000',
- 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &b&lDiamondVIP&d&l na 30 dni, Děkujeme',
- 'case givekey :NICK: VIP 1',
- 'eco give :NICK: 50000'
- ]
- ], [
- 'price' => 6,
- 'answer' => 'Dekujeme za zakoupeni GoldVIP za 6 eur. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770600',
- 'type' => 'GVIP',
- 'commands' => [
- 'pex user :NICK: group set EVIP',
- 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &6&lGoldVIP&d&l, Děkujeme',
- 'case givekey :NICK: VIP 1',
- 'eco give :NICK: 25000'
- ]
- ], [
- 'price' => 8,
- 'answer' => 'Dekujeme za zakoupeni DiamondVIP za 8 Eur, Tve vyhody budou automaticky aktivovany do 5 min. s pozdravem Terranet.cz;88770800',
- 'type' => 'DVIP',
- 'commands' => [
- 'pex user :NICK: group set GM',
- 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &b&lDiamondVIPd&l, Děkujeme',
- 'case givekey :NICK: VIP 1',
- 'eco give :NICK: 50000'
- ]
- ]
- ];
- /* 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);
- File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [LOG] SMS zpráva s obsahem " . json_encode($url) . PHP_EOL);
- try {
- if (isset($url->shortcode)) {
- $url->sms = trim($url->sms);
- // {"timestamp":"2017-03-13T16:03:52.081","phone":"421776789541","sms":"PM TERRASK VIP NICK","shortcode":"88770200","country":"SK","operator":"ORANGE"}
- $pattern = "/PM TERRASK (\d+)\s(.*?)\s(.*)/i";
- if(preg_match($pattern, $url->sms, $matches)) {
- $realPrice = intval($matches[1]);
- $vipType = $matches[2];
- $nick = $matches[3];
- File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [LOG] Cena: $realPrice Typ: $vipType Nick: $nick" . PHP_EOL);
- foreach ($levels as $level) {
- $price = $level['price'];
- $error = "";
- $foundType = false;
- $foundPrice = false;
- if(strcasecmp($vipType, $level['type']) == 0) {
- $foundType = true;
- }
- if($price === $realPrice) {
- $foundPrice = true;
- }
- if(!$foundPrice) {
- $error = $messages->sms_bad_price;
- }
- if(!$foundType) {
- $error = $messages->sms_not_found;
- }
- if($foundType && $foundPrice) {
- foreach ($level['commands'] as $command) $rcon->send_command(str_replace(':NICK:', $nick, $command));
- File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] SMS zpráva s obsahem '$url->sms' s cenou {$price}€ byla úspěšně zpracována.;" . $url->shortcode . PHP_EOL);
- File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] Odpověd: " . $level['answer'] . PHP_EOL);
- header('Content-type:text/plain');
- header('Content-length:' . mb_strlen($level['answer']));
- exit($level['answer']);
- } else if($foundType || $foundPrice) {
- File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => DRUH NEBYL NALEZEN" . PHP_EOL);
- header('Content-type:text/plain');
- header('Content-length:' . mb_strlen($error));
- exit($error);
- }
- }
- File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] Při zpracování SMS '$url->sms' došlo k chybě. Nebyl nalezen správný TYP." . PHP_EOL);
- header('Content-type:text/plain');
- header('Content-length:' . mb_strlen($messages->sms_not_found));
- exit("$messages->sms_not_found");
- } else {
- File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [!ERROR!] SMS zpráva ID $url->id s obsahem '$url->sms' 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");
- }
- } 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
Advertisement