021b1ce133ee27963fa520da465a4cafd771d019
[plcapi.git] / lib / xmlrpc_wrappers.inc
1 <?php
2
3 /******************************************************************************
4  *
5  * *** DEPRECATED ***
6  *
7  * This file is only used to insure backwards compatibility
8  * with the API of the library <= rev. 3
9  *****************************************************************************/
10
11 include_once(__DIR__.'/../src/Wrapper.php');
12
13 /* Expose as global functions the ones which are now class methods */
14
15 /**
16  * @see PhpXmlRpc\Wrapper::php_2_xmlrpc_type
17  * @param string $phpType
18  * @return string
19  */
20 function php_2_xmlrpc_type($phpType)
21 {
22     $wrapper = new PhpXmlRpc\Wrapper();
23     return $wrapper->php2XmlrpcType($phpType);
24 }
25
26 /**
27  * @see PhpXmlRpc\Wrapper::xmlrpc_2_php_type
28  * @param string $xmlrpcType
29  * @return string
30  */
31 function xmlrpc_2_php_type($xmlrpcType)
32 {
33     $wrapper = new PhpXmlRpc\Wrapper();
34     return $wrapper->xmlrpc2PhpType($xmlrpcType);
35 }
36
37 /// @todo backwards compat: return string instead of callable
38 /**
39  * @see PhpXmlRpc\Wrapper::wrap_php_function
40  * @param callable $funcName
41  * @param string $newFuncName
42  * @param array $extraOptions
43  * @return array|false
44  */
45 function wrap_php_function($funcName, $newFuncName='', $extraOptions=array())
46 {
47     $wrapper = new PhpXmlRpc\Wrapper();
48     return $wrapper->wrapPhpFunction($funcName, $newFuncName, $extraOptions);
49 }
50
51 /// @todo backwards compat: return strings instead of callables
52 /**
53  * @see PhpXmlRpc\Wrapper::wrap_php_class
54  * @param string|object $className
55  * @param array $extraOptions
56  * @return array|false
57  */
58 function wrap_php_class($className, $extraOptions=array())
59 {
60     $wrapper = new PhpXmlRpc\Wrapper();
61     return $wrapper->wrapPhpClass($className, $extraOptions);
62 }
63
64 /// @todo backwards compat: return string instead of callable
65 /**
66  * @see PhpXmlRpc\Wrapper::wrapXmlrpcMethod
67  * @param xmlrpc_client $client
68  * @param string $methodName
69  * @param int|array $extraOptions the usage of an int as signature number is deprecated, use an option in $extraOptions
70  * @param int $timeout            deprecated, use an option in $extraOptions
71  * @param string $protocol        deprecated, use an option in $extraOptions
72  * @param string $newFuncName     deprecated, use an option in $extraOptions
73  * @return array|callable|false
74  */
75 function wrap_xmlrpc_method($client, $methodName, $extraOptions=0, $timeout=0, $protocol='', $newFuncName='')
76 {
77     if (!is_array($extraOptions))
78     {
79         $sigNum = $extraOptions;
80         $extraOptions = array(
81             'signum' => $sigNum,
82             'timeout' => $timeout,
83             'protocol' => $protocol,
84             'new_function_name' => $newFuncName
85         );
86     }
87
88     $wrapper = new PhpXmlRpc\Wrapper();
89     return $wrapper->wrapXmlrpcMethod($client, $methodName, $extraOptions);
90 }
91
92 /// @todo backwards compat: return strings instead of callables
93 /**
94  * @see PhpXmlRpc\Wrapper::wrap_xmlrpc_server
95  * @param xmlrpc_client $client
96  * @param array $extraOptions
97  * @return mixed
98  */
99 function wrap_xmlrpc_server($client, $extraOptions=array())
100 {
101     $wrapper = new PhpXmlRpc\Wrapper();
102     return $wrapper->wrapXmlrpcServer($client, $extraOptions);
103 }
104
105 /// @todo fix dangling usage of $this->
106 /**
107  * Given the necessary info, build php code that creates a new function to invoke a remote xmlrpc method.
108  * Take care that no full checking of input parameters is done to ensure that valid php code is emitted.
109  * Only kept for backwards compatibility
110  * Note: real spaghetti code follows...
111  *
112  * @deprecated
113  */
114 function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName,
115      $mSig, $mDesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc',
116      $decodePhpObjects = false, $encodePhpObjects = false, $decodeFault = false,
117      $faultResponse = '', $namespace = '\\PhpXmlRpc\\')
118 {
119     $code = "function $xmlrpcFuncName (";
120     if ($clientCopyMode < 2) {
121         // client copy mode 0 or 1 == partial / full client copy in emitted code
122         $innerCode = build_client_wrapper_code($client, $clientCopyMode, $prefix, $namespace);
123         $innerCode .= "\$client->setDebug(\$debug);\n";
124         $this_ = '';
125     } else {
126         // client copy mode 2 == no client copy in emitted code
127         $innerCode = '';
128         $this_ = 'this->';
129     }
130     $innerCode .= "\$req = new {$namespace}Request('$methodName');\n";
131
132     if ($mDesc != '') {
133         // take care that PHP comment is not terminated unwillingly by method description
134         $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n";
135     } else {
136         $mDesc = "/**\nFunction $xmlrpcFuncName\n";
137     }
138
139     // param parsing
140     $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
141     $plist = array();
142     $pCount = count($mSig);
143     for ($i = 1; $i < $pCount; $i++) {
144         $plist[] = "\$p$i";
145         $pType = $mSig[$i];
146         if ($pType == 'i4' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' ||
147             $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null'
148         ) {
149             // only build directly xmlrpc values when type is known and scalar
150             $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n";
151         } else {
152             if ($encodePhpObjects) {
153                 $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
154             } else {
155                 $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n";
156             }
157         }
158         $innerCode .= "\$req->addparam(\$p$i);\n";
159         $mDesc .= '* @param ' . xmlrpc_2_php_type($pType) . " \$p$i\n";
160     }
161     if ($clientCopyMode < 2) {
162         $plist[] = '$debug=0';
163         $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";
164     }
165     $plist = implode(', ', $plist);
166     $mDesc .= '* @return ' . xmlrpc_2_php_type($mSig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n";
167
168     $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n";
169     if ($decodeFault) {
170         if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) {
171             $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')";
172         } else {
173             $respCode = var_export($faultResponse, true);
174         }
175     } else {
176         $respCode = '$res';
177     }
178     if ($decodePhpObjects) {
179         $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));";
180     } else {
181         $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());";
182     }
183
184     $code = $code . $plist . ") {\n" . $innerCode . "\n}\n";
185
186     return array('source' => $code, 'docstring' => $mDesc);
187 }
188
189 /**
190  * @deprecated
191  */
192 function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc')
193 {
194     $code = "\$client = new {$prefix}_client('".str_replace("'", "\'", $client->path).
195         "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n";
196
197     // copy all client fields to the client that will be generated runtime
198     // (this provides for future expansion or subclassing of client obj)
199     if ($verbatim_client_copy)
200     {
201         foreach($client as $fld => $val)
202         {
203             if($fld != 'debug' && $fld != 'return_type')
204             {
205                 $val = var_export($val, true);
206                 $code .= "\$client->$fld = $val;\n";
207             }
208         }
209     }
210     // only make sure that client always returns the correct data type
211     $code .= "\$client->return_type = '{$prefix}vals';\n";
212     //$code .= "\$client->setDebug(\$debug);\n";
213     return $code;
214 }