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
Branch master (84e1e5)
by Chris
01:45
created

Rcon::get_response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 {
0 ignored issues
show
Coding Style introduced by
The property $last_response is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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())
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->authorize();.
Loading history...
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);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
119
120
		// get response
121
		$response_packet = $this->readPacket();
122
		if ($response_packet['id'] == Rcon::PACKET_COMMAND) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
123
			if ($response_packet['type'] == Rcon::SERVERDATA_RESPONSE_VALUE) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
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);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
141
		$response_packet = $this->readPacket();
142
143
		if ($response_packet['type'] == Rcon::SERVERDATA_AUTH_RESPONSE) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
144
			if ($response_packet['id'] == Rcon::PACKET_AUTHORIZE) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $packet_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $packet_type is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $packet_body is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
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);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal VV does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal V does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal V1size does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal V1id/V1type/a*body does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
220
	{
221
		return $this->getResponse();
222
	}
223
	
224
	/**
225
	 * @deprecated
226
	 * @see Rcon::isConnected()
227
	 */
228
	public function is_connected()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
229
	{
230
		return $this->isConnected();
231
	}
232
	
233
	/**
234
	 * @deprecated
235
	 * @see Rcon::sendCommand()
236
	 */
237
	public function send_command($command) 
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
238
	{
239
		return $this->sendCommand($command);
240
	}
241
}
242