removed 'application' folder to have everything in the root directory

This commit is contained in:
Steffen Schröder
2014-09-29 18:20:09 +02:00
parent 1569fd5488
commit 9642433363
284 changed files with 4 additions and 50 deletions

View File

@ -0,0 +1,30 @@
<?php
/**
* ManiaPlanet dedicated server Xml-RPC client
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
*/
namespace Maniaplanet\DedicatedServer\Xmlrpc;
class Base64
{
public $scalar;
public $xmlrpc_type = 'base64';
/**
* @param string $data
*/
function __construct($data)
{
$this->scalar = $data;
}
/**
* @return string
*/
function __toString()
{
return $this->scalar;
}
}

View File

@ -0,0 +1,10 @@
<?php
/**
* ManiaPlanet dedicated server Xml-RPC client
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
*/
namespace Maniaplanet\DedicatedServer\Xmlrpc;
class Exception extends \Exception {}

View File

@ -0,0 +1,114 @@
<?php
/**
* ManiaPlanet dedicated server Xml-RPC client
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
*/
namespace Maniaplanet\DedicatedServer\Xmlrpc;
class FaultException extends Exception
{
static function create($faultString, $faultCode)
{
switch($faultString)
{
case 'Password incorrect.':
case 'Permission denied.':
return new AuthenticationException($faultString, $faultCode);
case 'not connected to the internet':
case 'Not connected to the masterserver.':
case 'Not a game server.':
case 'Not a server.':
case 'Couldn\'t create the fake player.':
case 'Only server can receive a callvote':
case 'No map currently loaded.':
case 'No replay to save':
case 'Internal error.':
return new UnavailableFeatureException($faultString, $faultCode);
case 'You must enable the callbacks to be able to do chat routing.':
case 'Chat routing not enabled.':
case 'Script cloud disabled.':
case 'Already waiting for a vote.':
case 'You must stop server first.':
return new LockedFeatureException($faultString, $faultCode);
case 'Login or Uid unknown.':
case 'Login unknown.':
return new UnknownPlayerException($faultString, $faultCode);
case 'The player is not a spectator':
case 'The player is not a spectator.':
case 'Not a network player.':
case 'Player is not a fake player':
return new PlayerStateException($faultString, $faultCode);
case 'Player already ignored.':
case 'Player already black listed.':
case 'Player already on guest list.':
case 'Map already added.':
return new AlreadyInListException($faultString, $faultCode);
case 'Login not banned.':
case 'Player not ignored.':
case 'Player not black listed.':
case 'Player not on guest list.':
case 'Map not in the selection.':
case 'The map isn\'t in the current selection.':
case 'Map not found.':
return new NotInListException($faultString, $faultCode);
case 'Start index out of bound.':
case 'invalid index':
return new IndexOutOfBoundException($faultString, $faultCode);
case 'the next map must be different from the current one.':
return new NextMapException($faultString, $faultCode);
case 'Change in progress.':
return new ChangeInProgressException($faultString, $faultCode);
case 'Incompatible map type.':
case 'Map not complete.':
case 'Map corrupted.':
case 'Map lightmap is not up to date.':
case 'The map doesn\'t match the server packmask.':
return new InvalidMapException($faultString, $faultCode);
case 'Ladder mode unknown.':
case 'You cannot change the max players count: AllowSpectatorRelays is activated.':
case 'You cannot change the max spectators count: AllowSpectatorRelays is activated.':
case 'There are too many players':
case 'There are too many spectators':
return new ServerOptionsException($faultString, $faultCode);
case 'New mode unknown.':
case 'You need to stop the server to change to/from script mode.':
case 'Not in script mode.':
case 'Not in Team mode.':
case 'Not in Rounds or Laps mode.':
case 'The scores must be decreasing.':
return new GameModeException($faultString, $faultCode);
case 'Unable to write the black list file.':
case 'Unable to write the guest list file.':
case 'Unable to write the playlist file.':
case 'Could not save file.':
case 'Map unknown.':
case 'The playlist file does not exist.':
case 'Invalid url or file.':
case 'Invalid url.':
return new FileException($faultString, $faultCode);
}
if(preg_match('~^Unknown setting \'.*\'\.$~iu', $faultString))
return new GameModeException($faultString, $faultCode);
if(preg_match('~^Couldn\'t load \'.*\'\.$~iu', $faultString))
return new FileException($faultString, $faultCode);
return new self($faultString, $faultCode);
}
}
class AuthenticationException extends FaultException {}
class UnavailableFeatureException extends FaultException {}
class LockedFeatureException extends FaultException {}
class UnknownPlayerException extends FaultException {}
class PlayerStateException extends FaultException {}
class AlreadyInListException extends FaultException {}
class NotInListException extends FaultException {}
class IndexOutOfBoundException extends FaultException {}
class NextMapException extends FaultException{}
class ChangeInProgressException extends FaultException {}
class InvalidMapException extends FaultException{}
class GameModeException extends FaultException {}
class ServerOptionsException extends FaultException {}
class FileException extends FaultException {}

View File

@ -0,0 +1,326 @@
<?php
/**
* ManiaPlanet dedicated server Xml-RPC client
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
*/
namespace Maniaplanet\DedicatedServer\Xmlrpc;
class GbxRemote
{
const MAX_REQUEST_SIZE = 0x200000; // 2MB
const MAX_RESPONSE_SIZE = 0x400000; // 4MB
public static $received;
public static $sent;
private $socket;
private $readTimeout = array('sec' => 5, 'usec' => 0);
private $writeTimeout = array('sec' => 5, 'usec' => 0);
private $requestHandle;
private $callbacksBuffer = array();
private $multicallBuffer = array();
private $lastNetworkActivity = 0;
/**
* @param string $host
* @param int $port
* @param int $timeout Timeout when opening connection
*/
function __construct($host, $port, $timeout = 5)
{
$this->requestHandle = (int) 0x80000000;
$this->connect($host, $port, $timeout);
}
function __destruct()
{
$this->terminate();
}
/**
* 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)
{
if($read)
{
$this->readTimeout['sec'] = (int) ($read / 1000);
$this->readTimeout['usec'] = ($read % 1000) * 1000;
}
if($write)
{
$this->writeTimeout['sec'] = (int) ($write / 1000);
$this->writeTimeout['usec'] = ($write % 1000) * 1000;
}
}
/**
* @return int Network idle time in seconds
*/
function getIdleTime()
{
$this->assertConnected();
return time() - $this->lastNetworkActivity;
}
/**
* @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)
$this->onIoFailure('during handshake');
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();
}
function terminate()
{
if($this->socket)
{
fclose($this->socket);
$this->socket = null;
}
}
/**
* @param string $method
* @param mixed[] $args
* @return mixed
* @throws MessageException
*/
function query($method, $args=array())
{
$this->assertConnected();
$xml = Request::encode($method, $args);
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)));
return array_merge($res1, $res2);
}
$this->writeMessage($xml);
return $this->flush(true);
}
/**
* @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;
}
/**
* @throws TransportException
*/
private function assertConnected()
{
if(!$this->socket)
throw new TransportException('Connection not initialized', TransportException::NOT_INITIALIZED);
}
/**
* @param bool $waitResponse
* @return mixed
* @throws FaultException
*/
private function flush($waitResponse=false)
{
$r = array($this->socket);
while($waitResponse || @stream_select($r, $w, $e, 0) > 0)
{
list($handle, $xml) = $this->readMessage();
list($type, $value) = Request::decode($xml);
switch($type)
{
case 'fault':
throw FaultException::create($value['faultString'], $value['faultCode']);
case 'response':
if($handle == $this->requestHandle)
return $value;
break;
case 'call':
$this->callbacksBuffer[] = $value;
}
}
}
/**
* @return mixed[]
* @throws TransportException
* @throws MessageException
*/
private function readMessage()
{
$header = $this->read(8);
if($header === false)
$this->onIoFailure('while reading header');
extract(unpack('Vsize/Vhandle', $header));
/** @var $size int */
/** @var $handle int */
if($size == 0 || $handle == 0)
throw new TransportException('Incorrect header', TransportException::PROTOCOL_ERROR);
if($size > self::MAX_RESPONSE_SIZE)
throw new MessageException('Response too large', MessageException::RESPONSE_TOO_LARGE);
$data = $this->read($size);
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;
}
/**
* @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;
}
/**
* @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);
}
}
class TransportException extends Exception
{
const NOT_INITIALIZED = 1;
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 RESPONSE_TOO_LARGE = 2;
}

View File

@ -0,0 +1,200 @@
<?php
/**
* ManiaPlanet dedicated server Xml-RPC client
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
*/
namespace Maniaplanet\DedicatedServer\Xmlrpc;
if(extension_loaded('xmlrpc'))
{
abstract class Request
{
private static $options = array(
'encoding' => 'utf-8',
'escaping' => 'markup',
'verbosity' => 'no_white_space'
);
/**
* @param string $method
* @param mixed[] $args
* @return string
*/
static function encode($method, $args, $escape=true)
{
$opts = self::$options;
if(!$escape)
$opts['escaping'] = array();
return xmlrpc_encode_request($method, $args, $opts);
}
/**
* @param string $message
* @return mixed
* @throws ParseException
*/
static function decode($message)
{
$value = xmlrpc_decode_request($message, $method, 'utf-8');
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));
}
}
}
else
{
abstract class Request
{
const DATE_FORMAT = 'Ymd\TH:i:s';
/**
* @param string $method
* @param mixed[] $args
* @return string
*/
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 .= '<params>';
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)
{
switch(gettype($v))
{
case 'boolean':
return '<boolean>'.((int) $v).'</boolean>';
case 'integer':
return '<int>'.$v.'</int>';
case 'double':
return '<double>'.$v.'</double>';
case 'string':
case 'NULL':
if(!$v)
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>';
$v = get_object_vars($v);
// fallthrough
case 'array':
// empty array case
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>';
}
// 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>';
}
return '';
}
/**
* @param string $str
* @param bool $escape
* @return string
*/
private static function escape($str, $escape=true)
{
if($escape)
return '<![CDATA['.str_replace(']]>', ']]]]><![CDATA[>', $str).']]>';
return $str;
}
/**
* @param string $message
* @return mixed
* @throws ParseException
*/
static function decode($message)
{
$xml = @simplexml_load_string($message);
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)
$params[] = self::decodeValue($param->value);
return array('call', array((string) $xml->methodName, $params));
}
/**
* @param \SimpleXMLElement $elt
* @return mixed
*/
private static function decodeValue($elt)
{
$elt = $elt->children();
$elt = $elt[0];
switch($elt->getName())
{
case 'boolean':
return (bool) (int) $elt;
case 'i4':
case 'int':
return (int) $elt;
case 'double':
return (double) $elt;
case 'string':
return (string) $elt;
case 'base64':
return new Base64(base64_decode($elt));
case 'dateTime.iso8601':
return \DateTime::createFromFormat(self::DATE_FORMAT, (string) $elt);
case 'array':
$arr = array();
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);
return $struct;
}
}
}
}
class ParseException extends Exception {}