blob: 2167e5358a1cf18dd3e0082342a5caefc948557e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<?php
// SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri
// SPDX-FileCopyrightText: 2024 Harald Eilertsen
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace VolseNet\Webtrap;
/**
* A representation of an XML-RPC method call.
*
* This is just a representation to hold information about the attempted
* call. It does not try to perform any action on it.
*/
class XmlRpcMethod
{
/**
* Parse raw XML to an XmlRpcMethod object.
*
* We're not very strict when parsing, but trying to extract the
* most useful information. At the moment that is the method `name`,
* and an array of the `params` passed to the method.
*
* @param string $payload The raw XML representation of the method call.
*
* @return XmlRpcMethod
*/
public static function parse(string $payload): self
{
$parser = xml_parser_create();
xml_parse_into_struct($parser, $payload, $elements, $index);
xml_parser_free($parser);
$in_param = false;
$method_name = null;
foreach ($elements as $e) {
switch ($e['tag']) {
case 'METHODNAME':
$method_name = $e['value'];
break;
case 'PARAM':
if ($e['type'] === 'open') {
$in_param = true;
} else {
$in_param = false;
}
break;
case 'VALUE':
if ($in_param) {
$params[] = $e['value'];
}
break;
default:
// nothing
}
}
return new XmlRpcMethod($method_name, $params);
}
/**
* Constructs a new XmlRpcMethod object from the method name and params.
*
* @param string $name The method name
* @param array $params An array or params for the method call.
*/
public function __construct(public string $name, public array $params)
{
}
}
|