Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.86 KB | None | 0 0
  1. $settings = (object)[
  2. 'ip' => '151.80.47.171',
  3. 'port' => 25575,
  4. 'password' => 'acsforever'
  5. ];
  6.  
  7. $messages = (object)[
  8. 'sms_bad_price' => 'Litujeme, ale poslali jste platnou sms na spatne cislo, prekontrolujte udaje.;FREE6674',
  9. 'sms_not_found' => 'Litujeme, ale poslali jste neplatnou sms, prosime zkontrolujte si tvar sms.;FREE6674'
  10. ];
  11.  
  12. $levels = (object) [
  13. [
  14. 'price' => 3,
  15. 'answer' => 'Dekujeme za zakoupeni IronVIP za 3 eura, Tve vyhody budou automaticky aktivovany do 5 min. s pozdravem Terranet.cz;88770300',
  16. 'type' => 'IVIP',
  17. 'commands' => [
  18. 'pex user :NICK: group set VIP',
  19. 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &f&lIronVIP&d&l, Děkujeme',
  20. 'case givekey :NICK: VIP 1',
  21. 'eco give :NICK: 10000'
  22. ]
  23. ], [
  24. 'price' => 4,
  25. 'answer' => 'Dekujeme za zakoupeni SuperBedny za 4 eura. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770400',
  26. 'type' => 'BEDNA',
  27. 'commands' => [
  28. 'case givekey :NICK: SuperBedna 1',
  29. 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil SuperBednu, Děkujeme'
  30. ]
  31. ], [
  32. 'price' => 2,
  33. 'answer' => 'Dekujeme za zakoupeni IronVIP za 2 eura na 30 dni. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770200',
  34. 'type' => 'TIVIP',
  35. 'commands' => [
  36. 'pex user :NICK: group add VIP "" 2628000',
  37. 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &f&lIronVIP&d&l na 30 dni, Děkujeme',
  38. 'case givekey :NICK: VIP 1',
  39. 'eco give :NICK: 10000'
  40. ]
  41. ], [
  42. 'price' => 4,
  43. 'answer' => 'Dekujeme za zakoupeni GoldVIP za 4 eur na 30 dni. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770400',
  44. 'type' => 'TGVIP',
  45. 'commands' => [
  46. 'pex user :NICK: group add EVIP "" 2628000',
  47. 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &6&lGoldVIP&d&l na 30 dni, Děkujeme',
  48. 'case givekey :NICK: VIP 1',
  49. 'eco give :NICK: 25000'
  50. ]
  51. ], [
  52. 'price' => 6,
  53. 'answer' => 'Dekujeme za zakoupeni DiamondVIP za 6 eur na 30 dni. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770600',
  54. 'type' => 'TDVIP',
  55. 'commands' => [
  56. 'pex user :NICK: group add GM "" 2628000',
  57. 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &b&lDiamondVIP&d&l na 30 dni, Děkujeme',
  58. 'case givekey :NICK: VIP 1',
  59. 'eco give :NICK: 50000'
  60. ]
  61. ], [
  62. 'price' => 6,
  63. 'answer' => 'Dekujeme za zakoupeni GoldVIP za 6 eur. Tve vyhody budou brzo aktivovany s pozdravem Terranet.cz;88770600',
  64. 'type' => 'GVIP',
  65. 'commands' => [
  66. 'pex user :NICK: group set EVIP',
  67. 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &6&lGoldVIP&d&l, Děkujeme',
  68. 'case givekey :NICK: VIP 1',
  69. 'eco give :NICK: 25000'
  70. ]
  71. ], [
  72. 'price' => 8,
  73. 'answer' => 'Dekujeme za zakoupeni DiamondVIP za 8 Eur, Tve vyhody budou automaticky aktivovany do 5 min. s pozdravem Terranet.cz;88770800',
  74. 'type' => 'DVIP',
  75. 'commands' => [
  76. 'pex user :NICK: group set GM',
  77. 'broadcast &d&lHráč &3&l:NICK: &d&lsi zakoupil &b&lDiamondVIPd&l, Děkujeme',
  78. 'case givekey :NICK: VIP 1',
  79. 'eco give :NICK: 50000'
  80. ]
  81. ]
  82. ];
  83.  
  84. /* Do not make any changes below this line if you don't know what are you doing! */
  85. $rcon = new Rcon($settings->ip, $settings->port, $settings->password, 3);
  86. if (!$rcon->connect()) exit('Cannot connect to server RCON. Please check your settings.');
  87. $url = (object)filter_input_array(INPUT_GET);
  88. File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [LOG] SMS zpráva s obsahem " . json_encode($url) . PHP_EOL);
  89. try {
  90. if (isset($url->shortcode)) {
  91. $url->sms = trim($url->sms);
  92.  
  93. // {"timestamp":"2017-03-13T16:03:52.081","phone":"421776789541","sms":"PM TERRASK VIP NICK","shortcode":"88770200","country":"SK","operator":"ORANGE"}
  94. $pattern = "/PM TERRASK (\d+)\s(.*?)\s(.*)/i";
  95. if(preg_match($pattern, $url->sms, $matches)) {
  96. $realPrice = intval($matches[1]);
  97. $vipType = $matches[2];
  98. $nick = $matches[3];
  99.  
  100. File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [LOG] Cena: $realPrice Typ: $vipType Nick: $nick" . PHP_EOL);
  101.  
  102. foreach ($levels as $level) {
  103. $price = $level['price'];
  104. $error = "";
  105. $foundType = false;
  106. $foundPrice = false;
  107.  
  108. if(strcasecmp($vipType, $level['type']) == 0) {
  109. $foundType = true;
  110. }
  111.  
  112. if($price === $realPrice) {
  113. $foundPrice = true;
  114. }
  115.  
  116. if(!$foundPrice) {
  117. $error = $messages->sms_bad_price;
  118. }
  119.  
  120. if(!$foundType) {
  121. $error = $messages->sms_not_found;
  122. }
  123.  
  124. if($foundType && $foundPrice) {
  125. foreach ($level['commands'] as $command) $rcon->send_command(str_replace(':NICK:', $nick, $command));
  126. 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);
  127. File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => [SUCCESS] Odpověd: " . $level['answer'] . PHP_EOL);
  128. header('Content-type:text/plain');
  129. header('Content-length:' . mb_strlen($level['answer']));
  130. exit($level['answer']);
  131. } else if($foundType || $foundPrice) {
  132. File::write('./mobilni-platby.log', date('d. m. Y H:i:s', time()) . " => DRUH NEBYL NALEZEN" . PHP_EOL);
  133. header('Content-type:text/plain');
  134. header('Content-length:' . mb_strlen($error));
  135. exit($error);
  136. }
  137. }
  138.  
  139. 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);
  140. header('Content-type:text/plain');
  141. header('Content-length:' . mb_strlen($messages->sms_not_found));
  142. exit("$messages->sms_not_found");
  143.  
  144.  
  145. } else {
  146. 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);
  147. header('Content-type:text/plain');
  148. header('Content-length:' . mb_strlen($messages->sms_not_found));
  149. exit("$messages->sms_not_found");
  150. }
  151. } elseif (isset($url->request)) {
  152. if ($url->status === 'DELIVERED') {
  153. $file = "./database/$url->request.txt";
  154. if (is_array($commands = explode(';;;', File::read($file)))) foreach ($commands as $command) $rcon->send_command($command);
  155. File::remove($file);
  156. 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);
  157. } 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);
  158. header('HTTP/1.1 204 No Content');
  159. exit;
  160. }
  161. } catch (\FileException $e) { exit($e->getMessage()); }
  162. class File {
  163. public static function read($source, $mode = 'r') {
  164. if (file_exists($source)) {
  165. if ($file = fopen($source, $mode)) {
  166. if ($content = fread($file, filesize($source))) {
  167. if (fclose($file)) {
  168. return $content;
  169. } else throw new \FileException("Cannot close file $source.");
  170. } else throw new \FileException("Cannot read file $source.");
  171. } else throw new \FileException("Cannot open file $source.");
  172. } else throw new \FileException("Cannot find file $source.");
  173. }
  174.  
  175. public static function write($destination, $content, $mode = 'a') {
  176. if (!is_dir(dirname($destination))) if (!mkdir(dirname($destination), 0777, TRUE)) throw new \FileException("Cannot create path $destination.");
  177. if ($file = fopen($destination, $mode)) {
  178. if (fwrite($file, $content)) {
  179. if (fclose($file)) {
  180. return TRUE;
  181. } else throw new \FileException("Cannot close file $destination.");
  182. } else throw new \FileException("Cannot write file $destination.");
  183. } else throw new \FileException("Cannot find file $destination.");
  184. }
  185.  
  186. public static function remove($source) {
  187. if (file_exists($source)) {
  188. if (unlink($source)) {
  189. return TRUE;
  190. } else throw new \FileException("Cannot remove file $source.");
  191. } else throw new \FileException("Cannot find file $source.");
  192. }
  193. }
  194. class FileException extends \Exception { }
  195. class Rcon {
  196. private $host;
  197. private $port;
  198. private $password;
  199. private $timeout;
  200. private $socket;
  201. private $authorized;
  202. private $last_response;
  203.  
  204. const PACKET_AUTHORIZE = 5;
  205. const PACKET_COMMAND = 6;
  206. const SERVERDATA_AUTH = 3;
  207. const SERVERDATA_AUTH_RESPONSE = 2;
  208. const SERVERDATA_EXECCOMMAND = 2;
  209. const SERVERDATA_RESPONSE_VALUE = 0;
  210.  
  211. public function __construct($host, $port, $password, $timeout) {
  212. $this->host = $host;
  213. $this->port = $port;
  214. $this->password = $password;
  215. $this->timeout = $timeout;
  216. }
  217.  
  218. public function get_response() {
  219. return $this->last_response;
  220. }
  221.  
  222. public function connect() {
  223.  
  224. $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  225.  
  226. if (!$this->socket) {
  227. $this->last_response = $errstr;
  228. return false;
  229. }
  230.  
  231. //set timeout
  232. stream_set_timeout($this->socket, 3, 0);
  233.  
  234. //authorize
  235. $auth = $this->authorize();
  236.  
  237. if ($auth) {
  238. return true;
  239. }
  240.  
  241. return false;
  242. }
  243.  
  244. public function disconnect() {
  245. if ($this->socket) {
  246. fclose($this->socket);
  247. }
  248. }
  249.  
  250. public function is_connected() {
  251. return $this->authorized;
  252. }
  253.  
  254. public function send_command($command) {
  255. if (!$this->is_connected()) return false;
  256.  
  257. // send command packet.
  258. $this->write_packet(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command);
  259.  
  260. // get response.
  261. $response_packet = $this->read_packet();
  262. if ($response_packet['id'] == Rcon::PACKET_COMMAND) {
  263. if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) {
  264. $this->last_response = $response_packet['body'];
  265. return $response_packet['body'];
  266. }
  267. }
  268.  
  269. return false;
  270. }
  271.  
  272. private function authorize() {
  273. $this->write_packet(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password);
  274. $response_packet = $this->read_packet();
  275.  
  276. if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE) {
  277. if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) {
  278. $this->authorized = true;
  279. return true;
  280. }
  281. }
  282.  
  283. $this->disconnect();
  284. return false;
  285. }
  286.  
  287. /**
  288. * Writes a packet to the socket stream..
  289. */
  290. private function write_packet($packet_id, $packet_type, $packet_body) {
  291. /*
  292. Size 32-bit little-endian Signed Integer Varies, see below.
  293. ID 32-bit little-endian Signed Integer Varies, see below.
  294. Type 32-bit little-endian Signed Integer Varies, see below.
  295. Body Null-terminated ASCII String Varies, see below.
  296. Empty String Null-terminated ASCII String 0x00
  297. */
  298.  
  299. //create packet
  300. $packet = pack("VV", $packet_id, $packet_type);
  301. $packet = $packet . $packet_body . "\x00";
  302. $packet = $packet . "\x00";
  303.  
  304. // get packet size.
  305. $packet_size = strlen($packet);
  306.  
  307. // attach size to packet.
  308. $packet = pack("V", $packet_size) . $packet;
  309.  
  310. // write packet.
  311. fwrite($this->socket, $packet, strlen($packet));
  312. }
  313.  
  314. private function read_packet() {
  315. //get packet size.
  316. $size_data = fread($this->socket, 4);
  317. $size_pack = unpack("V1size", $size_data);
  318. $size = $size_pack['size'];
  319.  
  320. // if size is > 4096, the response will be in multiple packets.
  321. // this needs to be address. get more info about multi-packet responses
  322. // from the RCON protocol specification at
  323. // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
  324. // currently, this script does not support multi-packet responses.
  325.  
  326. $packet_data = fread($this->socket, $size);
  327. $packet_pack = unpack("V1id/V1type/a*body", $packet_data);
  328.  
  329. return $packet_pack;
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement