| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
|
|
|
4 |
|
|
|
5 |
// LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* +-----------------------------------------------------------------------------+
|
|
|
9 |
* | Copyright (c) 2004-2006 Sergio Goncalves Carvalho |
|
|
|
10 |
* +-----------------------------------------------------------------------------+
|
|
|
11 |
* | This file is part of XML_RPC2. |
|
|
|
12 |
* | |
|
|
|
13 |
* | XML_RPC2 is free software; you can redistribute it and/or modify |
|
|
|
14 |
* | it under the terms of the GNU Lesser General Public License as published by |
|
|
|
15 |
* | the Free Software Foundation; either version 2.1 of the License, or |
|
|
|
16 |
* | (at your option) any later version. |
|
|
|
17 |
* | |
|
|
|
18 |
* | XML_RPC2 is distributed in the hope that it will be useful, |
|
|
|
19 |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
20 |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
21 |
* | GNU Lesser General Public License for more details. |
|
|
|
22 |
* | |
|
|
|
23 |
* | You should have received a copy of the GNU Lesser General Public License |
|
|
|
24 |
* | along with XML_RPC2; if not, write to the Free Software |
|
|
|
25 |
* | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
|
|
|
26 |
* | 02111-1307 USA |
|
|
|
27 |
* +-----------------------------------------------------------------------------+
|
|
|
28 |
* | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com> |
|
|
|
29 |
* +-----------------------------------------------------------------------------+
|
|
|
30 |
*
|
|
|
31 |
* @category XML
|
|
|
32 |
* @package XML_RPC2
|
|
|
33 |
* @author Sergio Carvalho <sergio.carvalho@portugalmail.com>
|
|
|
34 |
* @copyright 2004-2006 Sergio Carvalho
|
|
|
35 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
|
|
36 |
* @version CVS: $Id: Client.php 308707 2011-02-26 17:08:55Z sergiosgc $
|
|
|
37 |
* @link http://pear.php.net/package/XML_RPC2
|
|
|
38 |
*/
|
|
|
39 |
|
|
|
40 |
// }}}
|
|
|
41 |
|
|
|
42 |
// dependencies {{{
|
|
|
43 |
require_once 'XML/RPC2/Exception.php';
|
|
|
44 |
require_once 'XML/RPC2/Backend.php';
|
|
|
45 |
require_once 'XML/RPC2/ClientHelper.php';
|
|
|
46 |
// }}}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* XML_RPC client class. Use this class to access remote methods.
|
|
|
50 |
*
|
|
|
51 |
* To use this class, construct it providing the server URL and method prefix.
|
|
|
52 |
* Then, call remote methods on the new instance as if they were local.
|
|
|
53 |
*
|
|
|
54 |
* Example:
|
|
|
55 |
* <code>
|
|
|
56 |
* require_once 'XML_RPC2/Client.php';
|
|
|
57 |
*
|
|
|
58 |
* $client = XML_RPC2_Client('http://xmlrpc.example.com/1.0/', 'example.');
|
|
|
59 |
* $result = $client->hello('Sergio');
|
|
|
60 |
* print($result);
|
|
|
61 |
* </code>
|
|
|
62 |
*
|
|
|
63 |
* The above example will call the example.hello method on the xmlrpc.example.com
|
|
|
64 |
* server, under the /1.0/ URI.
|
|
|
65 |
*
|
|
|
66 |
* @category XML
|
|
|
67 |
* @package XML_RPC2
|
|
|
68 |
* @author Sergio Carvalho <sergio.carvalho@portugalmail.com>
|
|
|
69 |
* @copyright 2004-2006 Sergio Carvalho
|
|
|
70 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
|
|
71 |
* @link http://pear.php.net/package/XML_RPC2
|
|
|
72 |
*/
|
|
|
73 |
abstract class XML_RPC2_Client
|
|
|
74 |
{
|
|
|
75 |
// {{{ properties
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* uri Field (holds the uri for the XML_RPC server)
|
|
|
79 |
*
|
|
|
80 |
* @var string
|
|
|
81 |
*/
|
|
|
82 |
protected $uri = null;
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* proxy Field (holds the proxy server data)
|
|
|
86 |
*
|
|
|
87 |
* @var string
|
|
|
88 |
*/
|
|
|
89 |
protected $proxy = null;
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Holds the prefix to prepend to method names
|
|
|
93 |
*
|
|
|
94 |
* @var string
|
|
|
95 |
*/
|
|
|
96 |
protected $prefix = null;
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Holds the debug flag
|
|
|
100 |
*
|
|
|
101 |
* @var boolean
|
|
|
102 |
*/
|
|
|
103 |
protected $debug = false;
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Hold the encoding of the client request
|
|
|
107 |
*
|
|
|
108 |
* @var string
|
|
|
109 |
*/
|
|
|
110 |
protected $encoding = 'utf-8';
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Holds the escaping method(s) of the client request
|
|
|
114 |
*
|
|
|
115 |
* @var string
|
|
|
116 |
*/
|
|
|
117 |
protected $escaping = array('non-ascii', 'non-print', 'markup');
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Holds the SSL verify flag
|
|
|
121 |
*
|
|
|
122 |
* @var boolean
|
|
|
123 |
*/
|
|
|
124 |
protected $sslverify = true;
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Holds the connection timeout in milliseconds of the client request.
|
|
|
128 |
*
|
|
|
129 |
* @var integer
|
|
|
130 |
*/
|
|
|
131 |
protected $connectionTimeout = null;
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* ugly hack flag to avoid http://bugs.php.net/bug.php?id=21949
|
|
|
135 |
*
|
|
|
136 |
* see XML_RPC2_Backend_Xmlrpcext_Value::createFromNative() from more infos
|
|
|
137 |
*/
|
|
|
138 |
protected $uglyStructHack = true;
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Preconfigured HTTP_Request2 provided by the user
|
|
|
142 |
*
|
|
|
143 |
* @var HTTP_Request2
|
|
|
144 |
*/
|
|
|
145 |
protected $httpRequest;
|
|
|
146 |
// }}}
|
|
|
147 |
|
|
|
148 |
// {{{ constructor
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Construct a new XML_RPC2_Client.
|
|
|
152 |
*
|
|
|
153 |
* To create a new XML_RPC2_Client, a URI must be provided (e.g. http://xmlrpc.example.com/1.0/).
|
|
|
154 |
* Optionally, some options may be set as an associative array. Accepted keys are :
|
|
|
155 |
* 'prefix', 'proxy', 'debug' => see correspondant property to get more informations
|
|
|
156 |
* 'encoding' => The XML encoding for the requests (optional, defaults to utf-8)
|
|
|
157 |
* 'sslverify' => boolean, true iff SSL certificates are to be verified against local cert database (optional, default false)
|
|
|
158 |
* 'connectionTimeout' => Timeout, in seconds, for the XML-RPC HTTP request (optional, default is no timeout)
|
|
|
159 |
* 'httpRequest' => Preconfigured HTTP_Request2 instance to be used in executing the XML-RPC calls (optional)
|
|
|
160 |
*
|
|
|
161 |
* @param string URI for the XML-RPC server
|
|
|
162 |
* @param array (optional) Associative array of options
|
|
|
163 |
*/
|
|
|
164 |
protected function __construct($uri, $options = array())
|
|
|
165 |
{
|
|
|
166 |
if (!$uriParse = parse_url($uri)) {
|
|
|
167 |
throw new XML_RPC2_InvalidUriException(sprintf('Client URI \'%s\' is not valid', $uri));
|
|
|
168 |
}
|
|
|
169 |
$this->uri = $uri;
|
|
|
170 |
if (isset($options['prefix'])) {
|
|
|
171 |
if (!(XML_RPC2_ClientHelper::testMethodName($options['prefix']))) {
|
|
|
172 |
throw new XML_RPC2_InvalidPrefixException(sprintf('Prefix \'%s\' is not valid', $options['prefix']));
|
|
|
173 |
}
|
|
|
174 |
$this->prefix = $options['prefix'];
|
|
|
175 |
}
|
|
|
176 |
if (isset($options['proxy'])) {
|
|
|
177 |
if (!$proxyParse = parse_url($options['proxy'])) {
|
|
|
178 |
throw new XML_RPC2_InvalidProxyException(sprintf('Proxy URI \'%s\' is not valid', $options['proxy']));
|
|
|
179 |
}
|
|
|
180 |
$this->proxy = $options['proxy'];
|
|
|
181 |
}
|
|
|
182 |
if (isset($options['debug'])) {
|
|
|
183 |
if (!(is_bool($options['debug']))) {
|
|
|
184 |
throw new XML_RPC2_InvalidDebugException(sprintf('Debug \'%s\' is not valid', $options['debug']));
|
|
|
185 |
}
|
|
|
186 |
$this->debug = $options['debug'];
|
|
|
187 |
}
|
|
|
188 |
if (isset($options['encoding'])) {
|
|
|
189 |
// TODO : control & exception
|
|
|
190 |
$this->encoding = $options['encoding'];
|
|
|
191 |
}
|
|
|
192 |
if (isset($options['escaping'])) {
|
|
|
193 |
// TODO : control & exception
|
|
|
194 |
$this->escaping = $options['escaping'];
|
|
|
195 |
}
|
|
|
196 |
if (isset($options['uglyStructHack'])) {
|
|
|
197 |
$this->uglyStructHack = $options['uglyStructHack'];
|
|
|
198 |
}
|
|
|
199 |
if (isset($options['sslverify'])) {
|
|
|
200 |
if (!(is_bool($options['sslverify']))) {
|
|
|
201 |
throw new XML_RPC2_InvalidSslverifyException(sprintf('SSL verify \'%s\' is not valid', $options['sslverify']));
|
|
|
202 |
}
|
|
|
203 |
$this->sslverify = $options['sslverify'];
|
|
|
204 |
}
|
|
|
205 |
if (isset($options['connectionTimeout'])) {
|
|
|
206 |
if (!(is_int($options['connectionTimeout']))) {
|
|
|
207 |
throw new XML_RPC2_InvalidConnectionTimeoutException(sprintf('Connection timeout \'%s\' is not valid', $options['connectionTimeout']));
|
|
|
208 |
}
|
|
|
209 |
$this->connectionTimeout = $options['connectionTimeout'];
|
|
|
210 |
}
|
|
|
211 |
if (isset($options['httpRequest'])) {
|
|
|
212 |
$this->httpRequest = $options['httpRequest'];
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
// }}}
|
|
|
217 |
// {{{ create()
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Factory method to select, create and return a XML_RPC2_Client backend
|
|
|
221 |
*
|
|
|
222 |
* To create a new XML_RPC2_Client, a URI must be provided (e.g. http://xmlrpc.example.com/1.0/).
|
|
|
223 |
*
|
|
|
224 |
* Optionally, some options may be set.
|
|
|
225 |
*
|
|
|
226 |
* @param string URI for the XML-RPC server
|
|
|
227 |
* @param array (optional) associative array of options (see constructor)
|
|
|
228 |
*/
|
|
|
229 |
public static function create($uri, $options = array())
|
|
|
230 |
{
|
|
|
231 |
if (isset($this)) { // Method called non-statically forward to remote call() as RPC
|
|
|
232 |
$this->__call('create', func_get_args());
|
|
|
233 |
}
|
|
|
234 |
if (isset($options['backend'])) {
|
|
|
235 |
XML_RPC2_Backend::setBackend($options['backend']);
|
|
|
236 |
}
|
|
|
237 |
$backend = XML_RPC2_Backend::getClientClassname();
|
|
|
238 |
return new $backend($uri, $options);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
// }}}
|
|
|
242 |
// {{{ __call()
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* __call Catchall. This method catches remote method calls and provides for remote forwarding.
|
|
|
246 |
*
|
|
|
247 |
* If the parameters are native types, this method will use XML_RPC_Value::createFromNative to
|
|
|
248 |
* convert it into an XML-RPC type. Whenever a parameter is already an instance of XML_RPC_Value
|
|
|
249 |
* it will be used as provided. It follows that, in situations when XML_RPC_Value::createFromNative
|
|
|
250 |
* proves inacurate -- as when encoding DateTime values -- you should present an instance of
|
|
|
251 |
* XML_RPC_Value in lieu of the native parameter.
|
|
|
252 |
*
|
|
|
253 |
* @param string Method name
|
|
|
254 |
* @param array Parameters
|
|
|
255 |
* @return mixed The call result, already decoded into native types
|
|
|
256 |
*/
|
|
|
257 |
public abstract function __call($methodName, $parameters);
|
|
|
258 |
|
|
|
259 |
// }}}
|
|
|
260 |
}
|
|
|
261 |
|