diff options
Diffstat (limited to 'web/phpxmlrpclib/xmlrpc_wrappers.inc')
-rw-r--r-- | web/phpxmlrpclib/xmlrpc_wrappers.inc | 944 |
1 files changed, 0 insertions, 944 deletions
diff --git a/web/phpxmlrpclib/xmlrpc_wrappers.inc b/web/phpxmlrpclib/xmlrpc_wrappers.inc deleted file mode 100644 index cb0c6e8..0000000 --- a/web/phpxmlrpclib/xmlrpc_wrappers.inc +++ /dev/null | |||
@@ -1,944 +0,0 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * PHP-XMLRPC "wrapper" functions | ||
4 | * Generate stubs to transparently access xmlrpc methods as php functions and viceversa | ||
5 | * | ||
6 | * @version $Id: xmlrpc_wrappers.inc,v 1.13 2008/09/20 01:23:47 ggiunta Exp $ | ||
7 | * @author Gaetano Giunta | ||
8 | * @copyright (C) 2006-2008 G. Giunta | ||
9 | * @license code licensed under the BSD License: http://phpxmlrpc.sourceforge.net/license.txt | ||
10 | * | ||
11 | * @todo separate introspection from code generation for func-2-method wrapping | ||
12 | * @todo use some better templating system for code generation? | ||
13 | * @todo implement method wrapping with preservation of php objs in calls | ||
14 | * @todo when wrapping methods without obj rebuilding, use return_type = 'phpvals' (faster) | ||
15 | * @todo implement self-parsing of php code for PHP <= 4 | ||
16 | */ | ||
17 | |||
18 | // requires: xmlrpc.inc | ||
19 | |||
20 | /** | ||
21 | * Given a string defining a php type or phpxmlrpc type (loosely defined: strings | ||
22 | * accepted come from javadoc blocks), return corresponding phpxmlrpc type. | ||
23 | * NB: for php 'resource' types returns empty string, since resources cannot be serialized; | ||
24 | * for php class names returns 'struct', since php objects can be serialized as xmlrpc structs | ||
25 | * @param string $phptype | ||
26 | * @return string | ||
27 | */ | ||
28 | function php_2_xmlrpc_type($phptype) | ||
29 | { | ||
30 | switch(strtolower($phptype)) | ||
31 | { | ||
32 | case 'string': | ||
33 | return $GLOBALS['xmlrpcString']; | ||
34 | case 'integer': | ||
35 | case $GLOBALS['xmlrpcInt']: // 'int' | ||
36 | case $GLOBALS['xmlrpcI4']: | ||
37 | return $GLOBALS['xmlrpcInt']; | ||
38 | case 'double': | ||
39 | return $GLOBALS['xmlrpcDouble']; | ||
40 | case 'boolean': | ||
41 | return $GLOBALS['xmlrpcBoolean']; | ||
42 | case 'array': | ||
43 | return $GLOBALS['xmlrpcArray']; | ||
44 | case 'object': | ||
45 | return $GLOBALS['xmlrpcStruct']; | ||
46 | case $GLOBALS['xmlrpcBase64']: | ||
47 | case $GLOBALS['xmlrpcStruct']: | ||
48 | return strtolower($phptype); | ||
49 | case 'resource': | ||
50 | return ''; | ||
51 | default: | ||
52 | if(class_exists($phptype)) | ||
53 | { | ||
54 | return $GLOBALS['xmlrpcStruct']; | ||
55 | } | ||
56 | else | ||
57 | { | ||
58 | // unknown: might be any 'extended' xmlrpc type | ||
59 | return $GLOBALS['xmlrpcValue']; | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * Given a string defining a phpxmlrpc type return corresponding php type. | ||
66 | * @param string $xmlrpctype | ||
67 | * @return string | ||
68 | */ | ||
69 | function xmlrpc_2_php_type($xmlrpctype) | ||
70 | { | ||
71 | switch(strtolower($xmlrpctype)) | ||
72 | { | ||
73 | case 'base64': | ||
74 | case 'datetime.iso8601': | ||
75 | case 'string': | ||
76 | return $GLOBALS['xmlrpcString']; | ||
77 | case 'int': | ||
78 | case 'i4': | ||
79 | return 'integer'; | ||
80 | case 'struct': | ||
81 | case 'array': | ||
82 | return 'array'; | ||
83 | case 'double': | ||
84 | return 'float'; | ||
85 | case 'undefined': | ||
86 | return 'mixed'; | ||
87 | case 'boolean': | ||
88 | case 'null': | ||
89 | default: | ||
90 | // unknown: might be any xmlrpc type | ||
91 | return strtolower($xmlrpctype); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * Given a user-defined PHP function, create a PHP 'wrapper' function that can | ||
97 | * be exposed as xmlrpc method from an xmlrpc_server object and called from remote | ||
98 | * clients (as well as its corresponding signature info). | ||
99 | * | ||
100 | * Since php is a typeless language, to infer types of input and output parameters, | ||
101 | * it relies on parsing the javadoc-style comment block associated with the given | ||
102 | * function. Usage of xmlrpc native types (such as datetime.dateTime.iso8601 and base64) | ||
103 | * in the @param tag is also allowed, if you need the php function to receive/send | ||
104 | * data in that particular format (note that base64 encoding/decoding is transparently | ||
105 | * carried out by the lib, while datetime vals are passed around as strings) | ||
106 | * | ||
107 | * Known limitations: | ||
108 | * - requires PHP 5.0.3 + | ||
109 | * - only works for user-defined functions, not for PHP internal functions | ||
110 | * (reflection does not support retrieving number/type of params for those) | ||
111 | * - functions returning php objects will generate special xmlrpc responses: | ||
112 | * when the xmlrpc decoding of those responses is carried out by this same lib, using | ||
113 | * the appropriate param in php_xmlrpc_decode, the php objects will be rebuilt. | ||
114 | * In short: php objects can be serialized, too (except for their resource members), | ||
115 | * using this function. | ||
116 | * Other libs might choke on the very same xml that will be generated in this case | ||
117 | * (i.e. it has a nonstandard attribute on struct element tags) | ||
118 | * - usage of javadoc @param tags using param names in a different order from the | ||
119 | * function prototype is not considered valid (to be fixed?) | ||
120 | * | ||
121 | * Note that since rel. 2.0RC3 the preferred method to have the server call 'standard' | ||
122 | * php functions (ie. functions not expecting a single xmlrpcmsg obj as parameter) | ||
123 | * is by making use of the functions_parameters_type class member. | ||
124 | * | ||
125 | * @param string $funcname the name of the PHP user function to be exposed as xmlrpc method; array($obj, 'methodname') and array('class', 'methodname') are ok too | ||
126 | * @param string $newfuncname (optional) name for function to be created | ||
127 | * @param array $extra_options (optional) array of options for conversion. valid values include: | ||
128 | * bool return_source when true, php code w. function definition will be returned, not evaluated | ||
129 | * bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects | ||
130 | * bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- | ||
131 | * bool suppress_warnings remove from produced xml any runtime warnings due to the php function being invoked | ||
132 | * @return false on error, or an array containing the name of the new php function, | ||
133 | * its signature and docs, to be used in the server dispatch map | ||
134 | * | ||
135 | * @todo decide how to deal with params passed by ref: bomb out or allow? | ||
136 | * @todo finish using javadoc info to build method sig if all params are named but out of order | ||
137 | * @todo add a check for params of 'resource' type | ||
138 | * @todo add some trigger_errors / error_log when returning false? | ||
139 | * @todo what to do when the PHP function returns NULL? we are currently returning an empty string value... | ||
140 | * @todo add an option to suppress php warnings in invocation of user function, similar to server debug level 3? | ||
141 | * @todo if $newfuncname is empty, we could use create_user_func instead of eval, as it is possibly faster | ||
142 | * @todo add a verbatim_object_copy parameter to allow avoiding the same obj instance? | ||
143 | */ | ||
144 | function wrap_php_function($funcname, $newfuncname='', $extra_options=array()) | ||
145 | { | ||
146 | $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; | ||
147 | $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; | ||
148 | $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; | ||
149 | $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; | ||
150 | $catch_warnings = isset($extra_options['suppress_warnings']) && $extra_options['suppress_warnings'] ? '@' : ''; | ||
151 | |||
152 | if(version_compare(phpversion(), '5.0.3') == -1) | ||
153 | { | ||
154 | // up to php 5.0.3 some useful reflection methods were missing | ||
155 | error_log('XML-RPC: cannot not wrap php functions unless running php version bigger than 5.0.3'); | ||
156 | return false; | ||
157 | } | ||
158 | |||
159 | $exists = false; | ||
160 | if(is_array($funcname)) | ||
161 | { | ||
162 | if(count($funcname) < 2 || (!is_string($funcname[0]) && !is_object($funcname[0]))) | ||
163 | { | ||
164 | error_log('XML-RPC: syntax for function to be wrapped is wrong'); | ||
165 | return false; | ||
166 | } | ||
167 | if(is_string($funcname[0])) | ||
168 | { | ||
169 | $plainfuncname = implode('::', $funcname); | ||
170 | } | ||
171 | elseif(is_object($funcname[0])) | ||
172 | { | ||
173 | $plainfuncname = get_class($funcname[0]) . '->' . $funcname[1]; | ||
174 | } | ||
175 | $exists = method_exists($funcname[0], $funcname[1]); | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | $plainfuncname = $funcname; | ||
180 | $exists = function_exists($funcname); | ||
181 | } | ||
182 | |||
183 | if(!$exists) | ||
184 | { | ||
185 | error_log('XML-RPC: function to be wrapped is not defined: '.$plainfuncname); | ||
186 | return false; | ||
187 | } | ||
188 | else | ||
189 | { | ||
190 | // determine name of new php function | ||
191 | if($newfuncname == '') | ||
192 | { | ||
193 | if(is_array($funcname)) | ||
194 | { | ||
195 | if(is_string($funcname[0])) | ||
196 | $xmlrpcfuncname = "{$prefix}_".implode('_', $funcname); | ||
197 | else | ||
198 | $xmlrpcfuncname = "{$prefix}_".get_class($funcname[0]) . '_' . $funcname[1]; | ||
199 | } | ||
200 | else | ||
201 | { | ||
202 | $xmlrpcfuncname = "{$prefix}_$funcname"; | ||
203 | } | ||
204 | } | ||
205 | else | ||
206 | { | ||
207 | $xmlrpcfuncname = $newfuncname; | ||
208 | } | ||
209 | while($buildit && function_exists($xmlrpcfuncname)) | ||
210 | { | ||
211 | $xmlrpcfuncname .= 'x'; | ||
212 | } | ||
213 | |||
214 | // start to introspect PHP code | ||
215 | if(is_array($funcname)) | ||
216 | { | ||
217 | $func =& new ReflectionMethod($funcname[0], $funcname[1]); | ||
218 | if($func->isPrivate()) | ||
219 | { | ||
220 | error_log('XML-RPC: method to be wrapped is private: '.$plainfuncname); | ||
221 | return false; | ||
222 | } | ||
223 | if($func->isProtected()) | ||
224 | { | ||
225 | error_log('XML-RPC: method to be wrapped is protected: '.$plainfuncname); | ||
226 | return false; | ||
227 | } | ||
228 | if($func->isConstructor()) | ||
229 | { | ||
230 | error_log('XML-RPC: method to be wrapped is the constructor: '.$plainfuncname); | ||
231 | return false; | ||
232 | } | ||
233 | if($func->isDestructor()) | ||
234 | { | ||
235 | error_log('XML-RPC: method to be wrapped is the destructor: '.$plainfuncname); | ||
236 | return false; | ||
237 | } | ||
238 | if($func->isAbstract()) | ||
239 | { | ||
240 | error_log('XML-RPC: method to be wrapped is abstract: '.$plainfuncname); | ||
241 | return false; | ||
242 | } | ||
243 | /// @todo add more checks for static vs. nonstatic? | ||
244 | } | ||
245 | else | ||
246 | { | ||
247 | $func =& new ReflectionFunction($funcname); | ||
248 | } | ||
249 | if($func->isInternal()) | ||
250 | { | ||
251 | // Note: from PHP 5.1.0 onward, we will possibly be able to use invokeargs | ||
252 | // instead of getparameters to fully reflect internal php functions ? | ||
253 | error_log('XML-RPC: function to be wrapped is internal: '.$plainfuncname); | ||
254 | return false; | ||
255 | } | ||
256 | |||
257 | // retrieve parameter names, types and description from javadoc comments | ||
258 | |||
259 | // function description | ||
260 | $desc = ''; | ||
261 | // type of return val: by default 'any' | ||
262 | $returns = $GLOBALS['xmlrpcValue']; | ||
263 | // desc of return val | ||
264 | $returnsDocs = ''; | ||
265 | // type + name of function parameters | ||
266 | $paramDocs = array(); | ||
267 | |||
268 | $docs = $func->getDocComment(); | ||
269 | if($docs != '') | ||
270 | { | ||
271 | $docs = explode("\n", $docs); | ||
272 | $i = 0; | ||
273 | foreach($docs as $doc) | ||
274 | { | ||
275 | $doc = trim($doc, " \r\t/*"); | ||
276 | if(strlen($doc) && strpos($doc, '@') !== 0 && !$i) | ||
277 | { | ||
278 | if($desc) | ||
279 | { | ||
280 | $desc .= "\n"; | ||
281 | } | ||
282 | $desc .= $doc; | ||
283 | } | ||
284 | elseif(strpos($doc, '@param') === 0) | ||
285 | { | ||
286 | // syntax: @param type [$name] desc | ||
287 | if(preg_match('/@param\s+(\S+)(\s+\$\S+)?\s+(.+)/', $doc, $matches)) | ||
288 | { | ||
289 | if(strpos($matches[1], '|')) | ||
290 | { | ||
291 | //$paramDocs[$i]['type'] = explode('|', $matches[1]); | ||
292 | $paramDocs[$i]['type'] = 'mixed'; | ||
293 | } | ||
294 | else | ||
295 | { | ||
296 | $paramDocs[$i]['type'] = $matches[1]; | ||
297 | } | ||
298 | $paramDocs[$i]['name'] = trim($matches[2]); | ||
299 | $paramDocs[$i]['doc'] = $matches[3]; | ||
300 | } | ||
301 | $i++; | ||
302 | } | ||
303 | elseif(strpos($doc, '@return') === 0) | ||
304 | { | ||
305 | // syntax: @return type desc | ||
306 | //$returns = preg_split('/\s+/', $doc); | ||
307 | if(preg_match('/@return\s+(\S+)\s+(.+)/', $doc, $matches)) | ||
308 | { | ||
309 | $returns = php_2_xmlrpc_type($matches[1]); | ||
310 | if(isset($matches[2])) | ||
311 | { | ||
312 | $returnsDocs = $matches[2]; | ||
313 | } | ||
314 | } | ||
315 | } | ||
316 | } | ||
317 | } | ||
318 | |||
319 | // execute introspection of actual function prototype | ||
320 | $params = array(); | ||
321 | $i = 0; | ||
322 | foreach($func->getParameters() as $paramobj) | ||
323 | { | ||
324 | $params[$i] = array(); | ||
325 | $params[$i]['name'] = '$'.$paramobj->getName(); | ||
326 | $params[$i]['isoptional'] = $paramobj->isOptional(); | ||
327 | $i++; | ||
328 | } | ||
329 | |||
330 | |||
331 | // start building of PHP code to be eval'd | ||
332 | $innercode = ''; | ||
333 | $i = 0; | ||
334 | $parsvariations = array(); | ||
335 | $pars = array(); | ||
336 | $pnum = count($params); | ||
337 | foreach($params as $param) | ||
338 | { | ||
339 | if (isset($paramDocs[$i]['name']) && $paramDocs[$i]['name'] && strtolower($paramDocs[$i]['name']) != strtolower($param['name'])) | ||
340 | { | ||
341 | // param name from phpdoc info does not match param definition! | ||
342 | $paramDocs[$i]['type'] = 'mixed'; | ||
343 | } | ||
344 | |||
345 | if($param['isoptional']) | ||
346 | { | ||
347 | // this particular parameter is optional. save as valid previous list of parameters | ||
348 | $innercode .= "if (\$paramcount > $i) {\n"; | ||
349 | $parsvariations[] = $pars; | ||
350 | } | ||
351 | $innercode .= "\$p$i = \$msg->getParam($i);\n"; | ||
352 | if ($decode_php_objects) | ||
353 | { | ||
354 | $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i, array('decode_php_objs'));\n"; | ||
355 | } | ||
356 | else | ||
357 | { | ||
358 | $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i);\n"; | ||
359 | } | ||
360 | |||
361 | $pars[] = "\$p$i"; | ||
362 | $i++; | ||
363 | if($param['isoptional']) | ||
364 | { | ||
365 | $innercode .= "}\n"; | ||
366 | } | ||
367 | if($i == $pnum) | ||
368 | { | ||
369 | // last allowed parameters combination | ||
370 | $parsvariations[] = $pars; | ||
371 | } | ||
372 | } | ||
373 | |||
374 | $sigs = array(); | ||
375 | $psigs = array(); | ||
376 | if(count($parsvariations) == 0) | ||
377 | { | ||
378 | // only known good synopsis = no parameters | ||
379 | $parsvariations[] = array(); | ||
380 | $minpars = 0; | ||
381 | } | ||
382 | else | ||
383 | { | ||
384 | $minpars = count($parsvariations[0]); | ||
385 | } | ||
386 | |||
387 | if($minpars) | ||
388 | { | ||
389 | // add to code the check for min params number | ||
390 | // NB: this check needs to be done BEFORE decoding param values | ||
391 | $innercode = "\$paramcount = \$msg->getNumParams();\n" . | ||
392 | "if (\$paramcount < $minpars) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}');\n" . $innercode; | ||
393 | } | ||
394 | else | ||
395 | { | ||
396 | $innercode = "\$paramcount = \$msg->getNumParams();\n" . $innercode; | ||
397 | } | ||
398 | |||
399 | $innercode .= "\$np = false;\n"; | ||
400 | // since there are no closures in php, if we are given an object instance, | ||
401 | // we store a pointer to it in a global var... | ||
402 | if ( is_array($funcname) && is_object($funcname[0]) ) | ||
403 | { | ||
404 | $GLOBALS['xmlrpcWPFObjHolder'][$xmlrpcfuncname] =& $funcname[0]; | ||
405 | $innercode .= "\$obj =& \$GLOBALS['xmlrpcWPFObjHolder']['$xmlrpcfuncname'];\n"; | ||
406 | $realfuncname = '$obj->'.$funcname[1]; | ||
407 | } | ||
408 | else | ||
409 | { | ||
410 | $realfuncname = $plainfuncname; | ||
411 | } | ||
412 | foreach($parsvariations as $pars) | ||
413 | { | ||
414 | $innercode .= "if (\$paramcount == " . count($pars) . ") \$retval = {$catch_warnings}$realfuncname(" . implode(',', $pars) . "); else\n"; | ||
415 | // build a 'generic' signature (only use an appropriate return type) | ||
416 | $sig = array($returns); | ||
417 | $psig = array($returnsDocs); | ||
418 | for($i=0; $i < count($pars); $i++) | ||
419 | { | ||
420 | if (isset($paramDocs[$i]['type'])) | ||
421 | { | ||
422 | $sig[] = php_2_xmlrpc_type($paramDocs[$i]['type']); | ||
423 | } | ||
424 | else | ||
425 | { | ||
426 | $sig[] = $GLOBALS['xmlrpcValue']; | ||
427 | } | ||
428 | $psig[] = isset($paramDocs[$i]['doc']) ? $paramDocs[$i]['doc'] : ''; | ||
429 | } | ||
430 | $sigs[] = $sig; | ||
431 | $psigs[] = $psig; | ||
432 | } | ||
433 | $innercode .= "\$np = true;\n"; | ||
434 | $innercode .= "if (\$np) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}'); else {\n"; | ||
435 | //$innercode .= "if (\$_xmlrpcs_error_occurred) return new xmlrpcresp(0, $GLOBALS['xmlrpcerr']user, \$_xmlrpcs_error_occurred); else\n"; | ||
436 | $innercode .= "if (is_a(\$retval, '{$prefix}resp')) return \$retval; else\n"; | ||
437 | if($returns == $GLOBALS['xmlrpcDateTime'] || $returns == $GLOBALS['xmlrpcBase64']) | ||
438 | { | ||
439 | $innercode .= "return new {$prefix}resp(new {$prefix}val(\$retval, '$returns'));"; | ||
440 | } | ||
441 | else | ||
442 | { | ||
443 | if ($encode_php_objects) | ||
444 | $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval, array('encode_php_objs')));\n"; | ||
445 | else | ||
446 | $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval));\n"; | ||
447 | } | ||
448 | // shall we exclude functions returning by ref? | ||
449 | // if($func->returnsReference()) | ||
450 | // return false; | ||
451 | $code = "function $xmlrpcfuncname(\$msg) {\n" . $innercode . "}\n}"; | ||
452 | //print_r($code); | ||
453 | if ($buildit) | ||
454 | { | ||
455 | $allOK = 0; | ||
456 | eval($code.'$allOK=1;'); | ||
457 | // alternative | ||
458 | //$xmlrpcfuncname = create_function('$m', $innercode); | ||
459 | |||
460 | if(!$allOK) | ||
461 | { | ||
462 | error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap php function '.$plainfuncname); | ||
463 | return false; | ||
464 | } | ||
465 | } | ||
466 | |||
467 | /// @todo examine if $paramDocs matches $parsvariations and build array for | ||
468 | /// usage as method signature, plus put together a nice string for docs | ||
469 | |||
470 | $ret = array('function' => $xmlrpcfuncname, 'signature' => $sigs, 'docstring' => $desc, 'signature_docs' => $psigs, 'source' => $code); | ||
471 | return $ret; | ||
472 | } | ||
473 | } | ||
474 | |||
475 | /** | ||
476 | * Given a user-defined PHP class or php object, map its methods onto a list of | ||
477 | * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc_server | ||
478 | * object and called from remote clients (as well as their corresponding signature info). | ||
479 | * | ||
480 | * @param mixed $classname the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class | ||
481 | * @param array $extra_options see the docs for wrap_php_method for more options | ||
482 | * string method_type 'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on wheter $classname is a class name or object instance | ||
483 | * @return array or false on failure | ||
484 | * | ||
485 | * @todo get_class_methods will return both static and non-static methods. | ||
486 | * we have to differentiate the action, depending on wheter we recived a class name or object | ||
487 | */ | ||
488 | function wrap_php_class($classname, $extra_options=array()) | ||
489 | { | ||
490 | $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; | ||
491 | $methodtype = isset($extra_options['method_type']) ? $extra_options['method_type'] : 'auto'; | ||
492 | |||
493 | if(version_compare(phpversion(), '5.0.3') == -1) | ||
494 | { | ||
495 | // up to php 5.0.3 some useful reflection methods were missing | ||
496 | error_log('XML-RPC: cannot not wrap php functions unless running php version bigger than 5.0.3'); | ||
497 | return false; | ||
498 | } | ||
499 | |||
500 | $result = array(); | ||
501 | $mlist = get_class_methods($classname); | ||
502 | foreach($mlist as $mname) | ||
503 | { | ||
504 | if ($methodfilter == '' || preg_match($methodfilter, $mname)) | ||
505 | { | ||
506 | // echo $mlist."\n"; | ||
507 | $func =& new ReflectionMethod($classname, $mname); | ||
508 | if(!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract()) | ||
509 | { | ||
510 | if(($func->isStatic && ($methodtype == 'all' || $methodtype == 'static' || ($methodtype == 'auto' && is_string($classname)))) || | ||
511 | (!$func->isStatic && ($methodtype == 'all' || $methodtype == 'nonstatic' || ($methodtype == 'auto' && is_object($classname))))) | ||
512 | { | ||
513 | $methodwrap = wrap_php_function(array($classname, $mname), '', $extra_options); | ||
514 | if ( $methodwrap ) | ||
515 | { | ||
516 | $result[$methodwrap['function']] = $methodwrap['function']; | ||
517 | } | ||
518 | } | ||
519 | } | ||
520 | } | ||
521 | } | ||
522 | return $result; | ||
523 | } | ||
524 | |||
525 | /** | ||
526 | * Given an xmlrpc client and a method name, register a php wrapper function | ||
527 | * that will call it and return results using native php types for both | ||
528 | * params and results. The generated php function will return an xmlrpcresp | ||
529 | * oject for failed xmlrpc calls | ||
530 | * | ||
531 | * Known limitations: | ||
532 | * - server must support system.methodsignature for the wanted xmlrpc method | ||
533 | * - for methods that expose many signatures, only one can be picked (we | ||
534 | * could in priciple check if signatures differ only by number of params | ||
535 | * and not by type, but it would be more complication than we can spare time) | ||
536 | * - nested xmlrpc params: the caller of the generated php function has to | ||
537 | * encode on its own the params passed to the php function if these are structs | ||
538 | * or arrays whose (sub)members include values of type datetime or base64 | ||
539 | * | ||
540 | * Notes: the connection properties of the given client will be copied | ||
541 | * and reused for the connection used during the call to the generated | ||
542 | * php function. | ||
543 | * Calling the generated php function 'might' be slow: a new xmlrpc client | ||
544 | * is created on every invocation and an xmlrpc-connection opened+closed. | ||
545 | * An extra 'debug' param is appended to param list of xmlrpc method, useful | ||
546 | * for debugging purposes. | ||
547 | * | ||
548 | * @param xmlrpc_client $client an xmlrpc client set up correctly to communicate with target server | ||
549 | * @param string $methodname the xmlrpc method to be mapped to a php function | ||
550 | * @param array $extra_options array of options that specify conversion details. valid ptions include | ||
551 | * integer signum the index of the method signature to use in mapping (if method exposes many sigs) | ||
552 | * integer timeout timeout (in secs) to be used when executing function/calling remote method | ||
553 | * string protocol 'http' (default), 'http11' or 'https' | ||
554 | * string new_function_name the name of php function to create. If unsepcified, lib will pick an appropriate name | ||
555 | * string return_source if true return php code w. function definition instead fo function name | ||
556 | * bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects | ||
557 | * bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- | ||
558 | * mixed return_on_fault a php value to be returned when the xmlrpc call fails/returns a fault response (by default the xmlrpcresp object is returned in this case). If a string is used, '%faultCode%' and '%faultString%' tokens will be substituted with actual error values | ||
559 | * bool debug set it to 1 or 2 to see debug results of querying server for method synopsis | ||
560 | * @return string the name of the generated php function (or false) - OR AN ARRAY... | ||
561 | */ | ||
562 | function wrap_xmlrpc_method($client, $methodname, $extra_options=0, $timeout=0, $protocol='', $newfuncname='') | ||
563 | { | ||
564 | // mind numbing: let caller use sane calling convention (as per javadoc, 3 params), | ||
565 | // OR the 2.0 calling convention (no options) - we really love backward compat, don't we? | ||
566 | if (!is_array($extra_options)) | ||
567 | { | ||
568 | $signum = $extra_options; | ||
569 | $extra_options = array(); | ||
570 | } | ||
571 | else | ||
572 | { | ||
573 | $signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; | ||
574 | $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; | ||
575 | $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; | ||
576 | $newfuncname = isset($extra_options['new_function_name']) ? $extra_options['new_function_name'] : ''; | ||
577 | } | ||
578 | //$encode_php_objects = in_array('encode_php_objects', $extra_options); | ||
579 | //$verbatim_client_copy = in_array('simple_client_copy', $extra_options) ? 1 : | ||
580 | // in_array('build_class_code', $extra_options) ? 2 : 0; | ||
581 | |||
582 | $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; | ||
583 | $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; | ||
584 | $simple_client_copy = isset($extra_options['simple_client_copy']) ? (int)($extra_options['simple_client_copy']) : 0; | ||
585 | $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; | ||
586 | $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; | ||
587 | if (isset($extra_options['return_on_fault'])) | ||
588 | { | ||
589 | $decode_fault = true; | ||
590 | $fault_response = $extra_options['return_on_fault']; | ||
591 | } | ||
592 | else | ||
593 | { | ||
594 | $decode_fault = false; | ||
595 | $fault_response = ''; | ||
596 | } | ||
597 | $debug = isset($extra_options['debug']) ? ($extra_options['debug']) : 0; | ||
598 | |||
599 | $msgclass = $prefix.'msg'; | ||
600 | $valclass = $prefix.'val'; | ||
601 | $decodefunc = 'php_'.$prefix.'_decode'; | ||
602 | |||
603 | $msg =& new $msgclass('system.methodSignature'); | ||
604 | $msg->addparam(new $valclass($methodname)); | ||
605 | $client->setDebug($debug); | ||
606 | $response =& $client->send($msg, $timeout, $protocol); | ||
607 | if($response->faultCode()) | ||
608 | { | ||
609 | error_log('XML-RPC: could not retrieve method signature from remote server for method '.$methodname); | ||
610 | return false; | ||
611 | } | ||
612 | else | ||
613 | { | ||
614 | $msig = $response->value(); | ||
615 | if ($client->return_type != 'phpvals') | ||
616 | { | ||
617 | $msig = $decodefunc($msig); | ||
618 | } | ||
619 | if(!is_array($msig) || count($msig) <= $signum) | ||
620 | { | ||
621 | error_log('XML-RPC: could not retrieve method signature nr.'.$signum.' from remote server for method '.$methodname); | ||
622 | return false; | ||
623 | } | ||
624 | else | ||
625 | { | ||
626 | // pick a suitable name for the new function, avoiding collisions | ||
627 | if($newfuncname != '') | ||
628 | { | ||
629 | $xmlrpcfuncname = $newfuncname; | ||
630 | } | ||
631 | else | ||
632 | { | ||
633 | // take care to insure that methodname is translated to valid | ||
634 | // php function name | ||
635 | $xmlrpcfuncname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), | ||
636 | array('_', ''), $methodname); | ||
637 | } | ||
638 | while($buildit && function_exists($xmlrpcfuncname)) | ||
639 | { | ||
640 | $xmlrpcfuncname .= 'x'; | ||
641 | } | ||
642 | |||
643 | $msig = $msig[$signum]; | ||
644 | $mdesc = ''; | ||
645 | // if in 'offline' mode, get method description too. | ||
646 | // in online mode, favour speed of operation | ||
647 | if(!$buildit) | ||
648 | { | ||
649 | $msg =& new $msgclass('system.methodHelp'); | ||
650 | $msg->addparam(new $valclass($methodname)); | ||
651 | $response =& $client->send($msg, $timeout, $protocol); | ||
652 | if (!$response->faultCode()) | ||
653 | { | ||
654 | $mdesc = $response->value(); | ||
655 | if ($client->return_type != 'phpvals') | ||
656 | { | ||
657 | $mdesc = $mdesc->scalarval(); | ||
658 | } | ||
659 | } | ||
660 | } | ||
661 | |||
662 | $results = build_remote_method_wrapper_code($client, $methodname, | ||
663 | $xmlrpcfuncname, $msig, $mdesc, $timeout, $protocol, $simple_client_copy, | ||
664 | $prefix, $decode_php_objects, $encode_php_objects, $decode_fault, | ||
665 | $fault_response); | ||
666 | |||
667 | //print_r($code); | ||
668 | if ($buildit) | ||
669 | { | ||
670 | $allOK = 0; | ||
671 | eval($results['source'].'$allOK=1;'); | ||
672 | // alternative | ||
673 | //$xmlrpcfuncname = create_function('$m', $innercode); | ||
674 | if($allOK) | ||
675 | { | ||
676 | return $xmlrpcfuncname; | ||
677 | } | ||
678 | else | ||
679 | { | ||
680 | error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap remote method '.$methodname); | ||
681 | return false; | ||
682 | } | ||
683 | } | ||
684 | else | ||
685 | { | ||
686 | $results['function'] = $xmlrpcfuncname; | ||
687 | return $results; | ||
688 | } | ||
689 | } | ||
690 | } | ||
691 | } | ||
692 | |||
693 | /** | ||
694 | * Similar to wrap_xmlrpc_method, but will generate a php class that wraps | ||
695 | * all xmlrpc methods exposed by the remote server as own methods. | ||
696 | * For more details see wrap_xmlrpc_method. | ||
697 | * @param xmlrpc_client $client the client obj all set to query the desired server | ||
698 | * @param array $extra_options list of options for wrapped code | ||
699 | * @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 appropriatevoption is set in extra_options) | ||
700 | */ | ||
701 | function wrap_xmlrpc_server($client, $extra_options=array()) | ||
702 | { | ||
703 | $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; | ||
704 | //$signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; | ||
705 | $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; | ||
706 | $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; | ||
707 | $newclassname = isset($extra_options['new_class_name']) ? $extra_options['new_class_name'] : ''; | ||
708 | $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; | ||
709 | $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; | ||
710 | $verbatim_client_copy = isset($extra_options['simple_client_copy']) ? !($extra_options['simple_client_copy']) : true; | ||
711 | $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; | ||
712 | $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; | ||
713 | |||
714 | $msgclass = $prefix.'msg'; | ||
715 | //$valclass = $prefix.'val'; | ||
716 | $decodefunc = 'php_'.$prefix.'_decode'; | ||
717 | |||
718 | $msg =& new $msgclass('system.listMethods'); | ||
719 | $response =& $client->send($msg, $timeout, $protocol); | ||
720 | if($response->faultCode()) | ||
721 | { | ||
722 | error_log('XML-RPC: could not retrieve method list from remote server'); | ||
723 | return false; | ||
724 | } | ||
725 | else | ||
726 | { | ||
727 | $mlist = $response->value(); | ||
728 | if ($client->return_type != 'phpvals') | ||
729 | { | ||
730 | $mlist = $decodefunc($mlist); | ||
731 | } | ||
732 | if(!is_array($mlist) || !count($mlist)) | ||
733 | { | ||
734 | error_log('XML-RPC: could not retrieve meaningful method list from remote server'); | ||
735 | return false; | ||
736 | } | ||
737 | else | ||
738 | { | ||
739 | // pick a suitable name for the new function, avoiding collisions | ||
740 | if($newclassname != '') | ||
741 | { | ||
742 | $xmlrpcclassname = $newclassname; | ||
743 | } | ||
744 | else | ||
745 | { | ||
746 | $xmlrpcclassname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), | ||
747 | array('_', ''), $client->server).'_client'; | ||
748 | } | ||
749 | while($buildit && class_exists($xmlrpcclassname)) | ||
750 | { | ||
751 | $xmlrpcclassname .= 'x'; | ||
752 | } | ||
753 | |||
754 | /// @todo add function setdebug() to new class, to enable/disable debugging | ||
755 | $source = "class $xmlrpcclassname\n{\nvar \$client;\n\n"; | ||
756 | $source .= "function $xmlrpcclassname()\n{\n"; | ||
757 | $source .= build_client_wrapper_code($client, $verbatim_client_copy, $prefix); | ||
758 | $source .= "\$this->client =& \$client;\n}\n\n"; | ||
759 | $opts = array('simple_client_copy' => 2, 'return_source' => true, | ||
760 | 'timeout' => $timeout, 'protocol' => $protocol, | ||
761 | 'encode_php_objs' => $encode_php_objects, 'prefix' => $prefix, | ||
762 | 'decode_php_objs' => $decode_php_objects | ||
763 | ); | ||
764 | /// @todo build javadoc for class definition, too | ||
765 | foreach($mlist as $mname) | ||
766 | { | ||
767 | if ($methodfilter == '' || preg_match($methodfilter, $mname)) | ||
768 | { | ||
769 | $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), | ||
770 | array('_', ''), $mname); | ||
771 | $methodwrap = wrap_xmlrpc_method($client, $mname, $opts); | ||
772 | if ($methodwrap) | ||
773 | { | ||
774 | if (!$buildit) | ||
775 | { | ||
776 | $source .= $methodwrap['docstring']; | ||
777 | } | ||
778 | $source .= $methodwrap['source']."\n"; | ||
779 | } | ||
780 | else | ||
781 | { | ||
782 | error_log('XML-RPC: will not create class method to wrap remote method '.$mname); | ||
783 | } | ||
784 | } | ||
785 | } | ||
786 | $source .= "}\n"; | ||
787 | if ($buildit) | ||
788 | { | ||
789 | $allOK = 0; | ||
790 | eval($source.'$allOK=1;'); | ||
791 | // alternative | ||
792 | //$xmlrpcfuncname = create_function('$m', $innercode); | ||
793 | if($allOK) | ||
794 | { | ||
795 | return $xmlrpcclassname; | ||
796 | } | ||
797 | else | ||
798 | { | ||
799 | error_log('XML-RPC: could not create class '.$xmlrpcclassname.' to wrap remote server '.$client->server); | ||
800 | return false; | ||
801 | } | ||
802 | } | ||
803 | else | ||
804 | { | ||
805 | return array('class' => $xmlrpcclassname, 'code' => $source, 'docstring' => ''); | ||
806 | } | ||
807 | } | ||
808 | } | ||
809 | } | ||
810 | |||
811 | /** | ||
812 | * Given the necessary info, build php code that creates a new function to | ||
813 | * invoke a remote xmlrpc method. | ||
814 | * Take care that no full checking of input parameters is done to ensure that | ||
815 | * valid php code is emitted. | ||
816 | * Note: real spaghetti code follows... | ||
817 | * @access private | ||
818 | */ | ||
819 | function build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname, | ||
820 | $msig, $mdesc='', $timeout=0, $protocol='', $client_copy_mode=0, $prefix='xmlrpc', | ||
821 | $decode_php_objects=false, $encode_php_objects=false, $decode_fault=false, | ||
822 | $fault_response='') | ||
823 | { | ||
824 | $code = "function $xmlrpcfuncname ("; | ||
825 | if ($client_copy_mode < 2) | ||
826 | { | ||
827 | // client copy mode 0 or 1 == partial / full client copy in emitted code | ||
828 | $innercode = build_client_wrapper_code($client, $client_copy_mode, $prefix); | ||
829 | $innercode .= "\$client->setDebug(\$debug);\n"; | ||
830 | $this_ = ''; | ||
831 | } | ||
832 | else | ||
833 | { | ||
834 | // client copy mode 2 == no client copy in emitted code | ||
835 | $innercode = ''; | ||
836 | $this_ = 'this->'; | ||
837 | } | ||
838 | $innercode .= "\$msg =& new {$prefix}msg('$methodname');\n"; | ||
839 | |||
840 | if ($mdesc != '') | ||
841 | { | ||
842 | // take care that PHP comment is not terminated unwillingly by method description | ||
843 | $mdesc = "/**\n* ".str_replace('*/', '* /', $mdesc)."\n"; | ||
844 | } | ||
845 | else | ||
846 | { | ||
847 | $mdesc = "/**\nFunction $xmlrpcfuncname\n"; | ||
848 | } | ||
849 | |||
850 | // param parsing | ||
851 | $plist = array(); | ||
852 | $pcount = count($msig); | ||
853 | for($i = 1; $i < $pcount; $i++) | ||
854 | { | ||
855 | $plist[] = "\$p$i"; | ||
856 | $ptype = $msig[$i]; | ||
857 | if($ptype == 'i4' || $ptype == 'int' || $ptype == 'boolean' || $ptype == 'double' || | ||
858 | $ptype == 'string' || $ptype == 'dateTime.iso8601' || $ptype == 'base64' || $ptype == 'null') | ||
859 | { | ||
860 | // only build directly xmlrpcvals when type is known and scalar | ||
861 | $innercode .= "\$p$i =& new {$prefix}val(\$p$i, '$ptype');\n"; | ||
862 | } | ||
863 | else | ||
864 | { | ||
865 | if ($encode_php_objects) | ||
866 | { | ||
867 | $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i, array('encode_php_objs'));\n"; | ||
868 | } | ||
869 | else | ||
870 | { | ||
871 | $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i);\n"; | ||
872 | } | ||
873 | } | ||
874 | $innercode .= "\$msg->addparam(\$p$i);\n"; | ||
875 | $mdesc .= '* @param '.xmlrpc_2_php_type($ptype)." \$p$i\n"; | ||
876 | } | ||
877 | if ($client_copy_mode < 2) | ||
878 | { | ||
879 | $plist[] = '$debug=0'; | ||
880 | $mdesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n"; | ||
881 | } | ||
882 | $plist = implode(', ', $plist); | ||
883 | $mdesc .= '* @return '.xmlrpc_2_php_type($msig[0])." (or an {$prefix}resp obj instance if call fails)\n*/\n"; | ||
884 | |||
885 | $innercode .= "\$res =& \${$this_}client->send(\$msg, $timeout, '$protocol');\n"; | ||
886 | if ($decode_fault) | ||
887 | { | ||
888 | if (is_string($fault_response) && ((strpos($fault_response, '%faultCode%') !== false) || (strpos($fault_response, '%faultString%') !== false))) | ||
889 | { | ||
890 | $respcode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '".str_replace("'", "''", $fault_response)."')"; | ||
891 | } | ||
892 | else | ||
893 | { | ||
894 | $respcode = var_export($fault_response, true); | ||
895 | } | ||
896 | } | ||
897 | else | ||
898 | { | ||
899 | $respcode = '$res'; | ||
900 | } | ||
901 | if ($decode_php_objects) | ||
902 | { | ||
903 | $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value(), array('decode_php_objs'));"; | ||
904 | } | ||
905 | else | ||
906 | { | ||
907 | $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value());"; | ||
908 | } | ||
909 | |||
910 | $code = $code . $plist. ") {\n" . $innercode . "\n}\n"; | ||
911 | |||
912 | return array('source' => $code, 'docstring' => $mdesc); | ||
913 | } | ||
914 | |||
915 | /** | ||
916 | * Given necessary info, generate php code that will rebuild a client object | ||
917 | * Take care that no full checking of input parameters is done to ensure that | ||
918 | * valid php code is emitted. | ||
919 | * @access private | ||
920 | */ | ||
921 | function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc') | ||
922 | { | ||
923 | $code = "\$client =& new {$prefix}_client('".str_replace("'", "\'", $client->path). | ||
924 | "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n"; | ||
925 | |||
926 | // copy all client fields to the client that will be generated runtime | ||
927 | // (this provides for future expansion or subclassing of client obj) | ||
928 | if ($verbatim_client_copy) | ||
929 | { | ||
930 | foreach($client as $fld => $val) | ||
931 | { | ||
932 | if($fld != 'debug' && $fld != 'return_type') | ||
933 | { | ||
934 | $val = var_export($val, true); | ||
935 | $code .= "\$client->$fld = $val;\n"; | ||
936 | } | ||
937 | } | ||
938 | } | ||
939 | // only make sure that client always returns the correct data type | ||
940 | $code .= "\$client->return_type = '{$prefix}vals';\n"; | ||
941 | //$code .= "\$client->setDebug(\$debug);\n"; | ||
942 | return $code; | ||
943 | } | ||
944 | ?> \ No newline at end of file | ||