Passed
Push — master ( 18eded...8ccda3 )
by Gaetano
12:44
created

sleepSeconds()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Defines functions and signatures which can be registered as methods exposed by an XML-RPC Server.
4
 *
5
 * To use this, use something akin to:
6
 * $signatures = include('tests.php');
7
 * NB: requires 'functions.php' to be included first
8
 *
9
 * Methods used by the phpxmlrpc testsuite
10
 */
11
12
use PhpXmlRpc\Encoder;
13
use PhpXmlRpc\Response;
14
use PhpXmlRpc\Value;
15
16
$getallheaders_sig = array(array(Value::$xmlrpcStruct));
17
$getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS';
18
function getAllHeaders_xmlrpc($req)
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
function getAllHeaders_xmlrpc(/** @scrutinizer ignore-unused */ $req)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
{
20
    if (function_exists('getallheaders')) {
21
        $headers = getallheaders();
22
    } else {
23
        // poor man's version of getallheaders. Thanks ralouphie/getallheaders
24
        $headers = array();
25
        $copy_server = array(
26
            'CONTENT_TYPE'   => 'Content-Type',
27
            'CONTENT_LENGTH' => 'Content-Length',
28
            'CONTENT_MD5'    => 'Content-Md5',
29
        );
30
        foreach ($_SERVER as $key => $value) {
31
            if (substr($key, 0, 5) === 'HTTP_') {
32
                $key = substr($key, 5);
33
                if (!isset($copy_server[$key]) || !isset($_SERVER[$key])) {
34
                    $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key))));
35
                    $headers[$key] = $value;
36
                }
37
            } elseif (isset($copy_server[$key])) {
38
                $headers[$copy_server[$key]] = $value;
39
            }
40
        }
41
        if (!isset($headers['Authorization'])) {
42
            if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
43
                $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
44
            } elseif (isset($_SERVER['PHP_AUTH_USER'])) {
45
                $basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
46
                $headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass);
47
            } elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
48
                $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
49
            }
50
        }
51
    }
52
53
    $encoder = new Encoder();
54
    return new Response($encoder->encode($headers));
55
}
56
57
// used to test mixed-convention calling
58
$setcookies_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
59
$setcookies_doc = 'Sends to client a response containing a single \'1\' digit, and sets to it http cookies as received in the request (array of structs describing a cookie)';
60
function setCookies($cookies)
61
{
62
    foreach ($cookies as $name => $cookieDesc) {
63
        if (is_array($cookieDesc)) {
64
            setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']);
65
        } else {
66
            /// @todo
67
        }
68
    }
69
70
    return 1;
71
}
72
73
$getcookies_sig = array(array(Value::$xmlrpcStruct));
74
$getcookies_doc = 'Sends to client a response containing all http cookies as received in the request (as struct)';
75
function getCookies($req)
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

75
function getCookies(/** @scrutinizer ignore-unused */ $req)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
{
77
    $encoder = new Encoder();
78
    return new Response($encoder->encode($_COOKIE));
79
}
80
81
// used to test signatures with NULL params
82
$findstate12_sig = array(
83
    array(Value::$xmlrpcString, Value::$xmlrpcInt, Value::$xmlrpcNull),
84
    array(Value::$xmlrpcString, Value::$xmlrpcNull, Value::$xmlrpcInt),
85
);
86
function findStateWithNulls($req)
87
{
88
    $a = $req->getParam(0);
89
    $b = $req->getParam(1);
90
91
    if ($a->scalarTyp() == Value::$xmlrpcNull)
92
        return new Response(new Value(plain_findstate($b->scalarVal())));
93
    else
94
        return new Response(new Value(plain_findstate($a->scalarVal())));
95
}
96
97
$sleep_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt));
98
$sleep_doc = 'Sleeps for the requested number of seconds (between 1 and 60), before sending back the response';
99
function sleepSeconds($secs) {
100
    if ($secs > 0 && $secs < 61) {
101
        sleep($secs);
102
    }
103
    return $secs;
104
}
105
106
return array(
107
    "tests.getallheaders" => array(
108
        "function" => 'getAllHeaders_xmlrpc',
109
        "signature" => $getallheaders_sig,
110
        "docstring" => $getallheaders_doc,
111
    ),
112
    "tests.setcookies" => array(
113
        "function" => 'setCookies',
114
        "signature" => $setcookies_sig,
115
        "docstring" => $setcookies_doc,
116
        "parameters_type" => 'phpvals',
117
    ),
118
    "tests.getcookies" => array(
119
        "function" => 'getCookies',
120
        "signature" => $getcookies_sig,
121
        "docstring" => $getcookies_doc,
122
    ),
123
124
    // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
125
    // NB: we can only register this when setting internal encoding to UTF-8, or it will break system.listMethods
126
    "tests.utf8methodname." . 'κόσμε' => array(
127
        "function" => "exampleMethods::stringEcho",
128
        "signature" => exampleMethods::$stringecho_sig,
129
        "docstring" => exampleMethods::$stringecho_doc,
130
    ),
131
    /*"tests.iso88591methodname." . chr(224) . chr(252) . chr(232) => array(
132
        "function" => "stringEcho",
133
        "signature" => $stringecho_sig,
134
        "docstring" => $stringecho_doc,
135
    ),*/
136
137
    'tests.getStateName.12' => array(
138
        "function" => "findStateWithNulls",
139
        "signature" => $findstate12_sig,
140
        "docstring" => exampleMethods::$findstate_doc,
141
    ),
142
143
    'tests.sleep' => array(
144
        "function" => 'sleepSeconds',
145
        "signature" => $sleep_sig,
146
        "docstring" => $sleep_doc,
147
        "parameters_type" => 'phpvals',
148
    ),
149
);
150