PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/var/IXR/Message.php

https://gitlab.com/wuhang2003/typecho
PHP | 167 lines | 132 code | 1 blank | 34 comment | 13 complexity | 1434fc58cfef4af8cff9ec061f926f68 MD5 | raw file
  1. <?php
  2. /*
  3. IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002
  4. Version 1.61 - Simon Willison, 11th July 2003 (htmlentities -> htmlspecialchars)
  5. Site: http://scripts.incutio.com/xmlrpc/
  6. Manual: http://scripts.incutio.com/xmlrpc/manual.php
  7. Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php
  8. */
  9. /**
  10. * IXR消息
  11. *
  12. * @package IXR
  13. */
  14. class IXR_Message {
  15. var $message;
  16. var $messageType; // methodCall / methodResponse / fault
  17. var $faultCode;
  18. var $faultString;
  19. var $methodName;
  20. var $params;
  21. // Current variable stacks
  22. var $_arraystructs = array(); // The stack used to keep track of the current array/struct
  23. var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array
  24. var $_currentStructName = array(); // A stack as well
  25. var $_param;
  26. var $_value;
  27. var $_currentTag;
  28. var $_currentTagContents;
  29. // The XML parser
  30. var $_parser;
  31. function IXR_Message ($message) {
  32. $this->message = $message;
  33. }
  34. function parse() {
  35. // first remove the XML declaration
  36. $this->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message);
  37. if (trim($this->message) == '') {
  38. return false;
  39. }
  40. $this->_parser = xml_parser_create();
  41. // Set XML parser to take the case of tags in to account
  42. xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
  43. // Set XML parser callback functions
  44. xml_set_object($this->_parser, $this);
  45. xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
  46. xml_set_character_data_handler($this->_parser, 'cdata');
  47. if (!xml_parse($this->_parser, $this->message)) {
  48. /* die(sprintf('XML error: %s at line %d',
  49. xml_error_string(xml_get_error_code($this->_parser)),
  50. xml_get_current_line_number($this->_parser))); */
  51. return false;
  52. }
  53. xml_parser_free($this->_parser);
  54. // Grab the error messages, if any
  55. if ($this->messageType == 'fault') {
  56. $this->faultCode = $this->params[0]['faultCode'];
  57. $this->faultString = $this->params[0]['faultString'];
  58. }
  59. return true;
  60. }
  61. function tag_open($parser, $tag, $attr) {
  62. $this->currentTag = $tag;
  63. switch($tag) {
  64. case 'methodCall':
  65. case 'methodResponse':
  66. case 'fault':
  67. $this->messageType = $tag;
  68. break;
  69. /* Deal with stacks of arrays and structs */
  70. case 'data': // data is to all intents and puposes more interesting than array
  71. $this->_arraystructstypes[] = 'array';
  72. $this->_arraystructs[] = array();
  73. break;
  74. case 'struct':
  75. $this->_arraystructstypes[] = 'struct';
  76. $this->_arraystructs[] = array();
  77. break;
  78. }
  79. }
  80. function cdata($parser, $cdata) {
  81. $this->_currentTagContents .= $cdata;
  82. }
  83. function tag_close($parser, $tag) {
  84. $valueFlag = false;
  85. switch($tag) {
  86. case 'int':
  87. case 'i4':
  88. $value = (int)trim($this->_currentTagContents);
  89. $this->_currentTagContents = '';
  90. $valueFlag = true;
  91. break;
  92. case 'double':
  93. $value = (double)trim($this->_currentTagContents);
  94. $this->_currentTagContents = '';
  95. $valueFlag = true;
  96. break;
  97. case 'string':
  98. $value = (string)trim($this->_currentTagContents);
  99. $this->_currentTagContents = '';
  100. $valueFlag = true;
  101. break;
  102. case 'dateTime.iso8601':
  103. $value = new IXR_Date(trim($this->_currentTagContents));
  104. // $value = $iso->getTimestamp();
  105. $this->_currentTagContents = '';
  106. $valueFlag = true;
  107. break;
  108. case 'value':
  109. // "If no type is indicated, the type is string."
  110. if (trim($this->_currentTagContents) != '') {
  111. $value = (string)$this->_currentTagContents;
  112. $this->_currentTagContents = '';
  113. $valueFlag = true;
  114. }
  115. break;
  116. case 'boolean':
  117. $value = (boolean)trim($this->_currentTagContents);
  118. $this->_currentTagContents = '';
  119. $valueFlag = true;
  120. break;
  121. case 'base64':
  122. $value = base64_decode($this->_currentTagContents);
  123. $this->_currentTagContents = '';
  124. $valueFlag = true;
  125. break;
  126. /* Deal with stacks of arrays and structs */
  127. case 'data':
  128. case 'struct':
  129. $value = array_pop($this->_arraystructs);
  130. array_pop($this->_arraystructstypes);
  131. $valueFlag = true;
  132. break;
  133. case 'member':
  134. array_pop($this->_currentStructName);
  135. break;
  136. case 'name':
  137. $this->_currentStructName[] = trim($this->_currentTagContents);
  138. $this->_currentTagContents = '';
  139. break;
  140. case 'methodName':
  141. $this->methodName = trim($this->_currentTagContents);
  142. $this->_currentTagContents = '';
  143. break;
  144. }
  145. if ($valueFlag) {
  146. /*
  147. if (!is_array($value) && !is_object($value)) {
  148. $value = trim($value);
  149. }
  150. */
  151. if (count($this->_arraystructs) > 0) {
  152. // Add value to struct or array
  153. if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') {
  154. // Add to struct
  155. $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value;
  156. } else {
  157. // Add to array
  158. $this->_arraystructs[count($this->_arraystructs)-1][] = $value;
  159. }
  160. } else {
  161. // Just add as a paramater
  162. $this->params[] = $value;
  163. }
  164. }
  165. }
  166. }