cherry pick maniaplanet lib 6.1

This commit is contained in:
Beu 2024-08-25 22:30:06 +02:00
parent 3044e466b9
commit c8bbb3d7c6
18 changed files with 1626 additions and 1698 deletions

View File

@ -299,7 +299,6 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener,
}
// Delete client
Connection::delete($this->getClient());
$this->client = null;
}
@ -722,7 +721,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener,
Logger::log("Connecting to Server at {$serverConfig->host}:{$serverConfig->port}...");
try {
$this->client = Connection::factory($serverConfig->host, $serverConfig->port, self::SCRIPT_TIMEOUT, $serverConfig->user, $serverConfig->pass, self::API_VERSION);
$this->client = new Connection($serverConfig->host, $serverConfig->port, self::SCRIPT_TIMEOUT, $serverConfig->user, $serverConfig->pass, self::API_VERSION);
} catch (TransportException $exception) {
$message = "Couldn't connect to the server: '{$exception->getMessage()}'";
$this->quit($message, true);

2554
libs/Maniaplanet/DedicatedServer/Connection.php Executable file → Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,46 +9,52 @@ namespace Maniaplanet\DedicatedServer\Structures;
abstract class AbstractStructure
{
static public function fromArray($array)
public static function fromArrayOfArray($array)
{
if(!is_array($array))
if (!is_array($array)) {
return $array;
}
$object = new static;
foreach($array as $key => $value)
$object->{lcfirst($key)} = $value;
return $object;
}
static public function fromArrayOfArray($array)
{
if(!is_array($array))
return $array;
$result = array();
foreach($array as $key => $value)
$result = [];
foreach ($array as $key => $value) {
$result[$key] = static::fromArray($value);
}
return $result;
}
static public function getPropertyFromArray($array, $property)
public static function fromArray($array)
{
return array_map(get_called_class().'::extractProperty', $array, array_fill(0, count($array), $property));
if (!is_array($array)) {
return $array;
}
$object = new static;
foreach ($array as $key => $value) {
$object->{lcfirst($key)} = $value;
}
return $object;
}
static protected function extractProperty($element, $property)
public static function getPropertyFromArray($array, $property)
{
if(!is_a($element, get_called_class()) || !property_exists($element, $property))
throw new \InvalidArgumentException('property '.$property.' does not exists in class: '.get_called_class());
return array_map(get_called_class() . '::extractProperty', $array, array_fill(0, count($array), $property));
}
protected static function extractProperty($element, $property)
{
if (!is_a($element, get_called_class()) || !property_exists($element, $property)) {
throw new \InvalidArgumentException('property ' . $property . ' does not exists in class: ' . get_called_class());
}
return $element->$property;
}
function toArray()
{
$out = array();
foreach(get_object_vars($this) as $key => $value)
$out = [];
foreach (get_object_vars($this) as $key => $value) {
$out[ucfirst($key)] = $value;
}
return $out;
}
}

View File

@ -10,11 +10,11 @@ namespace Maniaplanet\DedicatedServer\Structures;
class Bill extends AbstractStructure
{
const STATE_CREATING_TRANSACTION = 1;
const STATE_ISSUED = 2;
const STATE_VALIDATING_PAYMENT = 3;
const STATE_PAYED = 4;
const STATE_REFUSED = 5;
const STATE_ERROR = 6;
const STATE_ISSUED = 2;
const STATE_VALIDATING_PAYMENT = 3;
const STATE_PAYED = 4;
const STATE_REFUSED = 5;
const STATE_ERROR = 6;
/** @var int */
public $state;

View File

@ -12,13 +12,13 @@ class GameInfos extends AbstractStructure
/**
* Game Modes
*/
const GAMEMODE_SCRIPT = 0;
const GAMEMODE_ROUNDS = 1;
const GAMEMODE_SCRIPT = 0;
const GAMEMODE_ROUNDS = 1;
const GAMEMODE_TIMEATTACK = 2;
const GAMEMODE_TEAM = 3;
const GAMEMODE_LAPS = 4;
const GAMEMODE_CUP = 5;
const GAMEMODE_STUNTS = 6;
const GAMEMODE_TEAM = 3;
const GAMEMODE_LAPS = 4;
const GAMEMODE_CUP = 5;
const GAMEMODE_STUNTS = 6;
/** @var int */
public $gameMode;

View File

@ -28,7 +28,7 @@ class NetworkStats extends AbstractStructure
/** @var PlayerNetInfo[] */
public $playerNetInfos;
static public function fromArray($array)
public static function fromArray($array)
{
$object = parent::fromArray($array);
$object->playerNetInfos = PlayerNetInfo::fromArrayOfArray($object->playerNetInfos);

1
libs/Maniaplanet/DedicatedServer/Structures/Player.php Executable file → Normal file
View File

@ -4,6 +4,7 @@
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
*/
namespace Maniaplanet\DedicatedServer\Structures;
class Player extends AbstractStructure

View File

@ -46,10 +46,22 @@ class PlayerDetailedInfo extends Player
/** @var string */
public $broadcasterLogin;
/** @var string[] */
public $allies = array();
public $allies = [];
/** @var string */
public $clubLink;
/**
* @return PlayerDetailedInfo
*/
public static function fromArray($array)
{
$object = parent::fromArray($array);
$object->avatar = FileDesc::fromArray($object->avatar);
$object->skins = Skin::fromArrayOfArray($object->skins);
$object->ladderStats = LadderStats::fromArray($object->ladderStats);
return $object;
}
/**
* @return string[]
*/
@ -57,16 +69,4 @@ class PlayerDetailedInfo extends Player
{
return explode('|', $this->path);
}
/**
* @return PlayerDetailedInfo
*/
static public function fromArray($array)
{
$object = parent::fromArray($array);
$object->avatar = FileDesc::fromArray($object->avatar);
$object->skins = Skin::fromArrayOfArray($object->skins);
$object->ladderStats = LadderStats::fromArray($object->ladderStats);
return $object;
}
}

View File

@ -20,7 +20,7 @@ class PlayerInfo extends Player
/** @var bool */
public $isInOfficialMode;
/** @var int */
public $ladderScore;
public $ladderScore; // TODO CHECK IF EXISTS
/** @var int */
public $ladderRanking;
/** @var int */
@ -63,26 +63,26 @@ class PlayerInfo extends Player
/**
* @return PlayerInfo
*/
static public function fromArray($array)
public static function fromArray($array)
{
$object = parent::fromArray($array);
//Detail flags
$object->forceSpectator = $object->flags % 10; // 0, 1 or 2
$object->isReferee = (bool) (intval($object->flags / 10) % 10);
$object->isPodiumReady = (bool) (intval($object->flags / 100) % 10);
$object->isUsingStereoscopy = (bool) (intval($object->flags / 1000) % 10);
$object->isManagedByAnOtherServer = (bool) (intval($object->flags / 10000) % 10);
$object->isServer = (bool) (intval($object->flags / 100000) % 10);
$object->hasPlayerSlot = (bool) (intval($object->flags / 1000000) % 10);
$object->isBroadcasting = (bool) (intval($object->flags / 10000000) % 10);
$object->hasJoinedGame = (bool) (intval($object->flags / 100000000) % 10);
$object->forceSpectator = $object->flags % 10; // 0, 1 or 2
$object->isReferee = (bool)(intval($object->flags / 10) % 10);
$object->isPodiumReady = (bool)(intval($object->flags / 100) % 10);
$object->isUsingStereoscopy = (bool)(intval($object->flags / 1000) % 10);
$object->isManagedByAnOtherServer = (bool)(intval($object->flags / 10000) % 10);
$object->isServer = (bool)(intval($object->flags / 100000) % 10);
$object->hasPlayerSlot = (bool)(intval($object->flags / 1000000) % 10);
$object->isBroadcasting = (bool)(intval($object->flags / 10000000) % 10);
$object->hasJoinedGame = (bool)(intval($object->flags / 100000000) % 10);
//Details spectatorStatus
$object->spectator = (bool) ($object->spectatorStatus % 10);
$object->temporarySpectator = (bool) (intval($object->spectatorStatus / 10) % 10);
$object->pureSpectator = (bool) (intval($object->spectatorStatus / 100) % 10);
$object->autoTarget = (bool) (intval($object->spectatorStatus / 1000) % 10);
$object->currentTargetId = intval($object->spectatorStatus / 10000);
$object->spectator = (bool)($object->spectatorStatus % 10);
$object->temporarySpectator = (bool)(intval($object->spectatorStatus / 10) % 10);
$object->pureSpectator = (bool)(intval($object->spectatorStatus / 100) % 10);
$object->autoTarget = (bool)(intval($object->spectatorStatus / 1000) % 10);
$object->currentTargetId = intval($object->spectatorStatus / 10000);
return $object;
}

View File

@ -18,9 +18,9 @@ class ScriptInfo extends AbstractStructure
/** @var string */
public $version;
/** @var ScriptSettings[] */
public $paramDescs = array();
public $paramDescs = [];
/** @var Command[] */
public $commandDescs = array();
public $commandDescs = [];
/**
* @return ScriptInfo

View File

@ -27,7 +27,7 @@ class ServerOptions extends AbstractStructure
* @internal
* @return bool
*/
function isValid()
public function isValid(): bool
{
return is_string($this->name)
&& is_string($this->comment)
@ -41,15 +41,16 @@ class ServerOptions extends AbstractStructure
* @internal
* @return mixed[]
*/
function toSetterArray()
public function toSetterArray()
{
$out = array();
foreach(get_object_vars($this) as $key => $value)
{
if(substr($key, 0, 7) == 'current' || $value === null)
$out = [];
foreach (get_object_vars($this) as $key => $value) {
if (str_starts_with($key, 'current') || $value === null) {
continue;
if($key == 'nextUseChangingValidationSeed')
}
if ($key === 'nextUseChangingValidationSeed') {
$key = 'useChangingValidationSeed';
}
$out[ucfirst($key)] = $value;
}
return $out;

View File

@ -9,13 +9,13 @@ namespace Maniaplanet\DedicatedServer\Structures;
class Status extends AbstractStructure
{
const UNKNOWN = 0;
const WAITING = 1;
const LAUNCHING = 2;
const UNKNOWN = 0;
const WAITING = 1;
const LAUNCHING = 2;
const SYNCHRONIZATION = 3;
const PLAY = 4;
const EXITING = 6;
const LOCAL = 7;
const PLAY = 4;
const EXITING = 6;
const LOCAL = 7;
/** @var int */
public $code;

View File

@ -27,7 +27,7 @@ class Vote extends AbstractStructure
* @param string $cmdName
* @param mixed[] $cmdParam
*/
function __construct($cmdName='', $cmdParam=array())
function __construct($cmdName = '', $cmdParam = [])
{
$this->cmdName = $cmdName;
$this->cmdParam = $cmdParam;

View File

@ -9,15 +9,15 @@ namespace Maniaplanet\DedicatedServer\Structures;
class VoteRatio extends AbstractStructure
{
const COMMAND_DEFAULT = '*';
const COMMAND_DEFAULT = '*';
const COMMAND_SCRIPT_SETTINGS = 'SetModeScriptSettingsAndCommands';
const COMMAND_NEXT_MAP = 'NextMap';
const COMMAND_JUMP_MAP = 'JumpToMapIdent';
const COMMAND_SET_NEXT_MAP = 'SetNextMapIdent';
const COMMAND_RESTART_MAP = 'RestartMap';
const COMMAND_TEAM_BALANCE = 'AutoTeamBalance';
const COMMAND_KICK = 'Kick';
const COMMAND_BAN = 'Ban';
const COMMAND_NEXT_MAP = 'NextMap';
const COMMAND_JUMP_MAP = 'JumpToMapIdent';
const COMMAND_SET_NEXT_MAP = 'SetNextMapIdent';
const COMMAND_RESTART_MAP = 'RestartMap';
const COMMAND_TEAM_BALANCE = 'AutoTeamBalance';
const COMMAND_KICK = 'Kick';
const COMMAND_BAN = 'Ban';
/** @var string '*' for default */
public $command;

View File

@ -7,4 +7,6 @@
namespace Maniaplanet\DedicatedServer\Xmlrpc;
class Exception extends \Exception {}
class Exception extends \Exception
{
}

View File

@ -7,8 +7,10 @@
namespace Maniaplanet\DedicatedServer\Xmlrpc;
class FaultException extends Exception {
static function create($faultString, $faultCode) {
class FaultException extends Exception
{
static function create($faultString, $faultCode)
{
switch ($faultString) {
case 'Password incorrect.':
case 'Permission denied.':
@ -31,6 +33,7 @@ class FaultException extends Exception {
case 'Already waiting for a vote.':
case 'You must stop server first.':
return new LockedFeatureException($faultString, $faultCode);
case 'Can\'t kick server.':
case 'Login or Uid unknown.':
case 'Login unknown.':
case 'Payer login unknown.':
@ -110,44 +113,58 @@ class FaultException extends Exception {
}
}
class AuthenticationException extends FaultException {
class AuthenticationException extends FaultException
{
}
class UnavailableFeatureException extends FaultException {
class UnavailableFeatureException extends FaultException
{
}
class LockedFeatureException extends FaultException {
class LockedFeatureException extends FaultException
{
}
class UnknownPlayerException extends FaultException {
class UnknownPlayerException extends FaultException
{
}
class PlayerStateException extends FaultException {
class PlayerStateException extends FaultException
{
}
class AlreadyInListException extends FaultException {
class AlreadyInListException extends FaultException
{
}
class NotInListException extends FaultException {
class NotInListException extends FaultException
{
}
class IndexOutOfBoundException extends FaultException {
class IndexOutOfBoundException extends FaultException
{
}
class NextMapException extends FaultException {
class NextMapException extends FaultException
{
}
class ChangeInProgressException extends FaultException {
class ChangeInProgressException extends FaultException
{
}
class InvalidMapException extends FaultException {
class InvalidMapException extends FaultException
{
}
class GameModeException extends FaultException {
class GameModeException extends FaultException
{
}
class ServerOptionsException extends FaultException {
class ServerOptionsException extends FaultException
{
}
class FileException extends FaultException {
class FileException extends FaultException
{
}

View File

@ -9,18 +9,18 @@ namespace Maniaplanet\DedicatedServer\Xmlrpc;
class GbxRemote
{
const MAX_REQUEST_SIZE = 0x200000; // 2MB
const MAX_REQUEST_SIZE = 0x400000; // 4MB
const MAX_RESPONSE_SIZE = 0x400000; // 4MB
public static $received;
public static $sent;
private $socket;
private $readTimeout = array('sec' => 30, 'usec' => 0);
private $writeTimeout = array('sec' => 30, 'usec' => 0);
private $readTimeout = ['sec' => 5, 'usec' => 0];
private $writeTimeout = ['sec' => 5, 'usec' => 0];
private $requestHandle;
private $callbacksBuffer = array();
private $multicallBuffer = array();
private $callbacksBuffer = [];
private $multicallBuffer = [];
private $lastNetworkActivity = 0;
/**
@ -30,30 +30,104 @@ class GbxRemote
*/
function __construct($host, $port, $timeout = 5)
{
$this->requestHandle = (int) 0x80000000;
$this->requestHandle = (int)0x80000000;
$this->connect($host, $port, $timeout);
}
/**
* @param string $host
* @param int $port
* @param int $timeout
* @throws TransportException
*/
private function connect($host, $port, $timeout)
{
$this->socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$this->socket) {
throw new TransportException('Cannot open socket', TransportException::NOT_INITIALIZED);
}
stream_set_read_buffer($this->socket, 0);
stream_set_write_buffer($this->socket, 0);
// handshake
$header = $this->read(15);
if ($header === false) {
if (!is_resource($this->socket)) {
$this->onIoFailure('socket closed during handshake');
}
$this->onIoFailure(sprintf('during handshake (%s)', socket_strerror(socket_last_error())));
}
extract(unpack('Vsize/a*protocol', $header));
/** @var $size int */
/** @var $protocol string */
if ($size != 11 || $protocol != 'GBXRemote 2') {
throw new TransportException('Wrong protocol header', TransportException::WRONG_PROTOCOL);
}
$this->lastNetworkActivity = time();
}
/**
* @param int $size
* @return boolean|string
*/
private function read($size)
{
@stream_set_timeout($this->socket, $this->readTimeout['sec'], $this->readTimeout['usec']);
$data = '';
while (strlen($data) < $size) {
$buf = @fread($this->socket, $size - strlen($data));
if ($buf === '' || $buf === false) {
return false;
}
$data .= $buf;
}
self::$received += $size;
return $data;
}
/**
* @param string $when
* @throws TransportException
*/
private function onIoFailure($when)
{
$meta = stream_get_meta_data($this->socket);
if ($meta['timed_out']) {
throw new TransportException('Connection timed out ' . $when, TransportException::TIMED_OUT);
}
throw new TransportException('Connection interrupted ' . $when, TransportException::INTERRUPTED);
}
function __destruct()
{
$this->terminate();
}
public function terminate()
{
if ($this->socket) {
fclose($this->socket);
$this->socket = null;
}
}
/**
* Change timeouts
* @param int $read read timeout (in ms), 0 to leave unchanged
* @param int $write write timeout (in ms), 0 to leave unchanged
*/
function setTimeouts($read=0, $write=0)
public function setTimeouts($read = 0, $write = 0)
{
if($read)
{
$this->readTimeout['sec'] = (int) ($read / 1000);
if ($read) {
$this->readTimeout['sec'] = (int)($read / 1000);
$this->readTimeout['usec'] = ($read % 1000) * 1000;
}
if($write)
{
$this->writeTimeout['sec'] = (int) ($write / 1000);
if ($write) {
$this->writeTimeout['sec'] = (int)($write / 1000);
$this->writeTimeout['usec'] = ($write % 1000) * 1000;
}
}
@ -68,39 +142,49 @@ class GbxRemote
}
/**
* @param string $host
* @param int $port
* @param int $timeout
* @throws TransportException
*/
private function connect($host, $port, $timeout)
private function assertConnected()
{
$this->socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
if(!$this->socket)
throw new TransportException('Cannot open socket', TransportException::NOT_INITIALIZED);
stream_set_read_buffer($this->socket, 0);
stream_set_write_buffer($this->socket, 0);
// handshake
$header = $this->read(15);
if($header === false)
$this->onIoFailure(sprintf('during handshake (%s)', socket_strerror(socket_last_error($this->socket))));
extract(unpack('Vsize/a*protocol', $header));
/** @var $size int */
/** @var $protocol string */
if($size != 11 || $protocol != 'GBXRemote 2')
throw new TransportException('Wrong protocol header', TransportException::WRONG_PROTOCOL);
$this->lastNetworkActivity = time();
if (!$this->socket) {
throw new TransportException('Connection not initialized', TransportException::NOT_INITIALIZED);
}
}
function terminate()
/**
* @param string $method
* @param mixed[] $args
*/
function addCall($method, $args)
{
if($this->socket)
{
fclose($this->socket);
$this->socket = null;
$this->multicallBuffer[] = [
'methodName' => $method,
'params' => $args
];
}
/**
* @return mixed
*/
function multiquery()
{
switch (count($this->multicallBuffer)) {
case 0:
return [];
case 1:
$call = array_shift($this->multicallBuffer);
return [$this->query($call['methodName'], $call['params'])];
default:
$result = $this->query('system.multicall', [$this->multicallBuffer]);
foreach ($result as &$value) {
if (isset($value['faultCode'])) {
$value = FaultException::create($value['faultString'], $value['faultCode']);
} else {
$value = $value[0];
}
}
$this->multicallBuffer = [];
return $result;
}
}
@ -110,19 +194,19 @@ class GbxRemote
* @return mixed
* @throws MessageException
*/
function query($method, $args=array())
function query($method, $args = [])
{
$this->assertConnected();
$xml = Request::encode($method, $args);
if(strlen($xml) > self::MAX_REQUEST_SIZE-8)
{
if($method != 'system.multicall' || count($args[0]) < 2)
if (strlen($xml) > self::MAX_REQUEST_SIZE - 8) {
if ($method != 'system.multicall' || count($args[0]) < 2) {
throw new MessageException('Request too large', MessageException::REQUEST_TOO_LARGE);
}
$mid = count($args[0]) >> 1;
$res1 = $this->query('system.multicall', array(array_slice($args[0], 0, $mid)));
$res2 = $this->query('system.multicall', array(array_slice($args[0], $mid)));
$res1 = $this->query('system.multicall', [array_slice($args[0], 0, $mid)]);
$res2 = $this->query('system.multicall', [array_slice($args[0], $mid)]);
return array_merge($res1, $res2);
}
@ -131,60 +215,40 @@ class GbxRemote
}
/**
* @param string $method
* @param mixed[] $args
*/
function addCall($method, $args)
{
$this->multicallBuffer[] = array(
'methodName' => $method,
'params' => $args
);
}
/**
* @return mixed
*/
function multiquery()
{
switch(count($this->multicallBuffer))
{
case 0:
return array();
case 1:
$call = array_shift($this->multicallBuffer);
return array($this->query($call['methodName'], $call['params']));
default:
$result = $this->query('system.multicall', array($this->multicallBuffer));
foreach($result as &$value)
if(isset($value['faultCode']))
$value = FaultException::create($value['faultString'], $value['faultCode']);
else
$value = $value[0];
$this->multicallBuffer = array();
return $result;
}
}
/**
* @return mixed[]
*/
function getCallbacks()
{
$this->assertConnected();
$this->flush();
$cb = $this->callbacksBuffer;
$this->callbacksBuffer = array();
return $cb;
}
/**
* @param string $xml
* @throws TransportException
*/
private function assertConnected()
private function writeMessage($xml)
{
if(!$this->socket)
throw new TransportException('Connection not initialized', TransportException::NOT_INITIALIZED);
if ($this->requestHandle == (int)0xffffffff) {
$this->requestHandle = (int)0x80000000;
}
$data = pack('V2', strlen($xml), ++$this->requestHandle) . $xml;
if (!$this->write($data)) {
$this->onIoFailure('while writing');
}
$this->lastNetworkActivity = time();
}
/**
* @param string $data
* @return boolean
*/
private function write($data)
{
@stream_set_timeout($this->socket, $this->writeTimeout['sec'], $this->writeTimeout['usec']);
self::$sent += strlen($data);
while (strlen($data) > 0) {
$written = @fwrite($this->socket, $data);
if ($written === 0 || $written === false) {
return false;
}
$data = substr($data, $written);
}
return true;
}
/**
@ -192,20 +256,19 @@ class GbxRemote
* @return mixed
* @throws FaultException
*/
private function flush($waitResponse=false)
private function flush($waitResponse = false)
{
$r = array($this->socket);
while($waitResponse || @stream_select($r, $w, $e, 0) > 0)
{
$r = [$this->socket];
while ($waitResponse || @stream_select($r, $w, $e, 0) > 0) {
list($handle, $xml) = $this->readMessage();
list($type, $value) = Request::decode($xml);
switch($type)
{
switch ($type) {
case 'fault':
throw FaultException::create($value['faultString'], $value['faultCode']);
case 'response':
if($handle == $this->requestHandle)
if ($handle == $this->requestHandle) {
return $value;
}
break;
case 'call':
$this->callbacksBuffer[] = $value;
@ -221,106 +284,54 @@ class GbxRemote
private function readMessage()
{
$header = $this->read(8);
if($header === false)
if ($header === false) {
$this->onIoFailure('while reading header');
}
extract(unpack('Vsize/Vhandle', $header));
/** @var $size int */
/** @var $handle int */
if($size == 0 || $handle == 0)
if ($size == 0 || $handle == 0) {
throw new TransportException('Incorrect header', TransportException::PROTOCOL_ERROR);
}
if($size > self::MAX_RESPONSE_SIZE)
if ($size > self::MAX_RESPONSE_SIZE) {
throw new MessageException('Response too large', MessageException::RESPONSE_TOO_LARGE);
}
$data = $this->read($size);
if($data === false)
if ($data === false) {
$this->onIoFailure('while reading data');
$this->lastNetworkActivity = time();
return array($handle, $data);
}
/**
* @param string $xml
* @throws TransportException
*/
private function writeMessage($xml)
{
if($this->requestHandle == (int) 0xffffffff)
$this->requestHandle = (int) 0x80000000;
$data = pack('V2', strlen($xml), ++$this->requestHandle).$xml;
if(!$this->write($data))
$this->onIoFailure('while writing');
$this->lastNetworkActivity = time();
}
/**
* @param int $size
* @return boolean|string
*/
private function read($size)
{
@stream_set_timeout($this->socket, $this->readTimeout['sec'], $this->readTimeout['usec']);
$data = '';
while(strlen($data) < $size)
{
$buf = @fread($this->socket, $size - strlen($data));
if($buf === '' || $buf === false)
return false;
$data .= $buf;
}
self::$received += $size;
return $data;
$this->lastNetworkActivity = time();
return [$handle, $data];
}
/**
* @param string $data
* @return boolean
* @return mixed[]
*/
private function write($data)
function getCallbacks()
{
@stream_set_timeout($this->socket, $this->writeTimeout['sec'], $this->writeTimeout['usec']);
self::$sent += strlen($data);
while(strlen($data) > 0)
{
$written = @fwrite($this->socket, $data);
if($written === 0 || $written === false)
return false;
$data = substr($data, $written);
}
return true;
}
/**
* @param string $when
* @throws TransportException
*/
private function onIoFailure($when)
{
$meta = stream_get_meta_data($this->socket);
if($meta['timed_out'])
throw new TransportException('Connection timed out '.$when, TransportException::TIMED_OUT);
throw new TransportException('Connection interrupted '.$when, TransportException::INTERRUPTED);
$this->assertConnected();
$this->flush();
$cb = $this->callbacksBuffer;
$this->callbacksBuffer = [];
return $cb;
}
}
class TransportException extends Exception
{
const NOT_INITIALIZED = 1;
const INTERRUPTED = 2;
const TIMED_OUT = 3;
const WRONG_PROTOCOL = 4;
const PROTOCOL_ERROR = 5;
const INTERRUPTED = 2;
const TIMED_OUT = 3;
const WRONG_PROTOCOL = 4;
const PROTOCOL_ERROR = 5;
}
class MessageException extends Exception
{
const REQUEST_TOO_LARGE = 1;
const REQUEST_TOO_LARGE = 1;
const RESPONSE_TOO_LARGE = 2;
}

View File

@ -7,26 +7,26 @@
namespace Maniaplanet\DedicatedServer\Xmlrpc;
if(extension_loaded('xmlrpc'))
{
if (extension_loaded('xmlrpc')) {
abstract class Request
{
private static $options = array(
private static $options = [
'encoding' => 'utf-8',
'escaping' => 'markup',
'verbosity' => 'no_white_space'
);
];
/**
* @param string $method
* @param mixed[] $args
* @return string
*/
static function encode($method, $args, $escape=true)
static function encode($method, $args, $escape = true)
{
$opts = self::$options;
if(!$escape)
$opts['escaping'] = array();
if (!$escape) {
$opts['escaping'] = [];
}
return xmlrpc_encode_request($method, $args, $opts);
}
@ -38,21 +38,20 @@ if(extension_loaded('xmlrpc'))
static function decode($message)
{
$value = xmlrpc_decode_request($message, $method, 'utf-8');
if($value === null)
if ($value === null) {
throw new ParseException();
if($method === null)
{
if(is_array($value) && xmlrpc_is_fault($value))
return array('fault', $value);
return array('response', $value);
}
return array('call', array($method, $value));
if ($method === null) {
if (is_array($value) && xmlrpc_is_fault($value)) {
return ['fault', $value];
}
return ['response', $value];
}
return ['call', [$method, $value]];
}
}
}
else
{
} else {
abstract class Request
{
const DATE_FORMAT = 'Ymd\TH:i:s';
@ -62,64 +61,70 @@ else
* @param mixed[] $args
* @return string
*/
static function encode($method, $args, $escape=true)
static function encode($method, $args, $escape = true)
{
$xml = '<?xml version="1.0" encoding="utf-8"?><methodCall><methodName>'.self::escape($method, $escape).'</methodName>';
if(!$args)
return $xml.'<params/></methodCall>';
$xml = '<?xml version="1.0" encoding="utf-8"?><methodCall><methodName>' . self::escape($method, $escape) . '</methodName>';
if (!$args) {
return $xml . '<params/></methodCall>';
}
$xml .= '<params>';
foreach($args as $arg)
$xml .= '<param><value>'.self::encodeValue($arg, $escape).'</value></param>';
return $xml.'</params></methodCall>';
foreach ($args as $arg) {
$xml .= '<param><value>' . self::encodeValue($arg, $escape) . '</value></param>';
}
return $xml . '</params></methodCall>';
}
/**
* @param mixed $v
* @return string
*/
private static function encodeValue($v, $escape=true)
private static function encodeValue($v, $escape = true)
{
switch(gettype($v))
{
switch (gettype($v)) {
case 'boolean':
return '<boolean>'.((int) $v).'</boolean>';
return '<boolean>' . ((int)$v) . '</boolean>';
case 'integer':
return '<int>'.$v.'</int>';
return '<int>' . $v . '</int>';
case 'double':
return '<double>'.$v.'</double>';
return '<double>' . $v . '</double>';
case 'string':
case 'NULL':
if(!$v)
if (strlen($v) === 0) {
return '<string/>';
return '<string>'.self::escape($v, $escape).'</string>';
case 'object':
if($v instanceof Base64)
{
if(!$v->scalar)
return '<base64/>';
return '<base64>'.base64_encode($v->scalar).'</base64>';
}
if($v instanceof \DateTime)
return '<dateTime.iso8601>'.$v->format(self::DATE_FORMAT).'</dateTime.iso8601>';
return '<string>' . self::escape($v, $escape) . '</string>';
case 'NULL':
return '<string/>';
case 'object':
if ($v instanceof Base64) {
if (!$v->scalar) {
return '<base64/>';
}
return '<base64>' . base64_encode($v->scalar) . '</base64>';
}
if ($v instanceof \DateTime) {
return '<dateTime.iso8601>' . $v->format(self::DATE_FORMAT) . '</dateTime.iso8601>';
}
$v = get_object_vars($v);
// fallthrough
// fallthrough
case 'array':
// empty array case
if(!$v)
if (!$v) {
return '<array><data/></array>';
}
$return = '';
// pure array case
if(array_keys($v) === range(0, count($v) - 1))
{
foreach($v as $item)
$return .= '<value>'.self::encodeValue($item, $escape).'</value>';
return '<array><data>'.$return.'</data></array>';
if (array_keys($v) === range(0, count($v) - 1)) {
foreach ($v as $item) {
$return .= '<value>' . self::encodeValue($item, $escape) . '</value>';
}
return '<array><data>' . $return . '</data></array>';
}
// else it's a struct
foreach($v as $name => $value)
$return .= '<member><name>'.self::escape($name, $escape).'</name><value>'.self::encodeValue($value, $escape).'</value></member>';
return '<struct>'.$return.'</struct>';
foreach ($v as $name => $value) {
$return .= '<member><name>' . self::escape($name, $escape) . '</name><value>' . self::encodeValue($value, $escape) . '</value></member>';
}
return '<struct>' . $return . '</struct>';
}
return '';
}
@ -129,10 +134,11 @@ else
* @param bool $escape
* @return string
*/
private static function escape($str, $escape=true)
private static function escape($str, $escape = true)
{
if($escape)
return '<![CDATA['.str_replace(']]>', ']]]]><![CDATA[>', $str).']]>';
if ($escape) {
return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $str) . ']]>';
}
return $str;
}
@ -144,19 +150,21 @@ else
static function decode($message)
{
$xml = @simplexml_load_string($message);
if(!$xml)
if (!$xml) {
throw new ParseException();
if($xml->getName() == 'methodResponse')
{
if($xml->fault)
return array('fault', self::decodeValue($xml->fault->value));
return array('response', self::decodeValue($xml->params->param->value));
}
$params = array();
foreach($xml->params->param as $param)
if ($xml->getName() == 'methodResponse') {
if ($xml->fault) {
return ['fault', self::decodeValue($xml->fault->value)];
}
return ['response', self::decodeValue($xml->params->param->value)];
}
$params = [];
foreach ($xml->params->param as $param) {
$params[] = self::decodeValue($param->value);
return array('call', array((string) $xml->methodName, $params));
}
return ['call', [(string)$xml->methodName, $params]];
}
/**
@ -167,34 +175,37 @@ else
{
$elt = $elt->children();
$elt = $elt[0];
switch($elt->getName())
{
switch ($elt->getName()) {
case 'boolean':
return (bool) (int) $elt;
return (bool)(int)$elt;
case 'i4':
case 'int':
return (int) $elt;
return (int)$elt;
case 'double':
return (double) $elt;
return (double)$elt;
case 'string':
return (string) $elt;
return (string)$elt;
case 'base64':
return new Base64(base64_decode($elt));
case 'dateTime.iso8601':
return \DateTime::createFromFormat(self::DATE_FORMAT, (string) $elt);
return \DateTime::createFromFormat(self::DATE_FORMAT, (string)$elt);
case 'array':
$arr = array();
foreach($elt->data->value as $v)
$arr = [];
foreach ($elt->data->value as $v) {
$arr[] = self::decodeValue($v);
}
return $arr;
case 'struct':
$struct = array();
foreach($elt as $member)
$struct[(string) $member->name] = self::decodeValue($member->value);
$struct = [];
foreach ($elt as $member) {
$struct[(string)$member->name] = self::decodeValue($member->value);
}
return $struct;
}
}
}
}
class ParseException extends Exception {}
class ParseException extends Exception
{
}