3 * XMLRPC server acting as proxy for requests to other servers
4 * (useful e.g. for ajax-originated calls that can only connect back to
5 * the originating server).
7 * @author Gaetano Giunta
8 * @copyright (C) 2006-2020 G. Giunta
9 * @license code licensed under the BSD License: see file license.txt
12 require_once __DIR__ . "/_bootstrap.php";
15 * Forward an xmlrpc request to another server, and return to client the response received.
17 * DO NOT RUN AS IS IN PRODUCTION - this is an open relay !!!
19 * @param PhpXmlRpc\Request $req (see method docs below for a description of the expected parameters)
21 * @return PhpXmlRpc\Response
23 function forward_request($req)
25 $encoder = new \PhpXmlRpc\Encoder();
29 $url = $encoder->decode($req->getParam(0));
30 $client = new PhpXmlRpc\Client($url);
32 if ($req->getNumParams() > 3) {
33 // we have to set some options onto the client.
34 // Note that if we do not untaint the received values, warnings might be generated...
35 $options = $encoder->decode($req->getParam(3));
36 foreach ($options as $key => $val) {
42 case 'RequestCompression':
43 $client->setRequestCompression($val);
46 $client->setSSLVerifyHost($val);
49 $client->setSSLVerifyPeer($val);
52 $timeout = (integer)$val;
58 // build call for remote server
59 /// @todo find a way to forward client info (such as IP) to server, either
60 /// - as xml comments in the payload, or
61 /// - using std http header conventions, such as X-forwarded-for...
62 $reqMethod = $encoder->decode($req->getParam(1));
63 $pars = $req->getParam(2);
64 $req = new PhpXmlRpc\Request($reqMethod);
65 foreach ($pars as $par) {
69 // add debug info into response we give back to caller
70 PhpXmlRpc\Server::xmlrpc_debugmsg("Sending to server $url the payload: " . $req->serialize());
72 return $client->send($req, $timeout);
76 // NB: take care not to output anything else after this call, as it will mess up the responses and it will be hard to
77 // debug. In case you have to do so, at least re-emit a correct Content-Length http header (requires output buffering)
78 $server = new PhpXmlRpc\Server(
80 'xmlrpcproxy.call' => array(
81 'function' => 'forward_request',
83 array('mixed', 'string', 'string', 'array'),
84 array('mixed', 'string', 'string', 'array', 'struct'),
86 'docstring' => 'forwards xmlrpc calls to remote servers. Returns remote method\'s response. Accepts params: remote server url (might include basic auth credentials), method name, array of params, and (optionally) a struct containing call options',