2008-03-29 18:45:29 +00:00
|
|
|
<?php
|
2008-04-02 08:23:24 +01:00
|
|
|
/*
|
|
|
|
XMPPHP: The PHP XMPP Library
|
|
|
|
Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
XMPPHP is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
XMPPHP is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with XMPPHP; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-04-02 05:50:27 +01:00
|
|
|
require_once("xmlobj.php");
|
2008-04-02 08:09:28 +01:00
|
|
|
require_once("logging.php");
|
2008-03-30 10:06:46 +01:00
|
|
|
|
2008-03-29 18:45:29 +00:00
|
|
|
class XMLStream {
|
|
|
|
var $socket;
|
|
|
|
var $parser;
|
|
|
|
var $buffer;
|
2008-03-30 10:06:46 +01:00
|
|
|
var $xml_depth = 0;
|
2008-03-29 18:45:29 +00:00
|
|
|
var $host;
|
|
|
|
var $port;
|
|
|
|
var $stream_start = '<stream>';
|
2008-04-02 08:09:28 +01:00
|
|
|
var $stream_end = '</stream';
|
|
|
|
var $disconnected = false;
|
2008-04-02 21:12:15 +01:00
|
|
|
var $sent_disconnect = False;
|
2008-03-30 10:06:46 +01:00
|
|
|
var $ns_map = array();
|
2008-04-02 05:50:27 +01:00
|
|
|
var $current_ns = array();
|
2008-03-30 10:06:46 +01:00
|
|
|
var $xmlobj = Null;
|
|
|
|
var $nshandlers = array();
|
|
|
|
var $idhandlers = array();
|
2008-04-02 05:50:27 +01:00
|
|
|
var $eventhandlers = array();
|
2008-03-30 10:06:46 +01:00
|
|
|
var $lastid = 0;
|
2008-04-02 05:50:27 +01:00
|
|
|
var $default_ns;
|
2008-04-04 08:19:17 +01:00
|
|
|
var $until = '';
|
2008-04-02 05:50:27 +01:00
|
|
|
var $until_happened = False;
|
|
|
|
var $until_payload = array();
|
2008-04-02 08:09:28 +01:00
|
|
|
var $log;
|
|
|
|
var $reconnect = True;
|
|
|
|
var $been_reset = False;
|
2008-04-04 08:19:17 +01:00
|
|
|
var $is_server;
|
2008-03-29 18:45:29 +00:00
|
|
|
|
2008-04-04 08:19:17 +01:00
|
|
|
function XMLStream($host=Null, $port=Null, $log=False, $loglevel=Null, $is_server=False) {
|
|
|
|
$this->reconnect = !$is_server;
|
|
|
|
$this->is_server = $is_server;
|
2008-03-29 18:45:29 +00:00
|
|
|
$this->host = $host;
|
|
|
|
$this->port = $port;
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->setupParser();
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->log = new Logging($log, $loglevel);
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getId() {
|
|
|
|
$this->lastid++;
|
|
|
|
return $this->lastid;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addIdHandler($id, $pointer, $obj=Null) {
|
|
|
|
$this->idhandlers[$id] = array($pointer, $obj);
|
|
|
|
}
|
|
|
|
|
2008-04-04 08:19:17 +01:00
|
|
|
function addHandler($name, $ns, $pointer, $obj=Null, $depth=1) {
|
|
|
|
$this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 05:50:27 +01:00
|
|
|
function addEventHandler($name, $pointer, $obj) {
|
|
|
|
$this->eventhanders[] = array($name, $pointer, $obj);
|
|
|
|
}
|
|
|
|
|
2008-04-02 08:09:28 +01:00
|
|
|
function connect($persistent=False, $sendinit=True) {
|
|
|
|
$this->disconnected = False;
|
2008-04-02 21:12:15 +01:00
|
|
|
$this->sent_disconnect = False;
|
2008-04-02 05:50:27 +01:00
|
|
|
if($persistent) {
|
2008-04-02 21:19:55 +01:00
|
|
|
$conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
|
2008-04-02 05:50:27 +01:00
|
|
|
} else {
|
|
|
|
$conflag = STREAM_CLIENT_CONNECT;
|
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->log->log("Connecting to tcp://{$this->host}:{$this->port}");
|
2008-04-02 05:50:27 +01:00
|
|
|
$this->socket = stream_socket_client("tcp://{$this->host}:{$this->port}", $flags=$conflag);
|
2008-04-02 21:19:55 +01:00
|
|
|
stream_set_blocking($this->socket, 1);
|
2008-04-02 08:09:28 +01:00
|
|
|
if($sendinit) $this->send($this->stream_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
function apply_socket($socket) {
|
|
|
|
$this->socket = $socket;
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function process() {
|
2008-04-02 08:09:28 +01:00
|
|
|
$updated = '';
|
2008-03-29 18:45:29 +00:00
|
|
|
while(!$this->disconnect) {
|
2008-04-02 08:09:28 +01:00
|
|
|
$read = array($this->socket);
|
|
|
|
$write = NULL;
|
|
|
|
$except = NULL;
|
|
|
|
$updated = stream_select($read, $write, $except, 1);
|
|
|
|
if ($updated > 0) {
|
2008-04-02 21:19:55 +01:00
|
|
|
$buff = @fread($this->socket, 1024);
|
2008-04-02 20:48:52 +01:00
|
|
|
if(!$buff) {
|
|
|
|
if($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
} else {
|
|
|
|
fclose($this->socket);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
|
|
|
xml_parse($this->parser, $buff, False);
|
|
|
|
}
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-04 08:19:17 +01:00
|
|
|
function read() {
|
|
|
|
$buff = @fread($this->socket, 1024);
|
|
|
|
if(!$buff) {
|
|
|
|
if($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
} else {
|
|
|
|
fclose($this->socket);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
|
|
|
xml_parse($this->parser, $buff, False);
|
|
|
|
}
|
|
|
|
|
2008-04-02 01:48:31 +01:00
|
|
|
function processTime($timeout=-1) {
|
|
|
|
$start = time();
|
2008-04-02 05:50:27 +01:00
|
|
|
$updated = '';
|
2008-04-02 08:09:28 +01:00
|
|
|
while(!$this->disconnected and ($timeout == -1 or time() - $start < $timeout)) {
|
2008-04-02 01:48:31 +01:00
|
|
|
$read = array($this->socket);
|
|
|
|
$write = NULL;
|
|
|
|
$except = NULL;
|
2008-04-02 08:09:28 +01:00
|
|
|
$updated = stream_select($read, $write, $except, 1);
|
2008-04-02 01:48:31 +01:00
|
|
|
if ($updated > 0) {
|
2008-04-02 21:19:55 +01:00
|
|
|
$buff = @fread($this->socket, 1024);
|
2008-04-02 20:48:52 +01:00
|
|
|
if(!$buff) {
|
|
|
|
if($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
} else {
|
|
|
|
fclose($this->socket);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
2008-04-02 01:48:31 +01:00
|
|
|
xml_parse($this->parser, $buff, False);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-02 05:50:27 +01:00
|
|
|
function processUntil($event, $timeout=-1) {
|
|
|
|
$start = time();
|
|
|
|
if(!is_array($event)) $event = array($event);
|
2008-04-04 08:19:17 +01:00
|
|
|
$this->until[] = $event;
|
|
|
|
end($this->until);
|
|
|
|
$event_key = key($this->until);
|
|
|
|
reset($this->until);
|
2008-04-02 05:50:27 +01:00
|
|
|
$updated = '';
|
2008-04-04 08:19:17 +01:00
|
|
|
while(!$this->disconnected and $this->until[$event_key] and (time() - $start < $timeout or $timeout == -1)) {
|
2008-04-02 05:50:27 +01:00
|
|
|
$read = array($this->socket);
|
|
|
|
$write = NULL;
|
|
|
|
$except = NULL;
|
|
|
|
$updated = stream_select($read, $write, $except, 1);
|
|
|
|
if ($updated > 0) {
|
2008-04-02 21:19:55 +01:00
|
|
|
$buff = @fread($this->socket, 1024);
|
2008-04-02 20:48:52 +01:00
|
|
|
if(!$buff) {
|
|
|
|
if($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
} else {
|
|
|
|
fclose($this->socket);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
2008-04-02 05:50:27 +01:00
|
|
|
xml_parse($this->parser, $buff, False);
|
|
|
|
}
|
|
|
|
}
|
2008-04-04 08:19:17 +01:00
|
|
|
$payload = $this->until_payload[$event_key];
|
|
|
|
unset($this->until_payload[$event_key]);
|
2008-04-02 05:50:27 +01:00
|
|
|
return $payload;
|
2008-04-02 01:48:31 +01:00
|
|
|
}
|
|
|
|
|
2008-03-29 18:45:29 +00:00
|
|
|
function startXML($parser, $name, $attr) {
|
2008-04-02 08:09:28 +01:00
|
|
|
if($this->been_reset) {
|
|
|
|
$this->been_reset = False;
|
|
|
|
$this->xml_depth = 0;
|
|
|
|
}
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->xml_depth++;
|
|
|
|
if(array_key_exists('XMLNS', $attr)) {
|
2008-04-02 05:50:27 +01:00
|
|
|
$this->current_ns[$this->xml_depth] = $attr['XMLNS'];
|
|
|
|
} else {
|
|
|
|
$this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1];
|
|
|
|
if(!$this->current_ns[$this->xml_depth]) $this->current_ns[$this->xml_depth] = $this->default_ns;
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
2008-04-02 05:50:27 +01:00
|
|
|
$ns = $this->current_ns[$this->xml_depth];
|
2008-03-30 10:06:46 +01:00
|
|
|
foreach($attr as $key => $value) {
|
|
|
|
if(strstr($key, ":")) {
|
|
|
|
$key = explode(':', $key);
|
|
|
|
$key = $key[1];
|
|
|
|
$this->ns_map[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!strstr($name, ":") === False)
|
|
|
|
{
|
|
|
|
$name = explode(':', $name);
|
|
|
|
$ns = $this->ns_map[$name[0]];
|
|
|
|
$name = $name[1];
|
|
|
|
}
|
|
|
|
$obj = new XMLObj($name, $ns, $attr);
|
|
|
|
if($this->xml_depth > 1)
|
|
|
|
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
|
|
|
|
$this->xmlobj[$this->xml_depth] = $obj;
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function endXML($parser, $name) {
|
2008-04-04 18:24:44 +01:00
|
|
|
#$this->log->log("Ending $name", LOGGING_DEBUG);
|
|
|
|
#print "$name\n";
|
2008-04-02 08:09:28 +01:00
|
|
|
if($this->been_reset) {
|
|
|
|
$this->been_reset = False;
|
|
|
|
$this->xml_depth = 0;
|
|
|
|
}
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->xml_depth--;
|
|
|
|
if($this->xml_depth == 1) {
|
|
|
|
#clean-up old objects
|
|
|
|
$found = False;
|
|
|
|
foreach($this->nshandlers as $handler) {
|
2008-04-04 08:19:17 +01:00
|
|
|
if($handler[4] != 1 and $this->xmlobj[2]->hassub($handler[0])) {
|
|
|
|
$searchxml = $this->xmlobj[2]->sub($handler[0]);
|
|
|
|
} else {
|
|
|
|
$searchxml = $this->xmlobj[2];
|
|
|
|
}
|
|
|
|
if($searchxml->name == $handler[0] and ($searchxml->ns == $handler[1] or (!$handler[1] and $searchxml->ns == $this->default_ns))) {
|
2008-03-30 10:06:46 +01:00
|
|
|
if($handler[3] === Null) $handler[3] = $this;
|
2008-04-04 18:24:44 +01:00
|
|
|
$this->log->log("Calling {$handler[2]}", LOGGING_DEBUG);
|
2008-03-30 10:06:46 +01:00
|
|
|
call_user_method($handler[2], $handler[3], $this->xmlobj[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach($this->idhandlers as $id => $handler) {
|
|
|
|
if($this->xmlobj[2]->attrs['id'] == $id) {
|
|
|
|
if($handler[1] === Null) $handler[1] = $this;
|
|
|
|
call_user_method($handler[0], $handler[1], $this->xmlobj[2]);
|
|
|
|
#id handlers are only used once
|
|
|
|
unset($this->idhandlers[$id]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_array($this->xmlobj)) {
|
|
|
|
$this->xmlobj = array_slice($this->xmlobj, 0, 1);
|
|
|
|
$this->xmlobj[0]->subs = Null;
|
|
|
|
}
|
2008-04-04 08:19:17 +01:00
|
|
|
unset($this->xmlobj[2]);
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
if($this->xml_depth == 0 and !$this->been_reset) {
|
|
|
|
if(!$this->disconnected) {
|
2008-04-02 21:12:15 +01:00
|
|
|
if(!$this->sent_disconnect) {
|
|
|
|
$this->send($this->stream_end);
|
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->disconnected = True;
|
2008-04-02 21:12:15 +01:00
|
|
|
$this->sent_disconnect = True;
|
2008-04-02 08:09:28 +01:00
|
|
|
fclose($this->socket);
|
|
|
|
if($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->event('end_stream');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function doReconnect() {
|
2008-04-04 08:19:17 +01:00
|
|
|
if(!$this->is_server) {
|
|
|
|
$this->log->log("Reconnecting...", LOGGING_WARNING);
|
|
|
|
$this->connect(False, False);
|
|
|
|
$this->reset();
|
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function disconnect() {
|
|
|
|
$this->reconnect = False;
|
|
|
|
$this->send($this->stream_end);
|
2008-04-02 21:12:15 +01:00
|
|
|
$this->sent_disconnect = True;
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->processUntil('end_stream', 5);
|
|
|
|
$this->disconnected = True;
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
2008-04-02 05:50:27 +01:00
|
|
|
|
|
|
|
function event($name, $payload=Null) {
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->log->log("EVENT: $name", LOGGING_DEBUG);
|
2008-04-02 05:50:27 +01:00
|
|
|
foreach($this->eventhandlers as $handler) {
|
|
|
|
if($name == $handler[0]) {
|
|
|
|
if($handler[2] === Null) $handler[2] = $this;
|
|
|
|
call_user_method($handler[1], $handler[2], $payload);
|
|
|
|
}
|
|
|
|
}
|
2008-04-04 08:19:17 +01:00
|
|
|
foreach($this->until as $key => $until) {
|
|
|
|
if(is_array($until)) {
|
|
|
|
if(in_array($name, $until)) {
|
|
|
|
$this->until_payload[$key][] = array($name, $payload);
|
|
|
|
$this->until[$key] = False;
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 05:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-29 18:45:29 +00:00
|
|
|
function charXML($parser, $data) {
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->xmlobj[$this->xml_depth]->data .= $data;
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function send($msg) {
|
2008-03-30 10:55:59 +01:00
|
|
|
#socket_write($this->socket, $msg);
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->log->log("SENT: $msg", LOGGING_VERBOSE);
|
2008-03-30 10:55:59 +01:00
|
|
|
fwrite($this->socket, $msg);
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
$this->xml_depth = 0;
|
2008-04-04 08:19:17 +01:00
|
|
|
unset($this->xmlobj);
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->setupParser();
|
2008-04-04 08:19:17 +01:00
|
|
|
if(!$this->is_server) {
|
|
|
|
$this->send($this->stream_start);
|
|
|
|
}
|
2008-04-02 08:09:28 +01:00
|
|
|
$this->been_reset = True;
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupParser() {
|
2008-04-04 08:19:17 +01:00
|
|
|
$this->parser = xml_parser_create('UTF-8');
|
|
|
|
xml_parser_set_option($this->parser,XML_OPTION_SKIP_WHITE,1);
|
|
|
|
xml_parser_set_option($this->parser,XML_OPTION_TARGET_ENCODING, "UTF-8");
|
2008-03-30 10:06:46 +01:00
|
|
|
xml_set_object($this->parser, $this);
|
|
|
|
xml_set_element_handler($this->parser, 'startXML', 'endXML');
|
|
|
|
xml_set_character_data_handler($this->parser, 'charXML');
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|