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