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