TrackManiaControl/libs/Maniaplanet/DedicatedServer/Xmlrpc/Request.php

212 lines
5.0 KiB
PHP
Raw Normal View History

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')) {
abstract class Request
2014-01-16 16:56:24 +01:00
{
2024-08-25 22:30:06 +02:00
private static $options = [
'encoding' => 'utf-8',
2014-06-12 15:39:50 +02:00
'escaping' => 'markup',
'verbosity' => 'no_white_space'
2024-08-25 22:30:06 +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
}
/**
* @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) {
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];
}
2024-08-25 22:30:06 +02:00
return ['call', [$method, $value]];
}
}
2024-08-25 22:30:06 +02:00
} else {
abstract class Request
2014-01-16 16:56:24 +01: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)
{
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>';
}
/**
* @param mixed $v
* @return string
*/
2024-08-25 22:30:06 +02:00
private static function encodeValue($v, $escape = true)
{
2024-08-25 22:30:06 +02:00
switch (gettype($v)) {
case 'boolean':
2024-08-25 22:30:06 +02:00
return '<boolean>' . ((int)$v) . '</boolean>';
case 'integer':
2024-08-25 22:30:06 +02:00
return '<int>' . $v . '</int>';
case 'double':
2024-08-25 22:30:06 +02:00
return '<double>' . $v . '</double>';
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/>';
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
}
$v = get_object_vars($v);
2024-08-25 22:30:06 +02:00
// fallthrough
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 = '';
// 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>';
}
// 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>';
}
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;
}
/**
* @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) {
throw new ParseException();
2024-08-25 22:30:06 +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)];
}
2024-08-25 22:30:06 +02:00
$params = [];
foreach ($xml->params->param as $param) {
$params[] = self::decodeValue($param->value);
2024-08-25 22:30:06 +02:00
}
return ['call', [(string)$xml->methodName, $params]];
}
/**
* @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()) {
case 'boolean':
2024-08-25 22:30:06 +02:00
return (bool)(int)$elt;
case 'i4':
case 'int':
2024-08-25 22:30:06 +02:00
return (int)$elt;
case 'double':
2024-08-25 22:30:06 +02:00
return (double)$elt;
case 'string':
2024-08-25 22:30:06 +02:00
return (string)$elt;
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);
case 'array':
2024-08-25 22:30:06 +02:00
$arr = [];
foreach ($elt->data->value as $v) {
$arr[] = self::decodeValue($v);
2024-08-25 22:30:06 +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);
}
return $struct;
}
}
2014-01-16 16:56:24 +01:00
}
}
2024-08-25 22:30:06 +02:00
class ParseException extends Exception
{
}