forked from lsmonki/php-ixr
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMessage.php
210 lines (195 loc) · 7.03 KB
/
Message.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace IXR\Message;
use IXR\DataType\Date;
class Message
{
public $message;
public $messageType; // methodCall / methodResponse / fault
public $faultCode;
public $faultString;
public $methodName;
public $params;
// Current variable stacks
private $_arraystructs = []; // The stack used to keep track of the current array/struct
private $_arraystructstypes = []; // Stack keeping track of if things are structs or array
private $_currentStructName = []; // A stack as well
private $_param;
private $_value;
private $_currentTag;
private $_currentTagContents;
// The XML parser
private $_parser;
public function __construct($message)
{
$this->message =& $message;
}
public function parse()
{
// first remove the XML declaration
// merged from WP #10698 - this method avoids the RAM usage of preg_replace on very large messages
$header = preg_replace('/<\?xml.*?\?' . '>/s', '', substr($this->message, 0, 100), 1);
$this->message = trim(substr_replace($this->message, $header, 0, 100));
if ('' == $this->message) {
return false;
}
// Then remove the DOCTYPE
$header = preg_replace('/^<!DOCTYPE[^>]*+>/i', '', substr($this->message, 0, 200), 1);
$this->message = trim(substr_replace($this->message, $header, 0, 200));
if ('' == $this->message) {
return false;
}
// Check that the root tag is valid
$root_tag = substr($this->message, 0, strcspn(substr($this->message, 0, 20), "> \t\r\n"));
if ('<!DOCTYPE' === strtoupper($root_tag)) {
return false;
}
if (!in_array($root_tag, ['<methodCall', '<methodResponse', '<fault'])) {
return false;
}
// Bail if there are too many elements to parse
$element_limit = 30000;
if ($element_limit && 2 * $element_limit < substr_count($this->message, '<')) {
return false;
}
$this->_parser = xml_parser_create();
// Set XML parser to take the case of tags in to account
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
// Set XML parser callback functions
xml_set_object($this->_parser, $this);
xml_set_element_handler($this->_parser, 'tagOpen', 'tagClose');
xml_set_character_data_handler($this->_parser, 'cdata');
$chunk_size = 262144; // 256Kb, parse in chunks to avoid the RAM usage on very large messages
$final = false;
do {
if (strlen($this->message) <= $chunk_size) {
$final = true;
}
$part = substr($this->message, 0, $chunk_size);
$this->message = substr($this->message, $chunk_size);
if (!xml_parse($this->_parser, $part, $final)) {
return false;
}
if ($final) {
break;
}
} while (true);
xml_parser_free($this->_parser);
// Grab the error messages, if any
if ($this->messageType === 'fault') {
$this->faultCode = $this->params[0]['faultCode'];
$this->faultString = $this->params[0]['faultString'];
}
return true;
}
/**
* Opening tag handler
* @param $parser
* @param $tag
* @param $attr
*/
public function tagOpen($parser, $tag, $attr)
{
$this->_currentTagContents = '';
$this->currentTag = $tag;
switch ($tag) {
case 'methodCall':
case 'methodResponse':
case 'fault':
$this->messageType = $tag;
break;
/* Deal with stacks of arrays and structs */
case 'data': // data is to all intents and puposes more interesting than array
$this->_arraystructstypes[] = 'array';
$this->_arraystructs[] = [];
break;
case 'struct':
$this->_arraystructstypes[] = 'struct';
$this->_arraystructs[] = [];
break;
}
}
/**
* Character Data handler
* @param $parser
* @param $cdata
*/
public function cdata($parser, $cdata)
{
$this->_currentTagContents .= $cdata;
}
/**
* Closing tag handler
* @param $parser
* @param $tag
*/
public function tagClose($parser, $tag)
{
$valueFlag = false;
switch ($tag) {
case 'int':
case 'i4':
$value = (int)trim($this->_currentTagContents);
$valueFlag = true;
break;
case 'double':
$value = (double)trim($this->_currentTagContents);
$valueFlag = true;
break;
case 'string':
$value = (string)($this->_currentTagContents);
$valueFlag = true;
break;
case 'dateTime.iso8601':
$value = new Date(trim($this->_currentTagContents));
$valueFlag = true;
break;
case 'value':
// "If no type is indicated, the type is string."
if (trim($this->_currentTagContents) != '') {
$value = (string)$this->_currentTagContents;
$valueFlag = true;
}
break;
case 'boolean':
$value = (boolean)trim($this->_currentTagContents);
$valueFlag = true;
break;
case 'base64':
$value = base64_decode($this->_currentTagContents);
$valueFlag = true;
break;
/* Deal with stacks of arrays and structs */
case 'data':
case 'struct':
$value = array_pop($this->_arraystructs);
array_pop($this->_arraystructstypes);
$valueFlag = true;
break;
case 'member':
array_pop($this->_currentStructName);
break;
case 'name':
$this->_currentStructName[] = trim($this->_currentTagContents);
break;
case 'methodName':
$this->methodName = trim($this->_currentTagContents);
break;
}
if ($valueFlag) {
if (count($this->_arraystructs) > 0) {
// Add value to struct or array
if ($this->_arraystructstypes[count($this->_arraystructstypes) - 1] === 'struct') {
// Add to struct
$this->_arraystructs[count($this->_arraystructs) - 1][$this->_currentStructName[count($this->_currentStructName) - 1]] = $value;
} else {
// Add to array
$this->_arraystructs[count($this->_arraystructs) - 1][] = $value;
}
} else {
// Just add as a paramater
$this->params[] = $value;
}
}
$this->_currentTagContents = '';
}
}