3
* XMPPHP: The PHP XMPP Library
4
* Copyright (C) 2008 Nathanael C. Fritz
5
* This file is part of SleekXMPP.
7
* XMPPHP is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* XMPPHP is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with XMPPHP; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
24
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
25
* @copyright 2008 Nathanael C. Fritz
28
/** XMPPHP_Exception */
29
require_once 'Exception.php';
32
require_once 'XMLObj.php';
35
require_once 'Log.php';
42
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
43
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
44
* @copyright 2008 Nathanael C. Fritz
47
class XMPPHP_XMLStream {
63
protected $xml_depth = 0;
75
protected $stream_start = '<stream>';
79
protected $stream_end = '</stream>';
83
protected $disconnected = false;
87
protected $sent_disconnect = false;
91
protected $ns_map = array();
95
protected $current_ns = array();
99
protected $xmlobj = null;
103
protected $nshandlers = array();
107
protected $idhandlers = array();
111
protected $eventhandlers = array();
115
protected $lastid = 0;
119
protected $default_ns;
123
protected $until = '';
127
protected $until_happened = false;
131
protected $until_payload = array();
139
protected $reconnect = true;
143
protected $been_reset = false;
147
protected $is_server;
151
protected $last_send = 0;
155
protected $use_ssl = false;
160
* @param string $host
161
* @param string $port
162
* @param boolean $printlog
163
* @param string $loglevel
164
* @param boolean $is_server
166
public function __construct($host = null, $port = null, $printlog = false, $loglevel = null, $is_server = false) {
167
$this->reconnect = !$is_server;
168
$this->is_server = $is_server;
171
$this->setupParser();
172
$this->log = new XMPPHP_Log($printlog, $loglevel);
179
public function __destruct() {
180
if(!$this->disconnected && $this->socket) {
186
* Return the log instance
190
public function getLog() {
199
public function getId() {
201
return $this->lastid;
209
public function useSSL($use=true) {
210
$this->use_ssl = $use;
217
* @param string $pointer
220
public function addIdHandler($id, $pointer, $obj = null) {
221
$this->idhandlers[$id] = array($pointer, $obj);
229
* @param string $pointer
231
* @param integer $depth
233
public function addHandler($name, $ns, $pointer, $obj = null, $depth = 1) {
234
$this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
241
* @param string $pointer
244
public function addEventHandler($name, $pointer, $obj) {
245
$this->eventhandlers[] = array($name, $pointer, $obj);
249
* Connect to XMPP Host
251
* @param integer $timeout
252
* @param boolean $persistent
253
* @param boolean $sendinit
255
public function connect($timeout = 30, $persistent = false, $sendinit = true) {
256
$this->disconnected = false;
257
$this->sent_disconnect = false;
259
$conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
261
$conflag = STREAM_CLIENT_CONNECT;
264
if($this->use_ssl) $conntype = 'ssl';
265
$this->log->log("Connecting to $conntype://{$this->host}:{$this->port}");
267
$this->socket = @stream_socket_client("$conntype://{$this->host}:{$this->port}", $errno, $errstr, $timeout, $conflag);
268
} catch (Exception $e) {
269
throw new XMPPHP_Exception($e->getMessage());
272
$this->log->log("Could not connect.", XMPPHP_Log::LEVEL_ERROR);
273
$this->disconnected = true;
275
throw new XMPPHP_Exception('Could not connect.');
277
stream_set_blocking($this->socket, 1);
278
if($sendinit) $this->send($this->stream_start);
282
* Reconnect XMPP Host
284
public function doReconnect() {
285
if(!$this->is_server) {
286
$this->log->log("Reconnecting...", XMPPHP_Log::LEVEL_WARNING);
287
$this->connect(30, false, false);
293
* Disconnect from XMPP Host
295
public function disconnect() {
296
$this->log->log("Disconnecting...", XMPPHP_Log::LEVEL_VERBOSE);
297
$this->reconnect = false;
298
$this->send($this->stream_end);
299
$this->sent_disconnect = true;
300
$this->processUntil('end_stream', 5);
301
$this->disconnected = true;
305
* Are we are disconnected?
309
public function isDisconnected() {
310
return $this->disconnected;
313
private function __process() {
314
$read = array($this->socket);
317
$updated = @stream_select($read, $write, $except, 1);
319
$buff = @fread($this->socket, 1024);
321
if($this->reconnect) {
322
$this->doReconnect();
324
fclose($this->socket);
328
$this->log->log("RECV: $buff", XMPPHP_Log::LEVEL_VERBOSE);
329
xml_parse($this->parser, $buff, false);
338
public function process() {
340
while(!$this->disconnect) {
346
* Process until a timeout occurs
348
* @param integer $timeout
351
public function processTime($timeout = -1) {
354
while(!$this->disconnected and ($timeout == -1 or time() - $start < $timeout)) {
360
* Process until a specified event or a timeout occurs
362
* @param string|array $event
363
* @param integer $timeout
366
public function processUntil($event, $timeout=-1) {
368
if(!is_array($event)) $event = array($event);
369
$this->until[] = $event;
371
$event_key = key($this->until);
374
while(!$this->disconnected and $this->until[$event_key] and (time() - $start < $timeout or $timeout == -1)) {
377
if(array_key_exists($event_key, $this->until_payload)) {
378
$payload = $this->until_payload[$event_key];
382
unset($this->until_payload[$event_key]);
389
public function Xapply_socket($socket) {
390
$this->socket = $socket;
396
* @see xml_set_element_handler
398
* @param resource $parser
399
* @param string $name
401
public function startXML($parser, $name, $attr) {
402
if($this->been_reset) {
403
$this->been_reset = false;
404
$this->xml_depth = 0;
407
if(array_key_exists('XMLNS', $attr)) {
408
$this->current_ns[$this->xml_depth] = $attr['XMLNS'];
410
$this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1];
411
if(!$this->current_ns[$this->xml_depth]) $this->current_ns[$this->xml_depth] = $this->default_ns;
413
$ns = $this->current_ns[$this->xml_depth];
414
foreach($attr as $key => $value) {
415
if(strstr($key, ":")) {
416
$key = explode(':', $key);
418
$this->ns_map[$key] = $value;
421
if(!strstr($name, ":") === false)
423
$name = explode(':', $name);
424
$ns = $this->ns_map[$name[0]];
427
$obj = new XMPPHP_XMLObj($name, $ns, $attr);
428
if($this->xml_depth > 1) {
429
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
431
$this->xmlobj[$this->xml_depth] = $obj;
437
* @see xml_set_element_handler
439
* @param resource $parser
440
* @param string $name
442
public function endXML($parser, $name) {
443
#$this->log->log("Ending $name", XMPPHP_Log::LEVEL_DEBUG);
445
if($this->been_reset) {
446
$this->been_reset = false;
447
$this->xml_depth = 0;
450
if($this->xml_depth == 1) {
451
#clean-up old objects
453
foreach($this->nshandlers as $handler) {
454
if($handler[4] != 1 and $this->xmlobj[2]->hasSub($handler[0])) {
455
$searchxml = $this->xmlobj[2]->sub($handler[0]);
456
} elseif(is_array($this->xmlobj) and array_key_exists(2, $this->xmlobj)) {
457
$searchxml = $this->xmlobj[2];
459
if($searchxml !== null and $searchxml->name == $handler[0] and ($searchxml->ns == $handler[1] or (!$handler[1] and $searchxml->ns == $this->default_ns))) {
460
if($handler[3] === null) $handler[3] = $this;
461
$this->log->log("Calling {$handler[2]}", XMPPHP_Log::LEVEL_DEBUG);
462
$handler[3]->$handler[2]($this->xmlobj[2]);
465
foreach($this->idhandlers as $id => $handler) {
466
if(array_key_exists('id', $this->xmlobj[2]->attrs) and $this->xmlobj[2]->attrs['id'] == $id) {
467
if($handler[1] === null) $handler[1] = $this;
468
$handler[1]->$handler[0]($this->xmlobj[2]);
469
#id handlers are only used once
470
unset($this->idhandlers[$id]);
474
if(is_array($this->xmlobj)) {
475
$this->xmlobj = array_slice($this->xmlobj, 0, 1);
476
if(isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMPPHP_XMLObj) {
477
$this->xmlobj[0]->subs = null;
480
unset($this->xmlobj[2]);
482
if($this->xml_depth == 0 and !$this->been_reset) {
483
if(!$this->disconnected) {
484
if(!$this->sent_disconnect) {
485
$this->send($this->stream_end);
487
$this->disconnected = true;
488
$this->sent_disconnect = true;
489
fclose($this->socket);
490
if($this->reconnect) {
491
$this->doReconnect();
494
$this->event('end_stream');
499
* XML character callback
500
* @see xml_set_character_data_handler
502
* @param resource $parser
503
* @param string $data
505
public function charXML($parser, $data) {
506
if(array_key_exists($this->xml_depth, $this->xmlobj)) {
507
$this->xmlobj[$this->xml_depth]->data .= $data;
514
* @param string $name
515
* @param string $payload
517
public function event($name, $payload = null) {
518
$this->log->log("EVENT: $name", XMPPHP_Log::LEVEL_DEBUG);
519
foreach($this->eventhandlers as $handler) {
520
if($name == $handler[0]) {
521
if($handler[2] === null) {
524
$handler[2]->$handler[1]($payload);
527
foreach($this->until as $key => $until) {
528
if(is_array($until)) {
529
if(in_array($name, $until)) {
530
$this->until_payload[$key][] = array($name, $payload);
531
$this->until[$key] = false;
540
public function read() {
541
$buff = @fread($this->socket, 1024);
543
if($this->reconnect) {
544
$this->doReconnect();
546
fclose($this->socket);
550
$this->log->log("RECV: $buff", XMPPHP_Log::LEVEL_VERBOSE);
551
xml_parse($this->parser, $buff, false);
559
public function send($msg, $rec=false) {
560
if($this->time() - $this->last_send < .1) {
566
$write = array($this->socket);
568
$select = @stream_select($read, $write, $except, 0, 0);
569
if($select === False) {
570
$this->doReconnect();
572
} elseif ($select > 0) {
576
//$this->processTime(.25);
579
$sentbytes = @fwrite($this->socket, $msg, 1024);
580
$this->last_send = $this->time();
581
$this->log->log("SENT: " . mb_substr($msg, 0, $sentbytes, '8bit'), XMPPHP_Log::LEVEL_VERBOSE);
582
if($sentbytes === FALSE) {
583
$this->doReconnect();
584
} elseif ($sentbytes != mb_strlen($msg, '8bit')) {
585
$this->send(mb_substr($msg, $sentbytes, mb_strlen($msg, '8bit'), '8bit'), true);
589
public function time() {
590
list($usec, $sec) = explode(" ", microtime());
591
return (float)$sec + (float)$usec;
597
public function reset() {
598
$this->xml_depth = 0;
599
unset($this->xmlobj);
600
$this->xmlobj = array();
601
$this->setupParser();
602
if(!$this->is_server) {
603
$this->send($this->stream_start);
605
$this->been_reset = true;
609
* Setup the XML parser
611
public function setupParser() {
612
$this->parser = xml_parser_create('UTF-8');
613
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
614
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
615
xml_set_object($this->parser, $this);
616
xml_set_element_handler($this->parser, 'startXML', 'endXML');
617
xml_set_character_data_handler($this->parser, 'charXML');