added a class prefix, refactored log-class, finished scopes for methods in xmlstream and xmpp

git-svn-id: svn://netflint.net/xmpphp@29 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
temp 2008-05-01 14:29:33 +00:00
parent 798d473090
commit 6dae72dd78
7 changed files with 543 additions and 187 deletions

101
XMPPHP/Log.php Normal file
View File

@ -0,0 +1,101 @@
<?php
/*
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
*/
class XMPPHP_Log {
const LEVEL_ERROR = 0;
const LEVEL_WARNING = 1;
const LEVEL_INFO = 2;
const LEVEL_DEBUG = 3;
const LEVEL_VERBOSE = 4;
/**
* @var array
*/
protected $data = array();
/**
* @var array
*/
protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
/**
* @var integer
*/
protected $runlevel;
/**
* @var boolean
*/
protected $printout;
/**
* Constructor
*
* @param boolean $printout
* @param string $runlevel
*/
public function __construct($printout = false, $runlevel = self::LEVEL_INFO) {
$this->runlevel = (int)$runlevel;
$this->printout = (boolean)$printout;
}
/**
* Add a message to the log data array
* If printout in this instance is set to true, directly output the message
*
* @param string $msg
* @param integer $runlevel
*/
public function log($msg, $runlevel = self::LEVEL_INFO) {
$time = time();
$this->data[] = array($this->runlevel, $msg, $time);
if($this->printout and $runlevel <= $this->runlevel) {
$this->writeLine($msg, $runlevel, $time);
}
}
/**
* Output the complete log.
* Log will be cleared if $clear = true
*
* @param boolean $clear
* @param integer $runlevel
*/
public function printout($clear = true, $runlevel = null) {
if($runlevel === null) {
$runlevel = $this->runlevel;
}
foreach($this->data as $data) {
if($runlevel <= $data[0]) {
$this->writeLine($data[1], $runlevel, $data[2]);
}
}
if($clear) {
$this->data = array();
}
}
protected function writeLine($msg, $runlevel, $time) {
//echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
echo $time." [".$this->names[$runlevel]."]: ".$msg."\n";
}
}

View File

@ -19,11 +19,40 @@ along with XMPPHP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
class XMLObj { class XMPPHP_XMLObj {
/**
* Tag name
*
* @var string
*/
public $name; public $name;
/**
* Namespace
*
* @var string
*/
public $ns; public $ns;
/**
* Attributes
*
* @var array
*/
public $attrs = array(); public $attrs = array();
/**
* Subs?
*
* @var array
*/
public $subs = array(); public $subs = array();
/**
* Node data
*
* @var string
*/
public $data = ''; public $data = '';
/** /**
@ -45,6 +74,11 @@ class XMLObj {
$this->data = $data; $this->data = $data;
} }
/**
* Dump this XML Object to output.
*
* @param integer $depth
*/
public function printObj($depth = 0) { public function printObj($depth = 0) {
print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data; print str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data;
print "\n"; print "\n";
@ -53,6 +87,11 @@ class XMLObj {
} }
} }
/**
* Return this XML Object in xml notation
*
* @param string $str
*/
public function toString($str = '') { public function toString($str = '') {
$str .= "<{$this->name} xmlns='{$this->ns}' "; $str .= "<{$this->name} xmlns='{$this->ns}' ";
foreach($this->attrs as $key => $value) { foreach($this->attrs as $key => $value) {
@ -70,16 +109,31 @@ class XMLObj {
return $str; return $str;
} }
/**
* Has this XML Object the given sub?
*
* @param string $name
* @return boolean
*/
public function hasSub($name) { public 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;
} }
return False; return false;
} }
/**
* Return a sub
*
* @param string $name
* @param string $attrs
* @param string $ns
*/
public function sub($name, $attrs = null, $ns = null) { public function sub($name, $attrs = null, $ns = null) {
foreach($this->subs as $sub) { foreach($this->subs as $sub) {
if($sub->name == $name) return $sub; if($sub->name == $name) {
return $sub;
}
} }
} }
} }

View File

@ -18,34 +18,110 @@ You should have received a copy of the GNU General Public License
along with XMPPHP; if not, write to the Free Software along with XMPPHP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
require_once("xmlobj.php");
require_once("logging.php");
class XMLStream { require_once("XMLObj.php");
require_once("Log.php");
class XMPPHP_XMLStream {
/**
* @var resource
*/
protected $socket; protected $socket;
/**
* @var resource
*/
protected $parser; protected $parser;
/**
* @var string
*/
protected $buffer; protected $buffer;
/**
* @var integer
*/
protected $xml_depth = 0; protected $xml_depth = 0;
/**
* @var string
*/
protected $host; protected $host;
/**
* @var integer
*/
protected $port; protected $port;
/**
* @var string
*/
protected $stream_start = '<stream>'; protected $stream_start = '<stream>';
/**
* @var string
*/
protected $stream_end = '</stream>'; protected $stream_end = '</stream>';
public $disconnected = false; /**
* @var boolean
*/
protected $disconnected = false;
/**
* @var boolean
*/
protected $sent_disconnect = false; protected $sent_disconnect = false;
/**
* @var array
*/
protected $ns_map = array(); protected $ns_map = array();
/**
* @var array
*/
protected $current_ns = array(); protected $current_ns = array();
/**
* @var array
*/
protected $xmlobj = null; protected $xmlobj = null;
/**
* @var array
*/
protected $nshandlers = array(); protected $nshandlers = array();
/**
* @var array
*/
protected $idhandlers = array(); protected $idhandlers = array();
/**
* @var array
*/
protected $eventhandlers = array(); protected $eventhandlers = array();
/**
* @var integer
*/
protected $lastid = 0; protected $lastid = 0;
/**
* @var string
*/
protected $default_ns; protected $default_ns;
/**
* @var string
*/
protected $until = ''; protected $until = '';
/**
* @var array
*/
protected $until_happened = false; protected $until_happened = false;
/**
* @var array
*/
protected $until_payload = array(); protected $until_payload = array();
/**
* @var XMPPHP_Log
*/
protected $log; protected $log;
/**
* @var boolean
*/
protected $reconnect = true; protected $reconnect = true;
/**
* @var boolean
*/
protected $been_reset = false; protected $been_reset = false;
/**
* @var boolean
*/
protected $is_server; protected $is_server;
/** /**
@ -53,36 +129,79 @@ class XMLStream {
* *
* @param string $host * @param string $host
* @param string $port * @param string $port
* @param boolean $log * @param boolean $printlog
* @param string $loglevel * @param string $loglevel
* @param boolean $is_server * @param boolean $is_server
*/ */
public function __construct($host = null, $port = null, $log = false, $loglevel = null, $is_server = false) { public function __construct($host = null, $port = null, $printlog = false, $loglevel = null, $is_server = false) {
$this->reconnect = !$is_server; $this->reconnect = !$is_server;
$this->is_server = $is_server; $this->is_server = $is_server;
$this->host = $host; $this->host = $host;
$this->port = $port; $this->port = $port;
$this->setupParser(); $this->setupParser();
$this->log = new Logging($log, $loglevel); $this->log = new XMPPHP_Log($printlog, $loglevel);
} }
/**
* Return the log instance
*
* @return XMPPHP_Log
*/
public function getLog() {
return $this->log;
}
/**
* Get next ID
*
* @return integer
*/
public function getId() { public function getId() {
$this->lastid++; $this->lastid++;
return $this->lastid; return $this->lastid;
} }
/**
* Add ID Handler
*
* @param integer $id
* @param string $pointer
* @param string $obj
*/
public function addIdHandler($id, $pointer, $obj = null) { public function addIdHandler($id, $pointer, $obj = null) {
$this->idhandlers[$id] = array($pointer, $obj); $this->idhandlers[$id] = array($pointer, $obj);
} }
/**
* Add Handler
*
* @param integer $id
* @param string $ns
* @param string $pointer
* @param string $obj
* @param integer $depth
*/
public 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); $this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
} }
/**
* Add Evemt Handler
*
* @param integer $id
* @param string $pointer
* @param string $obj
*/
public function addEventHandler($name, $pointer, $obj) { public function addEventHandler($name, $pointer, $obj) {
$this->eventhanders[] = array($name, $pointer, $obj); $this->eventhanders[] = array($name, $pointer, $obj);
} }
/**
* Connect to XMPP Host
*
* @param boolean $persistent
* @param boolean $sendinit
*/
public function connect($persistent = false, $sendinit = true) { public function connect($persistent = false, $sendinit = true) {
$this->disconnected = false; $this->disconnected = false;
$this->sent_disconnect = false; $this->sent_disconnect = false;
@ -94,17 +213,49 @@ class XMLStream {
$this->log->log("Connecting to tcp://{$this->host}:{$this->port}"); $this->log->log("Connecting to tcp://{$this->host}:{$this->port}");
$this->socket = stream_socket_client("tcp://{$this->host}:{$this->port}", $conflag); $this->socket = stream_socket_client("tcp://{$this->host}:{$this->port}", $conflag);
if(!$this->socket) { if(!$this->socket) {
$this->log->log("Could not connect.", Logging::LOG_ERROR); $this->log->log("Could not connect.", XMPPHP_Log::LEVEL_ERROR);
$this->disconnected = true; $this->disconnected = true;
} }
stream_set_blocking($this->socket, 1); stream_set_blocking($this->socket, 1);
if($sendinit) $this->send($this->stream_start); if($sendinit) $this->send($this->stream_start);
} }
public function apply_socket($socket) { /**
$this->socket = $socket; * Reconnect XMPP Host
*/
public function doReconnect() {
if(!$this->is_server) {
$this->log->log("Reconnecting...", XMPPHP_Log::LEVEL_WARNING);
$this->connect(false, false);
$this->reset();
}
} }
/**
* Disconnect from XMPP Host
*/
public function disconnect() {
$this->reconnect = false;
$this->send($this->stream_end);
$this->sent_disconnect = true;
$this->processUntil('end_stream', 5);
$this->disconnected = true;
}
/**
* Are we are disconnected?
*
* @return boolean
*/
public function isDisconnected() {
return $this->connected;
}
/**
* Process
*
* @return string
*/
public function process() { public function process() {
$updated = ''; $updated = '';
while(!$this->disconnect) { while(!$this->disconnect) {
@ -122,26 +273,18 @@ class XMLStream {
return false; return false;
} }
} }
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE); $this->log->log("RECV: $buff", XMPPHP_Log::LEVEL_VERBOSE);
xml_parse($this->parser, $buff, false); xml_parse($this->parser, $buff, false);
} }
} }
} }
public function read() { /**
$buff = @fread($this->socket, 1024); * Process until a timeout occurs
if(!$buff) { *
if($this->reconnect) { * @param integer $timeout
$this->doReconnect(); * @return string
} else { */
fclose($this->socket);
return false;
}
}
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
xml_parse($this->parser, $buff, false);
}
public function processTime($timeout = -1) { public function processTime($timeout = -1) {
$start = time(); $start = time();
$updated = ''; $updated = '';
@ -160,12 +303,19 @@ class XMLStream {
return false; return false;
} }
} }
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE); $this->log->log("RECV: $buff", XMPPHP_Log::LEVEL_VERBOSE);
xml_parse($this->parser, $buff, false); xml_parse($this->parser, $buff, false);
} }
} }
} }
/**
* Process until a specified event or a timeout occurs
*
* @param string|array $event
* @param integer $timeout
* @return string
*/
public function processUntil($event, $timeout=-1) { public function processUntil($event, $timeout=-1) {
$start = time(); $start = time();
if(!is_array($event)) $event = array($event); if(!is_array($event)) $event = array($event);
@ -189,7 +339,7 @@ class XMLStream {
return false; return false;
} }
} }
$this->log->log("RECV: $buff", Logging::LOG_VERBOSE); $this->log->log("RECV: $buff", XMPPHP_Log::LEVEL_VERBOSE);
xml_parse($this->parser, $buff, false); xml_parse($this->parser, $buff, false);
} }
} }
@ -202,7 +352,22 @@ class XMLStream {
return $payload; return $payload;
} }
public function startXML($parser, $name, $attr) { /**
* Obsolete?
*/
protected function Xapply_socket($socket) {
$this->socket = $socket;
}
/**
* XML start callback
*
* @see xml_set_element_handler
*
* @param resource $parser
* @param string $name
*/
protected function startXML($parser, $name, $attr) {
if($this->been_reset) { if($this->been_reset) {
$this->been_reset = false; $this->been_reset = false;
$this->xml_depth = 0; $this->xml_depth = 0;
@ -228,14 +393,22 @@ class XMLStream {
$ns = $this->ns_map[$name[0]]; $ns = $this->ns_map[$name[0]];
$name = $name[1]; $name = $name[1];
} }
$obj = new XMLObj($name, $ns, $attr); $obj = new XMPPHP_XMLObj($name, $ns, $attr);
if($this->xml_depth > 1) if($this->xml_depth > 1)
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj; $this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
$this->xmlobj[$this->xml_depth] = $obj; $this->xmlobj[$this->xml_depth] = $obj;
} }
public function endXML($parser, $name) { /**
#$this->log->log("Ending $name", Logging::LOG_DEBUG); * XML end callback
*
* @see xml_set_element_handler
*
* @param resource $parser
* @param string $name
*/
protected function endXML($parser, $name) {
#$this->log->log("Ending $name", XMPPHP_Log::LEVEL_DEBUG);
#print "$name\n"; #print "$name\n";
if($this->been_reset) { if($this->been_reset) {
$this->been_reset = false; $this->been_reset = false;
@ -246,14 +419,14 @@ 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($handler[4] != 1 and $this->xmlobj[2]->hassub($handler[0])) { if($handler[4] != 1 and $this->xmlobj[2]->hasSub($handler[0])) {
$searchxml = $this->xmlobj[2]->sub($handler[0]); $searchxml = $this->xmlobj[2]->sub($handler[0]);
} elseif(is_array($this->xmlobj) and array_key_exists(2, $this->xmlobj)) { } elseif(is_array($this->xmlobj) and array_key_exists(2, $this->xmlobj)) {
$searchxml = $this->xmlobj[2]; $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($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; if($handler[3] === null) $handler[3] = $this;
$this->log->log("Calling {$handler[2]}", Logging::LOG_DEBUG); $this->log->log("Calling {$handler[2]}", XMPPHP_Log::LEVEL_DEBUG);
call_user_func(array($handler[3], $handler[2]), $this->xmlobj[2]); call_user_func(array($handler[3], $handler[2]), $this->xmlobj[2]);
} }
} }
@ -268,7 +441,7 @@ class XMLStream {
} }
if(is_array($this->xmlobj)) { if(is_array($this->xmlobj)) {
$this->xmlobj = array_slice($this->xmlobj, 0, 1); $this->xmlobj = array_slice($this->xmlobj, 0, 1);
if(isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMLObj) { if(isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMPPHP_XMLObj) {
$this->xmlobj[0]->subs = null; $this->xmlobj[0]->subs = null;
} }
} }
@ -290,24 +463,26 @@ class XMLStream {
} }
} }
public function doReconnect() { /**
if(!$this->is_server) { * XML character callback
$this->log->log("Reconnecting...", Logging::LOG_WARNING); * @see xml_set_character_data_handler
$this->connect(false, false); *
$this->reset(); * @param resource $parser
} * @param string $data
*/
protected function charXML($parser, $data) {
if(array_key_exists($this->xml_depth, $this->xmlobj))
$this->xmlobj[$this->xml_depth]->data .= $data;
} }
public function disconnect() { /**
$this->reconnect = false; * Event?
$this->send($this->stream_end); *
$this->sent_disconnect = true; * @param string $name
$this->processUntil('end_stream', 5); * @param string $payload
$this->disconnected = true; */
} protected function event($name, $payload = null) {
$this->log->log("EVENT: $name", XMPPHP_Log::LEVEL_DEBUG);
public function event($name, $payload = null) {
$this->log->log("EVENT: $name", Logging::LOG_DEBUG);
foreach($this->eventhandlers as $handler) { foreach($this->eventhandlers as $handler) {
if($name == $handler[0]) { if($name == $handler[0]) {
if($handler[2] === null) $handler[2] = $this; if($handler[2] === null) $handler[2] = $this;
@ -324,18 +499,38 @@ class XMLStream {
} }
} }
public function charXML($parser, $data) { /**
if(array_key_exists($this->xml_depth, $this->xmlobj)) * Read from socket
$this->xmlobj[$this->xml_depth]->data .= $data; */
protected 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", XMPPHP_Log::LEVEL_VERBOSE);
xml_parse($this->parser, $buff, false);
} }
public function send($msg) { /**
* Send to socket
*
* @param string $msg
*/
protected function send($msg) {
#socket_write($this->socket, $msg); #socket_write($this->socket, $msg);
$this->log->log("SENT: $msg", Logging::LOG_VERBOSE); $this->log->log("SENT: $msg", XMPPHP_Log::LEVEL_VERBOSE);
@fwrite($this->socket, $msg); @fwrite($this->socket, $msg);
} }
public function reset() { /**
* Reset connection
*/
protected function reset() {
$this->xml_depth = 0; $this->xml_depth = 0;
unset($this->xmlobj); unset($this->xmlobj);
$this->xmlobj = array(); $this->xmlobj = array();
@ -346,10 +541,13 @@ class XMLStream {
$this->been_reset = true; $this->been_reset = true;
} }
public function setupParser() { /**
* Setup the XML parser
*/
protected function setupParser() {
$this->parser = xml_parser_create('UTF-8'); $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_SKIP_WHITE, 1);
xml_parser_set_option($this->parser,XML_OPTION_TARGET_ENCODING, "UTF-8"); 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');

View File

@ -18,9 +18,10 @@ You should have received a copy of the GNU General Public License
along with XMPPHP; if not, write to the Free Software along with XMPPHP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
require_once("xmlstream.php");
class XMPP extends XMLStream { require_once("XMLStream.php");
class XMPPHP_XMPP extends XMPPHP_XMLStream {
protected $server; protected $server;
protected $user; protected $user;
protected $password; protected $password;
@ -42,50 +43,59 @@ class XMPP extends XMLStream {
*/ */
public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) { public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) {
parent::__construct($host, $port, $printlog, $loglevel); parent::__construct($host, $port, $printlog, $loglevel);
$this->user = $user; $this->user = $user;
$this->password = $password; $this->password = $password;
$this->resource = $resource; $this->resource = $resource;
if(!$server) $server = $host; if(!$server) $server = $host;
$this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">'; $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
$this->stream_end = '</stream:stream>'; $this->stream_end = '</stream:stream>';
$this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler'); $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('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler');
$this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler'); $this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler');
$this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler'); $this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler');
$this->default_ns = 'jabber:client';
$this->addHandler('message', 'jabber:client', 'message_handler'); $this->addHandler('message', 'jabber:client', 'message_handler');
$this->addHandler('presence', 'jabber:client', 'presence_handler'); $this->addHandler('presence', 'jabber:client', 'presence_handler');
$this->default_ns = 'jabber:client';
$this->authed = false; $this->authed = false;
$this->use_encryption = true; $this->use_encryption = true;
} }
public function message_handler($xml) { /**
if(isset($xml->attrs['type'])) { * Send XMPP Message
$payload['type'] = $xml->attrs['type']; *
} else { * @param string $to
$payload['type'] = 'chat'; * @param string $body
} * @param string $type
$payload['from'] = $xml->attrs['from']; * @param string $subject
$payload['body'] = $xml->sub('body')->data; */
$this->log->log("Message: {$xml->sub('body')->data}", Logging::LOG_DEBUG);
$this->event('message', $payload);
}
public function message($to, $body, $type = 'chat', $subject = null) { public function message($to, $body, $type = 'chat', $subject = null) {
$to = htmlspecialchars($to); $to = htmlspecialchars($to);
$body = htmlspecialchars($body); $body = htmlspecialchars($body);
$subject = htmlspecialchars($subject); $subject = htmlspecialchars($subject);
$out = "<message from='{$this->fulljid}' to='$to' type='$type'>"; $out = "<message from='{$this->fulljid}' to='$to' type='$type'>";
if($subject) $out .= "<subject>$subject</subject>"; if($subject) $out .= "<subject>$subject</subject>";
$out .= "<body>$body</body></message>"; $out .= "<body>$body</body></message>";
$this->send($out); $this->send($out);
} }
/**
* Set Presence
*
* @param string $status
* @param string $show
* @param string $to
*/
public function presence($status = null, $show = 'available', $to = null) { public function presence($status = null, $show = 'available', $to = null) {
$type = ''; $type = '';
$to = htmlspecialchars($to); $to = htmlspecialchars($to);
$status = htmlspecialchars($status); $status = htmlspecialchars($status);
if($show == 'unavailable') $type = 'unavailable'; if($show == 'unavailable') $type = 'unavailable';
$out = "<presence"; $out = "<presence";
if($to) $out .= " to='$to'"; if($to) $out .= " to='$to'";
if($type) $out .= " type='$type'"; if($type) $out .= " type='$type'";
@ -97,22 +107,50 @@ class XMPP extends XMLStream {
if($status) $out .= "<status>$status</status>"; if($status) $out .= "<status>$status</status>";
$out .= "</presence>"; $out .= "</presence>";
} }
$this->send($out); $this->send($out);
} }
/**
* Message handler
*
* @param string $xml
*/
public function message_handler($xml) {
if(isset($xml->attrs['type'])) {
$payload['type'] = $xml->attrs['type'];
} else {
$payload['type'] = 'chat';
}
$payload['from'] = $xml->attrs['from'];
$payload['body'] = $xml->sub('body')->data;
$this->log->log("Message: {$xml->sub('body')->data}", XMPPHP_Log::LEVEL_DEBUG);
$this->event('message', $payload);
}
/**
* Presence handler
*
* @param string $xml
*/
public function presence_handler($xml) { public function presence_handler($xml) {
$payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available'; $payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
$payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type']; $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
$payload['from'] = $xml->attrs['from']; $payload['from'] = $xml->attrs['from'];
$payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : ''; $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
$this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", Logging::LOG_DEBUG); $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG);
$this->event('presence', $payload); $this->event('presence', $payload);
} }
/**
* Features handler
*
* @param string $xml
*/
public function features_handler($xml) { public function features_handler($xml) {
if($xml->hassub('starttls') and $this->use_encryption) { if($xml->hasSub('starttls') and $this->use_encryption) {
$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>"); $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
} elseif($xml->hassub('bind')) { } elseif($xml->hasSub('bind')) {
$id = $this->getId(); $id = $this->getId();
$this->addIdHandler($id, 'resource_bind_handler'); $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>"); $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>");
@ -122,17 +160,32 @@ class XMPP extends XMLStream {
} }
} }
/**
* SASL success handler
*
* @param string $xml
*/
public function sasl_success_handler($xml) { public function sasl_success_handler($xml) {
$this->log->log("Auth success!"); $this->log->log("Auth success!");
$this->authed = true; $this->authed = true;
$this->reset(); $this->reset();
} }
/**
* SASL feature handler
*
* @param string $xml
*/
public function sasl_failure_handler($xml) { public function sasl_failure_handler($xml) {
$this->log->log("Auth failed!", Logging::LOG_ERROR); $this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR);
$this->disconnect(); $this->disconnect();
} }
/**
* Resource bind handler
*
* @param string $xml
*/
public function resource_bind_handler($xml) { public function resource_bind_handler($xml) {
if($xml->attrs['type'] == 'result') { if($xml->attrs['type'] == 'result') {
$this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data); $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
@ -143,11 +196,21 @@ class XMPP extends XMLStream {
$this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>"); $this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>");
} }
/**
* Session start handler
*
* @param string $xml
*/
public function session_start_handler($xml) { public function session_start_handler($xml) {
$this->log->log("Session started"); $this->log->log("Session started");
$this->event('session_start'); $this->event('session_start');
} }
/**
* TLS proceed handler
*
* @param string $xml
*/
public function tls_proceed_handler($xml) { public function tls_proceed_handler($xml) {
$this->log->log("Starting TLS encryption"); $this->log->log("Starting TLS encryption");
stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

View File

@ -3,10 +3,11 @@
// activate full error reporting // activate full error reporting
error_reporting(E_ALL & E_STRICT); error_reporting(E_ALL & E_STRICT);
include("xmpp.php"); include("XMPPHP/XMPP.php");
$conn = new XMPP('talk.google.com', 5222, 'user', 'password', 'xmpphp', 'gmail.com', $printlog=True, $loglevel=Logging::LOG_INFO); //$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'user', 'password', 'xmpphp', 'gmail.com', $printlog=true, $loglevel=Logging::LEVEL_INFO);
$conn = new XMPPHP_XMPP('jabber.wentz.it', 5222, 'dev', 'd3vd3v', 'xmpphp', 'jabber.wentz.it', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);
$conn->connect(); $conn->connect();
while(!$conn->disconnected) { while(!$conn->isDisconnected()) {
$payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start')); $payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start'));
foreach($payloads as $event) { foreach($payloads as $event) {
$pl = $event[1]; $pl = $event[1];
@ -25,10 +26,9 @@ while(!$conn->disconnected) {
print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n"; print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n";
break; break;
case 'session_start': case 'session_start':
print "Session Start\n";
$conn->presence($status="Cheese!"); $conn->presence($status="Cheese!");
break; break;
} }
} }
} }
?>

View File

@ -1,60 +0,0 @@
<?php
/*
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
*/
class Logging {
const LOG_ERROR = 0;
const LOG_WARNING = 1;
const LOG_INFO = 2;
const LOG_DEBUG = 3;
const LOG_VERBOSE = 4;
protected $data = array();
protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
protected $runlevel;
protected $printout;
/**
* Constructor
*
* @param boolean $printout
* @param string $runlevel
*/
public function __construct($printout = false, $runlevel = self::LOG_INFO) {
$this->runlevel = $runlevel;
$this->printout = $printout;
}
public function log($msg, $runlevel = null) {
if($runlevel === null) $runlevel = self::LOG_INFO;
$this->data[] = array($this->runlevel, $msg);
if($this->printout and $runlevel <= $this->runlevel) echo "{$this->names[$runlevel]}: $msg\n";
}
public function printout($clear = true, $runlevel = null) {
if($runlevel === null) $runlevel = $this->runlevel;
foreach($this->data as $data) {
if($runlevel <= $data[0]) echo "{$this->names[$runlevel]}: $data[1]\n";
}
if($clear) $this->data = array();
}
}

View File

@ -3,12 +3,12 @@
// activate full error reporting // activate full error reporting
error_reporting(E_ALL & E_STRICT); error_reporting(E_ALL & E_STRICT);
include("xmpp.php"); include("XMPPHP/XMPP.php");
$conn = new XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=False, $loglevel=Logging::LOG_INFO);
//$conn = new XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=false, $loglevel=Logging::LEVEL_INFO);
$conn = new XMPPHP_XMPP('jabber.wentz.it', 5222, 'dev', 'd3vd3v', 'xmpphp', 'jabber.wentz.it', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);
$conn->connect(); $conn->connect();
$conn->processUntil('session_start'); $conn->processUntil('session_start');
$conn->presence(); $conn->presence();
$conn->message('someguy@someserver.net', 'This is a test message!'); $conn->message('stephan@jabber.wentz.it', 'This is a test message!');
$conn->disconnect(); $conn->disconnect();
?>