2008-03-29 18:45:29 +00:00
|
|
|
<?php
|
|
|
|
|
2008-03-30 10:06:46 +01:00
|
|
|
class XMLObj {
|
|
|
|
var $name;
|
|
|
|
var $ns;
|
|
|
|
var $attrs = array();
|
|
|
|
var $subs = array();
|
|
|
|
var $data = '';
|
|
|
|
|
|
|
|
function XMLObj($name, $ns='', $attrs=array(), $data='') {
|
|
|
|
$this->name = strtolower($name);
|
|
|
|
$this->ns = $ns;
|
|
|
|
if(is_array($attrs)) {
|
|
|
|
foreach($attrs as $key => $value) {
|
|
|
|
$this->attrs[strtolower($key)] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function printobj($depth=0) {
|
|
|
|
print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data;
|
|
|
|
print "\n";
|
|
|
|
foreach($this->subs as $sub) {
|
|
|
|
$sub->printobj($depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hassub($name) {
|
|
|
|
foreach($this->subs as $sub) {
|
|
|
|
if($sub->name == $name) return True;
|
|
|
|
}
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sub($name, $attrs=Null, $ns=Null) {
|
|
|
|
foreach($this->subs as $sub) {
|
|
|
|
if($sub->name == $name) return $sub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>';
|
|
|
|
var $disconnect = false;
|
2008-03-30 10:06:46 +01:00
|
|
|
var $ns_map = array();
|
|
|
|
var $current_ns;
|
|
|
|
var $xmlobj = Null;
|
|
|
|
var $nshandlers = array();
|
|
|
|
var $idhandlers = array();
|
|
|
|
var $lastid = 0;
|
2008-03-29 18:45:29 +00:00
|
|
|
|
|
|
|
function XMLStream($host, $port) {
|
2008-03-30 10:55:59 +01:00
|
|
|
#$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
2008-03-29 18:45:29 +00:00
|
|
|
$this->host = $host;
|
|
|
|
$this->port = $port;
|
|
|
|
#set up the parser
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->setupParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getId() {
|
|
|
|
$this->lastid++;
|
|
|
|
return $this->lastid;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addIdHandler($id, $pointer, $obj=Null) {
|
|
|
|
$this->idhandlers[$id] = array($pointer, $obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addHandler($name, $ns, $pointer, $obj=Null) {
|
|
|
|
$this->nshandlers[] = array($name,$ns,$pointer,$obj);
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function connect() {
|
2008-03-30 10:55:59 +01:00
|
|
|
#if(socket_connect($this->socket, $this->host, $this->port)) {
|
|
|
|
# socket_write($this->socket, $this->stream_start);
|
|
|
|
#}
|
2008-04-02 01:48:31 +01:00
|
|
|
print "connecting to tcp://{$this->host}:{$this->port}\n";
|
2008-03-30 10:55:59 +01:00
|
|
|
$this->socket = stream_socket_client("tcp://{$this->host}:{$this->port}");
|
|
|
|
$this->send($this->stream_start);
|
2008-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function process() {
|
|
|
|
while(!$this->disconnect) {
|
2008-03-30 10:55:59 +01:00
|
|
|
#$buff = socket_read($this->socket, 1024);
|
|
|
|
$buff = fread($this->socket, 1024);
|
2008-04-02 01:48:31 +01:00
|
|
|
print "RECV: '$buff'\n";
|
2008-03-30 10:06:46 +01:00
|
|
|
xml_parse($this->parser, $buff, False);
|
2008-04-02 01:48:31 +01:00
|
|
|
sleep(1);
|
2008-03-29 18:45:29 +00:00
|
|
|
# parse whatever we get out of the socket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-02 01:48:31 +01:00
|
|
|
function processTime($timeout=-1) {
|
|
|
|
$start = time();
|
|
|
|
$updated = 'hi';
|
|
|
|
while($timeout == -1 or time() - $start < $timeout) {
|
|
|
|
$timeleft = $timeout - (time() - $start);
|
|
|
|
$read = array($this->socket);
|
|
|
|
$write = NULL;
|
|
|
|
$except = NULL;
|
|
|
|
$updated = stream_select($read, $write, $except, intval($timeleft));
|
|
|
|
if ($updated > 0) {
|
|
|
|
$buff = fread($this->socket, 1024);
|
|
|
|
xml_parse($this->parser, $buff, False);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processUntil($mask) {
|
|
|
|
}
|
|
|
|
|
2008-03-29 18:45:29 +00:00
|
|
|
function startXML($parser, $name, $attr) {
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->xml_depth++;
|
|
|
|
if(array_key_exists('XMLNS', $attr)) {
|
|
|
|
$this->current_ns = $attr['XMLNS'];
|
|
|
|
}
|
|
|
|
$ns = $this->current_ns;
|
|
|
|
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-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) {
|
|
|
|
if($this->xmlobj[2]->name == $handler[0] and $this->xmlobj[2]->ns == $handler[1]) {
|
|
|
|
if($handler[3] === Null) $handler[3] = $this;
|
|
|
|
call_user_method($handler[2], $handler[3], $this->xmlobj[2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-03-29 18:45:29 +00:00
|
|
|
}
|
|
|
|
function charXML($parser, $data) {
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->xmlobj[$this->xml_depth]->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function send($msg) {
|
2008-03-30 10:55:59 +01:00
|
|
|
#socket_write($this->socket, $msg);
|
2008-04-02 01:48:31 +01:00
|
|
|
print "SENT: $msg \n";
|
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;
|
|
|
|
$this->xmlobj = Null;
|
|
|
|
$this->setupParser();
|
|
|
|
$this->send($this->stream_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupParser() {
|
|
|
|
$this->parser = xml_parser_create();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-30 10:06:46 +01:00
|
|
|
class XMPP extends XMLStream {
|
|
|
|
var $server;
|
|
|
|
var $user;
|
|
|
|
var $password;
|
|
|
|
var $resource;
|
|
|
|
|
2008-03-30 10:55:59 +01:00
|
|
|
function XMPP($host, $port, $user, $password, $resource, $server=Null) {
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->XMLStream($host, $port);
|
|
|
|
$this->user = $user;
|
|
|
|
$this->password = $password;
|
|
|
|
$this->resource = $resource;
|
2008-03-30 10:55:59 +01:00
|
|
|
if(!$server) $server = $host;
|
|
|
|
$this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">\n';
|
2008-03-30 10:06:46 +01:00
|
|
|
$this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler');
|
|
|
|
$this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler');
|
|
|
|
$this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler');
|
2008-03-30 10:55:59 +01:00
|
|
|
$this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler');
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function features_handler($xml) {
|
2008-03-30 10:55:59 +01:00
|
|
|
if($xml->hassub('starttls')) {
|
|
|
|
$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
|
|
|
|
} elseif($xml->hassub('bind')) {
|
2008-03-30 10:06:46 +01:00
|
|
|
$id = $this->getId();
|
|
|
|
print "ok, we can bind $id\n";
|
|
|
|
$this->addIdHandler($id, 'resource_bind_handler');
|
|
|
|
$this->send("<iq xmlns=\"jabber:client\" type=\"set\" id=\"$id\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><resource>{$this->resource}</resource></bind></iq>");
|
|
|
|
} else {
|
|
|
|
print "Attempting Auth...\n";
|
|
|
|
$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sasl_success_handler($xml) {
|
|
|
|
print "Auth success!\n";
|
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
function sasl_failure_handler($xml) {
|
|
|
|
print "Auth failed!\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
function resource_bind_handler($xml) {
|
|
|
|
if($xml->attrs['type'] == 'result') print "Bound to " . $xml->sub('bind')->sub('jid')->data . "\n";
|
|
|
|
$id = $this->getId();
|
|
|
|
$this->addIdHandler($id, 'session_start_handler');
|
|
|
|
$this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>");
|
|
|
|
}
|
|
|
|
|
|
|
|
function session_start_handler($xml) {
|
|
|
|
print "session started\n";
|
|
|
|
}
|
2008-03-30 10:55:59 +01:00
|
|
|
|
|
|
|
function tls_proceed_handler($xml) {
|
|
|
|
print "Starting TLS connection\n";
|
|
|
|
stream_socket_enable_crypto($this->socket, True, STREAM_CRYPTO_METHOD_TLS_CLIENT);
|
2008-04-02 01:48:31 +01:00
|
|
|
print stream_socket_get_name($this->socket, True) . "\n";
|
2008-03-30 10:55:59 +01:00
|
|
|
$this->reset();
|
|
|
|
}
|
2008-03-30 10:06:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-29 18:45:29 +00:00
|
|
|
|