var $messageType = false; // methodCall / methodResponse / fault
var $faultString = false;
// Current variable stacks
var $_arraystructs = array(); // The stack used to keep track of the current array/struct
var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array
var $_currentStructName = array(); // A stack as well
var $_currentTagContents;
function __construct( $message )
$this->message =& $message;
public function IXR_Message( $message ) {
self::__construct( $message );
if ( ! function_exists( 'xml_parser_create' ) ) {
trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
// first remove the XML declaration
// merged from WP #10698 - this method avoids the RAM usage of preg_replace on very large messages
$header = preg_replace( '/<\?xml.*?\?'.'>/s', '', substr( $this->message, 0, 100 ), 1 );
$this->message = trim( substr_replace( $this->message, $header, 0, 100 ) );
if ( '' == $this->message ) {
// Then remove the DOCTYPE
$header = preg_replace( '/^<!DOCTYPE[^>]*+>/i', '', substr( $this->message, 0, 200 ), 1 );
$this->message = trim( substr_replace( $this->message, $header, 0, 200 ) );
if ( '' == $this->message ) {
// Check that the root tag is valid
$root_tag = substr( $this->message, 0, strcspn( substr( $this->message, 0, 20 ), "> \t\r\n" ) );
if ( '<!DOCTYPE' === strtoupper( $root_tag ) ) {
if ( ! in_array( $root_tag, array( '<methodCall', '<methodResponse', '<fault' ) ) ) {
// Bail if there are too many elements to parse
if ( function_exists( 'apply_filters' ) ) {
* Filters the number of elements to parse in an XML-RPC response.
* @param int $element_limit Default elements limit.
$element_limit = apply_filters( 'xmlrpc_element_limit', $element_limit );
if ( $element_limit && 2 * $element_limit < substr_count( $this->message, '<' ) ) {
$this->_parser = xml_parser_create();
// Set XML parser to take the case of tags in to account
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
// Set XML parser callback functions
xml_set_element_handler($this->_parser, array($this, 'tag_open'), array($this, 'tag_close'));
xml_set_character_data_handler($this->_parser, array($this, 'cdata'));
// 256Kb, parse in chunks to avoid the RAM usage on very large messages
* Filters the chunk size that can be used to parse an XML-RPC response message.
* @param int $chunk_size Chunk size to parse in bytes.
$chunk_size = apply_filters( 'xmlrpc_chunk_parsing_size', $chunk_size );
if (strlen($this->message) <= $chunk_size) {
$part = substr($this->message, 0, $chunk_size);
$this->message = substr($this->message, $chunk_size);
if (!xml_parse($this->_parser, $part, $final)) {
xml_parser_free($this->_parser);
xml_parser_free($this->_parser);
// Grab the error messages, if any
if ($this->messageType == 'fault') {
$this->faultCode = $this->params[0]['faultCode'];
$this->faultString = $this->params[0]['faultString'];
function tag_open($parser, $tag, $attr)
$this->_currentTagContents = '';
$this->_currentTag = $tag;
$this->messageType = $tag;
/* Deal with stacks of arrays and structs */
case 'data': // data is to all intents and puposes more interesting than array
$this->_arraystructstypes[] = 'array';
$this->_arraystructs[] = array();
$this->_arraystructstypes[] = 'struct';
$this->_arraystructs[] = array();
function cdata($parser, $cdata)
$this->_currentTagContents .= $cdata;
function tag_close($parser, $tag)
$value = (int)trim($this->_currentTagContents);
$value = (double)trim($this->_currentTagContents);
$value = (string)trim($this->_currentTagContents);
$value = new IXR_Date(trim($this->_currentTagContents));
// "If no type is indicated, the type is string."
if (trim($this->_currentTagContents) != '') {
$value = (string)$this->_currentTagContents;
$value = (boolean)trim($this->_currentTagContents);
$value = base64_decode($this->_currentTagContents);
/* Deal with stacks of arrays and structs */
$value = array_pop($this->_arraystructs);
array_pop($this->_arraystructstypes);
array_pop($this->_currentStructName);
$this->_currentStructName[] = trim($this->_currentTagContents);
$this->methodName = trim($this->_currentTagContents);
if (count($this->_arraystructs) > 0) {
// Add value to struct or array
if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') {
$this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value;
$this->_arraystructs[count($this->_arraystructs)-1][] = $value;
// Just add as a parameter
$this->params[] = $value;
$this->_currentTagContents = '';