Passed
Push — master ( b9401f...4e5288 )
by Gaetano
06:45
created

Wrapper::php2XmlrpcType()   D

Complexity

Conditions 19
Paths 19

Size

Total Lines 36
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 23.156

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 19
eloc 32
c 3
b 0
f 0
nc 19
nop 1
dl 0
loc 36
ccs 24
cts 31
cp 0.7742
crap 23.156
rs 4.5166

How to fix   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
 * @author Gaetano Giunta
4
 * @copyright (C) 2006-2021 G. Giunta
5
 * @license code licensed under the BSD License: see file license.txt
6
 */
7
8
namespace PhpXmlRpc;
9
10
use PhpXmlRpc\Helper\Logger;
11
12
/**
13
 * PHP-XMLRPC "wrapper" class - generate stubs to transparently access xmlrpc methods as php functions and vice-versa.
14
 * Note: this class implements the PROXY pattern, but it is not named so to avoid confusion with http proxies.
15
 *
16
 * @todo use some better templating system for code generation?
17
 * @todo implement method wrapping with preservation of php objs in calls
18
 * @todo when wrapping methods without obj rebuilding, use return_type = 'phpvals' (faster)
19
 * @todo add support for 'epivals' mode
20
 * @todo allow setting custom namespace for generated wrapping code
21
 */
22
class Wrapper
23
{
24
    /// used to hold a reference to object instances whose methods get wrapped by wrapPhpFunction(), in 'create source' mode
25
    public static $objHolder = array();
26
27
    protected static $logger;
28
29 22
    public function getLogger()
30
    {
31 22
        if (self::$logger === null) {
32 1
            self::$logger = Logger::instance();
33
        }
34 22
        return self::$logger;
35
    }
36
37
    public static function setLogger($logger)
38
    {
39
        self::$logger = $logger;
40
    }
41
42
    /**
43
     * Given a string defining a php type or phpxmlrpc type (loosely defined: strings
44
     * accepted come from javadoc blocks), return corresponding phpxmlrpc type.
45
     * Notes:
46
     * - for php 'resource' types returns empty string, since resources cannot be serialized;
47
     * - for php class names returns 'struct', since php objects can be serialized as xmlrpc structs
48
     * - for php arrays always return array, even though arrays sometimes serialize as structs...
49
     * - for 'void' and 'null' returns 'undefined'
50
     *
51
     * @param string $phpType
52
     *
53
     * @return string
54
     *
55
     * @todo support notation `something[]` as 'array'
56
     */
57 507
    public function php2XmlrpcType($phpType)
58
    {
59 507
        switch (strtolower($phpType)) {
60 507
            case 'string':
61 507
                return Value::$xmlrpcString;
62 507
            case 'integer':
63 507
            case Value::$xmlrpcInt: // 'int'
64 507
            case Value::$xmlrpcI4:
65 507
            case Value::$xmlrpcI8:
66 507
                return Value::$xmlrpcInt;
67 507
            case Value::$xmlrpcDouble: // 'double'
68
                return Value::$xmlrpcDouble;
69 507
            case 'bool':
70 507
            case Value::$xmlrpcBoolean: // 'boolean'
71 507
            case 'false':
72 507
            case 'true':
73
                return Value::$xmlrpcBoolean;
74 507
            case Value::$xmlrpcArray: // 'array':
75 507
            case 'array[]';
76
                return Value::$xmlrpcArray;
77 507
            case 'object':
78 507
            case Value::$xmlrpcStruct: // 'struct'
79
                return Value::$xmlrpcStruct;
80 507
            case Value::$xmlrpcBase64:
81
                return Value::$xmlrpcBase64;
82 507
            case 'resource':
83
                return '';
84
            default:
85 507
                if (class_exists($phpType)) {
86 507
                    if (is_a($phpType, 'DateTimeInterface')) {
87
                        return Value::$xmlrpcDateTime;
88
                    }
89 507
                    return Value::$xmlrpcStruct;
90
                } else {
91
                    // unknown: might be any 'extended' xmlrpc type
92 507
                    return Value::$xmlrpcValue;
93
                }
94
        }
95
    }
96
97
    /**
98
     * Given a string defining a phpxmlrpc type return the corresponding php type.
99
     *
100
     * @param string $xmlrpcType
101
     *
102
     * @return string
103
     */
104 77
    public function xmlrpc2PhpType($xmlrpcType)
105
    {
106 77
        switch (strtolower($xmlrpcType)) {
107 77
            case 'base64':
108 77
            case 'datetime.iso8601':
109 77
            case 'string':
110 58
                return Value::$xmlrpcString;
111 77
            case 'int':
112 20
            case 'i4':
113 20
            case 'i8':
114 58
                return 'integer';
115 20
            case 'struct':
116 20
            case 'array':
117
                return 'array';
118 20
            case 'double':
119
                return 'float';
120 20
            case 'undefined':
121 20
                return 'mixed';
122
            case 'boolean':
123
            case 'null':
124
            default:
125
                // unknown: might be any xmlrpc type
126
                return strtolower($xmlrpcType);
127
        }
128
    }
129
130
    /**
131
     * Given a user-defined PHP function, create a PHP 'wrapper' function that can
132
     * be exposed as xmlrpc method from an xmlrpc server object and called from remote
133
     * clients (as well as its corresponding signature info).
134
     *
135
     * Since php is a typeless language, to infer types of input and output parameters,
136
     * it relies on parsing the javadoc-style comment block associated with the given
137
     * function. Usage of xmlrpc native types (such as datetime.dateTime.iso8601 and base64)
138
     * in the '@param' tag is also allowed, if you need the php function to receive/send
139
     * data in that particular format (note that base64 encoding/decoding is transparently
140
     * carried out by the lib, while datetime vals are passed around as strings)
141
     *
142
     * Known limitations:
143
     * - only works for user-defined functions, not for PHP internal functions
144
     *   (reflection does not support retrieving number/type of params for those)
145
     * - functions returning php objects will generate special structs in xmlrpc responses:
146
     *   when the xmlrpc decoding of those responses is carried out by this same lib, using
147
     *   the appropriate param in php_xmlrpc_decode, the php objects will be rebuilt.
148
     *   In short: php objects can be serialized, too (except for their resource members),
149
     *   using this function.
150
     *   Other libs might choke on the very same xml that will be generated in this case
151
     *   (i.e. it has a nonstandard attribute on struct element tags)
152
     *
153
     * Note that since rel. 2.0RC3 the preferred method to have the server call 'standard'
154
     * php functions (ie. functions not expecting a single Request obj as parameter)
155
     * is by making use of the functions_parameters_type class member.
156
     *
157
     * @param callable $callable the PHP user function to be exposed as xmlrpc method/ a closure, function name, array($obj, 'methodname') or array('class', 'methodname') are ok
158
     * @param string $newFuncName (optional) name for function to be created. Used only when return_source in $extraOptions is true
159
     * @param array $extraOptions (optional) array of options for conversion. valid values include:
160
     *                            - bool return_source     when true, php code w. function definition will be returned, instead of a closure
161
     *                            - bool encode_php_objs   let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects
162
     *                            - bool decode_php_objs   --- WARNING !!! possible security hazard. only use it with trusted servers ---
163
     *                            - bool suppress_warnings remove from produced xml any warnings generated at runtime by the php function being invoked
164
     *
165
     * @return array|false false on error, or an array containing the name of the new php function,
166
     *                     its signature and docs, to be used in the server dispatch map
167
     *
168
     * @todo decide how to deal with params passed by ref in function definition: bomb out or allow?
169
     * @todo finish using phpdoc info to build method sig if all params are named but out of order
170
     * @todo add a check for params of 'resource' type
171
     * @todo add some trigger_errors / error_log when returning false?
172
     * @todo what to do when the PHP function returns NULL? We are currently returning an empty string value...
173
     * @todo add an option to suppress php warnings in invocation of user function, similar to server debug level 3?
174
     * @todo add a verbatim_object_copy parameter to allow avoiding usage the same obj instance?
175
     * @todo add an option to allow generated function to skip validation of number of parameters, as that is done by the server anyway
176
     */
177 507
    public function wrapPhpFunction($callable, $newFuncName = '', $extraOptions = array())
178
    {
179 507
        $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true;
180
181 507
        if (is_string($callable) && strpos($callable, '::') !== false) {
182 507
            $callable = explode('::', $callable);
183
        }
184 507
        if (is_array($callable)) {
185 507
            if (count($callable) < 2 || (!is_string($callable[0]) && !is_object($callable[0]))) {
186
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': syntax for function to be wrapped is wrong');
187
                return false;
188
            }
189 507
            if (is_string($callable[0])) {
190 507
                $plainFuncName = implode('::', $callable);
191 507
            } elseif (is_object($callable[0])) {
192 507
                $plainFuncName = get_class($callable[0]) . '->' . $callable[1];
193
            }
194 507
            $exists = method_exists($callable[0], $callable[1]);
195 507
        } else if ($callable instanceof \Closure) {
196
            // we do not support creating code which wraps closures, as php does not allow to serialize them
197 507
            if (!$buildIt) {
198
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': a closure can not be wrapped in generated source code');
199
                return false;
200
            }
201
202 507
            $plainFuncName = 'Closure';
203 507
            $exists = true;
204
        } else {
205 507
            $plainFuncName = $callable;
206 507
            $exists = function_exists($callable);
0 ignored issues
show
Bug introduced by
$callable of type callable is incompatible with the type string expected by parameter $function of function_exists(). ( Ignorable by Annotation )

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

206
            $exists = function_exists(/** @scrutinizer ignore-type */ $callable);
Loading history...
207
        }
208
209 507
        if (!$exists) {
210
            $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': function to be wrapped is not defined: ' . $plainFuncName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $plainFuncName does not seem to be defined for all execution paths leading up to this point.
Loading history...
211
            return false;
212
        }
213
214 507
        $funcDesc = $this->introspectFunction($callable, $plainFuncName);
215 507
        if (!$funcDesc) {
216
            return false;
217
        }
218
219 507
        $funcSigs = $this->buildMethodSignatures($funcDesc);
220
221 507
        if ($buildIt) {
222 507
            $callable = $this->buildWrapFunctionClosure($callable, $extraOptions, $plainFuncName, $funcDesc);
223
        } else {
224 507
            $newFuncName = $this->newFunctionName($callable, $newFuncName, $extraOptions);
225 507
            $code = $this->buildWrapFunctionSource($callable, $newFuncName, $extraOptions, $plainFuncName, $funcDesc);
226
        }
227
228
        $ret = array(
229 507
            'function' => $callable,
230 507
            'signature' => $funcSigs['sigs'],
231 507
            'docstring' => $funcDesc['desc'],
232 507
            'signature_docs' => $funcSigs['sigsDocs'],
233
        );
234 507
        if (!$buildIt) {
235 507
            $ret['function'] = $newFuncName;
236 507
            $ret['source'] = $code;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $code does not seem to be defined for all execution paths leading up to this point.
Loading history...
237
        }
238 507
        return $ret;
239
    }
240
241
    /**
242
     * Introspect a php callable and its phpdoc block and extract information about its signature
243
     *
244
     * @param callable $callable
245
     * @param string $plainFuncName
246
     * @return array|false
247
     */
248 507
    protected function introspectFunction($callable, $plainFuncName)
249
    {
250
        // start to introspect PHP code
251 507
        if (is_array($callable)) {
252 507
            $func = new \ReflectionMethod($callable[0], $callable[1]);
253 507
            if ($func->isPrivate()) {
254
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is private: ' . $plainFuncName);
255
                return false;
256
            }
257 507
            if ($func->isProtected()) {
258
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is protected: ' . $plainFuncName);
259
                return false;
260
            }
261 507
            if ($func->isConstructor()) {
262
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is the constructor: ' . $plainFuncName);
263
                return false;
264
            }
265 507
            if ($func->isDestructor()) {
266
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is the destructor: ' . $plainFuncName);
267
                return false;
268
            }
269 507
            if ($func->isAbstract()) {
270
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': method to be wrapped is abstract: ' . $plainFuncName);
271
                return false;
272
            }
273
            /// @todo add more checks for static vs. nonstatic?
274
        } else {
275 507
            $func = new \ReflectionFunction($callable);
276
        }
277 507
        if ($func->isInternal()) {
278
            // Note: from PHP 5.1.0 onward, we will possibly be able to use invokeargs
279
            // instead of getparameters to fully reflect internal php functions ?
280
            $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': function to be wrapped is internal: ' . $plainFuncName);
281
            return false;
282
        }
283
284
        // retrieve parameter names, types and description from javadoc comments
285
286
        // function description
287 507
        $desc = '';
288
        // type of return val: by default 'any'
289 507
        $returns = Value::$xmlrpcValue;
290
        // desc of return val
291 507
        $returnsDocs = '';
292
        // type + name of function parameters
293 507
        $paramDocs = array();
294
295 507
        $docs = $func->getDocComment();
296 507
        if ($docs != '') {
297 507
            $docs = explode("\n", $docs);
298 507
            $i = 0;
299 507
            foreach ($docs as $doc) {
300 507
                $doc = trim($doc, " \r\t/*");
301 507
                if (strlen($doc) && strpos($doc, '@') !== 0 && !$i) {
302 507
                    if ($desc) {
303 507
                        $desc .= "\n";
304
                    }
305 507
                    $desc .= $doc;
306 507
                } elseif (strpos($doc, '@param') === 0) {
307
                    // syntax: @param type $name [desc]
308 507
                    if (preg_match('/@param\s+(\S+)\s+(\$\S+)\s*(.+)?/', $doc, $matches)) {
309 507
                        $name = strtolower(trim($matches[2]));
310
                        //$paramDocs[$name]['name'] = trim($matches[2]);
311 507
                        $paramDocs[$name]['doc'] = isset($matches[3]) ? $matches[3] : '';
312 507
                        $paramDocs[$name]['type'] = $matches[1];
313
                    }
314 507
                    $i++;
315 507
                } elseif (strpos($doc, '@return') === 0) {
316
                    // syntax: @return type [desc]
317 507
                    if (preg_match('/@return\s+(\S+)(\s+.+)?/', $doc, $matches)) {
318 507
                        $returns = $matches[1];
319 507
                        if (isset($matches[2])) {
320 507
                            $returnsDocs = trim($matches[2]);
321
                        }
322
                    }
323
                }
324
            }
325
        }
326
327
        // execute introspection of actual function prototype
328 507
        $params = array();
329 507
        $i = 0;
330 507
        foreach ($func->getParameters() as $paramObj) {
331 507
            $params[$i] = array();
332 507
            $params[$i]['name'] = '$' . $paramObj->getName();
333 507
            $params[$i]['isoptional'] = $paramObj->isOptional();
334 507
            $i++;
335
        }
336
337
        return array(
338 507
            'desc' => $desc,
339 507
            'docs' => $docs,
340 507
            'params' => $params, // array, positionally indexed
341 507
            'paramDocs' => $paramDocs, // array, indexed by name
342 507
            'returns' => $returns,
343 507
            'returnsDocs' =>$returnsDocs,
344
        );
345
    }
346
347
    /**
348
     * Given the method description given by introspection, create method signature data
349
     *
350
     * @todo support better docs with multiple types separated by pipes by creating multiple signatures
351
     *       (this is questionable, as it might produce a big matrix of possible signatures with many such occurrences)
352
     *
353
     * @param array $funcDesc as generated by self::introspectFunction()
354
     *
355
     * @return array
356
     */
357 507
    protected function buildMethodSignatures($funcDesc)
358
    {
359 507
        $i = 0;
360 507
        $parsVariations = array();
361 507
        $pars = array();
362 507
        $pNum = count($funcDesc['params']);
363 507
        foreach ($funcDesc['params'] as $param) {
364
            /* // match by name real param and documented params
365
            $name = strtolower($param['name']);
366
            if (!isset($funcDesc['paramDocs'][$name])) {
367
                $funcDesc['paramDocs'][$name] = array();
368
            }
369
            if (!isset($funcDesc['paramDocs'][$name]['type'])) {
370
                $funcDesc['paramDocs'][$name]['type'] = 'mixed';
371
            }*/
372
373 507
            if ($param['isoptional']) {
374
                // this particular parameter is optional. save as valid previous list of parameters
375
                $parsVariations[] = $pars;
376
            }
377
378 507
            $pars[] = "\$p$i";
379 507
            $i++;
380 507
            if ($i == $pNum) {
381
                // last allowed parameters combination
382 507
                $parsVariations[] = $pars;
383
            }
384
        }
385
386 507
        if (count($parsVariations) == 0) {
387
            // only known good synopsis = no parameters
388 507
            $parsVariations[] = array();
389
        }
390
391 507
        $sigs = array();
392 507
        $sigsDocs = array();
393 507
        foreach ($parsVariations as $pars) {
394
            // build a signature
395 507
            $sig = array($this->php2XmlrpcType($funcDesc['returns']));
396 507
            $pSig = array($funcDesc['returnsDocs']);
397 507
            for ($i = 0; $i < count($pars); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
398 507
                $name = strtolower($funcDesc['params'][$i]['name']);
399 507
                if (isset($funcDesc['paramDocs'][$name]['type'])) {
400 507
                    $sig[] = $this->php2XmlrpcType($funcDesc['paramDocs'][$name]['type']);
401
                } else {
402 507
                    $sig[] = Value::$xmlrpcValue;
403
                }
404 507
                $pSig[] = isset($funcDesc['paramDocs'][$name]['doc']) ? $funcDesc['paramDocs'][$name]['doc'] : '';
405
            }
406 507
            $sigs[] = $sig;
407 507
            $sigsDocs[] = $pSig;
408
        }
409
410
        return array(
411 507
            'sigs' => $sigs,
412 507
            'sigsDocs' => $sigsDocs
413
        );
414
    }
415
416
    /**
417
     * Creates a closure that will execute $callable
418
     * @todo validate params? In theory all validation is left to the dispatch map...
419
     * @todo add support for $catchWarnings
420
     *
421
     * @param $callable
422
     * @param array $extraOptions
423
     * @param string $plainFuncName
424
     * @param array $funcDesc
425
     * @return \Closure
426
     */
427 507
    protected function buildWrapFunctionClosure($callable, $extraOptions, $plainFuncName, $funcDesc)
428
    {
429
        /**
430
         * @param Request $req
431
         * @return mixed
432
         */
433
        $function = function($req) use($callable, $extraOptions, $funcDesc)
434
        {
435 98
            $nameSpace = '\\PhpXmlRpc\\';
436 98
            $encoderClass = $nameSpace.'Encoder';
437 98
            $responseClass = $nameSpace.'Response';
438 98
            $valueClass = $nameSpace.'Value';
439
440
            // validate number of parameters received
441
            // this should be optional really, as we assume the server does the validation
442 98
            $minPars = count($funcDesc['params']);
443 98
            $maxPars = $minPars;
444 98
            foreach ($funcDesc['params'] as $i => $param) {
445 98
                if ($param['isoptional']) {
446
                    // this particular parameter is optional. We assume later ones are as well
447
                    $minPars = $i;
448
                    break;
449
                }
450
            }
451 98
            $numPars = $req->getNumParams();
452 98
            if ($numPars < $minPars || $numPars > $maxPars) {
453
                return new $responseClass(0, 3, 'Incorrect parameters passed to method');
454
            }
455
456 98
            $encoder = new $encoderClass();
457 98
            $options = array();
458 98
            if (isset($extraOptions['decode_php_objs']) && $extraOptions['decode_php_objs']) {
459
                $options[] = 'decode_php_objs';
460
            }
461 98
            $params = $encoder->decode($req, $options);
462
463 98
            $result = call_user_func_array($callable, $params);
464
465 98
            if (! is_a($result, $responseClass)) {
466 98
                if ($funcDesc['returns'] == Value::$xmlrpcDateTime || $funcDesc['returns'] == Value::$xmlrpcBase64) {
467
                    $result = new $valueClass($result, $funcDesc['returns']);
468
                } else {
469 98
                    $options = array();
470 98
                    if (isset($extraOptions['encode_php_objs']) && $extraOptions['encode_php_objs']) {
471 1
                        $options[] = 'encode_php_objs';
472
                    }
473
474 98
                    $result = $encoder->encode($result, $options);
475
                }
476 98
                $result = new $responseClass($result);
477
            }
478
479 98
            return $result;
480 507
        };
481
482 507
        return $function;
483
    }
484
485
    /**
486
     * Return a name for a new function, based on $callable, insuring its uniqueness
487
     * @param mixed $callable a php callable, or the name of an xmlrpc method
488
     * @param string $newFuncName when not empty, it is used instead of the calculated version
489
     * @return string
490
     */
491 583
    protected function newFunctionName($callable, $newFuncName, $extraOptions)
492
    {
493
        // determine name of new php function
494
495 583
        $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc';
496
497 583
        if ($newFuncName == '') {
498 564
            if (is_array($callable)) {
499 507
                if (is_string($callable[0])) {
500 507
                    $xmlrpcFuncName = "{$prefix}_" . implode('_', $callable);
501
                } else {
502 507
                    $xmlrpcFuncName = "{$prefix}_" . get_class($callable[0]) . '_' . $callable[1];
503
                }
504
            } else {
505 564
                if ($callable instanceof \Closure) {
506
                    $xmlrpcFuncName = "{$prefix}_closure";
507
                } else {
508 564
                    $callable = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'),
509 564
                        array('_', ''), $callable);
510 564
                    $xmlrpcFuncName = "{$prefix}_$callable";
511
                }
512
            }
513
        } else {
514 20
            $xmlrpcFuncName = $newFuncName;
515
        }
516
517 583
        while (function_exists($xmlrpcFuncName)) {
518 562
            $xmlrpcFuncName .= 'x';
519
        }
520
521 583
        return $xmlrpcFuncName;
522
    }
523
524
    /**
525
     * @param $callable
526
     * @param string $newFuncName
527
     * @param array $extraOptions
528
     * @param string $plainFuncName
529
     * @param array $funcDesc
530
     * @return string
531
     *
532
     * @todo add a nice phpdoc block in the generated source
533
     */
534 507
    protected function buildWrapFunctionSource($callable, $newFuncName, $extraOptions, $plainFuncName, $funcDesc)
535
    {
536 507
        $namespace = '\\PhpXmlRpc\\';
537
538 507
        $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false;
539 507
        $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false;
540 507
        $catchWarnings = isset($extraOptions['suppress_warnings']) && $extraOptions['suppress_warnings'] ? '@' : '';
541
542 507
        $i = 0;
543 507
        $parsVariations = array();
544 507
        $pars = array();
545 507
        $pNum = count($funcDesc['params']);
546 507
        foreach ($funcDesc['params'] as $param) {
547
548 507
            if ($param['isoptional']) {
549
                // this particular parameter is optional. save as valid previous list of parameters
550
                $parsVariations[] = $pars;
551
            }
552
553 507
            $pars[] = "\$p[$i]";
554 507
            $i++;
555 507
            if ($i == $pNum) {
556
                // last allowed parameters combination
557 507
                $parsVariations[] = $pars;
558
            }
559
        }
560
561 507
        if (count($parsVariations) == 0) {
562
            // only known good synopsis = no parameters
563
            $parsVariations[] = array();
564
            $minPars = 0;
565
            $maxPars = 0;
566
        } else {
567 507
            $minPars = count($parsVariations[0]);
568 507
            $maxPars = count($parsVariations[count($parsVariations)-1]);
569
        }
570
571
        // build body of new function
572
573 507
        $innerCode = "\$paramCount = \$req->getNumParams();\n";
574 507
        $innerCode .= "if (\$paramCount < $minPars || \$paramCount > $maxPars) return new {$namespace}Response(0, " . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . ", '" . PhpXmlRpc::$xmlrpcstr['incorrect_params'] . "');\n";
575
576 507
        $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
577 507
        if ($decodePhpObjects) {
578
            $innerCode .= "\$p = \$encoder->decode(\$req, array('decode_php_objs'));\n";
579
        } else {
580 507
            $innerCode .= "\$p = \$encoder->decode(\$req);\n";
581
        }
582
583
        // since we are building source code for later use, if we are given an object instance,
584
        // we go out of our way and store a pointer to it in a static class var...
585 507
        if (is_array($callable) && is_object($callable[0])) {
586 507
            self::$objHolder[$newFuncName] = $callable[0];
587 507
            $innerCode .= "\$obj = PhpXmlRpc\\Wrapper::\$objHolder['$newFuncName'];\n";
588 507
            $realFuncName = '$obj->' . $callable[1];
589
        } else {
590 507
            $realFuncName = $plainFuncName;
591
        }
592 507
        foreach ($parsVariations as $i => $pars) {
593 507
            $innerCode .= "if (\$paramCount == " . count($pars) . ") \$retval = {$catchWarnings}$realFuncName(" . implode(',', $pars) . ");\n";
594 507
            if ($i < (count($parsVariations) - 1))
595
                $innerCode .= "else\n";
596
        }
597 507
        $innerCode .= "if (is_a(\$retval, '{$namespace}Response')) return \$retval; else\n";
598 507
        if ($funcDesc['returns'] == Value::$xmlrpcDateTime || $funcDesc['returns'] == Value::$xmlrpcBase64) {
599
            $innerCode .= "return new {$namespace}Response(new {$namespace}Value(\$retval, '{$funcDesc['returns']}'));";
600
        } else {
601 507
            if ($encodePhpObjects) {
602
                $innerCode .= "return new {$namespace}Response(\$encoder->encode(\$retval, array('encode_php_objs')));\n";
603
            } else {
604 507
                $innerCode .= "return new {$namespace}Response(\$encoder->encode(\$retval));\n";
605
            }
606
        }
607
        // shall we exclude functions returning by ref?
608
        // if($func->returnsReference())
609
        //     return false;
610
611 507
        $code = "function $newFuncName(\$req) {\n" . $innerCode . "\n}";
612
613 507
        return $code;
614
    }
615
616
    /**
617
     * Given a user-defined PHP class or php object, map its methods onto a list of
618
     * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc server
619
     * object and called from remote clients (as well as their corresponding signature info).
620
     *
621
     * @param string|object $className the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class
622
     * @param array $extraOptions see the docs for wrapPhpMethod for basic options, plus
623
     *                            - string method_type    'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on whether $className is a class name or object instance
624
     *                            - string method_filter  a regexp used to filter methods to wrap based on their names
625
     *                            - string prefix         used for the names of the xmlrpc methods created.
626
     *                            - string replace_class_name use to completely replace the class name with the prefix in the generated method names. e.g. instead of \Some\Namespace\Class.method use prefixmethod
627
     * @return array|false false on failure
628
     */
629 507
    public function wrapPhpClass($className, $extraOptions = array())
630
    {
631 507
        $methodFilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : '';
632 507
        $methodType = isset($extraOptions['method_type']) ? $extraOptions['method_type'] : 'auto';
633
634 507
        $results = array();
635 507
        $mList = get_class_methods($className);
636 507
        foreach ($mList as $mName) {
637 507
            if ($methodFilter == '' || preg_match($methodFilter, $mName)) {
638 507
                $func = new \ReflectionMethod($className, $mName);
639 507
                if (!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract()) {
640 507
                    if (($func->isStatic() && ($methodType == 'all' || $methodType == 'static' || ($methodType == 'auto' && is_string($className)))) ||
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($func->isStatic() && $m...& is_object($className), Probably Intended Meaning: $func->isStatic() && ($m... is_object($className))
Loading history...
641 507
                        (!$func->isStatic() && ($methodType == 'all' || $methodType == 'nonstatic' || ($methodType == 'auto' && is_object($className))))
642
                    ) {
643 507
                        $methodWrap = $this->wrapPhpFunction(array($className, $mName), '', $extraOptions);
644
645 507
                        if ($methodWrap) {
646 507
                            $results[$this->generateMethodNameForClassMethod($className, $mName, $extraOptions)] = $methodWrap;
647
                        }
648
                    }
649
                }
650
            }
651
        }
652
653 507
        return $results;
654
    }
655
656
    /**
657
     * @param string|object $className
658
     * @param string $classMethod
659
     * @param array $extraOptions
660
     * @return string
661
     */
662 507
    protected function generateMethodNameForClassMethod($className, $classMethod, $extraOptions = array())
663
    {
664 507
        if (isset($extraOptions['replace_class_name']) && $extraOptions['replace_class_name']) {
665 507
            return (isset($extraOptions['prefix']) ?  $extraOptions['prefix'] : '') . $classMethod;
666
        }
667
668 507
        if (is_object($className)) {
669 507
            $realClassName = get_class($className);
670
        } else {
671
            $realClassName = $className;
672
        }
673 507
        return (isset($extraOptions['prefix']) ?  $extraOptions['prefix'] : '') . "$realClassName.$classMethod";
674
    }
675
676
    /**
677
     * Given an xmlrpc client and a method name, register a php wrapper function
678
     * that will call it and return results using native php types for both
679
     * params and results. The generated php function will return a Response
680
     * object for failed xmlrpc calls.
681
     *
682
     * Known limitations:
683
     * - server must support system.methodsignature for the wanted xmlrpc method
684
     * - for methods that expose many signatures, only one can be picked (we
685
     *   could in principle check if signatures differ only by number of params
686
     *   and not by type, but it would be more complication than we can spare time)
687
     * - nested xmlrpc params: the caller of the generated php function has to
688
     *   encode on its own the params passed to the php function if these are structs
689
     *   or arrays whose (sub)members include values of type datetime or base64
690
     *
691
     * Notes: the connection properties of the given client will be copied
692
     * and reused for the connection used during the call to the generated
693
     * php function.
694
     * Calling the generated php function 'might' be slow: a new xmlrpc client
695
     * is created on every invocation and an xmlrpc-connection opened+closed.
696
     * An extra 'debug' param is appended to param list of xmlrpc method, useful
697
     * for debugging purposes.
698
     *
699
     * @todo allow caller to give us the method signature instead of querying for it, or just say 'skip it'
700
     * @todo if we can not retrieve method signature, create a php function with varargs
701
     * @todo allow the created function to throw exceptions on method calls failures
702
     * @todo if caller did not specify a specific sig, shall we support all of them?
703
     *       It might be hard (hence slow) to match based on type and number of arguments...
704
     *
705
     * @param Client $client an xmlrpc client set up correctly to communicate with target server
706
     * @param string $methodName the xmlrpc method to be mapped to a php function
707
     * @param array $extraOptions array of options that specify conversion details. Valid options include
708
     *                            - integer signum              the index of the method signature to use in mapping (if method exposes many sigs)
709
     *                            - integer timeout             timeout (in secs) to be used when executing function/calling remote method
710
     *                            - string  protocol            'http' (default), 'http11' or 'https'
711
     *                            - string  new_function_name   the name of php function to create, when return_source is used. If unspecified, lib will pick an appropriate name
712
     *                            - string  return_source       if true return php code w. function definition instead of function itself (closure)
713
     *                            - bool    encode_php_objs     let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects
714
     *                            - bool    decode_php_objs     --- WARNING !!! possible security hazard. only use it with trusted servers ---
715
     *                            - mixed   return_on_fault     a php value to be returned when the xmlrpc call fails/returns a fault response (by default the Response object is returned in this case). If a string is used, '%faultCode%' and '%faultString%' tokens will be substituted with actual error values
716
     *                            - bool    debug               set it to 1 or 2 to see debug results of querying server for method synopsis
717
     *                            - int     simple_client_copy  set it to 1 to have a lightweight copy of the $client object made in the generated code (only used when return_source = true)
718
     *
719
     * @return \closure|string[]|false false on failure, closure by default and array for return_source = true
720
     */
721 99
    public function wrapXmlrpcMethod($client, $methodName, $extraOptions = array())
722
    {
723 99
        $newFuncName = isset($extraOptions['new_function_name']) ? $extraOptions['new_function_name'] : '';
724
725 99
        $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true;
726
727 99
        $mSig = $this->retrieveMethodSignature($client, $methodName, $extraOptions);
728 99
        if (!$mSig) {
729 22
            return false;
730
        }
731
732 78
        if ($buildIt) {
733 1
            return $this->buildWrapMethodClosure($client, $methodName, $extraOptions, $mSig);
734
        } else {
735
            // if in 'offline' mode, retrieve method description too.
736
            // in online mode, favour speed of operation
737 77
            $mDesc = $this->retrieveMethodHelp($client, $methodName, $extraOptions);
738
739 77
            $newFuncName = $this->newFunctionName($methodName, $newFuncName, $extraOptions);
740
741 77
            $results = $this->buildWrapMethodSource($client, $methodName, $extraOptions, $newFuncName, $mSig, $mDesc);
742
            /* was: $results = $this->build_remote_method_wrapper_code($client, $methodName,
743
                $newFuncName, $mSig, $mDesc, $timeout, $protocol, $simpleClientCopy,
744
                $prefix, $decodePhpObjects, $encodePhpObjects, $decodeFault,
745
                $faultResponse, $namespace);*/
746
747 77
            $results['function'] = $newFuncName;
748
749 77
            return $results;
750
        }
751
    }
752
753
    /**
754
     * Retrieves an xmlrpc method signature from a server which supports system.methodSignature
755
     * @param Client $client
756
     * @param string $methodName
757
     * @param array $extraOptions
758
     * @return false|array
759
     */
760 99
    protected function retrieveMethodSignature($client, $methodName, array $extraOptions = array())
761
    {
762 99
        $namespace = '\\PhpXmlRpc\\';
763 99
        $reqClass = $namespace . 'Request';
764 99
        $valClass = $namespace . 'Value';
765 99
        $decoderClass = $namespace . 'Encoder';
766
767 99
        $debug = isset($extraOptions['debug']) ? ($extraOptions['debug']) : 0;
768 99
        $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0;
769 99
        $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : '';
770 99
        $sigNum = isset($extraOptions['signum']) ? (int)$extraOptions['signum'] : 0;
771
772 99
        $req = new $reqClass('system.methodSignature');
773 99
        $req->addparam(new $valClass($methodName));
774 99
        $client->setDebug($debug);
775 99
        $response = $client->send($req, $timeout, $protocol);
776 99
        if ($response->faultCode()) {
777 22
            $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve method signature from remote server for method ' . $methodName);
778 22
            return false;
779
        }
780
781 78
        $mSig = $response->value();
782 78
        if ($client->return_type != 'phpvals') {
783 77
            $decoder = new $decoderClass();
784 77
            $mSig = $decoder->decode($mSig);
785
        }
786
787 78
        if (!is_array($mSig) || count($mSig) <= $sigNum) {
788
            $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve method signature nr.' . $sigNum . ' from remote server for method ' . $methodName);
789
            return false;
790
        }
791
792 78
        return $mSig[$sigNum];
793
    }
794
795
    /**
796
     * @param Client $client
797
     * @param string $methodName
798
     * @param array $extraOptions
799
     * @return string in case of any error, an empty string is returned, no warnings generated
800
     */
801 77
    protected function retrieveMethodHelp($client, $methodName, array $extraOptions = array())
802
    {
803 77
        $namespace = '\\PhpXmlRpc\\';
804 77
        $reqClass = $namespace . 'Request';
805 77
        $valClass = $namespace . 'Value';
806
807 77
        $debug = isset($extraOptions['debug']) ? ($extraOptions['debug']) : 0;
808 77
        $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0;
809 77
        $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : '';
810
811 77
        $mDesc = '';
812
813 77
        $req = new $reqClass('system.methodHelp');
814 77
        $req->addparam(new $valClass($methodName));
815 77
        $client->setDebug($debug);
816 77
        $response = $client->send($req, $timeout, $protocol);
817 77
        if (!$response->faultCode()) {
818 77
            $mDesc = $response->value();
819 77
            if ($client->return_type != 'phpvals') {
820 77
                $mDesc = $mDesc->scalarval();
0 ignored issues
show
Bug introduced by
The method scalarval() does not exist on integer. ( Ignorable by Annotation )

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

820
                /** @scrutinizer ignore-call */ 
821
                $mDesc = $mDesc->scalarval();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
821
            }
822
        }
823
824 77
        return $mDesc;
825
    }
826
827
    /**
828
     * @param Client $client
829
     * @param string $methodName
830
     * @param array $extraOptions
831
     * @param array $mSig
832
     * @return \Closure
833
     *
834
     * @todo should we allow usage of parameter simple_client_copy to mean 'do not clone' in this case?
835
     */
836 1
    protected function buildWrapMethodClosure($client, $methodName, array $extraOptions, $mSig)
837
    {
838
        // we clone the client, so that we can modify it a bit independently of the original
839 1
        $clientClone = clone $client;
840
        $function = function() use($clientClone, $methodName, $extraOptions, $mSig)
841
        {
842 1
            $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0;
843 1
            $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : '';
844 1
            $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false;
845 1
            $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false;
846 1
            if (isset($extraOptions['return_on_fault'])) {
847
                $decodeFault = true;
848
                $faultResponse = $extraOptions['return_on_fault'];
849
            } else {
850 1
                $decodeFault = false;
851
            }
852
853 1
            $namespace = '\\PhpXmlRpc\\';
854 1
            $reqClass = $namespace . 'Request';
855 1
            $encoderClass = $namespace . 'Encoder';
856 1
            $valueClass = $namespace . 'Value';
857
858 1
            $encoder = new $encoderClass();
859 1
            $encodeOptions = array();
860 1
            if ($encodePhpObjects) {
861
                $encodeOptions[] = 'encode_php_objs';
862
            }
863 1
            $decodeOptions = array();
864 1
            if ($decodePhpObjects) {
865
                $decodeOptions[] = 'decode_php_objs';
866
            }
867
868
            /// @todo check for insufficient nr. of args besides excess ones? note that 'source' version does not...
869
870
            // support one extra parameter: debug
871 1
            $maxArgs = count($mSig)-1; // 1st element is the return type
872 1
            $currentArgs = func_get_args();
873 1
            if (func_num_args() == ($maxArgs+1)) {
874 1
                $debug = array_pop($currentArgs);
875 1
                $clientClone->setDebug($debug);
876
            }
877
878 1
            $xmlrpcArgs = array();
879 1
            foreach($currentArgs as $i => $arg) {
880 1
                if ($i == $maxArgs) {
881
                    break;
882
                }
883 1
                $pType = $mSig[$i+1];
884 1
                if ($pType == 'i4' || $pType == 'i8' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' ||
885
                    $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null'
886
                ) {
887
                    // by building directly xmlrpc values when type is known and scalar (instead of encode() calls),
888
                    // we make sure to honour the xmlrpc signature
889 1
                    $xmlrpcArgs[] = new $valueClass($arg, $pType);
890
                } else {
891
                    $xmlrpcArgs[] = $encoder->encode($arg, $encodeOptions);
892
                }
893
            }
894
895 1
            $req = new $reqClass($methodName, $xmlrpcArgs);
896
            // use this to get the maximum decoding flexibility
897 1
            $clientClone->return_type = 'xmlrpcvals';
898 1
            $resp = $clientClone->send($req, $timeout, $protocol);
899 1
            if ($resp->faultcode()) {
900
                if ($decodeFault) {
901
                    if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) ||
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $faultResponse does not seem to be defined for all execution paths leading up to this point.
Loading history...
902
                            (strpos($faultResponse, '%faultString%') !== false))) {
903
                        $faultResponse = str_replace(array('%faultCode%', '%faultString%'),
904
                            array($resp->faultCode(), $resp->faultString()), $faultResponse);
905
                    }
906
                    return $faultResponse;
907
                } else {
908
                    return $resp;
909
                }
910
            } else {
911 1
                return $encoder->decode($resp->value(), $decodeOptions);
912
            }
913 1
        };
914
915 1
        return $function;
916
    }
917
918
    /**
919
     * @param Client $client
920
     * @param string $methodName
921
     * @param array $extraOptions
922
     * @param string $newFuncName
923
     * @param array $mSig
924
     * @param string $mDesc
925
     * @return string[] keys: source, docstring
926
     */
927 77
    public function buildWrapMethodSource($client, $methodName, array $extraOptions, $newFuncName, $mSig, $mDesc='')
928
    {
929 77
        $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0;
930 77
        $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : '';
931 77
        $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false;
932 77
        $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false;
933 77
        $clientCopyMode = isset($extraOptions['simple_client_copy']) ? (int)($extraOptions['simple_client_copy']) : 0;
934 77
        $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc';
935 77
        if (isset($extraOptions['return_on_fault'])) {
936
            $decodeFault = true;
937
            $faultResponse = $extraOptions['return_on_fault'];
938
        } else {
939 77
            $decodeFault = false;
940 77
            $faultResponse = '';
941
        }
942
943 77
        $namespace = '\\PhpXmlRpc\\';
944
945 77
        $code = "function $newFuncName (";
946 77
        if ($clientCopyMode < 2) {
947
            // client copy mode 0 or 1 == full / partial client copy in emitted code
948 58
            $verbatimClientCopy = !$clientCopyMode;
949 58
            $innerCode = $this->buildClientWrapperCode($client, $verbatimClientCopy, $prefix, $namespace);
950 58
            $innerCode .= "\$client->setDebug(\$debug);\n";
951 58
            $this_ = '';
952
        } else {
953
            // client copy mode 2 == no client copy in emitted code
954 20
            $innerCode = '';
955 20
            $this_ = 'this->';
956
        }
957 77
        $innerCode .= "\$req = new {$namespace}Request('$methodName');\n";
958
959 77
        if ($mDesc != '') {
960
            // take care that PHP comment is not terminated unwillingly by method description
961 77
            $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n";
962
        } else {
963
            $mDesc = "/**\nFunction $newFuncName\n";
964
        }
965
966
        // param parsing
967 77
        $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
968 77
        $plist = array();
969 77
        $pCount = count($mSig);
970 77
        for ($i = 1; $i < $pCount; $i++) {
971 58
            $plist[] = "\$p$i";
972 58
            $pType = $mSig[$i];
973 58
            if ($pType == 'i4' || $pType == 'i8' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' ||
974 58
                $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null'
975
            ) {
976
                // only build directly xmlrpc values when type is known and scalar
977 58
                $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n";
978
            } else {
979
                if ($encodePhpObjects) {
980
                    $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
981
                } else {
982
                    $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n";
983
                }
984
            }
985 58
            $innerCode .= "\$req->addparam(\$p$i);\n";
986 58
            $mDesc .= '* @param ' . $this->xmlrpc2PhpType($pType) . " \$p$i\n";
987
        }
988 77
        if ($clientCopyMode < 2) {
989 58
            $plist[] = '$debug=0';
990 58
            $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";
991
        }
992 77
        $plist = implode(', ', $plist);
993 77
        $mDesc .= '* @return {$namespace}Response|' . $this->xmlrpc2PhpType($mSig[0]) . " (an {$namespace}Response obj instance if call fails)\n*/\n";
994
995 77
        $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n";
996 77
        if ($decodeFault) {
997
            if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) {
998
                $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')";
999
            } else {
1000
                $respCode = var_export($faultResponse, true);
1001
            }
1002
        } else {
1003 77
            $respCode = '$res';
1004
        }
1005 77
        if ($decodePhpObjects) {
1006 20
            $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));";
1007
        } else {
1008 58
            $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());";
1009
        }
1010
1011 77
        $code = $code . $plist . ") {\n" . $innerCode . "\n}\n";
1012
1013 77
        return array('source' => $code, 'docstring' => $mDesc);
1014
    }
1015
1016
    /**
1017
     * Similar to wrapXmlrpcMethod, but will generate a php class that wraps
1018
     * all xmlrpc methods exposed by the remote server as own methods.
1019
     * For more details see wrapXmlrpcMethod.
1020
     *
1021
     * For a slimmer alternative, see the code in demo/client/proxy.php
1022
     *
1023
     * Note that unlike wrapXmlrpcMethod, we always have to generate php code here. It seems that php 7 will have anon classes...
1024
     *
1025
     * @param Client $client the client obj all set to query the desired server
1026
     * @param array $extraOptions list of options for wrapped code. See the ones from wrapXmlrpcMethod plus
1027
     *              - string method_filter      regular expression
1028
     *              - string new_class_name
1029
     *              - string prefix
1030
     *              - bool   simple_client_copy set it to true to avoid copying all properties of $client into the copy made in the new class
1031
     *
1032
     * @return mixed false on error, the name of the created class if all ok or an array with code, class name and comments (if the appropriate option is set in extra_options)
1033
     */
1034 20
    public function wrapXmlrpcServer($client, $extraOptions = array())
1035
    {
1036 20
        $methodFilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : '';
1037 20
        $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0;
1038 20
        $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : '';
1039 20
        $newClassName = isset($extraOptions['new_class_name']) ? $extraOptions['new_class_name'] : '';
1040 20
        $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false;
1041 20
        $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false;
1042 20
        $verbatimClientCopy = isset($extraOptions['simple_client_copy']) ? !($extraOptions['simple_client_copy']) : true;
1043 20
        $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true;
1044 20
        $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc';
1045 20
        $namespace = '\\PhpXmlRpc\\';
1046
1047 20
        $reqClass = $namespace . 'Request';
1048 20
        $decoderClass = $namespace . 'Encoder';
1049
1050 20
        $req = new $reqClass('system.listMethods');
1051 20
        $response = $client->send($req, $timeout, $protocol);
1052 20
        if ($response->faultCode()) {
1053
            $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve method list from remote server');
1054
1055
            return false;
1056
        } else {
1057 20
            $mList = $response->value();
1058 20
            if ($client->return_type != 'phpvals') {
1059 20
                $decoder = new $decoderClass();
1060 20
                $mList = $decoder->decode($mList);
1061
            }
1062 20
            if (!is_array($mList) || !count($mList)) {
1063
                $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not retrieve meaningful method list from remote server');
1064
1065
                return false;
1066
            } else {
1067
                // pick a suitable name for the new function, avoiding collisions
1068 20
                if ($newClassName != '') {
1069
                    $xmlrpcClassName = $newClassName;
1070
                } else {
1071 20
                    $xmlrpcClassName = $prefix . '_' . preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'),
1072 20
                            array('_', ''), $client->server) . '_client';
1073
                }
1074 20
                while ($buildIt && class_exists($xmlrpcClassName)) {
1075 19
                    $xmlrpcClassName .= 'x';
1076
                }
1077
1078
                /// @todo add function setdebug() to new class, to enable/disable debugging
1079 20
                $source = "class $xmlrpcClassName\n{\npublic \$client;\n\n";
1080 20
                $source .= "function __construct()\n{\n";
1081 20
                $source .= $this->buildClientWrapperCode($client, $verbatimClientCopy, $prefix, $namespace);
1082 20
                $source .= "\$this->client = \$client;\n}\n\n";
1083
                $opts = array(
1084 20
                    'return_source' => true,
1085 20
                    'simple_client_copy' => 2, // do not produce code to copy the client object
1086 20
                    'timeout' => $timeout,
1087 20
                    'protocol' => $protocol,
1088 20
                    'encode_php_objs' => $encodePhpObjects,
1089 20
                    'decode_php_objs' => $decodePhpObjects,
1090 20
                    'prefix' => $prefix,
1091
                );
1092
                /// @todo build phpdoc for class definition, too
1093 20
                foreach ($mList as $mName) {
1094 20
                    if ($methodFilter == '' || preg_match($methodFilter, $mName)) {
1095
                        // note: this will fail if server exposes 2 methods called f.e. do.something and do_something
1096 20
                        $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'),
1097 20
                            array('_', ''), $mName);
1098 20
                        $methodWrap = $this->wrapXmlrpcMethod($client, $mName, $opts);
1099 20
                        if ($methodWrap) {
1100 20
                            if (!$buildIt) {
1101
                                $source .= $methodWrap['docstring'];
1102
                            }
1103 20
                            $source .= $methodWrap['source'] . "\n";
1104
                        } else {
1105
                            $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': will not create class method to wrap remote method ' . $mName);
1106
                        }
1107
                    }
1108
                }
1109 20
                $source .= "}\n";
1110 20
                if ($buildIt) {
1111 20
                    $allOK = 0;
1112 20
                    eval($source . '$allOK=1;');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
1113 20
                    if ($allOK) {
1114 20
                        return $xmlrpcClassName;
1115
                    } else {
1116
                        $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': could not create class ' . $xmlrpcClassName . ' to wrap remote server ' . $client->server);
1117
                        return false;
1118
                    }
1119
                } else {
1120
                    return array('class' => $xmlrpcClassName, 'code' => $source, 'docstring' => '');
1121
                }
1122
            }
1123
        }
1124
    }
1125
1126
    /**
1127
     * Given necessary info, generate php code that will build a client object just like the given one.
1128
     * Take care that no full checking of input parameters is done to ensure that
1129
     * valid php code is emitted.
1130
     * @param Client $client
1131
     * @param bool $verbatimClientCopy when true, copy all of the state of the client, except for 'debug' and 'return_type'
1132
     * @param string $prefix used for the return_type of the created client
1133
     * @param string $namespace
1134
     *
1135
     * @return string
1136
     */
1137 77
    protected function buildClientWrapperCode($client, $verbatimClientCopy, $prefix = 'xmlrpc', $namespace = '\\PhpXmlRpc\\' )
1138
    {
1139 77
        $code = "\$client = new {$namespace}Client('" . str_replace("'", "\'", $client->path) .
1140 77
            "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n";
1141
1142
        // copy all client fields to the client that will be generated runtime
1143
        // (this provides for future expansion or subclassing of client obj)
1144 77
        if ($verbatimClientCopy) {
1145 77
            foreach ($client as $fld => $val) {
1146
                /// @todo in php 8.0, curl handles became objects, but they have no __set_state, thus var_export will
1147
                ///        fail for xmlrpc_curl_handle. So we disabled copying it.
1148
                ///        We should examine in depth if this change can have side effects - at first sight if the
1149
                ///        client's curl handle is not set, all curl options are (re)set on each http call, so there
1150
                ///        should be no loss of state...
1151 77
                if ($fld != 'debug' && $fld != 'return_type' && $fld != 'xmlrpc_curl_handle') {
1152 77
                    $val = var_export($val, true);
1153 77
                    $code .= "\$client->$fld = $val;\n";
1154
                }
1155
            }
1156
        }
1157
        // only make sure that client always returns the correct data type
1158 77
        $code .= "\$client->return_type = '{$prefix}vals';\n";
1159
        //$code .= "\$client->setDebug(\$debug);\n";
1160 77
        return $code;
1161
    }
1162
}
1163