Issues (323)

demo/server/methodProviders/testsuite.php (2 issues)

Severity
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
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,
65
                isset($cookieDesc['value']) ? (string)$cookieDesc['value'] : '',
66
                isset($cookieDesc['expires']) ? $cookieDesc['expires'] : 0,
67
                isset($cookieDesc['path']) ? (string)$cookieDesc['path'] : '',
68
                isset($cookieDesc['domain']) ? (string)$cookieDesc['domain'] : '',
69
                isset($cookieDesc['secure']) ? (bool)$cookieDesc['secure'] : false,
70
                isset($cookieDesc['httponly']) ? (bool)$cookieDesc['httponly'] : false
71
            );
72
        } else {
73
            /// @todo what to do?
74
        }
75
    }
76
77
    return 1;
78
}
79
80
$getcookies_sig = array(array(Value::$xmlrpcStruct));
81
$getcookies_doc = 'Sends to client a response containing all http cookies as received in the request (as struct)';
82
function getCookies($req)
0 ignored issues
show
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

82
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...
83
{
84
    $encoder = new Encoder();
85
    return new Response($encoder->encode($_COOKIE));
86
}
87
88
// used to test signatures with NULL params
89
$findstate12_sig = array(
90
    array(Value::$xmlrpcString, Value::$xmlrpcInt, Value::$xmlrpcNull),
91
    array(Value::$xmlrpcString, Value::$xmlrpcNull, Value::$xmlrpcInt),
92
);
93
function findStateWithNulls($req)
94
{
95
    $a = $req->getParam(0);
96
    $b = $req->getParam(1);
97
98
    if ($a->scalarTyp() == Value::$xmlrpcNull)
99
        return new Response(new Value(plain_findstate($b->scalarVal())));
100
    else
101
        return new Response(new Value(plain_findstate($a->scalarVal())));
102
}
103
104
$sleep_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt));
105
$sleep_doc = 'Sleeps for the requested number of seconds (between 1 and 60), before sending back the response';
106
function sleepSeconds($secs) {
107
    if ($secs > 0 && $secs < 61) {
108
        sleep($secs);
109
    }
110
    return $secs;
111
}
112
113
$hashttp2_sig = array(array(Value::$xmlrpcBoolean));
114
$hashttp2_doc = 'Checks whether the server supports http2';
115
function hasHTTP2() {
116
    // NB: only works for apache2 on debian/ubuntu, that we know of...!
117
    return is_file('/etc/apache2/mods-enabled/http2.load');
118
}
119
120
return array(
121
    "tests.getallheaders" => array(
122
        "function" => 'getAllHeaders_xmlrpc',
123
        "signature" => $getallheaders_sig,
124
        "docstring" => $getallheaders_doc,
125
    ),
126
    "tests.setcookies" => array(
127
        "function" => 'setCookies',
128
        "signature" => $setcookies_sig,
129
        "docstring" => $setcookies_doc,
130
        "parameters_type" => 'phpvals',
131
    ),
132
    "tests.getcookies" => array(
133
        "function" => 'getCookies',
134
        "signature" => $getcookies_sig,
135
        "docstring" => $getcookies_doc,
136
    ),
137
138
    // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
139
    // NB: we can only register this when setting internal encoding to UTF-8, or it will break system.listMethods
140
    "tests.utf8methodname." . 'κόσμε' => array(
141
        "function" => "exampleMethods::stringEcho",
142
        "signature" => exampleMethods::$stringecho_sig,
143
        "docstring" => exampleMethods::$stringecho_doc,
144
    ),
145
    /*"tests.iso88591methodname." . chr(224) . chr(252) . chr(232) => array(
146
        "function" => "stringEcho",
147
        "signature" => $stringecho_sig,
148
        "docstring" => $stringecho_doc,
149
    ),*/
150
151
    'tests.getStateName.12' => array(
152
        "function" => "findStateWithNulls",
153
        "signature" => $findstate12_sig,
154
        "docstring" => exampleMethods::$findstate_doc,
155
    ),
156
157
    'tests.sleep' => array(
158
        "function" => 'sleepSeconds',
159
        "signature" => $sleep_sig,
160
        "docstring" => $sleep_doc,
161
        "parameters_type" => 'phpvals',
162
    ),
163
164
    'tests.hasHTTP2' => array(
165
        "function" => 'hasHTTP2',
166
        "signature" => $hashttp2_sig,
167
        "docstring" => $hashttp2_doc,
168
        "parameters_type" => 'phpvals',
169
    ),
170
);
171