Completed
Push — master ( f62d1a...5fc0c8 )
by Gaetano
9s
created

proxy.php ➔ forward_request()   C

Complexity

Conditions 10
Paths 4

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 10
eloc 32
nc 4
nop 1
dl 0
loc 51
rs 6
c 4
b 1
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
);
89