-
Notifications
You must be signed in to change notification settings - Fork 114
/
XmlrpcSource.php
303 lines (286 loc) · 7.98 KB
/
XmlrpcSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* XML-RPC Datasource
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @link http://www.xmlrpc.com/spec Specification
* @package datasources
* @subpackage datasources.models.datasources
* @since CakePHP Datasources v 0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('DataSource', 'Model/Datasource');
App::uses('Xml', 'Utility');
App::uses('HttpSocket', 'Network/Http');
/**
* XmlrpcSource
*
* Datasource for XML-RPC
*/
class XmlrpcSource extends DataSource {
/**
* Description string for this Data Source.
*
* @var string
*/
public $description = 'XmlRpc Datasource';
/**
* HttpSocket Object
*
* @var object HttpSocket
*/
public $HttpSocket = null;
/**
* Cache for describe
*
* @var mixed Array with methods or false if not supported by server
*/
protected $_cacheDescribe = null;
/**
* Configuration base
*
* @var array
*/
public $_baseConfig = array(
'host' => '127.0.0.1',
'port' => 80,
'url' => '/RPC2',
'timeout' => 20
);
/**
* Default Constructor
*
* @param array $config options
*/
public function __construct($config = array()) {
parent::__construct($config);
}
/**
* Checks if the source is connected.
*
* @return boolean
*/
public function isConnected() {
return true;
}
/**
* Perform a XML RPC call
*
* @param string $method XML-RPC method name
* @param array $params List with XML-RPC parameters
* @param Model $model Reference to model (unused)
* @return mixed Response of XML-RPC Server. If return false, $this->error contain a error message.
*/
public function query($method, $params = array(), &$model = null) {
if (!is_string($method)) {
return false;
}
return $this->_request($method, $params);
}
/**
* List supported methods by server.
*
* @param Model $model
* @return mixed Array with methods or false if not supported
*/
public function describe($model = null) {
if (!is_null($this->_cacheDescribe)) {
return $this->_cacheDescribe;
}
$this->_cacheDescribe = $this->query('system.listMethods');
return $this->_cacheDescribe;
}
/**
* Perform a request via HTTP
*
* @param string $method Name of method
* @param array $params List of methods
* @return mixed Response of XML-RPC Server
*/
protected function _request($method, $params) {
$xmlRequest = $this->generateXML($method, $params);
if (!$this->HttpSocket) {
$this->HttpSocket =& new HttpSocket(array('timeout' => $this->config['timeout']));
}
$uri = array(
'host' => $this->config['host'],
'port' => $this->config['port'],
'path' => $this->config['url']
);
try {
$response = $this->HttpSocket->post($uri, $xmlRequest, array('header' => array('Content-Type' => 'text/xml')));
} catch (Exception $e) {
return $this->_error(-32300, $e->getMessage());
}
if (!$this->HttpSocket->response['status']['code']) {
return $this->_error(-32300, __('Transport error - could not open socket', true));
}
if ($this->HttpSocket->response['status']['code'] != 200) {
return $this->_error(-32300, __('Transport error - HTTP status code was not 200', true));
}
return $this->parseResponse($response);
}
/**
* Generate a XML for request
*
* @param string $method Name of method
* @param array $params List of methods
* @return string XML of request
*/
public function generateXML($method, $params = array()) {
$query = array(
'methodCall' => array(
'methodName' => $method,
'params' => array()
)
);
if (!empty($params)) {
$query['methodCall']['params']['param'] = array();
foreach ($params as $param) {
$query['methodCall']['params']['param'][] = $this->_normalizeParam($param);
}
}
$Xml = Xml::fromArray($query, array('format' => 'tags'));
return $Xml->asXml();
}
/**
* Parse a response from XML RPC Server
*
* @param string $response XML from Server
* @return mixed Response as PHP
*/
public function parseResponse($response) {
$Xml = Xml::build(strval($response));
$data = Xml::toArray($Xml);
unset($Xml);
if (isset($data['methodResponse']['fault'])) {
return $this->__parseResponseError($data);
}
if (!isset($data['methodResponse']['params']['param']['value'])) {
return $this->_error(-32700, __('Parse error. Not well formed', true));
}
$this->_error(0, '');
return $this->__parseResponse($data['methodResponse']['params']['param']['value']);
}
/**
* Transform params in arrays to XML Class
*
* @param mixed $param Parameter
* @return array Parameter to XML Class
*/
protected function _normalizeParam($param) {
if (is_array($param)) {
if (empty($param) || isset($param[0])) { // Single consideration if is array or struct
// Is array
$data = array();
foreach ($param as $item) {
$normalized = $this->_normalizeParam($item);
$data[] = $normalized['value'];
}
$return = array('value' => array('array' => array('data' => array())));
if (!empty($data)) {
$return['value']['array']['data']['value'] = $data;
}
return $return;
}
// Is struct
$members = array();
foreach ($param as $name => $value) {
$members[] = array_merge(compact('name'), $this->_normalizeParam($value));
}
return array('value' => array('struct' => array('member' => $members)));
} elseif (is_int($param)) {
return array('value' => array('int' => $param));
} elseif (is_bool($param)) {
return array('value' => array('boolean' => $param ? '1' : '0'));
} elseif (is_numeric($param)) {
return array('value' => array('double' => $param));
}
return array('value' => array('string' => $param));
}
/**
* Parse a response if server response with error/fault
*
* @param array $data Response as array of XML Class
* @return boolean Always false
*/
private function __parseResponseError(&$data) {
foreach ($data['methodResponse']['fault']['value']['struct']['member'] as $member) {
if ($member['name'] === 'faultCode') {
if (isset($member['value']['int'])) {
$this->errno = (int)$member['value']['int'];
} elseif (isset($member['value']['i4'])) {
$this->errno = (int)$member['value']['i4'];
}
} elseif ($member['name'] === 'faultString' && isset($member['value']['string'])) {
$this->error = $member['value']['string'];
}
}
return false;
}
/**
* Parse a valid response from server
*
* @param array $value Value
* @return mixed
*/
private function __parseResponse($value) {
$type = array_keys($value);
$type = $type[0];
$value = $value[$type];
switch ($type) {
case 'i4':
return (int)$value;
case 'double':
return (float)$value;
case 'array':
$return = array();
if (isset($value['data']['value']) && is_array($value['data']['value'])) {
foreach ($value['data']['value'] as $key => $newValue) {
// Reconstruct an array form, for arrays with only one entry.
if (!is_array($newValue)) {
$newValue = array($key => $newValue);
$key = 0;
}
if ($key === 'struct' || $key === 'array') {
$return[] = $this->__parseResponse(array($key => $newValue));
} else {
$return[] = $this->__parseResponse($newValue);
}
}
}
return $return;
case 'struct':
$return = array();
foreach ($value['member'] as $member) {
$return[$member['name']] = $this->__parseResponse($member['value']);
}
return $return;
default:
settype($value, $type);
return $value;
}
return null;
}
/**
* Set a error message and number
*
* @param integer $number Number of error
* @param string $text Description of error
* @return boolean Always false
*/
protected function _error($number, $text) {
$this->errno = $number;
$this->error = $text;
return false;
}
}