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();
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---------------------------------
63 * | Symmetric-key-------------------------- |
64 * | |_____________________________________| |
66 * | Encrypted data------------------------- |
68 * | | -XML-Envelope------------------ | |
70 * | | | --Signature------------- | | |
71 * | | | |______________________| | | |
73 * | | | --Signed-Payload-------- | | |
75 * | | | | XML-RPC Request | | | |
76 * | | | |______________________| | | |
78 * | | |_____________________________| | |
79 * | |_____________________________________| |
81 * |________________________________________________|
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;
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).
119 function start_element($parser, $name, $attrs) {
121 $handler = 'discard_data';
122 switch(strtoupper($name)) {
124 $handler = 'parse_digest';
126 case 'SIGNATUREVALUE':
127 $handler = 'parse_signature';
130 $handler = 'parse_object';
132 case 'RETRIEVALMETHOD':
133 $this->key_URI
= $attrs['URI'];
136 $handler = 'parse_timestamp';
139 $handler = 'parse_wwwroot';
142 $this->cipher
[$this->tag_number
] = '';
143 $handler = 'parse_cipher';
148 xml_set_character_data_handler($this->parser
, $handler);
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)
159 function parse_timestamp($parser, $data) {
160 $this->remote_timestamp
.= $data;
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)
180 function parse_cipher($parser, $data) {
181 $this->cipher
[$this->tag_number
] .= $data;
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)
192 function parse_wwwroot($parser, $data) {
193 $this->remote_wwwroot
.= $data;
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)
204 function parse_digest($parser, $data) {
205 $this->digest
.= $data;
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)
216 function parse_signature($parser, $data) {
217 $this->signature
.= $data;
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)
228 function parse_object($parser, $data) {
229 $this->data_object
.= $data;
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)
242 function discard_data($parser, $data) {
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
254 function end_element($parser, $name) {
255 $ok = xml_set_character_data_handler($this->parser
, "discard_data");