IXR_Message

IXR_MESSAGE.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-IXR.php  
  1. class IXR_Message 
  2. var $message; 
  3. var $messageType; // methodCall / methodResponse / fault 
  4. var $faultCode; 
  5. var $faultString; 
  6. var $methodName; 
  7. var $params; 
  8.  
  9. // Current variable stacks 
  10. var $_arraystructs = array(); // The stack used to keep track of the current array/struct 
  11. var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array 
  12. var $_currentStructName = array(); // A stack as well 
  13. var $_param; 
  14. var $_value; 
  15. var $_currentTag; 
  16. var $_currentTagContents; 
  17. // The XML parser 
  18. var $_parser; 
  19.  
  20. /** 
  21. * PHP5 constructor. 
  22. */ 
  23. function __construct( $message ) 
  24. $this->message =& $message; 
  25.  
  26. /** 
  27. * PHP4 constructor. 
  28. */ 
  29. public function IXR_Message( $message ) { 
  30. self::__construct( $message ); 
  31.  
  32. function parse() 
  33. // first remove the XML declaration 
  34. // merged from WP #10698 - this method avoids the RAM usage of preg_replace on very large messages 
  35. $header = preg_replace( '/<\?xml.*?\?'.'>/s', '', substr( $this->message, 0, 100 ), 1 ); 
  36. $this->message = trim( substr_replace( $this->message, $header, 0, 100 ) ); 
  37. if ( '' == $this->message ) { 
  38. return false; 
  39.  
  40. // Then remove the DOCTYPE 
  41. $header = preg_replace( '/^<!DOCTYPE[^>]*+>/i', '', substr( $this->message, 0, 200 ), 1 ); 
  42. $this->message = trim( substr_replace( $this->message, $header, 0, 200 ) ); 
  43. if ( '' == $this->message ) { 
  44. return false; 
  45.  
  46. // Check that the root tag is valid 
  47. $root_tag = substr( $this->message, 0, strcspn( substr( $this->message, 0, 20 ), "> \t\r\n" ) ); 
  48. if ( '<!DOCTYPE' === strtoupper( $root_tag ) ) { 
  49. return false; 
  50. if ( ! in_array( $root_tag, array( '<methodCall', '<methodResponse', '<fault' ) ) ) { 
  51. return false; 
  52.  
  53. // Bail if there are too many elements to parse 
  54. $element_limit = 30000; 
  55. if ( function_exists( 'apply_filters' ) ) { 
  56. /** 
  57. * Filters the number of elements to parse in an XML-RPC response. 
  58. * @since 4.0.0 
  59. * @param int $element_limit Default elements limit. 
  60. */ 
  61. $element_limit = apply_filters( 'xmlrpc_element_limit', $element_limit ); 
  62. if ( $element_limit && 2 * $element_limit < substr_count( $this->message, '<' ) ) { 
  63. return false; 
  64.  
  65. $this->_parser = xml_parser_create(); 
  66. // Set XML parser to take the case of tags in to account 
  67. xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false); 
  68. // Set XML parser callback functions 
  69. xml_set_object($this->_parser, $this); 
  70. xml_set_element_handler($this->_parser, 'tag_open', 'tag_close'); 
  71. xml_set_character_data_handler($this->_parser, 'cdata'); 
  72.  
  73. // 256Kb, parse in chunks to avoid the RAM usage on very large messages 
  74. $chunk_size = 262144; 
  75.  
  76. /** 
  77. * Filters the chunk size that can be used to parse an XML-RPC reponse message. 
  78. * @since 4.4.0 
  79. * @param int $chunk_size Chunk size to parse in bytes. 
  80. */ 
  81. $chunk_size = apply_filters( 'xmlrpc_chunk_parsing_size', $chunk_size ); 
  82.  
  83. $final = false; 
  84. do { 
  85. if (strlen($this->message) <= $chunk_size) { 
  86. $final = true; 
  87. $part = substr($this->message, 0, $chunk_size); 
  88. $this->message = substr($this->message, $chunk_size); 
  89. if (!xml_parse($this->_parser, $part, $final)) { 
  90. return false; 
  91. if ($final) { 
  92. break; 
  93. } while (true); 
  94. xml_parser_free($this->_parser); 
  95.  
  96. // Grab the error messages, if any 
  97. if ($this->messageType == 'fault') { 
  98. $this->faultCode = $this->params[0]['faultCode']; 
  99. $this->faultString = $this->params[0]['faultString']; 
  100. return true; 
  101.  
  102. function tag_open($parser, $tag, $attr) 
  103. $this->_currentTagContents = ''; 
  104. $this->currentTag = $tag; 
  105. switch($tag) { 
  106. case 'methodCall': 
  107. case 'methodResponse': 
  108. case 'fault': 
  109. $this->messageType = $tag; 
  110. break; 
  111. /** Deal with stacks of arrays and structs */ 
  112. case 'data': // data is to all intents and puposes more interesting than array 
  113. $this->_arraystructstypes[] = 'array'; 
  114. $this->_arraystructs[] = array(); 
  115. break; 
  116. case 'struct': 
  117. $this->_arraystructstypes[] = 'struct'; 
  118. $this->_arraystructs[] = array(); 
  119. break; 
  120.  
  121. function cdata($parser, $cdata) 
  122. $this->_currentTagContents .= $cdata; 
  123.  
  124. function tag_close($parser, $tag) 
  125. $valueFlag = false; 
  126. switch($tag) { 
  127. case 'int': 
  128. case 'i4': 
  129. $value = (int)trim($this->_currentTagContents); 
  130. $valueFlag = true; 
  131. break; 
  132. case 'double': 
  133. $value = (double)trim($this->_currentTagContents); 
  134. $valueFlag = true; 
  135. break; 
  136. case 'string': 
  137. $value = (string)trim($this->_currentTagContents); 
  138. $valueFlag = true; 
  139. break; 
  140. case 'dateTime.iso8601': 
  141. $value = new IXR_Date(trim($this->_currentTagContents)); 
  142. $valueFlag = true; 
  143. break; 
  144. case 'value': 
  145. // "If no type is indicated, the type is string." 
  146. if (trim($this->_currentTagContents) != '') { 
  147. $value = (string)$this->_currentTagContents; 
  148. $valueFlag = true; 
  149. break; 
  150. case 'boolean': 
  151. $value = (boolean)trim($this->_currentTagContents); 
  152. $valueFlag = true; 
  153. break; 
  154. case 'base64': 
  155. $value = base64_decode($this->_currentTagContents); 
  156. $valueFlag = true; 
  157. break; 
  158. /** Deal with stacks of arrays and structs */ 
  159. case 'data': 
  160. case 'struct': 
  161. $value = array_pop($this->_arraystructs); 
  162. array_pop($this->_arraystructstypes); 
  163. $valueFlag = true; 
  164. break; 
  165. case 'member': 
  166. array_pop($this->_currentStructName); 
  167. break; 
  168. case 'name': 
  169. $this->_currentStructName[] = trim($this->_currentTagContents); 
  170. break; 
  171. case 'methodName': 
  172. $this->methodName = trim($this->_currentTagContents); 
  173. break; 
  174.  
  175. if ($valueFlag) { 
  176. if (count($this->_arraystructs) > 0) { 
  177. // Add value to struct or array 
  178. if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') { 
  179. // Add to struct 
  180. $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value; 
  181. } else { 
  182. // Add to array 
  183. $this->_arraystructs[count($this->_arraystructs)-1][] = $value; 
  184. } else { 
  185. // Just add as a parameter 
  186. $this->params[] = $value; 
  187. $this->_currentTagContents = '';