fix backwards compatibility in xmlrpc_wrappers.inc
[plcapi.git] / lib / xmlrpc_wrappers.inc
index f96edd5..3c2390a 100644 (file)
-<?php\r
-/**\r
- * PHP-XMLRPC "wrapper" functions\r
- * Generate stubs to transparently access xmlrpc methods as php functions and viceversa\r
- *\r
- * @version $Id: xmlrpc_wrappers.inc,v 1.13 2008/09/20 01:23:47 ggiunta Exp $\r
- * @author Gaetano Giunta\r
- * @copyright (C) 2006-2009 G. Giunta\r
- * @license code licensed under the BSD License: http://phpxmlrpc.sourceforge.net/license.txt\r
- *\r
- * @todo separate introspection from code generation for func-2-method wrapping\r
- * @todo use some better templating system for code generation?\r
- * @todo implement method wrapping with preservation of php objs in calls\r
- * @todo when wrapping methods without obj rebuilding, use return_type = 'phpvals' (faster)\r
- * @todo implement self-parsing of php code for PHP <= 4\r
- */\r
-\r
-       // requires: xmlrpc.inc\r
-\r
-       /**\r
-       * Given a string defining a php type or phpxmlrpc type (loosely defined: strings\r
-       * accepted come from javadoc blocks), return corresponding phpxmlrpc type.\r
-       * NB: for php 'resource' types returns empty string, since resources cannot be serialized;\r
-       * for php class names returns 'struct', since php objects can be serialized as xmlrpc structs\r
-       * for php arrays always return array, even though arrays sometiles serialize as json structs\r
-       * @param string $phptype\r
-       * @return string\r
-       */\r
-       function php_2_xmlrpc_type($phptype)\r
-       {\r
-               switch(strtolower($phptype))\r
-               {\r
-                       case 'string':\r
-                               return $GLOBALS['xmlrpcString'];\r
-                       case 'integer':\r
-                       case $GLOBALS['xmlrpcInt']: // 'int'\r
-                       case $GLOBALS['xmlrpcI4']:\r
-                               return $GLOBALS['xmlrpcInt'];\r
-                       case 'double':\r
-                               return $GLOBALS['xmlrpcDouble'];\r
-                       case 'boolean':\r
-                               return $GLOBALS['xmlrpcBoolean'];\r
-                       case 'array':\r
-                               return $GLOBALS['xmlrpcArray'];\r
-                       case 'object':\r
-                               return $GLOBALS['xmlrpcStruct'];\r
-                       case $GLOBALS['xmlrpcBase64']:\r
-                       case $GLOBALS['xmlrpcStruct']:\r
-                               return strtolower($phptype);\r
-                       case 'resource':\r
-                               return '';\r
-                       default:\r
-                               if(class_exists($phptype))\r
-                               {\r
-                                       return $GLOBALS['xmlrpcStruct'];\r
-                               }\r
-                               else\r
-                               {\r
-                                       // unknown: might be any 'extended' xmlrpc type\r
-                                       return $GLOBALS['xmlrpcValue'];\r
-                               }\r
-               }\r
-       }\r
-\r
-       /**\r
-       * Given a string defining a phpxmlrpc type return corresponding php type.\r
-       * @param string $xmlrpctype\r
-       * @return string\r
-       */\r
-       function xmlrpc_2_php_type($xmlrpctype)\r
-       {\r
-               switch(strtolower($xmlrpctype))\r
-               {\r
-                       case 'base64':\r
-                       case 'datetime.iso8601':\r
-                       case 'string':\r
-                               return $GLOBALS['xmlrpcString'];\r
-                       case 'int':\r
-                       case 'i4':\r
-                               return 'integer';\r
-                       case 'struct':\r
-                       case 'array':\r
-                               return 'array';\r
-                       case 'double':\r
-                               return 'float';\r
-                       case 'undefined':\r
-                               return 'mixed';\r
-                       case 'boolean':\r
-                       case 'null':\r
-                       default:\r
-                               // unknown: might be any xmlrpc type\r
-                               return strtolower($xmlrpctype);\r
-               }\r
-       }\r
-\r
-       /**\r
-       * Given a user-defined PHP function, create a PHP 'wrapper' function that can\r
-       * be exposed as xmlrpc method from an xmlrpc_server object and called from remote\r
-       * clients (as well as its corresponding signature info).\r
-       *\r
-       * Since php is a typeless language, to infer types of input and output parameters,\r
-       * it relies on parsing the javadoc-style comment block associated with the given\r
-       * function. Usage of xmlrpc native types (such as datetime.dateTime.iso8601 and base64)\r
-       * in the @param tag is also allowed, if you need the php function to receive/send\r
-       * data in that particular format (note that base64 encoding/decoding is transparently\r
-       * carried out by the lib, while datetime vals are passed around as strings)\r
-       *\r
-       * Known limitations:\r
-       * - requires PHP 5.0.3 +\r
-       * - only works for user-defined functions, not for PHP internal functions\r
-       *   (reflection does not support retrieving number/type of params for those)\r
-       * - functions returning php objects will generate special xmlrpc responses:\r
-       *   when the xmlrpc decoding of those responses is carried out by this same lib, using\r
-       *   the appropriate param in php_xmlrpc_decode, the php objects will be rebuilt.\r
-       *   In short: php objects can be serialized, too (except for their resource members),\r
-       *   using this function.\r
-       *   Other libs might choke on the very same xml that will be generated in this case\r
-       *   (i.e. it has a nonstandard attribute on struct element tags)\r
-       * - usage of javadoc @param tags using param names in a different order from the\r
-       *   function prototype is not considered valid (to be fixed?)\r
-       *\r
-       * Note that since rel. 2.0RC3 the preferred method to have the server call 'standard'\r
-       * php functions (ie. functions not expecting a single xmlrpcmsg obj as parameter)\r
-       * is by making use of the functions_parameters_type class member.\r
-       *\r
-       * @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\r
-       * @param string $newfuncname (optional) name for function to be created\r
-       * @param array $extra_options (optional) array of options for conversion. valid values include:\r
-       *        bool  return_source when true, php code w. function definition will be returned, not evaluated\r
-       *        bool  encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects\r
-       *        bool  decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers ---\r
-       *        bool  suppress_warnings  remove from produced xml any runtime warnings due to the php function being invoked\r
-       * @return false on error, or an array containing the name of the new php function,\r
-       *         its signature and docs, to be used in the server dispatch map\r
-       *\r
-       * @todo decide how to deal with params passed by ref: bomb out or allow?\r
-       * @todo finish using javadoc info to build method sig if all params are named but out of order\r
-       * @todo add a check for params of 'resource' type\r
-       * @todo add some trigger_errors / error_log when returning false?\r
-       * @todo what to do when the PHP function returns NULL? we are currently returning an empty string value...\r
-       * @todo add an option to suppress php warnings in invocation of user function, similar to server debug level 3?\r
-       * @todo if $newfuncname is empty, we could use create_user_func instead of eval, as it is possibly faster\r
-       * @todo add a verbatim_object_copy parameter to allow avoiding the same obj instance?\r
-       */\r
-       function wrap_php_function($funcname, $newfuncname='', $extra_options=array())\r
-       {\r
-               $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true;\r
-               $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc';\r
-               $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false;\r
-               $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false;\r
-               $catch_warnings = isset($extra_options['suppress_warnings']) && $extra_options['suppress_warnings'] ? '@' : '';\r
-\r
-               if(version_compare(phpversion(), '5.0.3') == -1)\r
-               {\r
-                       // up to php 5.0.3 some useful reflection methods were missing\r
-                       error_log('XML-RPC: cannot not wrap php functions unless running php version bigger than 5.0.3');\r
-                       return false;\r
-               }\r
-\r
-        $exists = false;\r
-        if(is_array($funcname))\r
-        {\r
-            if(count($funcname) < 2 || (!is_string($funcname[0]) && !is_object($funcname[0])))\r
-            {\r
-                       error_log('XML-RPC: syntax for function to be wrapped is wrong');\r
-                       return false;\r
-            }\r
-            if(is_string($funcname[0]))\r
-            {\r
-                $plainfuncname = implode('::', $funcname);\r
-            }\r
-            elseif(is_object($funcname[0]))\r
-            {\r
-                $plainfuncname = get_class($funcname[0]) . '->' . $funcname[1];\r
-            }\r
-            $exists = method_exists($funcname[0], $funcname[1]);\r
-        }\r
-        else\r
-        {\r
-            $plainfuncname = $funcname;\r
-            $exists = function_exists($funcname);\r
-        }\r
-\r
-               if(!$exists)\r
-               {\r
-                       error_log('XML-RPC: function to be wrapped is not defined: '.$plainfuncname);\r
-                       return false;\r
-               }\r
-               else\r
-               {\r
-                       // determine name of new php function\r
-                       if($newfuncname == '')\r
-                       {\r
-                               if(is_array($funcname))\r
-                               {\r
-                               if(is_string($funcname[0]))\r
-                                       $xmlrpcfuncname = "{$prefix}_".implode('_', $funcname);\r
-                               else\r
-                                       $xmlrpcfuncname = "{$prefix}_".get_class($funcname[0]) . '_' . $funcname[1];\r
-                               }\r
-                               else\r
-                               {\r
-                                       $xmlrpcfuncname = "{$prefix}_$funcname";\r
-                               }\r
-                       }\r
-                       else\r
-                       {\r
-                               $xmlrpcfuncname = $newfuncname;\r
-                       }\r
-                       while($buildit && function_exists($xmlrpcfuncname))\r
-                       {\r
-                               $xmlrpcfuncname .= 'x';\r
-                       }\r
-\r
-                       // start to introspect PHP code\r
-                       if(is_array($funcname))\r
-                       {\r
-                       $func = new ReflectionMethod($funcname[0], $funcname[1]);\r
-                       if($func->isPrivate())\r
-                       {\r
-                               error_log('XML-RPC: method to be wrapped is private: '.$plainfuncname);\r
-                               return false;\r
-                       }\r
-                       if($func->isProtected())\r
-                       {\r
-                               error_log('XML-RPC: method to be wrapped is protected: '.$plainfuncname);\r
-                               return false;\r
-                       }\r
-                       if($func->isConstructor())\r
-                       {\r
-                               error_log('XML-RPC: method to be wrapped is the constructor: '.$plainfuncname);\r
-                               return false;\r
-                       }\r
-                       if($func->isDestructor())\r
-                       {\r
-                               error_log('XML-RPC: method to be wrapped is the destructor: '.$plainfuncname);\r
-                               return false;\r
-                       }\r
-                       if($func->isAbstract())\r
-                       {\r
-                               error_log('XML-RPC: method to be wrapped is abstract: '.$plainfuncname);\r
-                               return false;\r
-                       }\r
-                /// @todo add more checks for static vs. nonstatic?\r
-            }\r
-                       else\r
-                       {\r
-                       $func = new ReflectionFunction($funcname);\r
-            }\r
-                       if($func->isInternal())\r
-                       {\r
-                               // Note: from PHP 5.1.0 onward, we will possibly be able to use invokeargs\r
-                               // instead of getparameters to fully reflect internal php functions ?\r
-                               error_log('XML-RPC: function to be wrapped is internal: '.$plainfuncname);\r
-                               return false;\r
-                       }\r
-\r
-                       // retrieve parameter names, types and description from javadoc comments\r
-\r
-                       // function description\r
-                       $desc = '';\r
-                       // type of return val: by default 'any'\r
-                       $returns = $GLOBALS['xmlrpcValue'];\r
-                       // desc of return val\r
-                       $returnsDocs = '';\r
-                       // type + name of function parameters\r
-                       $paramDocs = array();\r
-\r
-                       $docs = $func->getDocComment();\r
-                       if($docs != '')\r
-                       {\r
-                               $docs = explode("\n", $docs);\r
-                               $i = 0;\r
-                               foreach($docs as $doc)\r
-                               {\r
-                                       $doc = trim($doc, " \r\t/*");\r
-                                       if(strlen($doc) && strpos($doc, '@') !== 0 && !$i)\r
-                                       {\r
-                                               if($desc)\r
-                                               {\r
-                                                       $desc .= "\n";\r
-                                               }\r
-                                               $desc .= $doc;\r
-                                       }\r
-                                       elseif(strpos($doc, '@param') === 0)\r
-                                       {\r
-                                               // syntax: @param type [$name] desc\r
-                                               if(preg_match('/@param\s+(\S+)(\s+\$\S+)?\s+(.+)/', $doc, $matches))\r
-                                               {\r
-                                                       if(strpos($matches[1], '|'))\r
-                                                       {\r
-                                                               //$paramDocs[$i]['type'] = explode('|', $matches[1]);\r
-                                                               $paramDocs[$i]['type'] = 'mixed';\r
-                                                       }\r
-                                                       else\r
-                                                       {\r
-                                                               $paramDocs[$i]['type'] = $matches[1];\r
-                                                       }\r
-                                                       $paramDocs[$i]['name'] = trim($matches[2]);\r
-                                                       $paramDocs[$i]['doc'] = $matches[3];\r
-                                               }\r
-                                               $i++;\r
-                                       }\r
-                                       elseif(strpos($doc, '@return') === 0)\r
-                                       {\r
-                                               // syntax: @return type desc\r
-                                               //$returns = preg_split('/\s+/', $doc);\r
-                                               if(preg_match('/@return\s+(\S+)\s+(.+)/', $doc, $matches))\r
-                                               {\r
-                                                       $returns = php_2_xmlrpc_type($matches[1]);\r
-                                                       if(isset($matches[2]))\r
-                                                       {\r
-                                                               $returnsDocs = $matches[2];\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-\r
-                       // execute introspection of actual function prototype\r
-                       $params = array();\r
-                       $i = 0;\r
-                       foreach($func->getParameters() as $paramobj)\r
-                       {\r
-                               $params[$i] = array();\r
-                               $params[$i]['name'] = '$'.$paramobj->getName();\r
-                               $params[$i]['isoptional'] = $paramobj->isOptional();\r
-                               $i++;\r
-                       }\r
-\r
-\r
-                       // start  building of PHP code to be eval'd\r
-                       $innercode = '';\r
-                       $i = 0;\r
-                       $parsvariations = array();\r
-                       $pars = array();\r
-                       $pnum = count($params);\r
-                       foreach($params as $param)\r
-                       {\r
-                               if (isset($paramDocs[$i]['name']) && $paramDocs[$i]['name'] && strtolower($paramDocs[$i]['name']) != strtolower($param['name']))\r
-                               {\r
-                                       // param name from phpdoc info does not match param definition!\r
-                                       $paramDocs[$i]['type'] = 'mixed';\r
-                               }\r
-\r
-                               if($param['isoptional'])\r
-                               {\r
-                                       // this particular parameter is optional. save as valid previous list of parameters\r
-                                       $innercode .= "if (\$paramcount > $i) {\n";\r
-                                       $parsvariations[] = $pars;\r
-                               }\r
-                               $innercode .= "\$p$i = \$msg->getParam($i);\n";\r
-                               if ($decode_php_objects)\r
-                               {\r
-                                       $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i, array('decode_php_objs'));\n";\r
-                               }\r
-                               else\r
-                               {\r
-                                       $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i);\n";\r
-                               }\r
-\r
-                               $pars[] = "\$p$i";\r
-                               $i++;\r
-                               if($param['isoptional'])\r
-                               {\r
-                                       $innercode .= "}\n";\r
-                               }\r
-                               if($i == $pnum)\r
-                               {\r
-                                       // last allowed parameters combination\r
-                                       $parsvariations[] = $pars;\r
-                               }\r
-                       }\r
-\r
-                       $sigs = array();\r
-                       $psigs = array();\r
-                       if(count($parsvariations) == 0)\r
-                       {\r
-                               // only known good synopsis = no parameters\r
-                               $parsvariations[] = array();\r
-                               $minpars = 0;\r
-                       }\r
-                       else\r
-                       {\r
-                               $minpars = count($parsvariations[0]);\r
-                       }\r
-\r
-                       if($minpars)\r
-                       {\r
-                               // add to code the check for min params number\r
-                               // NB: this check needs to be done BEFORE decoding param values\r
-                               $innercode = "\$paramcount = \$msg->getNumParams();\n" .\r
-                               "if (\$paramcount < $minpars) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}');\n" . $innercode;\r
-                       }\r
-                       else\r
-                       {\r
-                               $innercode = "\$paramcount = \$msg->getNumParams();\n" . $innercode;\r
-                       }\r
-\r
-                       $innercode .= "\$np = false;\n";\r
-                       // since there are no closures in php, if we are given an object instance,\r
-            // we store a pointer to it in a global var...\r
-                       if ( is_array($funcname) && is_object($funcname[0]) )\r
-                       {\r
-                           $GLOBALS['xmlrpcWPFObjHolder'][$xmlrpcfuncname] =& $funcname[0];\r
-                           $innercode .= "\$obj =& \$GLOBALS['xmlrpcWPFObjHolder']['$xmlrpcfuncname'];\n";\r
-                           $realfuncname = '$obj->'.$funcname[1];\r
-                       }\r
-                       else\r
-                       {\r
-                       $realfuncname = $plainfuncname;\r
-            }\r
-                       foreach($parsvariations as $pars)\r
-                       {\r
-                               $innercode .= "if (\$paramcount == " . count($pars) . ") \$retval = {$catch_warnings}$realfuncname(" . implode(',', $pars) . "); else\n";\r
-                               // build a 'generic' signature (only use an appropriate return type)\r
-                               $sig = array($returns);\r
-                               $psig = array($returnsDocs);\r
-                               for($i=0; $i < count($pars); $i++)\r
-                               {\r
-                                       if (isset($paramDocs[$i]['type']))\r
-                                       {\r
-                                               $sig[] = php_2_xmlrpc_type($paramDocs[$i]['type']);\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               $sig[] = $GLOBALS['xmlrpcValue'];\r
-                                       }\r
-                                       $psig[] = isset($paramDocs[$i]['doc']) ? $paramDocs[$i]['doc'] : '';\r
-                               }\r
-                               $sigs[] = $sig;\r
-                               $psigs[] = $psig;\r
-                       }\r
-                       $innercode .= "\$np = true;\n";\r
-                       $innercode .= "if (\$np) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}'); else {\n";\r
-                       //$innercode .= "if (\$_xmlrpcs_error_occurred) return new xmlrpcresp(0, $GLOBALS['xmlrpcerr']user, \$_xmlrpcs_error_occurred); else\n";\r
-                       $innercode .= "if (is_a(\$retval, '{$prefix}resp')) return \$retval; else\n";\r
-                       if($returns == $GLOBALS['xmlrpcDateTime'] || $returns == $GLOBALS['xmlrpcBase64'])\r
-                       {\r
-                               $innercode .= "return new {$prefix}resp(new {$prefix}val(\$retval, '$returns'));";\r
-                       }\r
-                       else\r
-                       {\r
-                               if ($encode_php_objects)\r
-                                       $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval, array('encode_php_objs')));\n";\r
-                               else\r
-                                       $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval));\n";\r
-                       }\r
-                       // shall we exclude functions returning by ref?\r
-                       // if($func->returnsReference())\r
-                       //      return false;\r
-                       $code = "function $xmlrpcfuncname(\$msg) {\n" . $innercode . "}\n}";\r
-                       //print_r($code);\r
-                       if ($buildit)\r
-                       {\r
-                               $allOK = 0;\r
-                               eval($code.'$allOK=1;');\r
-                               // alternative\r
-                               //$xmlrpcfuncname = create_function('$m', $innercode);\r
-\r
-                               if(!$allOK)\r
-                               {\r
-                                       error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap php function '.$plainfuncname);\r
-                                       return false;\r
-                               }\r
-                       }\r
-\r
-                       /// @todo examine if $paramDocs matches $parsvariations and build array for\r
-                       /// usage as method signature, plus put together a nice string for docs\r
-\r
-                       $ret = array('function' => $xmlrpcfuncname, 'signature' => $sigs, 'docstring' => $desc, 'signature_docs' => $psigs, 'source' => $code);\r
-                       return $ret;\r
-               }\r
-       }\r
-\r
-    /**\r
-    * Given a user-defined PHP class or php object, map its methods onto a list of\r
-       * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc_server\r
-       * object and called from remote clients (as well as their corresponding signature info).\r
-       *\r
-    * @param mixed $classname the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class\r
-    * @param array $extra_options see the docs for wrap_php_method for more options\r
-    *        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\r
-    * @return array or false on failure\r
-    *\r
-    * @todo get_class_methods will return both static and non-static methods.\r
-    *       we have to differentiate the action, depending on wheter we recived a class name or object\r
-    */\r
-    function wrap_php_class($classname, $extra_options=array())\r
-    {\r
-               $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : '';\r
-               $methodtype = isset($extra_options['method_type']) ? $extra_options['method_type'] : 'auto';\r
-\r
-        if(version_compare(phpversion(), '5.0.3') == -1)\r
-               {\r
-                       // up to php 5.0.3 some useful reflection methods were missing\r
-                       error_log('XML-RPC: cannot not wrap php functions unless running php version bigger than 5.0.3');\r
-                       return false;\r
-               }\r
-\r
-        $result = array();\r
-               $mlist = get_class_methods($classname);\r
-               foreach($mlist as $mname)\r
-               {\r
-               if ($methodfilter == '' || preg_match($methodfilter, $mname))\r
-                       {\r
-                       // echo $mlist."\n";\r
-                       $func = new ReflectionMethod($classname, $mname);\r
-                       if(!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract())\r
-                       {\r
-                               if(($func->isStatic && ($methodtype == 'all' || $methodtype == 'static' || ($methodtype == 'auto' && is_string($classname)))) ||\r
-                               (!$func->isStatic && ($methodtype == 'all' || $methodtype == 'nonstatic' || ($methodtype == 'auto' && is_object($classname)))))\r
-                       {\r
-                        $methodwrap = wrap_php_function(array($classname, $mname), '', $extra_options);\r
-                        if ( $methodwrap )\r
-                        {\r
-                            $result[$methodwrap['function']] = $methodwrap['function'];\r
-                        }\r
-                    }\r
-                       }\r
-                       }\r
-               }\r
-        return $result;\r
-    }\r
-\r
-       /**\r
-       * Given an xmlrpc client and a method name, register a php wrapper function\r
-       * that will call it and return results using native php types for both\r
-       * params and results. The generated php function will return an xmlrpcresp\r
-       * oject for failed xmlrpc calls\r
-       *\r
-       * Known limitations:\r
-       * - server must support system.methodsignature for the wanted xmlrpc method\r
-       * - for methods that expose many signatures, only one can be picked (we\r
-       *   could in priciple check if signatures differ only by number of params\r
-       *   and not by type, but it would be more complication than we can spare time)\r
-       * - nested xmlrpc params: the caller of the generated php function has to\r
-       *   encode on its own the params passed to the php function if these are structs\r
-       *   or arrays whose (sub)members include values of type datetime or base64\r
-       *\r
-       * Notes: the connection properties of the given client will be copied\r
-       * and reused for the connection used during the call to the generated\r
-       * php function.\r
-       * Calling the generated php function 'might' be slow: a new xmlrpc client\r
-       * is created on every invocation and an xmlrpc-connection opened+closed.\r
-       * An extra 'debug' param is appended to param list of xmlrpc method, useful\r
-       * for debugging purposes.\r
-       *\r
-       * @param xmlrpc_client $client     an xmlrpc client set up correctly to communicate with target server\r
-       * @param string        $methodname the xmlrpc method to be mapped to a php function\r
-       * @param array         $extra_options array of options that specify conversion details. valid ptions include\r
-       *        integer       signum      the index of the method signature to use in mapping (if method exposes many sigs)\r
-       *        integer       timeout     timeout (in secs) to be used when executing function/calling remote method\r
-       *        string        protocol    'http' (default), 'http11' or 'https'\r
-       *        string        new_function_name the name of php function to create. If unsepcified, lib will pick an appropriate name\r
-       *        string        return_source if true return php code w. function definition instead fo function name\r
-       *        bool          encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects\r
-       *        bool          decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers ---\r
-       *        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\r
-       *        bool          debug        set it to 1 or 2 to see debug results of querying server for method synopsis\r
-       * @return string                   the name of the generated php function (or false) - OR AN ARRAY...\r
-       */\r
-       function wrap_xmlrpc_method($client, $methodname, $extra_options=0, $timeout=0, $protocol='', $newfuncname='')\r
-       {\r
-               // mind numbing: let caller use sane calling convention (as per javadoc, 3 params),\r
-               // OR the 2.0 calling convention (no options) - we really love backward compat, don't we?\r
-               if (!is_array($extra_options))\r
-               {\r
-                       $signum = $extra_options;\r
-                       $extra_options = array();\r
-               }\r
-               else\r
-               {\r
-                       $signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0;\r
-                       $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0;\r
-                       $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : '';\r
-                       $newfuncname = isset($extra_options['new_function_name']) ? $extra_options['new_function_name'] : '';\r
-               }\r
-               //$encode_php_objects = in_array('encode_php_objects', $extra_options);\r
-               //$verbatim_client_copy = in_array('simple_client_copy', $extra_options) ? 1 :\r
-               //      in_array('build_class_code', $extra_options) ? 2 : 0;\r
-\r
-               $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false;\r
-               $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false;\r
-               $simple_client_copy = isset($extra_options['simple_client_copy']) ? (int)($extra_options['simple_client_copy']) : 0;\r
-               $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true;\r
-               $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc';\r
-               if (isset($extra_options['return_on_fault']))\r
-               {\r
-                       $decode_fault = true;\r
-                       $fault_response = $extra_options['return_on_fault'];\r
-               }\r
-               else\r
-               {\r
-                       $decode_fault = false;\r
-                       $fault_response = '';\r
-               }\r
-               $debug = isset($extra_options['debug']) ? ($extra_options['debug']) : 0;\r
-\r
-               $msgclass = $prefix.'msg';\r
-               $valclass = $prefix.'val';\r
-               $decodefunc = 'php_'.$prefix.'_decode';\r
-\r
-               $msg = new $msgclass('system.methodSignature');\r
-               $msg->addparam(new $valclass($methodname));\r
-               $client->setDebug($debug);\r
-               $response =& $client->send($msg, $timeout, $protocol);\r
-               if($response->faultCode())\r
-               {\r
-                       error_log('XML-RPC: could not retrieve method signature from remote server for method '.$methodname);\r
-                       return false;\r
-               }\r
-               else\r
-               {\r
-                       $msig = $response->value();\r
-                       if ($client->return_type != 'phpvals')\r
-                       {\r
-                               $msig = $decodefunc($msig);\r
-                       }\r
-                       if(!is_array($msig) || count($msig) <= $signum)\r
-                       {\r
-                               error_log('XML-RPC: could not retrieve method signature nr.'.$signum.' from remote server for method '.$methodname);\r
-                               return false;\r
-                       }\r
-                       else\r
-                       {\r
-                               // pick a suitable name for the new function, avoiding collisions\r
-                               if($newfuncname != '')\r
-                               {\r
-                                       $xmlrpcfuncname = $newfuncname;\r
-                               }\r
-                               else\r
-                               {\r
-                                       // take care to insure that methodname is translated to valid\r
-                                       // php function name\r
-                                       $xmlrpcfuncname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'),\r
-                                               array('_', ''), $methodname);\r
-                               }\r
-                               while($buildit && function_exists($xmlrpcfuncname))\r
-                               {\r
-                                       $xmlrpcfuncname .= 'x';\r
-                               }\r
-\r
-                               $msig = $msig[$signum];\r
-                               $mdesc = '';\r
-                               // if in 'offline' mode, get method description too.\r
-                               // in online mode, favour speed of operation\r
-                               if(!$buildit)\r
-                               {\r
-                                       $msg = new $msgclass('system.methodHelp');\r
-                                       $msg->addparam(new $valclass($methodname));\r
-                                       $response =& $client->send($msg, $timeout, $protocol);\r
-                                       if (!$response->faultCode())\r
-                                       {\r
-                                               $mdesc = $response->value();\r
-                                               if ($client->return_type != 'phpvals')\r
-                                               {\r
-                                                       $mdesc = $mdesc->scalarval();\r
-                                               }\r
-                                       }\r
-                               }\r
-\r
-                               $results = build_remote_method_wrapper_code($client, $methodname,\r
-                                       $xmlrpcfuncname, $msig, $mdesc, $timeout, $protocol, $simple_client_copy,\r
-                                       $prefix, $decode_php_objects, $encode_php_objects, $decode_fault,\r
-                                       $fault_response);\r
-\r
-                               //print_r($code);\r
-                               if ($buildit)\r
-                               {\r
-                                       $allOK = 0;\r
-                                       eval($results['source'].'$allOK=1;');\r
-                                       // alternative\r
-                                       //$xmlrpcfuncname = create_function('$m', $innercode);\r
-                                       if($allOK)\r
-                                       {\r
-                                               return $xmlrpcfuncname;\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap remote method '.$methodname);\r
-                                               return false;\r
-                                       }\r
-                               }\r
-                               else\r
-                               {\r
-                                       $results['function'] = $xmlrpcfuncname;\r
-                                       return $results;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       /**\r
-       * Similar to wrap_xmlrpc_method, but will generate a php class that wraps\r
-       * all xmlrpc methods exposed by the remote server as own methods.\r
-       * For more details see wrap_xmlrpc_method.\r
-       * @param xmlrpc_client $client the client obj all set to query the desired server\r
-       * @param array $extra_options list of options for wrapped code\r
-       * @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)\r
-       */\r
-       function wrap_xmlrpc_server($client, $extra_options=array())\r
-       {\r
-               $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : '';\r
-               //$signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0;\r
-               $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0;\r
-               $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : '';\r
-               $newclassname = isset($extra_options['new_class_name']) ? $extra_options['new_class_name'] : '';\r
-               $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false;\r
-               $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false;\r
-               $verbatim_client_copy = isset($extra_options['simple_client_copy']) ? !($extra_options['simple_client_copy']) : true;\r
-               $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true;\r
-               $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc';\r
-\r
-               $msgclass = $prefix.'msg';\r
-               //$valclass = $prefix.'val';\r
-               $decodefunc = 'php_'.$prefix.'_decode';\r
-\r
-               $msg = new $msgclass('system.listMethods');\r
-               $response =& $client->send($msg, $timeout, $protocol);\r
-               if($response->faultCode())\r
-               {\r
-                       error_log('XML-RPC: could not retrieve method list from remote server');\r
-                       return false;\r
-               }\r
-               else\r
-               {\r
-                       $mlist = $response->value();\r
-                       if ($client->return_type != 'phpvals')\r
-                       {\r
-                               $mlist = $decodefunc($mlist);\r
-                       }\r
-                       if(!is_array($mlist) || !count($mlist))\r
-                       {\r
-                               error_log('XML-RPC: could not retrieve meaningful method list from remote server');\r
-                               return false;\r
-                       }\r
-                       else\r
-                       {\r
-                               // pick a suitable name for the new function, avoiding collisions\r
-                               if($newclassname != '')\r
-                               {\r
-                                       $xmlrpcclassname = $newclassname;\r
-                               }\r
-                               else\r
-                               {\r
-                                       $xmlrpcclassname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'),\r
-                                               array('_', ''), $client->server).'_client';\r
-                               }\r
-                               while($buildit && class_exists($xmlrpcclassname))\r
-                               {\r
-                                       $xmlrpcclassname .= 'x';\r
-                               }\r
-\r
-                               /// @todo add function setdebug() to new class, to enable/disable debugging\r
-                               $source = "class $xmlrpcclassname\n{\nvar \$client;\n\n";\r
-                               $source .= "function $xmlrpcclassname()\n{\n";\r
-                               $source .= build_client_wrapper_code($client, $verbatim_client_copy, $prefix);\r
-                               $source .= "\$this->client =& \$client;\n}\n\n";\r
-                               $opts = array('simple_client_copy' => 2, 'return_source' => true,\r
-                                       'timeout' => $timeout, 'protocol' => $protocol,\r
-                                       'encode_php_objs' => $encode_php_objects, 'prefix' => $prefix,\r
-                                       'decode_php_objs' => $decode_php_objects\r
-                                       );\r
-                               /// @todo build javadoc for class definition, too\r
-                               foreach($mlist as $mname)\r
-                               {\r
-                                       if ($methodfilter == '' || preg_match($methodfilter, $mname))\r
-                                       {\r
-                                               $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'),\r
-                                                       array('_', ''), $mname);\r
-                                               $methodwrap = wrap_xmlrpc_method($client, $mname, $opts);\r
-                                               if ($methodwrap)\r
-                                               {\r
-                                                       if (!$buildit)\r
-                                                       {\r
-                                                               $source .= $methodwrap['docstring'];\r
-                                                       }\r
-                                                       $source .= $methodwrap['source']."\n";\r
-                                               }\r
-                                               else\r
-                                               {\r
-                                                       error_log('XML-RPC: will not create class method to wrap remote method '.$mname);\r
-                                               }\r
-                                       }\r
-                               }\r
-                               $source .= "}\n";\r
-                               if ($buildit)\r
-                               {\r
-                                       $allOK = 0;\r
-                                       eval($source.'$allOK=1;');\r
-                                       // alternative\r
-                                       //$xmlrpcfuncname = create_function('$m', $innercode);\r
-                                       if($allOK)\r
-                                       {\r
-                                               return $xmlrpcclassname;\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               error_log('XML-RPC: could not create class '.$xmlrpcclassname.' to wrap remote server '.$client->server);\r
-                                               return false;\r
-                                       }\r
-                               }\r
-                               else\r
-                               {\r
-                                       return array('class' => $xmlrpcclassname, 'code' => $source, 'docstring' => '');\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       /**\r
-       * Given the necessary info, build php code that creates a new function to\r
-       * invoke a remote xmlrpc method.\r
-       * Take care that no full checking of input parameters is done to ensure that\r
-       * valid php code is emitted.\r
-       * Note: real spaghetti code follows...\r
-       * @access private\r
-       */\r
-       function build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname,\r
-               $msig, $mdesc='', $timeout=0, $protocol='', $client_copy_mode=0, $prefix='xmlrpc',\r
-               $decode_php_objects=false, $encode_php_objects=false, $decode_fault=false,\r
-               $fault_response='')\r
-       {\r
-               $code = "function $xmlrpcfuncname (";\r
-               if ($client_copy_mode < 2)\r
-               {\r
-                       // client copy mode 0 or 1 == partial / full client copy in emitted code\r
-                       $innercode = build_client_wrapper_code($client, $client_copy_mode, $prefix);\r
-                       $innercode .= "\$client->setDebug(\$debug);\n";\r
-                       $this_ = '';\r
-               }\r
-               else\r
-               {\r
-                       // client copy mode 2 == no client copy in emitted code\r
-                       $innercode = '';\r
-                       $this_ = 'this->';\r
-               }\r
-               $innercode .= "\$msg = new {$prefix}msg('$methodname');\n";\r
-\r
-               if ($mdesc != '')\r
-               {\r
-                       // take care that PHP comment is not terminated unwillingly by method description\r
-                       $mdesc = "/**\n* ".str_replace('*/', '* /', $mdesc)."\n";\r
-               }\r
-               else\r
-               {\r
-                       $mdesc = "/**\nFunction $xmlrpcfuncname\n";\r
-               }\r
-\r
-               // param parsing\r
-               $plist = array();\r
-               $pcount = count($msig);\r
-               for($i = 1; $i < $pcount; $i++)\r
-               {\r
-                       $plist[] = "\$p$i";\r
-                       $ptype = $msig[$i];\r
-                       if($ptype == 'i4' || $ptype == 'int' || $ptype == 'boolean' || $ptype == 'double' ||\r
-                               $ptype == 'string' || $ptype == 'dateTime.iso8601' || $ptype == 'base64' || $ptype == 'null')\r
-                       {\r
-                               // only build directly xmlrpcvals when type is known and scalar\r
-                               $innercode .= "\$p$i = new {$prefix}val(\$p$i, '$ptype');\n";\r
-                       }\r
-                       else\r
-                       {\r
-                               if ($encode_php_objects)\r
-                               {\r
-                                       $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i, array('encode_php_objs'));\n";\r
-                               }\r
-                               else\r
-                               {\r
-                                       $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i);\n";\r
-                               }\r
-                       }\r
-                       $innercode .= "\$msg->addparam(\$p$i);\n";\r
-                       $mdesc .= '* @param '.xmlrpc_2_php_type($ptype)." \$p$i\n";\r
-               }\r
-               if ($client_copy_mode < 2)\r
-               {\r
-                       $plist[] = '$debug=0';\r
-                       $mdesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";\r
-               }\r
-               $plist = implode(', ', $plist);\r
-               $mdesc .= '* @return '.xmlrpc_2_php_type($msig[0])." (or an {$prefix}resp obj instance if call fails)\n*/\n";\r
-\r
-               $innercode .= "\$res =& \${$this_}client->send(\$msg, $timeout, '$protocol');\n";\r
-               if ($decode_fault)\r
-               {\r
-                       if (is_string($fault_response) && ((strpos($fault_response, '%faultCode%') !== false) || (strpos($fault_response, '%faultString%') !== false)))\r
-                       {\r
-                               $respcode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '".str_replace("'", "''", $fault_response)."')";\r
-                       }\r
-                       else\r
-                       {\r
-                               $respcode = var_export($fault_response, true);\r
-                       }\r
-               }\r
-               else\r
-               {\r
-                       $respcode = '$res';\r
-               }\r
-               if ($decode_php_objects)\r
-               {\r
-                       $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value(), array('decode_php_objs'));";\r
-               }\r
-               else\r
-               {\r
-                       $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value());";\r
-               }\r
-\r
-               $code = $code . $plist. ") {\n" . $innercode . "\n}\n";\r
-\r
-               return array('source' => $code, 'docstring' => $mdesc);\r
-       }\r
-\r
-       /**\r
-       * Given necessary info, generate php code that will rebuild a client object\r
-       * Take care that no full checking of input parameters is done to ensure that\r
-       * valid php code is emitted.\r
-       * @access private\r
-       */\r
-       function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc')\r
-       {\r
-               $code = "\$client = new {$prefix}_client('".str_replace("'", "\'", $client->path).\r
-                       "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n";\r
-\r
-               // copy all client fields to the client that will be generated runtime\r
-               // (this provides for future expansion or subclassing of client obj)\r
-               if ($verbatim_client_copy)\r
-               {\r
-                       foreach($client as $fld => $val)\r
-                       {\r
-                               if($fld != 'debug' && $fld != 'return_type')\r
-                               {\r
-                                       $val = var_export($val, true);\r
-                                       $code .= "\$client->$fld = $val;\n";\r
-                               }\r
-                       }\r
-               }\r
-               // only make sure that client always returns the correct data type\r
-               $code .= "\$client->return_type = '{$prefix}vals';\n";\r
-               //$code .= "\$client->setDebug(\$debug);\n";\r
-               return $code;\r
-       }\r
-?>
\ No newline at end of file
+<?php
+
+/******************************************************************************
+ *
+ * *** DEPRECATED ***
+ *
+ * This file is only used to insure backwards compatibility
+ * with the API of the library <= rev. 3
+ *****************************************************************************/
+
+include_once(__DIR__.'/../src/Wrapper.php');
+
+/* Expose as global functions the ones which are now class methods */
+
+/**
+ * @see PhpXmlRpc\Wrapper::php_2_xmlrpc_type
+ * @param string $phpType
+ * @return string
+ */
+function php_2_xmlrpc_type($phpType)
+{
+    $wrapper = new PhpXmlRpc\Wrapper();
+    return $wrapper->php2XmlrpcType($phpType);
+}
+
+/**
+ * @see PhpXmlRpc\Wrapper::xmlrpc_2_php_type
+ * @param string $xmlrpcType
+ * @return string
+ */
+function xmlrpc_2_php_type($xmlrpcType)
+{
+    $wrapper = new PhpXmlRpc\Wrapper();
+    return $wrapper->xmlrpc2PhpType($xmlrpcType);
+}
+
+/**
+ * @see PhpXmlRpc\Wrapper::wrap_php_function
+ * @param callable $funcName
+ * @param string $newFuncName
+ * @param array $extraOptions
+ * @return array|false
+ */
+function wrap_php_function($funcName, $newFuncName='', $extraOptions=array())
+{
+    $wrapper = new PhpXmlRpc\Wrapper();
+    if (!isset($extraOptions['return_source'])  || $extraOptions['return_source'] == false) {
+        // backwards compat: return string instead of callable
+        $extraOptions['return_source'] = true;
+        $wrapped = $wrapper->wrapPhpFunction($funcName, $newFuncName, $extraOptions);
+        eval($wrapped['source']);
+    } else {
+        $wrapped = $wrapper->wrapPhpFunction($funcName, $newFuncName, $extraOptions);
+    }
+    return $wrapped;
+}
+
+/**
+ * NB: this function returns an array in a format which is unsuitable for direct use in the server dispatch map, unlike
+ * PhpXmlRpc\Wrapper::wrapPhpClass. This behaviour might seem like a bug, but has been kept for backwards compatibility.
+ *
+ * @see PhpXmlRpc\Wrapper::wrap_php_class
+ * @param string|object $className
+ * @param array $extraOptions
+ * @return array|false
+ */
+function wrap_php_class($className, $extraOptions=array())
+{
+    $wrapper = new PhpXmlRpc\Wrapper();
+    $fix = false;
+    if (!isset($extraOptions['return_source'])  || $extraOptions['return_source'] == false) {
+        // backwards compat: return string instead of callable
+        $extraOptions['return_source'] = true;
+        $fix = true;
+    }
+    $wrapped = $wrapper->wrapPhpClass($className, $extraOptions);
+    foreach($wrapped as $name => $value) {
+        if ($fix) {
+            eval($value['source']);
+        }
+        $wrapped[$name] = $value['function'];
+    }
+    return $wrapped;
+}
+
+/**
+ * @see PhpXmlRpc\Wrapper::wrapXmlrpcMethod
+ * @param xmlrpc_client $client
+ * @param string $methodName
+ * @param int|array $extraOptions the usage of an int as signature number is deprecated, use an option in $extraOptions
+ * @param int $timeout            deprecated, use an option in $extraOptions
+ * @param string $protocol        deprecated, use an option in $extraOptions
+ * @param string $newFuncName     deprecated, use an option in $extraOptions
+ * @return array|callable|false
+ */
+function wrap_xmlrpc_method($client, $methodName, $extraOptions=0, $timeout=0, $protocol='', $newFuncName='')
+{
+    if (!is_array($extraOptions))
+    {
+        $sigNum = $extraOptions;
+        $extraOptions = array(
+            'signum' => $sigNum,
+            'timeout' => $timeout,
+            'protocol' => $protocol,
+            'new_function_name' => $newFuncName
+        );
+    }
+
+    $wrapper = new PhpXmlRpc\Wrapper();
+
+    if (!isset($extraOptions['return_source'])  || $extraOptions['return_source'] == false) {
+        // backwards compat: return string instead of callable
+        $extraOptions['return_source'] = true;
+        $wrapped = $wrapper->wrapXmlrpcMethod($client, $methodName, $extraOptions);
+        eval($wrapped['source']);
+        $wrapped = $wrapped['function'];
+    } else {
+        $wrapped = $wrapper->wrapXmlrpcMethod($client, $methodName, $extraOptions);
+    }
+    return $wrapped;
+}
+
+/**
+ * @see PhpXmlRpc\Wrapper::wrap_xmlrpc_server
+ * @param xmlrpc_client $client
+ * @param array $extraOptions
+ * @return mixed
+ */
+function wrap_xmlrpc_server($client, $extraOptions=array())
+{
+    $wrapper = new PhpXmlRpc\Wrapper();
+    return $wrapper->wrapXmlrpcServer($client, $extraOptions);
+}
+
+/**
+ * Given the necessary info, build php code that creates a new function to invoke a remote xmlrpc method.
+ * Take care that no full checking of input parameters is done to ensure that valid php code is emitted.
+ * Only kept for backwards compatibility
+ * Note: real spaghetti code follows...
+ *
+ * @deprecated
+ */
+function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName,
+     $mSig, $mDesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc',
+     $decodePhpObjects = false, $encodePhpObjects = false, $decodeFault = false,
+     $faultResponse = '', $namespace = '\\PhpXmlRpc\\')
+{
+    $code = "function $xmlrpcFuncName (";
+    if ($clientCopyMode < 2) {
+        // client copy mode 0 or 1 == partial / full client copy in emitted code
+        $innerCode = build_client_wrapper_code($client, $clientCopyMode, $prefix, $namespace);
+        $innerCode .= "\$client->setDebug(\$debug);\n";
+        $this_ = '';
+    } else {
+        // client copy mode 2 == no client copy in emitted code
+        $innerCode = '';
+        $this_ = 'this->';
+    }
+    $innerCode .= "\$req = new {$namespace}Request('$methodName');\n";
+
+    if ($mDesc != '') {
+        // take care that PHP comment is not terminated unwillingly by method description
+        $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n";
+    } else {
+        $mDesc = "/**\nFunction $xmlrpcFuncName\n";
+    }
+
+    // param parsing
+    $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
+    $plist = array();
+    $pCount = count($mSig);
+    for ($i = 1; $i < $pCount; $i++) {
+        $plist[] = "\$p$i";
+        $pType = $mSig[$i];
+        if ($pType == 'i4' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' ||
+            $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null'
+        ) {
+            // only build directly xmlrpc values when type is known and scalar
+            $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n";
+        } else {
+            if ($encodePhpObjects) {
+                $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
+            } else {
+                $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n";
+            }
+        }
+        $innerCode .= "\$req->addparam(\$p$i);\n";
+        $mDesc .= '* @param ' . xmlrpc_2_php_type($pType) . " \$p$i\n";
+    }
+    if ($clientCopyMode < 2) {
+        $plist[] = '$debug=0';
+        $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";
+    }
+    $plist = implode(', ', $plist);
+    $mDesc .= '* @return ' . xmlrpc_2_php_type($mSig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n";
+
+    $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n";
+    if ($decodeFault) {
+        if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) {
+            $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')";
+        } else {
+            $respCode = var_export($faultResponse, true);
+        }
+    } else {
+        $respCode = '$res';
+    }
+    if ($decodePhpObjects) {
+        $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));";
+    } else {
+        $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());";
+    }
+
+    $code = $code . $plist . ") {\n" . $innerCode . "\n}\n";
+
+    return array('source' => $code, 'docstring' => $mDesc);
+}
+
+/**
+ * @deprecated
+ */
+function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc')
+{
+    $code = "\$client = new {$prefix}_client('".str_replace("'", "\'", $client->path).
+        "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n";
+
+    // copy all client fields to the client that will be generated runtime
+    // (this provides for future expansion or subclassing of client obj)
+    if ($verbatim_client_copy)
+    {
+        foreach($client as $fld => $val)
+        {
+            if($fld != 'debug' && $fld != 'return_type')
+            {
+                $val = var_export($val, true);
+                $code .= "\$client->$fld = $val;\n";
+            }
+        }
+    }
+    // only make sure that client always returns the correct data type
+    $code .= "\$client->return_type = '{$prefix}vals';\n";
+    //$code .= "\$client->setDebug(\$debug);\n";
+    return $code;
+}