migrated classes to php5, fixed issues #12 and #14, added full error_reporting to examples

git-svn-id: svn://netflint.net/xmpphp@24 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
temp 2008-04-28 22:05:31 +00:00
parent ef5ed5d3e1
commit ac78c821a0
6 changed files with 201 additions and 154 deletions

View File

@ -1,4 +1,8 @@
<?php <?php
// activate full error reporting
error_reporting(E_ALL & E_STRICT);
include("xmpp.php"); include("xmpp.php");
$conn = new XMPP('talk.google.com', 5222, 'user', 'password', 'xmpphp', 'gmail.com', $printlog=True, $loglevel=LOGGING_INFO); $conn = new XMPP('talk.google.com', 5222, 'user', 'password', 'xmpphp', 'gmail.com', $printlog=True, $loglevel=LOGGING_INFO);
$conn->connect(); $conn->connect();

View File

@ -19,34 +19,41 @@ 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
*/ */
define('LOGGING_ERROR', 0);
define('LOGGING_WARNING', 1);
define('LOGGING_INFO', 2);
define('LOGGING_DEBUG', 3);
define('LOGGING_VERBOSE', 4);
class Logging { class Logging {
var $data = array();
var $names = array(); const LOG_ERROR = 0;
var $runlevel; const LOG_WARNING = 1;
var $printout; 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;
function Logging($printout = False, $runlevel=LOGGING_INFO) { /**
$this->names = array('ERROR ', 'WARNING', 'INFO ', 'DEBUG ', 'VERBOSE'); * Constructor
*
* @param boolean $printout
* @param string $runlevel
*/
public function __construct($printout = false, $runlevel = self::LOG_INFO) {
$this->runlevel = $runlevel; $this->runlevel = $runlevel;
$this->printout = $printout; $this->printout = $printout;
} }
function log($msg, $runlevel=Null) { public function log($msg, $runlevel = null) {
if($runlevel === Null) $runlevel = LOGGING_INFO; if($runlevel === null) $runlevel = self::LOG_INFO;
$data[] = array($this->runlevel, $msg); $this->data[] = array($this->runlevel, $msg);
if($this->printout and $runlevel <= $this->runlevel) print "{$this->names[$runlevel]}: $msg\n"; if($this->printout and $runlevel <= $this->runlevel) echo "{$this->names[$runlevel]}: $msg\n";
} }
function printout($clear=True, $runlevel=Null) { public function printout($clear = true, $runlevel = null) {
if(!$runlevel) $runlevel = $this->runlevel; if($runlevel === null) $runlevel = $this->runlevel;
foreach($this->data as $data) { foreach($this->data as $data) {
if($runlevel <= $data[0]) print "{$this->names[$runlevel]}: $data[1]\n"; if($runlevel <= $data[0]) echo "{$this->names[$runlevel]}: $data[1]\n";
} }
if($clear) $this->data = array(); if($clear) $this->data = array();
} }

View File

@ -1,4 +1,8 @@
<?php <?php
// activate full error reporting
error_reporting(E_ALL & E_STRICT);
include("xmpp.php"); include("xmpp.php");
$conn = new XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=False, $loglevel=LOGGING_INFO); $conn = new XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=False, $loglevel=LOGGING_INFO);
$conn->connect(); $conn->connect();

View File

@ -20,16 +20,24 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
class XMLObj { class XMLObj {
var $name; public $name;
var $ns; public $ns;
var $attrs = array(); public $attrs = array();
var $subs = array(); public $subs = array();
var $data = ''; public $data = '';
function XMLObj($name, $ns='', $attrs=array(), $data='') { /**
* Constructor
*
* @param string $name
* @param string $ns
* @param array $attrs
* @param string $data
*/
public function __construct($name, $ns = '', $attrs = array(), $data = '') {
$this->name = strtolower($name); $this->name = strtolower($name);
$this->ns = $ns; $this->ns = $ns;
if(is_array($attrs)) { if(is_array($attrs) && count($attrs)) {
foreach($attrs as $key => $value) { foreach($attrs as $key => $value) {
$this->attrs[strtolower($key)] = $value; $this->attrs[strtolower($key)] = $value;
} }
@ -37,15 +45,15 @@ class XMLObj {
$this->data = $data; $this->data = $data;
} }
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";
foreach($this->subs as $sub) { foreach($this->subs as $sub) {
$sub->printobj($depth + 1); $sub->printObj($depth + 1);
} }
} }
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) {
if($key != 'xmlns') { if($key != 'xmlns') {
@ -55,21 +63,21 @@ class XMLObj {
} }
$str .= ">"; $str .= ">";
foreach($this->subs as $sub) { foreach($this->subs as $sub) {
$str .= $sub->tostring(); $str .= $sub->toString();
} }
$body = htmlspecialchars($this->data); $body = htmlspecialchars($this->data);
$str .= "$body</{$this->name}>"; $str .= "$body</{$this->name}>";
return $str; return $str;
} }
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;
} }
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

@ -22,33 +22,42 @@ require_once("xmlobj.php");
require_once("logging.php"); require_once("logging.php");
class XMLStream { class XMLStream {
var $socket; protected $socket;
var $parser; protected $parser;
var $buffer; protected $buffer;
var $xml_depth = 0; protected $xml_depth = 0;
var $host; protected $host;
var $port; protected $port;
var $stream_start = '<stream>'; protected $stream_start = '<stream>';
var $stream_end = '</stream'; protected $stream_end = '</stream';
var $disconnected = false; public $disconnected = false;
var $sent_disconnect = False; protected $sent_disconnect = false;
var $ns_map = array(); protected $ns_map = array();
var $current_ns = array(); protected $current_ns = array();
var $xmlobj = Null; protected $xmlobj = null;
var $nshandlers = array(); protected $nshandlers = array();
var $idhandlers = array(); protected $idhandlers = array();
var $eventhandlers = array(); protected $eventhandlers = array();
var $lastid = 0; protected $lastid = 0;
var $default_ns; protected $default_ns;
var $until = ''; protected $until = '';
var $until_happened = False; protected $until_happened = false;
var $until_payload = array(); protected $until_payload = array();
var $log; protected $log;
var $reconnect = True; protected $reconnect = true;
var $been_reset = False; protected $been_reset = false;
var $is_server; 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->reconnect = !$is_server;
$this->is_server = $is_server; $this->is_server = $is_server;
$this->host = $host; $this->host = $host;
@ -57,51 +66,51 @@ class XMLStream {
$this->log = new Logging($log, $loglevel); $this->log = new Logging($log, $loglevel);
} }
function getId() { public function getId() {
$this->lastid++; $this->lastid++;
return $this->lastid; return $this->lastid;
} }
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);
} }
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);
} }
function addEventHandler($name, $pointer, $obj) { public function addEventHandler($name, $pointer, $obj) {
$this->eventhanders[] = array($name, $pointer, $obj); $this->eventhanders[] = array($name, $pointer, $obj);
} }
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;
if($persistent) { if($persistent) {
$conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT; $conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
} else { } else {
$conflag = STREAM_CLIENT_CONNECT; $conflag = STREAM_CLIENT_CONNECT;
} }
$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}", $flags=$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_ERROR); $this->log->log("Could not connect.", Logging::LOG_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);
} }
function apply_socket($socket) { public function apply_socket($socket) {
$this->socket = $socket; $this->socket = $socket;
} }
function process() { public function process() {
$updated = ''; $updated = '';
while(!$this->disconnect) { while(!$this->disconnect) {
$read = array($this->socket); $read = array($this->socket);
$write = NULL; $write = null;
$except = NULL; $except = null;
$updated = stream_select($read, $write, $except, 1); $updated = stream_select($read, $write, $except, 1);
if ($updated > 0) { if ($updated > 0) {
$buff = @fread($this->socket, 1024); $buff = @fread($this->socket, 1024);
@ -110,36 +119,36 @@ class XMLStream {
$this->doReconnect(); $this->doReconnect();
} else { } else {
fclose($this->socket); fclose($this->socket);
return False; return false;
} }
} }
$this->log->log("RECV: $buff", LOGGING_VERBOSE); $this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
xml_parse($this->parser, $buff, False); xml_parse($this->parser, $buff, false);
} }
} }
} }
function read() { public function read() {
$buff = @fread($this->socket, 1024); $buff = @fread($this->socket, 1024);
if(!$buff) { if(!$buff) {
if($this->reconnect) { if($this->reconnect) {
$this->doReconnect(); $this->doReconnect();
} else { } else {
fclose($this->socket); fclose($this->socket);
return False; return false;
} }
} }
$this->log->log("RECV: $buff", LOGGING_VERBOSE); $this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
xml_parse($this->parser, $buff, False); xml_parse($this->parser, $buff, false);
} }
function processTime($timeout=-1) { public function processTime($timeout = -1) {
$start = time(); $start = time();
$updated = ''; $updated = '';
while(!$this->disconnected and ($timeout == -1 or time() - $start < $timeout)) { while(!$this->disconnected and ($timeout == -1 or time() - $start < $timeout)) {
$read = array($this->socket); $read = array($this->socket);
$write = NULL; $write = null;
$except = NULL; $except = null;
$updated = stream_select($read, $write, $except, 1); $updated = stream_select($read, $write, $except, 1);
if ($updated > 0) { if ($updated > 0) {
$buff = @fread($this->socket, 1024); $buff = @fread($this->socket, 1024);
@ -148,16 +157,16 @@ class XMLStream {
$this->doReconnect(); $this->doReconnect();
} else { } else {
fclose($this->socket); fclose($this->socket);
return False; return false;
} }
} }
$this->log->log("RECV: $buff", LOGGING_VERBOSE); $this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
xml_parse($this->parser, $buff, False); xml_parse($this->parser, $buff, false);
} }
} }
} }
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);
$this->until[] = $event; $this->until[] = $event;
@ -167,8 +176,8 @@ class XMLStream {
$updated = ''; $updated = '';
while(!$this->disconnected and $this->until[$event_key] 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;
$updated = stream_select($read, $write, $except, 1); $updated = stream_select($read, $write, $except, 1);
if ($updated > 0) { if ($updated > 0) {
$buff = @fread($this->socket, 1024); $buff = @fread($this->socket, 1024);
@ -177,11 +186,11 @@ class XMLStream {
$this->doReconnect(); $this->doReconnect();
} else { } else {
fclose($this->socket); fclose($this->socket);
return False; return false;
} }
} }
$this->log->log("RECV: $buff", LOGGING_VERBOSE); $this->log->log("RECV: $buff", Logging::LOG_VERBOSE);
xml_parse($this->parser, $buff, False); xml_parse($this->parser, $buff, false);
} }
} }
if(array_key_exists($event_key, $this->until_payload)) { if(array_key_exists($event_key, $this->until_payload)) {
@ -193,9 +202,9 @@ class XMLStream {
return $payload; return $payload;
} }
function startXML($parser, $name, $attr) { public 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;
} }
$this->xml_depth++; $this->xml_depth++;
@ -213,7 +222,7 @@ class XMLStream {
$this->ns_map[$key] = $value; $this->ns_map[$key] = $value;
} }
} }
if(!strstr($name, ":") === False) if(!strstr($name, ":") === false)
{ {
$name = explode(':', $name); $name = explode(':', $name);
$ns = $this->ns_map[$name[0]]; $ns = $this->ns_map[$name[0]];
@ -225,33 +234,33 @@ class XMLStream {
$this->xmlobj[$this->xml_depth] = $obj; $this->xmlobj[$this->xml_depth] = $obj;
} }
function endXML($parser, $name) { public function endXML($parser, $name) {
#$this->log->log("Ending $name", LOGGING_DEBUG); #$this->log->log("Ending $name", Logging::LOG_DEBUG);
#print "$name\n"; #print "$name\n";
if($this->been_reset) { if($this->been_reset) {
$this->been_reset = False; $this->been_reset = false;
$this->xml_depth = 0; $this->xml_depth = 0;
} }
$this->xml_depth--; $this->xml_depth--;
if($this->xml_depth == 1) { if($this->xml_depth == 1) {
#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_DEBUG); $this->log->log("Calling {$handler[2]}", Logging::LOG_DEBUG);
call_user_method($handler[2], $handler[3], $this->xmlobj[2]); call_user_func(array($handler[3], $handler[2]), $this->xmlobj[2]);
} }
} }
foreach($this->idhandlers as $id => $handler) { foreach($this->idhandlers as $id => $handler) {
if(array_key_exists('id', $this->xmlobj[2]->attrs) and $this->xmlobj[2]->attrs['id'] == $id) { if(array_key_exists('id', $this->xmlobj[2]->attrs) and $this->xmlobj[2]->attrs['id'] == $id) {
if($handler[1] === Null) $handler[1] = $this; if($handler[1] === null) $handler[1] = $this;
call_user_method($handler[0], $handler[1], $this->xmlobj[2]); call_user_func(array($handler[1], $handler[0]), $this->xmlobj[2]);
#id handlers are only used once #id handlers are only used once
unset($this->idhandlers[$id]); unset($this->idhandlers[$id]);
break; break;
@ -259,7 +268,9 @@ 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);
$this->xmlobj[0]->subs = Null; if($this->xmlobj[0] instanceof XMLObj) {
$this->xmlobj[0]->subs = null;
}
} }
unset($this->xmlobj[2]); unset($this->xmlobj[2]);
} }
@ -268,8 +279,8 @@ class XMLStream {
if(!$this->sent_disconnect) { if(!$this->sent_disconnect) {
$this->send($this->stream_end); $this->send($this->stream_end);
} }
$this->disconnected = True; $this->disconnected = true;
$this->sent_disconnect = True; $this->sent_disconnect = true;
fclose($this->socket); fclose($this->socket);
if($this->reconnect) { if($this->reconnect) {
$this->doReconnect(); $this->doReconnect();
@ -279,27 +290,27 @@ class XMLStream {
} }
} }
function doReconnect() { public function doReconnect() {
if(!$this->is_server) { if(!$this->is_server) {
$this->log->log("Reconnecting...", LOGGING_WARNING); $this->log->log("Reconnecting...", Logging::LOG_WARNING);
$this->connect(False, False); $this->connect(false, false);
$this->reset(); $this->reset();
} }
} }
function disconnect() { public function disconnect() {
$this->reconnect = False; $this->reconnect = false;
$this->send($this->stream_end); $this->send($this->stream_end);
$this->sent_disconnect = True; $this->sent_disconnect = true;
$this->processUntil('end_stream', 5); $this->processUntil('end_stream', 5);
$this->disconnected = True; $this->disconnected = true;
} }
function event($name, $payload=Null) { public function event($name, $payload = null) {
$this->log->log("EVENT: $name", LOGGING_DEBUG); $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;
call_user_method($handler[1], $handler[2], $payload); call_user_method($handler[1], $handler[2], $payload);
} }
} }
@ -307,24 +318,24 @@ class XMLStream {
if(is_array($until)) { if(is_array($until)) {
if(in_array($name, $until)) { if(in_array($name, $until)) {
$this->until_payload[$key][] = array($name, $payload); $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)) if(array_key_exists($this->xml_depth, $this->xmlobj))
$this->xmlobj[$this->xml_depth]->data .= $data; $this->xmlobj[$this->xml_depth]->data .= $data;
} }
function send($msg) { public function send($msg) {
#socket_write($this->socket, $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); @fwrite($this->socket, $msg);
} }
function reset() { public function reset() {
$this->xml_depth = 0; $this->xml_depth = 0;
unset($this->xmlobj); unset($this->xmlobj);
$this->xmlobj = array(); $this->xmlobj = array();
@ -332,10 +343,10 @@ class XMLStream {
if(!$this->is_server) { if(!$this->is_server) {
$this->send($this->stream_start); $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'); $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");

View File

@ -18,20 +18,30 @@ 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("xmlstream.php"); require_once("xmlstream.php");
require_once("logging.php");
class XMPP extends XMLStream { class XMPP extends XMLStream {
var $server; protected $server;
var $user; protected $user;
var $password; protected $password;
var $resource; protected $resource;
var $fulljid; protected $fulljid;
var $authed; protected $authed;
function XMPP($host, $port, $user, $password, $resource, $server=Null, $printlog=False, $loglevel=Null) { /**
$this->XMLStream($host, $port, $printlog, $loglevel); * Constructor
*
* @param string $host
* @param integer $port
* @param string $user
* @param string $password
* @param string $resource
* @param string $server
* @param boolean $printlog
* @param string $loglevel
*/
public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) {
parent::__construct($host, $port, $printlog, $loglevel);
$this->user = $user; $this->user = $user;
$this->password = $password; $this->password = $password;
$this->resource = $resource; $this->resource = $resource;
@ -45,20 +55,23 @@ class XMPP extends XMLStream {
$this->default_ns = 'jabber:client'; $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->authed = False; $this->authed = false;
$this->use_encryption = True; $this->use_encryption = true;
} }
function message_handler($xml) { public function message_handler($xml) {
$payload['type'] = $xml->attrs['type']; if(isset($xml->attrs['type'])) {
if(!$payload['type']) $payload['type'] = 'chat'; $payload['type'] = $xml->attrs['type'];
} else {
$payload['type'] = 'chat';
}
$payload['from'] = $xml->attrs['from']; $payload['from'] = $xml->attrs['from'];
$payload['body'] = $xml->sub('body')->data; $payload['body'] = $xml->sub('body')->data;
$this->log->log("Message: {$xml->sub('body')->data}", LOGGING_DEBUG); $this->log->log("Message: {$xml->sub('body')->data}", Logging::LOG_DEBUG);
$this->event('message', $payload); $this->event('message', $payload);
} }
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);
@ -68,7 +81,7 @@ class XMPP extends XMLStream {
$this->send($out); $this->send($out);
} }
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);
@ -87,16 +100,16 @@ class XMPP extends XMLStream {
$this->send($out); $this->send($out);
} }
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_DEBUG); $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", Logging::LOG_DEBUG);
$this->event('presence', $payload); $this->event('presence', $payload);
} }
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')) {
@ -109,18 +122,18 @@ class XMPP extends XMLStream {
} }
} }
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();
} }
function sasl_failure_handler($xml) { public function sasl_failure_handler($xml) {
$this->log->log("Auth failed!", LOGGING_ERROR); $this->log->log("Auth failed!", Logging::LOG_ERROR);
$this->disconnect(); $this->disconnect();
} }
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);
$this->fulljid = $xml->sub('bind')->sub('jid')->data; $this->fulljid = $xml->sub('bind')->sub('jid')->data;
@ -130,14 +143,14 @@ 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>");
} }
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');
} }
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);
$this->reset(); $this->reset();
} }
} }