MDL-16043: manage case when administrator set enrol_remotecoursefield or enrol_remote...
[moodle-linuxchix.git] / mnet / xmlrpc / xmlparser.php
blob1d74381d25546a5d74ab1e343395a65770193a46
1 <?php
2 /**
3 * Custom XML parser for signed and/or encrypted XML Docs
5 * @author Donal McMullan donal@catalyst.net.nz
6 * @version 0.0.1
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package mnet
9 */
11 /**
12 * Custom XML parser class for signed and/or encrypted XML Docs
14 class mnet_encxml_parser {
15 /**
16 * Constructor creates and initialises parser resource and calls initialise
18 * @return bool True
20 function mnet_encxml_parser() {
21 return $this->initialise();
24 /**
25 * Set default element handlers and initialise properties to empty.
27 * @return bool True
29 function initialise() {
30 $this->parser = xml_parser_create();
31 xml_set_object($this->parser, $this);
33 xml_set_element_handler($this->parser, "start_element", "end_element");
34 xml_set_character_data_handler($this->parser, "discard_data");
36 $this->tag_number = 0; // Just a unique ID for each tag
37 $this->digest = '';
38 $this->remote_timestamp = '';
39 $this->remote_wwwroot = '';
40 $this->signature = '';
41 $this->data_object = '';
42 $this->key_URI = '';
43 $this->payload_encrypted = false;
44 $this->cipher = array();
45 $this->error = array();
46 return true;
49 /**
50 * Parse a block of XML text
52 * The XML Text will be an XML-RPC request which is wrapped in an XML doc
53 * with a signature from the sender. This envelope may be encrypted and
54 * delivered within another XML envelope with a symmetric key. The parser
55 * should first decrypt this XML, and then place the XML-RPC request into
56 * the data_object property, and the signature into the signature property.
58 * See the W3C's {@link http://www.w3.org/TR/xmlenc-core/ XML Encryption Syntax and Processing}
59 * and {@link http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/ XML-Signature Syntax and Processing}
60 * guidelines for more detail on the XML.
62 * -----XML-Envelope---------------------------------
63 * | |
64 * | Symmetric-key-------------------------- |
65 * | |_____________________________________| |
66 * | |
67 * | Encrypted data------------------------- |
68 * | | | |
69 * | | -XML-Envelope------------------ | |
70 * | | | | | |
71 * | | | --Signature------------- | | |
72 * | | | |______________________| | | |
73 * | | | | | |
74 * | | | --Signed-Payload-------- | | |
75 * | | | | | | | |
76 * | | | | XML-RPC Request | | | |
77 * | | | |______________________| | | |
78 * | | | | | |
79 * | | |_____________________________| | |
80 * | |_____________________________________| |
81 * | |
82 * |________________________________________________|
84 * @uses $MNET
85 * @param string $data The XML that you want to parse
86 * @return bool True on success - false on failure
88 function parse($data) {
89 global $MNET, $MNET_REMOTE_CLIENT;
91 $p = xml_parse($this->parser, $data);
93 if ($p == 0) {
94 // Parse failed
95 $errcode = xml_get_error_code($this->parser);
96 $errstring = xml_error_string($errcode);
97 $lineno = xml_get_current_line_number($this->parser);
98 if ($lineno !== false) {
99 $error = array('lineno' => $lineno);
100 $lineno--; // Line numbering starts at 1.
101 while ($lineno > 0) {
102 $data = strstr($data, "\n");
103 $lineno--;
105 $data .= "\n"; // In case there's only one line (no newline)
106 $line = substr($data, 0, strpos($data, "\n"));
107 $error['code'] = $errcode;
108 $error['string'] = $errstring;
109 $error['line'] = $line;
110 $this->error[] = $error;
111 } else {
112 $this->error[] = array('code' => $errcode, 'string' => $errstring);
116 if (count($this->cipher) > 0) {
117 $this->cipher = array_values($this->cipher);
118 $this->payload_encrypted = true;
121 return (bool)$p;
125 * Destroy the parser and free up any related resource.
127 function free_resource() {
128 $free = xml_parser_free($this->parser);
132 * Set the character-data handler to the right function for each element
134 * For each tag (element) name, this function switches the character-data
135 * handler to the function that handles that element. Note that character
136 * data is referred to the handler in blocks of 1024 bytes.
138 * @param mixed $parser The XML parser
139 * @param string $name The name of the tag, e.g. method_call
140 * @param array $attrs The tag's attributes (if any exist).
141 * @return bool True
143 function start_element($parser, $name, $attrs) {
144 $this->tag_number++;
145 $handler = 'discard_data';
146 switch(strtoupper($name)) {
147 case 'DIGESTVALUE':
148 $handler = 'parse_digest';
149 break;
150 case 'SIGNATUREVALUE':
151 $handler = 'parse_signature';
152 break;
153 case 'OBJECT':
154 $handler = 'parse_object';
155 break;
156 case 'RETRIEVALMETHOD':
157 $this->key_URI = $attrs['URI'];
158 break;
159 case 'TIMESTAMP':
160 $handler = 'parse_timestamp';
161 break;
162 case 'WWWROOT':
163 $handler = 'parse_wwwroot';
164 break;
165 case 'CIPHERVALUE':
166 $this->cipher[$this->tag_number] = '';
167 $handler = 'parse_cipher';
168 break;
169 default:
170 break;
172 xml_set_character_data_handler($this->parser, $handler);
173 return true;
177 * Add the next chunk of character data to the remote_timestamp string
179 * @param mixed $parser The XML parser
180 * @param string $data The content of the current tag (1024 byte chunk)
181 * @return bool True
183 function parse_timestamp($parser, $data) {
184 $this->remote_timestamp .= $data;
185 return true;
189 * Add the next chunk of character data to the cipher string for that tag
191 * The XML parser calls the character-data handler with 1024-character
192 * chunks of data. This means that the handler may be called several times
193 * for a single tag, so we use the concatenate operator (.) to build the
194 * tag content into a string.
195 * We should not encounter more than one of each tag type, except for the
196 * cipher tag. We will often see two of those. We prevent the content of
197 * these two tags being concatenated together by counting each tag, and
198 * using its 'number' as the key to an array of ciphers.
200 * @param mixed $parser The XML parser
201 * @param string $data The content of the current tag (1024 byte chunk)
202 * @return bool True
204 function parse_cipher($parser, $data) {
205 $this->cipher[$this->tag_number] .= $data;
206 return true;
210 * Add the next chunk of character data to the remote_wwwroot string
212 * @param mixed $parser The XML parser
213 * @param string $data The content of the current tag (1024 byte chunk)
214 * @return bool True
216 function parse_wwwroot($parser, $data) {
217 $this->remote_wwwroot .= $data;
218 return true;
222 * Add the next chunk of character data to the digest string
224 * @param mixed $parser The XML parser
225 * @param string $data The content of the current tag (1024 byte chunk)
226 * @return bool True
228 function parse_digest($parser, $data) {
229 $this->digest .= $data;
230 return true;
234 * Add the next chunk of character data to the signature string
236 * @param mixed $parser The XML parser
237 * @param string $data The content of the current tag (1024 byte chunk)
238 * @return bool True
240 function parse_signature($parser, $data) {
241 $this->signature .= $data;
242 return true;
246 * Add the next chunk of character data to the data_object string
248 * @param mixed $parser The XML parser
249 * @param string $data The content of the current tag (1024 byte chunk)
250 * @return bool True
252 function parse_object($parser, $data) {
253 $this->data_object .= $data;
254 return true;
258 * Discard the next chunk of character data
260 * This is used for tags that we're not interested in.
262 * @param mixed $parser The XML parser
263 * @param string $data The content of the current tag (1024 byte chunk)
264 * @return bool True
266 function discard_data($parser, $data) {
267 // Not interested
268 return true;
272 * Switch the character-data handler to ignore the next chunk of data
274 * @param mixed $parser The XML parser
275 * @param string $name The name of the tag, e.g. method_call
276 * @return bool True
278 function end_element($parser, $name) {
279 $ok = xml_set_character_data_handler($this->parser, "discard_data");
280 return true;