Add 'php/phpxmlrpc/' from commit 'cd5dbb4a511e7a616a61187a5de1a611a9748cbd'
[plcapi.git] / php / phpxmlrpc / 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  * DO NOT RUN AS IS IN PRODUCTION - this is an open relay !!!
19  *
20  * @param PhpXmlRpc\Request $req (see method docs below for a description of the expected parameters)
21  *
22  * @return PhpXmlRpc\Response
23  */
24 function forward_request($req)
25 {
26     $encoder = new \PhpXmlRpc\Encoder();
27
28     // create client
29     $timeout = 0;
30     $url = $encoder->decode($req->getParam(0));
31     $client = new PhpXmlRpc\Client($url);
32
33     if ($req->getNumParams() > 3) {
34         // we have to set some options onto the client.
35         // Note that if we do not untaint the received values, warnings might be generated...
36         $options = $encoder->decode($req->getParam(3));
37         foreach ($options as $key => $val) {
38             switch ($key) {
39                 case 'Cookie':
40                     break;
41                 case 'Credentials':
42                     break;
43                 case 'RequestCompression':
44                     $client->setRequestCompression($val);
45                     break;
46                 case 'SSLVerifyHost':
47                     $client->setSSLVerifyHost($val);
48                     break;
49                 case 'SSLVerifyPeer':
50                     $client->setSSLVerifyPeer($val);
51                     break;
52                 case 'Timeout':
53                     $timeout = (integer)$val;
54                     break;
55             } // switch
56         }
57     }
58
59     // build call for remote server
60     /// @todo find a way to forward client info (such as IP) to server, either
61     /// - as xml comments in the payload, or
62     /// - using std http header conventions, such as X-forwarded-for...
63     $reqMethod = $encoder->decode($req->getParam(1));
64     $pars = $req->getParam(2);
65     $req = new PhpXmlRpc\Request($reqMethod);
66     foreach ($pars as $par) {
67         $req->addParam($par);
68     }
69
70     // add debug info into response we give back to caller
71     PhpXmlRpc\Server::xmlrpc_debugmsg("Sending to server $url the payload: " . $req->serialize());
72
73     return $client->send($req, $timeout);
74 }
75
76 // run the server
77 $server = new PhpXmlRpc\Server(
78     array(
79         'xmlrpcproxy.call' => array(
80             'function' => 'forward_request',
81             'signature' => array(
82                 array('mixed', 'string', 'string', 'array'),
83                 array('mixed', 'string', 'string', 'array', 'struct'),
84             ),
85             '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',
86         ),
87     )
88 );