13 var $messageType; // methodCall / methodResponse / fault
19 // Current variable stacks
20 var $_arraystructs = array(); // The stack used to keep track of the current array/struct
21 var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array
22 var $_currentStructName = array(); // A stack as well
26 var $_currentTagContents;
33 function __construct( $message )
35 $this->message =& $message;
41 public function IXR_Message( $message ) {
42 self::__construct( $message );
47 if ( ! function_exists( 'xml_parser_create' ) ) {
48 trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
52 // first remove the XML declaration
53 // merged from WP #10698 - this method avoids the RAM usage of preg_replace on very large messages
54 $header = preg_replace( '/<\?xml.*?\?'.'>/s', '', substr( $this->message, 0, 100 ), 1 );
55 $this->message = trim( substr_replace( $this->message, $header, 0, 100 ) );
56 if ( '' == $this->message ) {
60 // Then remove the DOCTYPE
61 $header = preg_replace( '/^<!DOCTYPE[^>]*+>/i', '', substr( $this->message, 0, 200 ), 1 );
62 $this->message = trim( substr_replace( $this->message, $header, 0, 200 ) );
63 if ( '' == $this->message ) {
67 // Check that the root tag is valid
68 $root_tag = substr( $this->message, 0, strcspn( substr( $this->message, 0, 20 ), "> \t\r\n" ) );
69 if ( '<!DOCTYPE' === strtoupper( $root_tag ) ) {
72 if ( ! in_array( $root_tag, array( '<methodCall', '<methodResponse', '<fault' ) ) ) {
76 // Bail if there are too many elements to parse
77 $element_limit = 30000;
78 if ( function_exists( 'apply_filters' ) ) {
80 * Filters the number of elements to parse in an XML-RPC response.
84 * @param int $element_limit Default elements limit.
86 $element_limit = apply_filters( 'xmlrpc_element_limit', $element_limit );
88 if ( $element_limit && 2 * $element_limit < substr_count( $this->message, '<' ) ) {
92 $this->_parser = xml_parser_create();
93 // Set XML parser to take the case of tags in to account
94 xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
95 // Set XML parser callback functions
96 xml_set_object($this->_parser, $this);
97 xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
98 xml_set_character_data_handler($this->_parser, 'cdata');
100 // 256Kb, parse in chunks to avoid the RAM usage on very large messages
101 $chunk_size = 262144;
104 * Filters the chunk size that can be used to parse an XML-RPC reponse message.
108 * @param int $chunk_size Chunk size to parse in bytes.
110 $chunk_size = apply_filters( 'xmlrpc_chunk_parsing_size', $chunk_size );
114 if (strlen($this->message) <= $chunk_size) {
117 $part = substr($this->message, 0, $chunk_size);
118 $this->message = substr($this->message, $chunk_size);
119 if (!xml_parse($this->_parser, $part, $final)) {
126 xml_parser_free($this->_parser);
128 // Grab the error messages, if any
129 if ($this->messageType == 'fault') {
130 $this->faultCode = $this->params[0]['faultCode'];
131 $this->faultString = $this->params[0]['faultString'];
136 function tag_open($parser, $tag, $attr)
138 $this->_currentTagContents = '';
139 $this->currentTag = $tag;
142 case 'methodResponse':
144 $this->messageType = $tag;
146 /* Deal with stacks of arrays and structs */
147 case 'data': // data is to all intents and puposes more interesting than array
148 $this->_arraystructstypes[] = 'array';
149 $this->_arraystructs[] = array();
152 $this->_arraystructstypes[] = 'struct';
153 $this->_arraystructs[] = array();
158 function cdata($parser, $cdata)
160 $this->_currentTagContents .= $cdata;
163 function tag_close($parser, $tag)
169 $value = (int)trim($this->_currentTagContents);
173 $value = (double)trim($this->_currentTagContents);
177 $value = (string)trim($this->_currentTagContents);
180 case 'dateTime.iso8601':
181 $value = new IXR_Date(trim($this->_currentTagContents));
185 // "If no type is indicated, the type is string."
186 if (trim($this->_currentTagContents) != '') {
187 $value = (string)$this->_currentTagContents;
192 $value = (boolean)trim($this->_currentTagContents);
196 $value = base64_decode($this->_currentTagContents);
199 /* Deal with stacks of arrays and structs */
202 $value = array_pop($this->_arraystructs);
203 array_pop($this->_arraystructstypes);
207 array_pop($this->_currentStructName);
210 $this->_currentStructName[] = trim($this->_currentTagContents);
213 $this->methodName = trim($this->_currentTagContents);
218 if (count($this->_arraystructs) > 0) {
219 // Add value to struct or array
220 if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') {
222 $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value;
225 $this->_arraystructs[count($this->_arraystructs)-1][] = $value;
228 // Just add as a parameter
229 $this->params[] = $value;
232 $this->_currentTagContents = '';