2014-01-16 16:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ManiaPlanet dedicated server Xml-RPC client
|
|
|
|
*
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Maniaplanet\DedicatedServer\Xmlrpc;
|
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
if (extension_loaded('xmlrpc')) {
|
2014-04-19 00:32:34 +02:00
|
|
|
abstract class Request
|
2014-01-16 16:56:24 +01:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
private static $options = [
|
2014-04-19 01:59:49 +02:00
|
|
|
'encoding' => 'utf-8',
|
2014-06-12 15:39:50 +02:00
|
|
|
'escaping' => 'markup',
|
2014-04-19 01:59:49 +02:00
|
|
|
'verbosity' => 'no_white_space'
|
2024-08-25 22:30:06 +02:00
|
|
|
];
|
2014-04-19 01:59:49 +02:00
|
|
|
|
2014-04-19 00:32:34 +02:00
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param mixed[] $args
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
static function encode($method, $args, $escape = true)
|
2014-01-16 16:56:24 +01:00
|
|
|
{
|
2014-05-08 19:38:15 +02:00
|
|
|
$opts = self::$options;
|
2024-08-25 22:30:06 +02:00
|
|
|
if (!$escape) {
|
|
|
|
$opts['escaping'] = [];
|
|
|
|
}
|
2014-05-08 19:38:15 +02:00
|
|
|
return xmlrpc_encode_request($method, $args, $opts);
|
2014-01-16 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:32:34 +02:00
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
* @return mixed
|
|
|
|
* @throws ParseException
|
|
|
|
*/
|
|
|
|
static function decode($message)
|
|
|
|
{
|
|
|
|
$value = xmlrpc_decode_request($message, $method, 'utf-8');
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($value === null) {
|
2014-04-19 00:32:34 +02:00
|
|
|
throw new ParseException();
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-01-16 16:56:24 +01:00
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($method === null) {
|
|
|
|
if (is_array($value) && xmlrpc_is_fault($value)) {
|
|
|
|
return ['fault', $value];
|
|
|
|
}
|
|
|
|
return ['response', $value];
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
2024-08-25 22:30:06 +02:00
|
|
|
return ['call', [$method, $value]];
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-25 22:30:06 +02:00
|
|
|
} else {
|
2014-04-19 00:32:34 +02:00
|
|
|
abstract class Request
|
2014-01-16 16:56:24 +01:00
|
|
|
{
|
2014-04-19 00:32:34 +02:00
|
|
|
const DATE_FORMAT = 'Ymd\TH:i:s';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param mixed[] $args
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
static function encode($method, $args, $escape = true)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
$xml = '<?xml version="1.0" encoding="utf-8"?><methodCall><methodName>' . self::escape($method, $escape) . '</methodName>';
|
|
|
|
if (!$args) {
|
|
|
|
return $xml . '<params/></methodCall>';
|
|
|
|
}
|
2014-05-08 19:38:15 +02:00
|
|
|
|
|
|
|
$xml .= '<params>';
|
2024-08-25 22:30:06 +02:00
|
|
|
foreach ($args as $arg) {
|
|
|
|
$xml .= '<param><value>' . self::encodeValue($arg, $escape) . '</value></param>';
|
|
|
|
}
|
|
|
|
return $xml . '</params></methodCall>';
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $v
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
private static function encodeValue($v, $escape = true)
|
2014-04-19 00:32:34 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
switch (gettype($v)) {
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'boolean':
|
2024-08-25 22:30:06 +02:00
|
|
|
return '<boolean>' . ((int)$v) . '</boolean>';
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'integer':
|
2024-08-25 22:30:06 +02:00
|
|
|
return '<int>' . $v . '</int>';
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'double':
|
2024-08-25 22:30:06 +02:00
|
|
|
return '<double>' . $v . '</double>';
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'string':
|
2024-08-25 22:30:06 +02:00
|
|
|
if (strlen($v) === 0) {
|
2014-05-08 19:38:15 +02:00
|
|
|
return '<string/>';
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
|
|
|
return '<string>' . self::escape($v, $escape) . '</string>';
|
|
|
|
case 'NULL':
|
|
|
|
return '<string/>';
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'object':
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($v instanceof Base64) {
|
|
|
|
if (!$v->scalar) {
|
2014-05-08 19:38:15 +02:00
|
|
|
return '<base64/>';
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
|
|
|
return '<base64>' . base64_encode($v->scalar) . '</base64>';
|
|
|
|
}
|
|
|
|
if ($v instanceof \DateTime) {
|
|
|
|
return '<dateTime.iso8601>' . $v->format(self::DATE_FORMAT) . '</dateTime.iso8601>';
|
2014-05-08 19:38:15 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
$v = get_object_vars($v);
|
2024-08-25 22:30:06 +02:00
|
|
|
// fallthrough
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'array':
|
2014-05-08 19:38:15 +02:00
|
|
|
// empty array case
|
2024-08-25 22:30:06 +02:00
|
|
|
if (!$v) {
|
2014-05-08 19:38:15 +02:00
|
|
|
return '<array><data/></array>';
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-06-12 15:39:50 +02:00
|
|
|
$return = '';
|
2014-04-19 00:32:34 +02:00
|
|
|
// pure array case
|
2024-08-25 22:30:06 +02:00
|
|
|
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>';
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
// else it's a struct
|
2024-08-25 22:30:06 +02:00
|
|
|
foreach ($v as $name => $value) {
|
|
|
|
$return .= '<member><name>' . self::escape($name, $escape) . '</name><value>' . self::encodeValue($value, $escape) . '</value></member>';
|
|
|
|
}
|
|
|
|
return '<struct>' . $return . '</struct>';
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:38:15 +02:00
|
|
|
/**
|
|
|
|
* @param string $str
|
|
|
|
* @param bool $escape
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-08-25 22:30:06 +02:00
|
|
|
private static function escape($str, $escape = true)
|
2014-05-08 19:38:15 +02:00
|
|
|
{
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($escape) {
|
|
|
|
return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $str) . ']]>';
|
|
|
|
}
|
2014-05-08 19:38:15 +02:00
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:32:34 +02:00
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
* @return mixed
|
|
|
|
* @throws ParseException
|
|
|
|
*/
|
|
|
|
static function decode($message)
|
|
|
|
{
|
|
|
|
$xml = @simplexml_load_string($message);
|
2024-08-25 22:30:06 +02:00
|
|
|
if (!$xml) {
|
2014-04-19 00:32:34 +02:00
|
|
|
throw new ParseException();
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
if ($xml->getName() == 'methodResponse') {
|
|
|
|
if ($xml->fault) {
|
|
|
|
return ['fault', self::decodeValue($xml->fault->value)];
|
|
|
|
}
|
|
|
|
return ['response', self::decodeValue($xml->params->param->value)];
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
2024-08-25 22:30:06 +02:00
|
|
|
$params = [];
|
|
|
|
foreach ($xml->params->param as $param) {
|
2014-04-19 00:32:34 +02:00
|
|
|
$params[] = self::decodeValue($param->value);
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
|
|
|
return ['call', [(string)$xml->methodName, $params]];
|
2014-04-19 00:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \SimpleXMLElement $elt
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private static function decodeValue($elt)
|
|
|
|
{
|
|
|
|
$elt = $elt->children();
|
|
|
|
$elt = $elt[0];
|
2024-08-25 22:30:06 +02:00
|
|
|
switch ($elt->getName()) {
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'boolean':
|
2024-08-25 22:30:06 +02:00
|
|
|
return (bool)(int)$elt;
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'i4':
|
|
|
|
case 'int':
|
2024-08-25 22:30:06 +02:00
|
|
|
return (int)$elt;
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'double':
|
2024-08-25 22:30:06 +02:00
|
|
|
return (double)$elt;
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'string':
|
2024-08-25 22:30:06 +02:00
|
|
|
return (string)$elt;
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'base64':
|
|
|
|
return new Base64(base64_decode($elt));
|
|
|
|
case 'dateTime.iso8601':
|
2024-08-25 22:30:06 +02:00
|
|
|
return \DateTime::createFromFormat(self::DATE_FORMAT, (string)$elt);
|
2014-04-19 00:32:34 +02:00
|
|
|
case 'array':
|
2024-08-25 22:30:06 +02:00
|
|
|
$arr = [];
|
|
|
|
foreach ($elt->data->value as $v) {
|
2014-04-19 00:32:34 +02:00
|
|
|
$arr[] = self::decodeValue($v);
|
2024-08-25 22:30:06 +02:00
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
return $arr;
|
|
|
|
case 'struct':
|
2024-08-25 22:30:06 +02:00
|
|
|
$struct = [];
|
|
|
|
foreach ($elt as $member) {
|
|
|
|
$struct[(string)$member->name] = self::decodeValue($member->value);
|
|
|
|
}
|
2014-04-19 00:32:34 +02:00
|
|
|
return $struct;
|
|
|
|
}
|
|
|
|
}
|
2014-01-16 16:56:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-25 22:30:06 +02:00
|
|
|
class ParseException extends Exception
|
|
|
|
{
|
|
|
|
}
|