Bring in CS-Fixer, reformat.

pull/150/head
Anthony Birkett 4 years ago
parent 3bfc073211
commit e62d011689

@ -1,8 +0,0 @@
# http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = tab
insert_final_newline = true

1
.gitattributes vendored

@ -1 +0,0 @@
* text=auto

2
.gitignore vendored

@ -1,2 +1,4 @@
vendor/
composer.lock
.php-cs-fixer.cache
.idea/

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../SourceQuery/bootstrap.php';
use xPaw\SourceQuery\SourceQuery;
@ -16,19 +19,14 @@
$Query = new SourceQuery();
try
{
try {
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
print_r($Query->GetInfo());
print_r($Query->GetPlayers());
print_r($Query->GetRules());
}
catch( Exception $e )
{
} catch (Exception $e) {
echo $e->getMessage();
}
finally
{
} finally {
$Query->Disconnect();
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../SourceQuery/bootstrap.php';
use xPaw\SourceQuery\SourceQuery;
@ -16,19 +19,14 @@
$Query = new SourceQuery();
try
{
try {
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
$Query->SetRconPassword('my_awesome_password');
var_dump($Query->Rcon('say hello'));
}
catch( Exception $e )
{
} catch (Exception $e) {
echo $e->getMessage();
}
finally
{
} finally {
$Query->Disconnect();
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../SourceQuery/bootstrap.php';
use xPaw\SourceQuery\SourceQuery;
@ -19,25 +22,21 @@
$Players = [];
$Exception = null;
try
{
try {
$Query->Connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
//$Query->SetUseOldGetChallengeMethod( true ); // Use this when players/rules retrieval fails on games like Starbound
$Info = $Query->GetInfo();
$Players = $Query->GetPlayers();
$Rules = $Query->GetRules();
}
catch( Exception $e )
{
} catch (Exception $e) {
$Exception = $e;
}
finally
{
} finally {
$Query->Disconnect();
}
$Timer = number_format(microtime(true) - $Timer, 4, '.', '');
?>
<!DOCTYPE html>
<html>
@ -108,24 +107,16 @@
<tr>
<td><?php echo htmlspecialchars($InfoKey); ?></td>
<td><?php
if( is_array( $InfoValue ) )
{
if (is_array($InfoValue)) {
echo "<pre>";
print_r($InfoValue);
echo "</pre>";
}
else
{
if( $InfoValue === true )
{
} else {
if ($InfoValue === true) {
echo 'true';
}
else if( $InfoValue === false )
{
} elseif ($InfoValue === false) {
echo 'false';
}
else
{
} else {
echo htmlspecialchars($InfoValue);
}
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -45,30 +48,24 @@
protected function ReadInternal(Buffer $Buffer, int $Length, callable $SherlockFunction): Buffer
{
if( $Buffer->Remaining( ) === 0 )
{
if ($Buffer->Remaining() === 0) {
throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
}
$Header = $Buffer->GetLong();
if( $Header === -1 ) // Single packet
{
if ($Header === -1) { // Single packet
// We don't have to do anything
}
else if( $Header === -2 ) // Split packet
{
} elseif ($Header === -2) { // Split packet
$Packets = [];
$IsCompressed = false;
$ReadMore = false;
$PacketChecksum = null;
do
{
do {
$RequestID = $Buffer->GetLong();
switch( $this->Engine )
{
switch ($this->Engine) {
case SourceQuery::GOLDSOURCE:
{
$PacketCountAndNumber = $Buffer->GetByte();
@ -83,14 +80,11 @@
$PacketCount = $Buffer->GetByte();
$PacketNumber = $Buffer->GetByte() + 1;
if( $IsCompressed )
{
if ($IsCompressed) {
$Buffer->GetLong(); // Split size
$PacketChecksum = $Buffer->GetUnsignedLong();
}
else
{
} else {
$Buffer->GetShort(); // Split size
}
@ -105,32 +99,26 @@
$Packets[ $PacketNumber ] = $Buffer->Get();
$ReadMore = $PacketCount > sizeof($Packets);
}
while( $ReadMore && $SherlockFunction( $Buffer, $Length ) );
} while ($ReadMore && $SherlockFunction($Buffer, $Length));
$Data = implode($Packets);
// TODO: Test this
if( $IsCompressed )
{
if ($IsCompressed) {
// Let's make sure this function exists, it's not included in PHP by default
if( !function_exists( 'bzdecompress' ) )
{
if (!function_exists('bzdecompress')) {
throw new \RuntimeException('Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.');
}
$Data = bzdecompress($Data);
if( !is_string( $Data ) || crc32( $Data ) !== $PacketChecksum )
{
if (!is_string($Data) || crc32($Data) !== $PacketChecksum) {
throw new InvalidPacketException('CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH);
}
}
$Buffer->Set(substr($Data, 4));
}
else
{
} else {
throw new InvalidPacketException('Socket read: Raw packet header mismatch. (0x' . dechex($Header) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -21,7 +24,7 @@
*
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
*/
class Buffer
final class Buffer
{
/**
* Buffer
@ -65,19 +68,15 @@
*/
public function Get(int $Length = -1): string
{
if( $Length === 0 )
{
if ($Length === 0) {
return '';
}
$Remaining = $this->Remaining();
if( $Length === -1 )
{
if ($Length === -1) {
$Length = $Remaining;
}
else if( $Length > $Remaining )
{
} elseif ($Length > $Remaining) {
return '';
}
@ -101,8 +100,7 @@
*/
public function GetShort(): int
{
if( $this->Remaining( ) < 2 )
{
if ($this->Remaining() < 2) {
throw new InvalidPacketException('Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY);
}
@ -116,8 +114,7 @@
*/
public function GetLong(): int
{
if( $this->Remaining( ) < 4 )
{
if ($this->Remaining() < 4) {
throw new InvalidPacketException('Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY);
}
@ -131,8 +128,7 @@
*/
public function GetFloat(): float
{
if( $this->Remaining( ) < 4 )
{
if ($this->Remaining() < 4) {
throw new InvalidPacketException('Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY);
}
@ -146,8 +142,7 @@
*/
public function GetUnsignedLong(): int
{
if( $this->Remaining( ) < 4 )
{
if ($this->Remaining() < 4) {
throw new InvalidPacketException('Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY);
}
@ -163,8 +158,7 @@
{
$ZeroBytePosition = strpos($this->Buffer, "\0", $this->Position);
if( $ZeroBytePosition === false )
{
if ($ZeroBytePosition === false) {
return '';
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -12,8 +15,8 @@
namespace xPaw\SourceQuery\Exception;
class AuthenticationException extends SourceQueryException
final class AuthenticationException extends SourceQueryException
{
const BAD_PASSWORD = 1;
const BANNED = 2;
public const BAD_PASSWORD = 1;
public const BANNED = 2;
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -12,7 +15,7 @@
namespace xPaw\SourceQuery\Exception;
class InvalidArgumentException extends SourceQueryException
final class InvalidArgumentException extends SourceQueryException
{
const TIMEOUT_NOT_INTEGER = 1;
public const TIMEOUT_NOT_INTEGER = 1;
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -12,10 +15,10 @@
namespace xPaw\SourceQuery\Exception;
class InvalidPacketException extends SourceQueryException
final class InvalidPacketException extends SourceQueryException
{
const PACKET_HEADER_MISMATCH = 1;
const BUFFER_EMPTY = 2;
const BUFFER_NOT_EMPTY = 3;
const CHECKSUM_MISMATCH = 4;
public const PACKET_HEADER_MISMATCH = 1;
public const BUFFER_EMPTY = 2;
public const BUFFER_NOT_EMPTY = 3;
public const CHECKSUM_MISMATCH = 4;
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -12,10 +15,10 @@
namespace xPaw\SourceQuery\Exception;
class SocketException extends SourceQueryException
final class SocketException extends SourceQueryException
{
const COULD_NOT_CREATE_SOCKET = 1;
const NOT_CONNECTED = 2;
const CONNECTION_FAILED = 3;
const INVALID_ENGINE = 3;
public const COULD_NOT_CREATE_SOCKET = 1;
public const NOT_CONNECTED = 2;
public const CONNECTION_FAILED = 3;
public const INVALID_ENGINE = 3;
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -20,10 +23,10 @@
*
* @package xPaw\SourceQuery
*
* @uses xPaw\SourceQuery\Exception\AuthenticationException
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
* @uses AuthenticationException
* @uses InvalidPacketException
*/
class GoldSourceRcon
final class GoldSourceRcon
{
/**
* Points to socket class
@ -73,14 +76,11 @@
$ReadMore = false;
// There is no indentifier of the end, so we just need to continue reading
do
{
do {
$ReadMore = $Buffer->Remaining() > 0;
if( $ReadMore )
{
if( $Buffer->GetByte( ) !== SourceQuery::S2A_RCON )
{
if ($ReadMore) {
if ($Buffer->GetByte() !== SourceQuery::S2A_RCON) {
throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
@ -91,22 +91,17 @@
// Let's assume if this packet is not long enough, there are no more after this one
$ReadMore = strlen($Packet) > 1000; // use 1300?
if( $ReadMore )
{
if ($ReadMore) {
$Buffer = $this->Socket->Read();
}
}
}
while( $ReadMore );
} while ($ReadMore);
$Trimmed = trim($StringBuffer);
if( $Trimmed === 'Bad rcon_password.' )
{
if ($Trimmed === 'Bad rcon_password.') {
throw new AuthenticationException($Trimmed, AuthenticationException::BAD_PASSWORD);
}
else if( $Trimmed === 'You have been banned from this server.' )
{
} elseif ($Trimmed === 'You have been banned from this server.') {
throw new AuthenticationException($Trimmed, AuthenticationException::BANNED);
}
@ -117,8 +112,7 @@
public function Command(string $Command): string
{
if( !$this->RconChallenge )
{
if (!$this->RconChallenge) {
throw new AuthenticationException('Tried to execute a RCON command before successful authorization.', AuthenticationException::BAD_PASSWORD);
}
@ -135,8 +129,7 @@
$this->Write(0, 'challenge rcon');
$Buffer = $this->Socket->Read();
if( $Buffer->Get( 14 ) !== 'challenge rcon' )
{
if ($Buffer->Get(14) !== 'challenge rcon') {
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -20,15 +23,14 @@
*
* @package xPaw\SourceQuery
*
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
* @uses xPaw\SourceQuery\Exception\SocketException
* @uses InvalidPacketException
* @uses SocketException
*/
class Socket extends BaseSocket
final class Socket extends BaseSocket
{
public function Close(): void
{
if( $this->Socket !== null )
{
if ($this->Socket !== null) {
fclose($this->Socket);
$this->Socket = null;
@ -44,8 +46,7 @@
$Socket = @fsockopen('udp://' . $Address, $Port, $ErrNo, $ErrStr, $Timeout);
if( $ErrNo || $Socket === false )
{
if ($ErrNo || $Socket === false) {
throw new SocketException('Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET);
}
@ -83,8 +84,7 @@
{
$Data = fread($this->Socket, $Length);
if( strlen( $Data ) < 4 )
{
if (strlen($Data) < 4) {
return false;
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* This class provides the public interface to the PHP-Source-Query library.
*
@ -22,51 +25,51 @@
*
* @package xPaw\SourceQuery
*
* @uses xPaw\SourceQuery\Exception\AuthenticationException
* @uses xPaw\SourceQuery\Exception\InvalidArgumentException
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
* @uses xPaw\SourceQuery\Exception\SocketException
* @uses AuthenticationException
* @uses InvalidArgumentException
* @uses InvalidPacketException
* @uses SocketException
*/
class SourceQuery
final class SourceQuery
{
/**
* Engines
*/
const GOLDSOURCE = 0;
const SOURCE = 1;
public const GOLDSOURCE = 0;
public const SOURCE = 1;
/**
* Packets sent
*/
const A2A_PING = 0x69;
const A2S_INFO = 0x54;
const A2S_PLAYER = 0x55;
const A2S_RULES = 0x56;
const A2S_SERVERQUERY_GETCHALLENGE = 0x57;
private const A2A_PING = 0x69;
private const A2S_INFO = 0x54;
private const A2S_PLAYER = 0x55;
private const A2S_RULES = 0x56;
private const A2S_SERVERQUERY_GETCHALLENGE = 0x57;
/**
* Packets received
*/
const A2A_ACK = 0x6A;
const S2C_CHALLENGE = 0x41;
const S2A_INFO_SRC = 0x49;
const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED)
const S2A_PLAYER = 0x44;
const S2A_RULES = 0x45;
const S2A_RCON = 0x6C;
private const A2A_ACK = 0x6A;
private const S2C_CHALLENGE = 0x41;
private const S2A_INFO_SRC = 0x49;
private const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED)
private const S2A_PLAYER = 0x44;
private const S2A_RULES = 0x45;
public const S2A_RCON = 0x6C;
/**
* Source rcon sent
*/
const SERVERDATA_REQUESTVALUE = 0;
const SERVERDATA_EXECCOMMAND = 2;
const SERVERDATA_AUTH = 3;
public const SERVERDATA_REQUESTVALUE = 0;
public const SERVERDATA_EXECCOMMAND = 2;
public const SERVERDATA_AUTH = 3;
/**
* Source rcon received
*/
const SERVERDATA_RESPONSE_VALUE = 0;
const SERVERDATA_AUTH_RESPONSE = 2;
public const SERVERDATA_RESPONSE_VALUE = 0;
public const SERVERDATA_AUTH_RESPONSE = 2;
/**
* Points to rcon class
@ -120,8 +123,7 @@
{
$this->Disconnect();
if( $Timeout < 0 )
{
if ($Timeout < 0) {
throw new InvalidArgumentException('Timeout must be a positive integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER);
}
@ -156,8 +158,7 @@
$this->Socket->Close();
if( $this->Rcon )
{
if ($this->Rcon) {
$this->Rcon->Close();
$this->Rcon = null;
@ -175,8 +176,7 @@
*/
public function Ping(): bool
{
if( !$this->Connected )
{
if (!$this->Connected) {
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
}
@ -196,17 +196,13 @@
*/
public function GetInfo(): array
{
if( !$this->Connected )
{
if (!$this->Connected) {
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
}
if( $this->Challenge )
{
if ($this->Challenge) {
$this->Socket->Write(self::A2S_INFO, "Source Engine Query\0" . $this->Challenge);
}
else
{
} else {
$this->Socket->Write(self::A2S_INFO, "Source Engine Query\0");
}
@ -214,8 +210,7 @@
$Type = $Buffer->GetByte();
$Server = [];
if( $Type === self::S2C_CHALLENGE )
{
if ($Type === self::S2C_CHALLENGE) {
$this->Challenge = $Buffer->Get(4);
$this->Socket->Write(self::A2S_INFO, "Source Engine Query\0" . $this->Challenge);
@ -224,8 +219,7 @@
}
// Old GoldSource protocol, HLTV still uses it
if( $Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE )
{
if ($Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE) {
/**
* If we try to read data again, and we get the result with type S2A_INFO (0x49)
* That means this server is running dproto,
@ -245,8 +239,7 @@
$Server[ 'Password' ] = $Buffer->GetByte() === 1;
$Server[ 'IsMod' ] = $Buffer->GetByte() === 1;
if( $Server[ 'IsMod' ] )
{
if ($Server[ 'IsMod' ]) {
$Mod = [];
$Mod[ 'Url' ] = $Buffer->GetString();
$Mod[ 'Download' ] = $Buffer->GetString();
@ -264,8 +257,7 @@
return $Server;
}
if( $Type !== self::S2A_INFO_SRC )
{
if ($Type !== self::S2A_INFO_SRC) {
throw new InvalidPacketException('GetInfo: Packet header mismatch. (0x' . dechex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
@ -284,8 +276,7 @@
$Server[ 'Secure' ] = $Buffer->GetByte() === 1;
// The Ship (they violate query protocol spec by modifying the response)
if( $Server[ 'AppID' ] === 2400 )
{
if ($Server[ 'AppID' ] === 2400) {
$Server[ 'GameMode' ] = $Buffer->GetByte();
$Server[ 'WitnessCount' ] = $Buffer->GetByte();
$Server[ 'WitnessTime' ] = $Buffer->GetByte();
@ -294,40 +285,31 @@
$Server[ 'Version' ] = $Buffer->GetString();
// Extra Data Flags
if( $Buffer->Remaining( ) > 0 )
{
if ($Buffer->Remaining() > 0) {
$Server[ 'ExtraDataFlags' ] = $Flags = $Buffer->GetByte();
// S2A_EXTRA_DATA_HAS_GAME_PORT - Next 2 bytes include the game port.
if( $Flags & 0x80 )
{
if ($Flags & 0x80) {
$Server[ 'GamePort' ] = $Buffer->GetShort();
}
// S2A_EXTRA_DATA_HAS_STEAMID - Next 8 bytes are the steamID
// Want to play around with this?
// You can use https://github.com/xPaw/SteamID.php
if( $Flags & 0x10 )
{
if ($Flags & 0x10) {
$SteamIDLower = $Buffer->GetUnsignedLong();
$SteamIDInstance = $Buffer->GetUnsignedLong(); // This gets shifted by 32 bits, which should be steamid instance
$SteamID = 0;
if( PHP_INT_SIZE === 4 )
{
if( extension_loaded( 'gmp' ) )
{
if (PHP_INT_SIZE === 4) {
if (extension_loaded('gmp')) {
$SteamIDLower = gmp_abs($SteamIDLower);
$SteamIDInstance = gmp_abs($SteamIDInstance);
$SteamID = gmp_strval(gmp_or($SteamIDLower, gmp_mul($SteamIDInstance, gmp_pow(2, 32))));
}
else
{
} else {
throw new \RuntimeException('Either 64-bit PHP installation or "gmp" module is required to correctly parse server\'s steamid.');
}
}
else
{
} else {
$SteamID = $SteamIDLower | ($SteamIDInstance << 32);
}
@ -337,28 +319,26 @@
}
// S2A_EXTRA_DATA_HAS_SPECTATOR_DATA - Next 2 bytes include the spectator port, then the spectator server name.
if( $Flags & 0x40 )
{
if ($Flags & 0x40) {
$Server[ 'SpecPort' ] = $Buffer->GetShort();
$Server[ 'SpecName' ] = $Buffer->GetString();
}
// S2A_EXTRA_DATA_HAS_GAMETAG_DATA - Next bytes are the game tag string
if( $Flags & 0x20 )
{
if ($Flags & 0x20) {
$Server[ 'GameTags' ] = $Buffer->GetString();
}
// S2A_EXTRA_DATA_GAMEID - Next 8 bytes are the gameID of the server
if( $Flags & 0x01 )
{
if ($Flags & 0x01) {
$Server[ 'GameID' ] = $Buffer->GetUnsignedLong() | ($Buffer->GetUnsignedLong() << 32);
}
if( $Buffer->Remaining( ) > 0 )
{
throw new InvalidPacketException( 'GetInfo: unread data? ' . $Buffer->Remaining( ) . ' bytes remaining in the buffer. Please report it to the library developer.',
InvalidPacketException::BUFFER_NOT_EMPTY );
if ($Buffer->Remaining() > 0) {
throw new InvalidPacketException(
'GetInfo: unread data? ' . $Buffer->Remaining() . ' bytes remaining in the buffer. Please report it to the library developer.',
InvalidPacketException::BUFFER_NOT_EMPTY
);
}
}
@ -375,8 +355,7 @@
*/
public function GetPlayers(): array
{
if( !$this->Connected )
{
if (!$this->Connected) {
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
}
@ -388,16 +367,14 @@
$Type = $Buffer->GetByte();
if( $Type !== self::S2A_PLAYER )
{
if ($Type !== self::S2A_PLAYER) {
throw new InvalidPacketException('GetPlayers: Packet header mismatch. (0x' . dechex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
$Players = [];
$Count = $Buffer->GetByte();
while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
{
while ($Count-- > 0 && $Buffer->Remaining() > 0) {
$Player = [];
$Player[ 'Id' ] = $Buffer->GetByte(); // PlayerID, is it just always 0?
$Player[ 'Name' ] = $Buffer->GetString();
@ -421,8 +398,7 @@
*/
public function GetRules(): array
{
if( !$this->Connected )
{
if (!$this->Connected) {
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
}
@ -433,21 +409,18 @@
$Type = $Buffer->GetByte();
if( $Type !== self::S2A_RULES )
{
if ($Type !== self::S2A_RULES) {
throw new InvalidPacketException('GetRules: Packet header mismatch. (0x' . dechex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
$Rules = [];
$Count = $Buffer->GetShort();
while( $Count-- > 0 && $Buffer->Remaining( ) > 0 )
{
while ($Count-- > 0 && $Buffer->Remaining() > 0) {
$Rule = $Buffer->GetString();
$Value = $Buffer->GetString();
if( !empty( $Rule ) )
{
if (!empty($Rule)) {
$Rules[ $Rule ] = $Value;
}
}
@ -462,13 +435,11 @@
*/
private function GetChallenge(int $Header, int $ExpectedResult): void
{
if( $this->Challenge )
{
if ($this->Challenge) {
return;
}
if( $this->UseOldGetChallengeMethod )
{
if ($this->UseOldGetChallengeMethod) {
$Header = self::A2S_SERVERQUERY_GETCHALLENGE;
}
@ -477,8 +448,7 @@
$Type = $Buffer->GetByte();
switch( $Type )
{
switch ($Type) {
case self::S2C_CHALLENGE:
{
$this->Challenge = $Buffer->Get(4);
@ -513,13 +483,11 @@
*/
public function SetRconPassword(string $Password): void
{
if( !$this->Connected )
{
if (!$this->Connected) {
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
}
switch( $this->Socket->Engine )
{
switch ($this->Socket->Engine) {
case SourceQuery::GOLDSOURCE:
{
$this->Rcon = new GoldSourceRcon($this->Socket);
@ -555,13 +523,11 @@
*/
public function Rcon(string $Command): string
{
if( !$this->Connected )
{
if (!$this->Connected) {
throw new SocketException('Not connected.', SocketException::NOT_CONNECTED);
}
if( $this->Rcon === null )
{
if ($this->Rcon === null) {
throw new SocketException('You must set a RCON password before trying to execute a RCON command.', SocketException::NOT_CONNECTED);
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
@ -21,11 +24,11 @@
*
* @package xPaw\SourceQuery
*
* @uses xPaw\SourceQuery\Exception\AuthenticationException
* @uses xPaw\SourceQuery\Exception\InvalidPacketException
* @uses xPaw\SourceQuery\Exception\SocketException
* @uses AuthenticationException
* @uses InvalidPacketException
* @uses SocketException
*/
class SourceRcon
final class SourceRcon
{
/**
* Points to socket class
@ -43,8 +46,7 @@
public function Close(): void
{
if( $this->RconSocket )
{
if ($this->RconSocket) {
fclose($this->RconSocket);
$this->RconSocket = null;
@ -55,12 +57,10 @@
public function Open(): void
{
if( !$this->RconSocket )
{
if (!$this->RconSocket) {
$RconSocket = @fsockopen($this->Socket->Address, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout);
if( $ErrNo || !$RconSocket )
{
if ($ErrNo || !$RconSocket) {
throw new SocketException('Can\'t connect to RCON server: ' . $ErrStr, SocketException::CONNECTION_FAILED);
}
@ -87,8 +87,7 @@
$Buffer = new Buffer();
$Buffer->Set(fread($this->RconSocket, 4));
if( $Buffer->Remaining( ) < 4 )
{
if ($Buffer->Remaining() < 4) {
throw new InvalidPacketException('Rcon read: Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
}
@ -100,14 +99,12 @@
$Remaining = $PacketSize - strlen($Data);
while( $Remaining > 0 )
{
while ($Remaining > 0) {
$Data2 = fread($this->RconSocket, $Remaining);
$PacketSize = strlen($Data2);
if( $PacketSize === 0 )
{
if ($PacketSize === 0) {
throw new InvalidPacketException('Read ' . strlen($Data) . ' bytes from socket, ' . $Remaining . ' remaining', InvalidPacketException::BUFFER_EMPTY);
}
@ -129,12 +126,9 @@
$Type = $Buffer->GetLong();
if( $Type === SourceQuery::SERVERDATA_AUTH_RESPONSE )
{
if ($Type === SourceQuery::SERVERDATA_AUTH_RESPONSE) {
throw new AuthenticationException('Bad rcon_password.', AuthenticationException::BAD_PASSWORD);
}
else if( $Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
{
} elseif ($Type !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
throw new InvalidPacketException('Invalid rcon response.', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
@ -142,31 +136,26 @@
// We do this stupid hack to handle split packets
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
if( strlen( $Data ) >= 4000 )
{
if (strlen($Data) >= 4000) {
$this->Write(SourceQuery::SERVERDATA_REQUESTVALUE);
do
{
do {
$Buffer = $this->Read();
$Buffer->GetLong(); // RequestID
if( $Buffer->GetLong( ) !== SourceQuery::SERVERDATA_RESPONSE_VALUE )
{
if ($Buffer->GetLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
break;
}
$Data2 = $Buffer->Get();
if( $Data2 === "\x00\x01\x00\x00\x00\x00" )
{
if ($Data2 === "\x00\x01\x00\x00\x00\x00") {
break;
}
$Data .= $Data2;
}
while( true );
} while (true);
}
return rtrim($Data, "\0");
@ -183,16 +172,14 @@
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
if( $Type === SourceQuery::SERVERDATA_RESPONSE_VALUE )
{
if ($Type === SourceQuery::SERVERDATA_RESPONSE_VALUE) {
$Buffer = $this->Read();
$RequestID = $Buffer->GetLong();
$Type = $Buffer->GetLong();
}
if( $RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE )
{
if ($RequestID === -1 || $Type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) {
throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
}
}

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* Library to query servers that implement Source Engine Query protocol.
*

@ -1,20 +1,21 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use xPaw\SourceQuery\BaseSocket;
use xPaw\SourceQuery\SourceQuery;
use xPaw\SourceQuery\Buffer;
class TestableSocket extends BaseSocket
final class TestableSocket extends BaseSocket
{
/** @var \SplQueue<string> */
private \SplQueue $PacketQueue;
public function __construct()
{
/** @var \SplQueue<string> */
$this->PacketQueue = new \SplQueue();
$this->PacketQueue->setIteratorMode(\SplDoublyLinkedList::IT_MODE_DELETE);
}
public function Queue(string $Data): void
@ -52,8 +53,7 @@
public function Sherlock(Buffer $Buffer, int $Length): bool
{
if( $this->PacketQueue->isEmpty() )
{
if ($this->PacketQueue->isEmpty()) {
return false;
}
@ -63,7 +63,7 @@
}
}
class Tests extends \PHPUnit\Framework\TestCase
final class Tests extends TestCase
{
private TestableSocket $Socket;
private SourceQuery $SourceQuery;
@ -145,8 +145,7 @@
*/
public function testGetInfo(string $RawInput, array $ExpectedOutput): void
{
if( isset( $ExpectedOutput[ 'IsMod' ] ) )
{
if (isset($ExpectedOutput[ 'IsMod' ])) {
$this->Socket->Engine = SourceQuery::GOLDSOURCE;
}
@ -154,7 +153,7 @@
$RealOutput = $this->SourceQuery->GetInfo();
$this->assertEquals( $ExpectedOutput, $RealOutput );
self::assertEquals($ExpectedOutput, $RealOutput);
}
public function InfoProvider(): array
@ -163,8 +162,7 @@
$Files = glob(__DIR__ . '/Info/*.raw', GLOB_ERR);
foreach( $Files as $File )
{
foreach ($Files as $File) {
$DataProvider[] =
[
hex2bin(trim(file_get_contents($File))),
@ -239,10 +237,10 @@
{
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x41\x11\x11\x11\x11");
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x45\x01\x00ayy\x00lmao\x00");
$this->assertEquals( [ 'ayy' => 'lmao' ], $this->SourceQuery->GetRules() );
self::assertEquals([ 'ayy' => 'lmao' ], $this->SourceQuery->GetRules());
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x45\x01\x00wow\x00much\x00");
$this->assertEquals( [ 'wow' => 'much' ], $this->SourceQuery->GetRules() );
self::assertEquals([ 'wow' => 'much' ], $this->SourceQuery->GetRules());
}
/**
@ -253,14 +251,13 @@
{
$this->Socket->Queue(hex2bin("ffffffff4104fce20e")); // Challenge
foreach( $RawInput as $Packet )
{
foreach ($RawInput as $Packet) {
$this->Socket->Queue(hex2bin($Packet));
}
$RealOutput = $this->SourceQuery->GetRules();
$this->assertEquals( $ExpectedOutput, $RealOutput );
self::assertEquals($ExpectedOutput, $RealOutput);
}
public function RulesProvider(): array
@ -269,8 +266,7 @@
$Files = glob(__DIR__ . '/Rules/*.raw', GLOB_ERR);
foreach( $Files as $File )
{
foreach ($Files as $File) {
$DataProvider[] =
[
file($File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
@ -289,14 +285,13 @@
{
$this->Socket->Queue(hex2bin("ffffffff4104fce20e")); // Challenge
foreach( $RawInput as $Packet )
{
foreach ($RawInput as $Packet) {
$this->Socket->Queue(hex2bin($Packet));
}
$RealOutput = $this->SourceQuery->GetPlayers();
$this->assertEquals( $ExpectedOutput, $RealOutput );
self::assertEquals($ExpectedOutput, $RealOutput);
}
public function PlayersProvider(): array
@ -305,8 +300,7 @@
$Files = glob(__DIR__ . '/Players/*.raw', GLOB_ERR);
foreach( $Files as $File )
{
foreach ($Files as $File) {
$DataProvider[] =
[
file($File, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
@ -320,9 +314,9 @@
public function testPing(): void
{
$this->Socket->Queue("\xFF\xFF\xFF\xFF\x6A\x00");
$this->assertTrue( $this->SourceQuery->Ping() );
self::assertTrue($this->SourceQuery->Ping());
$this->Socket->Queue("\xFF\xFF\xFF\xFF\xEE");
$this->assertFalse( $this->SourceQuery->Ping() );
self::assertFalse($this->SourceQuery->Ping());
}
}

@ -24,7 +24,8 @@
{
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.7",
"phpstan/phpstan": "^0.12.83"
"phpstan/phpstan": "^0.12.83",
"friendsofphp/php-cs-fixer": "^3.0"
},
"autoload":
{

Loading…
Cancel
Save