improve code coverage for demo files
[plcapi.git] / demo / server / proxy.php
1 <?php
2 /**
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).
6  *
7  * @author Gaetano Giunta
8  * @copyright (C) 2006-2020 G. Giunta
9  * @license code licensed under the BSD License: see file license.txt
10  */
11
12 require_once __DIR__ . "/_prepend.php";
13
14 /**
15  * Forward an xmlrpc request to another server, and return to client the response received.
16  *
17  * DO NOT RUN AS IS IN PRODUCTION - this is an open relay !!!
18  *
19  * @param PhpXmlRpc\Request $req (see method docs below for a description of the expected parameters)
20  *
21  * @return PhpXmlRpc\Response
22  */
23 function forward_request($req)
24 {
25     $encoder = new \PhpXmlRpc\Encoder();
26
27     // create client
28     $timeout = 0;
29     $url = $encoder->decode($req->getParam(0));
30     $client = new PhpXmlRpc\Client($url);
31
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) {
37             switch ($key) {
38                 case 'Cookie':
39                     break;
40                 case 'Credentials':
41                     break;
42                 case 'RequestCompression':
43                     $client->setRequestCompression($val);
44                     break;
45                 case 'SSLVerifyHost':
46                     $client->setSSLVerifyHost($val);
47                     break;
48                 case 'SSLVerifyPeer':
49                     $client->setSSLVerifyPeer($val);
50                     break;
51                 case 'Timeout':
52                     $timeout = (integer)$val;
53                     break;
54             } // switch
55         }
56     }
57
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) {
66         $req->addParam($par);
67     }
68
69     // add debug info into response we give back to caller
70     PhpXmlRpc\Server::xmlrpc_debugmsg("Sending to server $url the payload: " . $req->serialize());
71
72     return $client->send($req, $timeout);
73 }
74
75 // run the server
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(
79     array(
80         'xmlrpcproxy.call' => array(
81             'function' => 'forward_request',
82             'signature' => array(
83                 array('mixed', 'string', 'string', 'array'),
84                 array('mixed', 'string', 'string', 'array', 'struct'),
85             ),
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',
87         ),
88     )
89 );
90
91 require_once __DIR__ . "/_append.php";