* everything working
git-svn-id: svn://netflint.net/xmpphp@7 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
67
xmpp.php
67
xmpp.php
@@ -1,20 +1,23 @@
|
||||
<?php
|
||||
require_once("xmlobj.php");
|
||||
require_once("xmlstream.php");
|
||||
require_once("logging.php");
|
||||
|
||||
class XMPP extends XMLStream {
|
||||
var $server;
|
||||
var $user;
|
||||
var $password;
|
||||
var $resource;
|
||||
var $fulljid;
|
||||
|
||||
function XMPP($host, $port, $user, $password, $resource, $server=Null) {
|
||||
$this->XMLStream($host, $port);
|
||||
function XMPP($host, $port, $user, $password, $resource, $server=Null, $printlog=False, $loglevel=Null) {
|
||||
$this->XMLStream($host, $port, $loglevel, $loglevel);
|
||||
$this->user = $user;
|
||||
$this->password = $password;
|
||||
$this->resource = $resource;
|
||||
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';
|
||||
$this->stream_end = '</stream:stream>';
|
||||
$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');
|
||||
@@ -25,10 +28,51 @@ class XMPP extends XMLStream {
|
||||
}
|
||||
|
||||
function message_handler($xml) {
|
||||
print "Message: {$xml->sub('body')->data}\n";
|
||||
$payload['type'] = $xml->attrs['type'];
|
||||
if(!$paytload['type']) $payload['type'] = 'chat';
|
||||
$payload['from'] = $xml->attrs['from'];
|
||||
$payload['body'] = $xml->sub('body')->data;
|
||||
$this->log->log("Message: {$xml->sub('body')->data}", LOGGING_DEBUG);
|
||||
$this->event('message', $payload);
|
||||
}
|
||||
|
||||
function message($to, $body, $type='chat', $subject=Null) {
|
||||
$to = htmlentities($to);
|
||||
$body = htmlentities($body);
|
||||
$subject = htmlentities($subject);
|
||||
$out = "<message from='{$this->fulljid}' to='$to' type='$type'>";
|
||||
if($subject) $out .= "<subject>$subject</subject>";
|
||||
$out .= "<body>$body</body></message>";
|
||||
$this->send($out);
|
||||
}
|
||||
|
||||
function presence($status=Null, $show='available', $to=Null) {
|
||||
$to = htmlentities($to);
|
||||
$status = htmlentities($status);
|
||||
if($show == 'unavailable') $type = 'unavailable';
|
||||
$out = "<presence";
|
||||
if($to) $out .= " to='$to'";
|
||||
if($type) $out .= " type='$type'";
|
||||
if($show == 'available' and !$status) {
|
||||
$out .= "/>";
|
||||
} else {
|
||||
$out .= ">";
|
||||
if($show != 'available') $out .= "<show>$show</show>";
|
||||
if($status) $out .= "<status>$status</status>";
|
||||
$out .= "</presence>";
|
||||
}
|
||||
$this->send($out);
|
||||
}
|
||||
|
||||
function presence_handler($xml) {
|
||||
$payload['type'] = $xml->attrs['type'];
|
||||
if(!$payload['type']) $payload['type'] = 'available';
|
||||
$payload['show'] = $xml->sub('show')->data;
|
||||
if(!$payload['show']) $payload['show'] = $payload['type'];
|
||||
$payload['from'] = $xml->attrs['from'];
|
||||
$payload['status'] = $xml->sub('status')->data;
|
||||
$this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", LOGGING_DEBUG);
|
||||
$this->event('presence', $payload);
|
||||
}
|
||||
|
||||
function features_handler($xml) {
|
||||
@@ -36,40 +80,41 @@ class XMPP extends XMLStream {
|
||||
$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
|
||||
} elseif($xml->hassub('bind')) {
|
||||
$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->log->log("Attempting Auth...");
|
||||
$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->log->log("Auth success!");
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
function sasl_failure_handler($xml) {
|
||||
print "Auth failed!\n";
|
||||
$this->log->log("Auth failed!", LOGGING_ERROR);
|
||||
}
|
||||
|
||||
function resource_bind_handler($xml) {
|
||||
if($xml->attrs['type'] == 'result') print "Bound to " . $xml->sub('bind')->sub('jid')->data . "\n";
|
||||
if($xml->attrs['type'] == 'result') {
|
||||
$this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
|
||||
$this->fulljid = $xml->sub('bind')->sub('jid')->data;
|
||||
}
|
||||
$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";
|
||||
$this->log->log("Session started");
|
||||
$this->event('session_start');
|
||||
}
|
||||
|
||||
function tls_proceed_handler($xml) {
|
||||
print "Starting TLS connection\n";
|
||||
$this->log->log("Starting TLS encryption");
|
||||
stream_socket_enable_crypto($this->socket, True, STREAM_CRYPTO_METHOD_TLS_CLIENT);
|
||||
print stream_socket_get_name($this->socket, True) . "\n";
|
||||
$this->reset();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user