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