GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 79fa03...84e1e5 )
by Chris
01:58
created

Rcon::authorize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
/**
3
 * See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for
4
 * more information about Source RCON Packets
5
 *
6
 * PHP Version 7
7
 *
8
 * @copyright 2013-2017 Chris Churchwell
9
 * @author thedudeguy
10
 * @link https://github.com/thedudeguy/PHP-Minecraft-Rcon
11
 */
12
13
namespace Thedudeguy;
14
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
		/*
167
		Size			32-bit little-endian Signed Integer	 	Varies, see below.
168
		ID				32-bit little-endian Signed Integer		Varies, see below.
169
		Type	        32-bit little-endian Signed Integer		Varies, see below.
170
		Body		    Null-terminated ASCII String			Varies, see below.
171
		Empty String    Null-terminated ASCII String			0x00
172
		*/
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
	}
212
	
213
	// Below are the deprecated functions for reverse compatibility
214
	
215
	/**
216
	 * @deprecated
217
	 * @see Rcon::getResponse()
218
	 */
219
	public function get_response()
220
	{
221
		return $this->getResponse();
222
	}
223
	
224
	/**
225
	 * @deprecated
226
	 * @see Rcon::isConnected()
227
	 */
228
	public function is_connected()
229
	{
230
		return $this->isConnected();
231
	}
232
	
233
	/**
234
	 * @deprecated
235
	 * @see Rcon::sendCommand()
236
	 */
237
	public function send_command($command) 
238
	{
239
		return $this->sendCommand($command);
240
	}
241
}
242