@@ -157,7 +157,7 @@ |
||
157 | 157 | * |
158 | 158 | * @param $packet_id |
159 | 159 | * @param $packet_type |
160 | - * @param $packet_body |
|
160 | + * @param string $packet_body |
|
161 | 161 | * |
162 | 162 | * @return void |
163 | 163 | */ |
@@ -13,157 +13,157 @@ discard block |
||
13 | 13 | namespace Thedudeguy; |
14 | 14 | |
15 | 15 | class Rcon { |
16 | - private $host; |
|
17 | - private $port; |
|
18 | - private $password; |
|
19 | - private $timeout; |
|
20 | - |
|
21 | - private $socket; |
|
22 | - |
|
23 | - private $authorized; |
|
24 | - private $last_response; |
|
25 | - |
|
26 | - const PACKET_AUTHORIZE = 5; |
|
27 | - const PACKET_COMMAND = 6; |
|
28 | - |
|
29 | - const SERVERDATA_AUTH = 3; |
|
30 | - const SERVERDATA_AUTH_RESPONSE = 2; |
|
31 | - const SERVERDATA_EXECCOMMAND = 2; |
|
32 | - const SERVERDATA_RESPONSE_VALUE = 0; |
|
33 | - |
|
34 | - /** |
|
35 | - * Create a new instance of the Rcon class. |
|
36 | - * |
|
37 | - * @param string $host |
|
38 | - * @param integer $port |
|
39 | - * @param string $password |
|
40 | - * @param integer $timeout |
|
41 | - */ |
|
42 | - public function __construct($host, $port, $password, $timeout) |
|
43 | - { |
|
44 | - $this->host = $host; |
|
45 | - $this->port = $port; |
|
46 | - $this->password = $password; |
|
47 | - $this->timeout = $timeout; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Get the latest response from the server. |
|
52 | - * |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function getResponse() |
|
56 | - { |
|
57 | - return $this->last_response; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Connect to a server. |
|
62 | - * |
|
63 | - * @return boolean |
|
64 | - */ |
|
65 | - public function connect() |
|
66 | - { |
|
67 | - $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); |
|
68 | - |
|
69 | - if (!$this->socket) { |
|
70 | - $this->last_response = $errstr; |
|
71 | - return false; |
|
72 | - } |
|
73 | - |
|
74 | - //set timeout |
|
75 | - stream_set_timeout($this->socket, 3, 0); |
|
76 | - |
|
77 | - // check authorization |
|
78 | - if ($this->authorize()) |
|
79 | - return true; |
|
80 | - |
|
81 | - return false; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Disconnect from server. |
|
86 | - * |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - public function disconnect() |
|
90 | - { |
|
91 | - if ($this->socket) |
|
92 | - fclose($this->socket); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * True if socket is connected and authorized. |
|
97 | - * |
|
98 | - * @return boolean |
|
99 | - */ |
|
100 | - public function isConnected() |
|
101 | - { |
|
102 | - return $this->authorized; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Send a command to the connected server. |
|
107 | - * |
|
108 | - * @param string $command |
|
109 | - * |
|
110 | - * @return boolean|mixed |
|
111 | - */ |
|
112 | - public function sendCommand($command) |
|
113 | - { |
|
114 | - if (!$this->isConnected()) |
|
115 | - return false; |
|
116 | - |
|
117 | - // send command packet |
|
118 | - $this->writePacket(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command); |
|
119 | - |
|
120 | - // get response |
|
121 | - $response_packet = $this->readPacket(); |
|
122 | - if ($response_packet['id'] == Rcon::PACKET_COMMAND) { |
|
123 | - if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) { |
|
124 | - $this->last_response = $response_packet['body']; |
|
125 | - |
|
126 | - return $response_packet['body']; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - return false; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Log into the server with the given credentials. |
|
135 | - * |
|
136 | - * @return boolean |
|
137 | - */ |
|
138 | - private function authorize() |
|
139 | - { |
|
140 | - $this->writePacket(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password); |
|
141 | - $response_packet = $this->readPacket(); |
|
142 | - |
|
143 | - if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE) { |
|
144 | - if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) { |
|
145 | - $this->authorized = true; |
|
146 | - |
|
147 | - return true; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - $this->disconnect(); |
|
152 | - return false; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Writes a packet to the socket stream. |
|
157 | - * |
|
158 | - * @param $packet_id |
|
159 | - * @param $packet_type |
|
160 | - * @param $packet_body |
|
161 | - * |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - private function writePacket($packet_id, $packet_type, $packet_body) |
|
165 | - { |
|
166 | - /* |
|
16 | + private $host; |
|
17 | + private $port; |
|
18 | + private $password; |
|
19 | + private $timeout; |
|
20 | + |
|
21 | + private $socket; |
|
22 | + |
|
23 | + private $authorized; |
|
24 | + private $last_response; |
|
25 | + |
|
26 | + const PACKET_AUTHORIZE = 5; |
|
27 | + const PACKET_COMMAND = 6; |
|
28 | + |
|
29 | + const SERVERDATA_AUTH = 3; |
|
30 | + const SERVERDATA_AUTH_RESPONSE = 2; |
|
31 | + const SERVERDATA_EXECCOMMAND = 2; |
|
32 | + const SERVERDATA_RESPONSE_VALUE = 0; |
|
33 | + |
|
34 | + /** |
|
35 | + * Create a new instance of the Rcon class. |
|
36 | + * |
|
37 | + * @param string $host |
|
38 | + * @param integer $port |
|
39 | + * @param string $password |
|
40 | + * @param integer $timeout |
|
41 | + */ |
|
42 | + public function __construct($host, $port, $password, $timeout) |
|
43 | + { |
|
44 | + $this->host = $host; |
|
45 | + $this->port = $port; |
|
46 | + $this->password = $password; |
|
47 | + $this->timeout = $timeout; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Get the latest response from the server. |
|
52 | + * |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function getResponse() |
|
56 | + { |
|
57 | + return $this->last_response; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Connect to a server. |
|
62 | + * |
|
63 | + * @return boolean |
|
64 | + */ |
|
65 | + public function connect() |
|
66 | + { |
|
67 | + $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); |
|
68 | + |
|
69 | + if (!$this->socket) { |
|
70 | + $this->last_response = $errstr; |
|
71 | + return false; |
|
72 | + } |
|
73 | + |
|
74 | + //set timeout |
|
75 | + stream_set_timeout($this->socket, 3, 0); |
|
76 | + |
|
77 | + // check authorization |
|
78 | + if ($this->authorize()) |
|
79 | + return true; |
|
80 | + |
|
81 | + return false; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Disconnect from server. |
|
86 | + * |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + public function disconnect() |
|
90 | + { |
|
91 | + if ($this->socket) |
|
92 | + fclose($this->socket); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * True if socket is connected and authorized. |
|
97 | + * |
|
98 | + * @return boolean |
|
99 | + */ |
|
100 | + public function isConnected() |
|
101 | + { |
|
102 | + return $this->authorized; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Send a command to the connected server. |
|
107 | + * |
|
108 | + * @param string $command |
|
109 | + * |
|
110 | + * @return boolean|mixed |
|
111 | + */ |
|
112 | + public function sendCommand($command) |
|
113 | + { |
|
114 | + if (!$this->isConnected()) |
|
115 | + return false; |
|
116 | + |
|
117 | + // send command packet |
|
118 | + $this->writePacket(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command); |
|
119 | + |
|
120 | + // get response |
|
121 | + $response_packet = $this->readPacket(); |
|
122 | + if ($response_packet['id'] == Rcon::PACKET_COMMAND) { |
|
123 | + if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) { |
|
124 | + $this->last_response = $response_packet['body']; |
|
125 | + |
|
126 | + return $response_packet['body']; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + return false; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Log into the server with the given credentials. |
|
135 | + * |
|
136 | + * @return boolean |
|
137 | + */ |
|
138 | + private function authorize() |
|
139 | + { |
|
140 | + $this->writePacket(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password); |
|
141 | + $response_packet = $this->readPacket(); |
|
142 | + |
|
143 | + if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE) { |
|
144 | + if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) { |
|
145 | + $this->authorized = true; |
|
146 | + |
|
147 | + return true; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + $this->disconnect(); |
|
152 | + return false; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Writes a packet to the socket stream. |
|
157 | + * |
|
158 | + * @param $packet_id |
|
159 | + * @param $packet_type |
|
160 | + * @param $packet_body |
|
161 | + * |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + private function writePacket($packet_id, $packet_type, $packet_body) |
|
165 | + { |
|
166 | + /* |
|
167 | 167 | Size 32-bit little-endian Signed Integer Varies, see below. |
168 | 168 | ID 32-bit little-endian Signed Integer Varies, see below. |
169 | 169 | Type 32-bit little-endian Signed Integer Varies, see below. |
@@ -171,71 +171,71 @@ discard block |
||
171 | 171 | Empty String Null-terminated ASCII String 0x00 |
172 | 172 | */ |
173 | 173 | |
174 | - //create packet |
|
175 | - $packet = pack("VV", $packet_id, $packet_type); |
|
176 | - $packet = $packet . $packet_body . "\x00"; |
|
177 | - $packet = $packet . "\x00"; |
|
178 | - |
|
179 | - // get packet size. |
|
180 | - $packet_size = strlen($packet); |
|
181 | - |
|
182 | - // attach size to packet. |
|
183 | - $packet = pack("V", $packet_size) . $packet; |
|
184 | - |
|
185 | - // write packet. |
|
186 | - fwrite($this->socket, $packet, strlen($packet)); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Read a packet from the socket stream. |
|
191 | - * |
|
192 | - * @return array |
|
193 | - */ |
|
194 | - private function readPacket() |
|
195 | - { |
|
196 | - //get packet size. |
|
197 | - $size_data = fread($this->socket, 4); |
|
198 | - $size_pack = unpack("V1size", $size_data); |
|
199 | - $size = $size_pack['size']; |
|
200 | - |
|
201 | - // if size is > 4096, the response will be in multiple packets. |
|
202 | - // this needs to be address. get more info about multi-packet responses |
|
203 | - // from the RCON protocol specification at |
|
204 | - // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol |
|
205 | - // currently, this script does not support multi-packet responses. |
|
206 | - |
|
207 | - $packet_data = fread($this->socket, $size); |
|
208 | - $packet_pack = unpack("V1id/V1type/a*body", $packet_data); |
|
209 | - |
|
210 | - return $packet_pack; |
|
211 | - } |
|
174 | + //create packet |
|
175 | + $packet = pack("VV", $packet_id, $packet_type); |
|
176 | + $packet = $packet . $packet_body . "\x00"; |
|
177 | + $packet = $packet . "\x00"; |
|
178 | + |
|
179 | + // get packet size. |
|
180 | + $packet_size = strlen($packet); |
|
181 | + |
|
182 | + // attach size to packet. |
|
183 | + $packet = pack("V", $packet_size) . $packet; |
|
184 | + |
|
185 | + // write packet. |
|
186 | + fwrite($this->socket, $packet, strlen($packet)); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Read a packet from the socket stream. |
|
191 | + * |
|
192 | + * @return array |
|
193 | + */ |
|
194 | + private function readPacket() |
|
195 | + { |
|
196 | + //get packet size. |
|
197 | + $size_data = fread($this->socket, 4); |
|
198 | + $size_pack = unpack("V1size", $size_data); |
|
199 | + $size = $size_pack['size']; |
|
200 | + |
|
201 | + // if size is > 4096, the response will be in multiple packets. |
|
202 | + // this needs to be address. get more info about multi-packet responses |
|
203 | + // from the RCON protocol specification at |
|
204 | + // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol |
|
205 | + // currently, this script does not support multi-packet responses. |
|
206 | + |
|
207 | + $packet_data = fread($this->socket, $size); |
|
208 | + $packet_pack = unpack("V1id/V1type/a*body", $packet_data); |
|
209 | + |
|
210 | + return $packet_pack; |
|
211 | + } |
|
212 | 212 | |
213 | - // Below are the deprecated functions for reverse compatibility |
|
213 | + // Below are the deprecated functions for reverse compatibility |
|
214 | 214 | |
215 | - /** |
|
216 | - * @deprecated |
|
217 | - * @see Rcon::getResponse() |
|
218 | - */ |
|
219 | - public function get_response() |
|
220 | - { |
|
221 | - return $this->getResponse(); |
|
222 | - } |
|
215 | + /** |
|
216 | + * @deprecated |
|
217 | + * @see Rcon::getResponse() |
|
218 | + */ |
|
219 | + public function get_response() |
|
220 | + { |
|
221 | + return $this->getResponse(); |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * @deprecated |
|
226 | - * @see Rcon::isConnected() |
|
227 | - */ |
|
228 | - public function is_connected() |
|
229 | - { |
|
230 | - return $this->isConnected(); |
|
231 | - } |
|
224 | + /** |
|
225 | + * @deprecated |
|
226 | + * @see Rcon::isConnected() |
|
227 | + */ |
|
228 | + public function is_connected() |
|
229 | + { |
|
230 | + return $this->isConnected(); |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * @deprecated |
|
235 | - * @see Rcon::sendCommand() |
|
236 | - */ |
|
237 | - public function send_command($command) |
|
238 | - { |
|
239 | - return $this->sendCommand($command); |
|
240 | - } |
|
233 | + /** |
|
234 | + * @deprecated |
|
235 | + * @see Rcon::sendCommand() |
|
236 | + */ |
|
237 | + public function send_command($command) |
|
238 | + { |
|
239 | + return $this->sendCommand($command); |
|
240 | + } |
|
241 | 241 | } |
@@ -173,14 +173,14 @@ |
||
173 | 173 | |
174 | 174 | //create packet |
175 | 175 | $packet = pack("VV", $packet_id, $packet_type); |
176 | - $packet = $packet . $packet_body . "\x00"; |
|
177 | - $packet = $packet . "\x00"; |
|
176 | + $packet = $packet.$packet_body."\x00"; |
|
177 | + $packet = $packet."\x00"; |
|
178 | 178 | |
179 | 179 | // get packet size. |
180 | 180 | $packet_size = strlen($packet); |
181 | 181 | |
182 | 182 | // attach size to packet. |
183 | - $packet = pack("V", $packet_size) . $packet; |
|
183 | + $packet = pack("V", $packet_size).$packet; |
|
184 | 184 | |
185 | 185 | // write packet. |
186 | 186 | fwrite($this->socket, $packet, strlen($packet)); |
@@ -12,7 +12,8 @@ discard block |
||
12 | 12 | |
13 | 13 | namespace Thedudeguy; |
14 | 14 | |
15 | -class Rcon { |
|
15 | +class Rcon |
|
16 | +{ |
|
16 | 17 | private $host; |
17 | 18 | private $port; |
18 | 19 | private $password; |
@@ -39,7 +40,7 @@ discard block |
||
39 | 40 | * @param string $password |
40 | 41 | * @param integer $timeout |
41 | 42 | */ |
42 | - public function __construct($host, $port, $password, $timeout) |
|
43 | + public function __construct($host, $port, $password, $timeout) |
|
43 | 44 | { |
44 | 45 | $this->host = $host; |
45 | 46 | $this->port = $port; |
@@ -52,7 +53,7 @@ discard block |
||
52 | 53 | * |
53 | 54 | * @return string |
54 | 55 | */ |
55 | - public function getResponse() |
|
56 | + public function getResponse() |
|
56 | 57 | { |
57 | 58 | return $this->last_response; |
58 | 59 | } |
@@ -62,7 +63,7 @@ discard block |
||
62 | 63 | * |
63 | 64 | * @return boolean |
64 | 65 | */ |
65 | - public function connect() |
|
66 | + public function connect() |
|
66 | 67 | { |
67 | 68 | $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); |
68 | 69 | |
@@ -75,8 +76,9 @@ discard block |
||
75 | 76 | stream_set_timeout($this->socket, 3, 0); |
76 | 77 | |
77 | 78 | // check authorization |
78 | - if ($this->authorize()) |
|
79 | - return true; |
|
79 | + if ($this->authorize()) { |
|
80 | + return true; |
|
81 | + } |
|
80 | 82 | |
81 | 83 | return false; |
82 | 84 | } |
@@ -86,10 +88,11 @@ discard block |
||
86 | 88 | * |
87 | 89 | * @return void |
88 | 90 | */ |
89 | - public function disconnect() |
|
91 | + public function disconnect() |
|
90 | 92 | { |
91 | - if ($this->socket) |
|
92 | - fclose($this->socket); |
|
93 | + if ($this->socket) { |
|
94 | + fclose($this->socket); |
|
95 | + } |
|
93 | 96 | } |
94 | 97 | |
95 | 98 | /** |
@@ -97,7 +100,7 @@ discard block |
||
97 | 100 | * |
98 | 101 | * @return boolean |
99 | 102 | */ |
100 | - public function isConnected() |
|
103 | + public function isConnected() |
|
101 | 104 | { |
102 | 105 | return $this->authorized; |
103 | 106 | } |
@@ -109,10 +112,11 @@ discard block |
||
109 | 112 | * |
110 | 113 | * @return boolean|mixed |
111 | 114 | */ |
112 | - public function sendCommand($command) |
|
115 | + public function sendCommand($command) |
|
113 | 116 | { |
114 | - if (!$this->isConnected()) |
|
115 | - return false; |
|
117 | + if (!$this->isConnected()) { |
|
118 | + return false; |
|
119 | + } |
|
116 | 120 | |
117 | 121 | // send command packet |
118 | 122 | $this->writePacket(Rcon::PACKET_COMMAND, Rcon::SERVERDATA_EXECCOMMAND, $command); |
@@ -135,7 +139,7 @@ discard block |
||
135 | 139 | * |
136 | 140 | * @return boolean |
137 | 141 | */ |
138 | - private function authorize() |
|
142 | + private function authorize() |
|
139 | 143 | { |
140 | 144 | $this->writePacket(Rcon::PACKET_AUTHORIZE, Rcon::SERVERDATA_AUTH, $this->password); |
141 | 145 | $response_packet = $this->readPacket(); |
@@ -191,7 +195,7 @@ discard block |
||
191 | 195 | * |
192 | 196 | * @return array |
193 | 197 | */ |
194 | - private function readPacket() |
|
198 | + private function readPacket() |
|
195 | 199 | { |
196 | 200 | //get packet size. |
197 | 201 | $size_data = fread($this->socket, 4); |
@@ -234,7 +238,7 @@ discard block |
||
234 | 238 | * @deprecated |
235 | 239 | * @see Rcon::sendCommand() |
236 | 240 | */ |
237 | - public function send_command($command) |
|
241 | + public function send_command($command) |
|
238 | 242 | { |
239 | 243 | return $this->sendCommand($command); |
240 | 244 | } |