Completed
Push — master ( 87b7a4...6ce28d )
by Gaetano
11:11 queued 06:38
created

proxy.php ➔ forward_request()   B

Complexity

Conditions 10
Paths 4

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
nc 4
nop 1
dl 0
loc 51
ccs 0
cts 37
cp 0
crap 110
rs 7.2024
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2019 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
);
89