git-svn-id: svn://netflint.net/xmpphp@14 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
fritzy 2008-04-04 07:19:17 +00:00
parent 8478473813
commit aba0b3f7e7
2 changed files with 72 additions and 22 deletions

View File

@ -45,6 +45,23 @@ class XMLObj {
} }
} }
function tostring($str='') {
$str .= "<{$this->name} xmlns='{$this->ns}' ";
foreach($this->attrs as $key => $value) {
if($key != 'xmlns') {
$value = htmlentities($value);
$str .= "$key='$value' ";
}
}
$str .= ">";
foreach($this->subs as $sub) {
$str .= $sub->tostring();
}
$body = htmlentities($this->data);
$str .= "$body</{$this->name}>";
return $str;
}
function hassub($name) { function hassub($name) {
foreach($this->subs as $sub) { foreach($this->subs as $sub) {
if($sub->name == $name) return True; if($sub->name == $name) return True;

View File

@ -40,21 +40,20 @@ class XMLStream {
var $eventhandlers = array(); var $eventhandlers = array();
var $lastid = 0; var $lastid = 0;
var $default_ns; var $default_ns;
var $until; var $until = '';
var $until_happened = False; var $until_happened = False;
var $until_payload = array(); var $until_payload = array();
var $log; var $log;
var $reconnect = True; var $reconnect = True;
var $been_reset = False; var $been_reset = False;
var $is_server;
function XMLStream($host, $port, $log=False, $loglevel=Null) { function XMLStream($host=Null, $port=Null, $log=False, $loglevel=Null, $is_server=False) {
#$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $this->reconnect = !$is_server;
$this->reconnect = True; $this->is_server = $is_server;
$this->host = $host; $this->host = $host;
$this->port = $port; $this->port = $port;
#set up the parser
$this->setupParser(); $this->setupParser();
#set up logger
$this->log = new Logging($log, $loglevel); $this->log = new Logging($log, $loglevel);
} }
@ -67,8 +66,8 @@ class XMLStream {
$this->idhandlers[$id] = array($pointer, $obj); $this->idhandlers[$id] = array($pointer, $obj);
} }
function addHandler($name, $ns, $pointer, $obj=Null) { function addHandler($name, $ns, $pointer, $obj=Null, $depth=1) {
$this->nshandlers[] = array($name,$ns,$pointer,$obj); $this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
} }
function addEventHandler($name, $pointer, $obj) { function addEventHandler($name, $pointer, $obj) {
@ -116,6 +115,20 @@ class XMLStream {
} }
} }
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);
}
function processTime($timeout=-1) { function processTime($timeout=-1) {
$start = time(); $start = time();
$updated = ''; $updated = '';
@ -143,10 +156,12 @@ class XMLStream {
function processUntil($event, $timeout=-1) { function processUntil($event, $timeout=-1) {
$start = time(); $start = time();
if(!is_array($event)) $event = array($event); if(!is_array($event)) $event = array($event);
$this->until = $event; $this->until[] = $event;
$this->until_happened = False; end($this->until);
$event_key = key($this->until);
reset($this->until);
$updated = ''; $updated = '';
while(!$this->disconnected and !$this->until_happened and (time() - $start < $timeout or $timeout == -1)) { while(!$this->disconnected and $this->until[$event_key] and (time() - $start < $timeout or $timeout == -1)) {
$read = array($this->socket); $read = array($this->socket);
$write = NULL; $write = NULL;
$except = NULL; $except = NULL;
@ -165,8 +180,8 @@ class XMLStream {
xml_parse($this->parser, $buff, False); xml_parse($this->parser, $buff, False);
} }
} }
$payload = $this->until_payload; $payload = $this->until_payload[$event_key];
$this->until_payload = array(); unset($this->until_payload[$event_key]);
return $payload; return $payload;
} }
@ -212,7 +227,12 @@ class XMLStream {
#clean-up old objects #clean-up old objects
$found = False; $found = False;
foreach($this->nshandlers as $handler) { foreach($this->nshandlers as $handler) {
if($this->xmlobj[2]->name == $handler[0] and ($this->xmlobj[2]->ns == $handler[1] or (!$handler[1] and $this->xmlobj[2]->ns == $this->default_ns))) { 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))) {
if($handler[3] === Null) $handler[3] = $this; if($handler[3] === Null) $handler[3] = $this;
call_user_method($handler[2], $handler[3], $this->xmlobj[2]); call_user_method($handler[2], $handler[3], $this->xmlobj[2]);
} }
@ -230,6 +250,7 @@ class XMLStream {
$this->xmlobj = array_slice($this->xmlobj, 0, 1); $this->xmlobj = array_slice($this->xmlobj, 0, 1);
$this->xmlobj[0]->subs = Null; $this->xmlobj[0]->subs = Null;
} }
unset($this->xmlobj[2]);
} }
if($this->xml_depth == 0 and !$this->been_reset) { if($this->xml_depth == 0 and !$this->been_reset) {
if(!$this->disconnected) { if(!$this->disconnected) {
@ -248,8 +269,11 @@ class XMLStream {
} }
function doReconnect() { function doReconnect() {
$this->connect(False, False); if(!$this->is_server) {
$this->reset(); $this->log->log("Reconnecting...", LOGGING_WARNING);
$this->connect(False, False);
$this->reset();
}
} }
function disconnect() { function disconnect() {
@ -268,9 +292,13 @@ class XMLStream {
call_user_method($handler[1], $handler[2], $payload); call_user_method($handler[1], $handler[2], $payload);
} }
} }
if(in_array($name, $this->until)) { foreach($this->until as $key => $until) {
$this->until_happened = True; if(is_array($until)) {
$this->until_payload[] = array($name, $payload); if(in_array($name, $until)) {
$this->until_payload[$key][] = array($name, $payload);
$this->until[$key] = False;
}
}
} }
} }
@ -286,14 +314,19 @@ class XMLStream {
function reset() { function reset() {
$this->xml_depth = 0; $this->xml_depth = 0;
$this->xmlobj = Null; unset($this->xmlobj);
$this->setupParser(); $this->setupParser();
$this->send($this->stream_start); if(!$this->is_server) {
$this->send($this->stream_start);
}
$this->been_reset = True; $this->been_reset = True;
} }
function setupParser() { function setupParser() {
$this->parser = xml_parser_create(); unset($this->parser);
$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");
xml_set_object($this->parser, $this); xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'startXML', 'endXML'); xml_set_element_handler($this->parser, 'startXML', 'endXML');
xml_set_character_data_handler($this->parser, 'charXML'); xml_set_character_data_handler($this->parser, 'charXML');