fc178dc9c8e7bcbcd556d918c0961cf1ce7fcae2
[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 function php_2_xmlrpc_type($phpType)
16 {
17     $wrapper = new PhpXmlRpc\Wrapper();
18     return $wrapper->php_2_xmlrpc_type($phpType);
19 }
20
21 function xmlrpc_2_php_type($xmlrpcType)
22 {
23     $wrapper = new PhpXmlRpc\Wrapper();
24     return $wrapper->xmlrpc_2_php_type($xmlrpcType);
25 }
26
27 /// @todo return string instead of callable
28 function wrap_php_function($funcName, $newFuncName='', $extraOptions=array())
29 {
30     $wrapper = new PhpXmlRpc\Wrapper();
31     return $wrapper->wrap_php_function($funcName, $newFuncName, $extraOptions);
32 }
33
34 /// @todo return strings instead of callables
35 function wrap_php_class($className, $extraOptions=array())
36 {
37     $wrapper = new PhpXmlRpc\Wrapper();
38     return $wrapper->wrap_php_class($className, $extraOptions);
39 }
40
41 /// @todo support different calling convention
42 /// @todo return string instead of callable
43 function wrap_xmlrpc_method($client, $methodName, $extraOptions=0, $timeout=0, $protocol='', $newFuncName='')
44 {
45     $wrapper = new PhpXmlRpc\Wrapper();
46     return $wrapper->wrap_xmlrpc_method($client, $methodName, $extraOptions, $timeout, $protocol, $newFuncName);
47 }
48
49 /// @todo return strings instead of callables
50 function wrap_xmlrpc_server($client, $extraOptions=array())
51 {
52     $wrapper = new PhpXmlRpc\Wrapper();
53     return $wrapper->wrap_xmlrpc_server($client, $extraOptions);
54 }
55
56 /**
57  * Given the necessary info, build php code that creates a new function to invoke a remote xmlrpc method.
58  * Take care that no full checking of input parameters is done to ensure that valid php code is emitted.
59  * Only kept for backwards compatibility
60  * Note: real spaghetti code follows...
61  *
62  * @deprecated
63  */
64 function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName,
65                                                  $mSig, $mDesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc',
66                                                  $decodePhpObjects = false, $encodePhpObjects = false, $decodeFault = false,
67                                                  $faultResponse = '', $namespace = '\\PhpXmlRpc\\')
68 {
69     $code = "function $xmlrpcFuncName (";
70     if ($clientCopyMode < 2) {
71         // client copy mode 0 or 1 == partial / full client copy in emitted code
72         $innerCode = $this->build_client_wrapper_code($client, $clientCopyMode, $prefix, $namespace);
73         $innerCode .= "\$client->setDebug(\$debug);\n";
74         $this_ = '';
75     } else {
76         // client copy mode 2 == no client copy in emitted code
77         $innerCode = '';
78         $this_ = 'this->';
79     }
80     $innerCode .= "\$req = new {$namespace}Request('$methodName');\n";
81
82     if ($mDesc != '') {
83         // take care that PHP comment is not terminated unwillingly by method description
84         $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n";
85     } else {
86         $mDesc = "/**\nFunction $xmlrpcFuncName\n";
87     }
88
89     // param parsing
90     $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
91     $plist = array();
92     $pCount = count($mSig);
93     for ($i = 1; $i < $pCount; $i++) {
94         $plist[] = "\$p$i";
95         $pType = $mSig[$i];
96         if ($pType == 'i4' || $pType == 'int' || $pType == 'boolean' || $pType == 'double' ||
97             $pType == 'string' || $pType == 'dateTime.iso8601' || $pType == 'base64' || $pType == 'null'
98         ) {
99             // only build directly xmlrpc values when type is known and scalar
100             $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$pType');\n";
101         } else {
102             if ($encodePhpObjects) {
103                 $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
104             } else {
105                 $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n";
106             }
107         }
108         $innerCode .= "\$req->addparam(\$p$i);\n";
109         $mDesc .= '* @param ' . $this->xmlrpc_2_php_type($pType) . " \$p$i\n";
110     }
111     if ($clientCopyMode < 2) {
112         $plist[] = '$debug=0';
113         $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";
114     }
115     $plist = implode(', ', $plist);
116     $mDesc .= '* @return ' . $this->xmlrpc_2_php_type($mSig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n";
117
118     $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n";
119     if ($decodeFault) {
120         if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) {
121             $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')";
122         } else {
123             $respCode = var_export($faultResponse, true);
124         }
125     } else {
126         $respCode = '$res';
127     }
128     if ($decodePhpObjects) {
129         $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));";
130     } else {
131         $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());";
132     }
133
134     $code = $code . $plist . ") {\n" . $innerCode . "\n}\n";
135
136     return array('source' => $code, 'docstring' => $mDesc);
137 }