1 |
joko |
1.1 |
<? |
2 |
joko |
1.2 |
/** |
3 |
|
|
* This file contains the DataSource::Proxy::XMLRPC module. |
4 |
|
|
* |
5 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
|
|
* @package org.netfrag.glib |
7 |
|
|
* @name DataSource::Proxy::XMLRPC |
8 |
|
|
* |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
|
12 |
joko |
1.1 |
/* |
13 |
|
|
## -------------------------------------------------------------------------- |
14 |
joko |
1.5 |
## $Id: XMLRPC.php,v 1.4 2003/03/28 03:00:12 joko Exp $ |
15 |
joko |
1.1 |
## -------------------------------------------------------------------------- |
16 |
joko |
1.2 |
## $Log: XMLRPC.php,v $ |
17 |
joko |
1.5 |
## Revision 1.4 2003/03/28 03:00:12 joko |
18 |
|
|
## enhanced error- and exception-handling |
19 |
|
|
## |
20 |
joko |
1.4 |
## Revision 1.3 2003/03/27 16:05:01 joko |
21 |
|
|
## enhanced 'function _call': debugging, tracing and autodisconnect now parametrized |
22 |
|
|
## |
23 |
joko |
1.3 |
## Revision 1.2 2003/03/05 23:16:48 joko |
24 |
|
|
## updated docu - phpDocumentor is very strict about its 'blocks'... |
25 |
|
|
## |
26 |
joko |
1.2 |
## Revision 1.1 2003/03/03 21:53:52 joko |
27 |
|
|
## refactored from Data::Driver::RPC::Remote |
28 |
|
|
## |
29 |
joko |
1.1 |
## Revision 1.4 2003/02/20 21:42:43 joko |
30 |
|
|
## + fix: always converting from utf8 into latin here (for now) |
31 |
|
|
## |
32 |
|
|
## Revision 1.3 2003/02/13 21:51:29 joko |
33 |
|
|
## + now can remember its connection-state |
34 |
|
|
## + sub ping |
35 |
|
|
## |
36 |
|
|
## Revision 1.2 2003/02/13 00:42:44 joko |
37 |
|
|
## +- renamed modules |
38 |
|
|
## |
39 |
|
|
## Revision 1.1 2003/02/09 17:25:38 joko |
40 |
|
|
## + refactored from flib/Application/RPC/Remote.php |
41 |
|
|
## |
42 |
|
|
## Revision 1.6 2003/02/03 03:37:45 jonen |
43 |
|
|
## - removed unused argument '$decode' at function '_call()' |
44 |
|
|
## + added argument '$options=array()' at function '_call()' |
45 |
|
|
## which will be passed to function '&decodeData()' for e.g. utf8 decoding |
46 |
|
|
## + added '$encoder->toISO()' at '&decodeData()', (re)moved from 'ProxyObject.php' |
47 |
|
|
## |
48 |
|
|
## Revision 1.5 2002/12/22 13:24:09 jonen |
49 |
|
|
## + added utf8 encoding at 'send()' toggled by option |
50 |
|
|
## |
51 |
|
|
## Revision 1.4 2002/12/19 02:02:25 jonen |
52 |
|
|
## + minor changes |
53 |
|
|
## |
54 |
|
|
## Revision 1.3 2002/12/19 01:59:37 jonen |
55 |
|
|
## + minor changes: coment debug prints |
56 |
|
|
## |
57 |
|
|
## Revision 1.2 2002/12/05 21:45:31 joko |
58 |
|
|
## + debugging |
59 |
|
|
## |
60 |
|
|
## Revision 1.1 2002/12/01 17:23:58 joko |
61 |
|
|
## + initial check-in |
62 |
|
|
## |
63 |
|
|
## Revision 1.3 2002/12/01 06:22:57 cvsjoko |
64 |
|
|
## + minor update: now can use config defaults or given args |
65 |
|
|
## |
66 |
|
|
## Revision 1.2 2002/10/29 19:14:45 cvsjoko |
67 |
|
|
## - bugfix: dont' do utf8-encoding here |
68 |
|
|
## |
69 |
|
|
## Revision 1.1 2002/10/09 00:51:39 cvsjoko |
70 |
|
|
## + new |
71 |
|
|
## |
72 |
|
|
## |
73 |
|
|
## ------------------------------------------------------------------------- |
74 |
|
|
*/ |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
// V1: normal require |
78 |
|
|
//require_once 'XML/RPC/RPC.php'; |
79 |
|
|
|
80 |
|
|
// V2: first time load of foreign module (PEAR::XML::RPC) |
81 |
|
|
//require_once 'XML/RPC/RPC.php'; |
82 |
|
|
loadModule('XML::RPC::RPC'); |
83 |
|
|
|
84 |
|
|
loadModule('Class::Logger'); |
85 |
|
|
|
86 |
joko |
1.2 |
|
87 |
|
|
/** |
88 |
|
|
* This is the DataSource::Proxy::XMLRPC module. |
89 |
|
|
* |
90 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
91 |
|
|
* @package org.netfrag.glib |
92 |
|
|
* @subpackage DataSource |
93 |
|
|
* @name DataSource::Proxy::XMLRPC |
94 |
|
|
* |
95 |
|
|
* @todo SOAP? |
96 |
|
|
* |
97 |
|
|
*/ |
98 |
joko |
1.1 |
class DataSource_Proxy_XMLRPC extends Class_Logger { |
99 |
|
|
|
100 |
|
|
var $configured; |
101 |
joko |
1.4 |
var $meta = array(); |
102 |
|
|
var $response = null; |
103 |
|
|
var $errors = array(); |
104 |
joko |
1.1 |
|
105 |
|
|
function DataSource_Proxy_XMLRPC($args = array()) { |
106 |
|
|
|
107 |
|
|
parent::constructor(); |
108 |
|
|
|
109 |
|
|
//print Dumper($this, $args); |
110 |
joko |
1.4 |
//print Dumper($args); |
111 |
joko |
1.1 |
|
112 |
|
|
global $Data_Driver_RPC_Remote_meta; |
113 |
|
|
session_register_safe("Data_Driver_RPC_Remote_meta"); |
114 |
|
|
$this->meta = $Data_Driver_RPC_Remote_meta; |
115 |
|
|
|
116 |
joko |
1.4 |
// force connect? |
117 |
|
|
|
118 |
|
|
// V1 |
119 |
|
|
/* |
120 |
joko |
1.1 |
if (!isset($this->meta[connected]) || $args[connect]) { |
121 |
|
|
$this->meta[connected] = 1; |
122 |
|
|
} |
123 |
joko |
1.4 |
*/ |
124 |
|
|
|
125 |
|
|
// V2 |
126 |
|
|
$this->FORCE_CONNECT = $args[connect]; |
127 |
|
|
|
128 |
joko |
1.1 |
$this->_save_meta(); |
129 |
|
|
|
130 |
joko |
1.4 |
|
131 |
joko |
1.3 |
// merge args - V1 |
132 |
|
|
//if ($args['Host']) { $this->HOST = $args['Host']; } |
133 |
|
|
//if ($args['Port']) { $this->PORT = $args['Port']; } |
134 |
|
|
|
135 |
|
|
// merge args - V2 |
136 |
|
|
// TODO: php::merge_to($this, $args); |
137 |
|
|
//php::merge_to($this, $args); |
138 |
|
|
//print Dumper($args); |
139 |
|
|
if (is_array($args)) { |
140 |
|
|
foreach ($args as $key => $val) { |
141 |
joko |
1.5 |
//print "key: $key<br/>"; |
142 |
joko |
1.3 |
$key = strtoupper($key); |
143 |
|
|
$this->$key = $val; |
144 |
|
|
} |
145 |
|
|
} |
146 |
joko |
1.1 |
|
147 |
|
|
// check if host is valid |
148 |
joko |
1.3 |
if (!$this->HOST) { |
149 |
joko |
1.1 |
$this->_raiseException( "->constructor: attribute 'host' is empty, please check your settings"); |
150 |
|
|
return; |
151 |
|
|
} |
152 |
|
|
|
153 |
joko |
1.3 |
if (!$this->PORT) { |
154 |
joko |
1.1 |
$this->_raiseException( "->constructor: attribute 'port' is empty, please check your settings"); |
155 |
|
|
return; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
$this->configured = 1; |
159 |
|
|
|
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
function _save_meta() { |
163 |
|
|
global $Data_Driver_RPC_Remote_meta; |
164 |
|
|
$Data_Driver_RPC_Remote_meta = $this->meta; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
function send($command, $data = "", $options = array()) { |
168 |
|
|
|
169 |
|
|
$this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG); |
170 |
|
|
|
171 |
joko |
1.5 |
/* |
172 |
joko |
1.1 |
if (!$this->isConnected()) { |
173 |
joko |
1.3 |
$this->_raiseException( "->send[ command=$command ]: not connected while trying to send command!"); |
174 |
joko |
1.1 |
return; |
175 |
|
|
} |
176 |
joko |
1.5 |
*/ |
177 |
joko |
1.1 |
|
178 |
|
|
// do 'encode' here and ... |
179 |
|
|
if ($options[utf8]) { |
180 |
|
|
$encoder = new Data_Encode($data); |
181 |
|
|
$encoder->toUTF8(); |
182 |
|
|
} |
183 |
|
|
// call '_call' with 'decode' |
184 |
|
|
return $this->_call($command, $data, $options); |
185 |
|
|
} |
186 |
|
|
|
187 |
joko |
1.3 |
|
188 |
joko |
1.1 |
function _call($command, $data = "", $options = array() ) { |
189 |
|
|
|
190 |
|
|
if (!$this->configured) { |
191 |
|
|
$this->_raiseException( "->_call: class not configured properly"); |
192 |
|
|
return; |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
// populate options list - mostly for debugging purposes |
196 |
|
|
$options_list = array(); |
197 |
|
|
foreach ($options as $key => $value) { |
198 |
|
|
array_push($options_list, "$key=$value"); |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
$data_debug = $data; |
202 |
|
|
if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); } |
203 |
|
|
$options_debug = join(", ", $options_list); |
204 |
joko |
1.4 |
$this->log(get_class($this) . "->_call: " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG); |
205 |
joko |
1.1 |
|
206 |
|
|
// trace |
207 |
|
|
//print "call: $command<hr>"; |
208 |
|
|
//print Dumper($data); |
209 |
|
|
//print Dumper($this); |
210 |
|
|
|
211 |
|
|
// data |
212 |
joko |
1.3 |
$data_enc = XML_RPC_encode($data); |
213 |
joko |
1.1 |
|
214 |
|
|
// message - request |
215 |
joko |
1.3 |
$msg = new XML_RPC_Message($command); |
216 |
|
|
$msg->addParam($data_enc); |
217 |
|
|
|
218 |
|
|
// ??? |
219 |
|
|
//print htmlentities($msg->serialize()); |
220 |
|
|
|
221 |
joko |
1.1 |
// remote procedure call |
222 |
joko |
1.3 |
$rpc = new XML_RPC_Client("/", $this->HOST, $this->PORT); |
223 |
|
|
|
224 |
|
|
// TODO: detect highlevel errors, raise proper exceptions on them (e.g. 'Unknown method', 'Method signature error(s)') |
225 |
|
|
// done: now possible to declare tracing at this point in configuration |
226 |
|
|
// Please look inside your {appname}/etc/hosts/{hostname}.php |
227 |
|
|
$rpc->setDebug($this->TRACE); |
228 |
joko |
1.4 |
//$rpc->setDebug(1); |
229 |
joko |
1.3 |
|
230 |
joko |
1.4 |
if ( $this->response = $rpc->send($msg) ) { |
231 |
|
|
// intermediate result error checking |
232 |
|
|
$this->_checkException(); |
233 |
|
|
} else { |
234 |
joko |
1.3 |
// TODO: redirect this error elsewhere! |
235 |
|
|
//print "RPC-error!<br>"; |
236 |
|
|
$this->_raiseException( "->_call: no response"); |
237 |
|
|
return; |
238 |
|
|
} |
239 |
joko |
1.4 |
|
240 |
joko |
1.1 |
// message - response |
241 |
joko |
1.4 |
$response_enc = $this->response->value(); |
242 |
joko |
1.3 |
|
243 |
|
|
// TODO: what's this? prematurely returning here should not be considered "stable".... |
244 |
|
|
return $this->decodeData($response_enc, $options); |
245 |
joko |
1.1 |
|
246 |
|
|
} |
247 |
|
|
|
248 |
joko |
1.3 |
|
249 |
joko |
1.1 |
function &decodeData(&$payload, $options = array() ) { |
250 |
|
|
//if (!is_object($payload)) { return; } |
251 |
|
|
if ($payload) { |
252 |
|
|
// data |
253 |
|
|
$data = XML_RPC_decode($payload); |
254 |
|
|
//print "data: " . dumpVar($response); |
255 |
|
|
|
256 |
|
|
// decode UTF8 to ISO if wanted |
257 |
|
|
//if ($options[to_latin]) { |
258 |
|
|
$encoder = new Data_Encode($data); |
259 |
|
|
$encoder->toISO(); |
260 |
|
|
//} |
261 |
|
|
|
262 |
joko |
1.4 |
$this->_be_connected(); |
263 |
|
|
|
264 |
joko |
1.1 |
return $data; |
265 |
|
|
} else { |
266 |
|
|
//print "ERROR!<br>"; |
267 |
|
|
return 0; |
268 |
|
|
} |
269 |
|
|
} |
270 |
|
|
|
271 |
joko |
1.4 |
function _be_connected() { |
272 |
|
|
$this->meta[connected] = 1; |
273 |
|
|
$this->_save_meta(); |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
function _raiseException($message, $code = null) { |
277 |
|
|
|
278 |
joko |
1.5 |
$classname = get_class($this); |
279 |
|
|
|
280 |
joko |
1.4 |
// aggregate errors for this run/query |
281 |
joko |
1.5 |
$this->_add_error($classname . $message, $code); |
282 |
joko |
1.3 |
|
283 |
|
|
// spout out the error message of the raised exception |
284 |
joko |
1.5 |
$this->log($classname . $message, PEAR_LOG_ERR); |
285 |
joko |
1.3 |
|
286 |
joko |
1.4 |
// handle some stuff regarding more special behaviour (will this get rule-based sometimes?) |
287 |
|
|
|
288 |
|
|
// handle 'FORCE_CONNECT' |
289 |
|
|
$connect_condition = $this->FORCE_CONNECT = ($this->isConnected() && $this->DISCONNECT_ON_ERROR); |
290 |
|
|
if ($connect_condition) { |
291 |
joko |
1.3 |
$message = '->_raiseException: [DISCONNECT_ON_ERROR] done transparently. Please reconnect manually.'; |
292 |
joko |
1.5 |
$this->_add_error($classname . $message, $code); |
293 |
|
|
//$this->log($classname . $message, PEAR_LOG_WARNING); |
294 |
|
|
$this->log($classname . $message, PEAR_LOG_ERR); |
295 |
joko |
1.3 |
$this->meta[connected] = 0; |
296 |
|
|
$this->_save_meta(); |
297 |
|
|
} |
298 |
joko |
1.1 |
} |
299 |
|
|
|
300 |
|
|
function isConnected() { |
301 |
|
|
return $this->meta[connected]; |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
function ping() { |
305 |
|
|
$this->_call('ping'); |
306 |
joko |
1.4 |
} |
307 |
|
|
|
308 |
|
|
|
309 |
|
|
function _checkException() { |
310 |
|
|
// Please look inside your {appname}/etc/hosts/{hostname}.php for toggling $this->DEBUG |
311 |
|
|
if ($error_code = $this->response->faultCode()) { |
312 |
|
|
//print $msg_response->faultString(); |
313 |
|
|
$this->_raiseException( "->_call: " . $this->response->faultString(), $error_code ); |
314 |
|
|
} |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
//if ($this->DEBUG && $error_code = $msg_response->faultCode()) { |
318 |
|
|
|
319 |
|
|
|
320 |
|
|
function _add_error($message, $code) { |
321 |
|
|
array_push( $this->errors, array(code => $code, message => $message) ); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
function getStatus() { |
325 |
|
|
return array( connected => $this->isConnected(), errors => $this->errors ); |
326 |
joko |
1.1 |
} |
327 |
|
|
|
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
?> |