/var/IXR/IntrospectionServer.php

https://gitlab.com/wuhang2003/typecho · PHP · 158 lines · 138 code · 1 blank · 19 comment · 18 complexity · c2ed5f9dc27f54756056c151755ddfeb MD5 · raw file

  1. <?php
  2. if (!defined('__TYPECHO_ROOT_DIR__')) exit;
  3. /*
  4. IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002
  5. Version 1.61 - Simon Willison, 11th July 2003 (htmlentities -> htmlspecialchars)
  6. Site: http://scripts.incutio.com/xmlrpc/
  7. Manual: http://scripts.incutio.com/xmlrpc/manual.php
  8. Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php
  9. */
  10. /**
  11. * IXR服务器
  12. *
  13. * @package IXR
  14. */
  15. class IXR_IntrospectionServer extends IXR_Server {
  16. var $signatures;
  17. var $help;
  18. function IXR_IntrospectionServer() {
  19. $this->setCallbacks();
  20. $this->setCapabilities();
  21. $this->capabilities['introspection'] = array(
  22. 'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html',
  23. 'specVersion' => 1
  24. );
  25. $this->addCallback(
  26. 'system.methodSignature',
  27. 'this:methodSignature',
  28. array('array', 'string'),
  29. 'Returns an array describing the return type and required parameters of a method'
  30. );
  31. $this->addCallback(
  32. 'system.getCapabilities',
  33. 'this:getCapabilities',
  34. array('struct'),
  35. 'Returns a struct describing the XML-RPC specifications supported by this server'
  36. );
  37. $this->addCallback(
  38. 'system.listMethods',
  39. 'this:listMethods',
  40. array('array'),
  41. 'Returns an array of available methods on this server'
  42. );
  43. $this->addCallback(
  44. 'system.methodHelp',
  45. 'this:methodHelp',
  46. array('string', 'string'),
  47. 'Returns a documentation string for the specified method'
  48. );
  49. }
  50. function addCallback($method, $callback, $args, $help) {
  51. $this->callbacks[$method] = $callback;
  52. $this->signatures[$method] = $args;
  53. $this->help[$method] = $help;
  54. }
  55. function call($methodname, $args) {
  56. // Make sure it's in an array
  57. if ($args && !is_array($args)) {
  58. $args = array($args);
  59. }
  60. // Over-rides default call method, adds signature check
  61. if (!$this->hasMethod($methodname)) {
  62. return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.');
  63. }
  64. $method = $this->callbacks[$methodname];
  65. $signature = $this->signatures[$methodname];
  66. $returnType = array_shift($signature);
  67. // Check the number of arguments
  68. if (count($args) != count($signature)) {
  69. // print 'Num of args: '.count($args).' Num in signature: '.count($signature);
  70. return new IXR_Error(-32602, 'server error. wrong number of method parameters');
  71. }
  72. // Check the argument types
  73. $ok = true;
  74. $argsbackup = $args;
  75. for ($i = 0, $j = count($args); $i < $j; $i++) {
  76. $arg = array_shift($args);
  77. $type = array_shift($signature);
  78. switch ($type) {
  79. case 'int':
  80. case 'i4':
  81. if (is_array($arg) || !is_int($arg)) {
  82. $ok = false;
  83. }
  84. break;
  85. case 'base64':
  86. case 'string':
  87. if (!is_string($arg)) {
  88. $ok = false;
  89. }
  90. break;
  91. case 'boolean':
  92. if ($arg !== false && $arg !== true) {
  93. $ok = false;
  94. }
  95. break;
  96. case 'float':
  97. case 'double':
  98. if (!is_float($arg)) {
  99. $ok = false;
  100. }
  101. break;
  102. case 'date':
  103. case 'dateTime.iso8601':
  104. if (!is_a($arg, 'IXR_Date')) {
  105. $ok = false;
  106. }
  107. break;
  108. }
  109. if (!$ok) {
  110. return new IXR_Error(-32602, 'server error. invalid method parameters');
  111. }
  112. }
  113. // It passed the test - run the "real" method call
  114. return parent::call($methodname, $argsbackup);
  115. }
  116. function methodSignature($method) {
  117. if (!$this->hasMethod($method)) {
  118. return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.');
  119. }
  120. // We should be returning an array of types
  121. $types = $this->signatures[$method];
  122. $return = array();
  123. foreach ($types as $type) {
  124. switch ($type) {
  125. case 'string':
  126. $return[] = 'string';
  127. break;
  128. case 'int':
  129. case 'i4':
  130. $return[] = 42;
  131. break;
  132. case 'double':
  133. $return[] = 3.1415;
  134. break;
  135. case 'dateTime.iso8601':
  136. $return[] = new IXR_Date(time());
  137. break;
  138. case 'boolean':
  139. $return[] = true;
  140. break;
  141. case 'base64':
  142. $return[] = new IXR_Base64('base64');
  143. break;
  144. case 'array':
  145. $return[] = array('array');
  146. break;
  147. case 'struct':
  148. $return[] = array('struct' => 'struct');
  149. break;
  150. }
  151. }
  152. return $return;
  153. }
  154. function methodHelp($method) {
  155. return $this->help[$method];
  156. }
  157. }