1 |
<? |
2 |
/* |
3 |
## -------------------------------------------------------------------------- |
4 |
## $Id: Remote.php,v 1.4 2003/02/20 21:42:43 joko Exp $ |
5 |
## -------------------------------------------------------------------------- |
6 |
## $Log: Remote.php,v $ |
7 |
## Revision 1.4 2003/02/20 21:42:43 joko |
8 |
## + fix: always converting from utf8 into latin here (for now) |
9 |
## |
10 |
## Revision 1.3 2003/02/13 21:51:29 joko |
11 |
## + now can remember its connection-state |
12 |
## + sub ping |
13 |
## |
14 |
## Revision 1.2 2003/02/13 00:42:44 joko |
15 |
## +- renamed modules |
16 |
## |
17 |
## Revision 1.1 2003/02/09 17:25:38 joko |
18 |
## + refactored from flib/Application/RPC/Remote.php |
19 |
## |
20 |
## Revision 1.6 2003/02/03 03:37:45 jonen |
21 |
## - removed unused argument '$decode' at function '_call()' |
22 |
## + added argument '$options=array()' at function '_call()' |
23 |
## which will be passed to function '&decodeData()' for e.g. utf8 decoding |
24 |
## + added '$encoder->toISO()' at '&decodeData()', (re)moved from 'ProxyObject.php' |
25 |
## |
26 |
## Revision 1.5 2002/12/22 13:24:09 jonen |
27 |
## + added utf8 encoding at 'send()' toggled by option |
28 |
## |
29 |
## Revision 1.4 2002/12/19 02:02:25 jonen |
30 |
## + minor changes |
31 |
## |
32 |
## Revision 1.3 2002/12/19 01:59:37 jonen |
33 |
## + minor changes: coment debug prints |
34 |
## |
35 |
## Revision 1.2 2002/12/05 21:45:31 joko |
36 |
## + debugging |
37 |
## |
38 |
## Revision 1.1 2002/12/01 17:23:58 joko |
39 |
## + initial check-in |
40 |
## |
41 |
## Revision 1.3 2002/12/01 06:22:57 cvsjoko |
42 |
## + minor update: now can use config defaults or given args |
43 |
## |
44 |
## Revision 1.2 2002/10/29 19:14:45 cvsjoko |
45 |
## - bugfix: dont' do utf8-encoding here |
46 |
## |
47 |
## Revision 1.1 2002/10/09 00:51:39 cvsjoko |
48 |
## + new |
49 |
## |
50 |
## |
51 |
## ------------------------------------------------------------------------- |
52 |
*/ |
53 |
|
54 |
|
55 |
/* |
56 |
|
57 |
TODO: |
58 |
o SOAP? |
59 |
|
60 |
|
61 |
*/ |
62 |
|
63 |
|
64 |
// V1: normal require |
65 |
//require_once 'XML/RPC/RPC.php'; |
66 |
|
67 |
// V2: first time load of foreign module (PEAR::XML::RPC) |
68 |
//require_once 'XML/RPC/RPC.php'; |
69 |
loadModule('XML::RPC::RPC'); |
70 |
|
71 |
loadModule('Class::Logger'); |
72 |
|
73 |
class DataSource_Proxy_XMLRPC extends Class_Logger { |
74 |
|
75 |
var $configured; |
76 |
var $meta; |
77 |
|
78 |
function DataSource_Proxy_XMLRPC($args = array()) { |
79 |
|
80 |
parent::constructor(); |
81 |
|
82 |
//print Dumper($this, $args); |
83 |
|
84 |
global $Data_Driver_RPC_Remote_meta; |
85 |
session_register_safe("Data_Driver_RPC_Remote_meta"); |
86 |
$this->meta = $Data_Driver_RPC_Remote_meta; |
87 |
|
88 |
if (!isset($this->meta[connected]) || $args[connect]) { |
89 |
$this->meta[connected] = 1; |
90 |
} |
91 |
$this->_save_meta(); |
92 |
|
93 |
if ($args['Host']) { $this->host = $args['Host']; } |
94 |
if ($args['Port']) { $this->port = $args['Port']; } |
95 |
|
96 |
// check if host is valid |
97 |
if (!$this->host) { |
98 |
$this->_raiseException( "->constructor: attribute 'host' is empty, please check your settings"); |
99 |
return; |
100 |
} |
101 |
|
102 |
if (!$this->port) { |
103 |
$this->_raiseException( "->constructor: attribute 'port' is empty, please check your settings"); |
104 |
return; |
105 |
} |
106 |
|
107 |
$this->configured = 1; |
108 |
|
109 |
} |
110 |
|
111 |
function _save_meta() { |
112 |
global $Data_Driver_RPC_Remote_meta; |
113 |
$Data_Driver_RPC_Remote_meta = $this->meta; |
114 |
} |
115 |
|
116 |
function send($command, $data = "", $options = array()) { |
117 |
|
118 |
$this->log(get_class($this) . "->send: " . $command, PEAR_LOG_DEBUG); |
119 |
|
120 |
if (!$this->isConnected()) { |
121 |
$this->_raiseException( "->send: not connected!"); |
122 |
return; |
123 |
} |
124 |
|
125 |
// do 'encode' here and ... |
126 |
if ($options[utf8]) { |
127 |
$encoder = new Data_Encode($data); |
128 |
$encoder->toUTF8(); |
129 |
} |
130 |
// call '_call' with 'decode' |
131 |
return $this->_call($command, $data, $options); |
132 |
} |
133 |
|
134 |
function _call($command, $data = "", $options = array() ) { |
135 |
|
136 |
if (!$this->configured) { |
137 |
$this->_raiseException( "->_call: class not configured properly"); |
138 |
return; |
139 |
} |
140 |
|
141 |
// populate options list - mostly for debugging purposes |
142 |
$options_list = array(); |
143 |
foreach ($options as $key => $value) { |
144 |
array_push($options_list, "$key=$value"); |
145 |
} |
146 |
|
147 |
$data_debug = $data; |
148 |
if (is_array($data_debug)) { $data_debug = join(", ", $data_debug); } |
149 |
$options_debug = join(", ", $options_list); |
150 |
$this->log(get_class($this) . ": " . $command . "(" . $data_debug . ") [" . $options_debug . "]", PEAR_LOG_DEBUG); |
151 |
|
152 |
// trace |
153 |
//print "call: $command<hr>"; |
154 |
//print Dumper($data); |
155 |
//print Dumper($this); |
156 |
|
157 |
// data |
158 |
$data_enc = XML_RPC_encode($data); |
159 |
|
160 |
// message - request |
161 |
$msg = new XML_RPC_Message($command); |
162 |
$msg->addParam($data_enc); |
163 |
//print htmlentities($msg->serialize()); |
164 |
// remote procedure call |
165 |
$rpc = new XML_RPC_Client("/", $this->host, $this->port); |
166 |
$rpc->setDebug(0); |
167 |
if ( !$msg_response = $rpc->send($msg) ) { |
168 |
// TODO: redirect this error elsewhere! |
169 |
//print "RPC-error!<br>"; |
170 |
$this->_raiseException( "->_call: no response"); |
171 |
return; |
172 |
} |
173 |
// message - response |
174 |
$response_enc = $msg_response->value(); |
175 |
|
176 |
// TODO: what's this? prematurely returning here should not be considered "stable".... |
177 |
return $this->decodeData($response_enc, $options); |
178 |
} |
179 |
|
180 |
function &decodeData(&$payload, $options = array() ) { |
181 |
//if (!is_object($payload)) { return; } |
182 |
if ($payload) { |
183 |
// data |
184 |
$data = XML_RPC_decode($payload); |
185 |
//print "data: " . dumpVar($response); |
186 |
|
187 |
// decode UTF8 to ISO if wanted |
188 |
//if ($options[to_latin]) { |
189 |
$encoder = new Data_Encode($data); |
190 |
$encoder->toISO(); |
191 |
//} |
192 |
|
193 |
$this->meta[connected] = 1; |
194 |
$this->_save_meta(); |
195 |
|
196 |
return $data; |
197 |
} else { |
198 |
//print "ERROR!<br>"; |
199 |
return 0; |
200 |
} |
201 |
} |
202 |
|
203 |
function _raiseException($message) { |
204 |
$this->meta[connected] = 0; |
205 |
$this->_save_meta(); |
206 |
$this->log(get_class($this) . $message, PEAR_LOG_ERR); |
207 |
} |
208 |
|
209 |
function isConnected() { |
210 |
return $this->meta[connected]; |
211 |
} |
212 |
|
213 |
function ping() { |
214 |
$this->_call('ping'); |
215 |
} |
216 |
|
217 |
} |
218 |
|
219 |
?> |