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.

Rcon::getResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
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
16
{
17
    private $host;
18
    private $port;
19
    private $password;
20
    private $timeout;
21
22
    private $socket;
23
24
    private $authorized = false;
25
    private $lastResponse = '';
26
27
    const PACKET_AUTHORIZE = 5;
28
    const PACKET_COMMAND = 6;
29
30
    const SERVERDATA_AUTH = 3;
31
    const SERVERDATA_AUTH_RESPONSE = 2;
32
    const SERVERDATA_EXECCOMMAND = 2;
33
    const SERVERDATA_RESPONSE_VALUE = 0;
34
35
    /**
36
     * Create a new instance of the Rcon class.
37
     *
38
     * @param string $host
39
     * @param integer $port
40
     * @param string $password
41
     * @param integer $timeout
42
     */
43
    public function __construct($host, $port, $password, $timeout)
44
    {
45
        $this->host = $host;
46
        $this->port = $port;
47
        $this->password = $password;
48
        $this->timeout = $timeout;
49
    }
50
51
    /**
52
     * Get the latest response from the server.
53
     *
54
     * @return string
55
     */
56
    public function getResponse()
57
    {
58
        return $this->lastResponse;
59
    }
60
61
    /**
62
     * Connect to a server.
63
     *
64
     * @return boolean
65
     */
66
    public function connect()
67
    {
68
        $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
69
70
        if (!$this->socket) {
71
            $this->lastResponse = $errstr;
72
            return false;
73
        }
74
75
        //set timeout
76
        stream_set_timeout($this->socket, 3, 0);
77
78
        // check authorization
79
        return $this->authorize();
80
    }
81
82
    /**
83
     * Disconnect from server.
84
     *
85
     * @return void
86
     */
87
    public function disconnect()
88
    {
89
        if ($this->socket) {
90
                    fclose($this->socket);
91
        }
92
    }
93
94
    /**
95
     * True if socket is connected and authorized.
96
     *
97
     * @return boolean
98
     */
99
    public function isConnected()
100
    {
101
        return $this->authorized;
102
    }
103
104
    /**
105
     * Send a command to the connected server.
106
     *
107
     * @param string $command
108
     *
109
     * @return boolean|mixed
110
     */
111
    public function sendCommand($command)
112
    {
113
        if (!$this->isConnected()) {
114
                    return false;
115
        }
116
117
        // send command packet
118
        $this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);
119
120
        // get response
121
        $response_packet = $this->readPacket();
122
        if ($response_packet['id'] == self::PACKET_COMMAND) {
123
            if ($response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) {
124
                $this->lastResponse = $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(self::PACKET_AUTHORIZE, self::SERVERDATA_AUTH, $this->password);
141
        $response_packet = $this->readPacket();
142
143
        if ($response_packet['type'] == self::SERVERDATA_AUTH_RESPONSE) {
144
            if ($response_packet['id'] == self::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 $packetId
159
     * @param $packetType
160
     * @param string $packetBody
161
     *
162
     * @return void
163
     */
164
    private function writePacket($packetId, $packetType, $packetBody)
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', $packetId, $packetType);
176
        $packet = $packet.$packetBody."\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