· 7 years ago · Sep 30, 2018, 06:14 AM
1<?php
2define('SECRET_KEY', 'MIEJSCE NA KOD Z CSRV.PL');
3define('SERVER_ID', 'MIEJSCE NA SID SERWERA');
4
5define("SERVERDATA_EXECCOMMAND",2);
6define("SERVERDATA_AUTH",3);
7
8class rcon {
9 var $Password;
10 var $Host;
11 var $Port = 27015;
12 var $_Sock = null;
13 var $_Id = 5.200.172.232
14
15
16 function rcon ($Host,$Port,$Password) {
17 $this->Password = $Password;
18 $this->Host = $Host;
19 $this->Port = $Port;
20 $this->_Sock = @fsockopen($this->Host,$this->Port, $errno, $errstr, 30) or
21 die("Unable to open socket: $errstr ($errno)\n");
22 $this->_Set_Timeout($this->_Sock,2,500);
23 }
24
25 function Auth () {
26 $PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);
27
28 // Real response (id: -1 = failure)
29 $ret = $this->_PacketRead();
30 //var_dump($ret);
31 if ($ret[0]['ID'] == -1) {
32 return false;
33 }
34 return true;
35 }
36
37 function _Set_Timeout(&$res,$s,$m=0) {
38 if (version_compare(phpversion(),'4.3.0','<')) {
39 return socket_set_timeout($res,$s,$m);
40 }
41 return stream_set_timeout($res,$s,$m);
42 }
43
44 function _Write($cmd, $s1='', $s2='') {
45 // Get and increment the packet id
46 $id = ++$this->_Id;
47
48 // Put our packet together
49 $data = pack("VV",$id,$cmd).$s1.chr(0).$s2.chr(0);
50
51 // Prefix the packet size
52 $data = pack("V",strlen($data)).$data;
53
54 // Send packet
55 fwrite($this->_Sock,$data,strlen($data));
56
57 // In case we want it later we'll return the packet id
58 return $id;
59 }
60
61 function _PacketRead() {
62 //Declare the return array
63 $retarray = array();
64 //Fetch the packet size
65 while ($size = @fread($this->_Sock,4)) {
66 $size = unpack('V1Size',$size);
67 //Work around valve breaking the protocol
68 if ($size["Size"] > 4096) {
69 //pad with 8 nulls
70 $packet = "\x00\x00\x00\x00\x00\x00\x00\x00".fread($this->_Sock,4096);
71 } else {
72 //Read the packet back
73 $packet = fread($this->_Sock,$size["Size"]);
74 }
75 array_push($retarray,unpack("V1ID/V1Response/a*S1/a*S2",$packet));
76 }
77 return $retarray;
78 }
79
80 function Read() {
81 $Packets = $this->_PacketRead();
82
83 foreach($Packets as $pack) {
84 if (isset($ret[$pack['ID']])) {
85 $ret[$pack['ID']]['S1'] .= $pack['S1'];
86 $ret[$pack['ID']]['S2'] .= $pack['S1'];
87 } else {
88 $ret[$pack['ID']] = array(
89 'Response' => $pack['Response'],
90 'S1' => $pack['S1'],
91 'S2' => $pack['S2'],
92 );
93 }
94 }
95 return $ret;
96 }
97
98 function sendCommand($Command) {
99 //$Command = '"'.trim(str_replace(' ','" "', $Command)).'"';
100 //$Command="stop";
101 $this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
102 }
103
104 function rconCommand($Command) {
105 $this->sendcommand($Command);
106
107 $ret = $this->Read();
108 //ATM: Source servers don't return the request id, but if they fix this the code below should read as
109 // return $ret[$this->_Id]['S1'];
110 return $ret[$this->_Id]['S1'];
111 }
112}
113?>
114<?php
115function decode_transaction($payload, $signature) {
116 if(hash_hmac("sha256", $payload, SECRET_KEY) != $signature) {
117 return null;
118 }
119 return json_decode($payload, True);
120}
121if($_POST) {
122 $payload = $_POST['payload'];
123 $signature = $_POST['signature'];
124 $transaction = decode_transaction($payload, $signature);
125 if($transaction != null) {
126 if($transaction['server_id'] == SERVER_ID) {
127 $r = new rcon("127.0.0.1",10003,"foobar");
128 if( $r->Auth() ) {
129 $r->rconCommand("say DODANO TRANSAKCJE: ". $transaction['description']); //send a command
130 }
131 }
132 }
133}
134/* FORMAT TRANSAKCJI
135$transaction = array(
136 'server_id' => 1111, #SID serwera
137 'description' => 'Komentarz do płatności',
138 'source' => 'sms', #sms_cb, dotpay, paypal, bank_0173
139 'amount' => 15,
140 'date' => '2012-08-05T02:58:32.877866', #isodate
141 'transaction_id' => '501dc53811159f65c9000003',
142);
143*/
144
145?>