3 /******************************************************************************
7 * This file is only used to insure backwards compatibility
8 * with the API of the library <= rev. 3
9 *****************************************************************************/
11 include_once(__DIR__.'/../src/Wrapper.php');
13 /* Expose as global functions the ones which are now class methods */
16 * @see PhpXmlRpc\Wrapper::php_2_xmlrpc_type
17 * @param string $phpType
20 function php_2_xmlrpc_type($phpType)
22 $wrapper = new PhpXmlRpc\Wrapper();
23 return $wrapper->php2XmlrpcType($phpType);
27 * @see PhpXmlRpc\Wrapper::xmlrpc_2_php_type
28 * @param string $xmlrpcType
31 function xmlrpc_2_php_type($xmlrpcType)
33 $wrapper = new PhpXmlRpc\Wrapper();
34 return $wrapper->xmlrpc2PhpType($xmlrpcType);
38 * @see PhpXmlRpc\Wrapper::wrap_php_function
39 * @param callable $funcName
40 * @param string $newFuncName
41 * @param array $extraOptions
44 function wrap_php_function($funcName, $newFuncName='', $extraOptions=array())
46 $wrapper = new PhpXmlRpc\Wrapper();
47 if (!isset($extraOptions['return_source']) || $extraOptions['return_source'] == false) {
48 // backwards compat: return string instead of callable
49 $extraOptions['return_source'] = true;
50 $wrapped = $wrapper->wrapPhpFunction($funcName, $newFuncName, $extraOptions);
51 eval($wrapped['source']);
53 $wrapped = $wrapper->wrapPhpFunction($funcName, $newFuncName, $extraOptions);
59 * NB: this function returns an array in a format which is unsuitable for direct use in the server dispatch map, unlike
60 * PhpXmlRpc\Wrapper::wrapPhpClass. This behaviour might seem like a bug, but has been kept for backwards compatibility.
62 * @see PhpXmlRpc\Wrapper::wrap_php_class
63 * @param string|object $className
64 * @param array $extraOptions
67 function wrap_php_class($className, $extraOptions=array())
69 $wrapper = new PhpXmlRpc\Wrapper();
71 if (!isset($extraOptions['return_source']) || $extraOptions['return_source'] == false) {
72 // backwards compat: return string instead of callable
73 $extraOptions['return_source'] = true;
76 $wrapped = $wrapper->wrapPhpClass($className, $extraOptions);
77 foreach($wrapped as $name => $value) {
79 eval($value['source']);
81 $wrapped[$name] = $value['function'];
87 * @see PhpXmlRpc\Wrapper::wrapXmlrpcMethod
88 * @param xmlrpc_client $client
89 * @param string $methodName
90 * @param int|array $extraOptions the usage of an int as signature number is deprecated, use an option 'signum' in $extraOptions
91 * @param int $timeout deprecated, use an option in $extraOptions
92 * @param string $protocol deprecated, use an option in $extraOptions
93 * @param string $newFuncName deprecated, use an option in $extraOptions
94 * @return array|callable|false
96 function wrap_xmlrpc_method($client, $methodName, $extraOptions=0, $timeout=0, $protocol='', $newFuncName='')
98 if (!is_array($extraOptions))
100 $sigNum = $extraOptions;
101 $extraOptions = array(
103 'timeout' => $timeout,
104 'protocol' => $protocol,
105 'new_function_name' => $newFuncName
109 $wrapper = new PhpXmlRpc\Wrapper();
111 if (!isset($extraOptions['return_source']) || $extraOptions['return_source'] == false) {
112 // backwards compat: return string instead of callable
113 $extraOptions['return_source'] = true;
114 $wrapped = $wrapper->wrapXmlrpcMethod($client, $methodName, $extraOptions);
115 if (is_array($wrapped)) {
116 eval($wrapped['source']);
117 $wrapped = $wrapped['function'];
120 $wrapped = $wrapper->wrapXmlrpcMethod($client, $methodName, $extraOptions);
126 * @see PhpXmlRpc\Wrapper::wrap_xmlrpc_server
127 * @param xmlrpc_client $client
128 * @param array $extraOptions
131 function wrap_xmlrpc_server($client, $extraOptions=array())
133 $wrapper = new PhpXmlRpc\Wrapper();
134 return $wrapper->wrapXmlrpcServer($client, $extraOptions);
138 * Given the necessary info, build php code that creates a new function to invoke a remote xmlrpc method.
139 * Take care that no full checking of input parameters is done to ensure that valid php code is emitted.
140 * Only kept for backwards compatibility
141 * Note: real spaghetti code follows...
145 function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName,
146 $mSig, $mDesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc',
147 $decodePhpObjects = false, $encodePhpObjects = false, $decodeFault = false,
148 $faultResponse = '', $namespace = '\\PhpXmlRpc\\')
150 $code = "function $xmlrpcFuncName (";
151 if ($clientCopyMode < 2) {
152 // client copy mode 0 or 1 == partial / full client copy in emitted code
153 $innerCode = build_client_wrapper_code($client, $clientCopyMode, $prefix, $namespace);
154 $innerCode .= "\$client->setDebug(\$debug);\n";
157 // client copy mode 2 == no client copy in emitted code
161 $innerCode .= "\$req = new {$namespace}Request('$methodName');\n";
164 // take care that PHP comment is not terminated unwillingly by method description
165 $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n";
167 $mDesc = "/**\nFunction $xmlrpcFuncName\n";
171 $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
173 $pCount = count($mSig);
174 for ($i = 1; $i < $pCount; $i++) {
177 if ($pType == 'i4' || $pType == 'i8' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' ||
178 $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null'
180 // only build directly xmlrpc values when type is known and scalar
181 $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n";
183 if ($encodePhpObjects) {
184 $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
186 $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n";
189 $innerCode .= "\$req->addparam(\$p$i);\n";
190 $mDesc .= '* @param ' . xmlrpc_2_php_type($pType) . " \$p$i\n";
192 if ($clientCopyMode < 2) {
193 $plist[] = '$debug=0';
194 $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";
196 $plist = implode(', ', $plist);
197 $mDesc .= '* @return ' . xmlrpc_2_php_type($mSig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n";
199 $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n";
201 if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) {
202 $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')";
204 $respCode = var_export($faultResponse, true);
209 if ($decodePhpObjects) {
210 $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));";
212 $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());";
215 $code = $code . $plist . ") {\n" . $innerCode . "\n}\n";
217 return array('source' => $code, 'docstring' => $mDesc);
223 function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc')
225 $code = "\$client = new {$prefix}_client('".str_replace("'", "\'", $client->path).
226 "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n";
228 // copy all client fields to the client that will be generated runtime
229 // (this provides for future expansion or subclassing of client obj)
230 if ($verbatim_client_copy)
232 foreach($client as $fld => $val)
234 if($fld != 'debug' && $fld != 'return_type')
236 $val = var_export($val, true);
237 $code .= "\$client->$fld = $val;\n";
241 // only make sure that client always returns the correct data type
242 $code .= "\$client->return_type = '{$prefix}vals';\n";
243 //$code .= "\$client->setDebug(\$debug);\n";