IXR_Message

IXR_MESSAGE.

Defined (1)

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

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