MDL-10604:
[moodle-linuxchix.git] / mnet / xmlrpc / xmlparser.php
blob066265ff0cbc7b5f621c11416ac7e3cf1f641791
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 return true;
48 /**
49 * Parse a block of XML text
51 * The XML Text will be an XML-RPC request which is wrapped in an XML doc
52 * with a signature from the sender. This envelope may be encrypted and
53 * delivered within another XML envelope with a symmetric key. The parser
54 * should first decrypt this XML, and then place the XML-RPC request into
55 * the data_object property, and the signature into the signature property.
57 * See the W3C's {@link http://www.w3.org/TR/xmlenc-core/ XML Encryption Syntax and Processing}
58 * and {@link http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/ XML-Signature Syntax and Processing}
59 * guidelines for more detail on the XML.
61 * -----XML-Envelope---------------------------------
62 * | |
63 * | Symmetric-key-------------------------- |
64 * | |_____________________________________| |
65 * | |
66 * | Encrypted data------------------------- |
67 * | | | |
68 * | | -XML-Envelope------------------ | |
69 * | | | | | |
70 * | | | --Signature------------- | | |
71 * | | | |______________________| | | |
72 * | | | | | |
73 * | | | --Signed-Payload-------- | | |
74 * | | | | | | | |
75 * | | | | XML-RPC Request | | | |
76 * | | | |______________________| | | |
77 * | | | | | |
78 * | | |_____________________________| | |
79 * | |_____________________________________| |
80 * | |
81 * |________________________________________________|
83 * @uses $MNET
84 * @param string $data The XML that you want to parse
85 * @return bool True on success - false on failure
87 function parse($data) {
88 global $MNET, $MNET_REMOTE_CLIENT;
90 $p = xml_parse($this->parser, $data);
92 if (count($this->cipher) > 0) {
93 $this->cipher = array_values($this->cipher);
94 $this->payload_encrypted = true;
97 return (bool)$p;
101 * Destroy the parser and free up any related resource.
103 function free_resource() {
104 $free = xml_parser_free($this->parser);
108 * Set the character-data handler to the right function for each element
110 * For each tag (element) name, this function switches the character-data
111 * handler to the function that handles that element. Note that character
112 * data is referred to the handler in blocks of 1024 bytes.
114 * @param mixed $parser The XML parser
115 * @param string $name The name of the tag, e.g. method_call
116 * @param array $attrs The tag's attributes (if any exist).
117 * @return bool True
119 function start_element($parser, $name, $attrs) {
120 $this->tag_number++;
121 $handler = 'discard_data';
122 switch(strtoupper($name)) {
123 case 'DIGESTVALUE':
124 $handler = 'parse_digest';
125 break;
126 case 'SIGNATUREVALUE':
127 $handler = 'parse_signature';
128 break;
129 case 'OBJECT':
130 $handler = 'parse_object';
131 break;
132 case 'RETRIEVALMETHOD':
133 $this->key_URI = $attrs['URI'];
134 break;
135 case 'TIMESTAMP':
136 $handler = 'parse_timestamp';
137 break;
138 case 'WWWROOT':
139 $handler = 'parse_wwwroot';
140 break;
141 case 'CIPHERVALUE':
142 $this->cipher[$this->tag_number] = '';
143 $handler = 'parse_cipher';
144 break;
145 default:
146 break;
148 xml_set_character_data_handler($this->parser, $handler);
149 return true;
153 * Add the next chunk of character data to the remote_timestamp string
155 * @param mixed $parser The XML parser
156 * @param string $data The content of the current tag (1024 byte chunk)
157 * @return bool True
159 function parse_timestamp($parser, $data) {
160 $this->remote_timestamp .= $data;
161 return true;
165 * Add the next chunk of character data to the cipher string for that tag
167 * The XML parser calls the character-data handler with 1024-character
168 * chunks of data. This means that the handler may be called several times
169 * for a single tag, so we use the concatenate operator (.) to build the
170 * tag content into a string.
171 * We should not encounter more than one of each tag type, except for the
172 * cipher tag. We will often see two of those. We prevent the content of
173 * these two tags being concatenated together by counting each tag, and
174 * using its 'number' as the key to an array of ciphers.
176 * @param mixed $parser The XML parser
177 * @param string $data The content of the current tag (1024 byte chunk)
178 * @return bool True
180 function parse_cipher($parser, $data) {
181 $this->cipher[$this->tag_number] .= $data;
182 return true;
186 * Add the next chunk of character data to the remote_wwwroot string
188 * @param mixed $parser The XML parser
189 * @param string $data The content of the current tag (1024 byte chunk)
190 * @return bool True
192 function parse_wwwroot($parser, $data) {
193 $this->remote_wwwroot .= $data;
194 return true;
198 * Add the next chunk of character data to the digest string
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_digest($parser, $data) {
205 $this->digest .= $data;
206 return true;
210 * Add the next chunk of character data to the signature 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_signature($parser, $data) {
217 $this->signature .= $data;
218 return true;
222 * Add the next chunk of character data to the data_object 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_object($parser, $data) {
229 $this->data_object .= $data;
230 return true;
234 * Discard the next chunk of character data
236 * This is used for tags that we're not interested in.
238 * @param mixed $parser The XML parser
239 * @param string $data The content of the current tag (1024 byte chunk)
240 * @return bool True
242 function discard_data($parser, $data) {
243 // Not interested
244 return true;
248 * Switch the character-data handler to ignore the next chunk of data
250 * @param mixed $parser The XML parser
251 * @param string $name The name of the tag, e.g. method_call
252 * @return bool True
254 function end_element($parser, $name) {
255 $ok = xml_set_character_data_handler($this->parser, "discard_data");
256 return true;