PhpXmlRpc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace PhpXmlRpc;
  3. use PhpXmlRpc\Helper\Charset;
  4. use PhpXmlRpc\Helper\Http;
  5. use PhpXmlRpc\Helper\Interop;
  6. use PhpXmlRpc\Helper\XMLParser;
  7. /**
  8. * Manages global configuration for operation of the library.
  9. */
  10. class PhpXmlRpc
  11. {
  12. /**
  13. * @var int[]
  14. */
  15. public static $xmlrpcerr = array(
  16. 'unknown_method' => 1, // server
  17. /// @deprecated. left in for BC
  18. 'invalid_return' => 2, // client
  19. 'incorrect_params' => 3, // server
  20. 'introspect_unknown' => 4, // server
  21. 'http_error' => 5, // client
  22. 'no_data' => 6, // client
  23. 'no_ssl' => 7, // client
  24. 'curl_fail' => 8, // client
  25. 'invalid_request' => 15, // server
  26. 'no_curl' => 16, // client
  27. 'server_error' => 17, // server
  28. 'multicall_error' => 18, // client
  29. 'multicall_notstruct' => 9, // client
  30. 'multicall_nomethod' => 10, // client
  31. 'multicall_notstring' => 11, // client
  32. 'multicall_recursion' => 12, // client
  33. 'multicall_noparams' => 13, // client
  34. 'multicall_notarray' => 14, // client
  35. 'no_http2' => 19, // client
  36. 'unsupported_option' => 20, // client
  37. // the following 3 are meant to give greater insight than 'invalid_return'. They use the same code for BC,
  38. // but you can override their value in your own code
  39. 'invalid_xml' => 2, // client
  40. 'xml_not_compliant' => 2, // client
  41. 'xml_parsing_error' => 2, // client
  42. /// @todo verify: can these conflict with $xmlrpcerrxml?
  43. 'cannot_decompress' => 103,
  44. 'decompress_fail' => 104,
  45. 'dechunk_fail' => 105,
  46. 'server_cannot_decompress' => 106,
  47. 'server_decompress_fail' => 107,
  48. );
  49. /**
  50. * @var string[]
  51. */
  52. public static $xmlrpcstr = array(
  53. 'unknown_method' => 'Unknown method',
  54. /// @deprecated. left in for BC
  55. 'invalid_return' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
  56. 'incorrect_params' => 'Incorrect parameters passed to method',
  57. 'introspect_unknown' => "Can't introspect: method unknown",
  58. 'http_error' => "Didn't receive 200 OK from remote server",
  59. 'no_data' => 'No data received from server',
  60. 'no_ssl' => 'No SSL support compiled in',
  61. 'curl_fail' => 'CURL error',
  62. 'invalid_request' => 'Invalid request payload',
  63. 'no_curl' => 'No CURL support compiled in',
  64. 'server_error' => 'Internal server error',
  65. 'multicall_error' => 'Received from server invalid multicall response',
  66. 'multicall_notstruct' => 'system.multicall expected struct',
  67. 'multicall_nomethod' => 'Missing methodName',
  68. 'multicall_notstring' => 'methodName is not a string',
  69. 'multicall_recursion' => 'Recursive system.multicall forbidden',
  70. 'multicall_noparams' => 'Missing params',
  71. 'multicall_notarray' => 'params is not an array',
  72. 'no_http2' => 'No HTTP/2 support compiled in',
  73. 'unsupported_option' => 'Some client option is not supported with the transport method currently in use',
  74. // the following 3 are meant to give greater insight than 'invalid_return'. They use the same string for BC,
  75. // but you can override their value in your own code
  76. 'invalid_xml' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
  77. 'xml_not_compliant' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
  78. 'xml_parsing_error' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
  79. 'cannot_decompress' => 'Received from server compressed HTTP and cannot decompress',
  80. 'decompress_fail' => 'Received from server invalid compressed HTTP',
  81. 'dechunk_fail' => 'Received from server invalid chunked HTTP',
  82. 'server_cannot_decompress' => 'Received from client compressed HTTP request and cannot decompress',
  83. 'server_decompress_fail' => 'Received from client invalid compressed HTTP request',
  84. );
  85. /**
  86. * @var string
  87. * The charset encoding used by the server for received requests and by the client for received responses when
  88. * received charset cannot be determined and mbstring extension is not enabled.
  89. */
  90. public static $xmlrpc_defencoding = "UTF-8";
  91. /**
  92. * @var string[]
  93. * The list of preferred encodings used by the server for requests and by the client for responses to detect the
  94. * charset of the received payload when
  95. * - the charset cannot be determined by looking at http headers, xml declaration or BOM
  96. * - mbstring extension is enabled
  97. */
  98. public static $xmlrpc_detectencodings = array();
  99. /**
  100. * @var string
  101. * The encoding used internally by PHP.
  102. * String values received as xml will be converted to this, and php strings will be converted to xml as if
  103. * having been coded with this.
  104. * Valid also when defining names of xml-rpc methods
  105. */
  106. public static $xmlrpc_internalencoding = "UTF-8";
  107. /**
  108. * @var string
  109. */
  110. public static $xmlrpcName = "XML-RPC for PHP";
  111. /**
  112. * @var string
  113. */
  114. public static $xmlrpcVersion = "4.10.1";
  115. /**
  116. * @var int
  117. * Let user errors start at 800
  118. */
  119. public static $xmlrpcerruser = 800;
  120. /**
  121. * @var int
  122. * Let XML parse errors start at 100
  123. */
  124. public static $xmlrpcerrxml = 100;
  125. /**
  126. * @var bool
  127. * Set to TRUE to enable correct decoding of <NIL/> and <EX:NIL/> values
  128. */
  129. public static $xmlrpc_null_extension = false;
  130. /**
  131. * @var bool
  132. * Set to TRUE to make the library use DateTime objects instead of strings for all values parsed from incoming XML.
  133. * NB: if the received strings are not parseable as dates, NULL will be returned. To prevent that, enable as
  134. * well `xmlrpc_reject_invalid_values`, so that invalid dates will be rejected by the library
  135. */
  136. public static $xmlrpc_return_datetimes = false;
  137. /**
  138. * @var bool
  139. * Set to TRUE to make the library reject incoming xml which uses invalid data for xml-rpc elements, such
  140. * as base64 strings which can not be decoded, dateTime strings which do not represent a valid date, invalid bools,
  141. * floats and integers, method names with forbidden characters, or struct members missing the value or name
  142. */
  143. public static $xmlrpc_reject_invalid_values = false;
  144. /**
  145. * @var bool
  146. * Set to TRUE to enable encoding of php NULL values to <EX:NIL/> instead of <NIL/>
  147. */
  148. public static $xmlrpc_null_apache_encoding = false;
  149. public static $xmlrpc_null_apache_encoding_ns = "http://ws.apache.org/xmlrpc/namespaces/extensions";
  150. /**
  151. * @var int
  152. * Number of decimal digits used to serialize Double values.
  153. * @todo rename :'-(
  154. */
  155. public static $xmlpc_double_precision = 128;
  156. /**
  157. * @var string
  158. * Used to validate received date values. Alter this if the server/client you are communicating with uses date
  159. * formats non-conformant with the spec
  160. * NB: the string should not match any data which php can not successfully use in a DateTime object constructor call
  161. * NB: atm, the Date helper uses this regexp and expects to find matches in a specific order
  162. */
  163. public static $xmlrpc_datetime_format = '/^([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]|60)$/';
  164. /**
  165. * @var string
  166. * Used to validate received integer values. Alter this if the server/client you are communicating with uses
  167. * formats non-conformant with the spec.
  168. * We keep in spaces for BC, even though they are forbidden by the spec.
  169. * NB: the string should not match any data which php can not successfully cast to an integer
  170. */
  171. public static $xmlrpc_int_format = '/^[ \t]*[+-]?[0-9]+[ \t]*$/';
  172. /**
  173. * @var string
  174. * Used to validate received double values. Alter this if the server/client you are communicating with uses
  175. * formats non-conformant with the spec, e.g. with leading/trailing spaces/tabs/newlines.
  176. * We keep in spaces for BC, even though they are forbidden by the spec.
  177. * NB: the string should not match any data which php can not successfully cast to a float
  178. */
  179. public static $xmlrpc_double_format = '/^[ \t]*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?[ \t]*$/';
  180. /**
  181. * @var string
  182. * Used to validate received methodname values.
  183. * According to the spec: "The string may only contain identifier characters, upper and lower-case A-Z, the numeric
  184. * characters, 0-9, underscore, dot, colon and slash".
  185. * We keep in leading and trailing spaces for BC, even though they are forbidden by the spec.
  186. * But what about "identifier characters"? Is that meant to be 'identifier characters: upper and lower-case A-Z, ...'
  187. * or something else? If the latter, there is no consensus across programming languages about what is a valid
  188. * identifier character. PHP has one of the most crazy definitions of what is a valid identifier character, allowing
  189. * _bytes_ in range x80-xff, without even specifying a character set (and then lowercasing anyway in some cases)...
  190. */
  191. public static $xmlrpc_methodname_format = '|^[ \t]*[a-zA-Z0-9_.:/]+[ \t]*$|';
  192. /**
  193. * @var bool
  194. * Set this to false to have a warning added to the log whenever user code uses a deprecated method/parameter/property
  195. */
  196. public static $xmlrpc_silence_deprecations = true;
  197. // *** BC layer ***
  198. /**
  199. * Inject a logger into all classes of the PhpXmlRpc library which use one
  200. *
  201. * @param $logger
  202. * @return void
  203. */
  204. public static function setLogger($logger)
  205. {
  206. Charset::setLogger($logger);
  207. Client::setLogger($logger);
  208. Encoder::setLogger($logger);
  209. Http::setLogger($logger);
  210. Request::setLogger($logger);
  211. Server::setLogger($logger);
  212. Value::setLogger($logger);
  213. Wrapper::setLogger($logger);
  214. XMLParser::setLogger($logger);
  215. }
  216. /**
  217. * Makes the library use the error codes detailed at https://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
  218. *
  219. * @return void
  220. *
  221. * @tofo feature creep - allow switching back to the original set of codes; querying the current mode
  222. */
  223. public static function useInteropFaults()
  224. {
  225. self::$xmlrpcerr = Interop::$xmlrpcerr;
  226. self::$xmlrpcerruser = -Interop::$xmlrpcerruser;
  227. }
  228. /**
  229. * A function to be used for compatibility with legacy code: it creates all global variables which used to be declared,
  230. * such as library version etc...
  231. * @return void
  232. *
  233. * @deprecated
  234. */
  235. public static function exportGlobals()
  236. {
  237. $reflection = new \ReflectionClass('PhpXmlRpc\PhpXmlRpc');
  238. foreach ($reflection->getStaticProperties() as $name => $value) {
  239. if (!in_array($name, array('xmlrpc_return_datetimes', 'xmlrpc_reject_invalid_values', 'xmlrpc_datetime_format',
  240. 'xmlrpc_int_format', 'xmlrpc_double_format', 'xmlrpc_methodname_format', 'xmlrpc_silence_deprecations'))) {
  241. $GLOBALS[$name] = $value;
  242. }
  243. }
  244. // NB: all the variables exported into the global namespace below here do NOT guarantee 100% compatibility,
  245. // as they are NOT reimported back during calls to importGlobals()
  246. $reflection = new \ReflectionClass('PhpXmlRpc\Value');
  247. foreach ($reflection->getStaticProperties() as $name => $value) {
  248. if (!in_array($name, array('logger', 'charsetEncoder'))) {
  249. $GLOBALS[$name] = $value;
  250. }
  251. }
  252. /// @todo mke it possible to inject the XMLParser and Charset, as we do in other classes
  253. $parser = new Helper\XMLParser();
  254. $GLOBALS['xmlrpc_valid_parents'] = $parser->xmlrpc_valid_parents;
  255. $charset = Charset::instance();
  256. $GLOBALS['xml_iso88591_Entities'] = $charset->getEntities('iso88591');
  257. }
  258. /**
  259. * A function to be used for compatibility with legacy code: it gets the values of all global variables which used
  260. * to be declared, such as library version etc... and sets them to php classes.
  261. * It should be used by code which changed the values of those global variables to alter the working of the library.
  262. * Example code:
  263. * 1. include xmlrpc.inc
  264. * 2. set the values, e.g. $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
  265. * 3. import them: PhpXmlRpc\PhpXmlRpc::importGlobals();
  266. * 4. run your own code.
  267. *
  268. * @return void
  269. *
  270. * @deprecated
  271. *
  272. * @todo this function does not import back xmlrpc_valid_parents and xml_iso88591_Entities
  273. */
  274. public static function importGlobals()
  275. {
  276. $reflection = new \ReflectionClass('PhpXmlRpc\PhpXmlRpc');
  277. foreach ($reflection->getStaticProperties() as $name => $value) {
  278. if (!in_array($name, array('xmlrpc_return_datetimes', 'xmlrpc_reject_invalid_values', 'xmlrpc_datetime_format',
  279. 'xmlrpc_int_format', 'xmlrpc_double_format', 'xmlrpc_methodname_format', 'xmlrpc_silence_deprecations')))
  280. {
  281. if (isset($GLOBALS[$name])) {
  282. self::$$name = $GLOBALS[$name];
  283. }
  284. }
  285. }
  286. }
  287. }