14 |
## $Id$ |
## $Id$ |
15 |
## -------------------------------------------------------------------------- |
## -------------------------------------------------------------------------- |
16 |
## $Log$ |
## $Log$ |
17 |
|
## Revision 1.7 2003/04/18 15:42:26 joko |
18 |
|
## minor update to comment |
19 |
|
## |
20 |
|
## Revision 1.6 2003/04/04 21:23:29 joko |
21 |
|
## session-based rpc-communcation: revamped code (especially 'function _call') |
22 |
|
## renamed state-handling trigger-methods: now _sleep and _wakeup |
23 |
|
## |
24 |
|
## Revision 1.5 2003/03/28 06:56:52 joko |
25 |
|
## updated logging/debugging code |
26 |
|
## |
27 |
|
## Revision 1.4 2003/03/28 03:00:12 joko |
28 |
|
## enhanced error- and exception-handling |
29 |
|
## |
30 |
## Revision 1.3 2003/03/27 16:05:01 joko |
## Revision 1.3 2003/03/27 16:05:01 joko |
31 |
## enhanced 'function _call': debugging, tracing and autodisconnect now parametrized |
## enhanced 'function _call': debugging, tracing and autodisconnect now parametrized |
32 |
## |
## |
108 |
class DataSource_Proxy_XMLRPC extends Class_Logger { |
class DataSource_Proxy_XMLRPC extends Class_Logger { |
109 |
|
|
110 |
var $configured; |
var $configured; |
111 |
var $meta; |
var $meta = array(); |
112 |
|
var $response = null; |
113 |
|
var $errors = array(); |
114 |
|
|
115 |
function DataSource_Proxy_XMLRPC($args = array()) { |
function DataSource_Proxy_XMLRPC($args = array()) { |
116 |
|
|
117 |
parent::constructor(); |
parent::constructor(); |
118 |
|
|
119 |
//print Dumper($this, $args); |
//print Dumper($this, $args); |
120 |
|
//print Dumper($args); |
121 |
|
|
122 |
global $Data_Driver_RPC_Remote_meta; |
$this->_wakeup(); |
|
session_register_safe("Data_Driver_RPC_Remote_meta"); |
|
|
$this->meta = $Data_Driver_RPC_Remote_meta; |
|
123 |
|
|
124 |
|
// force connect? |
125 |
|
|
126 |
|
// V1 |
127 |
|
/* |
128 |
if (!isset($this->meta[connected]) || $args[connect]) { |
if (!isset($this->meta[connected]) || $args[connect]) { |
129 |
$this->meta[connected] = 1; |
$this->meta[connected] = 1; |
130 |
} |
} |
131 |
$this->_save_meta(); |
*/ |
132 |
|
|
133 |
|
// V2 |
134 |
|
$this->FORCE_CONNECT = $args[connect]; |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
// merge args - V1 |
// merge args - V1 |
139 |
//if ($args['Host']) { $this->HOST = $args['Host']; } |
//if ($args['Host']) { $this->HOST = $args['Host']; } |
145 |
//print Dumper($args); |
//print Dumper($args); |
146 |
if (is_array($args)) { |
if (is_array($args)) { |
147 |
foreach ($args as $key => $val) { |
foreach ($args as $key => $val) { |
148 |
|
//print "key: $key<br/>"; |
149 |
$key = strtoupper($key); |
$key = strtoupper($key); |
150 |
$this->$key = $val; |
$this->$key = $val; |
151 |
} |
} |
166 |
|
|
167 |
} |
} |
168 |
|
|
169 |
function _save_meta() { |
function _wakeup() { |
170 |
|
global $Data_Driver_RPC_Remote_meta; |
171 |
|
session_register_safe("Data_Driver_RPC_Remote_meta"); |
172 |
|
$this->meta = $Data_Driver_RPC_Remote_meta; |
173 |
|
$this->_sleep(); |
174 |
|
} |
175 |
|
|
176 |
|
function _sleep() { |
177 |
global $Data_Driver_RPC_Remote_meta; |
global $Data_Driver_RPC_Remote_meta; |
178 |
$Data_Driver_RPC_Remote_meta = $this->meta; |
$Data_Driver_RPC_Remote_meta = $this->meta; |
179 |
} |
} |
182 |
|
|
183 |
$this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG); |
$this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG); |
184 |
|
|
185 |
|
/* |
186 |
if (!$this->isConnected()) { |
if (!$this->isConnected()) { |
187 |
$this->_raiseException( "->send[ command=$command ]: not connected while trying to send command!"); |
$this->_raiseException( "->send[ command=$command ]: not connected while trying to send command!"); |
188 |
return; |
return; |
189 |
} |
} |
190 |
|
*/ |
191 |
|
|
192 |
// do 'encode' here and ... |
// do 'encode' here and ... |
193 |
if ($options[utf8]) { |
if ($options[utf8]) { |
201 |
|
|
202 |
function _call($command, $data = "", $options = array() ) { |
function _call($command, $data = "", $options = array() ) { |
203 |
|
|
204 |
if (!$this->configured) { |
$RETRY_COUNT = 0; |
205 |
$this->_raiseException( "->_call: class not configured properly"); |
$RETRY_MAX = 1; |
|
return; |
|
|
} |
|
|
|
|
|
// populate options list - mostly for debugging purposes |
|
|
$options_list = array(); |
|
|
foreach ($options as $key => $value) { |
|
|
array_push($options_list, "$key=$value"); |
|
|
} |
|
|
|
|
|
$data_debug = $data; |
|
|
if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); } |
|
|
$options_debug = join(", ", $options_list); |
|
|
$this->log(get_class($this) . ": " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG); |
|
|
|
|
|
// trace |
|
|
//print "call: $command<hr>"; |
|
|
//print Dumper($data); |
|
|
//print Dumper($this); |
|
206 |
|
|
207 |
// data |
do { |
|
$data_enc = XML_RPC_encode($data); |
|
208 |
|
|
209 |
// message - request |
$RETRY_COUNT++; |
|
$msg = new XML_RPC_Message($command); |
|
|
$msg->addParam($data_enc); |
|
|
|
|
|
// ??? |
|
|
//print htmlentities($msg->serialize()); |
|
|
|
|
|
// remote procedure call |
|
|
$rpc = new XML_RPC_Client("/", $this->HOST, $this->PORT); |
|
|
|
|
|
// TODO: detect highlevel errors, raise proper exceptions on them (e.g. 'Unknown method', 'Method signature error(s)') |
|
|
// done: now possible to declare tracing at this point in configuration |
|
|
// Please look inside your {appname}/etc/hosts/{hostname}.php |
|
|
$rpc->setDebug($this->TRACE); |
|
|
|
|
|
if ( !$msg_response = $rpc->send($msg) ) { |
|
|
// TODO: redirect this error elsewhere! |
|
|
//print "RPC-error!<br>"; |
|
|
$this->_raiseException( "->_call: no response"); |
|
|
return; |
|
|
} |
|
210 |
|
|
211 |
|
$this->__exception_method = '_call'; |
212 |
|
|
213 |
|
if (!$this->configured) { |
214 |
|
$this->_raiseException("class not configured properly"); |
215 |
|
return; |
216 |
|
} |
217 |
|
|
218 |
|
// populate options list - mostly for debugging purposes |
219 |
|
$options_list = array(); |
220 |
|
foreach ($options as $key => $value) { |
221 |
|
array_push($options_list, "$key=$value"); |
222 |
|
} |
223 |
|
|
224 |
|
$data_debug = $data; |
225 |
|
if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); } |
226 |
|
$options_debug = join(", ", $options_list); |
227 |
|
// FIXME: replace through '_raiseException'? |
228 |
|
$this->log(get_class($this) . "->_call: " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG); |
229 |
|
|
230 |
|
// trace |
231 |
|
//print "call: $command<hr>"; |
232 |
|
//print Dumper($data); |
233 |
|
//print Dumper($this); |
234 |
|
|
235 |
|
// message - request |
236 |
|
$msg = new XML_RPC_Message($command); |
237 |
|
|
238 |
|
// data |
239 |
|
if ($data) { |
240 |
|
$data_enc = XML_RPC_encode($data); |
241 |
|
$msg->addParam($data_enc); |
242 |
|
} |
243 |
|
|
244 |
|
$this->_hook_request($msg); |
245 |
|
|
246 |
|
// ??? |
247 |
|
//print htmlentities($msg->serialize()); |
248 |
|
|
249 |
|
// remote procedure call |
250 |
|
$rpc = new XML_RPC_Client("/", $this->HOST, $this->PORT); |
251 |
|
|
252 |
|
// TODO: detect highlevel errors, raise proper exceptions on them (e.g. 'Unknown method', 'Method signature error(s)') |
253 |
|
// done: now possible to declare tracing at this point in configuration |
254 |
|
// Please look inside your {appname}/etc/hosts/{hostname}.php |
255 |
|
$rpc->setDebug($this->TRACE); |
256 |
|
|
257 |
|
//$rpc->setDebug(1); |
258 |
|
|
259 |
|
if ( $this->response = $rpc->send($msg) ) { |
260 |
|
// intermediate response checking |
261 |
|
$errcode = $this->_hook_errorcheck(); |
262 |
|
$negotiation = $this->_hook_negotiation($errcode); |
263 |
|
} else { |
264 |
|
// TODO: redirect this error elsewhere! |
265 |
|
//print "RPC-error!<br>"; |
266 |
|
$this->_raiseException("no response"); |
267 |
|
return; |
268 |
|
} |
269 |
|
|
270 |
|
// retry method issued right now - i.e. in case an authentication came in between |
271 |
|
if (!$negotiation[retry]) { |
272 |
|
break; |
273 |
|
} |
274 |
|
|
275 |
|
} while ($RETRY_COUNT < $RETRY_MAX); |
276 |
|
|
277 |
// message - response |
// message - response |
278 |
$response_enc = $msg_response->value(); |
$response_enc = $this->response->value(); |
|
|
|
|
// error handling |
|
|
// Please look inside your {appname}/etc/hosts/{hostname}.php for toggling $this->DEBUG |
|
|
if ($this->DEBUG && $error_code = $msg_response->faultCode()) { |
|
|
//print $msg_response->faultString(); |
|
|
$this->_raiseException( "->_call: " . $msg_response->faultString() ); |
|
|
} |
|
279 |
|
|
280 |
// TODO: what's this? prematurely returning here should not be considered "stable".... |
// TODO: what's this? prematurely returning here should not be considered "stable".... |
281 |
return $this->decodeData($response_enc, $options); |
$data = $this->decodeData($response_enc, $options); |
282 |
|
$this->_hook_response($data); |
283 |
|
return $data; |
284 |
|
|
285 |
} |
} |
286 |
|
|
298 |
$encoder->toISO(); |
$encoder->toISO(); |
299 |
//} |
//} |
300 |
|
|
301 |
$this->meta[connected] = 1; |
$this->_be_connected(); |
302 |
$this->_save_meta(); |
|
|
|
|
303 |
return $data; |
return $data; |
304 |
} else { |
} else { |
305 |
//print "ERROR!<br>"; |
//print "ERROR!<br>"; |
307 |
} |
} |
308 |
} |
} |
309 |
|
|
310 |
function _raiseException($message) { |
function _be_connected() { |
311 |
|
$this->meta[connected] = 1; |
312 |
|
$this->_sleep(); |
313 |
|
} |
314 |
|
|
315 |
|
function _raiseException($message, $code = null) { |
316 |
|
|
317 |
|
$classname = get_class($this); |
318 |
|
|
319 |
|
if ($this->__exception_method) { |
320 |
|
$message_full = $classname . '->' . $this->__exception_method . ': ' . $message; |
321 |
|
} |
322 |
|
|
323 |
|
// aggregate errors for this run/query |
324 |
|
$this->_add_error($message, $code); |
325 |
|
|
326 |
// spout out the error message of the raised exception |
// spout out the error message of the raised exception |
327 |
$this->log(get_class($this) . $message, PEAR_LOG_ERR); |
$this->log($classname . $message_full, PEAR_LOG_ERR); |
328 |
|
|
329 |
// handle some stuff regarding more special behaviour (will this get a 'rule' sometimes?) |
// handle some stuff regarding more special behaviour (will this get rule-based sometimes?) |
330 |
if ($this->isConnected() && $this->DISCONNECT_ON_ERROR) { |
|
331 |
|
// handle 'FORCE_CONNECT' |
332 |
|
$connect_condition = $this->FORCE_CONNECT = ($this->isConnected() && $this->DISCONNECT_ON_ERROR); |
333 |
|
if ($connect_condition) { |
334 |
$message = '->_raiseException: [DISCONNECT_ON_ERROR] done transparently. Please reconnect manually.'; |
$message = '->_raiseException: [DISCONNECT_ON_ERROR] done transparently. Please reconnect manually.'; |
335 |
$this->log(get_class($this) . $message, PEAR_LOG_WARNING); |
$this->_add_error($classname . $message, $code); |
336 |
|
//$this->log($classname . $message, PEAR_LOG_WARNING); |
337 |
|
$this->log($classname . $message, PEAR_LOG_ERR); |
338 |
$this->meta[connected] = 0; |
$this->meta[connected] = 0; |
339 |
$this->_save_meta(); |
$this->_sleep(); |
340 |
} |
} |
341 |
} |
} |
342 |
|
|
347 |
function ping() { |
function ping() { |
348 |
$this->_call('ping'); |
$this->_call('ping'); |
349 |
} |
} |
350 |
|
|
351 |
|
|
352 |
|
function _hook_negotiation($error_code) { |
353 |
|
|
354 |
|
$result = array(); |
355 |
|
|
356 |
|
// 401 = authorization required |
357 |
|
if ($error_code == 401) { |
358 |
|
|
359 |
|
//print "<b>401</b>!!!<br/>"; |
360 |
|
//print Dumper($this); |
361 |
|
|
362 |
|
// V1 - authenticate using hardcoded plaintext values (bad thing inside here!!!) |
363 |
|
$auth_ok = $this->_call('authenticate', array( user => 'hello', pass => '123' )); |
364 |
|
//$auth_ok = $this->_call('authenticate', array( user => 'hello', pass => '12345' )); |
365 |
|
|
366 |
|
// authentication success? try issued method again now... |
367 |
|
if ($auth_ok) { |
368 |
|
$result[retry] = 1; |
369 |
|
} |
370 |
|
|
371 |
|
// V2 - sub-negotiation: |
372 |
|
// "Do we already have a password supplied in cache?" |
373 |
|
// Use it if we have, otherwise redirect to login page |
374 |
|
// prompting for credentials for specified transport. |
375 |
|
//Header("Location: " . topicLink('Login')); |
376 |
|
//print "user: " . $this->USER . "<br/>"; |
377 |
|
//print "pass: " . $this->PASS . "<br/>"; |
378 |
|
|
379 |
|
} |
380 |
|
|
381 |
|
// 511 = sent unknown session identifier (probably: session timed out) |
382 |
|
// solution: invalidate local session in state => new session will be attempted on next request |
383 |
|
if ($error_code == 511) { |
384 |
|
$this->meta[session][enabled] = 0; |
385 |
|
$this->_sleep(); |
386 |
|
} |
387 |
|
|
388 |
|
return $result; |
389 |
|
|
390 |
|
} |
391 |
|
|
392 |
|
function _hook_errorcheck() { |
393 |
|
// Please look inside your {appname}/etc/hosts/{hostname}.php for toggling $this->DEBUG |
394 |
|
if ($error_code = $this->response->faultCode()) { |
395 |
|
// raise exception |
396 |
|
//print "fault-code: " . $this->response->faultCode() . "<br/>"; |
397 |
|
//print "fault-string: " . $this->response->faultString() . "<br/>"; |
398 |
|
$this->_raiseException($this->response->faultString(), $error_code); |
399 |
|
return $error_code; |
400 |
|
} |
401 |
|
} |
402 |
|
|
403 |
|
//if ($this->DEBUG && $error_code = $msg_response->faultCode()) { |
404 |
|
|
405 |
|
function _add_error($message, $code) { |
406 |
|
array_push( $this->errors, array(code => $code, message => $message) ); |
407 |
|
} |
408 |
|
|
409 |
|
function status() { |
410 |
|
$status = array( |
411 |
|
connected => $this->isConnected(), |
412 |
|
errors => $this->errors, |
413 |
|
); |
414 |
|
$status[RPCSESSID] = $this->meta[session][id]; |
415 |
|
return $status; |
416 |
|
} |
417 |
|
|
418 |
|
|
419 |
|
// TODO: introduce condition: just do if connection requires session and/or auth |
420 |
|
function _hook_request(&$request) { |
421 |
|
|
422 |
|
//print Dumper($request); |
423 |
|
|
424 |
|
// ignore core methods (like 'session_id') here to prevent recursive loops! |
425 |
|
// would be a bad thing on the net! |
426 |
|
if (in_array($request->methodname, array('ping', 'session_id'))) { return 1; } |
427 |
|
|
428 |
|
// calculate conditions |
429 |
|
$session_requested = 1; |
430 |
|
//$use_session = $this->meta[session][requested]; |
431 |
|
$session_running = $this->meta[session][enabled]; |
432 |
|
$session_error = $this->meta[session][error]; |
433 |
|
$init_session = $session_requested && !$session_running && !$session_error; |
434 |
|
|
435 |
|
// 1. if session already established, add sessionid to request and return |
436 |
|
if ($session_running) { |
437 |
|
//trace("Session " . $this->meta[session][id] . " already initialized!" . "<br/>"); |
438 |
|
// append additional parameter (hash with key 'RPCSESSID') |
439 |
|
// to send assigned sessionid back to server inside each request |
440 |
|
$data = array( RPCSESSID => $this->meta[session][id] ); |
441 |
|
$data_enc = XML_RPC_encode($data); |
442 |
|
//print Dumper($data_enc); |
443 |
|
$request->addParam($data_enc); |
444 |
|
//return 1; |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
|
// 2. if session is required/requested, but not yet initialized, |
449 |
|
// do it to be able to propagate the sessionid transparently later |
450 |
|
|
451 |
|
if ($init_session) { |
452 |
|
static $init_count; |
453 |
|
$init_count++; |
454 |
|
//trace("Initializing session: $init_count" . "<br/>"); |
455 |
|
//exit; |
456 |
|
$response = $this->_call('session_id'); |
457 |
|
//print "response: " . Dumper($response) . "<br/>"; |
458 |
|
//exit; |
459 |
|
|
460 |
|
if ($response) { |
461 |
|
$this->meta[session][id] = $response; |
462 |
|
$this->meta[session][enabled] = 1; |
463 |
|
} else { |
464 |
|
$this->meta[session][error] = 1; |
465 |
|
} |
466 |
|
|
467 |
|
$this->_sleep(); |
468 |
|
//print Dumper($this->meta); |
469 |
|
//exit; |
470 |
|
} |
471 |
|
|
472 |
|
//print Dumper($request); |
473 |
|
|
474 |
|
return 1; |
475 |
|
} |
476 |
|
|
477 |
|
function _hook_response(&$data) { |
478 |
|
//$status[RPCSESSID] = $this->meta[session][RPCSESSID]; |
479 |
|
} |
480 |
|
|
481 |
} |
} |
482 |
|
|
483 |
?> |
?> |