forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlparser.php
307 lines (286 loc) · 10.8 KB
/
xmlparser.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* Custom XML parser for signed and/or encrypted XML Docs
*
* @author Donal McMullan [email protected]
* @version 0.0.1
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package mnet
*/
/**
* Custom XML parser class for signed and/or encrypted XML Docs
*/
class mnet_encxml_parser {
/**
* Constructor creates and initialises parser resource and calls initialise
*
* @return bool True
*/
function mnet_encxml_parser() {
return $this->initialise();
}
/**
* Set default element handlers and initialise properties to empty.
*
* @return bool True
*/
function initialise() {
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "start_element", "end_element");
xml_set_character_data_handler($this->parser, "discard_data");
$this->tag_number = 0; // Just a unique ID for each tag
$this->digest = '';
$this->remote_timestamp = '';
$this->remote_wwwroot = '';
$this->signature = '';
$this->data_object = '';
$this->key_URI = '';
$this->payload_encrypted = false;
$this->cipher = array();
$this->error = array();
$this->remoteerror = null;
$this->errorstarted = false;
return true;
}
/**
* Parse a block of XML text
*
* The XML Text will be an XML-RPC request which is wrapped in an XML doc
* with a signature from the sender. This envelope may be encrypted and
* delivered within another XML envelope with a symmetric key. The parser
* should first decrypt this XML, and then place the XML-RPC request into
* the data_object property, and the signature into the signature property.
*
* See the W3C's {@link http://www.w3.org/TR/xmlenc-core/ XML Encryption Syntax and Processing}
* and {@link http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/ XML-Signature Syntax and Processing}
* guidelines for more detail on the XML.
*
* -----XML-Envelope---------------------------------
* | |
* | Symmetric-key-------------------------- |
* | |_____________________________________| |
* | |
* | Encrypted data------------------------- |
* | | | |
* | | -XML-Envelope------------------ | |
* | | | | | |
* | | | --Signature------------- | | |
* | | | |______________________| | | |
* | | | | | |
* | | | --Signed-Payload-------- | | |
* | | | | | | | |
* | | | | XML-RPC Request | | | |
* | | | |______________________| | | |
* | | | | | |
* | | |_____________________________| | |
* | |_____________________________________| |
* | |
* |________________________________________________|
*
* @param string $data The XML that you want to parse
* @return bool True on success - false on failure
*/
function parse($data) {
$p = xml_parse($this->parser, $data);
if ($p == 0) {
// Parse failed
$errcode = xml_get_error_code($this->parser);
$errstring = xml_error_string($errcode);
$lineno = xml_get_current_line_number($this->parser);
if ($lineno !== false) {
$error = array('lineno' => $lineno);
$lineno--; // Line numbering starts at 1.
while ($lineno > 0) {
$data = strstr($data, "\n");
$lineno--;
}
$data .= "\n"; // In case there's only one line (no newline)
$line = substr($data, 0, strpos($data, "\n"));
$error['code'] = $errcode;
$error['string'] = $errstring;
$error['line'] = $line;
$this->error[] = $error;
} else {
$this->error[] = array('code' => $errcode, 'string' => $errstring);
}
}
if (!empty($this->remoteerror)) {
return false;
}
if (count($this->cipher) > 0) {
$this->cipher = array_values($this->cipher);
$this->payload_encrypted = true;
}
return (bool)$p;
}
/**
* Destroy the parser and free up any related resource.
*/
function free_resource() {
$free = xml_parser_free($this->parser);
}
/**
* Set the character-data handler to the right function for each element
*
* For each tag (element) name, this function switches the character-data
* handler to the function that handles that element. Note that character
* data is referred to the handler in blocks of 1024 bytes.
*
* @param mixed $parser The XML parser
* @param string $name The name of the tag, e.g. method_call
* @param array $attrs The tag's attributes (if any exist).
* @return bool True
*/
function start_element($parser, $name, $attrs) {
$this->tag_number++;
$handler = 'discard_data';
switch(strtoupper($name)) {
case 'DIGESTVALUE':
$handler = 'parse_digest';
break;
case 'SIGNATUREVALUE':
$handler = 'parse_signature';
break;
case 'OBJECT':
$handler = 'parse_object';
break;
case 'RETRIEVALMETHOD':
$this->key_URI = $attrs['URI'];
break;
case 'TIMESTAMP':
$handler = 'parse_timestamp';
break;
case 'WWWROOT':
$handler = 'parse_wwwroot';
break;
case 'CIPHERVALUE':
$this->cipher[$this->tag_number] = '';
$handler = 'parse_cipher';
break;
case 'FAULT':
$handler = 'parse_fault';
default:
break;
}
xml_set_character_data_handler($this->parser, $handler);
return true;
}
/**
* Add the next chunk of character data to the remote_timestamp string
*
* @param mixed $parser The XML parser
* @param string $data The content of the current tag (1024 byte chunk)
* @return bool True
*/
function parse_timestamp($parser, $data) {
$this->remote_timestamp .= $data;
return true;
}
/**
* Add the next chunk of character data to the cipher string for that tag
*
* The XML parser calls the character-data handler with 1024-character
* chunks of data. This means that the handler may be called several times
* for a single tag, so we use the concatenate operator (.) to build the
* tag content into a string.
* We should not encounter more than one of each tag type, except for the
* cipher tag. We will often see two of those. We prevent the content of
* these two tags being concatenated together by counting each tag, and
* using its 'number' as the key to an array of ciphers.
*
* @param mixed $parser The XML parser
* @param string $data The content of the current tag (1024 byte chunk)
* @return bool True
*/
function parse_cipher($parser, $data) {
$this->cipher[$this->tag_number] .= $data;
return true;
}
/**
* Add the next chunk of character data to the remote_wwwroot string
*
* @param mixed $parser The XML parser
* @param string $data The content of the current tag (1024 byte chunk)
* @return bool True
*/
function parse_wwwroot($parser, $data) {
$this->remote_wwwroot .= $data;
return true;
}
/**
* Add the next chunk of character data to the digest string
*
* @param mixed $parser The XML parser
* @param string $data The content of the current tag (1024 byte chunk)
* @return bool True
*/
function parse_digest($parser, $data) {
$this->digest .= $data;
return true;
}
/**
* Add the next chunk of character data to the signature string
*
* @param mixed $parser The XML parser
* @param string $data The content of the current tag (1024 byte chunk)
* @return bool True
*/
function parse_signature($parser, $data) {
$this->signature .= $data;
return true;
}
/**
* Add the next chunk of character data to the data_object string
*
* @param mixed $parser The XML parser
* @param string $data The content of the current tag (1024 byte chunk)
* @return bool True
*/
function parse_object($parser, $data) {
$this->data_object .= $data;
return true;
}
/**
* Discard the next chunk of character data
*
* This is used for tags that we're not interested in.
*
* @param mixed $parser The XML parser
* @param string $data The content of the current tag (1024 byte chunk)
* @return bool True
*/
function discard_data($parser, $data) {
if (!$this->errorstarted) {
// Not interested
return true;
}
$data = trim($data);
if (isset($this->errorstarted->faultstringstarted) && !empty($data)) {
$this->remoteerror .= ', message: ' . $data;
} else if (isset($this->errorstarted->faultcodestarted)) {
$this->remoteerror = 'code: ' . $data;
unset($this->errorstarted->faultcodestarted);
} else if ($data == 'faultCode') {
$this->errorstarted->faultcodestarted = true;
} else if ($data == 'faultString') {
$this->errorstarted->faultstringstarted = true;
}
return true;
}
function parse_fault($parser, $data) {
$this->errorstarted = new StdClass;
return true;
}
/**
* Switch the character-data handler to ignore the next chunk of data
*
* @param mixed $parser The XML parser
* @param string $name The name of the tag, e.g. method_call
* @return bool True
*/
function end_element($parser, $name) {
$ok = xml_set_character_data_handler($this->parser, "discard_data");
return true;
}
}