From c958bd7305f9f3e5ceb2a0b4b15f266fcea49709 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Tue, 20 Sep 2022 18:10:57 +0200 Subject: [PATCH] MDL-76055 libraries: Import phpxmlrpc 4.8.0 release --- lib/phpxmlrpc/Autoloader.php | 36 + lib/phpxmlrpc/Client.php | 1351 +++++++++++++++++ lib/phpxmlrpc/Encoder.php | 382 +++++ lib/phpxmlrpc/Exception/HttpException.php | 19 + .../Exception/PhpXmlrpcException.php | 7 + lib/phpxmlrpc/Helper/Charset.php | 328 ++++ lib/phpxmlrpc/Helper/Date.php | 60 + lib/phpxmlrpc/Helper/Http.php | 265 ++++ lib/phpxmlrpc/Helper/Logger.php | 72 + lib/phpxmlrpc/Helper/XMLParser.php | 720 +++++++++ lib/phpxmlrpc/PhpXmlRpc.php | 153 ++ lib/phpxmlrpc/Request.php | 422 +++++ lib/phpxmlrpc/Response.php | 263 ++++ lib/phpxmlrpc/Server.php | 1199 +++++++++++++++ lib/phpxmlrpc/Value.php | 672 ++++++++ lib/phpxmlrpc/Wrapper.php | 1162 ++++++++++++++ lib/phpxmlrpc/readme_moodle.txt | 20 + lib/thirdpartylibs.xml | 13 + 18 files changed, 7144 insertions(+) create mode 100644 lib/phpxmlrpc/Autoloader.php create mode 100644 lib/phpxmlrpc/Client.php create mode 100644 lib/phpxmlrpc/Encoder.php create mode 100644 lib/phpxmlrpc/Exception/HttpException.php create mode 100644 lib/phpxmlrpc/Exception/PhpXmlrpcException.php create mode 100644 lib/phpxmlrpc/Helper/Charset.php create mode 100644 lib/phpxmlrpc/Helper/Date.php create mode 100644 lib/phpxmlrpc/Helper/Http.php create mode 100644 lib/phpxmlrpc/Helper/Logger.php create mode 100644 lib/phpxmlrpc/Helper/XMLParser.php create mode 100644 lib/phpxmlrpc/PhpXmlRpc.php create mode 100644 lib/phpxmlrpc/Request.php create mode 100644 lib/phpxmlrpc/Response.php create mode 100644 lib/phpxmlrpc/Server.php create mode 100644 lib/phpxmlrpc/Value.php create mode 100644 lib/phpxmlrpc/Wrapper.php create mode 100644 lib/phpxmlrpc/readme_moodle.txt diff --git a/lib/phpxmlrpc/Autoloader.php b/lib/phpxmlrpc/Autoloader.php new file mode 100644 index 00000000000..40ec2190c23 --- /dev/null +++ b/lib/phpxmlrpc/Autoloader.php @@ -0,0 +1,36 @@ +send(). + * Use 'h2' to make the lib attempt to use http/2 over a secure connection, and 'h2c' + * for http/2 without tls. Note that 'h2c' will not use the h2c 'upgrade' method, and be + * thus incompatible with any server/proxy not supporting http/2. This is because POST + * request are not compatible with h2c upgrade. + */ + public function __construct($path, $server = '', $port = '', $method = '') + { + // allow user to specify all params in $path + if ($server == '' && $port == '' && $method == '') { + $parts = parse_url($path); + $server = $parts['host']; + $path = isset($parts['path']) ? $parts['path'] : ''; + if (isset($parts['query'])) { + $path .= '?' . $parts['query']; + } + if (isset($parts['fragment'])) { + $path .= '#' . $parts['fragment']; + } + if (isset($parts['port'])) { + $port = $parts['port']; + } + if (isset($parts['scheme'])) { + $method = $parts['scheme']; + } + if (isset($parts['user'])) { + $this->username = $parts['user']; + } + if (isset($parts['pass'])) { + $this->password = $parts['pass']; + } + } + if ($path == '' || $path[0] != '/') { + $this->path = '/' . $path; + } else { + $this->path = $path; + } + $this->server = $server; + if ($port != '') { + $this->port = $port; + } + if ($method != '') { + $this->method = $method; + } + + // if ZLIB is enabled, let the client by default accept compressed responses + if (function_exists('gzinflate') || ( + function_exists('curl_version') && (($info = curl_version()) && + ((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version']))) + ) + ) { + $this->accepted_compression = array('gzip', 'deflate'); + } + + // keepalives: enabled by default + $this->keepalive = true; + + // by default the xml parser can support these 3 charset encodings + $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); + + // Add all charsets which mbstring can handle, but remove junk not found in IANA registry at + // http://www.iana.org/assignments/character-sets/character-sets.xhtml + // NB: this is disabled to avoid making all the requests sent huge... mbstring supports more than 80 charsets! + /*if (function_exists('mb_list_encodings')) { + + $encodings = array_diff(mb_list_encodings(), array('pass', 'auto', 'wchar', 'BASE64', 'UUENCODE', 'ASCII', + 'HTML-ENTITIES', 'Quoted-Printable', '7bit','8bit', 'byte2be', 'byte2le', 'byte4be', 'byte4le')); + $this->accepted_charset_encodings = array_unique(array_merge($this->accepted_charset_encodings, $encodings)); + }*/ + + // initialize user_agent string + $this->user_agent = PhpXmlRpc::$xmlrpcName . ' ' . PhpXmlRpc::$xmlrpcVersion; + } + + /** + * Enable/disable the echoing to screen of the xmlrpc responses received. The default is not no output anything. + * + * The debugging information at level 1 includes the raw data returned from the XML-RPC server it was querying + * (including bot HTTP headers and the full XML payload), and the PHP value the client attempts to create to + * represent the value returned by the server + * At level2, the complete payload of the xmlrpc request is also printed, before being sent t the server. + * + * This option can be very useful when debugging servers as it allows you to see exactly what the client sends and + * the server returns. + * + * @param integer $level values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) + */ + public function setDebug($level) + { + $this->debug = $level; + } + + /** + * Sets the username and password for authorizing the client to the server. + * + * With the default (HTTP) transport, this information is used for HTTP Basic authorization. + * Note that username and password can also be set using the class constructor. + * With HTTP 1.1 and HTTPS transport, NTLM and Digest authentication protocols are also supported. To enable them use + * the constants CURLAUTH_DIGEST and CURLAUTH_NTLM as values for the auth type parameter. + * + * @param string $user username + * @param string $password password + * @param integer $authType auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC + * (basic auth). Note that auth types NTLM and Digest will only work if the Curl php + * extension is enabled. + */ + public function setCredentials($user, $password, $authType = 1) + { + $this->username = $user; + $this->password = $password; + $this->authtype = $authType; + } + + /** + * Set the optional certificate and passphrase used in SSL-enabled communication with a remote server. + * + * Note: to retrieve information about the client certificate on the server side, you will need to look into the + * environment variables which are set up by the webserver. Different webservers will typically set up different + * variables. + * + * @param string $cert the name of a file containing a PEM formatted certificate + * @param string $certPass the password required to use it + */ + public function setCertificate($cert, $certPass = '') + { + $this->cert = $cert; + $this->certpass = $certPass; + } + + /** + * Add a CA certificate to verify server with in SSL-enabled communication when SetSSLVerifypeer has been set to TRUE. + * + * See the php manual page about CURLOPT_CAINFO for more details. + * + * @param string $caCert certificate file name (or dir holding certificates) + * @param bool $isDir set to true to indicate cacert is a dir. defaults to false + */ + public function setCaCertificate($caCert, $isDir = false) + { + if ($isDir) { + $this->cacertdir = $caCert; + } else { + $this->cacert = $caCert; + } + } + + /** + * Set attributes for SSL communication: private SSL key. + * + * NB: does not work in older php/curl installs. + * Thanks to Daniel Convissor. + * + * @param string $key The name of a file containing a private SSL key + * @param string $keyPass The secret password needed to use the private SSL key + */ + public function setKey($key, $keyPass) + { + $this->key = $key; + $this->keypass = $keyPass; + } + + /** + * Set attributes for SSL communication: verify the remote host's SSL certificate, and cause the connection to fail + * if the cert verification fails. + * + * By default, verification is enabled. + * To specify custom SSL certificates to validate the server with, use the setCaCertificate method. + * + * @param bool $i enable/disable verification of peer certificate + */ + public function setSSLVerifyPeer($i) + { + $this->verifypeer = $i; + } + + /** + * Set attributes for SSL communication: verify the remote host's SSL certificate's common name (CN). + * + * Note that support for value 1 has been removed in cURL 7.28.1 + * + * @param int $i Set to 1 to only the existence of a CN, not that it matches + */ + public function setSSLVerifyHost($i) + { + $this->verifyhost = $i; + } + + /** + * Set attributes for SSL communication: SSL version to use. Best left at 0 (default value): let cURL decide + * + * @param int $i + */ + public function setSSLVersion($i) + { + $this->sslversion = $i; + } + + /** + * Set proxy info. + * + * NB: CURL versions before 7.11.10 cannot use a proxy to communicate with https servers. + * + * @param string $proxyHost + * @param string $proxyPort Defaults to 8080 for HTTP and 443 for HTTPS + * @param string $proxyUsername Leave blank if proxy has public access + * @param string $proxyPassword Leave blank if proxy has public access + * @param int $proxyAuthType defaults to CURLAUTH_BASIC (Basic authentication protocol); set to constant CURLAUTH_NTLM + * to use NTLM auth with proxy (has effect only when the client uses the HTTP 1.1 protocol) + */ + public function setProxy($proxyHost, $proxyPort, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1) + { + $this->proxy = $proxyHost; + $this->proxyport = $proxyPort; + $this->proxy_user = $proxyUsername; + $this->proxy_pass = $proxyPassword; + $this->proxy_authtype = $proxyAuthType; + } + + /** + * Enables/disables reception of compressed xmlrpc responses. + * + * This requires the "zlib" extension to be enabled in your php install. If it is, by default xmlrpc_client + * instances will enable reception of compressed content. + * Note that enabling reception of compressed responses merely adds some standard http headers to xmlrpc requests. + * It is up to the xmlrpc server to return compressed responses when receiving such requests. + * + * @param string $compMethod either 'gzip', 'deflate', 'any' or '' + */ + public function setAcceptedCompression($compMethod) + { + if ($compMethod == 'any') { + $this->accepted_compression = array('gzip', 'deflate'); + } elseif ($compMethod == false) { + $this->accepted_compression = array(); + } else { + $this->accepted_compression = array($compMethod); + } + } + + /** + * Enables/disables http compression of xmlrpc request. + * + * This requires the "zlib" extension to be enabled in your php install. + * Take care when sending compressed requests: servers might not support them (and automatic fallback to + * uncompressed requests is not yet implemented). + * + * @param string $compMethod either 'gzip', 'deflate' or '' + */ + public function setRequestCompression($compMethod) + { + $this->request_compression = $compMethod; + } + + /** + * Adds a cookie to list of cookies that will be sent to server with every further request (useful e.g. for keeping + * session info outside of the xml-rpc payload). + * + * NB: By default cookies are sent using the 'original/netscape' format, which is also the same as the RFC 2965; + * setting any param but name and value will turn the cookie into a 'version 1' cookie (i.e. RFC 2109 cookie) that + * might not be fully supported by the server. Note that RFC 2109 has currently 'historic' status... + * + * @param string $name nb: will not be escaped in the request's http headers. Take care not to use CTL chars or + * separators! + * @param string $value + * @param string $path leave this empty unless the xml-rpc server only accepts RFC 2109 cookies + * @param string $domain leave this empty unless the xml-rpc server only accepts RFC 2109 cookies + * @param int $port leave this empty unless the xml-rpc server only accepts RFC 2109 cookies + * + * @todo check correctness of urlencoding cookie value (copied from php way of doing it, but php is generally sending + * response not requests. We do the opposite...) + * @todo strip invalid chars from cookie name? As per RFC6265, we should follow RFC2616, Section 2.2 + */ + public function setCookie($name, $value = '', $path = '', $domain = '', $port = null) + { + $this->cookies[$name]['value'] = rawurlencode($value); + if ($path || $domain || $port) { + $this->cookies[$name]['path'] = $path; + $this->cookies[$name]['domain'] = $domain; + $this->cookies[$name]['port'] = $port; + $this->cookies[$name]['version'] = 1; + } else { + $this->cookies[$name]['version'] = 0; + } + } + + /** + * Directly set cURL options, for extra flexibility (when in cURL mode). + * + * It allows eg. to bind client to a specific IP interface / address. + * + * @param array $options + */ + public function setCurlOptions($options) + { + $this->extracurlopts = $options; + } + + /** + * @param int $useCurlMode self::USE_CURL_ALWAYS, self::USE_CURL_AUTO or self::USE_CURL_NEVER + */ + public function setUseCurl($useCurlMode) + { + $this->use_curl = $useCurlMode; + } + + + /** + * Set user-agent string that will be used by this client instance in http headers sent to the server. + * + * The default user agent string includes the name of this library and the version number. + * + * @param string $agentString + */ + public function setUserAgent($agentString) + { + $this->user_agent = $agentString; + } + + /** + * Send an xmlrpc request to the server. + * + * @param Request|Request[]|string $req The Request object, or an array of requests for using multicall, or the + * complete xml representation of a request. + * When sending an array of Request objects, the client will try to make use of + * a single 'system.multicall' xml-rpc method call to forward to the server all + * the requests in a single HTTP round trip, unless $this->no_multicall has + * been previously set to TRUE (see the multicall method below), in which case + * many consecutive xmlrpc requests will be sent. The method will return an + * array of Response objects in both cases. + * The third variant allows to build by hand (or any other means) a complete + * xmlrpc request message, and send it to the server. $req should be a string + * containing the complete xml representation of the request. It is e.g. useful + * when, for maximal speed of execution, the request is serialized into a + * string using the native php xmlrpc functions (see http://www.php.net/xmlrpc) + * @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply. + * This timeout value is passed to fsockopen(). It is also used for detecting server + * timeouts during communication (i.e. if the server does not send anything to the client + * for $timeout seconds, the connection will be closed). + * @param string $method valid values are 'http', 'http11', 'https', 'h2' and 'h2c'. If left unspecified, + * the http protocol chosen during creation of the object will be used. + * Use 'h2' to make the lib attempt to use http/2 over a secure connection, and 'h2c' + * for http/2 without tls. Note that 'h2c' will not use the h2c 'upgrade' method, and be + * thus incompatible with any server/proxy not supporting http/2. This is because POST + * request are not compatible with h2c upgrade. + * + * @return Response|Response[] Note that the client will always return a Response object, even if the call fails + * @todo allow throwing exceptions instead of returning responses in case of failed calls and/or Fault responses + * @todo refactor: we now support many options besides connection timeout and http version to use. Why only privilege those? + */ + public function send($req, $timeout = 0, $method = '') + { + // if user does not specify http protocol, use native method of this client + // (i.e. method set during call to constructor) + if ($method == '') { + $method = $this->method; + } + + if (is_array($req)) { + // $req is an array of Requests + $r = $this->multicall($req, $timeout, $method); + + return $r; + } elseif (is_string($req)) { + $n = new Request(''); + $n->payload = $req; + $req = $n; + } + + // where req is a Request + $req->setDebug($this->debug); + + /// @todo we could be smarter about this and force usage of curl in scenarios where it is both available and + /// needed, such as digest or ntlm auth. Do not attempt to use it for https if not present + $useCurl = ($this->use_curl == self::USE_CURL_ALWAYS) || ($this->use_curl == self::USE_CURL_AUTO && + (in_array($method, array('https', 'http11', 'h2c', 'h2')))); + + if ($useCurl) { + $r = $this->sendPayloadCURL( + $req, + $this->server, + $this->port, + $timeout, + $this->username, + $this->password, + $this->authtype, + $this->cert, + $this->certpass, + $this->cacert, + $this->cacertdir, + $this->proxy, + $this->proxyport, + $this->proxy_user, + $this->proxy_pass, + $this->proxy_authtype, + // bc + $method == 'http11' ? 'http' : $method, + $this->keepalive, + $this->key, + $this->keypass, + $this->sslversion + ); + } else { + // plain 'http 1.0': default to using socket + $r = $this->sendPayloadSocket( + $req, + $this->server, + $this->port, + $timeout, + $this->username, + $this->password, + $this->authtype, + $this->cert, + $this->certpass, + $this->cacert, + $this->cacertdir, + $this->proxy, + $this->proxyport, + $this->proxy_user, + $this->proxy_pass, + $this->proxy_authtype, + $method, + $this->key, + $this->keypass, + $this->sslversion + ); + } + + return $r; + } + + /** + * @deprecated + * @param Request $req + * @param string $server + * @param int $port + * @param int $timeout + * @param string $username + * @param string $password + * @param int $authType + * @param string $proxyHost + * @param int $proxyPort + * @param string $proxyUsername + * @param string $proxyPassword + * @param int $proxyAuthType + * @param string $method + * @return Response + */ + protected function sendPayloadHTTP10($req, $server, $port, $timeout = 0, $username = '', $password = '', + $authType = 1, $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, + $method='http') + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return $this->sendPayloadSocket($req, $server, $port, $timeout, $username, $password, $authType, null, null, + null, null, $proxyHost, $proxyPort, $proxyUsername, $proxyPassword, $proxyAuthType, $method); + } + + /** + * @deprecated + * @param Request $req + * @param string $server + * @param int $port + * @param int $timeout + * @param string $username + * @param string $password + * @param int $authType + * @param string $cert + * @param string $certPass + * @param string $caCert + * @param string $caCertDir + * @param string $proxyHost + * @param int $proxyPort + * @param string $proxyUsername + * @param string $proxyPassword + * @param int $proxyAuthType + * @param bool $keepAlive + * @param string $key + * @param string $keyPass + * @param int $sslVersion + * @return Response + */ + protected function sendPayloadHTTPS($req, $server, $port, $timeout = 0, $username = '', $password = '', + $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0, + $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $keepAlive = false, $key = '', $keyPass = '', + $sslVersion = 0) + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return $this->sendPayloadCURL($req, $server, $port, $timeout, $username, + $password, $authType, $cert, $certPass, $caCert, $caCertDir, $proxyHost, $proxyPort, + $proxyUsername, $proxyPassword, $proxyAuthType, 'https', $keepAlive, $key, $keyPass, $sslVersion); + } + + /** + * @param Request $req + * @param string $server + * @param int $port + * @param int $timeout + * @param string $username + * @param string $password + * @param int $authType only value supported is 1 + * @param string $cert + * @param string $certPass + * @param string $caCert + * @param string $caCertDir + * @param string $proxyHost + * @param int $proxyPort + * @param string $proxyUsername + * @param string $proxyPassword + * @param int $proxyAuthType only value supported is 1 + * @param string $method 'http' (synonym for 'http10'), 'http10' or 'https' + * @param string $key + * @param string $keyPass @todo not implemented yet. + * @param int $sslVersion @todo not implemented yet. See http://php.net/manual/en/migration56.openssl.php + * @return Response + * + * @todo refactor: we get many options for the call passed in, but some we use from $this. We should clean that up + */ + protected function sendPayloadSocket($req, $server, $port, $timeout = 0, $username = '', $password = '', + $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0, + $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method='http', $key = '', $keyPass = '', + $sslVersion = 0) + { + /// @todo log a warning if passed an unsupported method + + if ($port == 0) { + $port = ( $method === 'https' ) ? 443 : 80; + } + + // Only create the payload if it was not created previously + if (empty($req->payload)) { + $req->serialize($this->request_charset_encoding); + } + + $payload = $req->payload; + // Deflate request body and set appropriate request headers + $encodingHdr = ''; + if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) { + if ($this->request_compression == 'gzip') { + $a = @gzencode($payload); + if ($a) { + $payload = $a; + $encodingHdr = "Content-Encoding: gzip\r\n"; + } + } else { + $a = @gzcompress($payload); + if ($a) { + $payload = $a; + $encodingHdr = "Content-Encoding: deflate\r\n"; + } + } + } + + // thanks to Grant Rauscher for this + $credentials = ''; + if ($username != '') { + $credentials = 'Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n"; + if ($authType != 1) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported with HTTP 1.0'); + } + } + + $acceptedEncoding = ''; + if (is_array($this->accepted_compression) && count($this->accepted_compression)) { + $acceptedEncoding = 'Accept-Encoding: ' . implode(', ', $this->accepted_compression) . "\r\n"; + } + + $proxyCredentials = ''; + if ($proxyHost) { + if ($proxyPort == 0) { + $proxyPort = 8080; + } + $connectServer = $proxyHost; + $connectPort = $proxyPort; + $transport = 'tcp'; + $uri = 'http://' . $server . ':' . $port . $this->path; + if ($proxyUsername != '') { + if ($proxyAuthType != 1) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported with HTTP 1.0'); + } + $proxyCredentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyUsername . ':' . $proxyPassword) . "\r\n"; + } + } else { + $connectServer = $server; + $connectPort = $port; + $transport = ( $method === 'https' ) ? 'tls' : 'tcp'; + $uri = $this->path; + } + + // Cookie generation, as per rfc2965 (version 1 cookies) or netscape's rules (version 0 cookies) + $cookieHeader = ''; + if (count($this->cookies)) { + $version = ''; + foreach ($this->cookies as $name => $cookie) { + if ($cookie['version']) { + $version = ' $Version="' . $cookie['version'] . '";'; + $cookieHeader .= ' ' . $name . '="' . $cookie['value'] . '";'; + if ($cookie['path']) { + $cookieHeader .= ' $Path="' . $cookie['path'] . '";'; + } + if ($cookie['domain']) { + $cookieHeader .= ' $Domain="' . $cookie['domain'] . '";'; + } + if ($cookie['port']) { + $cookieHeader .= ' $Port="' . $cookie['port'] . '";'; + } + } else { + $cookieHeader .= ' ' . $name . '=' . $cookie['value'] . ";"; + } + } + $cookieHeader = 'Cookie:' . $version . substr($cookieHeader, 0, -1) . "\r\n"; + } + + // omit port if default + if (($port == 80 && in_array($method, array('http', 'http10'))) || ($port == 443 && $method == 'https')) { + $port = ''; + } else { + $port = ':' . $port; + } + + $op = 'POST ' . $uri . " HTTP/1.0\r\n" . + 'User-Agent: ' . $this->user_agent . "\r\n" . + 'Host: ' . $server . $port . "\r\n" . + $credentials . + $proxyCredentials . + $acceptedEncoding . + $encodingHdr . + 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings) . "\r\n" . + $cookieHeader . + 'Content-Type: ' . $req->content_type . "\r\nContent-Length: " . + strlen($payload) . "\r\n\r\n" . + $payload; + + if ($this->debug > 1) { + $this->getLogger()->debugMessage("---SENDING---\n$op\n---END---"); + } + + $contextOptions = array(); + if ($method == 'https') { + if ($cert != '') { + $contextOptions['ssl']['local_cert'] = $cert; + if ($certPass != '') { + $contextOptions['ssl']['passphrase'] = $certPass; + } + } + if ($caCert != '') { + $contextOptions['ssl']['cafile'] = $caCert; + } + if ($caCertDir != '') { + $contextOptions['ssl']['capath'] = $caCertDir; + } + if ($key != '') { + $contextOptions['ssl']['local_pk'] = $key; + } + $contextOptions['ssl']['verify_peer'] = $this->verifypeer; + $contextOptions['ssl']['verify_peer_name'] = $this->verifypeer; + } + + $context = stream_context_create($contextOptions); + + if ($timeout <= 0) { + $connectTimeout = ini_get('default_socket_timeout'); + } else { + $connectTimeout = $timeout; + } + + $this->errno = 0; + $this->errstr = ''; + + $fp = @stream_socket_client("$transport://$connectServer:$connectPort", $this->errno, $this->errstr, $connectTimeout, + STREAM_CLIENT_CONNECT, $context); + if ($fp) { + if ($timeout > 0) { + stream_set_timeout($fp, $timeout); + } + } else { + if ($this->errstr == '') { + $err = error_get_last(); + $this->errstr = $err['message']; + } + + $this->errstr = 'Connect error: ' . $this->errstr; + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr . ' (' . $this->errno . ')'); + + return $r; + } + + if (!fputs($fp, $op, strlen($op))) { + fclose($fp); + $this->errstr = 'Write error'; + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr); + + return $r; + } + + // Close socket before parsing. + // It should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects) + $ipd = ''; + do { + // shall we check for $data === FALSE? + // as per the manual, it signals an error + $ipd .= fread($fp, 32768); + } while (!feof($fp)); + fclose($fp); + + $r = $req->parseResponse($ipd, false, $this->return_type); + + return $r; + } + + /** + * Contributed by Justin Miller + * Requires curl to be built into PHP + * NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers! + * + * @param Request $req + * @param string $server + * @param int $port + * @param int $timeout + * @param string $username + * @param string $password + * @param int $authType + * @param string $cert + * @param string $certPass + * @param string $caCert + * @param string $caCertDir + * @param string $proxyHost + * @param int $proxyPort + * @param string $proxyUsername + * @param string $proxyPassword + * @param int $proxyAuthType + * @param string $method 'http' (let curl decide), 'http10', 'http11', 'https', 'h2c' or 'h2' + * @param bool $keepAlive + * @param string $key + * @param string $keyPass + * @param int $sslVersion + * @return Response + * + * @todo refactor: we get many options for the call passed in, but some we use from $this. We should clean that up + */ + protected function sendPayloadCURL($req, $server, $port, $timeout = 0, $username = '', $password = '', + $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0, + $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method = 'https', $keepAlive = false, $key = '', + $keyPass = '', $sslVersion = 0) + { + if (!function_exists('curl_init')) { + $this->errstr = 'CURL unavailable on this install'; + return new Response(0, PhpXmlRpc::$xmlrpcerr['no_curl'], PhpXmlRpc::$xmlrpcstr['no_curl']); + } + if ($method == 'https' || $method == 'h2') { + // q: what about installs where we get back a string, but curl is linked to other ssl libs than openssl? + if (($info = curl_version()) && + ((is_string($info) && strpos($info, 'OpenSSL') === null) || (is_array($info) && !isset($info['ssl_version']))) + ) { + $this->errstr = 'SSL unavailable on this install'; + return new Response(0, PhpXmlRpc::$xmlrpcerr['no_ssl'], PhpXmlRpc::$xmlrpcstr['no_ssl']); + } + } + if (($method == 'h2' && !defined('CURL_HTTP_VERSION_2_0')) || + ($method == 'h2c' && !defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE'))) { + $this->errstr = 'HTTP/2 unavailable on this install'; + return new Response(0, PhpXmlRpc::$xmlrpcerr['no_http2'], PhpXmlRpc::$xmlrpcstr['no_http2']); + } + + $curl = $this->prepareCurlHandle($req, $server, $port, $timeout, $username, $password, + $authType, $cert, $certPass, $caCert, $caCertDir, $proxyHost, $proxyPort, + $proxyUsername, $proxyPassword, $proxyAuthType, $method, $keepAlive, $key, + $keyPass, $sslVersion); + + $result = curl_exec($curl); + + if ($this->debug > 1) { + $message = "---CURL INFO---\n"; + foreach (curl_getinfo($curl) as $name => $val) { + if (is_array($val)) { + $val = implode("\n", $val); + } + $message .= $name . ': ' . $val . "\n"; + } + $message .= '---END---'; + $this->getLogger()->debugMessage($message); + } + + if (!$result) { + /// @todo we should use a better check here - what if we get back '' or '0'? + + $this->errstr = 'no response'; + $resp = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl)); + curl_close($curl); + if ($keepAlive) { + $this->xmlrpc_curl_handle = null; + } + } else { + if (!$keepAlive) { + curl_close($curl); + } + $resp = $req->parseResponse($result, true, $this->return_type); + // if we got back a 302, we can not reuse the curl handle for later calls + if ($resp->faultCode() == PhpXmlRpc::$xmlrpcerr['http_error'] && $keepAlive) { + curl_close($curl); + $this->xmlrpc_curl_handle = null; + } + } + + return $resp; + } + + protected function prepareCurlHandle($req, $server, $port, $timeout = 0, $username = '', $password = '', + $authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0, + $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method = 'https', $keepAlive = false, $key = '', + $keyPass = '', $sslVersion = 0) + { + if ($port == 0) { + if (in_array($method, array('http', 'http10', 'http11', 'h2c'))) { + $port = 80; + } else { + $port = 443; + } + } + + // Only create the payload if it was not created previously + if (empty($req->payload)) { + $req->serialize($this->request_charset_encoding); + } + + // Deflate request body and set appropriate request headers + $payload = $req->payload; + if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) { + if ($this->request_compression == 'gzip') { + $a = @gzencode($payload); + if ($a) { + $payload = $a; + $encodingHdr = 'Content-Encoding: gzip'; + } + } else { + $a = @gzcompress($payload); + if ($a) { + $payload = $a; + $encodingHdr = 'Content-Encoding: deflate'; + } + } + } else { + $encodingHdr = ''; + } + + if (!$keepAlive || !$this->xmlrpc_curl_handle) { + if ($method == 'http11' || $method == 'http10' || $method == 'h2c') { + $protocol = 'http'; + } else { + if ($method == 'h2') { + $protocol = 'https'; + } else { + // http, https + $protocol = $method; + } + } + $curl = curl_init($protocol . '://' . $server . ':' . $port . $this->path); + if ($keepAlive) { + $this->xmlrpc_curl_handle = $curl; + } + } else { + $curl = $this->xmlrpc_curl_handle; + } + + // results into variable + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + + if ($this->debug > 1) { + curl_setopt($curl, CURLOPT_VERBOSE, true); + /// @todo allow callers to redirect curlopt_stderr to some stream which can be buffered + } + curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent); + // required for XMLRPC: post the data + curl_setopt($curl, CURLOPT_POST, 1); + // the data + curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); + + // return the header too + curl_setopt($curl, CURLOPT_HEADER, 1); + + // NB: if we set an empty string, CURL will add http header indicating + // ALL methods it is supporting. This is possibly a better option than letting the user tell what curl can / cannot do... + if (is_array($this->accepted_compression) && count($this->accepted_compression)) { + //curl_setopt($curl, CURLOPT_ENCODING, implode(',', $this->accepted_compression)); + // empty string means 'any supported by CURL' (shall we catch errors in case CURLOPT_SSLKEY undefined ?) + if (count($this->accepted_compression) == 1) { + curl_setopt($curl, CURLOPT_ENCODING, $this->accepted_compression[0]); + } else { + curl_setopt($curl, CURLOPT_ENCODING, ''); + } + } + // extra headers + $headers = array('Content-Type: ' . $req->content_type, 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings)); + // if no keepalive is wanted, let the server know it in advance + if (!$keepAlive) { + $headers[] = 'Connection: close'; + } + // request compression header + if ($encodingHdr) { + $headers[] = $encodingHdr; + } + + // Fix the HTTP/1.1 417 Expectation Failed Bug (curl by default adds a 'Expect: 100-continue' header when POST + // size exceeds 1025 bytes, apparently) + $headers[] = 'Expect:'; + + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + // timeout is borked + if ($timeout) { + curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1); + } + + switch($method) { + case 'http10': + curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + break; + case 'http11': + curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); + break; + case 'h2c': + curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE); + break; + case 'h2': + curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); + break; + } + + if ($username && $password) { + curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password); + if (defined('CURLOPT_HTTPAUTH')) { + curl_setopt($curl, CURLOPT_HTTPAUTH, $authType); + } elseif ($authType != 1) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported by the current PHP/curl install'); + } + } + + if ($method == 'https' || $method == 'h2') { + // set cert file + if ($cert) { + curl_setopt($curl, CURLOPT_SSLCERT, $cert); + } + // set cert password + if ($certPass) { + curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $certPass); + } + // whether to verify remote host's cert + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifypeer); + // set ca certificates file/dir + if ($caCert) { + curl_setopt($curl, CURLOPT_CAINFO, $caCert); + } + if ($caCertDir) { + curl_setopt($curl, CURLOPT_CAPATH, $caCertDir); + } + // set key file (shall we catch errors in case CURLOPT_SSLKEY undefined ?) + if ($key) { + curl_setopt($curl, CURLOPT_SSLKEY, $key); + } + // set key password (shall we catch errors in case CURLOPT_SSLKEY undefined ?) + if ($keyPass) { + curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keyPass); + } + // whether to verify cert's common name (CN); 0 for no, 1 to verify that it exists, and 2 to verify that + // it matches the hostname used + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->verifyhost); + // allow usage of different SSL versions + curl_setopt($curl, CURLOPT_SSLVERSION, $sslVersion); + } + + // proxy info + if ($proxyHost) { + if ($proxyPort == 0) { + $proxyPort = 8080; // NB: even for HTTPS, local connection is on port 8080 + } + curl_setopt($curl, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort); + if ($proxyUsername) { + curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyUsername . ':' . $proxyPassword); + if (defined('CURLOPT_PROXYAUTH')) { + curl_setopt($curl, CURLOPT_PROXYAUTH, $proxyAuthType); + } elseif ($proxyAuthType != 1) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported by the current PHP/curl install'); + } + } + } + + // NB: should we build cookie http headers by hand rather than let CURL do it? + // the following code does not honour 'expires', 'path' and 'domain' cookie attributes set to client obj the the user... + if (count($this->cookies)) { + $cookieHeader = ''; + foreach ($this->cookies as $name => $cookie) { + $cookieHeader .= $name . '=' . $cookie['value'] . '; '; + } + curl_setopt($curl, CURLOPT_COOKIE, substr($cookieHeader, 0, -2)); + } + + foreach ($this->extracurlopts as $opt => $val) { + curl_setopt($curl, $opt, $val); + } + + if ($this->debug > 1) { + $this->getLogger()->debugMessage("---SENDING---\n$payload\n---END---"); + } + + return $curl; + } + + /** + * Send an array of requests and return an array of responses. + * + * Unless $this->no_multicall has been set to true, it will try first to use one single xmlrpc call to server method + * system.multicall, and revert to sending many successive calls in case of failure. + * This failure is also stored in $this->no_multicall for subsequent calls. + * Unfortunately, there is no server error code universally used to denote the fact that multicall is unsupported, + * so there is no way to reliably distinguish between that and a temporary failure. + * If you are sure that server supports multicall and do not want to fallback to using many single calls, set the + * fourth parameter to FALSE. + * + * NB: trying to shoehorn extra functionality into existing syntax has resulted + * in pretty much convoluted code... + * + * @param Request[] $reqs an array of Request objects + * @param integer $timeout connection timeout (in seconds). See the details in the docs for the send() method + * @param string $method the http protocol variant to be used. See the details in the docs for the send() method + * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be + * attempted + * + * @return Response[] + */ + public function multicall($reqs, $timeout = 0, $method = '', $fallback = true) + { + if ($method == '') { + $method = $this->method; + } + if (!$this->no_multicall) { + $results = $this->_try_multicall($reqs, $timeout, $method); + if (is_array($results)) { + // System.multicall succeeded + return $results; + } else { + // either system.multicall is unsupported by server, + // or call failed for some other reason. + if ($fallback) { + // Don't try it next time... + $this->no_multicall = true; + } else { + if (is_a($results, '\PhpXmlRpc\Response')) { + $result = $results; + } else { + $result = new Response(0, PhpXmlRpc::$xmlrpcerr['multicall_error'], PhpXmlRpc::$xmlrpcstr['multicall_error']); + } + } + } + } else { + // override fallback, in case careless user tries to do two + // opposite things at the same time + $fallback = true; + } + + $results = array(); + if ($fallback) { + // system.multicall is (probably) unsupported by server: + // emulate multicall via multiple requests + /// @todo use curl multi_ functions to make this quicker + foreach ($reqs as $req) { + $results[] = $this->send($req, $timeout, $method); + } + } else { + // user does NOT want to fallback on many single calls: + // since we should always return an array of responses, + // return an array with the same error repeated n times + foreach ($reqs as $req) { + $results[] = $result; + } + } + + return $results; + } + + /** + * Attempt to boxcar $reqs via system.multicall. + * + * Returns either an array of Response, a single error Response or false (when received response does not respect + * valid multicall syntax). + * + * @param Request[] $reqs + * @param int $timeout + * @param string $method + * @return Response[]|false|mixed|Response + */ + private function _try_multicall($reqs, $timeout, $method) + { + // Construct multicall request + $calls = array(); + foreach ($reqs as $req) { + $call['methodName'] = new Value($req->method(), 'string'); + $numParams = $req->getNumParams(); + $params = array(); + for ($i = 0; $i < $numParams; $i++) { + $params[$i] = $req->getParam($i); + } + $call['params'] = new Value($params, 'array'); + $calls[] = new Value($call, 'struct'); + } + $multiCall = new Request('system.multicall'); + $multiCall->addParam(new Value($calls, 'array')); + + // Attempt RPC call + $result = $this->send($multiCall, $timeout, $method); + + if ($result->faultCode() != 0) { + // call to system.multicall failed + return $result; + } + + // Unpack responses. + $rets = $result->value(); + + if ($this->return_type == 'xml') { + return $rets; + } elseif ($this->return_type == 'phpvals') { + /// @todo test this code branch... + $rets = $result->value(); + if (!is_array($rets)) { + return false; // bad return type from system.multicall + } + $numRets = count($rets); + if ($numRets != count($reqs)) { + return false; // wrong number of return values. + } + + $response = array(); + for ($i = 0; $i < $numRets; $i++) { + $val = $rets[$i]; + if (!is_array($val)) { + return false; + } + switch (count($val)) { + case 1: + if (!isset($val[0])) { + return false; // Bad value + } + // Normal return value + $response[$i] = new Response($val[0], 0, '', 'phpvals'); + break; + case 2: + /// @todo remove usage of @: it is apparently quite slow + $code = @$val['faultCode']; + if (!is_int($code)) { + return false; + } + $str = @$val['faultString']; + if (!is_string($str)) { + return false; + } + $response[$i] = new Response(0, $code, $str); + break; + default: + return false; + } + } + + return $response; + } else { + // return type == 'xmlrpcvals' + + $rets = $result->value(); + if ($rets->kindOf() != 'array') { + return false; // bad return type from system.multicall + } + $numRets = $rets->count(); + if ($numRets != count($reqs)) { + return false; // wrong number of return values. + } + + $response = array(); + foreach($rets as $val) { + switch ($val->kindOf()) { + case 'array': + if ($val->count() != 1) { + return false; // Bad value + } + // Normal return value + $response[] = new Response($val[0]); + break; + case 'struct': + $code = $val['faultCode']; + /** @var Value $code */ + if ($code->kindOf() != 'scalar' || $code->scalartyp() != 'int') { + return false; + } + $str = $val['faultString']; + /** @var Value $str */ + if ($str->kindOf() != 'scalar' || $str->scalartyp() != 'string') { + return false; + } + $response[] = new Response(0, $code->scalarval(), $str->scalarval()); + break; + default: + return false; + } + } + + return $response; + } + } +} diff --git a/lib/phpxmlrpc/Encoder.php b/lib/phpxmlrpc/Encoder.php new file mode 100644 index 00000000000..7ad2adb06d0 --- /dev/null +++ b/lib/phpxmlrpc/Encoder.php @@ -0,0 +1,382 @@ +kindOf()) { + case 'scalar': + if (in_array('extension_api', $options)) { + $val = reset($xmlrpcVal->me); + $typ = key($xmlrpcVal->me); + switch ($typ) { + case 'dateTime.iso8601': + $xmlrpcVal = array( + 'xmlrpc_type' => 'datetime', + 'scalar' => $val, + 'timestamp' => \PhpXmlRpc\Helper\Date::iso8601Decode($val) + ); + return (object)$xmlrpcVal; + case 'base64': + $xmlrpcVal = array( + 'xmlrpc_type' => 'base64', + 'scalar' => $val + ); + return (object)$xmlrpcVal; + case 'string': + if (isset($options['extension_api_encoding'])) { + $dval = @iconv('UTF-8', $options['extension_api_encoding'], $val); + if ($dval !== false) { + return $dval; + } + } + //return $val; + // break through voluntarily + default: + return $val; + } + } + if (in_array('dates_as_objects', $options) && $xmlrpcVal->scalartyp() == 'dateTime.iso8601') { + // we return a Datetime object instead of a string since now the constructor of xmlrpc value accepts + // safely strings, ints and datetimes, we cater to all 3 cases here + $out = $xmlrpcVal->scalarval(); + if (is_string($out)) { + $out = strtotime($out); + } + if (is_int($out)) { + $result = new \DateTime(); + $result->setTimestamp($out); + + return $result; + } elseif (is_a($out, 'DateTimeInterface')) { + return $out; + } + } + return $xmlrpcVal->scalarval(); + + case 'array': + $arr = array(); + foreach($xmlrpcVal as $value) { + $arr[] = $this->decode($value, $options); + } + return $arr; + + case 'struct': + // If user said so, try to rebuild php objects for specific struct vals. + /// @todo should we raise a warning for class not found? + // shall we check for proper subclass of xmlrpc value instead of presence of _php_class to detect + // what we can do? + if (in_array('decode_php_objs', $options) && $xmlrpcVal->_php_class != '' + && class_exists($xmlrpcVal->_php_class) + ) { + $obj = @new $xmlrpcVal->_php_class(); + foreach ($xmlrpcVal as $key => $value) { + $obj->$key = $this->decode($value, $options); + } + return $obj; + } else { + $arr = array(); + foreach ($xmlrpcVal as $key => $value) { + $arr[$key] = $this->decode($value, $options); + } + return $arr; + } + + case 'msg': + $paramCount = $xmlrpcVal->getNumParams(); + $arr = array(); + for ($i = 0; $i < $paramCount; $i++) { + $arr[] = $this->decode($xmlrpcVal->getParam($i), $options); + } + return $arr; + + /// @todo throw on unsupported type + } + } + + /** + * Takes native php types and encodes them into xmlrpc PHP object format. + * It will not re-encode xmlrpc value objects. + * + * Feature creep -- could support more types via optional type argument + * (string => datetime support has been added, ??? => base64 not yet) + * + * If given a proper options parameter, php object instances will be encoded into 'special' xmlrpc values, that can + * later be decoded into php objects by calling php_xmlrpc_decode() with a corresponding option + * + * @author Dan Libby (dan@libby.com) + * + * @param mixed $phpVal the value to be converted into an xmlrpc value object + * @param array $options can include 'encode_php_objs', 'auto_dates', 'null_extension' or 'extension_api' + * + * @return Value + */ + public function encode($phpVal, $options = array()) + { + $type = gettype($phpVal); + switch ($type) { + case 'string': + /// @todo should we be stricter in the accepted dates (ie. reject more of invalid days & times)? + if (in_array('auto_dates', $options) && preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $phpVal)) { + $xmlrpcVal = new Value($phpVal, Value::$xmlrpcDateTime); + } else { + $xmlrpcVal = new Value($phpVal, Value::$xmlrpcString); + } + break; + case 'integer': + $xmlrpcVal = new Value($phpVal, Value::$xmlrpcInt); + break; + case 'double': + $xmlrpcVal = new Value($phpVal, Value::$xmlrpcDouble); + break; + // Add support for encoding/decoding of booleans, since they are supported in PHP + case 'boolean': + $xmlrpcVal = new Value($phpVal, Value::$xmlrpcBoolean); + break; + case 'array': + // PHP arrays can be encoded to either xmlrpc structs or arrays, depending on whether they are hashes + // or plain 0..n integer indexed + // A shorter one-liner would be + // $tmp = array_diff(array_keys($phpVal), range(0, count($phpVal)-1)); + // but execution time skyrockets! + $j = 0; + $arr = array(); + $ko = false; + foreach ($phpVal as $key => $val) { + $arr[$key] = $this->encode($val, $options); + if (!$ko && $key !== $j) { + $ko = true; + } + $j++; + } + if ($ko) { + $xmlrpcVal = new Value($arr, Value::$xmlrpcStruct); + } else { + $xmlrpcVal = new Value($arr, Value::$xmlrpcArray); + } + break; + case 'object': + if (is_a($phpVal, 'PhpXmlRpc\Value')) { + $xmlrpcVal = $phpVal; + } elseif (is_a($phpVal, 'DateTimeInterface')) { + $xmlrpcVal = new Value($phpVal->format('Ymd\TH:i:s'), Value::$xmlrpcDateTime); + } elseif (in_array('extension_api', $options) && $phpVal instanceof \stdClass && isset($phpVal->xmlrpc_type)) { + // Handle the 'pre-converted' base64 and datetime values + if (isset($phpVal->scalar)) { + switch ($phpVal->xmlrpc_type) { + case 'base64': + $xmlrpcVal = new Value($phpVal->scalar, Value::$xmlrpcBase64); + break; + case 'datetime': + $xmlrpcVal = new Value($phpVal->scalar, Value::$xmlrpcDateTime); + break; + default: + $xmlrpcVal = new Value(); + } + } else { + $xmlrpcVal = new Value(); + } + + } else { + $arr = array(); + foreach($phpVal as $k => $v) { + $arr[$k] = $this->encode($v, $options); + } + $xmlrpcVal = new Value($arr, Value::$xmlrpcStruct); + if (in_array('encode_php_objs', $options)) { + // let's save original class name into xmlrpc value: + // might be useful later on... + $xmlrpcVal->_php_class = get_class($phpVal); + } + } + break; + case 'NULL': + if (in_array('extension_api', $options)) { + $xmlrpcVal = new Value('', Value::$xmlrpcString); + } elseif (in_array('null_extension', $options)) { + $xmlrpcVal = new Value('', Value::$xmlrpcNull); + } else { + $xmlrpcVal = new Value(); + } + break; + case 'resource': + if (in_array('extension_api', $options)) { + $xmlrpcVal = new Value((int)$phpVal, Value::$xmlrpcInt); + } else { + $xmlrpcVal = new Value(); + } + break; + // catch "user function", "unknown type" + default: + // giancarlo pinerolo + // it has to return an empty object in case, not a boolean. + $xmlrpcVal = new Value(); + break; + } + + return $xmlrpcVal; + } + + /** + * Convert the xml representation of a method response, method request or single + * xmlrpc value into the appropriate object (a.k.a. deserialize). + * + * @todo is this a good name/class for this method? It does something quite different from 'decode' after all + * (returning objects vs returns plain php values)... In fact it belongs rather to a Parser class + * + * @param string $xmlVal + * @param array $options + * + * @return Value|Request|Response|false false on error, or an instance of either Value, Request or Response + */ + public function decodeXml($xmlVal, $options = array()) + { + // 'guestimate' encoding + $valEncoding = XMLParser::guessEncoding('', $xmlVal); + if ($valEncoding != '') { + + // Since parsing will fail if + // - charset is not specified in the xml prologue, + // - the encoding is not UTF8 and + // - there are non-ascii chars in the text, + // we try to work round that... + // The following code might be better for mb_string enabled installs, but makes the lib about 200% slower... + //if (!is_valid_charset($valEncoding, array('UTF-8')) + if (!in_array($valEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($xmlVal)) { + if ($valEncoding == 'ISO-8859-1') { + $xmlVal = utf8_encode($xmlVal); + } else { + if (extension_loaded('mbstring')) { + $xmlVal = mb_convert_encoding($xmlVal, 'UTF-8', $valEncoding); + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of xml text: ' . $valEncoding); + } + } + } + } + + // What if internal encoding is not in one of the 3 allowed? We use the broadest one, ie. utf8! + if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) { + /// @todo emit a warning + $parserOptions = array(XML_OPTION_TARGET_ENCODING => 'UTF-8'); + } else { + $parserOptions = array(XML_OPTION_TARGET_ENCODING => PhpXmlRpc::$xmlrpc_internalencoding); + } + + $xmlRpcParser = $this->getParser(); + $xmlRpcParser->parse( + $xmlVal, + XMLParser::RETURN_XMLRPCVALS, + XMLParser::ACCEPT_REQUEST | XMLParser::ACCEPT_RESPONSE | XMLParser::ACCEPT_VALUE | XMLParser::ACCEPT_FAULT, + $parserOptions + ); + + if ($xmlRpcParser->_xh['isf'] > 1) { + // test that $xmlrpc->_xh['value'] is an obj, too??? + + $this->getLogger()->errorLog($xmlRpcParser->_xh['isf_reason']); + + return false; + } + + switch ($xmlRpcParser->_xh['rt']) { + case 'methodresponse': + $v = $xmlRpcParser->_xh['value']; + if ($xmlRpcParser->_xh['isf'] == 1) { + /** @var Value $vc */ + $vc = $v['faultCode']; + /** @var Value $vs */ + $vs = $v['faultString']; + $r = new Response(0, $vc->scalarval(), $vs->scalarval()); + } else { + $r = new Response($v); + } + return $r; + + case 'methodcall': + $req = new Request($xmlRpcParser->_xh['method']); + for ($i = 0; $i < count($xmlRpcParser->_xh['params']); $i++) { + $req->addParam($xmlRpcParser->_xh['params'][$i]); + } + return $req; + + case 'value': + return $xmlRpcParser->_xh['value']; + + case 'fault': + // EPI api emulation + $v = $xmlRpcParser->_xh['value']; + // use a known error code + /** @var Value $vc */ + $vc = isset($v['faultCode']) ? $v['faultCode']->scalarval() : PhpXmlRpc::$xmlrpcerr['invalid_return']; + /** @var Value $vs */ + $vs = isset($v['faultString']) ? $v['faultString']->scalarval() : ''; + if (!is_int($vc) || $vc == 0) { + $vc = PhpXmlRpc::$xmlrpcerr['invalid_return']; + } + return new Response(0, $vc, $vs); + default: + return false; + } + } +} diff --git a/lib/phpxmlrpc/Exception/HttpException.php b/lib/phpxmlrpc/Exception/HttpException.php new file mode 100644 index 00000000000..5b9322ef306 --- /dev/null +++ b/lib/phpxmlrpc/Exception/HttpException.php @@ -0,0 +1,19 @@ +statusCode = $statusCode; + } + + public function statusCode() + { + return $this->statusCode; + } +} diff --git a/lib/phpxmlrpc/Exception/PhpXmlrpcException.php b/lib/phpxmlrpc/Exception/PhpXmlrpcException.php new file mode 100644 index 00000000000..e143f4e1867 --- /dev/null +++ b/lib/phpxmlrpc/Exception/PhpXmlrpcException.php @@ -0,0 +1,7 @@ + array(), "out" => array()); + + //protected $xml_cp1252_Entities = array('in' => array(), out' => array()); + + protected $charset_supersets = array( + 'US-ASCII' => array('ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', + 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', + 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-12', + 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'UTF-8', + 'EUC-JP', 'EUC-', 'EUC-KR', 'EUC-CN',), + ); + + /** @var Charset $instance */ + protected static $instance = null; + + /** + * This class is singleton for performance reasons. + * @todo should we just make $xml_iso88591_Entities a static variable instead ? + * + * @return Charset + */ + public static function instance() + { + if (self::$instance === null) { + self::$instance = new static(); + } + + return self::$instance; + } + + /** + * Force usage as singleton + */ + protected function __construct() + { + } + + /** + * @param string $tableName + * @throws \Exception for unsupported $tableName + * @todo add support for cp1252 as well as latin-2 .. latin-10 + * Optimization creep: instead of building all those tables on load, keep them ready-made php files + * which are not even included until needed + * @todo should we add to the latin-1 table the characters from cp_1252 range, i.e. 128 to 159 ? + * Those will NOT be present in true ISO-8859-1, but will save the unwary windows user from sending junk + * (though no luck when receiving them...) + * Note also that, apparently, while 'ISO/IEC 8859-1' has no characters defined for bytes 128 to 159, + * IANA ISO-8859-1 does have well-defined 'C1' control codes for those - wikipedia's page on latin-1 says: + * "ISO-8859-1 is the IANA preferred name for this standard when supplemented with the C0 and C1 control codes from ISO/IEC 6429." + * Check what mbstring/iconv do by default with those? + */ + protected function buildConversionTable($tableName) + { + switch($tableName) { + case 'xml_iso88591_Entities': + if (count($this->xml_iso88591_Entities['in'])) { + return; + } + for ($i = 0; $i < 32; $i++) { + $this->xml_iso88591_Entities["in"][] = chr($i); + $this->xml_iso88591_Entities["out"][] = "&#{$i};"; + } + + /// @todo to be 'print safe', should we encode as well character 127 (DEL) ? + + for ($i = 160; $i < 256; $i++) { + $this->xml_iso88591_Entities["in"][] = chr($i); + $this->xml_iso88591_Entities["out"][] = "&#{$i};"; + } + break; + + /*case 'xml_cp1252_Entities': + if (count($this->xml_cp1252_Entities['in'])) { + return; + } + for ($i = 128; $i < 160; $i++) + { + $this->xml_cp1252_Entities['in'][] = chr($i); + } + $this->xml_cp1252_Entities['out'] = array( + '€', '?', '‚', 'ƒ', + '„', '…', '†', '‡', + 'ˆ', '‰', 'Š', '‹', + 'Œ', '?', 'Ž', '?', + '?', '‘', '’', '“', + '”', '•', '–', '—', + '˜', '™', 'š', '›', + 'œ', '?', 'ž', 'Ÿ' + ); + $this->buildConversionTable('xml_iso88591_Entities'); + break;*/ + + default: + throw new \Exception('Unsupported table: ' . $tableName); + } + } + + /** + * Convert a string to the correct XML representation in a target charset. + * This involves: + * - character transformation for all characters which have a different representation in source and dest charsets + * - using 'charset entity' representation for all characters which are outside of the target charset + * + * To help correct communication of non-ascii chars inside strings, regardless of the charset used when sending + * requests, parsing them, sending responses and parsing responses, an option is to convert all non-ascii chars + * present in the message into their equivalent 'charset entity'. Charset entities enumerated this way are + * independent of the charset encoding used to transmit them, and all XML parsers are bound to understand them. + * + * Note that when not sending a charset encoding mime type along with http headers, we are bound by RFC 3023 to emit + * strict us-ascii for 'text/xml' payloads (but we should review RFC 7303, which seems to have changed the rules...) + * + * @todo do a bit of basic benchmarking (strtr vs. str_replace) + * @todo make usage of iconv() or mb_string() where available + * @todo support aliases for charset names, eg ASCII, LATIN1, ISO-88591 (see f.e. polyfill-iconv for a list), + * but then take those into account as well in other methods, ie.isValidCharset) + * @todo when converting to ASCII, allow to choose whether to escape the range 0-31,127 (non-print chars) or not + * @todo allow picking different strategies to deal w. invalid chars? eg. source in latin-1 and chars 128-159 + * @todo add support for escaping using CDATA sections? (add cdata start and end tokens, replace only ']]>' with ']]]]>') + * + * @param string $data + * @param string $srcEncoding + * @param string $destEncoding + * + * @return string + */ + public function encodeEntities($data, $srcEncoding = '', $destEncoding = '') + { + if ($srcEncoding == '') { + // lame, but we know no better... + $srcEncoding = PhpXmlRpc::$xmlrpc_internalencoding; + } + + if ($destEncoding == '') { + $destEncoding = 'US-ASCII'; + } + + $conversion = strtoupper($srcEncoding . '_' . $destEncoding); + + // list ordered with (expected) most common scenarios first + switch ($conversion) { + case 'UTF-8_UTF-8': + case 'ISO-8859-1_ISO-8859-1': + case 'US-ASCII_UTF-8': + case 'US-ASCII_US-ASCII': + case 'US-ASCII_ISO-8859-1': + //case 'CP1252_CP1252': + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + break; + + case 'UTF-8_US-ASCII': + case 'UTF-8_ISO-8859-1': + // NB: this will choke on invalid UTF-8, going most likely beyond EOF + $escapedData = ''; + // be kind to users creating string xmlrpc values out of different php types + $data = (string)$data; + $ns = strlen($data); + for ($nn = 0; $nn < $ns; $nn++) { + $ch = $data[$nn]; + $ii = ord($ch); + // 7 bits in 1 byte: 0bbbbbbb (127) + if ($ii < 32) { + if ($conversion == 'UTF-8_US-ASCII') { + $escapedData .= sprintf('&#%d;', $ii); + } else { + $escapedData .= $ch; + } + } + else if ($ii < 128) { + /// @todo shall we replace this with a (supposedly) faster str_replace? + /// @todo to be 'print safe', should we encode as well character 127 (DEL) ? + switch ($ii) { + case 34: + $escapedData .= '"'; + break; + case 38: + $escapedData .= '&'; + break; + case 39: + $escapedData .= '''; + break; + case 60: + $escapedData .= '<'; + break; + case 62: + $escapedData .= '>'; + break; + default: + $escapedData .= $ch; + } // switch + } // 11 bits in 2 bytes: 110bbbbb 10bbbbbb (2047) + elseif ($ii >> 5 == 6) { + $b1 = ($ii & 31); + $b2 = (ord($data[$nn + 1]) & 63); + $ii = ($b1 * 64) + $b2; + $escapedData .= sprintf('&#%d;', $ii); + $nn += 1; + } // 16 bits in 3 bytes: 1110bbbb 10bbbbbb 10bbbbbb + elseif ($ii >> 4 == 14) { + $b1 = ($ii & 15); + $b2 = (ord($data[$nn + 1]) & 63); + $b3 = (ord($data[$nn + 2]) & 63); + $ii = ((($b1 * 64) + $b2) * 64) + $b3; + $escapedData .= sprintf('&#%d;', $ii); + $nn += 2; + } // 21 bits in 4 bytes: 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb + elseif ($ii >> 3 == 30) { + $b1 = ($ii & 7); + $b2 = (ord($data[$nn + 1]) & 63); + $b3 = (ord($data[$nn + 2]) & 63); + $b4 = (ord($data[$nn + 3]) & 63); + $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4; + $escapedData .= sprintf('&#%d;', $ii); + $nn += 3; + } + } + + // when converting to latin-1, do not be so eager with using entities for characters 160-255 + if ($conversion == 'UTF-8_ISO-8859-1') { + $this->buildConversionTable('xml_iso88591_Entities'); + $escapedData = str_replace(array_slice($this->xml_iso88591_Entities['out'], 32), array_slice($this->xml_iso88591_Entities['in'], 32), $escapedData); + } + break; + + case 'ISO-8859-1_UTF-8': + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = utf8_encode($escapedData); + break; + + case 'ISO-8859-1_US-ASCII': + $this->buildConversionTable('xml_iso88591_Entities'); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData); + break; + + /* + case 'CP1252_US-ASCII': + $this->buildConversionTable('xml_cp1252_Entities'); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = str_replace($this->xml_iso88591_Entities']['in'], $this->xml_iso88591_Entities['out'], $escapedData); + $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); + break; + case 'CP1252_UTF-8': + $this->buildConversionTable('xml_cp1252_Entities'); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all alone will NOT convert them) + $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); + $escapedData = utf8_encode($escapedData); + break; + case 'CP1252_ISO-8859-1': + $this->buildConversionTable('xml_cp1252_Entities'); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + // we might as well replace all funky chars with a '?' here, but we are kind and leave it to the receiving application layer to decide what to do with these weird entities... + $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); + break; + */ + + default: + $escapedData = ''; + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported..."); + } + + return $escapedData; + } + + /** + * Checks if a given charset encoding is present in a list of encodings or if it is a valid subset of any encoding + * in the list. + * + * @param string $encoding charset to be tested + * @param string|array $validList comma separated list of valid charsets (or array of charsets) + * + * @return bool + */ + public function isValidCharset($encoding, $validList) + { + if (is_string($validList)) { + $validList = explode(',', $validList); + } + if (@in_array(strtoupper($encoding), $validList)) { + return true; + } else { + if (array_key_exists($encoding, $this->charset_supersets)) { + foreach ($validList as $allowed) { + if (in_array($allowed, $this->charset_supersets[$encoding])) { + return true; + } + } + } + + return false; + } + } + + /** + * Used only for backwards compatibility + * @deprecated + * + * @param string $charset + * + * @return array + * + * @throws \Exception for unknown/unsupported charsets + */ + public function getEntities($charset) + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + switch ($charset) + { + case 'iso88591': + return $this->xml_iso88591_Entities; + default: + throw new \Exception('Unsupported charset: ' . $charset); + } + } +} diff --git a/lib/phpxmlrpc/Helper/Date.php b/lib/phpxmlrpc/Helper/Date.php new file mode 100644 index 00000000000..bc60cc4869d --- /dev/null +++ b/lib/phpxmlrpc/Helper/Date.php @@ -0,0 +1,60 @@ + 0) { + $chunkEnd = strpos($buffer, "\r\n", $chunkStart + $chunkSize); + + // just in case we got a broken connection + if ($chunkEnd == false) { + $chunk = substr($buffer, $chunkStart); + // append chunk-data to entity-body + $new .= $chunk; + $length += strlen($chunk); + break; + } + + // read chunk-data and crlf + $chunk = substr($buffer, $chunkStart, $chunkEnd - $chunkStart); + // append chunk-data to entity-body + $new .= $chunk; + // length := length + chunk-size + $length += strlen($chunk); + // read chunk-size and crlf + $chunkStart = $chunkEnd + 2; + + $chunkEnd = strpos($buffer, "\r\n", $chunkStart) + 2; + if ($chunkEnd == false) { + break; //just in case we got a broken connection + } + $temp = substr($buffer, $chunkStart, $chunkEnd - $chunkStart); + $chunkSize = hexdec(trim($temp)); + $chunkStart = $chunkEnd; + } + + return $new; + } + + /** + * Parses HTTP an http response headers and separates them from the body. + * + * @param string $data the http response, headers and body. It will be stripped of headers + * @param bool $headersProcessed when true, we assume that response inflating and dechunking has been already carried out + * + * @return array with keys 'headers', 'cookies', 'raw_data' and 'status_code' + * @throws HttpException + */ + public function parseResponseHeaders(&$data, $headersProcessed = false, $debug=0) + { + $httpResponse = array('raw_data' => $data, 'headers'=> array(), 'cookies' => array(), 'status_code' => null); + + // Support "web-proxy-tunnelling" connections for https through proxies + if (preg_match('/^HTTP\/1\.[0-1] 200 Connection established/', $data)) { + // Look for CR/LF or simple LF as line separator, + // (even though it is not valid http) + $pos = strpos($data, "\r\n\r\n"); + if ($pos || is_int($pos)) { + $bd = $pos + 4; + } else { + $pos = strpos($data, "\n\n"); + if ($pos || is_int($pos)) { + $bd = $pos + 2; + } else { + // No separation between response headers and body: fault? + $bd = 0; + } + } + if ($bd) { + // this filters out all http headers from proxy. + // maybe we could take them into account, too? + $data = substr($data, $bd); + } else { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': HTTPS via proxy error, tunnel connection possibly failed'); + throw new HttpException(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (HTTPS via proxy error, tunnel connection possibly failed)', PhpXmlRpc::$xmlrpcerr['http_error']); + } + } + + // Strip HTTP 1.1 100 Continue header if present + while (preg_match('/^HTTP\/1\.1 1[0-9]{2} /', $data)) { + $pos = strpos($data, 'HTTP', 12); + // server sent a Continue header without any (valid) content following... + // give the client a chance to know it + if (!$pos && !is_int($pos)) { + /// @todo this construct works fine in php 3, 4 and 5 - 8; would it not be enough to have !== false now ? + + break; + } + $data = substr($data, $pos); + } + + // When using Curl to query servers using Digest Auth, we get back a double set of http headers. + // We strip out the 1st... + if ($headersProcessed && preg_match('/^HTTP\/[0-9](?:\.[0-9])? 401 /', $data)) { + if (preg_match('/(\r?\n){2}HTTP\/[0-9](?:\.[0-9])? 200 /', $data)) { + $data = preg_replace('/^HTTP\/[0-9](?:\.[0-9])? 401 .+?(?:\r?\n){2}(HTTP\/[0-9.]+ 200 )/s', '$1', $data, 1); + } + } + + if (preg_match('/^HTTP\/([0-9](?:\.[0-9])?) ([0-9]{3}) /', $data, $matches)) { + $httpResponse['protocol_version'] = $matches[1]; + $httpResponse['status_code'] = $matches[2]; + } + + if ($httpResponse['status_code'] !== '200') { + $errstr = substr($data, 0, strpos($data, "\n") - 1); + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': HTTP error, got response: ' . $errstr); + throw new HttpException(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (' . $errstr . ')', PhpXmlRpc::$xmlrpcerr['http_error'], null, $httpResponse['status_code'] ); + } + + // be tolerant to usage of \n instead of \r\n to separate headers and data + // (even though it is not valid http) + $pos = strpos($data, "\r\n\r\n"); + if ($pos || is_int($pos)) { + $bd = $pos + 4; + } else { + $pos = strpos($data, "\n\n"); + if ($pos || is_int($pos)) { + $bd = $pos + 2; + } else { + // No separation between response headers and body: fault? + // we could take some action here instead of going on... + $bd = 0; + } + } + + // be tolerant to line endings, and extra empty lines + $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos))); + + foreach($ar as $line) { + // take care of multi-line headers and cookies + $arr = explode(':', $line, 2); + if (count($arr) > 1) { + $headerName = strtolower(trim($arr[0])); + /// @todo some other headers (the ones that allow a CSV list of values) + /// do allow many values to be passed using multiple header lines. + /// We should add content to $xmlrpc->_xh['headers'][$headerName] + /// instead of replacing it for those... + /// @todo should we drop support for rfc2965 (set-cookie2) cookies? It has been obsoleted since 2011 + if ($headerName == 'set-cookie' || $headerName == 'set-cookie2') { + if ($headerName == 'set-cookie2') { + // version 2 cookies: + // there could be many cookies on one line, comma separated + $cookies = explode(',', $arr[1]); + } else { + $cookies = array($arr[1]); + } + foreach ($cookies as $cookie) { + // glue together all received cookies, using a comma to separate them + // (same as php does with getallheaders()) + if (isset($httpResponse['headers'][$headerName])) { + $httpResponse['headers'][$headerName] .= ', ' . trim($cookie); + } else { + $httpResponse['headers'][$headerName] = trim($cookie); + } + // parse cookie attributes, in case user wants to correctly honour them + // feature creep: only allow rfc-compliant cookie attributes? + // @todo support for server sending multiple time cookie with same name, but using different PATHs + $cookie = explode(';', $cookie); + foreach ($cookie as $pos => $val) { + $val = explode('=', $val, 2); + $tag = trim($val[0]); + $val = isset($val[1]) ? trim($val[1]) : ''; + /// @todo with version 1 cookies, we should strip leading and trailing " chars + if ($pos == 0) { + $cookiename = $tag; + $httpResponse['cookies'][$tag] = array(); + $httpResponse['cookies'][$cookiename]['value'] = urldecode($val); + } else { + if ($tag != 'value') { + $httpResponse['cookies'][$cookiename][$tag] = $val; + } + } + } + } + } else { + $httpResponse['headers'][$headerName] = trim($arr[1]); + } + } elseif (isset($headerName)) { + /// @todo version1 cookies might span multiple lines, thus breaking the parsing above + $httpResponse['headers'][$headerName] .= ' ' . trim($line); + } + } + + $data = substr($data, $bd); + + if ($debug && count($httpResponse['headers'])) { + $msg = ''; + foreach ($httpResponse['headers'] as $header => $value) { + $msg .= "HEADER: $header: $value\n"; + } + foreach ($httpResponse['cookies'] as $header => $value) { + $msg .= "COOKIE: $header={$value['value']}\n"; + } + Logger::instance()->debugMessage($msg); + } + + // if CURL was used for the call, http headers have been processed, + // and dechunking + reinflating have been carried out + if (!$headersProcessed) { + + // Decode chunked encoding sent by http 1.1 servers + if (isset($httpResponse['headers']['transfer-encoding']) && $httpResponse['headers']['transfer-encoding'] == 'chunked') { + if (!$data = static::decodeChunked($data)) { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to rebuild the chunked data received from server'); + throw new HttpException(PhpXmlRpc::$xmlrpcstr['dechunk_fail'], PhpXmlRpc::$xmlrpcerr['dechunk_fail'], null, $httpResponse['status_code']); + } + } + + // Decode gzip-compressed stuff + // code shamelessly inspired from nusoap library by Dietrich Ayala + if (isset($httpResponse['headers']['content-encoding'])) { + $httpResponse['headers']['content-encoding'] = str_replace('x-', '', $httpResponse['headers']['content-encoding']); + if ($httpResponse['headers']['content-encoding'] == 'deflate' || $httpResponse['headers']['content-encoding'] == 'gzip') { + // if decoding works, use it. else assume data wasn't gzencoded + if (function_exists('gzinflate')) { + if ($httpResponse['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) { + $data = $degzdata; + if ($debug) { + Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); + } + } elseif ($httpResponse['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) { + $data = $degzdata; + if ($debug) { + Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); + } + } else { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server'); + throw new HttpException(PhpXmlRpc::$xmlrpcstr['decompress_fail'], PhpXmlRpc::$xmlrpcerr['decompress_fail'], null, $httpResponse['status_code']); + } + } else { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); + throw new HttpException(PhpXmlRpc::$xmlrpcstr['cannot_decompress'], PhpXmlRpc::$xmlrpcerr['cannot_decompress'], null, $httpResponse['status_code']); + } + } + } + } // end of 'if needed, de-chunk, re-inflate response' + + return $httpResponse; + } +} diff --git a/lib/phpxmlrpc/Helper/Logger.php b/lib/phpxmlrpc/Helper/Logger.php new file mode 100644 index 00000000000..bee7b08f00b --- /dev/null +++ b/lib/phpxmlrpc/Helper/Logger.php @@ -0,0 +1,72 @@ +\n".htmlentities($message, $flags, $encoding)."\n"; + } else { + print "
\n".htmlentities($message, $flags)."\n
"; + } + } else { + print "\n$message\n"; + } + + // let the user see this now in case there's a time out later... + flush(); + } + + /** + * Writes a message to the error log + * @param string $message + */ + public function errorLog($message) + { + error_log($message); + } +} diff --git a/lib/phpxmlrpc/Helper/XMLParser.php b/lib/phpxmlrpc/Helper/XMLParser.php new file mode 100644 index 00000000000..f6e79a9a0fa --- /dev/null +++ b/lib/phpxmlrpc/Helper/XMLParser.php @@ -0,0 +1,720 @@ + '', + 'stack' => array(), + 'valuestack' => array(), + 'isf' => 0, + 'isf_reason' => '', + 'value' => null, + 'method' => false, + 'params' => array(), + 'pt' => array(), + 'rt' => '', + ); + + public $xmlrpc_valid_parents = array( + 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'), + 'BOOLEAN' => array('VALUE'), + 'I4' => array('VALUE'), + 'I8' => array('VALUE'), + 'EX:I8' => array('VALUE'), + 'INT' => array('VALUE'), + 'STRING' => array('VALUE'), + 'DOUBLE' => array('VALUE'), + 'DATETIME.ISO8601' => array('VALUE'), + 'BASE64' => array('VALUE'), + 'MEMBER' => array('STRUCT'), + 'NAME' => array('MEMBER'), + 'DATA' => array('ARRAY'), + 'ARRAY' => array('VALUE'), + 'STRUCT' => array('VALUE'), + 'PARAM' => array('PARAMS'), + 'METHODNAME' => array('METHODCALL'), + 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'), + 'FAULT' => array('METHODRESPONSE'), + 'NIL' => array('VALUE'), // only used when extension activated + 'EX:NIL' => array('VALUE'), // only used when extension activated + ); + + /** @var array $parsing_options */ + protected $parsing_options = array(); + /** @var int $accept self::ACCEPT_REQUEST | self::ACCEPT_RESPONSE by default */ + protected $accept = 3; + /** @var int $maxChunkLength 4 MB by default. Any value below 10MB should be good */ + protected $maxChunkLength = 4194304; + + /** + * @param array $options passed to the xml parser + */ + public function __construct(array $options = array()) + { + $this->parsing_options = $options; + } + + /** + * @param string $data + * @param string $returnType + * @param int $accept a bit-combination of self::ACCEPT_REQUEST, self::ACCEPT_RESPONSE, self::ACCEPT_VALUE + * @param array $options + */ + public function parse($data, $returnType = self::RETURN_XMLRPCVALS, $accept = 3, $options = array()) + { + $this->_xh = array( + 'ac' => '', + 'stack' => array(), + 'valuestack' => array(), + 'isf' => 0, + 'isf_reason' => '', + 'value' => null, + 'method' => false, // so we can check later if we got a methodname or not + 'params' => array(), + 'pt' => array(), + 'rt' => '', + ); + + $len = strlen($data); + + // we test for empty documents here to save on resource allocation and simply the chunked-parsing loop below + if ($len == 0) { + $this->_xh['isf'] = 3; + $this->_xh['isf_reason'] = 'XML error 5: empty document'; + return; + } + + $parser = xml_parser_create(); + + foreach ($this->parsing_options as $key => $val) { + xml_parser_set_option($parser, $key, $val); + } + foreach ($options as $key => $val) { + xml_parser_set_option($parser, $key, $val); + } + // always set this, in case someone tries to disable it via options... + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 1); + + xml_set_object($parser, $this); + + switch($returnType) { + case self::RETURN_PHP: + xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast'); + break; + case self::RETURN_EPIVALS: + xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_epi'); + break; + default: + xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee'); + } + + xml_set_character_data_handler($parser, 'xmlrpc_cd'); + xml_set_default_handler($parser, 'xmlrpc_dh'); + + $this->accept = $accept; + + // @see ticket #70 - we have to parse big xml docks in chunks to avoid errors + for ($offset = 0; $offset < $len; $offset += $this->maxChunkLength) { + $chunk = substr($data, $offset, $this->maxChunkLength); + // error handling: xml not well formed + if (!xml_parse($parser, $chunk, $offset + $this->maxChunkLength >= $len)) { + $errCode = xml_get_error_code($parser); + $errStr = sprintf('XML error %s: %s at line %d, column %d', $errCode, xml_error_string($errCode), + xml_get_current_line_number($parser), xml_get_current_column_number($parser)); + + $this->_xh['isf'] = 3; + $this->_xh['isf_reason'] = $errStr; + break; + } + } + + xml_parser_free($parser); + } + + /** + * xml parser handler function for opening element tags. + * @internal + * @param resource $parser + * @param string $name + * @param $attrs + * @param bool $acceptSingleVals DEPRECATED use the $accept parameter instead + */ + public function xmlrpc_se($parser, $name, $attrs, $acceptSingleVals = false) + { + // if invalid xmlrpc already detected, skip all processing + if ($this->_xh['isf'] < 2) { + + // check for correct element nesting + if (count($this->_xh['stack']) == 0) { + // top level element can only be of 2 types + /// @todo optimization creep: save this check into a bool variable, instead of using count() every time: + /// there is only a single top level element in xml anyway + // BC + if ($acceptSingleVals === false) { + $accept = $this->accept; + } else { + $accept = self::ACCEPT_REQUEST | self::ACCEPT_RESPONSE | self::ACCEPT_VALUE; + } + if (($name == 'METHODCALL' && ($accept & self::ACCEPT_REQUEST)) || + ($name == 'METHODRESPONSE' && ($accept & self::ACCEPT_RESPONSE)) || + ($name == 'VALUE' && ($accept & self::ACCEPT_VALUE)) || + ($name == 'FAULT' && ($accept & self::ACCEPT_FAULT))) { + $this->_xh['rt'] = strtolower($name); + } else { + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = 'missing top level xmlrpc element. Found: ' . $name; + + return; + } + } else { + // not top level element: see if parent is OK + $parent = end($this->_xh['stack']); + if (!array_key_exists($name, $this->xmlrpc_valid_parents) || !in_array($parent, $this->xmlrpc_valid_parents[$name])) { + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = "xmlrpc element $name cannot be child of $parent"; + + return; + } + } + + switch ($name) { + // optimize for speed switch cases: most common cases first + case 'VALUE': + /// @todo we could check for 2 VALUE elements inside a MEMBER or PARAM element + $this->_xh['vt'] = 'value'; // indicator: no value found yet + $this->_xh['ac'] = ''; + $this->_xh['lv'] = 1; + $this->_xh['php_class'] = null; + break; + case 'I8': + case 'EX:I8': + if (PHP_INT_SIZE === 4) { + // INVALID ELEMENT: RAISE ISF so that it is later recognized!!! + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = "Received i8 element but php is compiled in 32 bit mode"; + + return; + } + // fall through voluntarily + case 'I4': + case 'INT': + case 'STRING': + case 'BOOLEAN': + case 'DOUBLE': + case 'DATETIME.ISO8601': + case 'BASE64': + if ($this->_xh['vt'] != 'value') { + // two data elements inside a value: an error occurred! + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = "$name element following a {$this->_xh['vt']} element inside a single value"; + + return; + } + $this->_xh['ac'] = ''; // reset the accumulator + break; + case 'STRUCT': + case 'ARRAY': + if ($this->_xh['vt'] != 'value') { + // two data elements inside a value: an error occurred! + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = "$name element following a {$this->_xh['vt']} element inside a single value"; + + return; + } + // create an empty array to hold child values, and push it onto appropriate stack + $curVal = array(); + $curVal['values'] = array(); + $curVal['type'] = $name; + // check for out-of-band information to rebuild php objs + // and in case it is found, save it + if (@isset($attrs['PHP_CLASS'])) { + $curVal['php_class'] = $attrs['PHP_CLASS']; + } + $this->_xh['valuestack'][] = $curVal; + $this->_xh['vt'] = 'data'; // be prepared for a data element next + break; + case 'DATA': + if ($this->_xh['vt'] != 'data') { + // two data elements inside a value: an error occurred! + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = "found two data elements inside an array element"; + + return; + } + case 'METHODCALL': + case 'METHODRESPONSE': + case 'PARAMS': + // valid elements that add little to processing + break; + case 'METHODNAME': + case 'NAME': + /// @todo we could check for 2 NAME elements inside a MEMBER element + $this->_xh['ac'] = ''; + break; + case 'FAULT': + $this->_xh['isf'] = 1; + break; + case 'MEMBER': + // set member name to null, in case we do not find in the xml later on + $this->_xh['valuestack'][count($this->_xh['valuestack']) - 1]['name'] = ''; + //$this->_xh['ac']=''; + // Drop trough intentionally + case 'PARAM': + // clear value type, so we can check later if no value has been passed for this param/member + $this->_xh['vt'] = null; + break; + case 'NIL': + case 'EX:NIL': + if (PhpXmlRpc::$xmlrpc_null_extension) { + if ($this->_xh['vt'] != 'value') { + // two data elements inside a value: an error occurred! + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = "$name element following a {$this->_xh['vt']} element inside a single value"; + + return; + } + $this->_xh['ac'] = ''; // reset the accumulator + break; + } + // we do not support the extension, so + // drop through intentionally + default: + // INVALID ELEMENT: RAISE ISF so that it is later recognized!!! + $this->_xh['isf'] = 2; + $this->_xh['isf_reason'] = "found not-xmlrpc xml element $name"; + break; + } + + // Save current element name to stack, to validate nesting + $this->_xh['stack'][] = $name; + + /// @todo optimization creep: move this inside the big switch() above + if ($name != 'VALUE') { + $this->_xh['lv'] = 0; + } + } + } + + /** + * xml parser handler function for opening element tags. + * Used in decoding xml chunks that might represent single xmlrpc values as well as requests, responses. + * @deprecated + * @param resource $parser + * @param $name + * @param $attrs + */ + public function xmlrpc_se_any($parser, $name, $attrs) + { + $this->xmlrpc_se($parser, $name, $attrs, true); + } + + /** + * xml parser handler function for close element tags. + * @internal + * @param resource $parser + * @param string $name + * @param int $rebuildXmlrpcvals >1 for rebuilding xmlrpcvals, 0 for rebuilding php values, -1 for xmlrpc-extension compatibility + */ + public function xmlrpc_ee($parser, $name, $rebuildXmlrpcvals = 1) + { + if ($this->_xh['isf'] < 2) { + // push this element name from stack + // NB: if XML validates, correct opening/closing is guaranteed and + // we do not have to check for $name == $currElem. + // we also checked for proper nesting at start of elements... + $currElem = array_pop($this->_xh['stack']); + + switch ($name) { + case 'VALUE': + // This if() detects if no scalar was inside + if ($this->_xh['vt'] == 'value') { + $this->_xh['value'] = $this->_xh['ac']; + $this->_xh['vt'] = Value::$xmlrpcString; + } + + if ($rebuildXmlrpcvals > 0) { + // build the xmlrpc val out of the data received, and substitute it + $temp = new Value($this->_xh['value'], $this->_xh['vt']); + // in case we got info about underlying php class, save it + // in the object we're rebuilding + if (isset($this->_xh['php_class'])) { + $temp->_php_class = $this->_xh['php_class']; + } + $this->_xh['value'] = $temp; + } elseif ($rebuildXmlrpcvals < 0) { + if ($this->_xh['vt'] == Value::$xmlrpcDateTime) { + $this->_xh['value'] = (object)array( + 'xmlrpc_type' => 'datetime', + 'scalar' => $this->_xh['value'], + 'timestamp' => \PhpXmlRpc\Helper\Date::iso8601Decode($this->_xh['value']) + ); + } elseif ($this->_xh['vt'] == Value::$xmlrpcBase64) { + $this->_xh['value'] = (object)array( + 'xmlrpc_type' => 'base64', + 'scalar' => $this->_xh['value'] + ); + } + } else { + /// @todo this should handle php-serialized objects, + /// since std deserializing is done by php_xmlrpc_decode, + /// which we will not be calling... + //if (isset($this->_xh['php_class'])) { + //} + } + + // check if we are inside an array or struct: + // if value just built is inside an array, let's move it into array on the stack + $vscount = count($this->_xh['valuestack']); + if ($vscount && $this->_xh['valuestack'][$vscount - 1]['type'] == 'ARRAY') { + $this->_xh['valuestack'][$vscount - 1]['values'][] = $this->_xh['value']; + } + break; + case 'BOOLEAN': + case 'I4': + case 'I8': + case 'EX:I8': + case 'INT': + case 'STRING': + case 'DOUBLE': + case 'DATETIME.ISO8601': + case 'BASE64': + $this->_xh['vt'] = strtolower($name); + /// @todo: optimization creep - remove the if/elseif cycle below + /// since the case() in which we are already did that + if ($name == 'STRING') { + $this->_xh['value'] = $this->_xh['ac']; + } elseif ($name == 'DATETIME.ISO8601') { + if (!preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $this->_xh['ac'])) { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in DATETIME: ' . $this->_xh['ac']); + } + $this->_xh['vt'] = Value::$xmlrpcDateTime; + $this->_xh['value'] = $this->_xh['ac']; + } elseif ($name == 'BASE64') { + /// @todo check for failure of base64 decoding / catch warnings + $this->_xh['value'] = base64_decode($this->_xh['ac']); + } elseif ($name == 'BOOLEAN') { + // special case here: we translate boolean 1 or 0 into PHP + // constants true or false. + // Strings 'true' and 'false' are accepted, even though the + // spec never mentions them (see eg. Blogger api docs) + // NB: this simple checks helps a lot sanitizing input, ie no + // security problems around here + if ($this->_xh['ac'] == '1' || strcasecmp($this->_xh['ac'], 'true') == 0) { + $this->_xh['value'] = true; + } else { + // log if receiving something strange, even though we set the value to false anyway + if ($this->_xh['ac'] != '0' && strcasecmp($this->_xh['ac'], 'false') != 0) { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in BOOLEAN: ' . $this->_xh['ac']); + } + $this->_xh['value'] = false; + } + } elseif ($name == 'DOUBLE') { + // we have a DOUBLE + // we must check that only 0123456789-. are characters here + // NOTE: regexp could be much stricter than this... + if (!preg_match('/^[+-eE0123456789 \t.]+$/', $this->_xh['ac'])) { + /// @todo: find a better way of throwing an error than this! + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in DOUBLE: ' . $this->_xh['ac']); + $this->_xh['value'] = 'ERROR_NON_NUMERIC_FOUND'; + } else { + // it's ok, add it on + $this->_xh['value'] = (double)$this->_xh['ac']; + } + } else { + // we have an I4/I8/INT + // we must check that only 0123456789- are characters here + if (!preg_match('/^[+-]?[0123456789 \t]+$/', $this->_xh['ac'])) { + /// @todo find a better way of throwing an error than this! + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in INT: ' . $this->_xh['ac']); + $this->_xh['value'] = 'ERROR_NON_NUMERIC_FOUND'; + } else { + // it's ok, add it on + $this->_xh['value'] = (int)$this->_xh['ac']; + } + } + $this->_xh['lv'] = 3; // indicate we've found a value + break; + case 'NAME': + $this->_xh['valuestack'][count($this->_xh['valuestack']) - 1]['name'] = $this->_xh['ac']; + break; + case 'MEMBER': + // add to array in the stack the last element built, + // unless no VALUE was found + if ($this->_xh['vt']) { + $vscount = count($this->_xh['valuestack']); + $this->_xh['valuestack'][$vscount - 1]['values'][$this->_xh['valuestack'][$vscount - 1]['name']] = $this->_xh['value']; + } else { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside STRUCT in received xml'); + } + break; + case 'DATA': + $this->_xh['vt'] = null; // reset this to check for 2 data elements in a row - even if they're empty + break; + case 'STRUCT': + case 'ARRAY': + // fetch out of stack array of values, and promote it to current value + $currVal = array_pop($this->_xh['valuestack']); + $this->_xh['value'] = $currVal['values']; + $this->_xh['vt'] = strtolower($name); + if (isset($currVal['php_class'])) { + $this->_xh['php_class'] = $currVal['php_class']; + } + break; + case 'PARAM': + // add to array of params the current value, + // unless no VALUE was found + if ($this->_xh['vt']) { + $this->_xh['params'][] = $this->_xh['value']; + $this->_xh['pt'][] = $this->_xh['vt']; + } else { + Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside PARAM in received xml'); + } + break; + case 'METHODNAME': + $this->_xh['method'] = preg_replace('/^[\n\r\t ]+/', '', $this->_xh['ac']); + break; + case 'NIL': + case 'EX:NIL': + if (PhpXmlRpc::$xmlrpc_null_extension) { + $this->_xh['vt'] = 'null'; + $this->_xh['value'] = null; + $this->_xh['lv'] = 3; + break; + } + // drop through intentionally if nil extension not enabled + case 'PARAMS': + case 'FAULT': + case 'METHODCALL': + case 'METHORESPONSE': + break; + default: + // End of INVALID ELEMENT! + // shall we add an assert here for unreachable code??? + break; + } + } + } + + /** + * Used in decoding xmlrpc requests/responses without rebuilding xmlrpc Values. + * @internal + * @param resource $parser + * @param string $name + */ + public function xmlrpc_ee_fast($parser, $name) + { + $this->xmlrpc_ee($parser, $name, 0); + } + + /** + * Used in decoding xmlrpc requests/responses while building xmlrpc-extension Values (plain php for all but base64 and datetime). + * @internal + * @param resource $parser + * @param string $name + */ + public function xmlrpc_ee_epi($parser, $name) + { + $this->xmlrpc_ee($parser, $name, -1); + } + + /** + * xml parser handler function for character data. + * @internal + * @param resource $parser + * @param string $data + */ + public function xmlrpc_cd($parser, $data) + { + // skip processing if xml fault already detected + if ($this->_xh['isf'] < 2) { + // "lookforvalue==3" means that we've found an entire value + // and should discard any further character data + if ($this->_xh['lv'] != 3) { + $this->_xh['ac'] .= $data; + } + } + } + + /** + * xml parser handler function for 'other stuff', ie. not char data or + * element start/end tag. In fact it only gets called on unknown entities... + * @internal + * @param $parser + * @param string data + */ + public function xmlrpc_dh($parser, $data) + { + // skip processing if xml fault already detected + if ($this->_xh['isf'] < 2) { + if (substr($data, 0, 1) == '&' && substr($data, -1, 1) == ';') { + $this->_xh['ac'] .= $data; + } + } + + //return true; + } + + /** + * xml charset encoding guessing helper function. + * Tries to determine the charset encoding of an XML chunk received over HTTP. + * NB: according to the spec (RFC 3023), if text/xml content-type is received over HTTP without a content-type, + * we SHOULD assume it is strictly US-ASCII. But we try to be more tolerant of non conforming (legacy?) clients/servers, + * which will be most probably using UTF-8 anyway... + * In order of importance checks: + * 1. http headers + * 2. BOM + * 3. XML declaration + * 4. guesses using mb_detect_encoding() + * + * @param string $httpHeader the http Content-type header + * @param string $xmlChunk xml content buffer + * @param string $encodingPrefs comma separated list of character encodings to be used as default (when mb extension is enabled). + * This can also be set globally using PhpXmlRpc::$xmlrpc_detectencodings + * @return string the encoding determined. Null if it can't be determined and mbstring is enabled, + * PhpXmlRpc::$xmlrpc_defencoding if it can't be determined and mbstring is not enabled + * + * @todo explore usage of mb_http_input(): does it detect http headers + post data? if so, use it instead of hand-detection!!! + */ + public static function guessEncoding($httpHeader = '', $xmlChunk = '', $encodingPrefs = null) + { + // discussion: see http://www.yale.edu/pclt/encoding/ + // 1 - test if encoding is specified in HTTP HEADERS + + // Details: + // LWS: (\13\10)?( |\t)+ + // token: (any char but excluded stuff)+ + // quoted string: " (any char but double quotes and control chars)* " + // header: Content-type = ...; charset=value(; ...)* + // where value is of type token, no LWS allowed between 'charset' and value + // Note: we do not check for invalid chars in VALUE: + // this had better be done using pure ereg as below + // Note 2: we might be removing whitespace/tabs that ought to be left in if + // the received charset is a quoted string. But nobody uses such charset names... + + /// @todo this test will pass if ANY header has charset specification, not only Content-Type. Fix it? + $matches = array(); + if (preg_match('/;\s*charset\s*=([^;]+)/i', $httpHeader, $matches)) { + return strtoupper(trim($matches[1], " \t\"")); + } + + // 2 - scan the first bytes of the data for a UTF-16 (or other) BOM pattern + // (source: http://www.w3.org/TR/2000/REC-xml-20001006) + // NOTE: actually, according to the spec, even if we find the BOM and determine + // an encoding, we should check if there is an encoding specified + // in the xml declaration, and verify if they match. + /// @todo implement check as described above? + /// @todo implement check for first bytes of string even without a BOM? (It sure looks harder than for cases WITH a BOM) + if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk)) { + return 'UCS-4'; + } elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk)) { + return 'UTF-16'; + } elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk)) { + return 'UTF-8'; + } + + // 3 - test if encoding is specified in the xml declaration + // Details: + // SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ + // EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* + if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" . + '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", + $xmlChunk, $matches)) { + return strtoupper(substr($matches[2], 1, -1)); + } + + // 4 - if mbstring is available, let it do the guesswork + if (extension_loaded('mbstring')) { + if ($encodingPrefs == null && PhpXmlRpc::$xmlrpc_detectencodings != null) { + $encodingPrefs = PhpXmlRpc::$xmlrpc_detectencodings; + } + if ($encodingPrefs) { + $enc = mb_detect_encoding($xmlChunk, $encodingPrefs); + } else { + $enc = mb_detect_encoding($xmlChunk); + } + // NB: mb_detect likes to call it ascii, xml parser likes to call it US_ASCII... + // IANA also likes better US-ASCII, so go with it + if ($enc == 'ASCII') { + $enc = 'US-' . $enc; + } + + return $enc; + } else { + // no encoding specified: as per HTTP1.1 assume it is iso-8859-1? + // Both RFC 2616 (HTTP 1.1) and 1945 (HTTP 1.0) clearly state that for text/xxx content types + // this should be the standard. And we should be getting text/xml as request and response. + // BUT we have to be backward compatible with the lib, which always used UTF-8 as default... + return PhpXmlRpc::$xmlrpc_defencoding; + } + } + + /** + * Helper function: checks if an xml chunk as a charset declaration (BOM or in the xml declaration) + * + * @param string $xmlChunk + * @return bool + */ + public static function hasEncoding($xmlChunk) + { + // scan the first bytes of the data for a UTF-16 (or other) BOM pattern + // (source: http://www.w3.org/TR/2000/REC-xml-20001006) + if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk)) { + return true; + } elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk)) { + return true; + } elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk)) { + return true; + } + + // test if encoding is specified in the xml declaration + // Details: + // SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ + // EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* + if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" . + '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", + $xmlChunk, $matches)) { + return true; + } + + return false; + } +} diff --git a/lib/phpxmlrpc/PhpXmlRpc.php b/lib/phpxmlrpc/PhpXmlRpc.php new file mode 100644 index 00000000000..76ea4cd2742 --- /dev/null +++ b/lib/phpxmlrpc/PhpXmlRpc.php @@ -0,0 +1,153 @@ + 1, + 'invalid_return' => 2, + 'incorrect_params' => 3, + 'introspect_unknown' => 4, + 'http_error' => 5, + 'no_data' => 6, + 'no_ssl' => 7, + 'curl_fail' => 8, + 'invalid_request' => 15, + 'no_curl' => 16, + 'server_error' => 17, + 'multicall_error' => 18, + 'multicall_notstruct' => 9, + 'multicall_nomethod' => 10, + 'multicall_notstring' => 11, + 'multicall_recursion' => 12, + 'multicall_noparams' => 13, + 'multicall_notarray' => 14, + 'no_http2' => 15, + + 'cannot_decompress' => 103, + 'decompress_fail' => 104, + 'dechunk_fail' => 105, + 'server_cannot_decompress' => 106, + 'server_decompress_fail' => 107, + ); + + static public $xmlrpcstr = array( + 'unknown_method' => 'Unknown method', + 'invalid_return' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)', + 'incorrect_params' => 'Incorrect parameters passed to method', + 'introspect_unknown' => "Can't introspect: method unknown", + 'http_error' => "Didn't receive 200 OK from remote server", + 'no_data' => 'No data received from server', + 'no_ssl' => 'No SSL support compiled in', + 'curl_fail' => 'CURL error', + 'invalid_request' => 'Invalid request payload', + 'no_curl' => 'No CURL support compiled in', + 'server_error' => 'Internal server error', + 'multicall_error' => 'Received from server invalid multicall response', + 'multicall_notstruct' => 'system.multicall expected struct', + 'multicall_nomethod' => 'Missing methodName', + 'multicall_notstring' => 'methodName is not a string', + 'multicall_recursion' => 'Recursive system.multicall forbidden', + 'multicall_noparams' => 'Missing params', + 'multicall_notarray' => 'params is not an array', + 'no_http2' => 'No HTTP/2 support compiled in', + + 'cannot_decompress' => 'Received from server compressed HTTP and cannot decompress', + 'decompress_fail' => 'Received from server invalid compressed HTTP', + 'dechunk_fail' => 'Received from server invalid chunked HTTP', + 'server_cannot_decompress' => 'Received from client compressed HTTP request and cannot decompress', + 'server_decompress_fail' => 'Received from client invalid compressed HTTP request', + ); + + // The charset encoding used by the server for received requests and by the client for received responses when + // received charset cannot be determined and mbstring extension is not enabled + public static $xmlrpc_defencoding = "UTF-8"; + + // The list of encodings used by the server for requests and by the client for responses to detect the charset of + // the received payload when + // - the charset cannot be determined by looking at http headers, xml declaration or BOM + // - mbstring extension is enabled + public static $xmlrpc_detectencodings = array(); + + // The encoding used internally by PHP. + // String values received as xml will be converted to this, and php strings will be converted to xml as if + // having been coded with this. + // Valid also when defining names of xmlrpc methods + public static $xmlrpc_internalencoding = "UTF-8"; + + public static $xmlrpcName = "XML-RPC for PHP"; + public static $xmlrpcVersion = "4.8.0"; + + // let user errors start at 800 + public static $xmlrpcerruser = 800; + // let XML parse errors start at 100 + public static $xmlrpcerrxml = 100; + + // set to TRUE to enable correct decoding of and values + public static $xmlrpc_null_extension = false; + + // set to TRUE to enable encoding of php NULL values to instead of + public static $xmlrpc_null_apache_encoding = false; + + public static $xmlrpc_null_apache_encoding_ns = "http://ws.apache.org/xmlrpc/namespaces/extensions"; + + // number of decimal digits used to serialize Double values + public static $xmlpc_double_precision = 128; + + /** + * A function to be used for compatibility with legacy code: it creates all global variables which used to be declared, + * such as library version etc... + */ + public static function exportGlobals() + { + $reflection = new \ReflectionClass('PhpXmlRpc\PhpXmlRpc'); + foreach ($reflection->getStaticProperties() as $name => $value) { + $GLOBALS[$name] = $value; + } + + // NB: all the variables exported into the global namespace below here do NOT guarantee 100% compatibility, + // as they are NOT reimported back during calls to importGlobals() + + $reflection = new \ReflectionClass('PhpXmlRpc\Value'); + foreach ($reflection->getStaticProperties() as $name => $value) { + $GLOBALS[$name] = $value; + } + + $parser = new Helper\XMLParser(); + $reflection = new \ReflectionClass('PhpXmlRpc\Helper\XMLParser'); + foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $name => $value) { + if (in_array($value->getName(), array('xmlrpc_valid_parents'))) + { + $GLOBALS[$value->getName()] = $value->getValue($parser); + } + } + + $charset = Helper\Charset::instance(); + $GLOBALS['xml_iso88591_Entities'] = $charset->getEntities('iso88591'); + } + + /** + * A function to be used for compatibility with legacy code: it gets the values of all global variables which used + * to be declared, such as library version etc... and sets them to php classes. + * It should be used by code which changed the values of those global variables to alter the working of the library. + * Example code: + * 1. include xmlrpc.inc + * 2. set the values, e.g. $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8'; + * 3. import them: PhpXmlRpc\PhpXmlRpc::importGlobals(); + * 4. run your own code. + */ + public static function importGlobals() + { + $reflection = new \ReflectionClass('PhpXmlRpc\PhpXmlRpc'); + $staticProperties = $reflection->getStaticProperties(); + foreach ($staticProperties as $name => $value) { + if (isset($GLOBALS[$name])) { + self::$$name = $GLOBALS[$name]; + } + } + } +} diff --git a/lib/phpxmlrpc/Request.php b/lib/phpxmlrpc/Request.php new file mode 100644 index 00000000000..9841c015031 --- /dev/null +++ b/lib/phpxmlrpc/Request.php @@ -0,0 +1,422 @@ +methodname = $methodName; + foreach ($params as $param) { + $this->addParam($param); + } + } + + /** + * @internal this function will become protected in the future + * @param string $charsetEncoding + * @return string + */ + public function xml_header($charsetEncoding = '') + { + if ($charsetEncoding != '') { + return "\n\n"; + } else { + return "\n\n"; + } + } + + /** + * @internal this function will become protected in the future + * @return string + */ + public function xml_footer() + { + return ''; + } + + /** + * @internal this function will become protected in the future + * @param string $charsetEncoding + */ + public function createPayload($charsetEncoding = '') + { + if ($charsetEncoding != '') { + $this->content_type = 'text/xml; charset=' . $charsetEncoding; + } else { + $this->content_type = 'text/xml'; + } + $this->payload = $this->xml_header($charsetEncoding); + $this->payload .= '' . $this->getCharsetEncoder()->encodeEntities( + $this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "\n"; + $this->payload .= "\n"; + foreach ($this->params as $p) { + $this->payload .= "\n" . $p->serialize($charsetEncoding) . + "\n"; + } + $this->payload .= "\n"; + $this->payload .= $this->xml_footer(); + } + + /** + * Gets/sets the xmlrpc method to be invoked. + * + * @param string $methodName the method to be set (leave empty not to set it) + * + * @return string the method that will be invoked + */ + public function method($methodName = '') + { + if ($methodName != '') { + $this->methodname = $methodName; + } + + return $this->methodname; + } + + /** + * Returns xml representation of the message. XML prologue included. + * + * @param string $charsetEncoding + * + * @return string the xml representation of the message, xml prologue included + */ + public function serialize($charsetEncoding = '') + { + $this->createPayload($charsetEncoding); + + return $this->payload; + } + + /** + * Add a parameter to the list of parameters to be used upon method invocation. + * + * Checks that $params is actually a Value object and not a plain php value. + * + * @param Value $param + * + * @return boolean false on failure + */ + public function addParam($param) + { + // check: do not add to self params which are not xmlrpc values + if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) { + $this->params[] = $param; + + return true; + } else { + return false; + } + } + + /** + * Returns the nth parameter in the request. The index zero-based. + * + * @param integer $i the index of the parameter to fetch (zero based) + * + * @return Value the i-th parameter + */ + public function getParam($i) + { + return $this->params[$i]; + } + + /** + * Returns the number of parameters in the message. + * + * @return integer the number of parameters currently set + */ + public function getNumParams() + { + return count($this->params); + } + + /** + * Given an open file handle, read all data available and parse it as an xmlrpc response. + * + * NB: the file handle is not closed by this function. + * NNB: might have trouble in rare cases to work on network streams, as we check for a read of 0 bytes instead of + * feof($fp). But since checking for feof(null) returns false, we would risk an infinite loop in that case, + * because we cannot trust the caller to give us a valid pointer to an open file... + * + * @param resource $fp stream pointer + * @param bool $headersProcessed + * @param string $returnType + * + * @return Response + */ + public function parseResponseFile($fp, $headersProcessed = false, $returnType = 'xmlrpcvals') + { + $ipd = ''; + while ($data = fread($fp, 32768)) { + $ipd .= $data; + } + return $this->parseResponse($ipd, $headersProcessed, $returnType); + } + + /** + * Parse the xmlrpc response contained in the string $data and return a Response object. + * + * When $this->debug has been set to a value greater than 0, will echo debug messages to screen while decoding. + * + * @param string $data the xmlrpc response, possibly including http headers + * @param bool $headersProcessed when true prevents parsing HTTP headers for interpretation of content-encoding and + * consequent decoding + * @param string $returnType decides return type, i.e. content of response->value(). Either 'xmlrpcvals', 'xml' or + * 'phpvals' + * + * @return Response + * + * @todo parsing Responses is not really the responsibility of the Request class. Maybe of the Client... + */ + public function parseResponse($data = '', $headersProcessed = false, $returnType = XMLParser::RETURN_XMLRPCVALS) + { + if ($this->debug) { + $this->getLogger()->debugMessage("---GOT---\n$data\n---END---"); + } + + $this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array()); + + if ($data == '') { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': no response received from server.'); + return new Response(0, PhpXmlRpc::$xmlrpcerr['no_data'], PhpXmlRpc::$xmlrpcstr['no_data']); + } + + // parse the HTTP headers of the response, if present, and separate them from data + if (substr($data, 0, 4) == 'HTTP') { + $httpParser = new Http(); + try { + $this->httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug); + } catch (HttpException $e) { + // failed processing of HTTP response headers + // save into response obj the full payload received, for debugging + return new Response(0, $e->getCode(), $e->getMessage(), '', array('raw_data' => $data, 'status_code', $e->statusCode())); + } catch(\Exception $e) { + return new Response(0, $e->getCode(), $e->getMessage(), '', array('raw_data' => $data)); + } + } + + // be tolerant of extra whitespace in response body + $data = trim($data); + + /// @todo return an error msg if $data == '' ? + + // be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts) + // idea from Luca Mariano originally in PEARified version of the lib + $pos = strrpos($data, ''); + if ($pos !== false) { + $data = substr($data, 0, $pos + 17); + } + + // try to 'guestimate' the character encoding of the received response + $respEncoding = XMLParser::guessEncoding(@$this->httpResponse['headers']['content-type'], $data); + + if ($this->debug) { + $start = strpos($data, '', $start); + $comments = substr($data, $start, $end - $start); + $this->getLogger()->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" . + str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", $respEncoding); + } + } + + // if user wants back raw xml, give it to her + if ($returnType == 'xml') { + return new Response($data, 0, '', 'xml', $this->httpResponse); + } + + if ($respEncoding != '') { + + // Since parsing will fail if charset is not specified in the xml prologue, + // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... + // The following code might be better for mb_string enabled installs, but makes the lib about 200% slower... + //if (!is_valid_charset($respEncoding, array('UTF-8'))) + if (!in_array($respEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) { + if ($respEncoding == 'ISO-8859-1') { + $data = utf8_encode($data); + } else { + + if (extension_loaded('mbstring')) { + $data = mb_convert_encoding($data, 'UTF-8', $respEncoding); + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received response: ' . $respEncoding); + } + } + } + } + + // PHP internally might use ISO-8859-1, so we have to tell the xml parser to give us back data in the expected charset. + // What if internal encoding is not in one of the 3 allowed? We use the broadest one, ie. utf8 + // This allows to send data which is native in various charset, by extending xmlrpc_encode_entities() and + // setting xmlrpc_internalencoding + if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) { + /// @todo emit a warning + $options = array(XML_OPTION_TARGET_ENCODING => 'UTF-8'); + } else { + $options = array(XML_OPTION_TARGET_ENCODING => PhpXmlRpc::$xmlrpc_internalencoding); + } + + $xmlRpcParser = $this->getParser(); + $xmlRpcParser->parse($data, $returnType, XMLParser::ACCEPT_RESPONSE, $options); + + // first error check: xml not well formed + if ($xmlRpcParser->_xh['isf'] > 2) { + + // BC break: in the past for some cases we used the error message: 'XML error at line 1, check URL' + + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], + PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', + $this->httpResponse + ); + + if ($this->debug) { + print $xmlRpcParser->_xh['isf_reason']; + } + } + // second error check: xml well formed but not xml-rpc compliant + elseif ($xmlRpcParser->_xh['isf'] == 2) { + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], + PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', + $this->httpResponse + ); + + if ($this->debug) { + /// @todo echo something for user? + } + } + // third error check: parsing of the response has somehow gone boink. + /// @todo shall we omit this check, since we trust the parsing code? + elseif ($returnType == XMLParser::RETURN_XMLRPCVALS && !is_object($xmlRpcParser->_xh['value'])) { + // something odd has happened + // and it's time to generate a client side error + // indicating something odd went on + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], PhpXmlRpc::$xmlrpcstr['invalid_return'], + '', $this->httpResponse + ); + } else { + if ($this->debug > 1) { + $this->getLogger()->debugMessage( + "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---" + ); + } + + $v = $xmlRpcParser->_xh['value']; + + if ($xmlRpcParser->_xh['isf']) { + /// @todo we should test here if server sent an int and a string, and/or coerce them into such... + if ($returnType == XMLParser::RETURN_XMLRPCVALS) { + $errNo_v = $v['faultCode']; + $errStr_v = $v['faultString']; + $errNo = $errNo_v->scalarval(); + $errStr = $errStr_v->scalarval(); + } else { + $errNo = $v['faultCode']; + $errStr = $v['faultString']; + } + + if ($errNo == 0) { + // FAULT returned, errno needs to reflect that + $errNo = -1; + } + + $r = new Response(0, $errNo, $errStr, '', $this->httpResponse); + } else { + $r = new Response($v, 0, '', $returnType, $this->httpResponse); + } + } + + return $r; + } + + /** + * Kept the old name even if Request class was renamed, for compatibility. + * + * @return string + */ + public function kindOf() + { + return 'msg'; + } + + /** + * Enables/disables the echoing to screen of the xmlrpc responses received. + * + * @param integer $level values 0, 1, 2 are supported + */ + public function setDebug($level) + { + $this->debug = $level; + } +} diff --git a/lib/phpxmlrpc/Response.php b/lib/phpxmlrpc/Response.php new file mode 100644 index 00000000000..f814d7bcb59 --- /dev/null +++ b/lib/phpxmlrpc/Response.php @@ -0,0 +1,263 @@ + array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null); + + public function getCharsetEncoder() + { + if (self::$charsetEncoder === null) { + self::$charsetEncoder = Charset::instance(); + } + return self::$charsetEncoder; + } + + public function setCharsetEncoder($charsetEncoder) + { + self::$charsetEncoder = $charsetEncoder; + } + + /** + * @param Value|string|mixed $val either a Value object, a php value or the xml serialization of an xmlrpc value (a string) + * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded + * @param string $fString the error string, in case of an error response + * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let + * the code guess the correct type. + * @param array|null $httpResponse + * + * @todo add check that $val / $fCode / $fString is of correct type??? + * NB: as of now we do not do it, since it might be either an xmlrpc value or a plain php val, or a complete + * xml chunk, depending on usage of Client::send() inside which creator is called... + */ + public function __construct($val, $fCode = 0, $fString = '', $valType = '', $httpResponse = null) + { + if ($fCode != 0) { + // error response + $this->errno = $fCode; + $this->errstr = $fString; + } else { + // successful response + $this->val = $val; + if ($valType == '') { + // user did not declare type of response value: try to guess it + if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) { + $this->valtyp = 'xmlrpcvals'; + } elseif (is_string($this->val)) { + $this->valtyp = 'xml'; + } else { + $this->valtyp = 'phpvals'; + } + } else { + // user declares type of resp value: believe him + $this->valtyp = $valType; + } + } + + if (is_array($httpResponse)) { + $this->httpResponse = array_merge(array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null), $httpResponse); + } + } + + /** + * Returns the error code of the response. + * + * @return integer the error code of this response (0 for not-error responses) + */ + public function faultCode() + { + return $this->errno; + } + + /** + * Returns the error code of the response. + * + * @return string the error string of this response ('' for not-error responses) + */ + public function faultString() + { + return $this->errstr; + } + + /** + * Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this + * method should not be used (it may not even be an object). + * + * @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value + * depending on the convention adopted when creating the Response + */ + public function value() + { + return $this->val; + } + + /** + * Returns an array with the cookies received from the server. + * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...) + * with attributes being e.g. 'expires', 'path', domain'. + * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array. + * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back + * with the next request to the server (using Client::setCookie) or not. + * + * @return array[] array of cookies received from the server + */ + public function cookies() + { + return $this->httpResponse['cookies']; + } + + /** + * @return array array with keys 'headers', 'cookies', 'raw_data' and 'status_code' + */ + public function httpResponse() + { + return $this->httpResponse; + } + + /** + * Returns xml representation of the response. XML prologue not included. + * + * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed + * + * @return string the xml representation of the response + * + * @throws \Exception + */ + public function serialize($charsetEncoding = '') + { + if ($charsetEncoding != '') { + $this->content_type = 'text/xml; charset=' . $charsetEncoding; + } else { + $this->content_type = 'text/xml'; + } + if (PhpXmlRpc::$xmlrpc_null_apache_encoding) { + $result = "\n"; + } else { + $result = "\n"; + } + if ($this->errno) { + // Let non-ASCII response messages be tolerated by clients by xml-encoding non ascii chars + $result .= "\n" . + "\nfaultCode\n" . $this->errno . + "\n\n\nfaultString\n" . + Charset::instance()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "\n\n" . + "\n\n"; + } else { + if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) { + if (is_string($this->val) && $this->valtyp == 'xml') { + $result .= "\n\n" . + $this->val . + "\n"; + } else { + /// @todo try to build something serializable using the Encoder... + throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values'); + } + } else { + $result .= "\n\n" . + $this->val->serialize($charsetEncoding) . + "\n"; + } + } + $result .= "\n"; + $this->payload = $result; + + return $result; + } + + // BC layer + + public function __get($name) + { + //trigger_error('getting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED); + + switch($name) { + case 'hdrs': + return $this->httpResponse['headers']; + case '_cookies': + return $this->httpResponse['cookies']; + case 'raw_data': + return $this->httpResponse['raw_data']; + default: + $trace = debug_backtrace(); + trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); + return null; + } + } + + public function __set($name, $value) + { + //trigger_error('setting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED); + + switch($name) { + case 'hdrs': + $this->httpResponse['headers'] = $value; + break; + case '_cookies': + $this->httpResponse['cookies'] = $value; + break; + case 'raw_data': + $this->httpResponse['raw_data'] = $value; + break; + default: + $trace = debug_backtrace(); + trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); + } + } + + public function __isset($name) + { + switch($name) { + case 'hdrs': + return isset($this->httpResponse['headers']); + case '_cookies': + return isset($this->httpResponse['cookies']); + case 'raw_data': + return isset($this->httpResponse['raw_data']); + default: + return false; + } + } + + public function __unset($name) + { + switch($name) { + case 'hdrs': + unset($this->httpResponse['headers']); + break; + case '_cookies': + unset($this->httpResponse['cookies']); + break; + case 'raw_data': + unset($this->httpResponse['raw_data']); + break; + default: + $trace = debug_backtrace(); + trigger_error('Undefined property via __unset(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); + } + } +} diff --git a/lib/phpxmlrpc/Server.php b/lib/phpxmlrpc/Server.php new file mode 100644 index 00000000000..1cc965f3d32 --- /dev/null +++ b/lib/phpxmlrpc/Server.php @@ -0,0 +1,1199 @@ +accepted_compression = array('gzip', 'deflate'); + $this->compress_response = true; + } + + // by default the xml parser can support these 3 charset encodings + $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); + + // dispMap is a dispatch array of methods mapped to function names and signatures. + // If a method doesn't appear in the map then an unknown method error is generated + /* milosch - changed to make passing dispMap optional. + * instead, you can use the class add_to_map() function + * to add functions manually (borrowed from SOAPX4) + */ + if ($dispatchMap) { + $this->dmap = $dispatchMap; + if ($serviceNow) { + $this->service(); + } + } + } + + /** + * Set debug level of server. + * + * @param integer $level debug lvl: determines info added to xmlrpc responses (as xml comments) + * 0 = no debug info, + * 1 = msgs set from user with debugmsg(), + * 2 = add complete xmlrpc request (headers and body), + * 3 = add also all processing warnings happened during method processing + * (NB: this involves setting a custom error handler, and might interfere + * with the standard processing of the php function exposed as method. In + * particular, triggering an USER_ERROR level error will not halt script + * execution anymore, but just end up logged in the xmlrpc response) + * Note that info added at level 2 and 3 will be base64 encoded + */ + public function setDebug($level) + { + $this->debug = $level; + } + + /** + * Add a string to the debug info that can be later serialized by the server as part of the response message. + * Note that for best compatibility, the debug string should be encoded using the PhpXmlRpc::$xmlrpc_internalencoding + * character set. + * + * @param string $msg + */ + public static function xmlrpc_debugmsg($msg) + { + static::$_xmlrpc_debuginfo .= $msg . "\n"; + } + + /** + * Add a string to the debug info that will be later serialized by the server as part of the response message + * (base64 encoded, only when debug level >= 2) + * + * character set. + * @param string $msg + */ + public static function error_occurred($msg) + { + static::$_xmlrpcs_occurred_errors .= $msg . "\n"; + } + + /** + * Return a string with the serialized representation of all debug info. + * + * @param string $charsetEncoding the target charset encoding for the serialization + * + * @return string an XML comment (or two) + */ + public function serializeDebug($charsetEncoding = '') + { + // Tough encoding problem: which internal charset should we assume for debug info? + // It might contain a copy of raw data received from client, ie with unknown encoding, + // intermixed with php generated data and user generated data... + // so we split it: system debug is base 64 encoded, + // user debug info should be encoded by the end user using the INTERNAL_ENCODING + $out = ''; + if ($this->debug_info != '') { + $out .= "\n"; + } + if (static::$_xmlrpc_debuginfo != '') { + $out .= "\n"; + // NB: a better solution MIGHT be to use CDATA, but we need to insert it + // into return payload AFTER the beginning tag + //$out .= "', ']_]_>', static::$_xmlrpc_debuginfo) . "\n]]>\n"; + } + + return $out; + } + + /** + * Execute the xmlrpc request, printing the response. + * + * @param string $data the request body. If null, the http POST request will be examined + * @param bool $returnPayload When true, return the response but do not echo it or any http header + * + * @return Response|string the response object (usually not used by caller...) or its xml serialization + * + * @throws \Exception in case the executed method does throw an exception (and depending on server configuration) + */ + public function service($data = null, $returnPayload = false) + { + if ($data === null) { + $data = file_get_contents('php://input'); + } + $rawData = $data; + + // reset internal debug info + $this->debug_info = ''; + + // Save what we received, before parsing it + if ($this->debug > 1) { + $this->debugmsg("+++GOT+++\n" . $data . "\n+++END+++"); + } + + $r = $this->parseRequestHeaders($data, $reqCharset, $respCharset, $respEncoding); + if (!$r) { + // this actually executes the request + $r = $this->parseRequest($data, $reqCharset); + + // save full body of request into response, for more debugging usages. + // Note that this is the _request_ data, not the response's own data, unlike what happens client-side + /// @todo try to move this injection to the resp. constructor or use a non-deprecated access method + $r->raw_data = $rawData; + } + + if ($this->debug > 2 && static::$_xmlrpcs_occurred_errors) { + $this->debugmsg("+++PROCESSING ERRORS AND WARNINGS+++\n" . + static::$_xmlrpcs_occurred_errors . "+++END+++"); + } + + $payload = $this->xml_header($respCharset); + if ($this->debug > 0) { + $payload = $payload . $this->serializeDebug($respCharset); + } + + // Do not create response serialization if it has already happened. Helps building json magic + if (empty($r->payload)) { + $r->serialize($respCharset); + } + $payload = $payload . $r->payload; + + if ($returnPayload) { + return $payload; + } + + // if we get a warning/error that has output some text before here, then we cannot + // add a new header. We cannot say we are sending xml, either... + if (!headers_sent()) { + header('Content-Type: ' . $r->content_type); + // we do not know if client actually told us an accepted charset, but if he did + // we have to tell him what we did + header("Vary: Accept-Charset"); + + // http compression of output: only + // if we can do it, and we want to do it, and client asked us to, + // and php ini settings do not force it already + /// @todo check separately for gzencode and gzcompress functions, in case of polyfills + $phpNoSelfCompress = !ini_get('zlib.output_compression') && (ini_get('output_handler') != 'ob_gzhandler'); + if ($this->compress_response && function_exists('gzencode') && $respEncoding != '' + && $phpNoSelfCompress + ) { + if (strpos($respEncoding, 'gzip') !== false) { + $payload = gzencode($payload); + header("Content-Encoding: gzip"); + header("Vary: Accept-Encoding"); + } elseif (strpos($respEncoding, 'deflate') !== false) { + $payload = gzcompress($payload); + header("Content-Encoding: deflate"); + header("Vary: Accept-Encoding"); + } + } + + // Do not output content-length header if php is compressing output for us: + // it will mess up measurements. + // Note that Apache/mod_php will add (and even alter!) the Content-Length header on its own, but only for + // responses up to 8000 bytes + if ($phpNoSelfCompress) { + header('Content-Length: ' . (int)strlen($payload)); + } + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': http headers already sent before response is fully generated. Check for php warning or error messages'); + } + + print $payload; + + // return request, in case subclasses want it + return $r; + } + + /** + * Add a method to the dispatch map. + * + * @param string $methodName the name with which the method will be made available + * @param callable $function the php function that will get invoked + * @param array[] $sig the array of valid method signatures. + * Each element is one signature: an array of strings with at least one element + * First element = type of returned value. Elements 2..N = types of parameters 1..N + * @param string $doc method documentation + * @param array[] $sigDoc the array of valid method signatures docs, following the format of $sig but with + * descriptions instead of types (one string for return type, one per param) + * + * @todo raise a warning if the user tries to register a 'system.' method + * @todo allow setting parameters_type + */ + public function add_to_map($methodName, $function, $sig = null, $doc = false, $sigDoc = false) + { + $this->dmap[$methodName] = array( + 'function' => $function, + 'docstring' => $doc, + ); + if ($sig) { + $this->dmap[$methodName]['signature'] = $sig; + } + if ($sigDoc) { + $this->dmap[$methodName]['signature_docs'] = $sigDoc; + } + } + + /** + * Verify type and number of parameters received against a list of known signatures. + * + * @param array|Request $in array of either xmlrpc value objects or xmlrpc type definitions + * @param array $sigs array of known signatures to match against + * + * @return array int, string + */ + protected function verifySignature($in, $sigs) + { + // check each possible signature in turn + if (is_object($in)) { + $numParams = $in->getNumParams(); + } else { + $numParams = count($in); + } + foreach ($sigs as $curSig) { + if (count($curSig) == $numParams + 1) { + $itsOK = 1; + for ($n = 0; $n < $numParams; $n++) { + if (is_object($in)) { + $p = $in->getParam($n); + if ($p->kindOf() == 'scalar') { + $pt = $p->scalartyp(); + } else { + $pt = $p->kindOf(); + } + } else { + $pt = ($in[$n] == 'i4') ? 'int' : strtolower($in[$n]); // dispatch maps never use i4... + } + + // param index is $n+1, as first member of sig is return type + if ($pt != $curSig[$n + 1] && $curSig[$n + 1] != Value::$xmlrpcValue) { + $itsOK = 0; + $pno = $n + 1; + $wanted = $curSig[$n + 1]; + $got = $pt; + break; + } + } + if ($itsOK) { + return array(1, ''); + } + } + } + if (isset($wanted)) { + return array(0, "Wanted ${wanted}, got ${got} at param ${pno}"); + } else { + return array(0, "No method signature matches number of parameters"); + } + } + + /** + * Parse http headers received along with xmlrpc request. If needed, inflate request. + * + * @return Response|null null on success or an error Response + */ + protected function parseRequestHeaders(&$data, &$reqEncoding, &$respEncoding, &$respCompression) + { + // check if $_SERVER is populated: it might have been disabled via ini file + // (this is true even when in CLI mode) + if (count($_SERVER) == 0) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': cannot parse request headers as $_SERVER is not populated'); + } + + if ($this->debug > 1) { + if (function_exists('getallheaders')) { + $this->debugmsg(''); // empty line + foreach (getallheaders() as $name => $val) { + $this->debugmsg("HEADER: $name: $val"); + } + } + } + + if (isset($_SERVER['HTTP_CONTENT_ENCODING'])) { + $contentEncoding = str_replace('x-', '', $_SERVER['HTTP_CONTENT_ENCODING']); + } else { + $contentEncoding = ''; + } + + $rawData = $data; + + // check if request body has been compressed and decompress it + if ($contentEncoding != '' && strlen($data)) { + if ($contentEncoding == 'deflate' || $contentEncoding == 'gzip') { + // if decoding works, use it. else assume data wasn't gzencoded + if (function_exists('gzinflate') && in_array($contentEncoding, $this->accepted_compression)) { + if ($contentEncoding == 'deflate' && $degzdata = @gzuncompress($data)) { + $data = $degzdata; + if ($this->debug > 1) { + $this->debugmsg("\n+++INFLATED REQUEST+++[" . strlen($data) . " chars]+++\n" . $data . "\n+++END+++"); + } + } elseif ($contentEncoding == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) { + $data = $degzdata; + if ($this->debug > 1) { + $this->debugmsg("+++INFLATED REQUEST+++[" . strlen($data) . " chars]+++\n" . $data . "\n+++END+++"); + } + } else { + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['server_decompress_fail'], + PhpXmlRpc::$xmlrpcstr['server_decompress_fail'], '', array('raw_data' => $rawData) + ); + + return $r; + } + } else { + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['server_cannot_decompress'], + PhpXmlRpc::$xmlrpcstr['server_cannot_decompress'], '', array('raw_data' => $rawData) + ); + + return $r; + } + } + } + + // check if client specified accepted charsets, and if we know how to fulfill + // the request + if ($this->response_charset_encoding == 'auto') { + $respEncoding = ''; + if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) { + // here we should check if we can match the client-requested encoding + // with the encodings we know we can generate. + /// @todo we should parse q=0.x preferences instead of getting first charset specified... + $clientAcceptedCharsets = explode(',', strtoupper($_SERVER['HTTP_ACCEPT_CHARSET'])); + // Give preference to internal encoding + $knownCharsets = array(PhpXmlRpc::$xmlrpc_internalencoding, 'UTF-8', 'ISO-8859-1', 'US-ASCII'); + foreach ($knownCharsets as $charset) { + foreach ($clientAcceptedCharsets as $accepted) { + if (strpos($accepted, $charset) === 0) { + $respEncoding = $charset; + break; + } + } + if ($respEncoding) { + break; + } + } + } + } else { + $respEncoding = $this->response_charset_encoding; + } + + if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { + $respCompression = $_SERVER['HTTP_ACCEPT_ENCODING']; + } else { + $respCompression = ''; + } + + // 'guestimate' request encoding + /// @todo check if mbstring is enabled and automagic input conversion is on: it might mingle with this check??? + $reqEncoding = XMLParser::guessEncoding(isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '', + $data); + + return null; + } + + /** + * Parse an xml chunk containing an xmlrpc request and execute the corresponding + * php function registered with the server. + * + * @param string $data the xml request + * @param string $reqEncoding (optional) the charset encoding of the xml request + * + * @return Response + * + * @throws \Exception in case the executed method does throw an exception (and depending on server configuration) + * + * @internal this function will become protected in the future + * @todo either rename this function or move the 'execute' part out of it... + */ + public function parseRequest($data, $reqEncoding = '') + { + // decompose incoming XML into request structure + + if ($reqEncoding != '') { + // Since parsing will fail if + // - charset is not specified in the xml prologue, + // - the encoding is not UTF8 and + // - there are non-ascii chars in the text, + // we try to work round that... + // The following code might be better for mb_string enabled installs, but + // makes the lib about 200% slower... + //if (!is_valid_charset($reqEncoding, array('UTF-8'))) + if (!in_array($reqEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) { + if ($reqEncoding == 'ISO-8859-1') { + $data = utf8_encode($data); + } else { + if (extension_loaded('mbstring')) { + $data = mb_convert_encoding($data, 'UTF-8', $reqEncoding); + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $reqEncoding); + } + } + } + } + + // PHP internally might use ISO-8859-1, so we have to tell the xml parser to give us back data in the expected charset. + // What if internal encoding is not in one of the 3 allowed? We use the broadest one, ie. utf8 + // This allows to send data which is native in various charset, + // by extending xmlrpc_encode_entities() and setting xmlrpc_internalencoding + if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) { + /// @todo emit a warning + $options = array(XML_OPTION_TARGET_ENCODING => 'UTF-8'); + } else { + $options = array(XML_OPTION_TARGET_ENCODING => PhpXmlRpc::$xmlrpc_internalencoding); + } + + $xmlRpcParser = $this->getParser(); + $xmlRpcParser->parse($data, $this->functions_parameters_type, XMLParser::ACCEPT_REQUEST, $options); + if ($xmlRpcParser->_xh['isf'] > 2) { + // (BC) we return XML error as a faultCode + preg_match('/^XML error ([0-9]+)/', $xmlRpcParser->_xh['isf_reason'], $matches); + $r = new Response(0, + PhpXmlRpc::$xmlrpcerrxml + $matches[1], + $xmlRpcParser->_xh['isf_reason']); + } elseif ($xmlRpcParser->_xh['isf']) { + $r = new Response(0, + PhpXmlRpc::$xmlrpcerr['invalid_request'], + PhpXmlRpc::$xmlrpcstr['invalid_request'] . ' ' . $xmlRpcParser->_xh['isf_reason']); + } else { + // small layering violation in favor of speed and memory usage: + // we should allow the 'execute' method handle this, but in the + // most common scenario (xmlrpc values type server with some methods + // registered as phpvals) that would mean a useless encode+decode pass + if ($this->functions_parameters_type != 'xmlrpcvals' || + (isset($this->dmap[$xmlRpcParser->_xh['method']]['parameters_type']) && + ($this->dmap[$xmlRpcParser->_xh['method']]['parameters_type'] != 'xmlrpcvals') + ) + ) { + if ($this->debug > 1) { + $this->debugmsg("\n+++PARSED+++\n" . var_export($xmlRpcParser->_xh['params'], true) . "\n+++END+++"); + } + $r = $this->execute($xmlRpcParser->_xh['method'], $xmlRpcParser->_xh['params'], $xmlRpcParser->_xh['pt']); + } else { + // build a Request object with data parsed from xml + $req = new Request($xmlRpcParser->_xh['method']); + // now add parameters in + for ($i = 0; $i < count($xmlRpcParser->_xh['params']); $i++) { + $req->addParam($xmlRpcParser->_xh['params'][$i]); + } + + if ($this->debug > 1) { + $this->debugmsg("\n+++PARSED+++\n" . var_export($req, true) . "\n+++END+++"); + } + $r = $this->execute($req); + } + } + + return $r; + } + + /** + * Execute a method invoked by the client, checking parameters used. + * + * @param Request|string $req either a Request obj or a method name + * @param mixed[] $params array with method parameters as php types (only if m is method name) + * @param string[] $paramTypes array with xmlrpc types of method parameters (only if m is method name) + * + * @return Response + * + * @throws \Exception in case the executed method does throw an exception (and depending on server configuration) + */ + protected function execute($req, $params = null, $paramTypes = null) + { + static::$_xmlrpcs_occurred_errors = ''; + static::$_xmlrpc_debuginfo = ''; + + if (is_object($req)) { + $methName = $req->method(); + } else { + $methName = $req; + } + $sysCall = $this->isSyscall($methName); + $dmap = $sysCall ? $this->getSystemDispatchMap() : $this->dmap; + + if (!isset($dmap[$methName]['function'])) { + // No such method + return new Response(0, + PhpXmlRpc::$xmlrpcerr['unknown_method'], + PhpXmlRpc::$xmlrpcstr['unknown_method']); + } + + // Check signature + if (isset($dmap[$methName]['signature'])) { + $sig = $dmap[$methName]['signature']; + if (is_object($req)) { + list($ok, $errStr) = $this->verifySignature($req, $sig); + } else { + list($ok, $errStr) = $this->verifySignature($paramTypes, $sig); + } + if (!$ok) { + // Didn't match. + return new Response( + 0, + PhpXmlRpc::$xmlrpcerr['incorrect_params'], + PhpXmlRpc::$xmlrpcstr['incorrect_params'] . ": ${errStr}" + ); + } + } + + $func = $dmap[$methName]['function']; + // let the 'class::function' syntax be accepted in dispatch maps + if (is_string($func) && strpos($func, '::')) { + $func = explode('::', $func); + } + + if (is_array($func)) { + if (is_object($func[0])) { + $funcName = get_class($func[0]) . '->' . $func[1]; + } else { + $funcName = implode('::', $func); + } + } else if ($func instanceof \Closure) { + $funcName = 'Closure'; + } else { + $funcName = $func; + } + + // verify that function to be invoked is in fact callable + if (!is_callable($func)) { + $this->getLogger()->errorLog("XML-RPC: " . __METHOD__ . ": function '$funcName' registered as method handler is not callable"); + return new Response( + 0, + PhpXmlRpc::$xmlrpcerr['server_error'], + PhpXmlRpc::$xmlrpcstr['server_error'] . ": no function matches method" + ); + } + + // If debug level is 3, we should catch all errors generated during + // processing of user function, and log them as part of response + if ($this->debug > 2) { + self::$_xmlrpcs_prev_ehandler = set_error_handler(array('\PhpXmlRpc\Server', '_xmlrpcs_errorHandler')); + } + + try { + // Allow mixed-convention servers + if (is_object($req)) { + if ($sysCall) { + $r = call_user_func($func, $this, $req); + } else { + $r = call_user_func($func, $req); + } + if (!is_a($r, 'PhpXmlRpc\Response')) { + $this->getLogger()->errorLog("XML-RPC: " . __METHOD__ . ": function '$funcName' registered as method handler does not return an xmlrpc response object but a " . gettype($r)); + if (is_a($r, 'PhpXmlRpc\Value')) { + $r = new Response($r); + } else { + $r = new Response( + 0, + PhpXmlRpc::$xmlrpcerr['server_error'], + PhpXmlRpc::$xmlrpcstr['server_error'] . ": function does not return xmlrpc response object" + ); + } + } + } else { + // call a 'plain php' function + if ($sysCall) { + array_unshift($params, $this); + $r = call_user_func_array($func, $params); + } else { + // 3rd API convention for method-handling functions: EPI-style + if ($this->functions_parameters_type == 'epivals') { + $r = call_user_func_array($func, array($methName, $params, $this->user_data)); + // mimic EPI behaviour: if we get an array that looks like an error, make it + // an error response + if (is_array($r) && array_key_exists('faultCode', $r) && array_key_exists('faultString', $r)) { + $r = new Response(0, (integer)$r['faultCode'], (string)$r['faultString']); + } else { + // functions using EPI api should NOT return resp objects, + // so make sure we encode the return type correctly + $encoder = new Encoder(); + $r = new Response($encoder->encode($r, array('extension_api'))); + } + } else { + $r = call_user_func_array($func, $params); + } + } + // the return type can be either a Response object or a plain php value... + if (!is_a($r, '\PhpXmlRpc\Response')) { + // what should we assume here about automatic encoding of datetimes + // and php classes instances??? + $encoder = new Encoder(); + $r = new Response($encoder->encode($r, $this->phpvals_encoding_options)); + } + } + } catch (\Exception $e) { + // (barring errors in the lib) an uncatched exception happened + // in the called function, we wrap it in a proper error-response + switch ($this->exception_handling) { + case 2: + if ($this->debug > 2) { + if (self::$_xmlrpcs_prev_ehandler) { + set_error_handler(self::$_xmlrpcs_prev_ehandler); + } else { + restore_error_handler(); + } + } + throw $e; + case 1: + $r = new Response(0, $e->getCode(), $e->getMessage()); + break; + default: + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['server_error'], PhpXmlRpc::$xmlrpcstr['server_error']); + } + } + if ($this->debug > 2) { + // note: restore the error handler we found before calling the + // user func, even if it has been changed inside the func itself + if (self::$_xmlrpcs_prev_ehandler) { + set_error_handler(self::$_xmlrpcs_prev_ehandler); + } else { + restore_error_handler(); + } + } + + return $r; + } + + /** + * Add a string to the 'internal debug message' (separate from 'user debug message'). + * + * @param string $string + */ + protected function debugmsg($string) + { + $this->debug_info .= $string . "\n"; + } + + /** + * @param string $charsetEncoding + * @return string + */ + protected function xml_header($charsetEncoding = '') + { + if ($charsetEncoding != '') { + return "\n"; + } else { + return "\n"; + } + } + + /** + * @param string $methName + * @return bool + */ + protected function isSyscall($methName) + { + return (strpos($methName, "system.") === 0); + } + + /** + * @return array[] + */ + public function getDispatchMap() + { + return $this->dmap; + } + + /** + * @return array[] + */ + public function getSystemDispatchMap() + { + if (!$this->allow_system_funcs) { + return array(); + } + + return array( + 'system.listMethods' => array( + 'function' => 'PhpXmlRpc\Server::_xmlrpcs_listMethods', + // listMethods: signature was either a string, or nothing. + // The useless string variant has been removed + 'signature' => array(array(Value::$xmlrpcArray)), + 'docstring' => 'This method lists all the methods that the XML-RPC server knows how to dispatch', + 'signature_docs' => array(array('list of method names')), + ), + 'system.methodHelp' => array( + 'function' => 'PhpXmlRpc\Server::_xmlrpcs_methodHelp', + 'signature' => array(array(Value::$xmlrpcString, Value::$xmlrpcString)), + 'docstring' => 'Returns help text if defined for the method passed, otherwise returns an empty string', + 'signature_docs' => array(array('method description', 'name of the method to be described')), + ), + 'system.methodSignature' => array( + 'function' => 'PhpXmlRpc\Server::_xmlrpcs_methodSignature', + 'signature' => array(array(Value::$xmlrpcArray, Value::$xmlrpcString)), + 'docstring' => 'Returns an array of known signatures (an array of arrays) for the method name passed. If no signatures are known, returns a none-array (test for type != array to detect missing signature)', + 'signature_docs' => array(array('list of known signatures, each sig being an array of xmlrpc type names', 'name of method to be described')), + ), + 'system.multicall' => array( + 'function' => 'PhpXmlRpc\Server::_xmlrpcs_multicall', + 'signature' => array(array(Value::$xmlrpcArray, Value::$xmlrpcArray)), + 'docstring' => 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details', + 'signature_docs' => array(array('list of response structs, where each struct has the usual members', 'list of calls, with each call being represented as a struct, with members "methodname" and "params"')), + ), + 'system.getCapabilities' => array( + 'function' => 'PhpXmlRpc\Server::_xmlrpcs_getCapabilities', + 'signature' => array(array(Value::$xmlrpcStruct)), + 'docstring' => 'This method lists all the capabilities that the XML-RPC server has: the (more or less standard) extensions to the xmlrpc spec that it adheres to', + 'signature_docs' => array(array('list of capabilities, described as structs with a version number and url for the spec')), + ), + ); + } + + /* Functions that implement system.XXX methods of xmlrpc servers */ + + /** + * @return array[] + */ + public function getCapabilities() + { + $outAr = array( + // xmlrpc spec: always supported + 'xmlrpc' => array( + 'specUrl' => 'http://www.xmlrpc.com/spec', + 'specVersion' => 1 + ), + // if we support system.xxx functions, we always support multicall, too... + // Note that, as of 2006/09/17, the following URL does not respond anymore + 'system.multicall' => array( + 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208', + 'specVersion' => 1 + ), + // introspection: version 2! we support 'mixed', too + 'introspection' => array( + 'specUrl' => 'http://phpxmlrpc.sourceforge.net/doc-2/ch10.html', + 'specVersion' => 2, + ), + ); + + // NIL extension + if (PhpXmlRpc::$xmlrpc_null_extension) { + $outAr['nil'] = array( + 'specUrl' => 'http://www.ontosys.com/xml-rpc/extensions.php', + 'specVersion' => 1 + ); + } + + return $outAr; + } + + /** + * @param Server $server + * @param Request $req + * @return Response + */ + public static function _xmlrpcs_getCapabilities($server, $req = null) + { + $encoder = new Encoder(); + return new Response($encoder->encode($server->getCapabilities())); + } + + /** + * @param Server $server + * @param Request $req if called in plain php values mode, second param is missing + * @return Response + */ + public static function _xmlrpcs_listMethods($server, $req = null) + { + $outAr = array(); + foreach ($server->dmap as $key => $val) { + $outAr[] = new Value($key, 'string'); + } + foreach ($server->getSystemDispatchMap() as $key => $val) { + $outAr[] = new Value($key, 'string'); + } + + return new Response(new Value($outAr, 'array')); + } + + /** + * @param Server $server + * @param Request $req + * @return Response + */ + public static function _xmlrpcs_methodSignature($server, $req) + { + // let accept as parameter both an xmlrpc value or string + if (is_object($req)) { + $methName = $req->getParam(0); + $methName = $methName->scalarval(); + } else { + $methName = $req; + } + if ($server->isSyscall($methName)) { + $dmap = $server->getSystemDispatchMap(); + } else { + $dmap = $server->dmap; + } + if (isset($dmap[$methName])) { + if (isset($dmap[$methName]['signature'])) { + $sigs = array(); + foreach ($dmap[$methName]['signature'] as $inSig) { + $curSig = array(); + foreach ($inSig as $sig) { + $curSig[] = new Value($sig, 'string'); + } + $sigs[] = new Value($curSig, 'array'); + } + $r = new Response(new Value($sigs, 'array')); + } else { + // NB: according to the official docs, we should be returning a + // "none-array" here, which means not-an-array + $r = new Response(new Value('undef', 'string')); + } + } else { + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['introspect_unknown'], PhpXmlRpc::$xmlrpcstr['introspect_unknown']); + } + + return $r; + } + + /** + * @param Server $server + * @param Request $req + * @return Response + */ + public static function _xmlrpcs_methodHelp($server, $req) + { + // let accept as parameter both an xmlrpc value or string + if (is_object($req)) { + $methName = $req->getParam(0); + $methName = $methName->scalarval(); + } else { + $methName = $req; + } + if ($server->isSyscall($methName)) { + $dmap = $server->getSystemDispatchMap(); + } else { + $dmap = $server->dmap; + } + if (isset($dmap[$methName])) { + if (isset($dmap[$methName]['docstring'])) { + $r = new Response(new Value($dmap[$methName]['docstring'], 'string')); + } else { + $r = new Response(new Value('', 'string')); + } + } else { + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['introspect_unknown'], PhpXmlRpc::$xmlrpcstr['introspect_unknown']); + } + + return $r; + } + + public static function _xmlrpcs_multicall_error($err) + { + if (is_string($err)) { + $str = PhpXmlRpc::$xmlrpcstr["multicall_${err}"]; + $code = PhpXmlRpc::$xmlrpcerr["multicall_${err}"]; + } else { + $code = $err->faultCode(); + $str = $err->faultString(); + } + $struct = array(); + $struct['faultCode'] = new Value($code, 'int'); + $struct['faultString'] = new Value($str, 'string'); + + return new Value($struct, 'struct'); + } + + /** + * @param Server $server + * @param Value $call + * @return Value + */ + public static function _xmlrpcs_multicall_do_call($server, $call) + { + if ($call->kindOf() != 'struct') { + return static::_xmlrpcs_multicall_error('notstruct'); + } + $methName = @$call['methodName']; + if (!$methName) { + return static::_xmlrpcs_multicall_error('nomethod'); + } + if ($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string') { + return static::_xmlrpcs_multicall_error('notstring'); + } + if ($methName->scalarval() == 'system.multicall') { + return static::_xmlrpcs_multicall_error('recursion'); + } + + $params = @$call['params']; + if (!$params) { + return static::_xmlrpcs_multicall_error('noparams'); + } + if ($params->kindOf() != 'array') { + return static::_xmlrpcs_multicall_error('notarray'); + } + + $req = new Request($methName->scalarval()); + foreach($params as $i => $param) { + if (!$req->addParam($param)) { + $i++; // for error message, we count params from 1 + return static::_xmlrpcs_multicall_error(new Response(0, + PhpXmlRpc::$xmlrpcerr['incorrect_params'], + PhpXmlRpc::$xmlrpcstr['incorrect_params'] . ": probable xml error in param " . $i)); + } + } + + $result = $server->execute($req); + + if ($result->faultCode() != 0) { + return static::_xmlrpcs_multicall_error($result); // Method returned fault. + } + + return new Value(array($result->value()), 'array'); + } + + /** + * @param Server $server + * @param Value $call + * @return Value + */ + public static function _xmlrpcs_multicall_do_call_phpvals($server, $call) + { + if (!is_array($call)) { + return static::_xmlrpcs_multicall_error('notstruct'); + } + if (!array_key_exists('methodName', $call)) { + return static::_xmlrpcs_multicall_error('nomethod'); + } + if (!is_string($call['methodName'])) { + return static::_xmlrpcs_multicall_error('notstring'); + } + if ($call['methodName'] == 'system.multicall') { + return static::_xmlrpcs_multicall_error('recursion'); + } + if (!array_key_exists('params', $call)) { + return static::_xmlrpcs_multicall_error('noparams'); + } + if (!is_array($call['params'])) { + return static::_xmlrpcs_multicall_error('notarray'); + } + + // this is a simplistic hack, since we might have received + // base64 or datetime values, but they will be listed as strings here... + $pt = array(); + $wrapper = new Wrapper(); + foreach ($call['params'] as $val) { + // support EPI-encoded base64 and datetime values + if ($val instanceof \stdClass && isset($val->xmlrpc_type)) { + $pt[] = $val->xmlrpc_type == 'datetime' ? Value::$xmlrpcDateTime : $val->xmlrpc_type; + } else { + $pt[] = $wrapper->php2XmlrpcType(gettype($val)); + } + } + + $result = $server->execute($call['methodName'], $call['params'], $pt); + + if ($result->faultCode() != 0) { + return static::_xmlrpcs_multicall_error($result); // Method returned fault. + } + + return new Value(array($result->value()), 'array'); + } + + /** + * @param Server $server + * @param Request|array $req + * @return Response + */ + public static function _xmlrpcs_multicall($server, $req) + { + $result = array(); + // let accept a plain list of php parameters, beside a single xmlrpc msg object + if (is_object($req)) { + $calls = $req->getParam(0); + foreach($calls as $call) { + $result[] = static::_xmlrpcs_multicall_do_call($server, $call); + } + } else { + $numCalls = count($req); + for ($i = 0; $i < $numCalls; $i++) { + $result[$i] = static::_xmlrpcs_multicall_do_call_phpvals($server, $req[$i]); + } + } + + return new Response(new Value($result, 'array')); + } + + /** + * Error handler used to track errors that occur during server-side execution of PHP code. + * This allows to report back to the client whether an internal error has occurred or not + * using an xmlrpc response object, instead of letting the client deal with the html junk + * that a PHP execution error on the server generally entails. + * + * NB: in fact a user defined error handler can only handle WARNING, NOTICE and USER_* errors. + */ + public static function _xmlrpcs_errorHandler($errCode, $errString, $filename = null, $lineNo = null, $context = null) + { + // obey the @ protocol + if (error_reporting() == 0) { + return; + } + + //if($errCode != E_NOTICE && $errCode != E_WARNING && $errCode != E_USER_NOTICE && $errCode != E_USER_WARNING) + if ($errCode != E_STRICT) { + \PhpXmlRpc\Server::error_occurred($errString); + } + // Try to avoid as much as possible disruption to the previous error handling + // mechanism in place + if (self::$_xmlrpcs_prev_ehandler == '') { + // The previous error handler was the default: all we should do is log error + // to the default error log (if level high enough) + if (ini_get('log_errors') && (intval(ini_get('error_reporting')) & $errCode)) { + if (self::$logger === null) { + self::$logger = Logger::instance(); + } + self::$logger->errorLog($errString); + } + } else { + // Pass control on to previous error handler, trying to avoid loops... + if (self::$_xmlrpcs_prev_ehandler != array('\PhpXmlRpc\Server', '_xmlrpcs_errorHandler')) { + if (is_array(self::$_xmlrpcs_prev_ehandler)) { + // the following works both with static class methods and plain object methods as error handler + call_user_func_array(self::$_xmlrpcs_prev_ehandler, array($errCode, $errString, $filename, $lineNo, $context)); + } else { + $method = self::$_xmlrpcs_prev_ehandler; + $method($errCode, $errString, $filename, $lineNo, $context); + } + } + } + } +} diff --git a/lib/phpxmlrpc/Value.php b/lib/phpxmlrpc/Value.php new file mode 100644 index 00000000000..ec43fc140e9 --- /dev/null +++ b/lib/phpxmlrpc/Value.php @@ -0,0 +1,672 @@ + 1, + "i8" => 1, + "int" => 1, + "boolean" => 1, + "double" => 1, + "string" => 1, + "dateTime.iso8601" => 1, + "base64" => 1, + "array" => 2, + "struct" => 3, + "null" => 1, + ); + + protected static $logger; + protected static $charsetEncoder; + + /// @todo: do these need to be public? + /** @var Value[]|mixed */ + public $me = array(); + /** + * @var int $mytype + * @internal + */ + public $mytype = 0; + /** @var string|null $_php_class */ + public $_php_class = null; + + public function getLogger() + { + if (self::$logger === null) { + self::$logger = Logger::instance(); + } + return self::$logger; + } + + public static function setLogger($logger) + { + self::$logger = $logger; + } + + public function getCharsetEncoder() + { + if (self::$charsetEncoder === null) { + self::$charsetEncoder = Charset::instance(); + } + return self::$charsetEncoder; + } + + public function setCharsetEncoder($charsetEncoder) + { + self::$charsetEncoder = $charsetEncoder; + } + + /** + * Build an xmlrpc value. + * + * When no value or type is passed in, the value is left uninitialized, and the value can be added later. + * + * @param Value[]|mixed $val if passing in an array, all array elements should be PhpXmlRpc\Value themselves + * @param string $type any valid xmlrpc type name (lowercase): i4, int, boolean, string, double, dateTime.iso8601, + * base64, array, struct, null. + * If null, 'string' is assumed. + * You should refer to http://www.xmlrpc.com/spec for more information on what each of these mean. + */ + public function __construct($val = -1, $type = '') + { + // optimization creep - do not call addXX, do it all inline. + // downside: booleans will not be coerced anymore + if ($val !== -1 || $type != '') { + switch ($type) { + case '': + $this->mytype = 1; + $this->me['string'] = $val; + break; + case 'i4': + case 'i8': + case 'int': + case 'double': + case 'string': + case 'boolean': + case 'dateTime.iso8601': + case 'base64': + case 'null': + $this->mytype = 1; + $this->me[$type] = $val; + break; + case 'array': + $this->mytype = 2; + $this->me['array'] = $val; + break; + case 'struct': + $this->mytype = 3; + $this->me['struct'] = $val; + break; + default: + $this->getLogger()->errorLog("XML-RPC: " . __METHOD__ . ": not a known type ($type)"); + } + } + } + + /** + * Add a single php value to an xmlrpc value. + * + * If the xmlrpc value is an array, the php value is added as its last element. + * If the xmlrpc value is empty (uninitialized), this method makes it a scalar value, and sets that value. + * Fails if the xmlrpc value is not an array and already initialized. + * + * @param mixed $val + * @param string $type allowed values: i4, i8, int, boolean, string, double, dateTime.iso8601, base64, null. + * + * @return int 1 or 0 on failure + */ + public function addScalar($val, $type = 'string') + { + $typeOf = null; + if (isset(static::$xmlrpcTypes[$type])) { + $typeOf = static::$xmlrpcTypes[$type]; + } + + if ($typeOf !== 1) { + $this->getLogger()->errorLog("XML-RPC: " . __METHOD__ . ": not a scalar type ($type)"); + return 0; + } + + // coerce booleans into correct values + // NB: we should either do it for datetimes, integers, i8 and doubles, too, + // or just plain remove this check, implemented on booleans only... + if ($type == static::$xmlrpcBoolean) { + if (strcasecmp($val, 'true') == 0 || $val == 1 || ($val == true && strcasecmp($val, 'false'))) { + $val = true; + } else { + $val = false; + } + } + + switch ($this->mytype) { + case 1: + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': scalar xmlrpc value can have only one value'); + return 0; + case 3: + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': cannot add anonymous scalar to struct xmlrpc value'); + return 0; + case 2: + // we're adding a scalar value to an array here + $this->me['array'][] = new Value($val, $type); + + return 1; + default: + // a scalar, so set the value and remember we're scalar + $this->me[$type] = $val; + $this->mytype = $typeOf; + + return 1; + } + } + + /** + * Add an array of xmlrpc value objects to an xmlrpc value. + * + * If the xmlrpc value is an array, the elements are appended to the existing ones. + * If the xmlrpc value is empty (uninitialized), this method makes it an array value, and sets that value. + * Fails otherwise. + * + * @param Value[] $values + * + * @return int 1 or 0 on failure + * + * @todo add some checking for $values to be an array of xmlrpc values? + */ + public function addArray($values) + { + if ($this->mytype == 0) { + $this->mytype = static::$xmlrpcTypes['array']; + $this->me['array'] = $values; + + return 1; + } elseif ($this->mytype == 2) { + // we're adding to an array here + $this->me['array'] = array_merge($this->me['array'], $values); + + return 1; + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']'); + return 0; + } + } + + /** + * Merges an array of named xmlrpc value objects into an xmlrpc value. + * + * If the xmlrpc value is a struct, the elements are merged with the existing ones (overwriting existing ones). + * If the xmlrpc value is empty (uninitialized), this method makes it a struct value, and sets that value. + * Fails otherwise. + * + * @param Value[] $values + * + * @return int 1 or 0 on failure + * + * @todo add some checking for $values to be an array? + */ + public function addStruct($values) + { + if ($this->mytype == 0) { + $this->mytype = static::$xmlrpcTypes['struct']; + $this->me['struct'] = $values; + + return 1; + } elseif ($this->mytype == 3) { + // we're adding to a struct here + $this->me['struct'] = array_merge($this->me['struct'], $values); + + return 1; + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']'); + return 0; + } + } + + /** + * Returns a string containing either "struct", "array", "scalar" or "undef", describing the base type of the value. + * + * @return string + */ + public function kindOf() + { + switch ($this->mytype) { + case 3: + return 'struct'; + case 2: + return 'array'; + case 1: + return 'scalar'; + default: + return 'undef'; + } + } + + /** + * @param string typ + * @param Value[]|mixed $val + * @param string $charsetEncoding + * @return string + */ + protected function serializedata($typ, $val, $charsetEncoding = '') + { + $rs = ''; + + if (!isset(static::$xmlrpcTypes[$typ])) { + return $rs; + } + + switch (static::$xmlrpcTypes[$typ]) { + case 1: + switch ($typ) { + case static::$xmlrpcBase64: + $rs .= "<${typ}>" . base64_encode($val) . ""; + break; + case static::$xmlrpcBoolean: + $rs .= "<${typ}>" . ($val ? '1' : '0') . ""; + break; + case static::$xmlrpcString: + // Do NOT use htmlentities, since it will produce named html entities, which are invalid xml + $rs .= "<${typ}>" . $this->getCharsetEncoder()->encodeEntities($val, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . ""; + break; + case static::$xmlrpcInt: + case static::$xmlrpcI4: + case static::$xmlrpcI8: + $rs .= "<${typ}>" . (int)$val . ""; + break; + case static::$xmlrpcDouble: + // avoid using standard conversion of float to string because it is locale-dependent, + // and also because the xmlrpc spec forbids exponential notation. + // sprintf('%F') could be most likely ok but it fails eg. on 2e-14. + // The code below tries its best at keeping max precision while avoiding exp notation, + // but there is of course no limit in the number of decimal places to be used... + $rs .= "<${typ}>" . preg_replace('/\\.?0+$/', '', number_format((double)$val, PhpXmlRpc::$xmlpc_double_precision, '.', '')) . ""; + break; + case static::$xmlrpcDateTime: + if (is_string($val)) { + $rs .= "<${typ}>${val}"; + } elseif (is_a($val, 'DateTime')) { + $rs .= "<${typ}>" . $val->format('Ymd\TH:i:s') . ""; + } elseif (is_int($val)) { + $rs .= "<${typ}>" . strftime("%Y%m%dT%H:%M:%S", $val) . ""; + } else { + // not really a good idea here: but what shall we output anyway? left for backward compat... + $rs .= "<${typ}>${val}"; + } + break; + case static::$xmlrpcNull: + if (PhpXmlRpc::$xmlrpc_null_apache_encoding) { + $rs .= ""; + } else { + $rs .= ""; + } + break; + default: + // no standard type value should arrive here, but provide a possibility + // for xmlrpc values of unknown type... + $rs .= "<${typ}>${val}"; + } + break; + case 3: + // struct + if ($this->_php_class) { + $rs .= '\n"; + } else { + $rs .= "\n"; + } + $charsetEncoder = $this->getCharsetEncoder(); + /** @var Value $val2 */ + foreach ($val as $key2 => $val2) { + $rs .= '' . $charsetEncoder->encodeEntities($key2, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "\n"; + //$rs.=$this->serializeval($val2); + $rs .= $val2->serialize($charsetEncoding); + $rs .= "\n"; + } + $rs .= ''; + break; + case 2: + // array + $rs .= "\n\n"; + /** @var Value $element */ + foreach ($val as $element) { + //$rs.=$this->serializeval($val[$i]); + $rs .= $element->serialize($charsetEncoding); + } + $rs .= "\n"; + break; + default: + break; + } + + return $rs; + } + + /** + * Returns the xml representation of the value. XML prologue not included. + * + * @param string $charsetEncoding the charset to be used for serialization. if null, US-ASCII is assumed + * + * @return string + */ + public function serialize($charsetEncoding = '') + { + $val = reset($this->me); + $typ = key($this->me); + + return '' . $this->serializedata($typ, $val, $charsetEncoding) . "\n"; + } + + /** + * Checks whether a struct member with a given name is present. + * + * Works only on xmlrpc values of type struct. + * + * @param string $key the name of the struct member to be looked up + * + * @return boolean + * + * @deprecated use array access, e.g. isset($val[$key]) + */ + public function structmemexists($key) + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return array_key_exists($key, $this->me['struct']); + } + + /** + * Returns the value of a given struct member (an xmlrpc value object in itself). + * Will raise a php warning if struct member of given name does not exist. + * + * @param string $key the name of the struct member to be looked up + * + * @return Value + * + * @deprecated use array access, e.g. $val[$key] + */ + public function structmem($key) + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return $this->me['struct'][$key]; + } + + /** + * Reset internal pointer for xmlrpc values of type struct. + * @deprecated iterate directly over the object using foreach instead + */ + public function structreset() + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + reset($this->me['struct']); + } + + /** + * Return next member element for xmlrpc values of type struct. + * + * @return Value + * @throws \Error starting with php 8.0, this function should not be used, as it will always throw + * + * @deprecated iterate directly over the object using foreach instead + */ + public function structeach() + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return @each($this->me['struct']); + } + + /** + * Returns the value of a scalar xmlrpc value (base 64 decoding is automatically handled here) + * + * @return mixed + */ + public function scalarval() + { + $b = reset($this->me); + + return $b; + } + + /** + * Returns the type of the xmlrpc value. + * + * For integers, 'int' is always returned in place of 'i4'. 'i8' is considered a separate type and returned as such + * + * @return string + */ + public function scalartyp() + { + reset($this->me); + $a = key($this->me); + if ($a == static::$xmlrpcI4) { + $a = static::$xmlrpcInt; + } + + return $a; + } + + /** + * Returns the m-th member of an xmlrpc value of array type. + * + * @param integer $key the index of the value to be retrieved (zero based) + * + * @return Value + * + * @deprecated use array access, e.g. $val[$key] + */ + public function arraymem($key) + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return $this->me['array'][$key]; + } + + /** + * Returns the number of members in an xmlrpc value of array type. + * + * @return integer + * + * @deprecated use count() instead + */ + public function arraysize() + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return count($this->me['array']); + } + + /** + * Returns the number of members in an xmlrpc value of struct type. + * + * @return integer + * + * @deprecated use count() instead + */ + public function structsize() + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + return count($this->me['struct']); + } + + /** + * Returns the number of members in an xmlrpc value: + * - 0 for uninitialized values + * - 1 for scalar values + * - the number of elements for struct and array values + * + * @return integer + */ + #[\ReturnTypeWillChange] + public function count() + { + switch ($this->mytype) { + case 3: + return count($this->me['struct']); + case 2: + return count($this->me['array']); + case 1: + return 1; + default: + return 0; + } + } + + /** + * Implements the IteratorAggregate interface + * + * @return \ArrayIterator + * @internal required to be public to implement an Interface + */ + #[\ReturnTypeWillChange] + public function getIterator() + { + switch ($this->mytype) { + case 3: + return new \ArrayIterator($this->me['struct']); + case 2: + return new \ArrayIterator($this->me['array']); + case 1: + return new \ArrayIterator($this->me); + default: + return new \ArrayIterator(); + } + } + + /** + * @internal required to be public to implement an Interface + * @param mixed $offset + * @param mixed $value + * @throws \Exception + */ + #[\ReturnTypeWillChange] + public function offsetSet($offset, $value) + { + switch ($this->mytype) { + case 3: + if (!($value instanceof \PhpXmlRpc\Value)) { + throw new \Exception('It is only possible to add Value objects to an XML-RPC Struct'); + } + if (is_null($offset)) { + // disallow struct members with empty names + throw new \Exception('It is not possible to add anonymous members to an XML-RPC Struct'); + } else { + $this->me['struct'][$offset] = $value; + } + return; + case 2: + if (!($value instanceof \PhpXmlRpc\Value)) { + throw new \Exception('It is only possible to add Value objects to an XML-RPC Array'); + } + if (is_null($offset)) { + $this->me['array'][] = $value; + } else { + // nb: we are not checking that $offset is above the existing array range... + $this->me['array'][$offset] = $value; + } + return; + case 1: +// todo: handle i4 vs int + reset($this->me); + $type = key($this->me); + if ($type != $offset) { + throw new \Exception(''); + } + $this->me[$type] = $value; + return; + default: + // it would be nice to allow empty values to be be turned into non-empty ones this way, but we miss info to do so + throw new \Exception("XML-RPC Value is of type 'undef' and its value can not be set using array index"); + } + } + + /** + * @internal required to be public to implement an Interface + * @param mixed $offset + * @return bool + */ + #[\ReturnTypeWillChange] + public function offsetExists($offset) + { + switch ($this->mytype) { + case 3: + return isset($this->me['struct'][$offset]); + case 2: + return isset($this->me['array'][$offset]); + case 1: +// todo: handle i4 vs int + return $offset == $this->scalartyp(); + default: + return false; + } + } + + /** + * @internal required to be public to implement an Interface + * @param mixed $offset + * @throws \Exception + */ + #[\ReturnTypeWillChange] + public function offsetUnset($offset) + { + switch ($this->mytype) { + case 3: + unset($this->me['struct'][$offset]); + return; + case 2: + unset($this->me['array'][$offset]); + return; + case 1: + // can not remove value from a scalar + throw new \Exception("XML-RPC Value is of type 'scalar' and its value can not be unset using array index"); + default: + throw new \Exception("XML-RPC Value is of type 'undef' and its value can not be unset using array index"); + } + } + + /** + * @internal required to be public to implement an Interface + * @param mixed $offset + * @return mixed|Value|null + * @throws \Exception + */ + #[\ReturnTypeWillChange] + public function offsetGet($offset) + { + switch ($this->mytype) { + case 3: + return isset($this->me['struct'][$offset]) ? $this->me['struct'][$offset] : null; + case 2: + return isset($this->me['array'][$offset]) ? $this->me['array'][$offset] : null; + case 1: +// on bad type: null or exception? + $value = reset($this->me); + $type = key($this->me); + return $type == $offset ? $value : null; + default: +// return null or exception? + throw new \Exception("XML-RPC Value is of type 'undef' and can not be accessed using array index"); + } + } +} diff --git a/lib/phpxmlrpc/Wrapper.php b/lib/phpxmlrpc/Wrapper.php new file mode 100644 index 00000000000..e000a0630a2 --- /dev/null +++ b/lib/phpxmlrpc/Wrapper.php @@ -0,0 +1,1162 @@ +getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': syntax for function to be wrapped is wrong'); + return false; + } + if (is_string($callable[0])) { + $plainFuncName = implode('::', $callable); + } elseif (is_object($callable[0])) { + $plainFuncName = get_class($callable[0]) . '->' . $callable[1]; + } + $exists = method_exists($callable[0], $callable[1]); + } else if ($callable instanceof \Closure) { + // we do not support creating code which wraps closures, as php does not allow to serialize them + if (!$buildIt) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': a closure can not be wrapped in generated source code'); + return false; + } + + $plainFuncName = 'Closure'; + $exists = true; + } else { + $plainFuncName = $callable; + $exists = function_exists($callable); + } + + if (!$exists) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': function to be wrapped is not defined: ' . $plainFuncName); + return false; + } + + $funcDesc = $this->introspectFunction($callable, $plainFuncName); + if (!$funcDesc) { + return false; + } + + $funcSigs = $this->buildMethodSignatures($funcDesc); + + if ($buildIt) { + $callable = $this->buildWrapFunctionClosure($callable, $extraOptions, $plainFuncName, $funcDesc); + } else { + $newFuncName = $this->newFunctionName($callable, $newFuncName, $extraOptions); + $code = $this->buildWrapFunctionSource($callable, $newFuncName, $extraOptions, $plainFuncName, $funcDesc); + } + + $ret = array( + 'function' => $callable, + 'signature' => $funcSigs['sigs'], + 'docstring' => $funcDesc['desc'], + 'signature_docs' => $funcSigs['sigsDocs'], + ); + if (!$buildIt) { + $ret['function'] = $newFuncName; + $ret['source'] = $code; + } + return $ret; + } + + /** + * Introspect a php callable and its phpdoc block and extract information about its signature + * + * @param callable $callable + * @param string $plainFuncName + * @return array|false + */ + protected function introspectFunction($callable, $plainFuncName) + { + // start to introspect PHP code + if (is_array($callable)) { + $func = new \ReflectionMethod($callable[0], $callable[1]); + if ($func->isPrivate()) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is private: ' . $plainFuncName); + return false; + } + if ($func->isProtected()) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is protected: ' . $plainFuncName); + return false; + } + if ($func->isConstructor()) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is the constructor: ' . $plainFuncName); + return false; + } + if ($func->isDestructor()) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is the destructor: ' . $plainFuncName); + return false; + } + if ($func->isAbstract()) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is abstract: ' . $plainFuncName); + return false; + } + /// @todo add more checks for static vs. nonstatic? + } else { + $func = new \ReflectionFunction($callable); + } + if ($func->isInternal()) { + // Note: from PHP 5.1.0 onward, we will possibly be able to use invokeargs + // instead of getparameters to fully reflect internal php functions ? + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': function to be wrapped is internal: ' . $plainFuncName); + return false; + } + + // retrieve parameter names, types and description from javadoc comments + + // function description + $desc = ''; + // type of return val: by default 'any' + $returns = Value::$xmlrpcValue; + // desc of return val + $returnsDocs = ''; + // type + name of function parameters + $paramDocs = array(); + + $docs = $func->getDocComment(); + if ($docs != '') { + $docs = explode("\n", $docs); + $i = 0; + foreach ($docs as $doc) { + $doc = trim($doc, " \r\t/*"); + if (strlen($doc) && strpos($doc, '@') !== 0 && !$i) { + if ($desc) { + $desc .= "\n"; + } + $desc .= $doc; + } elseif (strpos($doc, '@param') === 0) { + // syntax: @param type $name [desc] + if (preg_match('/@param\s+(\S+)\s+(\$\S+)\s*(.+)?/', $doc, $matches)) { + $name = strtolower(trim($matches[2])); + //$paramDocs[$name]['name'] = trim($matches[2]); + $paramDocs[$name]['doc'] = isset($matches[3]) ? $matches[3] : ''; + $paramDocs[$name]['type'] = $matches[1]; + } + $i++; + } elseif (strpos($doc, '@return') === 0) { + // syntax: @return type [desc] + if (preg_match('/@return\s+(\S+)(\s+.+)?/', $doc, $matches)) { + $returns = $matches[1]; + if (isset($matches[2])) { + $returnsDocs = trim($matches[2]); + } + } + } + } + } + + // execute introspection of actual function prototype + $params = array(); + $i = 0; + foreach ($func->getParameters() as $paramObj) { + $params[$i] = array(); + $params[$i]['name'] = '$' . $paramObj->getName(); + $params[$i]['isoptional'] = $paramObj->isOptional(); + $i++; + } + + return array( + 'desc' => $desc, + 'docs' => $docs, + 'params' => $params, // array, positionally indexed + 'paramDocs' => $paramDocs, // array, indexed by name + 'returns' => $returns, + 'returnsDocs' =>$returnsDocs, + ); + } + + /** + * Given the method description given by introspection, create method signature data + * + * @todo support better docs with multiple types separated by pipes by creating multiple signatures + * (this is questionable, as it might produce a big matrix of possible signatures with many such occurrences) + * + * @param array $funcDesc as generated by self::introspectFunction() + * + * @return array + */ + protected function buildMethodSignatures($funcDesc) + { + $i = 0; + $parsVariations = array(); + $pars = array(); + $pNum = count($funcDesc['params']); + foreach ($funcDesc['params'] as $param) { + /* // match by name real param and documented params + $name = strtolower($param['name']); + if (!isset($funcDesc['paramDocs'][$name])) { + $funcDesc['paramDocs'][$name] = array(); + } + if (!isset($funcDesc['paramDocs'][$name]['type'])) { + $funcDesc['paramDocs'][$name]['type'] = 'mixed'; + }*/ + + if ($param['isoptional']) { + // this particular parameter is optional. save as valid previous list of parameters + $parsVariations[] = $pars; + } + + $pars[] = "\$p$i"; + $i++; + if ($i == $pNum) { + // last allowed parameters combination + $parsVariations[] = $pars; + } + } + + if (count($parsVariations) == 0) { + // only known good synopsis = no parameters + $parsVariations[] = array(); + } + + $sigs = array(); + $sigsDocs = array(); + foreach ($parsVariations as $pars) { + // build a signature + $sig = array($this->php2XmlrpcType($funcDesc['returns'])); + $pSig = array($funcDesc['returnsDocs']); + for ($i = 0; $i < count($pars); $i++) { + $name = strtolower($funcDesc['params'][$i]['name']); + if (isset($funcDesc['paramDocs'][$name]['type'])) { + $sig[] = $this->php2XmlrpcType($funcDesc['paramDocs'][$name]['type']); + } else { + $sig[] = Value::$xmlrpcValue; + } + $pSig[] = isset($funcDesc['paramDocs'][$name]['doc']) ? $funcDesc['paramDocs'][$name]['doc'] : ''; + } + $sigs[] = $sig; + $sigsDocs[] = $pSig; + } + + return array( + 'sigs' => $sigs, + 'sigsDocs' => $sigsDocs + ); + } + + /** + * Creates a closure that will execute $callable + * @todo validate params? In theory all validation is left to the dispatch map... + * @todo add support for $catchWarnings + * + * @param $callable + * @param array $extraOptions + * @param string $plainFuncName + * @param array $funcDesc + * @return \Closure + */ + protected function buildWrapFunctionClosure($callable, $extraOptions, $plainFuncName, $funcDesc) + { + /** + * @param Request $req + * @return mixed + */ + $function = function($req) use($callable, $extraOptions, $funcDesc) + { + $nameSpace = '\\PhpXmlRpc\\'; + $encoderClass = $nameSpace.'Encoder'; + $responseClass = $nameSpace.'Response'; + $valueClass = $nameSpace.'Value'; + + // validate number of parameters received + // this should be optional really, as we assume the server does the validation + $minPars = count($funcDesc['params']); + $maxPars = $minPars; + foreach ($funcDesc['params'] as $i => $param) { + if ($param['isoptional']) { + // this particular parameter is optional. We assume later ones are as well + $minPars = $i; + break; + } + } + $numPars = $req->getNumParams(); + if ($numPars < $minPars || $numPars > $maxPars) { + return new $responseClass(0, 3, 'Incorrect parameters passed to method'); + } + + $encoder = new $encoderClass(); + $options = array(); + if (isset($extraOptions['decode_php_objs']) && $extraOptions['decode_php_objs']) { + $options[] = 'decode_php_objs'; + } + $params = $encoder->decode($req, $options); + + $result = call_user_func_array($callable, $params); + + if (! is_a($result, $responseClass)) { + if ($funcDesc['returns'] == Value::$xmlrpcDateTime || $funcDesc['returns'] == Value::$xmlrpcBase64) { + $result = new $valueClass($result, $funcDesc['returns']); + } else { + $options = array(); + if (isset($extraOptions['encode_php_objs']) && $extraOptions['encode_php_objs']) { + $options[] = 'encode_php_objs'; + } + + $result = $encoder->encode($result, $options); + } + $result = new $responseClass($result); + } + + return $result; + }; + + return $function; + } + + /** + * Return a name for a new function, based on $callable, insuring its uniqueness + * @param mixed $callable a php callable, or the name of an xmlrpc method + * @param string $newFuncName when not empty, it is used instead of the calculated version + * @return string + */ + protected function newFunctionName($callable, $newFuncName, $extraOptions) + { + // determine name of new php function + + $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc'; + + if ($newFuncName == '') { + if (is_array($callable)) { + if (is_string($callable[0])) { + $xmlrpcFuncName = "{$prefix}_" . implode('_', $callable); + } else { + $xmlrpcFuncName = "{$prefix}_" . get_class($callable[0]) . '_' . $callable[1]; + } + } else { + if ($callable instanceof \Closure) { + $xmlrpcFuncName = "{$prefix}_closure"; + } else { + $callable = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), + array('_', ''), $callable); + $xmlrpcFuncName = "{$prefix}_$callable"; + } + } + } else { + $xmlrpcFuncName = $newFuncName; + } + + while (function_exists($xmlrpcFuncName)) { + $xmlrpcFuncName .= 'x'; + } + + return $xmlrpcFuncName; + } + + /** + * @param $callable + * @param string $newFuncName + * @param array $extraOptions + * @param string $plainFuncName + * @param array $funcDesc + * @return string + * + * @todo add a nice phpdoc block in the generated source + */ + protected function buildWrapFunctionSource($callable, $newFuncName, $extraOptions, $plainFuncName, $funcDesc) + { + $namespace = '\\PhpXmlRpc\\'; + + $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false; + $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false; + $catchWarnings = isset($extraOptions['suppress_warnings']) && $extraOptions['suppress_warnings'] ? '@' : ''; + + $i = 0; + $parsVariations = array(); + $pars = array(); + $pNum = count($funcDesc['params']); + foreach ($funcDesc['params'] as $param) { + + if ($param['isoptional']) { + // this particular parameter is optional. save as valid previous list of parameters + $parsVariations[] = $pars; + } + + $pars[] = "\$p[$i]"; + $i++; + if ($i == $pNum) { + // last allowed parameters combination + $parsVariations[] = $pars; + } + } + + if (count($parsVariations) == 0) { + // only known good synopsis = no parameters + $parsVariations[] = array(); + $minPars = 0; + $maxPars = 0; + } else { + $minPars = count($parsVariations[0]); + $maxPars = count($parsVariations[count($parsVariations)-1]); + } + + // build body of new function + + $innerCode = "\$paramCount = \$req->getNumParams();\n"; + $innerCode .= "if (\$paramCount < $minPars || \$paramCount > $maxPars) return new {$namespace}Response(0, " . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . ", '" . PhpXmlRpc::$xmlrpcstr['incorrect_params'] . "');\n"; + + $innerCode .= "\$encoder = new {$namespace}Encoder();\n"; + if ($decodePhpObjects) { + $innerCode .= "\$p = \$encoder->decode(\$req, array('decode_php_objs'));\n"; + } else { + $innerCode .= "\$p = \$encoder->decode(\$req);\n"; + } + + // since we are building source code for later use, if we are given an object instance, + // we go out of our way and store a pointer to it in a static class var... + if (is_array($callable) && is_object($callable[0])) { + self::$objHolder[$newFuncName] = $callable[0]; + $innerCode .= "\$obj = PhpXmlRpc\\Wrapper::\$objHolder['$newFuncName'];\n"; + $realFuncName = '$obj->' . $callable[1]; + } else { + $realFuncName = $plainFuncName; + } + foreach ($parsVariations as $i => $pars) { + $innerCode .= "if (\$paramCount == " . count($pars) . ") \$retval = {$catchWarnings}$realFuncName(" . implode(',', $pars) . ");\n"; + if ($i < (count($parsVariations) - 1)) + $innerCode .= "else\n"; + } + $innerCode .= "if (is_a(\$retval, '{$namespace}Response')) return \$retval; else\n"; + if ($funcDesc['returns'] == Value::$xmlrpcDateTime || $funcDesc['returns'] == Value::$xmlrpcBase64) { + $innerCode .= "return new {$namespace}Response(new {$namespace}Value(\$retval, '{$funcDesc['returns']}'));"; + } else { + if ($encodePhpObjects) { + $innerCode .= "return new {$namespace}Response(\$encoder->encode(\$retval, array('encode_php_objs')));\n"; + } else { + $innerCode .= "return new {$namespace}Response(\$encoder->encode(\$retval));\n"; + } + } + // shall we exclude functions returning by ref? + // if($func->returnsReference()) + // return false; + + $code = "function $newFuncName(\$req) {\n" . $innerCode . "\n}"; + + return $code; + } + + /** + * Given a user-defined PHP class or php object, map its methods onto a list of + * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc server + * object and called from remote clients (as well as their corresponding signature info). + * + * @param string|object $className the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class + * @param array $extraOptions see the docs for wrapPhpMethod for basic options, plus + * - string method_type 'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on whether $className is a class name or object instance + * - string method_filter a regexp used to filter methods to wrap based on their names + * - string prefix used for the names of the xmlrpc methods created. + * - string replace_class_name use to completely replace the class name with the prefix in the generated method names. e.g. instead of \Some\Namespace\Class.method use prefixmethod + * @return array|false false on failure + */ + public function wrapPhpClass($className, $extraOptions = array()) + { + $methodFilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : ''; + $methodType = isset($extraOptions['method_type']) ? $extraOptions['method_type'] : 'auto'; + + $results = array(); + $mList = get_class_methods($className); + foreach ($mList as $mName) { + if ($methodFilter == '' || preg_match($methodFilter, $mName)) { + $func = new \ReflectionMethod($className, $mName); + if (!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract()) { + if (($func->isStatic() && ($methodType == 'all' || $methodType == 'static' || ($methodType == 'auto' && is_string($className)))) || + (!$func->isStatic() && ($methodType == 'all' || $methodType == 'nonstatic' || ($methodType == 'auto' && is_object($className)))) + ) { + $methodWrap = $this->wrapPhpFunction(array($className, $mName), '', $extraOptions); + + if ($methodWrap) { + $results[$this->generateMethodNameForClassMethod($className, $mName, $extraOptions)] = $methodWrap; + } + } + } + } + } + + return $results; + } + + /** + * @param string|object $className + * @param string $classMethod + * @param array $extraOptions + * @return string + */ + protected function generateMethodNameForClassMethod($className, $classMethod, $extraOptions = array()) + { + if (isset($extraOptions['replace_class_name']) && $extraOptions['replace_class_name']) { + return (isset($extraOptions['prefix']) ? $extraOptions['prefix'] : '') . $classMethod; + } + + if (is_object($className)) { + $realClassName = get_class($className); + } else { + $realClassName = $className; + } + return (isset($extraOptions['prefix']) ? $extraOptions['prefix'] : '') . "$realClassName.$classMethod"; + } + + /** + * Given an xmlrpc client and a method name, register a php wrapper function + * that will call it and return results using native php types for both + * params and results. The generated php function will return a Response + * object for failed xmlrpc calls. + * + * Known limitations: + * - server must support system.methodsignature for the wanted xmlrpc method + * - for methods that expose many signatures, only one can be picked (we + * could in principle check if signatures differ only by number of params + * and not by type, but it would be more complication than we can spare time) + * - nested xmlrpc params: the caller of the generated php function has to + * encode on its own the params passed to the php function if these are structs + * or arrays whose (sub)members include values of type datetime or base64 + * + * Notes: the connection properties of the given client will be copied + * and reused for the connection used during the call to the generated + * php function. + * Calling the generated php function 'might' be slow: a new xmlrpc client + * is created on every invocation and an xmlrpc-connection opened+closed. + * An extra 'debug' param is appended to param list of xmlrpc method, useful + * for debugging purposes. + * + * @todo allow caller to give us the method signature instead of querying for it, or just say 'skip it' + * @todo if we can not retrieve method signature, create a php function with varargs + * @todo allow the created function to throw exceptions on method calls failures + * @todo if caller did not specify a specific sig, shall we support all of them? + * It might be hard (hence slow) to match based on type and number of arguments... + * + * @param Client $client an xmlrpc client set up correctly to communicate with target server + * @param string $methodName the xmlrpc method to be mapped to a php function + * @param array $extraOptions array of options that specify conversion details. Valid options include + * - integer signum the index of the method signature to use in mapping (if method exposes many sigs) + * - integer timeout timeout (in secs) to be used when executing function/calling remote method + * - string protocol 'http' (default), 'http11' or 'https' + * - string new_function_name the name of php function to create, when return_source is used. If unspecified, lib will pick an appropriate name + * - string return_source if true return php code w. function definition instead of function itself (closure) + * - bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects + * - bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- + * - mixed return_on_fault a php value to be returned when the xmlrpc call fails/returns a fault response (by default the Response object is returned in this case). If a string is used, '%faultCode%' and '%faultString%' tokens will be substituted with actual error values + * - bool debug set it to 1 or 2 to see debug results of querying server for method synopsis + * - int simple_client_copy set it to 1 to have a lightweight copy of the $client object made in the generated code (only used when return_source = true) + * + * @return \closure|string[]|false false on failure, closure by default and array for return_source = true + */ + public function wrapXmlrpcMethod($client, $methodName, $extraOptions = array()) + { + $newFuncName = isset($extraOptions['new_function_name']) ? $extraOptions['new_function_name'] : ''; + + $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true; + + $mSig = $this->retrieveMethodSignature($client, $methodName, $extraOptions); + if (!$mSig) { + return false; + } + + if ($buildIt) { + return $this->buildWrapMethodClosure($client, $methodName, $extraOptions, $mSig); + } else { + // if in 'offline' mode, retrieve method description too. + // in online mode, favour speed of operation + $mDesc = $this->retrieveMethodHelp($client, $methodName, $extraOptions); + + $newFuncName = $this->newFunctionName($methodName, $newFuncName, $extraOptions); + + $results = $this->buildWrapMethodSource($client, $methodName, $extraOptions, $newFuncName, $mSig, $mDesc); + /* was: $results = $this->build_remote_method_wrapper_code($client, $methodName, + $newFuncName, $mSig, $mDesc, $timeout, $protocol, $simpleClientCopy, + $prefix, $decodePhpObjects, $encodePhpObjects, $decodeFault, + $faultResponse, $namespace);*/ + + $results['function'] = $newFuncName; + + return $results; + } + } + + /** + * Retrieves an xmlrpc method signature from a server which supports system.methodSignature + * @param Client $client + * @param string $methodName + * @param array $extraOptions + * @return false|array + */ + protected function retrieveMethodSignature($client, $methodName, array $extraOptions = array()) + { + $namespace = '\\PhpXmlRpc\\'; + $reqClass = $namespace . 'Request'; + $valClass = $namespace . 'Value'; + $decoderClass = $namespace . 'Encoder'; + + $debug = isset($extraOptions['debug']) ? ($extraOptions['debug']) : 0; + $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0; + $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; + $sigNum = isset($extraOptions['signum']) ? (int)$extraOptions['signum'] : 0; + + $req = new $reqClass('system.methodSignature'); + $req->addparam(new $valClass($methodName)); + $client->setDebug($debug); + $response = $client->send($req, $timeout, $protocol); + if ($response->faultCode()) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve method signature from remote server for method ' . $methodName); + return false; + } + + $mSig = $response->value(); + if ($client->return_type != 'phpvals') { + $decoder = new $decoderClass(); + $mSig = $decoder->decode($mSig); + } + + if (!is_array($mSig) || count($mSig) <= $sigNum) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve method signature nr.' . $sigNum . ' from remote server for method ' . $methodName); + return false; + } + + return $mSig[$sigNum]; + } + + /** + * @param Client $client + * @param string $methodName + * @param array $extraOptions + * @return string in case of any error, an empty string is returned, no warnings generated + */ + protected function retrieveMethodHelp($client, $methodName, array $extraOptions = array()) + { + $namespace = '\\PhpXmlRpc\\'; + $reqClass = $namespace . 'Request'; + $valClass = $namespace . 'Value'; + + $debug = isset($extraOptions['debug']) ? ($extraOptions['debug']) : 0; + $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0; + $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; + + $mDesc = ''; + + $req = new $reqClass('system.methodHelp'); + $req->addparam(new $valClass($methodName)); + $client->setDebug($debug); + $response = $client->send($req, $timeout, $protocol); + if (!$response->faultCode()) { + $mDesc = $response->value(); + if ($client->return_type != 'phpvals') { + $mDesc = $mDesc->scalarval(); + } + } + + return $mDesc; + } + + /** + * @param Client $client + * @param string $methodName + * @param array $extraOptions + * @param array $mSig + * @return \Closure + * + * @todo should we allow usage of parameter simple_client_copy to mean 'do not clone' in this case? + */ + protected function buildWrapMethodClosure($client, $methodName, array $extraOptions, $mSig) + { + // we clone the client, so that we can modify it a bit independently of the original + $clientClone = clone $client; + $function = function() use($clientClone, $methodName, $extraOptions, $mSig) + { + $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0; + $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; + $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false; + $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false; + if (isset($extraOptions['return_on_fault'])) { + $decodeFault = true; + $faultResponse = $extraOptions['return_on_fault']; + } else { + $decodeFault = false; + } + + $namespace = '\\PhpXmlRpc\\'; + $reqClass = $namespace . 'Request'; + $encoderClass = $namespace . 'Encoder'; + $valueClass = $namespace . 'Value'; + + $encoder = new $encoderClass(); + $encodeOptions = array(); + if ($encodePhpObjects) { + $encodeOptions[] = 'encode_php_objs'; + } + $decodeOptions = array(); + if ($decodePhpObjects) { + $decodeOptions[] = 'decode_php_objs'; + } + + /// @todo check for insufficient nr. of args besides excess ones? note that 'source' version does not... + + // support one extra parameter: debug + $maxArgs = count($mSig)-1; // 1st element is the return type + $currentArgs = func_get_args(); + if (func_num_args() == ($maxArgs+1)) { + $debug = array_pop($currentArgs); + $clientClone->setDebug($debug); + } + + $xmlrpcArgs = array(); + foreach($currentArgs as $i => $arg) { + if ($i == $maxArgs) { + break; + } + $pType = $mSig[$i+1]; + if ($pType == 'i4' || $pType == 'i8' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' || + $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null' + ) { + // by building directly xmlrpc values when type is known and scalar (instead of encode() calls), + // we make sure to honour the xmlrpc signature + $xmlrpcArgs[] = new $valueClass($arg, $pType); + } else { + $xmlrpcArgs[] = $encoder->encode($arg, $encodeOptions); + } + } + + $req = new $reqClass($methodName, $xmlrpcArgs); + // use this to get the maximum decoding flexibility + $clientClone->return_type = 'xmlrpcvals'; + $resp = $clientClone->send($req, $timeout, $protocol); + if ($resp->faultcode()) { + if ($decodeFault) { + if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || + (strpos($faultResponse, '%faultString%') !== false))) { + $faultResponse = str_replace(array('%faultCode%', '%faultString%'), + array($resp->faultCode(), $resp->faultString()), $faultResponse); + } + return $faultResponse; + } else { + return $resp; + } + } else { + return $encoder->decode($resp->value(), $decodeOptions); + } + }; + + return $function; + } + + /** + * @param Client $client + * @param string $methodName + * @param array $extraOptions + * @param string $newFuncName + * @param array $mSig + * @param string $mDesc + * @return string[] keys: source, docstring + */ + public function buildWrapMethodSource($client, $methodName, array $extraOptions, $newFuncName, $mSig, $mDesc='') + { + $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0; + $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; + $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false; + $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false; + $clientCopyMode = isset($extraOptions['simple_client_copy']) ? (int)($extraOptions['simple_client_copy']) : 0; + $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc'; + if (isset($extraOptions['return_on_fault'])) { + $decodeFault = true; + $faultResponse = $extraOptions['return_on_fault']; + } else { + $decodeFault = false; + $faultResponse = ''; + } + + $namespace = '\\PhpXmlRpc\\'; + + $code = "function $newFuncName ("; + if ($clientCopyMode < 2) { + // client copy mode 0 or 1 == full / partial client copy in emitted code + $verbatimClientCopy = !$clientCopyMode; + $innerCode = $this->buildClientWrapperCode($client, $verbatimClientCopy, $prefix, $namespace); + $innerCode .= "\$client->setDebug(\$debug);\n"; + $this_ = ''; + } else { + // client copy mode 2 == no client copy in emitted code + $innerCode = ''; + $this_ = 'this->'; + } + $innerCode .= "\$req = new {$namespace}Request('$methodName');\n"; + + if ($mDesc != '') { + // take care that PHP comment is not terminated unwillingly by method description + $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n"; + } else { + $mDesc = "/**\nFunction $newFuncName\n"; + } + + // param parsing + $innerCode .= "\$encoder = new {$namespace}Encoder();\n"; + $plist = array(); + $pCount = count($mSig); + for ($i = 1; $i < $pCount; $i++) { + $plist[] = "\$p$i"; + $pType = $mSig[$i]; + if ($pType == 'i4' || $pType == 'i8' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' || + $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null' + ) { + // only build directly xmlrpc values when type is known and scalar + $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n"; + } else { + if ($encodePhpObjects) { + $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n"; + } else { + $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n"; + } + } + $innerCode .= "\$req->addparam(\$p$i);\n"; + $mDesc .= '* @param ' . $this->xmlrpc2PhpType($pType) . " \$p$i\n"; + } + if ($clientCopyMode < 2) { + $plist[] = '$debug=0'; + $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n"; + } + $plist = implode(', ', $plist); + $mDesc .= '* @return {$namespace}Response|' . $this->xmlrpc2PhpType($mSig[0]) . " (an {$namespace}Response obj instance if call fails)\n*/\n"; + + $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n"; + if ($decodeFault) { + if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) { + $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')"; + } else { + $respCode = var_export($faultResponse, true); + } + } else { + $respCode = '$res'; + } + if ($decodePhpObjects) { + $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));"; + } else { + $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());"; + } + + $code = $code . $plist . ") {\n" . $innerCode . "\n}\n"; + + return array('source' => $code, 'docstring' => $mDesc); + } + + /** + * Similar to wrapXmlrpcMethod, but will generate a php class that wraps + * all xmlrpc methods exposed by the remote server as own methods. + * For more details see wrapXmlrpcMethod. + * + * For a slimmer alternative, see the code in demo/client/proxy.php + * + * Note that unlike wrapXmlrpcMethod, we always have to generate php code here. It seems that php 7 will have anon classes... + * + * @param Client $client the client obj all set to query the desired server + * @param array $extraOptions list of options for wrapped code. See the ones from wrapXmlrpcMethod plus + * - string method_filter regular expression + * - string new_class_name + * - string prefix + * - bool simple_client_copy set it to true to avoid copying all properties of $client into the copy made in the new class + * + * @return mixed false on error, the name of the created class if all ok or an array with code, class name and comments (if the appropriate option is set in extra_options) + */ + public function wrapXmlrpcServer($client, $extraOptions = array()) + { + $methodFilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : ''; + $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0; + $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; + $newClassName = isset($extraOptions['new_class_name']) ? $extraOptions['new_class_name'] : ''; + $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false; + $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false; + $verbatimClientCopy = isset($extraOptions['simple_client_copy']) ? !($extraOptions['simple_client_copy']) : true; + $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true; + $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc'; + $namespace = '\\PhpXmlRpc\\'; + + $reqClass = $namespace . 'Request'; + $decoderClass = $namespace . 'Encoder'; + + $req = new $reqClass('system.listMethods'); + $response = $client->send($req, $timeout, $protocol); + if ($response->faultCode()) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve method list from remote server'); + + return false; + } else { + $mList = $response->value(); + if ($client->return_type != 'phpvals') { + $decoder = new $decoderClass(); + $mList = $decoder->decode($mList); + } + if (!is_array($mList) || !count($mList)) { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve meaningful method list from remote server'); + + return false; + } else { + // pick a suitable name for the new function, avoiding collisions + if ($newClassName != '') { + $xmlrpcClassName = $newClassName; + } else { + $xmlrpcClassName = $prefix . '_' . preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), + array('_', ''), $client->server) . '_client'; + } + while ($buildIt && class_exists($xmlrpcClassName)) { + $xmlrpcClassName .= 'x'; + } + + /// @todo add function setdebug() to new class, to enable/disable debugging + $source = "class $xmlrpcClassName\n{\npublic \$client;\n\n"; + $source .= "function __construct()\n{\n"; + $source .= $this->buildClientWrapperCode($client, $verbatimClientCopy, $prefix, $namespace); + $source .= "\$this->client = \$client;\n}\n\n"; + $opts = array( + 'return_source' => true, + 'simple_client_copy' => 2, // do not produce code to copy the client object + 'timeout' => $timeout, + 'protocol' => $protocol, + 'encode_php_objs' => $encodePhpObjects, + 'decode_php_objs' => $decodePhpObjects, + 'prefix' => $prefix, + ); + /// @todo build phpdoc for class definition, too + foreach ($mList as $mName) { + if ($methodFilter == '' || preg_match($methodFilter, $mName)) { + // note: this will fail if server exposes 2 methods called f.e. do.something and do_something + $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), + array('_', ''), $mName); + $methodWrap = $this->wrapXmlrpcMethod($client, $mName, $opts); + if ($methodWrap) { + if (!$buildIt) { + $source .= $methodWrap['docstring']; + } + $source .= $methodWrap['source'] . "\n"; + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': will not create class method to wrap remote method ' . $mName); + } + } + } + $source .= "}\n"; + if ($buildIt) { + $allOK = 0; + eval($source . '$allOK=1;'); + if ($allOK) { + return $xmlrpcClassName; + } else { + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not create class ' . $xmlrpcClassName . ' to wrap remote server ' . $client->server); + return false; + } + } else { + return array('class' => $xmlrpcClassName, 'code' => $source, 'docstring' => ''); + } + } + } + } + + /** + * Given necessary info, generate php code that will build a client object just like the given one. + * Take care that no full checking of input parameters is done to ensure that + * valid php code is emitted. + * @param Client $client + * @param bool $verbatimClientCopy when true, copy all of the state of the client, except for 'debug' and 'return_type' + * @param string $prefix used for the return_type of the created client + * @param string $namespace + * + * @return string + */ + protected function buildClientWrapperCode($client, $verbatimClientCopy, $prefix = 'xmlrpc', $namespace = '\\PhpXmlRpc\\' ) + { + $code = "\$client = new {$namespace}Client('" . str_replace("'", "\'", $client->path) . + "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n"; + + // copy all client fields to the client that will be generated runtime + // (this provides for future expansion or subclassing of client obj) + if ($verbatimClientCopy) { + foreach ($client as $fld => $val) { + /// @todo in php 8.0, curl handles became objects, but they have no __set_state, thus var_export will + /// fail for xmlrpc_curl_handle. So we disabled copying it. + /// We should examine in depth if this change can have side effects - at first sight if the + /// client's curl handle is not set, all curl options are (re)set on each http call, so there + /// should be no loss of state... + if ($fld != 'debug' && $fld != 'return_type' && $fld != 'xmlrpc_curl_handle') { + $val = var_export($val, true); + $code .= "\$client->$fld = $val;\n"; + } + } + } + // only make sure that client always returns the correct data type + $code .= "\$client->return_type = '{$prefix}vals';\n"; + //$code .= "\$client->setDebug(\$debug);\n"; + return $code; + } +} diff --git a/lib/phpxmlrpc/readme_moodle.txt b/lib/phpxmlrpc/readme_moodle.txt new file mode 100644 index 00000000000..28e0fae2bc3 --- /dev/null +++ b/lib/phpxmlrpc/readme_moodle.txt @@ -0,0 +1,20 @@ +Description of XMLRPC for PHP library import into Moodle. + +Source: https://github.com/gggeek/phpxmlrpc + +This library provides XMLRPC client and server support +from PHP code. It's a modern replacement for the old +(removed from core since PHP 8.0) xmlrpc extension. + +To update: +- Pick a release of the library @ https://github.com/gggeek/phpxmlrpc/releases. +- Download or checkout it. +- Delete the contents on the lib/phpxmlrpc directory (but this file) completely. +- Copy the /src directory contents of the library to lib/phpxmlrpc. +- Edit this file and update the release and commit details below. +- Edit lib/thirdpartylibs.xml and update the information details too. + +Current version imported: 4.8.0 (39e37ed) + +Local changes: + * readme_moodle.txt - this file ;-) diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml index c87c766eb56..f460c94a198 100644 --- a/lib/thirdpartylibs.xml +++ b/lib/thirdpartylibs.xml @@ -614,4 +614,17 @@ All rights reserved. MIT https://github.com/php-fig/http-message + + phpxmlrpc + phpxmlrpc + A php library for building xml-rpc clients and servers + 4.8.0 + BSD + 3-Clause + https://github.com/gggeek/phpxmlrpc + + 1999,2000,2001 Edd Dumbill, Useful Information Company + + +