1 <?php
2
3
4 /**
5 * xmlBlaster.inc
6 *
7 * Client Functions for xmlBlaster Server - www.xmlBlaster.org
8 * Contains functions for accessing the server via XMLRPC
9 *
10 * TODO :
11 * - replace durty (but so fast ;o) regular expression by a real xml parsing !
12 *
13 * 08/07/02 21:36 cyrille@ktaland.com
14 * Fixed dirty reg exp for new sessionId layout. PB
15 * 28/02/03 10:58 bencom@bencom.co.nz
16 */
17
18 // php xml-rpc implemetation by Edd Dumbill (C) 1999-2001 <edd@usefulinc.com>
19 include ("xmlrpc.inc");
20
21 // =================
22 // 08/07/02 21:36 cyrille@ktaland.com
23 //
24 class xmlBlaster {
25
26 var $server;
27 var $port;
28 var $user;
29 var $password;
30
31 var $securityService_type = "htpasswd";
32 var $securityService_version = "1.0";
33
34 var $client;
35 var $connectQos;
36 var $isConnected;
37
38 // =================
39 function xmlBlaster($server, $port, $user, $password) {
40
41 dbgprint("xmlBlaster::xmlBlaster()");
42
43 $this->server = $server;
44 $this->port = ($port * 1);
45 $this->user = $user;
46 $this->password = $password;
47
48 dbgprint("xmlBlaster::xmlBlaster() $this->server::$this->port $this->user:$this->password");
49
50 $this->connectQos = null;
51 $this->isConnected = false;
52
53 } //xmlBlaster
54
55 // =================
56 function isConnected() {
57
58 return $this->isConnected;
59
60 } //isConnected
61
62 // =================
63 function sessionId() {
64
65 //dbgprint("xmlBlaster::sessionId()");
66
67 if (isset ($this->connectQos)) {
68 dbgprint("xmlBlaster::sessionId() sessionId = " . $this->connectQos->sessionId());
69 return $this->connectQos->sessionId();
70 }
71 return null;
72
73 } //sessionId
74
75 // =================
76 function connect() {
77
78 dbgprint("xmlBlaster::connect()");
79
80 if (!isset ($this->server)) {
81 return array (
82 'ERROR',
83 "To connect, you have to fill fields <server> <port> <user> <password>"
84 );
85 }
86
87 $connect_qos_xml = "<qos>\n" .
88 "<securityService type=\"" . $this->securityService_type . "\" version=\"" . $this->securityService_version . "\">\n" .
89 " <user>" . $this->user . "</user>\n" .
90 " <passwd>" . $this->password . "</passwd>\n" .
91 "</securityService>\n" .
92 "</qos>\n";
93
94 $this->client = new xmlrpc_client('/', $this->server, $this->port);
95
96 /*if( ! isset( $this->client ) || $this->client==null ){
97 return array( 'ERROR', "Failed to connect, check you <server> and <port> parameters." );
98 }*/
99
100 $rpcmsg = new xmlrpcmsg("authenticate.connect", array (
101 new xmlrpcval($connect_qos_xml, "string")
102 ));
103 $result = $this->client->send($rpcmsg); // class xmlrpcresp, see http://phpxmlrpc.sourceforge.net/doc-2/
104
105 //if( ! isset( $result ) ){
106 // return array( 'ERROR', "Failed to connect, check you <server> and <port> parameters." );
107 //}
108
109 if ($result->faultCode()) {
110 // a xml-rpc error occured
111 //$res = xmlrpc_decode($result->faultString());
112 //dbgprint("ERROR xmlBlaster::connect() xml-rpc ERROR = $res");
113 dbgprint("ERROR xmlBlaster::connect() xml-rpc ERROR = " . ($result->faultString()));
114 //dbgprint("ERROR xmlBlaster::connect() xml-rpc ERROR = " . ($result->serialize()));
115 return array (
116 'ERROR',
117 $result->faultString() // e.g. XmlBlasterException from server
118 );
119 }
120
121 // $result contains the xmlrpc markup response
122 // $res contains the ConnectReturnQos xml: "<qos>...</qos>"
123 $res = php_xmlrpc_decode($result->value());
124 $res_type = gettype($res);
125
126 if (gettype($res) == 'array' && isset ($res['faultString'])) {
127 // a xmlBlaster error occured
128 dbgprint("ERROR xmlBlaster::connect() xmlBlaster ERROR = [" . $res['faultString'] . "]");
129 return array (
130 'ERROR',
131 $res['faultString']
132 );
133 }
134
135 //dbgprint("OK xmlBlaster::connect() response= [$res]");
136
137 $this->connectQos = new ConnectQos($res);
138 $this->isConnected = true;
139
140 return array (
141 'OK',
142 $this->connectQos
143 );
144
145 } //connect
146
147 // =================
148 function logout() {
149
150 dbgprint("xmlBlaster::logout()");
151
152 if (!isset ($this->client)) {
153 return;
154 }
155
156 // $self->{'server'}->call( 'authenticate.logout', $self->{'sessionId'} );
157 $rpcmsg = new xmlrpcmsg("authenticate.logout", array (
158 new xmlrpcval($this->sessionId(), "string")
159 ));
160 $result = $this->client->send($rpcmsg);
161
162 } //logout
163
164 // =================
165 function get($key, $qos='<qos></qos>' ) {
166
167 dbgprint("xmlBlaster::get(" . $key . ")");
168
169 //my $messages = $self->{'server'}->call( 'xmlBlaster.get', $self->{'sessionId'}, $key ,$qos );
170 $rpcmsg = new xmlrpcmsg("xmlBlaster.get", array (
171 new xmlrpcval($this->sessionId(), "string"),
172 new xmlrpcval($key, "string"),
173 new xmlrpcval($qos, "string"),
174
175
176 ));
177 $result = $this->client->send($rpcmsg);
178
179 // check if xml-rpc error
180
181 if ($result->faultCode()) {
182 // a xml-rpc error occured
183 $res = xmlrpc_decode($result->faultString());
184 dbgprint("ERROR xmlBlaster::get() xml-rpc ERROR = $res");
185 return array (
186 'ERROR',
187 $result->faultString()
188 );
189 }
190
191 $res_array = php_xmlrpc_decode($result->value()); // fails for content: $result->serialize()
192
193 // check if xmlBlaster error
194 if (gettype($res_array) == 'array' && isset ($res_array['faultString'])) {
195 dbgprint("ERROR xmlBlaster::get() xmlBlaster ERROR = [" . $res_array['faultString'] . "]");
196 return array (
197 'ERROR',
198 $res_array['faultString']
199 );
200 }
201
202 // copy raw results in a array of messageUnit
203
204 $messages = array (
205 count($res_array)
206 );
207
208 $cpt = 0;
209 foreach ($res_array as $r) {
210 //dbgprint( "OK xmlBlaster::get() response key= [$r[0]]" );
211 //dbgprint( "OK xmlBlaster::get() response cont = " . (string)$r[1] );
212 //dbgprint( "OK xmlBlaster::get() response qos = [$r[2]]" );
213 $messages[$cpt++] = new messageUnit($r[0], $r[1], $r[2]);
214 }
215
216 return array (
217 'OK',
218 $messages
219 );
220
221 } //get
222
223 /**
224 * Publish a message.
225 * @param string $key PublishKey
226 * @param string $content byte array
227 * @param string $qos PublishQos
228 * @return array 0: 'OK' or 'ERROR'', 1: class PublishReturnQos or errorString
229 */
230 function publish($key, $content, $qos) {
231
232 dbgprint("xmlBlaster::publish(" . $key . ")");
233
234 $rpcmsg = new xmlrpcmsg("xmlBlaster.publish", array (
235 new xmlrpcval($this->sessionId(), "string"),
236 new xmlrpcval($key, "string"),
237 new xmlrpcval($content, "base64"),
238 new xmlrpcval($qos, "string"),
239
240
241 ));
242 $result = $this->client->send($rpcmsg);
243
244 // check if xml-rpc error
245
246 if ($result->faultCode()) {
247 // a xml-rpc error occured
248 $res = xmlrpc_decode($result->faultString());
249 dbgprint("ERROR xmlBlaster::publish() xml-rpc ERROR = $res");
250 return array (
251 'ERROR',
252 $result->faultString()
253 );
254 }
255
256 //dbgprint( "publishResponse=" . $result->serialize() );
257
258 $res = php_xmlrpc_decode($result->value());
259
260 //dbgprint("publishResponse=" . $res);
261
262 // check if xmlBlaster error
263
264 if (gettype($res) == 'array' && isset ($res['faultString'])) {
265 dbgprint("ERROR xmlBlaster::publish() xmlBlaster ERROR = [" . $res['faultString'] . "]");
266 return array (
267 'ERROR',
268 $res['faultString']
269 );
270 }
271
272 return array (
273 'OK',
274 new PublishReturnQos($res)
275 );
276
277 } //publish
278
279 } //class xmlBlaster
280
281 class messageUnit {
282
283 var $keyOid;
284 var $content;
285 var $qos;
286
287 function messageUnit($key, $content, $qos) {
288
289 $this->keyOid = $key;
290 $this->content = $content;
291 $this->qos = $qos;
292
293 } //messageUnit
294
295 function content() {
296
297 return $this->content;
298
299 } //content
300
301 function keyOid() {
302
303 return $this->keyOid;
304
305 } //keyOid
306
307 function qos() {
308
309 return $this->qos;
310
311 } //qos
312
313 } //messageUnit
314
315 // =================
316 // 08/07/02 23:25 cyrille@ktaland.com
317 //
318 class ConnectQos {
319
320 /*
321 <qos>
322 <securityService type="htpasswd" version="1.0">
323 <user>joe</user>
324 <passwd>secret</passwd>
325 </securityService>
326 <instanceId>/xmlBlaster/node/xmlBlaster_192_168_1_25_3412/instanceId/1216309365453</instanceId>
327 <session name='/node/xmlBlaster_192_168_1_25_3412/client/joe/-46' timeout='86400000' maxSessions='10' clearSessions='false'
328 reconnectSameClientOnly='false' sessionId='sessionId:192.168.1.25-null-1216311403568--785272225-45'/>
329 <queue relating='connection' maxEntries='10000000' maxEntriesCache='1000'>
330 <address type='SOCKET' dispatchPlugin='undef'></address>
331 </queue>
332 <queue relating='subject'/>
333 <queue relating='callback' maxEntries='1000' maxEntriesCache='1000' maxBytes='4147483647' maxBytesCache='200555666'/>
334 <clientProperty name='__rcvTimestampStr'>2008-07-17 16:16:43.573Z</clientProperty>
335 </qos>
336 */
337 var $sessionId;
338
339 var $xml;
340
341 // =================
342 function ConnectQos($xml) {
343
344 dbgprint("ConnectQos::ConnectQos()");
345
346 $this->xml = $xml;
347 $this->sessionId = null;
348
349 } //ConnectQos
350
351 // =================
352 function sessionId() {
353
354 //dbgprint("ConnectQos::sessionId()");
355
356 if (!isset ($this->sessionId))
357 if (preg_match("/sessionId=\'(.*)\'\/>/", $this->xml, $matches)) {
358 $this->sessionId = $matches[1];
359 }
360 return $this->sessionId;
361
362 } //sessionId
363
364 } //class ConnectQos
365
366 /**
367 * @see http://www.xmlblaster.org/xmlBlaster/doc/client-api/org/xmlBlaster/client/qos/PublishReturnQos.html
368 */
369 class PublishReturnQos {
370 /*
371 <qos>
372 <state id='OK' info='QUEUED[bilbo]'/> <!-- optionally, defaults to 'OK' -->
373 <key oid='HelloWorld'/>
374 <rcvTimestamp nanos='1007764305862000002'/>
375 <!-- UTC time when message was created in xmlBlaster server with a publish() call, in nanoseconds since 1970 -->
376 <isPublish/>
377 </qos>
378 */
379 var $xml;
380 var $state;
381 function PublishReturnQos($xml) {
382
383 dbgprint("PublishReturnQos::PublishReturnQos()");
384
385 $this->xml = $xml;
386 $this->state = 'OK';
387
388 } //PublishReturnQos
389 function state() {
390 if (preg_match("/state id=\'(.*)\'\/>/", $this->xml, $matches)) {
391 $this->state = $matches[1];
392 }
393 return $this->state;
394
395 } //sessionId
396 } // class PublishReturnQos
397 ?>
syntax highlighted by Code2HTML, v. 0.9.1