Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 401 and 402]

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