X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2FWrapper.php;h=0b7663cacddd866ed08c32a0668086ae824e1b5e;hb=ee97067e0c7de2fc25a0834672f27dcad4468631;hp=a710fddd8c5c77a2912732891736b99992e499ad;hpb=5f01a1f84d32893310c1d13e6d2720370a93f57d;p=plcapi.git diff --git a/src/Wrapper.php b/src/Wrapper.php index a710fdd..0b7663c 100644 --- a/src/Wrapper.php +++ b/src/Wrapper.php @@ -9,7 +9,7 @@ namespace PhpXmlRpc; /** * PHP-XMLRPC "wrapper" class. - * Generate stubs to transparently access xmlrpc methods as php functions and viceversa. + * Generate stubs to transparently access xmlrpc methods as php functions and vice-versa. * Note: this class implements the PROXY pattern, but it is not named so to avoid confusion with http proxies. * * @todo separate introspection from code generation for func-2-method wrapping @@ -26,13 +26,13 @@ class Wrapper * for php class names returns 'struct', since php objects can be serialized as xmlrpc structs * for php arrays always return array, even though arrays sometimes serialize as json structs. * - * @param string $phptype + * @param string $phpType * * @return string */ - public function php_2_xmlrpc_type($phptype) + public function php_2_xmlrpc_type($phpType) { - switch (strtolower($phptype)) { + switch (strtolower($phpType)) { case 'string': return Value::$xmlrpcString; case 'integer': @@ -49,11 +49,11 @@ class Wrapper return Value::$xmlrpcStruct; case Value::$xmlrpcBase64: case Value::$xmlrpcStruct: - return strtolower($phptype); + return strtolower($phpType); case 'resource': return ''; default: - if (class_exists($phptype)) { + if (class_exists($phpType)) { return Value::$xmlrpcStruct; } else { // unknown: might be any 'extended' xmlrpc type @@ -65,13 +65,13 @@ class Wrapper /** * Given a string defining a phpxmlrpc type return corresponding php type. * - * @param string $xmlrpctype + * @param string $xmlrpcType * * @return string */ - public function xmlrpc_2_php_type($xmlrpctype) + public function xmlrpc_2_php_type($xmlrpcType) { - switch (strtolower($xmlrpctype)) { + switch (strtolower($xmlrpcType)) { case 'base64': case 'datetime.iso8601': case 'string': @@ -90,13 +90,13 @@ class Wrapper case 'null': default: // unknown: might be any xmlrpc type - return strtolower($xmlrpctype); + return strtolower($xmlrpcType); } } /** * Given a user-defined PHP function, create a PHP 'wrapper' function that can - * be exposed as xmlrpc method from an xmlrpc_server object and called from remote + * be exposed as xmlrpc method from an xmlrpc server object and called from remote * clients (as well as its corresponding signature info). * * Since php is a typeless language, to infer types of input and output parameters, @@ -123,9 +123,9 @@ class Wrapper * php functions (ie. functions not expecting a single Request obj as parameter) * is by making use of the functions_parameters_type class member. * - * @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 - * @param string $newfuncname (optional) name for function to be created - * @param array $extra_options (optional) array of options for conversion. valid values include: + * @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 + * @param string $newFuncName (optional) name for function to be created + * @param array $extraOptions (optional) array of options for conversion. valid values include: * bool return_source when true, php code w. function definition will be returned, not evaluated * bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects * bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- @@ -140,97 +140,97 @@ class Wrapper * @todo add some trigger_errors / error_log when returning false? * @todo what to do when the PHP function returns NULL? we are currently returning an empty string value... * @todo add an option to suppress php warnings in invocation of user function, similar to server debug level 3? - * @todo if $newfuncname is empty, we could use create_user_func instead of eval, as it is possibly faster + * @todo if $newFuncName is empty, we could use create_user_func instead of eval, as it is possibly faster * @todo add a verbatim_object_copy parameter to allow avoiding the same obj instance? */ - public function wrap_php_function($funcname, $newfuncname = '', $extra_options = array()) + public function wrap_php_function($funcName, $newFuncName = '', $extraOptions = array()) { - $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; - $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; - $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; - $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; - $catch_warnings = isset($extra_options['suppress_warnings']) && $extra_options['suppress_warnings'] ? '@' : ''; - - $exists = false; - if (is_string($funcname) && strpos($funcname, '::') !== false) { - $funcname = explode('::', $funcname); + $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true; + $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc'; + $namespace = '\\PhpXmlRpc\\'; + $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false; + $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false; + $catchWarnings = isset($extraOptions['suppress_warnings']) && $extraOptions['suppress_warnings'] ? '@' : ''; + + if (is_string($funcName) && strpos($funcName, '::') !== false) { + $funcName = explode('::', $funcName); } - if (is_array($funcname)) { - if (count($funcname) < 2 || (!is_string($funcname[0]) && !is_object($funcname[0]))) { + if (is_array($funcName)) { + if (count($funcName) < 2 || (!is_string($funcName[0]) && !is_object($funcName[0]))) { error_log('XML-RPC: syntax for function to be wrapped is wrong'); return false; } - if (is_string($funcname[0])) { - $plainfuncname = implode('::', $funcname); - } elseif (is_object($funcname[0])) { - $plainfuncname = get_class($funcname[0]) . '->' . $funcname[1]; + if (is_string($funcName[0])) { + $plainFuncName = implode('::', $funcName); + } elseif (is_object($funcName[0])) { + $plainFuncName = get_class($funcName[0]) . '->' . $funcName[1]; } - $exists = method_exists($funcname[0], $funcname[1]); + $exists = method_exists($funcName[0], $funcName[1]); } else { - $plainfuncname = $funcname; - $exists = function_exists($funcname); + $plainFuncName = $funcName; + $exists = function_exists($funcName); } if (!$exists) { - error_log('XML-RPC: function to be wrapped is not defined: ' . $plainfuncname); + error_log('XML-RPC: function to be wrapped is not defined: ' . $plainFuncName); return false; } else { // determine name of new php function - if ($newfuncname == '') { - if (is_array($funcname)) { - if (is_string($funcname[0])) { - $xmlrpcfuncname = "{$prefix}_" . implode('_', $funcname); + if ($newFuncName == '') { + if (is_array($funcName)) { + if (is_string($funcName[0])) { + $xmlrpcFuncName = "{$prefix}_" . implode('_', $funcName); } else { - $xmlrpcfuncname = "{$prefix}_" . get_class($funcname[0]) . '_' . $funcname[1]; + $xmlrpcFuncName = "{$prefix}_" . get_class($funcName[0]) . '_' . $funcName[1]; } } else { - $xmlrpcfuncname = "{$prefix}_$funcname"; + $xmlrpcFuncName = "{$prefix}_$funcName"; } } else { - $xmlrpcfuncname = $newfuncname; + $xmlrpcFuncName = $newFuncName; } - while ($buildit && function_exists($xmlrpcfuncname)) { - $xmlrpcfuncname .= 'x'; + while ($buildIt && function_exists($xmlrpcFuncName)) { + $xmlrpcFuncName .= 'x'; } // start to introspect PHP code - if (is_array($funcname)) { - $func = new \ReflectionMethod($funcname[0], $funcname[1]); + if (is_array($funcName)) { + $func = new \ReflectionMethod($funcName[0], $funcName[1]); if ($func->isPrivate()) { - error_log('XML-RPC: method to be wrapped is private: ' . $plainfuncname); + error_log('XML-RPC: method to be wrapped is private: ' . $plainFuncName); return false; } if ($func->isProtected()) { - error_log('XML-RPC: method to be wrapped is protected: ' . $plainfuncname); + error_log('XML-RPC: method to be wrapped is protected: ' . $plainFuncName); return false; } if ($func->isConstructor()) { - error_log('XML-RPC: method to be wrapped is the constructor: ' . $plainfuncname); + error_log('XML-RPC: method to be wrapped is the constructor: ' . $plainFuncName); return false; } if ($func->isDestructor()) { - error_log('XML-RPC: method to be wrapped is the destructor: ' . $plainfuncname); + error_log('XML-RPC: method to be wrapped is the destructor: ' . $plainFuncName); return false; } if ($func->isAbstract()) { - error_log('XML-RPC: method to be wrapped is abstract: ' . $plainfuncname); + error_log('XML-RPC: method to be wrapped is abstract: ' . $plainFuncName); return false; } /// @todo add more checks for static vs. nonstatic? } else { - $func = new \ReflectionFunction($funcname); + $func = new \ReflectionFunction($funcName); } if ($func->isInternal()) { // Note: from PHP 5.1.0 onward, we will possibly be able to use invokeargs // instead of getparameters to fully reflect internal php functions ? - error_log('XML-RPC: function to be wrapped is internal: ' . $plainfuncname); + error_log('XML-RPC: function to be wrapped is internal: ' . $plainFuncName); return false; } @@ -274,7 +274,7 @@ class Wrapper // syntax: @return type desc //$returns = preg_split('/\s+/', $doc); if (preg_match('/@return\s+(\S+)\s+(.+)/', $doc, $matches)) { - $returns = php_2_xmlrpc_type($matches[1]); + $returns = $this->php_2_xmlrpc_type($matches[1]); if (isset($matches[2])) { $returnsDocs = $matches[2]; } @@ -286,19 +286,20 @@ class Wrapper // execute introspection of actual function prototype $params = array(); $i = 0; - foreach ($func->getParameters() as $paramobj) { + foreach ($func->getParameters() as $paramObj) { $params[$i] = array(); - $params[$i]['name'] = '$' . $paramobj->getName(); - $params[$i]['isoptional'] = $paramobj->isOptional(); + $params[$i]['name'] = '$' . $paramObj->getName(); + $params[$i]['isoptional'] = $paramObj->isOptional(); $i++; } // start building of PHP code to be eval'd - $innercode = ''; + + $innerCode = "\$encoder = new {$namespace}Encoder();\n"; $i = 0; - $parsvariations = array(); + $parsVariations = array(); $pars = array(); - $pnum = count($params); + $pNum = count($params); foreach ($params as $param) { if (isset($paramDocs[$i]['name']) && $paramDocs[$i]['name'] && strtolower($paramDocs[$i]['name']) != strtolower($param['name'])) { // param name from phpdoc info does not match param definition! @@ -307,107 +308,107 @@ class Wrapper if ($param['isoptional']) { // this particular parameter is optional. save as valid previous list of parameters - $innercode .= "if (\$paramcount > $i) {\n"; - $parsvariations[] = $pars; + $innerCode .= "if (\$paramcount > $i) {\n"; + $parsVariations[] = $pars; } - $innercode .= "\$p$i = \$msg->getParam($i);\n"; - if ($decode_php_objects) { - $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i, array('decode_php_objs'));\n"; + $innerCode .= "\$p$i = \$msg->getParam($i);\n"; + if ($decodePhpObjects) { + $innerCode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = \$encoder->decode(\$p$i, array('decode_php_objs'));\n"; } else { - $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i);\n"; + $innerCode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = \$encoder->decode(\$p$i);\n"; } $pars[] = "\$p$i"; $i++; if ($param['isoptional']) { - $innercode .= "}\n"; + $innerCode .= "}\n"; } - if ($i == $pnum) { + if ($i == $pNum) { // last allowed parameters combination - $parsvariations[] = $pars; + $parsVariations[] = $pars; } } $sigs = array(); - $psigs = array(); - if (count($parsvariations) == 0) { + $pSigs = array(); + if (count($parsVariations) == 0) { // only known good synopsis = no parameters - $parsvariations[] = array(); - $minpars = 0; + $parsVariations[] = array(); + $minPars = 0; } else { - $minpars = count($parsvariations[0]); + $minPars = count($parsVariations[0]); } - if ($minpars) { + if ($minPars) { // add to code the check for min params number // NB: this check needs to be done BEFORE decoding param values - $innercode = "\$paramcount = \$msg->getNumParams();\n" . - "if (\$paramcount < $minpars) return new {$prefix}resp(0, " . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . ", '" . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . "');\n" . $innercode; + $innerCode = "\$paramcount = \$msg->getNumParams();\n" . + "if (\$paramcount < $minPars) return new {$namespace}Response(0, " . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . ", '" . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . "');\n" . $innerCode; } else { - $innercode = "\$paramcount = \$msg->getNumParams();\n" . $innercode; + $innerCode = "\$paramcount = \$msg->getNumParams();\n" . $innerCode; } - $innercode .= "\$np = false;\n"; + $innerCode .= "\$np = false;\n"; // since there are no closures in php, if we are given an object instance, // we store a pointer to it in a global var... - if (is_array($funcname) && is_object($funcname[0])) { - $GLOBALS['xmlrpcWPFObjHolder'][$xmlrpcfuncname] = &$funcname[0]; - $innercode .= "\$obj =& \$GLOBALS['xmlrpcWPFObjHolder']['$xmlrpcfuncname'];\n"; - $realfuncname = '$obj->' . $funcname[1]; + if (is_array($funcName) && is_object($funcName[0])) { + $GLOBALS['xmlrpcWPFObjHolder'][$xmlrpcFuncName] = &$funcName[0]; + $innerCode .= "\$obj =& \$GLOBALS['xmlrpcWPFObjHolder']['$xmlrpcFuncName'];\n"; + $realFuncName = '$obj->' . $funcName[1]; } else { - $realfuncname = $plainfuncname; + $realFuncName = $plainFuncName; } - foreach ($parsvariations as $pars) { - $innercode .= "if (\$paramcount == " . count($pars) . ") \$retval = {$catch_warnings}$realfuncname(" . implode(',', $pars) . "); else\n"; + foreach ($parsVariations as $pars) { + $innerCode .= "if (\$paramcount == " . count($pars) . ") \$retval = {$catchWarnings}$realFuncName(" . implode(',', $pars) . "); else\n"; // build a 'generic' signature (only use an appropriate return type) $sig = array($returns); - $psig = array($returnsDocs); + $pSig = array($returnsDocs); for ($i = 0; $i < count($pars); $i++) { if (isset($paramDocs[$i]['type'])) { $sig[] = $this->php_2_xmlrpc_type($paramDocs[$i]['type']); } else { $sig[] = Value::$xmlrpcValue; } - $psig[] = isset($paramDocs[$i]['doc']) ? $paramDocs[$i]['doc'] : ''; + $pSig[] = isset($paramDocs[$i]['doc']) ? $paramDocs[$i]['doc'] : ''; } $sigs[] = $sig; - $psigs[] = $psig; + $pSigs[] = $pSig; } - $innercode .= "\$np = true;\n"; - $innercode .= "if (\$np) return new {$prefix}resp(0, " . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . ", '" . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . "'); else {\n"; - //$innercode .= "if (\$_xmlrpcs_error_occurred) return new Response(0, $GLOBALS['xmlrpcerr']user, \$_xmlrpcs_error_occurred); else\n"; - $innercode .= "if (is_a(\$retval, '{$prefix}resp')) return \$retval; else\n"; + $innerCode .= "\$np = true;\n"; + $innerCode .= "if (\$np) return new {$namespace}Response(0, " . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . ", '" . PhpXmlRpc::$xmlrpcerr['incorrect_params'] . "'); else {\n"; + //$innerCode .= "if (\$_xmlrpcs_error_occurred) return new Response(0, $GLOBALS['xmlrpcerr']user, \$_xmlrpcs_error_occurred); else\n"; + $innerCode .= "if (is_a(\$retval, '{$namespace}Response')) return \$retval; else\n"; if ($returns == Value::$xmlrpcDateTime || $returns == Value::$xmlrpcBase64) { - $innercode .= "return new {$prefix}resp(new {$prefix}val(\$retval, '$returns'));"; + $innerCode .= "return new {$namespace}Response(new {$namespace}Value(\$retval, '$returns'));"; } else { - if ($encode_php_objects) { - $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval, array('encode_php_objs')));\n"; + if ($encodePhpObjects) { + $innerCode .= "return new {$namespace}Response(\$encoder->encode(\$retval, array('encode_php_objs')));\n"; } else { - $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval));\n"; + $innerCode .= "return new {$namespace}Response(\$encoder->encode(\$retval));\n"; } } // shall we exclude functions returning by ref? // if($func->returnsReference()) // return false; - $code = "function $xmlrpcfuncname(\$msg) {\n" . $innercode . "}\n}"; + $code = "function $xmlrpcFuncName(\$msg) {\n" . $innerCode . "}\n}"; //print_r($code); - if ($buildit) { + if ($buildIt) { $allOK = 0; eval($code . '$allOK=1;'); // alternative - //$xmlrpcfuncname = create_function('$m', $innercode); + //$xmlrpcFuncName = create_function('$m', $innerCode); if (!$allOK) { - error_log('XML-RPC: could not create function ' . $xmlrpcfuncname . ' to wrap php function ' . $plainfuncname); + error_log('XML-RPC: could not create function ' . $xmlrpcFuncName . ' to wrap php function ' . $plainFuncName); return false; } } - /// @todo examine if $paramDocs matches $parsvariations and build array for + /// @todo examine if $paramDocs matches $parsVariations and build array for /// usage as method signature, plus put together a nice string for docs - $ret = array('function' => $xmlrpcfuncname, 'signature' => $sigs, 'docstring' => $desc, 'signature_docs' => $psigs, 'source' => $code); + $ret = array('function' => $xmlrpcFuncName, 'signature' => $sigs, 'docstring' => $desc, 'signature_docs' => $pSigs, 'source' => $code); return $ret; } @@ -415,11 +416,11 @@ class Wrapper /** * Given a user-defined PHP class or php object, map its methods onto a list of - * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc_server + * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc server * object and called from remote clients (as well as their corresponding signature info). * * @param mixed $classname the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class - * @param array $extra_options see the docs for wrap_php_method for more options + * @param array $extraOptions see the docs for wrap_php_method for more options * 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 * * @return array or false on failure @@ -427,10 +428,10 @@ class Wrapper * @todo get_class_methods will return both static and non-static methods. * we have to differentiate the action, depending on wheter we recived a class name or object */ - public function wrap_php_class($classname, $extra_options = array()) + public function wrap_php_class($classname, $extraOptions = array()) { - $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; - $methodtype = isset($extra_options['method_type']) ? $extra_options['method_type'] : 'auto'; + $methodfilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : ''; + $methodtype = isset($extraOptions['method_type']) ? $extraOptions['method_type'] : 'auto'; $result = array(); $mlist = get_class_methods($classname); @@ -442,7 +443,7 @@ class Wrapper if (($func->isStatic && ($methodtype == 'all' || $methodtype == 'static' || ($methodtype == 'auto' && is_string($classname)))) || (!$func->isStatic && ($methodtype == 'all' || $methodtype == 'nonstatic' || ($methodtype == 'auto' && is_object($classname)))) ) { - $methodwrap = wrap_php_function(array($classname, $mname), '', $extra_options); + $methodwrap = $this->wrap_php_function(array($classname, $mname), '', $extraOptions); if ($methodwrap) { $result[$methodwrap['function']] = $methodwrap['function']; } @@ -478,8 +479,8 @@ class Wrapper * for debugging purposes. * * @param Client $client an xmlrpc client set up correctly to communicate with target server - * @param string $methodname the xmlrpc method to be mapped to a php function - * @param array $extra_options array of options that specify conversion details. valid options include + * @param string $methodName the xmlrpc method to be mapped to a php function + * @param array $extraOptions array of options that specify conversion details. valid options include * integer signum the index of the method signature to use in mapping (if method exposes many sigs) * integer timeout timeout (in secs) to be used when executing function/calling remote method * string protocol 'http' (default), 'http11' or 'https' @@ -492,80 +493,82 @@ class Wrapper * * @return string the name of the generated php function (or false) - OR AN ARRAY... */ - public function wrap_xmlrpc_method($client, $methodname, $extra_options = 0, $timeout = 0, $protocol = '', $newfuncname = '') + public function wrap_xmlrpc_method($client, $methodName, $extraOptions = 0, $timeout = 0, $protocol = '', $newFuncName = '') { // mind numbing: let caller use sane calling convention (as per javadoc, 3 params), // OR the 2.0 calling convention (no options) - we really love backward compat, don't we? - if (!is_array($extra_options)) { - $signum = $extra_options; - $extra_options = array(); + if (!is_array($extraOptions)) { + $signum = $extraOptions; + $extraOptions = array(); } else { - $signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; - $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; - $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; - $newfuncname = isset($extra_options['new_function_name']) ? $extra_options['new_function_name'] : ''; + $signum = isset($extraOptions['signum']) ? (int)$extraOptions['signum'] : 0; + $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0; + $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; + $newFuncName = isset($extraOptions['new_function_name']) ? $extraOptions['new_function_name'] : ''; } - //$encode_php_objects = in_array('encode_php_objects', $extra_options); - //$verbatim_client_copy = in_array('simple_client_copy', $extra_options) ? 1 : - // in_array('build_class_code', $extra_options) ? 2 : 0; + //$encodePhpObjects = in_array('encode_php_objects', $extraOptions); + //$verbatimClientCopy = in_array('simple_client_copy', $extraOptions) ? 1 : + // in_array('build_class_code', $extraOptions) ? 2 : 0; - $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; - $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; + $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false; + $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false; // it seems like the meaning of 'simple_client_copy' here is swapped wrt client_copy_mode later on... - $simple_client_copy = isset($extra_options['simple_client_copy']) ? (int)($extra_options['simple_client_copy']) : 0; - $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; - $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; - if (isset($extra_options['return_on_fault'])) { - $decode_fault = true; - $fault_response = $extra_options['return_on_fault']; + $simpleClientCopy = isset($extraOptions['simple_client_copy']) ? (int)($extraOptions['simple_client_copy']) : 0; + $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true; + $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc'; + $namespace = '\\PhpXmlRpc\\'; + if (isset($extraOptions['return_on_fault'])) { + $decodeFault = true; + $faultResponse = $extraOptions['return_on_fault']; } else { - $decode_fault = false; - $fault_response = ''; + $decodeFault = false; + $faultResponse = ''; } - $debug = isset($extra_options['debug']) ? ($extra_options['debug']) : 0; + $debug = isset($extraOptions['debug']) ? ($extraOptions['debug']) : 0; - $msgclass = $prefix . 'msg'; - $valclass = $prefix . 'val'; - $decodefunc = 'php_' . $prefix . '_decode'; + $msgclass = $namespace . 'Request'; + $valclass = $namespace . 'Value'; + $decoderClass = $namespace . 'Encoder'; $msg = new $msgclass('system.methodSignature'); - $msg->addparam(new $valclass($methodname)); + $msg->addparam(new $valclass($methodName)); $client->setDebug($debug); $response = $client->send($msg, $timeout, $protocol); if ($response->faultCode()) { - error_log('XML-RPC: could not retrieve method signature from remote server for method ' . $methodname); + error_log('XML-RPC: could not retrieve method signature from remote server for method ' . $methodName); return false; } else { $msig = $response->value(); if ($client->return_type != 'phpvals') { - $msig = $decodefunc($msig); + $decoder = new $decoderClass(); + $msig = $decoder->decode($msig); } if (!is_array($msig) || count($msig) <= $signum) { - error_log('XML-RPC: could not retrieve method signature nr.' . $signum . ' from remote server for method ' . $methodname); + error_log('XML-RPC: could not retrieve method signature nr.' . $signum . ' from remote server for method ' . $methodName); return false; } else { // pick a suitable name for the new function, avoiding collisions - if ($newfuncname != '') { - $xmlrpcfuncname = $newfuncname; + if ($newFuncName != '') { + $xmlrpcFuncName = $newFuncName; } else { // take care to insure that methodname is translated to valid // php function name - $xmlrpcfuncname = $prefix . '_' . preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), - array('_', ''), $methodname); + $xmlrpcFuncName = $prefix . '_' . preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), + array('_', ''), $methodName); } - while ($buildit && function_exists($xmlrpcfuncname)) { - $xmlrpcfuncname .= 'x'; + while ($buildIt && function_exists($xmlrpcFuncName)) { + $xmlrpcFuncName .= 'x'; } $msig = $msig[$signum]; $mdesc = ''; // if in 'offline' mode, get method description too. // in online mode, favour speed of operation - if (!$buildit) { + if (!$buildIt) { $msg = new $msgclass('system.methodHelp'); - $msg->addparam(new $valclass($methodname)); + $msg->addparam(new $valclass($methodName)); $response = $client->send($msg, $timeout, $protocol); if (!$response->faultCode()) { $mdesc = $response->value(); @@ -575,26 +578,25 @@ class Wrapper } } - $results = $this->build_remote_method_wrapper_code($client, $methodname, - $xmlrpcfuncname, $msig, $mdesc, $timeout, $protocol, $simple_client_copy, - $prefix, $decode_php_objects, $encode_php_objects, $decode_fault, - $fault_response); - + $results = $this->build_remote_method_wrapper_code($client, $methodName, + $xmlrpcFuncName, $msig, $mdesc, $timeout, $protocol, $simpleClientCopy, + $prefix, $decodePhpObjects, $encodePhpObjects, $decodeFault, + $faultResponse, $namespace); //print_r($code); - if ($buildit) { + if ($buildIt) { $allOK = 0; eval($results['source'] . '$allOK=1;'); // alternative - //$xmlrpcfuncname = create_function('$m', $innercode); + //$xmlrpcFuncName = create_function('$m', $innerCode); if ($allOK) { - return $xmlrpcfuncname; + return $xmlrpcFuncName; } else { - error_log('XML-RPC: could not create function ' . $xmlrpcfuncname . ' to wrap remote method ' . $methodname); + error_log('XML-RPC: could not create function ' . $xmlrpcFuncName . ' to wrap remote method ' . $methodName); return false; } } else { - $results['function'] = $xmlrpcfuncname; + $results['function'] = $xmlrpcFuncName; return $results; } @@ -608,26 +610,27 @@ class Wrapper * For more details see wrap_xmlrpc_method. * * @param Client $client the client obj all set to query the desired server - * @param array $extra_options list of options for wrapped code + * @param array $extraOptions list of options for wrapped code * * @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) */ - public function wrap_xmlrpc_server($client, $extra_options = array()) + public function wrap_xmlrpc_server($client, $extraOptions = array()) { - $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; - //$signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; - $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; - $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; - $newclassname = isset($extra_options['new_class_name']) ? $extra_options['new_class_name'] : ''; - $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; - $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; - $verbatim_client_copy = isset($extra_options['simple_client_copy']) ? !($extra_options['simple_client_copy']) : true; - $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; - $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; - - $msgclass = $prefix . 'msg'; + $methodfilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : ''; + //$signum = isset($extraOptions['signum']) ? (int)$extraOptions['signum'] : 0; + $timeout = isset($extraOptions['timeout']) ? (int)$extraOptions['timeout'] : 0; + $protocol = isset($extraOptions['protocol']) ? $extraOptions['protocol'] : ''; + $newclassname = isset($extraOptions['new_class_name']) ? $extraOptions['new_class_name'] : ''; + $encodePhpObjects = isset($extraOptions['encode_php_objs']) ? (bool)$extraOptions['encode_php_objs'] : false; + $decodePhpObjects = isset($extraOptions['decode_php_objs']) ? (bool)$extraOptions['decode_php_objs'] : false; + $verbatimClientCopy = isset($extraOptions['simple_client_copy']) ? !($extraOptions['simple_client_copy']) : true; + $buildIt = isset($extraOptions['return_source']) ? !($extraOptions['return_source']) : true; + $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : 'xmlrpc'; + $namespace = '\\PhpXmlRpc\\'; + + $msgclass = $namespace . 'Request'; //$valclass = $prefix.'val'; - $decodefunc = 'php_' . $prefix . '_decode'; + $decoderClass = $namespace . 'Encoder'; $msg = new $msgclass('system.listMethods'); $response = $client->send($msg, $timeout, $protocol); @@ -638,7 +641,8 @@ class Wrapper } else { $mlist = $response->value(); if ($client->return_type != 'phpvals') { - $mlist = $decodefunc($mlist); + $decoder = new $decoderClass(); + $mlist = $decoder->decode($mlist); } if (!is_array($mlist) || !count($mlist)) { error_log('XML-RPC: could not retrieve meaningful method list from remote server'); @@ -647,33 +651,33 @@ class Wrapper } else { // pick a suitable name for the new function, avoiding collisions if ($newclassname != '') { - $xmlrpcclassname = $newclassname; + $xmlrpcClassName = $newclassname; } else { - $xmlrpcclassname = $prefix . '_' . preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), + $xmlrpcClassName = $prefix . '_' . preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), array('_', ''), $client->server) . '_client'; } - while ($buildit && class_exists($xmlrpcclassname)) { - $xmlrpcclassname .= 'x'; + while ($buildIt && class_exists($xmlrpcClassName)) { + $xmlrpcClassName .= 'x'; } /// @todo add function setdebug() to new class, to enable/disable debugging - $source = "class $xmlrpcclassname\n{\nvar \$client;\n\n"; - $source .= "function $xmlrpcclassname()\n{\n"; - $source .= $this->build_client_wrapper_code($client, $verbatim_client_copy, $prefix); - $source .= "\$this->client =& \$client;\n}\n\n"; + $source = "class $xmlrpcClassName\n{\nvar \$client;\n\n"; + $source .= "function __construct()\n{\n"; + $source .= $this->build_client_wrapper_code($client, $verbatimClientCopy, $prefix, $namespace); + $source .= "\$this->client = \$client;\n}\n\n"; $opts = array('simple_client_copy' => 2, 'return_source' => true, 'timeout' => $timeout, 'protocol' => $protocol, - 'encode_php_objs' => $encode_php_objects, 'prefix' => $prefix, - 'decode_php_objs' => $decode_php_objects, + 'encode_php_objs' => $encodePhpObjects, 'prefix' => $prefix, + 'decode_php_objs' => $decodePhpObjects, ); /// @todo build javadoc for class definition, too foreach ($mlist as $mname) { if ($methodfilter == '' || preg_match($methodfilter, $mname)) { $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), array('_', ''), $mname); - $methodwrap = wrap_xmlrpc_method($client, $mname, $opts); + $methodwrap = $this->wrap_xmlrpc_method($client, $mname, $opts); if ($methodwrap) { - if (!$buildit) { + if (!$buildIt) { $source .= $methodwrap['docstring']; } $source .= $methodwrap['source'] . "\n"; @@ -683,20 +687,20 @@ class Wrapper } } $source .= "}\n"; - if ($buildit) { + if ($buildIt) { $allOK = 0; eval($source . '$allOK=1;'); // alternative - //$xmlrpcfuncname = create_function('$m', $innercode); + //$xmlrpcFuncName = create_function('$m', $innerCode); if ($allOK) { - return $xmlrpcclassname; + return $xmlrpcClassName; } else { - error_log('XML-RPC: could not create class ' . $xmlrpcclassname . ' to wrap remote server ' . $client->server); + error_log('XML-RPC: could not create class ' . $xmlrpcClassName . ' to wrap remote server ' . $client->server); return false; } } else { - return array('class' => $xmlrpcclassname, 'code' => $source, 'docstring' => ''); + return array('class' => $xmlrpcClassName, 'code' => $source, 'docstring' => ''); } } } @@ -709,32 +713,33 @@ class Wrapper * valid php code is emitted. * Note: real spaghetti code follows... */ - protected function build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname, - $msig, $mdesc = '', $timeout = 0, $protocol = '', $client_copy_mode = 0, $prefix = 'xmlrpc', - $decode_php_objects = false, $encode_php_objects = false, $decode_fault = false, - $fault_response = '') + public function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName, + $msig, $mdesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc', + $decodePhpObjects = false, $encodePhpObjects = false, $decdoeFault = false, + $faultResponse = '', $namespace = '\\PhpXmlRpc\\') { - $code = "function $xmlrpcfuncname ("; - if ($client_copy_mode < 2) { + $code = "function $xmlrpcFuncName ("; + if ($clientCopyMode < 2) { // client copy mode 0 or 1 == partial / full client copy in emitted code - $innercode = $this->build_client_wrapper_code($client, $client_copy_mode, $prefix); - $innercode .= "\$client->setDebug(\$debug);\n"; + $innerCode = $this->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 = ''; + $innerCode = ''; $this_ = 'this->'; } - $innercode .= "\$msg = new {$prefix}msg('$methodname');\n"; + $innerCode .= "\$msg = 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"; + $mdesc = "/**\nFunction $xmlrpcFuncName\n"; } // param parsing + $innerCode .= "\$encoder = new {$namespace}Encoder();\n"; $plist = array(); $pcount = count($msig); for ($i = 1; $i < $pcount; $i++) { @@ -743,42 +748,42 @@ class Wrapper if ($ptype == 'i4' || $ptype == 'int' || $ptype == 'boolean' || $ptype == 'double' || $ptype == 'string' || $ptype == 'dateTime.iso8601' || $ptype == 'base64' || $ptype == 'null' ) { - // only build directly xmlrpcvals when type is known and scalar - $innercode .= "\$p$i = new {$prefix}val(\$p$i, '$ptype');\n"; + // only build directly xmlrpc values when type is known and scalar + $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$ptype');\n"; } else { - if ($encode_php_objects) { - $innercode .= "\$p$i = php_{$prefix}_encode(\$p$i, array('encode_php_objs'));\n"; + if ($encodePhpObjects) { + $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n"; } else { - $innercode .= "\$p$i = php_{$prefix}_encode(\$p$i);\n"; + $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n"; } } - $innercode .= "\$msg->addparam(\$p$i);\n"; + $innerCode .= "\$msg->addparam(\$p$i);\n"; $mdesc .= '* @param ' . $this->xmlrpc_2_php_type($ptype) . " \$p$i\n"; } - if ($client_copy_mode < 2) { + 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 ' . $this->xmlrpc_2_php_type($msig[0]) . " (or an {$prefix}resp obj instance if call fails)\n*/\n"; + $mdesc .= '* @return ' . $this->xmlrpc_2_php_type($msig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n"; - $innercode .= "\$res = \${$this_}client->send(\$msg, $timeout, '$protocol');\n"; - if ($decode_fault) { - if (is_string($fault_response) && ((strpos($fault_response, '%faultCode%') !== false) || (strpos($fault_response, '%faultString%') !== false))) { - $respcode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $fault_response) . "')"; + $innerCode .= "\$res = \${$this_}client->send(\$msg, $timeout, '$protocol');\n"; + if ($decdoeFault) { + 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($fault_response, true); + $respCode = var_export($faultResponse, true); } } else { - $respcode = '$res'; + $respCode = '$res'; } - if ($decode_php_objects) { - $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value(), array('decode_php_objs'));"; + 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 php_{$prefix}_decode(\$res->value());"; + $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());"; } - $code = $code . $plist . ") {\n" . $innercode . "\n}\n"; + $code = $code . $plist . ") {\n" . $innerCode . "\n}\n"; return array('source' => $code, 'docstring' => $mdesc); } @@ -787,15 +792,20 @@ class Wrapper * Given necessary info, generate php code that will rebuild a client object * Take care that no full checking of input parameters is done to ensure that * valid php code is emitted. + * @param Client $client + * @param bool $verbatimClientCopy + * @param string $prefix + * @param string $namespace + * @return string */ - protected function build_client_wrapper_code($client, $verbatim_client_copy, $prefix = 'xmlrpc') + protected function build_client_wrapper_code($client, $verbatimClientCopy, $prefix = 'xmlrpc', $namespace = '\\PhpXmlRpc\\' ) { - $code = "\$client = new {$prefix}_client('" . str_replace("'", "\'", $client->path) . + $code = "\$client = new {$namespace}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) { + if ($verbatimClientCopy) { foreach ($client as $fld => $val) { if ($fld != 'debug' && $fld != 'return_type') { $val = var_export($val, true);