Passed
Push — master ( e665f3...5d18c7 )
by Gaetano
07:54
created

PhpXmlRpc::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 2
rs 9.9666
1
<?php
2
3
namespace PhpXmlRpc;
4
5
use PhpXmlRpc\Helper\Charset;
6
use PhpXmlRpc\Helper\Http;
7
use PhpXmlRpc\Helper\XMLParser;
8
9
/**
10
 * Manages global configuration for operation of the library.
11
 */
12
class PhpXmlRpc
13
{
14
    /**
15
     * @var int[]
16
     */
17
    static public $xmlrpcerr = array(
18
        'unknown_method' => 1,
19
        /// @deprecated. left in for BC
20
        'invalid_return' => 2,
21
        'incorrect_params' => 3,
22
        'introspect_unknown' => 4,
23
        'http_error' => 5,
24
        'no_data' => 6,
25
        'no_ssl' => 7,
26
        'curl_fail' => 8,
27
        'invalid_request' => 15,
28
        'no_curl' => 16,
29
        'server_error' => 17,
30
        'multicall_error' => 18,
31
        'multicall_notstruct' => 9,
32
        'multicall_nomethod' => 10,
33
        'multicall_notstring' => 11,
34
        'multicall_recursion' => 12,
35
        'multicall_noparams' => 13,
36
        'multicall_notarray' => 14,
37
        'no_http2' => 19,
38
        // the following 3 are meant to give greater insight than 'invalid_return'. They use the same code for BC,
39
        // but you can override their value in your own code
40
        'invalid_xml' => 2,
41
        'xml_not_compliant' => 2,
42
        'xml_parsing_error' => 2,
43
44
        /// @todo verify: can these conflict with $xmlrpcerrxml?
45
        'cannot_decompress' => 103,
46
        'decompress_fail' => 104,
47
        'dechunk_fail' => 105,
48
        'server_cannot_decompress' => 106,
49
        'server_decompress_fail' => 107,
50
    );
51
52
    /**
53
     * @var string[]
54
     */
55
    static public $xmlrpcstr = array(
56
        'unknown_method' => 'Unknown method',
57
        /// @deprecated. left in for BC
58
        'invalid_return' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
59
        'incorrect_params' => 'Incorrect parameters passed to method',
60
        'introspect_unknown' => "Can't introspect: method unknown",
61
        'http_error' => "Didn't receive 200 OK from remote server",
62
        'no_data' => 'No data received from server',
63
        'no_ssl' => 'No SSL support compiled in',
64
        'curl_fail' => 'CURL error',
65
        'invalid_request' => 'Invalid request payload',
66
        'no_curl' => 'No CURL support compiled in',
67
        'server_error' => 'Internal server error',
68
        'multicall_error' => 'Received from server invalid multicall response',
69
        'multicall_notstruct' => 'system.multicall expected struct',
70
        'multicall_nomethod' => 'Missing methodName',
71
        'multicall_notstring' => 'methodName is not a string',
72
        'multicall_recursion' => 'Recursive system.multicall forbidden',
73
        'multicall_noparams' => 'Missing params',
74
        'multicall_notarray' => 'params is not an array',
75
        'no_http2' => 'No HTTP/2 support compiled in',
76
        // the following 3 are meant to give greater insight than 'invalid_return'. They use the same string for BC,
77
        // but you can override their value in your own code
78
        'invalid_xml' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
79
        'xml_not_compliant' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
80
        'xml_parsing_error' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)',
81
82
        'cannot_decompress' => 'Received from server compressed HTTP and cannot decompress',
83
        'decompress_fail' => 'Received from server invalid compressed HTTP',
84
        'dechunk_fail' => 'Received from server invalid chunked HTTP',
85
        'server_cannot_decompress' => 'Received from client compressed HTTP request and cannot decompress',
86
        'server_decompress_fail' => 'Received from client invalid compressed HTTP request',
87
    );
88
89
    /**
90
     * @var string
91
     * The charset encoding used by the server for received requests and by the client for received responses when
92
     * received charset cannot be determined and mbstring extension is not enabled.
93
     */
94
    public static $xmlrpc_defencoding = "UTF-8";
95
96
    /**
97
     * @var string[]
98
     * The list of preferred encodings used by the server for requests and by the client for responses to detect the
99
     * charset of the received payload when
100
     * - the charset cannot be determined by looking at http headers, xml declaration or BOM
101
     * - mbstring extension is enabled
102
     */
103
    public static $xmlrpc_detectencodings = array();
104
105
    /**
106
     * @var string
107
     * The encoding used internally by PHP.
108
     * String values received as xml will be converted to this, and php strings will be converted to xml as if
109
     * having been coded with this.
110
     * Valid also when defining names of xml-rpc methods
111
     */
112
    public static $xmlrpc_internalencoding = "UTF-8";
113
114
    /**
115
     * @var string
116
     */
117
    public static $xmlrpcName = "XML-RPC for PHP";
118
    /**
119
     * @var string
120
     */
121
    public static $xmlrpcVersion = "4.9.5";
122
123
    /**
124
     * @var int
125
     * Let user errors start at 800
126
     */
127
    public static $xmlrpcerruser = 800;
128
    /**
129
     * @var int
130
     * Let XML parse errors start at 100
131
     */
132
    public static $xmlrpcerrxml = 100;
133
134
    /**
135
     * @var bool
136
     * Set to TRUE to enable correct decoding of <NIL/> and <EX:NIL/> values
137
     */
138
    public static $xmlrpc_null_extension = false;
139
140
    /**
141
     * @var bool
142
     * Set to TRUE to make the library use DateTime objects instead of strings for all values parsed from incoming XML.
143 2
     * NB: if the received strings are not parseable as dates, NULL will be returned. To prevent that, enable as
144
     * well `xmlrpc_reject_invalid_values`, so that invalid dates will be rejected by the library
145 2
     */
146 2
    public static $xmlrpc_return_datetimes = false;
147 2
148 2
    /**
149 2
     * @var bool
150
     * Set to TRUE to make the library reject incoming xml which uses invalid data for xml-rpc elements, such
151
     * as base64 strings which can not be decoded, dateTime strings which do not represent a valid date, invalid bools,
152 2
     * floats and integers, method names with forbidden characters, or struct members missing the value or name
153
     */
154
    public static $xmlrpc_reject_invalid_values = false;
155
156
    /**
157
     * @var bool
158
     * Set to TRUE to enable encoding of php NULL values to <EX:NIL/> instead of <NIL/>
159
     */
160
    public static $xmlrpc_null_apache_encoding = false;
161
162
    public static $xmlrpc_null_apache_encoding_ns = "http://ws.apache.org/xmlrpc/namespaces/extensions";
163
164
    /**
165
     * @var int
166
     * Number of decimal digits used to serialize Double values.
167
     * @todo rename :'-(
168
     */
169
    public static $xmlpc_double_precision = 128;
170
171
    /**
172
     * @var string
173
     * Used to validate received date values. Alter this if the server/client you are communicating with uses date
174
     * formats non-conformant with the spec
175
     * NB: atm, the Date helper uses this regexp and expects to find matches in a specific order
176
     */
177
    public static $xmlrpc_datetime_format = '/^([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]|60)$/';
178
179
    /**
180
     * @var string
181
     * Used to validate received integer values. Alter this if the server/client you are communicating with uses
182
     * formats non-conformant with the spec.
183
     * We keep in spaces for BC, even though they are forbidden by the spec.
184
     * NB: the string should not match any data which php can not successfully cast to an integer
185
     */
186
    public static $xmlrpc_int_format = '/^[ \t]*[+-]?[0-9]+[ \t]*$/';
187
188
    /**
189
     * @var string
190
     * Used to validate received double values. Alter this if the server/client you are communicating with uses
191
     * formats non-conformant with the spec, e.g. with leading/trailing spaces/tabs/newlines.
192
     * We keep in spaces for BC, even though they are forbidden by the spec.
193
     * NB: the string should not match any data which php can not successfully cast to a float
194
     */
195
    public static $xmlrpc_double_format = '/^[ \t]*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?[ \t]*$/';
196
197
    /**
198
     * @var string
199
     * Used to validate received methodname values.
200
     * According to the spec: "The string may only contain identifier characters, upper and lower-case A-Z, the numeric
201
     * characters, 0-9, underscore, dot, colon and slash".
202
     * We keep in spaces for BC, even though they are forbidden by the spec.
203
     */
204
    public static $xmlrpc_methodname_format = '|^[ \t]*[a-zA-Z0-9_.:/]+[ \t]*$|';
205
206
    /// @todo review - should we use the range -32099 .. -32000 for some server erors?
207
    public static $xmlrpcerr_interop = array(
208
        'unknown_method' => -32601,
209
        'invalid_return' => 2,
210
        'incorrect_params' => -32602,
211
        'introspect_unknown' => -32601, // this shares the same code but has a separate meaning from 'unknown_method'...
212
        'http_error' => 32300,
213
        'no_data' => -32700,
214
        'no_ssl' => -32400,
215
        'curl_fail' => -32400,
216
        'invalid_request' => -32600,
217
        'no_curl' => -32400,
218
        'server_error' => -32500,
219
        'multicall_error' => -32700,
220
        'multicall_notstruct' => -32600,
221
        'multicall_nomethod' => -32601,
222
        'multicall_notstring' => -32600,
223
        'multicall_recursion' => -32603,
224
        'multicall_noparams' => -32602,
225
        'multicall_notarray' => -32600,
226
        'no_http2' => -32400,
227
        'invalid_xml' => -32700,
228
        'xml_not_compliant' => -32700,
229
        'xml_parsing_error' => -32700,
230
        'cannot_decompress' => -32400,
231
        'decompress_fail' => -32300,
232
        'dechunk_fail' => -32300,
233
        'server_cannot_decompress' => -32300,
234
        'server_decompress_fail' => -32300,
235
    );
236
237
    /**
238
     * A function to be used for compatibility with legacy code: it creates all global variables which used to be declared,
239
     * such as library version etc...
240
     * @return void
241
     */
242
    public static function exportGlobals()
243
    {
244
        $reflection = new \ReflectionClass('PhpXmlRpc\PhpXmlRpc');
245
        foreach ($reflection->getStaticProperties() as $name => $value) {
246
            $GLOBALS[$name] = $value;
247
        }
248
249
        // NB: all the variables exported into the global namespace below here do NOT guarantee 100% compatibility,
250
        // as they are NOT reimported back during calls to importGlobals()
251
252
        $reflection = new \ReflectionClass('PhpXmlRpc\Value');
253
        foreach ($reflection->getStaticProperties() as $name => $value) {
254
            if (!in_array($name, array('logger', 'charsetEncoder'))) {
255
                $GLOBALS[$name] = $value;
256
            }
257
        }
258
259
        $parser = new Helper\XMLParser();
260
        $reflection = new \ReflectionClass('PhpXmlRpc\Helper\XMLParser');
261
        foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $name => $value) {
262
            if (in_array($value->getName(), array('xmlrpc_valid_parents')))
263
            {
264
                $GLOBALS[$value->getName()] = $value->getValue($parser);
265
            }
266
        }
267
268
        $charset = Charset::instance();
269
        $GLOBALS['xml_iso88591_Entities'] = $charset->getEntities('iso88591');
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Helper\Charset::getEntities() has been deprecated. ( Ignorable by Annotation )

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

269
        $GLOBALS['xml_iso88591_Entities'] = /** @scrutinizer ignore-deprecated */ $charset->getEntities('iso88591');
Loading history...
270
    }
271
272
    /**
273
     * A function to be used for compatibility with legacy code: it gets the values of all global variables which used
274
     * to be declared, such as library version etc... and sets them to php classes.
275
     * It should be used by code which changed the values of those global variables to alter the working of the library.
276
     * Example code:
277
     * 1. include xmlrpc.inc
278
     * 2. set the values, e.g. $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
279
     * 3. import them: PhpXmlRpc\PhpXmlRpc::importGlobals();
280
     * 4. run your own code.
281
     *
282
     * @return void
283
     */
284
    public static function importGlobals()
285
    {
286
        $reflection = new \ReflectionClass('PhpXmlRpc\PhpXmlRpc');
287
        $staticProperties = $reflection->getStaticProperties();
288
        foreach ($staticProperties as $name => $value) {
289
            if (isset($GLOBALS[$name])) {
290
                self::$$name = $GLOBALS[$name];
291
            }
292
        }
293
    }
294
295
    /**
296
     * Inject a logger into all classes of the PhpXmlRpc library which use one
297
     *
298
     * @param $logger
299
     * @return void
300
     */
301
    public static function setLogger($logger)
302
    {
303
        Charset::setLogger($logger);
304
        Client::setLogger($logger);
305
        Encoder::setLogger($logger);
306
        Http::setLogger($logger);
307
        Request::setLogger($logger);
308
        Server::setLogger($logger);
309
        Value::setLogger($logger);
310
        Wrapper::setLogger($logger);
311
        XMLParser::setLogger($logger);
312
    }
313
314
    /**
315
     * Makes the library use the error codes detailed at https://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
316
     *
317
     * @return void
318
     *
319
     * @tofo feature creep - allow switching back to the original set of codes; querying the current mode
320
     */
321
    public static function useInteropFaults()
322
    {
323
        self::$xmlrpcerr = self::$xmlrpcerr_interop;
324
325
        self::$xmlrpcerruser = -32000;
326
    }
327
}
328