space fixes, added timeout to connect(), general fixes
git-svn-id: svn://netflint.net/xmpphp@39 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
parent
1a4d1756c1
commit
b5aa1b77b0
@ -45,72 +45,72 @@ class XMPPHP_Log {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data = array();
|
||||
protected $data = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
|
||||
protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $runlevel;
|
||||
protected $runlevel;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $printout;
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param boolean $printout
|
||||
* @param string $runlevel
|
||||
*/
|
||||
public function __construct($printout = false, $runlevel = self::LEVEL_INFO) {
|
||||
$this->printout = (boolean)$printout;
|
||||
$this->runlevel = (int)$runlevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param string $msg
|
||||
* @param integer $runlevel
|
||||
*/
|
||||
public function log($msg, $runlevel = self::LEVEL_INFO) {
|
||||
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->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
|
||||
*
|
||||
* @param boolean $clear
|
||||
* @param integer $runlevel
|
||||
*/
|
||||
public function printout($clear = true, $runlevel = null) {
|
||||
if($runlevel === null) {
|
||||
public function printout($clear = true, $runlevel = null) {
|
||||
if($runlevel === null) {
|
||||
$runlevel = $this->runlevel;
|
||||
}
|
||||
foreach($this->data as $data) {
|
||||
if($runlevel <= $data[0]) {
|
||||
foreach($this->data as $data) {
|
||||
if($runlevel <= $data[0]) {
|
||||
$this->writeLine($data[1], $runlevel, $data[2]);
|
||||
}
|
||||
}
|
||||
if($clear) {
|
||||
}
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
@ -41,89 +41,89 @@ class XMPPHP_XMLObj {
|
||||
*
|
||||
* @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 = '';
|
||||
|
||||
/**
|
||||
* 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->ns = $ns;
|
||||
if(is_array($attrs) && count($attrs)) {
|
||||
foreach($attrs as $key => $value) {
|
||||
$this->attrs[strtolower($key)] = $value;
|
||||
}
|
||||
}
|
||||
$this->data = $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->ns = $ns;
|
||||
if(is_array($attrs) && count($attrs)) {
|
||||
foreach($attrs as $key => $value) {
|
||||
$this->attrs[strtolower($key)] = $value;
|
||||
}
|
||||
}
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump this XML Object to output.
|
||||
*
|
||||
* @param integer $depth
|
||||
*/
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this XML Object in xml notation
|
||||
*
|
||||
* @param string $str
|
||||
*/
|
||||
public function toString($str = '') {
|
||||
$str .= "<{$this->name} xmlns='{$this->ns}' ";
|
||||
foreach($this->attrs as $key => $value) {
|
||||
if($key != 'xmlns') {
|
||||
$value = htmlspecialchars($value);
|
||||
$str .= "$key='$value' ";
|
||||
}
|
||||
}
|
||||
$str .= ">";
|
||||
foreach($this->subs as $sub) {
|
||||
$str .= $sub->toString();
|
||||
}
|
||||
$body = htmlspecialchars($this->data);
|
||||
$str .= "$body</{$this->name}>";
|
||||
return $str;
|
||||
}
|
||||
public function toString($str = '') {
|
||||
$str .= "<{$this->name} xmlns='{$this->ns}' ";
|
||||
foreach($this->attrs as $key => $value) {
|
||||
if($key != 'xmlns') {
|
||||
$value = htmlspecialchars($value);
|
||||
$str .= "$key='$value' ";
|
||||
}
|
||||
}
|
||||
$str .= ">";
|
||||
foreach($this->subs as $sub) {
|
||||
$str .= $sub->toString();
|
||||
}
|
||||
$body = htmlspecialchars($this->data);
|
||||
$str .= "$body</{$this->name}>";
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has this XML Object the given sub?
|
||||
@ -131,12 +131,12 @@ class XMPPHP_XMLObj {
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasSub($name) {
|
||||
foreach($this->subs as $sub) {
|
||||
if($sub->name == $name) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function hasSub($name) {
|
||||
foreach($this->subs as $sub) {
|
||||
if($sub->name == $name) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a sub
|
||||
@ -145,11 +145,11 @@ class XMPPHP_XMLObj {
|
||||
* @param string $attrs
|
||||
* @param string $ns
|
||||
*/
|
||||
public function sub($name, $attrs = null, $ns = null) {
|
||||
foreach($this->subs as $sub) {
|
||||
if($sub->name == $name) {
|
||||
public function sub($name, $attrs = null, $ns = null) {
|
||||
foreach($this->subs as $sub) {
|
||||
if($sub->name == $name) {
|
||||
return $sub;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,17 +45,17 @@ require_once 'Log.php';
|
||||
* @version $Id$
|
||||
*/
|
||||
class XMPPHP_XMLStream {
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
protected $socket;
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
protected $parser;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $buffer;
|
||||
/**
|
||||
* @var integer
|
||||
@ -65,7 +65,7 @@ class XMPPHP_XMLStream {
|
||||
* @var string
|
||||
*/
|
||||
protected $host;
|
||||
/**
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $port;
|
||||
@ -100,80 +100,80 @@ class XMPPHP_XMLStream {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $nshandlers = array();
|
||||
protected $nshandlers = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $idhandlers = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $eventhandlers = array();
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $lastid = 0;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $default_ns;
|
||||
protected $idhandlers = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $eventhandlers = array();
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $lastid = 0;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $until = '';
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $until_happened = false;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $until_payload = array();
|
||||
/**
|
||||
* @var XMPPHP_Log
|
||||
*/
|
||||
protected $log;
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $reconnect = true;
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $been_reset = false;
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $is_server;
|
||||
protected $default_ns;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $until = '';
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $until_happened = false;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $until_payload = array();
|
||||
/**
|
||||
* @var XMPPHP_Log
|
||||
*/
|
||||
protected $log;
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $reconnect = true;
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $been_reset = false;
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $is_server;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $host
|
||||
* @param string $port
|
||||
* @param boolean $printlog
|
||||
* @param string $loglevel
|
||||
* @param boolean $is_server
|
||||
*/
|
||||
public function __construct($host = null, $port = null, $printlog = false, $loglevel = null, $is_server = false) {
|
||||
$this->reconnect = !$is_server;
|
||||
$this->is_server = $is_server;
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->setupParser();
|
||||
$this->log = new XMPPHP_Log($printlog, $loglevel);
|
||||
}
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $host
|
||||
* @param string $port
|
||||
* @param boolean $printlog
|
||||
* @param string $loglevel
|
||||
* @param boolean $is_server
|
||||
*/
|
||||
public function __construct($host = null, $port = null, $printlog = false, $loglevel = null, $is_server = false) {
|
||||
$this->reconnect = !$is_server;
|
||||
$this->is_server = $is_server;
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->setupParser();
|
||||
$this->log = new XMPPHP_Log($printlog, $loglevel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
* Cleanup connection
|
||||
*/
|
||||
public function __destruct() {
|
||||
if(!$this->disconnected) {
|
||||
$this->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
* Cleanup connection
|
||||
*/
|
||||
public function __destruct() {
|
||||
if(!$this->disconnected && $this->socket) {
|
||||
$this->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the log instance
|
||||
*
|
||||
@ -188,21 +188,21 @@ class XMPPHP_XMLStream {
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getId() {
|
||||
$this->lastid++;
|
||||
return $this->lastid;
|
||||
}
|
||||
public function getId() {
|
||||
$this->lastid++;
|
||||
return $this->lastid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add ID Handler
|
||||
*
|
||||
* @param integer $id
|
||||
* @param string $pointer
|
||||
* @param string $obj
|
||||
*/
|
||||
public function addIdHandler($id, $pointer, $obj = null) {
|
||||
$this->idhandlers[$id] = array($pointer, $obj);
|
||||
}
|
||||
/**
|
||||
* Add ID Handler
|
||||
*
|
||||
* @param integer $id
|
||||
* @param string $pointer
|
||||
* @param string $obj
|
||||
*/
|
||||
public function addIdHandler($id, $pointer, $obj = null) {
|
||||
$this->idhandlers[$id] = array($pointer, $obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Handler
|
||||
@ -213,9 +213,9 @@ class XMPPHP_XMLStream {
|
||||
* @param string $obj
|
||||
* @param integer $depth
|
||||
*/
|
||||
public function addHandler($name, $ns, $pointer, $obj = null, $depth = 1) {
|
||||
$this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
|
||||
}
|
||||
public function addHandler($name, $ns, $pointer, $obj = null, $depth = 1) {
|
||||
$this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Evemt Handler
|
||||
@ -224,35 +224,40 @@ class XMPPHP_XMLStream {
|
||||
* @param string $pointer
|
||||
* @param string $obj
|
||||
*/
|
||||
public function addEventHandler($name, $pointer, $obj) {
|
||||
$this->eventhanders[] = array($name, $pointer, $obj);
|
||||
}
|
||||
public function addEventHandler($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) {
|
||||
$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}", $conflag);
|
||||
if(!$this->socket) {
|
||||
$this->log->log("Could not connect.", XMPPHP_Log::LEVEL_ERROR);
|
||||
$this->disconnected = true;
|
||||
|
||||
throw new XMPPHP_Exception('Could not connect.');
|
||||
}
|
||||
stream_set_blocking($this->socket, 1);
|
||||
if($sendinit) $this->send($this->stream_start);
|
||||
}
|
||||
/**
|
||||
* Connect to XMPP Host
|
||||
*
|
||||
* @param integer $timeout
|
||||
* @param boolean $persistent
|
||||
* @param boolean $sendinit
|
||||
*/
|
||||
public function connect($timeout = 30, $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}");
|
||||
try {
|
||||
$this->socket = @stream_socket_client("tcp://{$this->host}:{$this->port}", $errno, $errstr, $timeout, $conflag);
|
||||
} catch (Exception $e) {
|
||||
throw new XMPPHP_Exception($e->getMessage());
|
||||
}
|
||||
if(!$this->socket) {
|
||||
$this->log->log("Could not connect.", XMPPHP_Log::LEVEL_ERROR);
|
||||
$this->disconnected = true;
|
||||
|
||||
throw new XMPPHP_Exception('Could not connect.');
|
||||
}
|
||||
stream_set_blocking($this->socket, 1);
|
||||
if($sendinit) $this->send($this->stream_start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconnect XMPP Host
|
||||
@ -286,106 +291,106 @@ class XMPPHP_XMLStream {
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process() {
|
||||
$updated = '';
|
||||
while(!$this->disconnect) {
|
||||
$read = array($this->socket);
|
||||
$write = null;
|
||||
$except = null;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Process
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process() {
|
||||
$updated = '';
|
||||
while(!$this->disconnect) {
|
||||
$read = array($this->socket);
|
||||
$write = null;
|
||||
$except = null;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process until a timeout occurs
|
||||
*
|
||||
* @param integer $timeout
|
||||
* @return string
|
||||
*/
|
||||
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;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Process until a timeout occurs
|
||||
*
|
||||
* @param integer $timeout
|
||||
* @return string
|
||||
*/
|
||||
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;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process until a specified event or a timeout occurs
|
||||
*
|
||||
* @param string|array $event
|
||||
* @param integer $timeout
|
||||
* @return string
|
||||
*/
|
||||
public function processUntil($event, $timeout=-1) {
|
||||
$start = time();
|
||||
if(!is_array($event)) $event = array($event);
|
||||
$this->until[] = $event;
|
||||
end($this->until);
|
||||
$event_key = key($this->until);
|
||||
reset($this->until);
|
||||
$updated = '';
|
||||
while(!$this->disconnected and $this->until[$event_key] and (time() - $start < $timeout or $timeout == -1)) {
|
||||
$read = array($this->socket);
|
||||
$write = null;
|
||||
$except = null;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
if(array_key_exists($event_key, $this->until_payload)) {
|
||||
$payload = $this->until_payload[$event_key];
|
||||
} else {
|
||||
$payload = array();
|
||||
}
|
||||
unset($this->until_payload[$event_key]);
|
||||
return $payload;
|
||||
}
|
||||
/**
|
||||
* Process until a specified event or a timeout occurs
|
||||
*
|
||||
* @param string|array $event
|
||||
* @param integer $timeout
|
||||
* @return string
|
||||
*/
|
||||
public function processUntil($event, $timeout=-1) {
|
||||
$start = time();
|
||||
if(!is_array($event)) $event = array($event);
|
||||
$this->until[] = $event;
|
||||
end($this->until);
|
||||
$event_key = key($this->until);
|
||||
reset($this->until);
|
||||
$updated = '';
|
||||
while(!$this->disconnected and $this->until[$event_key] and (time() - $start < $timeout or $timeout == -1)) {
|
||||
$read = array($this->socket);
|
||||
$write = null;
|
||||
$except = null;
|
||||
$updated = stream_select($read, $write, $except, 1);
|
||||
if ($updated > 0) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
if(array_key_exists($event_key, $this->until_payload)) {
|
||||
$payload = $this->until_payload[$event_key];
|
||||
} else {
|
||||
$payload = array();
|
||||
}
|
||||
unset($this->until_payload[$event_key]);
|
||||
return $payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obsolete?
|
||||
@ -403,37 +408,37 @@ class XMPPHP_XMLStream {
|
||||
* @param string $name
|
||||
*/
|
||||
protected function startXML($parser, $name, $attr) {
|
||||
if($this->been_reset) {
|
||||
$this->been_reset = false;
|
||||
$this->xml_depth = 0;
|
||||
}
|
||||
$this->xml_depth++;
|
||||
if(array_key_exists('XMLNS', $attr)) {
|
||||
$this->current_ns[$this->xml_depth] = $attr['XMLNS'];
|
||||
} else {
|
||||
$this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1];
|
||||
if(!$this->current_ns[$this->xml_depth]) $this->current_ns[$this->xml_depth] = $this->default_ns;
|
||||
}
|
||||
$ns = $this->current_ns[$this->xml_depth];
|
||||
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 XMPPHP_XMLObj($name, $ns, $attr);
|
||||
if($this->xml_depth > 1) {
|
||||
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
|
||||
}
|
||||
$this->xmlobj[$this->xml_depth] = $obj;
|
||||
}
|
||||
if($this->been_reset) {
|
||||
$this->been_reset = false;
|
||||
$this->xml_depth = 0;
|
||||
}
|
||||
$this->xml_depth++;
|
||||
if(array_key_exists('XMLNS', $attr)) {
|
||||
$this->current_ns[$this->xml_depth] = $attr['XMLNS'];
|
||||
} else {
|
||||
$this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1];
|
||||
if(!$this->current_ns[$this->xml_depth]) $this->current_ns[$this->xml_depth] = $this->default_ns;
|
||||
}
|
||||
$ns = $this->current_ns[$this->xml_depth];
|
||||
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 XMPPHP_XMLObj($name, $ns, $attr);
|
||||
if($this->xml_depth > 1) {
|
||||
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
|
||||
}
|
||||
$this->xmlobj[$this->xml_depth] = $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* XML end callback
|
||||
@ -443,61 +448,61 @@ class XMPPHP_XMLStream {
|
||||
* @param resource $parser
|
||||
* @param string $name
|
||||
*/
|
||||
protected function endXML($parser, $name) {
|
||||
#$this->log->log("Ending $name", XMPPHP_Log::LEVEL_DEBUG);
|
||||
#print "$name\n";
|
||||
if($this->been_reset) {
|
||||
$this->been_reset = false;
|
||||
$this->xml_depth = 0;
|
||||
}
|
||||
$this->xml_depth--;
|
||||
if($this->xml_depth == 1) {
|
||||
#clean-up old objects
|
||||
$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]}", XMPPHP_Log::LEVEL_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_func(array($handler[1], $handler[0]), $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);
|
||||
if(isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMPPHP_XMLObj) {
|
||||
$this->xmlobj[0]->subs = null;
|
||||
}
|
||||
}
|
||||
unset($this->xmlobj[2]);
|
||||
}
|
||||
if($this->xml_depth == 0 and !$this->been_reset) {
|
||||
if(!$this->disconnected) {
|
||||
if(!$this->sent_disconnect) {
|
||||
$this->send($this->stream_end);
|
||||
}
|
||||
$this->disconnected = true;
|
||||
$this->sent_disconnect = true;
|
||||
fclose($this->socket);
|
||||
if($this->reconnect) {
|
||||
$this->doReconnect();
|
||||
}
|
||||
}
|
||||
$this->event('end_stream');
|
||||
}
|
||||
}
|
||||
protected function endXML($parser, $name) {
|
||||
#$this->log->log("Ending $name", XMPPHP_Log::LEVEL_DEBUG);
|
||||
#print "$name\n";
|
||||
if($this->been_reset) {
|
||||
$this->been_reset = false;
|
||||
$this->xml_depth = 0;
|
||||
}
|
||||
$this->xml_depth--;
|
||||
if($this->xml_depth == 1) {
|
||||
#clean-up old objects
|
||||
$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]}", XMPPHP_Log::LEVEL_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_func(array($handler[1], $handler[0]), $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);
|
||||
if(isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMPPHP_XMLObj) {
|
||||
$this->xmlobj[0]->subs = null;
|
||||
}
|
||||
}
|
||||
unset($this->xmlobj[2]);
|
||||
}
|
||||
if($this->xml_depth == 0 and !$this->been_reset) {
|
||||
if(!$this->disconnected) {
|
||||
if(!$this->sent_disconnect) {
|
||||
$this->send($this->stream_end);
|
||||
}
|
||||
$this->disconnected = true;
|
||||
$this->sent_disconnect = true;
|
||||
fclose($this->socket);
|
||||
if($this->reconnect) {
|
||||
$this->doReconnect();
|
||||
}
|
||||
}
|
||||
$this->event('end_stream');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XML character callback
|
||||
@ -506,7 +511,7 @@ class XMPPHP_XMLStream {
|
||||
* @param resource $parser
|
||||
* @param string $data
|
||||
*/
|
||||
protected function charXML($parser, $data) {
|
||||
protected function charXML($parser, $data) {
|
||||
if(array_key_exists($this->xml_depth, $this->xmlobj)) {
|
||||
$this->xmlobj[$this->xml_depth]->data .= $data;
|
||||
}
|
||||
@ -518,29 +523,29 @@ class XMPPHP_XMLStream {
|
||||
* @param string $name
|
||||
* @param string $payload
|
||||
*/
|
||||
protected function event($name, $payload = null) {
|
||||
$this->log->log("EVENT: $name", XMPPHP_Log::LEVEL_DEBUG);
|
||||
foreach($this->eventhandlers as $handler) {
|
||||
if($name == $handler[0]) {
|
||||
if($handler[2] === null) {
|
||||
$handler[2] = $this;
|
||||
}
|
||||
call_user_method($handler[1], $handler[2], $payload);
|
||||
}
|
||||
}
|
||||
foreach($this->until as $key => $until) {
|
||||
if(is_array($until)) {
|
||||
if(in_array($name, $until)) {
|
||||
$this->until_payload[$key][] = array($name, $payload);
|
||||
$this->until[$key] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function event($name, $payload = null) {
|
||||
$this->log->log("EVENT: $name", XMPPHP_Log::LEVEL_DEBUG);
|
||||
foreach($this->eventhandlers as $handler) {
|
||||
if($name == $handler[0]) {
|
||||
if($handler[2] === null) {
|
||||
$handler[2] = $this;
|
||||
}
|
||||
call_user_method($handler[1], $handler[2], $payload);
|
||||
}
|
||||
}
|
||||
foreach($this->until as $key => $until) {
|
||||
if(is_array($until)) {
|
||||
if(in_array($name, $until)) {
|
||||
$this->until_payload[$key][] = array($name, $payload);
|
||||
$this->until[$key] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from socket
|
||||
*/
|
||||
/**
|
||||
* Read from socket
|
||||
*/
|
||||
protected function read() {
|
||||
$buff = @fread($this->socket, 1024);
|
||||
if(!$buff) {
|
||||
@ -560,35 +565,35 @@ class XMPPHP_XMLStream {
|
||||
*
|
||||
* @param string $msg
|
||||
*/
|
||||
protected function send($msg) {
|
||||
#socket_write($this->socket, $msg);
|
||||
$this->log->log("SENT: $msg", XMPPHP_Log::LEVEL_VERBOSE);
|
||||
@fwrite($this->socket, $msg);
|
||||
}
|
||||
protected function send($msg) {
|
||||
#socket_write($this->socket, $msg);
|
||||
$this->log->log("SENT: $msg", XMPPHP_Log::LEVEL_VERBOSE);
|
||||
@fwrite($this->socket, $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset connection
|
||||
*/
|
||||
protected function reset() {
|
||||
$this->xml_depth = 0;
|
||||
unset($this->xmlobj);
|
||||
$this->xmlobj = array();
|
||||
$this->setupParser();
|
||||
if(!$this->is_server) {
|
||||
$this->send($this->stream_start);
|
||||
}
|
||||
$this->been_reset = true;
|
||||
}
|
||||
/**
|
||||
* Reset connection
|
||||
*/
|
||||
protected function reset() {
|
||||
$this->xml_depth = 0;
|
||||
unset($this->xmlobj);
|
||||
$this->xmlobj = array();
|
||||
$this->setupParser();
|
||||
if(!$this->is_server) {
|
||||
$this->send($this->stream_start);
|
||||
}
|
||||
$this->been_reset = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the XML parser
|
||||
*/
|
||||
protected 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');
|
||||
xml_set_object($this->parser, $this);
|
||||
xml_set_element_handler($this->parser, 'startXML', 'endXML');
|
||||
xml_set_character_data_handler($this->parser, 'charXML');
|
||||
}
|
||||
/**
|
||||
* Setup the XML parser
|
||||
*/
|
||||
protected 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');
|
||||
xml_set_object($this->parser, $this);
|
||||
xml_set_element_handler($this->parser, 'startXML', 'endXML');
|
||||
xml_set_character_data_handler($this->parser, 'charXML');
|
||||
}
|
||||
}
|
||||
|
248
XMPPHP/XMPP.php
248
XMPPHP/XMPP.php
@ -42,80 +42,80 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $server;
|
||||
protected $server;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $password;
|
||||
|
||||
protected $password;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $resource;
|
||||
|
||||
protected $resource;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $fulljid;
|
||||
|
||||
protected $fulljid;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $basejid;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $authed = false;
|
||||
protected $basejid;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $auto_subscribe = false;
|
||||
protected $authed = false;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $use_encryption = true;
|
||||
protected $auto_subscribe = false;
|
||||
|
||||
/**
|
||||
* 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->password = $password;
|
||||
$this->resource = $resource;
|
||||
if(!$server) $server = $host;
|
||||
$this->basejid = $this->user . '@' . $this->host;
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $use_encryption = true;
|
||||
|
||||
/**
|
||||
* 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->password = $password;
|
||||
$this->resource = $resource;
|
||||
if(!$server) $server = $host;
|
||||
$this->basejid = $this->user . '@' . $this->host;
|
||||
|
||||
$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_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
|
||||
$this->stream_end = '</stream:stream>';
|
||||
$this->default_ns = 'jabber:client';
|
||||
|
||||
$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');
|
||||
$this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler');
|
||||
$this->addHandler('message', 'jabber:client', 'message_handler');
|
||||
$this->addHandler('presence', 'jabber:client', 'presence_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('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('message', 'jabber:client', 'message_handler');
|
||||
$this->addHandler('presence', 'jabber:client', 'presence_handler');
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn encryption on/ff
|
||||
@ -126,14 +126,14 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
|
||||
$this->use_encryption = $useEncryption;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn on auto-authorization of subscription requests.
|
||||
/**
|
||||
* Turn on auto-authorization of subscription requests.
|
||||
*
|
||||
* @param boolean $autoSubscribe
|
||||
*/
|
||||
public function autoSubscribe($autoSubscribe = true) {
|
||||
$this->auto_subscribe = $autoSubscribe;
|
||||
}
|
||||
*/
|
||||
public function autoSubscribe($autoSubscribe = true) {
|
||||
$this->auto_subscribe = $autoSubscribe;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send XMPP Message
|
||||
@ -143,7 +143,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
|
||||
* @param string $type
|
||||
* @param string $subject
|
||||
*/
|
||||
public function message($to, $body, $type = 'chat', $subject = null) {
|
||||
public function message($to, $body, $type = 'chat', $subject = null) {
|
||||
$to = htmlspecialchars($to);
|
||||
$body = htmlspecialchars($body);
|
||||
$subject = htmlspecialchars($subject);
|
||||
@ -183,118 +183,118 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
|
||||
$this->send($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Message handler
|
||||
*
|
||||
* @param string $xml
|
||||
*/
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
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) {
|
||||
$payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
|
||||
$payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
|
||||
$payload['from'] = $xml->attrs['from'];
|
||||
$payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
|
||||
$this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG);
|
||||
if($xml->attrs['type'] == 'subscribe') {
|
||||
if($this->auto_subscribe) $this->send("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->basejid}' /><presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->basejid}' />");
|
||||
$this->event('subscription_requested', $payload);
|
||||
} elseif($xml->attrs['type'] == 'subscribed') {
|
||||
$this->event('subscription_accepted', $payload);
|
||||
} else {
|
||||
$this->event('presence', $payload);
|
||||
}
|
||||
}
|
||||
public function presence_handler($xml) {
|
||||
$payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
|
||||
$payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
|
||||
$payload['from'] = $xml->attrs['from'];
|
||||
$payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
|
||||
$this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG);
|
||||
if($xml->attrs['type'] == 'subscribe') {
|
||||
if($this->auto_subscribe) $this->send("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->basejid}' /><presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->basejid}' />");
|
||||
$this->event('subscription_requested', $payload);
|
||||
} elseif($xml->attrs['type'] == 'subscribed') {
|
||||
$this->event('subscription_accepted', $payload);
|
||||
} else {
|
||||
$this->event('presence', $payload);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Features handler
|
||||
*
|
||||
* @param string $xml
|
||||
*/
|
||||
protected function features_handler($xml) {
|
||||
if($xml->hasSub('starttls') and $this->use_encryption) {
|
||||
$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
|
||||
} elseif($xml->hasSub('bind')) {
|
||||
$id = $this->getId();
|
||||
$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 {
|
||||
$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>");
|
||||
}
|
||||
}
|
||||
protected function features_handler($xml) {
|
||||
if($xml->hasSub('starttls') and $this->use_encryption) {
|
||||
$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
|
||||
} elseif($xml->hasSub('bind')) {
|
||||
$id = $this->getId();
|
||||
$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 {
|
||||
$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>");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SASL success handler
|
||||
*
|
||||
* @param string $xml
|
||||
*/
|
||||
protected function sasl_success_handler($xml) {
|
||||
$this->log->log("Auth success!");
|
||||
$this->authed = true;
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
protected function sasl_success_handler($xml) {
|
||||
$this->log->log("Auth success!");
|
||||
$this->authed = true;
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* SASL feature handler
|
||||
*
|
||||
* @param string $xml
|
||||
*/
|
||||
protected function sasl_failure_handler($xml) {
|
||||
$this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR);
|
||||
$this->disconnect();
|
||||
protected function sasl_failure_handler($xml) {
|
||||
$this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR);
|
||||
$this->disconnect();
|
||||
|
||||
throw new XMPPHP_Exception('Auth failed!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resource bind handler
|
||||
*
|
||||
* @param string $xml
|
||||
*/
|
||||
protected function resource_bind_handler($xml) {
|
||||
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>");
|
||||
}
|
||||
protected function resource_bind_handler($xml) {
|
||||
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>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Session start handler
|
||||
*
|
||||
* @param string $xml
|
||||
*/
|
||||
protected function session_start_handler($xml) {
|
||||
$this->log->log("Session started");
|
||||
$this->event('session_start');
|
||||
}
|
||||
protected function session_start_handler($xml) {
|
||||
$this->log->log("Session started");
|
||||
$this->event('session_start');
|
||||
}
|
||||
|
||||
/**
|
||||
* TLS proceed handler
|
||||
*
|
||||
* @param string $xml
|
||||
*/
|
||||
protected function tls_proceed_handler($xml) {
|
||||
$this->log->log("Starting TLS encryption");
|
||||
stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
|
||||
$this->reset();
|
||||
}
|
||||
protected function tls_proceed_handler($xml) {
|
||||
$this->log->log("Starting TLS encryption");
|
||||
stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
|
||||
$this->reset();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user