|
|
|
@ -1,9 +1,13 @@
|
|
|
|
|
<?php
|
|
|
|
|
class SourceQueryException extends Exception
|
|
|
|
|
if( !defined( '__DIR__' ) ) // PHP < 5.3
|
|
|
|
|
{
|
|
|
|
|
// Exception thrown by SourceQuery class
|
|
|
|
|
define( '__DIR__', dirname( __FILE__ ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
require __DIR__ . '/Exception.class.php';
|
|
|
|
|
require __DIR__ . '/Buffer.class.php';
|
|
|
|
|
require __DIR__ . '/Socket.class.php';
|
|
|
|
|
require __DIR__ . '/Rcon.class.php';
|
|
|
|
|
|
|
|
|
|
class SourceQuery
|
|
|
|
|
{
|
|
|
|
@ -240,7 +244,7 @@ class SourceQuery
|
|
|
|
|
|
|
|
|
|
if( $Type != self :: S2A_INFO )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'GetInfo: Packet header mismatch. (' . $Type . ')' );
|
|
|
|
|
throw new SourceQueryException( 'GetInfo: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Server[ 'Protocol' ] = $this->Buffer->GetByte( );
|
|
|
|
@ -336,7 +340,7 @@ class SourceQuery
|
|
|
|
|
}
|
|
|
|
|
else if( $Type != self :: S2A_PLAYER )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'GetPlayers: Packet header mismatch. (' . $Type . ')' );
|
|
|
|
|
throw new SourceQueryException( 'GetPlayers: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
@ -393,7 +397,7 @@ class SourceQuery
|
|
|
|
|
}
|
|
|
|
|
else if( $Type != self :: S2A_RULES )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'GetRules: Packet header mismatch. (' . $Type . ')' );
|
|
|
|
|
throw new SourceQueryException( 'GetRules: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
@ -454,7 +458,7 @@ class SourceQuery
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'GetChallenge: Packet header mismatch. (' . $Type . ')' );
|
|
|
|
|
throw new SourceQueryException( 'GetChallenge: Packet header mismatch. (0x' . DecHex( $Type ) . ')' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -495,552 +499,3 @@ class SourceQuery
|
|
|
|
|
return $this->Rcon->Command( $Command );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SourceQuerySocket
|
|
|
|
|
{
|
|
|
|
|
public $Socket;
|
|
|
|
|
public $Engine;
|
|
|
|
|
|
|
|
|
|
public $Ip;
|
|
|
|
|
public $Port;
|
|
|
|
|
public $Timeout;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Points to buffer class
|
|
|
|
|
*
|
|
|
|
|
* @var SourceQueryBuffer
|
|
|
|
|
*/
|
|
|
|
|
private $Buffer;
|
|
|
|
|
|
|
|
|
|
public function __construct( $Buffer )
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer = $Buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Close( )
|
|
|
|
|
{
|
|
|
|
|
if( $this->Socket )
|
|
|
|
|
{
|
|
|
|
|
FClose( $this->Socket );
|
|
|
|
|
|
|
|
|
|
$this->Socket = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Open( $Ip, $Port, $Timeout, $Engine )
|
|
|
|
|
{
|
|
|
|
|
$this->Timeout = $Timeout;
|
|
|
|
|
$this->Engine = $Engine;
|
|
|
|
|
$this->Port = $Port;
|
|
|
|
|
$this->Ip = $Ip;
|
|
|
|
|
|
|
|
|
|
$this->Socket = FSockOpen( 'udp://' . $Ip, $Port, $ErrNo, $ErrStr, $Timeout );
|
|
|
|
|
|
|
|
|
|
if( $ErrNo || !$this->Socket )
|
|
|
|
|
{
|
|
|
|
|
throw new Exception( 'Could not create socket: ' . $ErrStr );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream_Set_Timeout( $this->Socket, $Timeout );
|
|
|
|
|
Stream_Set_Blocking( $this->Socket, true );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Write( $Header, $String = '' )
|
|
|
|
|
{
|
|
|
|
|
$Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
|
|
|
|
|
$Length = StrLen( $Command );
|
|
|
|
|
|
|
|
|
|
return $Length === FWrite( $this->Socket, $Command, $Length );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Read( $Length = 1400 )
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer->Set( FRead( $this->Socket, $Length ) );
|
|
|
|
|
|
|
|
|
|
if( $this->Buffer->Remaining( ) > 0 && $this->Buffer->GetLong( ) == -2 )
|
|
|
|
|
{
|
|
|
|
|
$Packets = Array( );
|
|
|
|
|
$IsCompressed = false;
|
|
|
|
|
$ReadMore = false;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
$RequestID = $this->Buffer->GetLong( );
|
|
|
|
|
|
|
|
|
|
switch( $this->Engine )
|
|
|
|
|
{
|
|
|
|
|
case SourceQuery :: GOLDSOURCE:
|
|
|
|
|
{
|
|
|
|
|
$PacketCountAndNumber = $this->Buffer->GetByte( );
|
|
|
|
|
$PacketCount = $PacketCountAndNumber & 0xF;
|
|
|
|
|
$PacketNumber = $PacketCountAndNumber >> 4;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SourceQuery :: SOURCE:
|
|
|
|
|
{
|
|
|
|
|
$IsCompressed = ( $RequestID & 0x80000000 ) != 0;
|
|
|
|
|
$PacketCount = $this->Buffer->GetByte( );
|
|
|
|
|
$PacketNumber = $this->Buffer->GetByte( ) + 1;
|
|
|
|
|
|
|
|
|
|
if( $IsCompressed )
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer->GetLong( ); // Split size
|
|
|
|
|
|
|
|
|
|
$PacketChecksum = $this->Buffer->GetUnsignedLong( );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer->GetShort( ); // Split size
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Packets[ $PacketNumber ] = $this->Buffer->Get( );
|
|
|
|
|
|
|
|
|
|
$ReadMore = $PacketCount > sizeof( $Packets );
|
|
|
|
|
}
|
|
|
|
|
while( $ReadMore && $this->Sherlock( $Length ) );
|
|
|
|
|
|
|
|
|
|
$Buffer = Implode( $Packets );
|
|
|
|
|
|
|
|
|
|
// TODO: Test this
|
|
|
|
|
if( $IsCompressed )
|
|
|
|
|
{
|
|
|
|
|
// Let's make sure this function exists, it's not included in PHP by default
|
|
|
|
|
if( !Function_Exists( 'bzdecompress' ) )
|
|
|
|
|
{
|
|
|
|
|
throw new RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Data = bzdecompress( $Data );
|
|
|
|
|
|
|
|
|
|
if( CRC32( $Data ) != $PacketChecksum )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'CRC32 checksum mismatch of uncompressed packet data.' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->Buffer->Set( SubStr( $Buffer, 4 ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function Sherlock( $Length )
|
|
|
|
|
{
|
|
|
|
|
$Data = FRead( $this->Socket, $Length );
|
|
|
|
|
|
|
|
|
|
if( StrLen( $Data ) < 4 )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->Buffer->Set( $Data );
|
|
|
|
|
|
|
|
|
|
return $this->Buffer->GetLong( ) == -2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SourceQueryRcon
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Points to buffer class
|
|
|
|
|
*
|
|
|
|
|
* @var SourceQueryBuffer
|
|
|
|
|
*/
|
|
|
|
|
private $Buffer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Points to socket class
|
|
|
|
|
*
|
|
|
|
|
* @var SourceQuerySocket
|
|
|
|
|
*/
|
|
|
|
|
private $Socket;
|
|
|
|
|
|
|
|
|
|
private $RconSocket;
|
|
|
|
|
private $RconPassword;
|
|
|
|
|
private $RconRequestId;
|
|
|
|
|
private $RconChallenge;
|
|
|
|
|
|
|
|
|
|
public function __construct( $Buffer, $Socket )
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer = $Buffer;
|
|
|
|
|
$this->Socket = $Socket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Close( )
|
|
|
|
|
{
|
|
|
|
|
if( $this->RconSocket )
|
|
|
|
|
{
|
|
|
|
|
FClose( $this->RconSocket );
|
|
|
|
|
|
|
|
|
|
$this->RconSocket = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->RconChallenge = 0;
|
|
|
|
|
$this->RconRequestId = 0;
|
|
|
|
|
$this->RconPassword = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Open( )
|
|
|
|
|
{
|
|
|
|
|
if( !$this->RconSocket && $this->Socket->Engine == SourceQuery :: SOURCE )
|
|
|
|
|
{
|
|
|
|
|
$this->RconSocket = FSockOpen( $this->Socket->Ip, $this->Socket->Port, $ErrNo, $ErrStr, $this->Socket->Timeout );
|
|
|
|
|
|
|
|
|
|
if( $ErrNo || !$this->RconSocket )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'Can\'t connect to RCON server: ' . $ErrStr );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream_Set_Timeout( $this->RconSocket, $this->Socket->Timeout );
|
|
|
|
|
Stream_Set_Blocking( $this->RconSocket, true );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Write( $Header, $String = '' )
|
|
|
|
|
{
|
|
|
|
|
switch( $this->Socket->Engine )
|
|
|
|
|
{
|
|
|
|
|
case SourceQuery :: GOLDSOURCE:
|
|
|
|
|
{
|
|
|
|
|
$Command = Pack( 'cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $String );
|
|
|
|
|
$Length = StrLen( $Command );
|
|
|
|
|
|
|
|
|
|
return $Length === FWrite( $this->Socket->Socket, $Command, $Length );
|
|
|
|
|
}
|
|
|
|
|
case SourceQuery :: SOURCE:
|
|
|
|
|
{
|
|
|
|
|
// Pack the packet together
|
|
|
|
|
$Command = Pack( 'VV', ++$this->RequestId, $Header ) . $String . "\x00\x00\x00";
|
|
|
|
|
|
|
|
|
|
// Prepend packet length
|
|
|
|
|
$Command = Pack( 'V', StrLen( $Command ) ) . $Command;
|
|
|
|
|
$Length = StrLen( $Command );
|
|
|
|
|
|
|
|
|
|
return $Length === FWrite( $this->RconSocket, $Command, $Length );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Read( $Length = 1400 )
|
|
|
|
|
{
|
|
|
|
|
switch( $this->Socket->Engine )
|
|
|
|
|
{
|
|
|
|
|
case SourceQuery :: GOLDSOURCE:
|
|
|
|
|
{
|
|
|
|
|
// GoldSource RCON has same structure as Query
|
|
|
|
|
$this->Socket->Read( );
|
|
|
|
|
|
|
|
|
|
if( $this->Buffer->GetByte( ) != SourceQuery :: S2A_RCON )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Buffer = $this->Buffer->Get( );
|
|
|
|
|
$Trimmed = Trim( $Buffer );
|
|
|
|
|
|
|
|
|
|
if( $Trimmed == 'Bad rcon_password.'
|
|
|
|
|
|| $Trimmed == 'You have been banned from this server.' )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( $Trimmed );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ReadMore = false;
|
|
|
|
|
|
|
|
|
|
// There is no indentifier of the end, so we just need to continue reading
|
|
|
|
|
// TODO: Needs to be looked again, it causes timeouts
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
$this->Socket->Read( );
|
|
|
|
|
|
|
|
|
|
$ReadMore = $this->Buffer->Remaining( ) > 0 && $this->Buffer->GetByte( ) == SourceQuery :: S2A_RCON;
|
|
|
|
|
|
|
|
|
|
if( $ReadMore )
|
|
|
|
|
{
|
|
|
|
|
$Packet = $this->Buffer->Get( );
|
|
|
|
|
$Buffer .= SubStr( $Packet, 0, -2 );
|
|
|
|
|
|
|
|
|
|
// Let's assume if this packet is not long enough, there are no more after this one
|
|
|
|
|
$ReadMore = StrLen( $Packet ) > 1000; // use 1300?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while( $ReadMore );
|
|
|
|
|
|
|
|
|
|
$this->Buffer->Set( Trim( $Buffer ) );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SourceQuery :: SOURCE:
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer->Set( FRead( $this->RconSocket, $Length ) );
|
|
|
|
|
|
|
|
|
|
$Buffer = "";
|
|
|
|
|
|
|
|
|
|
$PacketSize = $this->Buffer->GetLong( );
|
|
|
|
|
|
|
|
|
|
$Buffer .= $this->Buffer->Get( );
|
|
|
|
|
|
|
|
|
|
// TODO: multi packet reading
|
|
|
|
|
|
|
|
|
|
$this->Buffer->Set( $Buffer );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Command( $Command )
|
|
|
|
|
{
|
|
|
|
|
if( !$this->RconChallenge )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Buffer = false;
|
|
|
|
|
|
|
|
|
|
switch( $this->Socket->Engine )
|
|
|
|
|
{
|
|
|
|
|
case SourceQuery :: GOLDSOURCE:
|
|
|
|
|
{
|
|
|
|
|
$this->Write( 0, 'rcon ' . $this->RconChallenge . ' "' . $this->RconPassword . '" ' . $Command . "\0" );
|
|
|
|
|
$this->Read( );
|
|
|
|
|
|
|
|
|
|
$Buffer = $this->Buffer->Get( );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SourceQuery :: SOURCE:
|
|
|
|
|
{
|
|
|
|
|
$this->Write( SourceQuery :: SERVERDATA_EXECCOMMAND, $Command );
|
|
|
|
|
$this->Read( );
|
|
|
|
|
|
|
|
|
|
$RequestID = $this->Buffer->GetLong( );
|
|
|
|
|
$Type = $this->Buffer->GetLong( );
|
|
|
|
|
|
|
|
|
|
if( $Type == SourceQuery :: SERVERDATA_AUTH_RESPONSE )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'Bad rcon_password.' );
|
|
|
|
|
}
|
|
|
|
|
else if( $Type != SourceQuery :: SERVERDATA_RESPONSE_VALUE )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: It should use GetString, but there are no null bytes at the end, why?
|
|
|
|
|
// $Buffer = $this->Buffer->GetString( );
|
|
|
|
|
$Buffer = $this->Buffer->Get( );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $Buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Authorize( $Password )
|
|
|
|
|
{
|
|
|
|
|
$this->RconPassword = $Password;
|
|
|
|
|
|
|
|
|
|
switch( $this->Socket->Engine )
|
|
|
|
|
{
|
|
|
|
|
case SourceQuery :: GOLDSOURCE:
|
|
|
|
|
{
|
|
|
|
|
$this->Write( 0, 'challenge rcon' );
|
|
|
|
|
$this->Socket->Read( );
|
|
|
|
|
|
|
|
|
|
if( $this->Buffer->Get( 14 ) != 'challenge rcon' )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->RconChallenge = Trim( $this->Buffer->Get( ) );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SourceQuery :: SOURCE:
|
|
|
|
|
{
|
|
|
|
|
$this->Write( SourceQuery :: SERVERDATA_AUTH, $Password );
|
|
|
|
|
$this->Read( );
|
|
|
|
|
|
|
|
|
|
$RequestID = $this->Buffer->GetLong( );
|
|
|
|
|
$Type = $this->Buffer->GetLong( );
|
|
|
|
|
|
|
|
|
|
// 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 )
|
|
|
|
|
{
|
|
|
|
|
$this->Read( );
|
|
|
|
|
|
|
|
|
|
$RequestID = $this->Buffer->GetLong( );
|
|
|
|
|
$Type = $this->Buffer->GetLong( );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( $RequestID == -1 || $Type != SourceQuery :: SERVERDATA_AUTH_RESPONSE )
|
|
|
|
|
{
|
|
|
|
|
throw new SourceQueryException( 'RCON authorization failed.' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->RconChallenge = 1;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SourceQueryBuffer
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Buffer
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $Buffer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Buffer length
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $Length;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Current position in buffer
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $Position;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets buffer
|
|
|
|
|
*
|
|
|
|
|
* @param string $Buffer Buffer
|
|
|
|
|
*/
|
|
|
|
|
public function Set( $Buffer )
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer = $Buffer;
|
|
|
|
|
$this->Length = StrLen( $Buffer );
|
|
|
|
|
$this->Position = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets buffer
|
|
|
|
|
*/
|
|
|
|
|
public function Reset( )
|
|
|
|
|
{
|
|
|
|
|
$this->Buffer = "";
|
|
|
|
|
$this->Length = 0;
|
|
|
|
|
$this->Position = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get remaining bytes
|
|
|
|
|
*
|
|
|
|
|
* @return int Remaining bytes in buffer
|
|
|
|
|
*/
|
|
|
|
|
public function Remaining( )
|
|
|
|
|
{
|
|
|
|
|
return $this->Length - $this->Position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets data from buffer
|
|
|
|
|
*
|
|
|
|
|
* @param int $Length Bytes to read
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function Get( $Length = -1 )
|
|
|
|
|
{
|
|
|
|
|
if( $Length == 0 )
|
|
|
|
|
{
|
|
|
|
|
// Why even bother
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Remaining = $this->Remaining( );
|
|
|
|
|
|
|
|
|
|
if( $Length == -1 )
|
|
|
|
|
{
|
|
|
|
|
$Length = $Remaining;
|
|
|
|
|
}
|
|
|
|
|
else if( $Length > $Remaining )
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$Data = SubStr( $this->Buffer, $this->Position, $Length );
|
|
|
|
|
|
|
|
|
|
$this->Position += $Length;
|
|
|
|
|
|
|
|
|
|
return $Data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get byte from buffer
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function GetByte( )
|
|
|
|
|
{
|
|
|
|
|
return Ord( $this->Get( 1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function GetShort( )
|
|
|
|
|
{
|
|
|
|
|
$Data = UnPack( 'v', $this->Get( 2 ) );
|
|
|
|
|
|
|
|
|
|
return $Data[ 1 ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function GetLong( )
|
|
|
|
|
{
|
|
|
|
|
$Data = UnPack( 'l', $this->Get( 4 ) );
|
|
|
|
|
|
|
|
|
|
return $Data[ 1 ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function GetFloat( )
|
|
|
|
|
{
|
|
|
|
|
$Data = UnPack( 'f', $this->Get( 4 ) );
|
|
|
|
|
|
|
|
|
|
return $Data[ 1 ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function GetUnsignedLong( )
|
|
|
|
|
{
|
|
|
|
|
$Data = UnPack( 'V', $this->Get( 4 ) );
|
|
|
|
|
|
|
|
|
|
return $Data[ 1 ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read one string from buffer ending with null byte
|
|
|
|
|
*
|
|
|
|
|
* @return string String
|
|
|
|
|
*/
|
|
|
|
|
public function GetString( )
|
|
|
|
|
{
|
|
|
|
|
$ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position );
|
|
|
|
|
|
|
|
|
|
if( $ZeroBytePosition === false )
|
|
|
|
|
{
|
|
|
|
|
$String = "";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$String = $this->Get( $ZeroBytePosition - $this->Position );
|
|
|
|
|
|
|
|
|
|
$this->Position++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $String;
|
|
|
|
|
}
|
|
|
|
|
}
|