3 * Custom XML parser for signed and/or encrypted XML Docs
5 * @author Donal McMullan donal@catalyst.net.nz
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * Custom XML parser class for signed and/or encrypted XML Docs
14 class mnet_encxml_parser
{
16 * Constructor creates and initialises parser resource and calls initialise
20 function mnet_encxml_parser() {
21 return $this->initialise();
25 * Set default element handlers and initialise properties to empty.
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
38 $this->remote_timestamp
= '';
39 $this->remote_wwwroot
= '';
40 $this->signature
= '';
41 $this->data_object
= '';
43 $this->payload_encrypted
= false;
44 $this->cipher
= array();
45 $this->error
= array();
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---------------------------------
64 * | Symmetric-key-------------------------- |
65 * | |_____________________________________| |
67 * | Encrypted data------------------------- |
69 * | | -XML-Envelope------------------ | |
71 * | | | --Signature------------- | | |
72 * | | | |______________________| | | |
74 * | | | --Signed-Payload-------- | | |
76 * | | | | XML-RPC Request | | | |
77 * | | | |______________________| | | |
79 * | | |_____________________________| | |
80 * | |_____________________________________| |
82 * |________________________________________________|
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);
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");
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;
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;
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).
143 function start_element($parser, $name, $attrs) {
145 $handler = 'discard_data';
146 switch(strtoupper($name)) {
148 $handler = 'parse_digest';
150 case 'SIGNATUREVALUE':
151 $handler = 'parse_signature';
154 $handler = 'parse_object';
156 case 'RETRIEVALMETHOD':
157 $this->key_URI
= $attrs['URI'];
160 $handler = 'parse_timestamp';
163 $handler = 'parse_wwwroot';
166 $this->cipher
[$this->tag_number
] = '';
167 $handler = 'parse_cipher';
172 xml_set_character_data_handler($this->parser
, $handler);
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)
183 function parse_timestamp($parser, $data) {
184 $this->remote_timestamp
.= $data;
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)
204 function parse_cipher($parser, $data) {
205 $this->cipher
[$this->tag_number
] .= $data;
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)
216 function parse_wwwroot($parser, $data) {
217 $this->remote_wwwroot
.= $data;
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)
228 function parse_digest($parser, $data) {
229 $this->digest
.= $data;
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)
240 function parse_signature($parser, $data) {
241 $this->signature
.= $data;
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)
252 function parse_object($parser, $data) {
253 $this->data_object
.= $data;
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)
266 function discard_data($parser, $data) {
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
278 function end_element($parser, $name) {
279 $ok = xml_set_character_data_handler($this->parser
, "discard_data");