git-svn-id: svn://netflint.net/xmpphp@24 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
195
xmlstream.php
195
xmlstream.php
@@ -22,33 +22,42 @@ require_once("xmlobj.php");
|
||||
require_once("logging.php");
|
||||
|
||||
class XMLStream {
|
||||
var $socket;
|
||||
var $parser;
|
||||
var $buffer;
|
||||
var $xml_depth = 0;
|
||||
var $host;
|
||||
var $port;
|
||||
var $stream_start = '<stream>';
|
||||
var $stream_end = '</stream';
|
||||
var $disconnected = false;
|
||||
var $sent_disconnect = False;
|
||||
var $ns_map = array();
|
||||
var $current_ns = array();
|
||||
var $xmlobj = Null;
|
||||
var $nshandlers = array();
|
||||
var $idhandlers = array();
|
||||
var $eventhandlers = array();
|
||||
var $lastid = 0;
|
||||
var $default_ns;
|
||||
var $until = '';
|
||||
var $until_happened = False;
|
||||
var $until_payload = array();
|
||||
var $log;
|
||||
var $reconnect = True;
|
||||
var $been_reset = False;
|
||||
var $is_server;
|
||||
protected $socket;
|
||||
protected $parser;
|
||||
protected $buffer;
|
||||
protected $xml_depth = 0;
|
||||
protected $host;
|
||||
protected $port;
|
||||
protected $stream_start = '<stream>';
|
||||
protected $stream_end = '</stream';
|
||||
public $disconnected = false;
|
||||
protected $sent_disconnect = false;
|
||||
protected $ns_map = array();
|
||||
protected $current_ns = array();
|
||||
protected $xmlobj = null;
|
||||
protected $nshandlers = array();
|
||||
protected $idhandlers = array();
|
||||
protected $eventhandlers = array();
|
||||
protected $lastid = 0;
|
||||
protected $default_ns;
|
||||
protected $until = '';
|
||||
protected $until_happened = false;
|
||||
protected $until_payload = array();
|
||||
protected $log;
|
||||
protected $reconnect = true;
|
||||
protected $been_reset = false;
|
||||
protected $is_server;
|
||||
|
||||
function XMLStream($host=Null, $port=Null, $log=False, $loglevel=Null, $is_server=False) {
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $host
|
||||
* @param string $port
|
||||
* @param boolean $log
|
||||
* @param string $loglevel
|
||||
* @param boolean $is_server
|
||||
*/
|
||||
public function __construct($host = null, $port = null, $log = false, $loglevel = null, $is_server = false) {
|
||||
$this->reconnect = !$is_server;
|
||||
$this->is_server = $is_server;
|
||||
$this->host = $host;
|
||||
@@ -57,51 +66,51 @@ class XMLStream {
|
||||
$this->log = new Logging($log, $loglevel);
|
||||
}
|
||||
|
||||
function getId() {
|
||||
public function getId() {
|
||||
$this->lastid++;
|
||||
return $this->lastid;
|
||||
}
|
||||
|
||||
function addIdHandler($id, $pointer, $obj=Null) {
|
||||
public function addIdHandler($id, $pointer, $obj = null) {
|
||||
$this->idhandlers[$id] = array($pointer, $obj);
|
||||
}
|
||||
|
||||
function addHandler($name, $ns, $pointer, $obj=Null, $depth=1) {
|
||||
public function addHandler($name, $ns, $pointer, $obj = null, $depth = 1) {
|
||||
$this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
|
||||
}
|
||||
|
||||
function addEventHandler($name, $pointer, $obj) {
|
||||
public function addEventHandler($name, $pointer, $obj) {
|
||||
$this->eventhanders[] = array($name, $pointer, $obj);
|
||||
}
|
||||
|
||||
function connect($persistent=False, $sendinit=True) {
|
||||
$this->disconnected = False;
|
||||
$this->sent_disconnect = False;
|
||||
public function connect($persistent = false, $sendinit = true) {
|
||||
$this->disconnected = false;
|
||||
$this->sent_disconnect = false;
|
||||
if($persistent) {
|
||||
$conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
|
||||
} else {
|
||||
$conflag = STREAM_CLIENT_CONNECT;
|
||||
}
|
||||
$this->log->log("Connecting to tcp://{$this->host}:{$this->port}");
|
||||
$this->socket = stream_socket_client("tcp://{$this->host}:{$this->port}", $flags=$conflag);
|
||||
$this->socket = stream_socket_client("tcp://{$this->host}:{$this->port}", $conflag);
|
||||
if(!$this->socket) {
|
||||
$this->log->log("Could not connect.", LOGGING_ERROR);
|
||||
$this->disconnected = True;
|
||||
$this->log->log("Could not connect.", Logging::LOG_ERROR);
|
||||
$this->disconnected = true;
|
||||
}
|
||||
stream_set_blocking($this->socket, 1);
|
||||
if($sendinit) $this->send($this->stream_start);
|
||||
}
|
||||
|
||||
function apply_socket($socket) {
|
||||
public function apply_socket($socket) {
|
||||
$this->socket = $socket;
|
||||
}
|
||||
|
||||
function process() {
|
||||
public function process() {
|
||||
$updated = '';
|
||||
while(!$this->disconnect) {
|
||||
$read = array($this->socket);
|
||||
$write = NULL;
|
||||
$except = NULL;
|
||||
$write = null;
|
||||
$except = null;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$buff = @fread($this->socket, 1024);
|
||||
@@ -110,36 +119,36 @@ class XMLStream {
|
||||
$this->doReconnect();
|
||||
} else {
|
||||
fclose($this->socket);
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
||||
xml_parse($this->parser, $buff, False);
|
||||
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
|
||||
xml_parse($this->parser, $buff, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function read() {
|
||||
public function read() {
|
||||
$buff = @fread($this->socket, 1024);
|
||||
if(!$buff) {
|
||||
if($this->reconnect) {
|
||||
$this->doReconnect();
|
||||
} else {
|
||||
fclose($this->socket);
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
||||
xml_parse($this->parser, $buff, False);
|
||||
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
|
||||
xml_parse($this->parser, $buff, false);
|
||||
}
|
||||
|
||||
function processTime($timeout=-1) {
|
||||
public function processTime($timeout = -1) {
|
||||
$start = time();
|
||||
$updated = '';
|
||||
while(!$this->disconnected and ($timeout == -1 or time() - $start < $timeout)) {
|
||||
$read = array($this->socket);
|
||||
$write = NULL;
|
||||
$except = NULL;
|
||||
$write = null;
|
||||
$except = null;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$buff = @fread($this->socket, 1024);
|
||||
@@ -148,16 +157,16 @@ class XMLStream {
|
||||
$this->doReconnect();
|
||||
} else {
|
||||
fclose($this->socket);
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
||||
xml_parse($this->parser, $buff, False);
|
||||
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
|
||||
xml_parse($this->parser, $buff, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processUntil($event, $timeout=-1) {
|
||||
public function processUntil($event, $timeout=-1) {
|
||||
$start = time();
|
||||
if(!is_array($event)) $event = array($event);
|
||||
$this->until[] = $event;
|
||||
@@ -167,8 +176,8 @@ class XMLStream {
|
||||
$updated = '';
|
||||
while(!$this->disconnected and $this->until[$event_key] and (time() - $start < $timeout or $timeout == -1)) {
|
||||
$read = array($this->socket);
|
||||
$write = NULL;
|
||||
$except = NULL;
|
||||
$write = null;
|
||||
$except = null;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$buff = @fread($this->socket, 1024);
|
||||
@@ -177,11 +186,11 @@ class XMLStream {
|
||||
$this->doReconnect();
|
||||
} else {
|
||||
fclose($this->socket);
|
||||
return False;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$this->log->log("RECV: $buff", LOGGING_VERBOSE);
|
||||
xml_parse($this->parser, $buff, False);
|
||||
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
|
||||
xml_parse($this->parser, $buff, false);
|
||||
}
|
||||
}
|
||||
if(array_key_exists($event_key, $this->until_payload)) {
|
||||
@@ -193,9 +202,9 @@ class XMLStream {
|
||||
return $payload;
|
||||
}
|
||||
|
||||
function startXML($parser, $name, $attr) {
|
||||
public function startXML($parser, $name, $attr) {
|
||||
if($this->been_reset) {
|
||||
$this->been_reset = False;
|
||||
$this->been_reset = false;
|
||||
$this->xml_depth = 0;
|
||||
}
|
||||
$this->xml_depth++;
|
||||
@@ -213,7 +222,7 @@ class XMLStream {
|
||||
$this->ns_map[$key] = $value;
|
||||
}
|
||||
}
|
||||
if(!strstr($name, ":") === False)
|
||||
if(!strstr($name, ":") === false)
|
||||
{
|
||||
$name = explode(':', $name);
|
||||
$ns = $this->ns_map[$name[0]];
|
||||
@@ -225,33 +234,33 @@ class XMLStream {
|
||||
$this->xmlobj[$this->xml_depth] = $obj;
|
||||
}
|
||||
|
||||
function endXML($parser, $name) {
|
||||
#$this->log->log("Ending $name", LOGGING_DEBUG);
|
||||
public function endXML($parser, $name) {
|
||||
#$this->log->log("Ending $name", Logging::LOG_DEBUG);
|
||||
#print "$name\n";
|
||||
if($this->been_reset) {
|
||||
$this->been_reset = False;
|
||||
$this->been_reset = false;
|
||||
$this->xml_depth = 0;
|
||||
}
|
||||
$this->xml_depth--;
|
||||
if($this->xml_depth == 1) {
|
||||
#clean-up old objects
|
||||
$found = False;
|
||||
$found = false;
|
||||
foreach($this->nshandlers as $handler) {
|
||||
if($handler[4] != 1 and $this->xmlobj[2]->hassub($handler[0])) {
|
||||
$searchxml = $this->xmlobj[2]->sub($handler[0]);
|
||||
} elseif(is_array($this->xmlobj) and array_key_exists(2, $this->xmlobj)) {
|
||||
$searchxml = $this->xmlobj[2];
|
||||
}
|
||||
if($searchxml !== Null and $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;
|
||||
$this->log->log("Calling {$handler[2]}", LOGGING_DEBUG);
|
||||
call_user_method($handler[2], $handler[3], $this->xmlobj[2]);
|
||||
if($searchxml !== null and $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;
|
||||
$this->log->log("Calling {$handler[2]}", Logging::LOG_DEBUG);
|
||||
call_user_func(array($handler[3], $handler[2]), $this->xmlobj[2]);
|
||||
}
|
||||
}
|
||||
foreach($this->idhandlers as $id => $handler) {
|
||||
if(array_key_exists('id', $this->xmlobj[2]->attrs) and $this->xmlobj[2]->attrs['id'] == $id) {
|
||||
if($handler[1] === Null) $handler[1] = $this;
|
||||
call_user_method($handler[0], $handler[1], $this->xmlobj[2]);
|
||||
if($handler[1] === null) $handler[1] = $this;
|
||||
call_user_func(array($handler[1], $handler[0]), $this->xmlobj[2]);
|
||||
#id handlers are only used once
|
||||
unset($this->idhandlers[$id]);
|
||||
break;
|
||||
@@ -259,7 +268,9 @@ class XMLStream {
|
||||
}
|
||||
if(is_array($this->xmlobj)) {
|
||||
$this->xmlobj = array_slice($this->xmlobj, 0, 1);
|
||||
$this->xmlobj[0]->subs = Null;
|
||||
if($this->xmlobj[0] instanceof XMLObj) {
|
||||
$this->xmlobj[0]->subs = null;
|
||||
}
|
||||
}
|
||||
unset($this->xmlobj[2]);
|
||||
}
|
||||
@@ -268,8 +279,8 @@ class XMLStream {
|
||||
if(!$this->sent_disconnect) {
|
||||
$this->send($this->stream_end);
|
||||
}
|
||||
$this->disconnected = True;
|
||||
$this->sent_disconnect = True;
|
||||
$this->disconnected = true;
|
||||
$this->sent_disconnect = true;
|
||||
fclose($this->socket);
|
||||
if($this->reconnect) {
|
||||
$this->doReconnect();
|
||||
@@ -279,27 +290,27 @@ class XMLStream {
|
||||
}
|
||||
}
|
||||
|
||||
function doReconnect() {
|
||||
public function doReconnect() {
|
||||
if(!$this->is_server) {
|
||||
$this->log->log("Reconnecting...", LOGGING_WARNING);
|
||||
$this->connect(False, False);
|
||||
$this->log->log("Reconnecting...", Logging::LOG_WARNING);
|
||||
$this->connect(false, false);
|
||||
$this->reset();
|
||||
}
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
$this->reconnect = False;
|
||||
public function disconnect() {
|
||||
$this->reconnect = false;
|
||||
$this->send($this->stream_end);
|
||||
$this->sent_disconnect = True;
|
||||
$this->sent_disconnect = true;
|
||||
$this->processUntil('end_stream', 5);
|
||||
$this->disconnected = True;
|
||||
$this->disconnected = true;
|
||||
}
|
||||
|
||||
function event($name, $payload=Null) {
|
||||
$this->log->log("EVENT: $name", LOGGING_DEBUG);
|
||||
public function event($name, $payload = null) {
|
||||
$this->log->log("EVENT: $name", Logging::LOG_DEBUG);
|
||||
foreach($this->eventhandlers as $handler) {
|
||||
if($name == $handler[0]) {
|
||||
if($handler[2] === Null) $handler[2] = $this;
|
||||
if($handler[2] === null) $handler[2] = $this;
|
||||
call_user_method($handler[1], $handler[2], $payload);
|
||||
}
|
||||
}
|
||||
@@ -307,24 +318,24 @@ class XMLStream {
|
||||
if(is_array($until)) {
|
||||
if(in_array($name, $until)) {
|
||||
$this->until_payload[$key][] = array($name, $payload);
|
||||
$this->until[$key] = False;
|
||||
$this->until[$key] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function charXML($parser, $data) {
|
||||
public function charXML($parser, $data) {
|
||||
if(array_key_exists($this->xml_depth, $this->xmlobj))
|
||||
$this->xmlobj[$this->xml_depth]->data .= $data;
|
||||
}
|
||||
|
||||
function send($msg) {
|
||||
public function send($msg) {
|
||||
#socket_write($this->socket, $msg);
|
||||
$this->log->log("SENT: $msg", LOGGING_VERBOSE);
|
||||
$this->log->log("SENT: $msg", Logging::LOG_VERBOSE);
|
||||
@fwrite($this->socket, $msg);
|
||||
}
|
||||
|
||||
function reset() {
|
||||
public function reset() {
|
||||
$this->xml_depth = 0;
|
||||
unset($this->xmlobj);
|
||||
$this->xmlobj = array();
|
||||
@@ -332,10 +343,10 @@ class XMLStream {
|
||||
if(!$this->is_server) {
|
||||
$this->send($this->stream_start);
|
||||
}
|
||||
$this->been_reset = True;
|
||||
$this->been_reset = true;
|
||||
}
|
||||
|
||||
function setupParser() {
|
||||
public function setupParser() {
|
||||
$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");
|
||||
|
Reference in New Issue
Block a user