4f860de187d3d95e047c10c9f2715ce6d088793d
[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-2015 G. Giunta
9  * @license code licensed under the BSD License: see file license.txt
10  */
11
12 include_once __DIR__ . "/../../src/Autoloader.php";
13 PhpXmlRpc\Autoloader::register();
14
15 /**
16  * Forward an xmlrpc request to another server, and return to client the response received.
17  *
18  * @param PhpXmlRpc\Request $req (see method docs below for a description of the expected parameters)
19  *
20  * @return PhpXmlRpc\Response
21  */
22 function forward_request($req)
23 {
24     $encoder = new \PhpXmlRpc\Encoder();
25
26     // create client
27     $timeout = 0;
28     $url = $encoder->decode($req->getParam(0));
29     $client = new PhpXmlRpc\Client($url);
30
31     if ($req->getNumParams() > 3) {
32         // we have to set some options onto the client.
33         // Note that if we do not untaint the received values, warnings might be generated...
34         $options = $encoder->decode($req->getParam(3));
35         foreach ($options as $key => $val) {
36             switch ($key) {
37                 case 'Cookie':
38                     break;
39                 case 'Credentials':
40                     break;
41                 case 'RequestCompression':
42                     $client->setRequestCompression($val);
43                     break;
44                 case 'SSLVerifyHost':
45                     $client->setSSLVerifyHost($val);
46                     break;
47                 case 'SSLVerifyPeer':
48                     $client->setSSLVerifyPeer($val);
49                     break;
50                 case 'Timeout':
51                     $timeout = (integer)$val;
52                     break;
53             } // switch
54         }
55     }
56
57     // build call for remote server
58     /// @todo find a way to forward client info (such as IP) to server, either
59     /// - as xml comments in the payload, or
60     /// - using std http header conventions, such as X-forwarded-for...
61     $reqethod = $encoder->decode($req->getParam(1));
62     $pars = $req->getParam(2);
63     $req = new PhpXmlRpc\Request($reqethod);
64     for ($i = 0; $i < $pars->arraySize(); $i++) {
65         $req->addParam($pars->arraymem($i));
66     }
67
68     // add debug info into response we give back to caller
69     PhpXmlRpc\Server::xmlrpc_debugmsg("Sending to server $url the payload: " . $req->serialize());
70
71     return $client->send($req, $timeout);
72 }
73
74 // run the server
75 $server = new PhpXmlRpc\Server(
76     array(
77         'xmlrpcproxy.call' => array(
78             'function' => 'forward_request',
79             'signature' => array(
80                 array('mixed', 'string', 'string', 'array'),
81                 array('mixed', 'string', 'string', 'array', 'stuct'),
82             ),
83             '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',
84         ),
85     )
86 );