diff --git a/XMPPHP/Log.php b/XMPPHP/Log.php
index 903d514..a33353c 100644
--- a/XMPPHP/Log.php
+++ b/XMPPHP/Log.php
@@ -1,24 +1,36 @@
log = new XMPPHP_Log($printlog, $loglevel);
}
+ /**
+ * Destructor
+ * Cleanup connection
+ */
+ public function __destruct() {
+ if(!$this->disconnected) {
+ $this->disconnect();
+ }
+ }
+
/**
* Return the log instance
*
@@ -215,6 +243,8 @@ class XMPPHP_XMLStream {
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);
@@ -235,6 +265,7 @@ class XMPPHP_XMLStream {
* Disconnect from XMPP Host
*/
public function disconnect() {
+ $this->log->log("Disconnecting...", XMPPHP_Log::LEVEL_VERBOSE);
$this->reconnect = false;
$this->send($this->stream_end);
$this->sent_disconnect = true;
@@ -394,8 +425,9 @@ class XMPPHP_XMLStream {
$name = $name[1];
}
$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] = $obj;
}
@@ -471,8 +503,9 @@ class XMPPHP_XMLStream {
* @param string $data
*/
protected 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;
+ }
}
/**
@@ -485,7 +518,9 @@ class XMPPHP_XMLStream {
$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;
+ if($handler[2] === null) {
+ $handler[2] = $this;
+ }
call_user_method($handler[1], $handler[2], $payload);
}
}
diff --git a/XMPPHP/XMPP.php b/XMPPHP/XMPP.php
index ce1417f..faa1ee7 100644
--- a/XMPPHP/XMPP.php
+++ b/XMPPHP/XMPP.php
@@ -1,36 +1,85 @@
basejid = $this->user . '@' . $this->host;
$this->stream_start = '';
- $this->stream_end = '';
+ $this->stream_end = '';
+ $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->default_ns = 'jabber:client';
- $this->authed = false;
- $this->use_encryption = true;
}
+ /**
+ * Turn encryption on/ff
+ *
+ * @param boolean $useEncryption
+ */
+ public function useEncryption($useEncryption = true) {
+ $this->use_encryption = $useEncryption;
+ }
+
/**
* Turn on auto-authorization of subscription requests.
+ *
+ * @param boolean $autoSubscribe
*/
- public function autoSubscribe() {
- $this->auto_subscribe = true;
+ public function autoSubscribe($autoSubscribe = true) {
+ $this->auto_subscribe = $autoSubscribe;
}
/**
@@ -164,7 +222,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*
* @param string $xml
*/
- public function features_handler($xml) {
+ protected function features_handler($xml) {
if($xml->hasSub('starttls') and $this->use_encryption) {
$this->send("");
} elseif($xml->hasSub('bind')) {
@@ -182,7 +240,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*
* @param string $xml
*/
- public function sasl_success_handler($xml) {
+ protected function sasl_success_handler($xml) {
$this->log->log("Auth success!");
$this->authed = true;
$this->reset();
@@ -193,9 +251,11 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*
* @param string $xml
*/
- public function sasl_failure_handler($xml) {
+ protected function sasl_failure_handler($xml) {
$this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR);
$this->disconnect();
+
+ throw new XMPPHP_Exception('Auth failed!');
}
/**
@@ -203,7 +263,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*
* @param string $xml
*/
- public function resource_bind_handler($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;
@@ -218,7 +278,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*
* @param string $xml
*/
- public function session_start_handler($xml) {
+ protected function session_start_handler($xml) {
$this->log->log("Session started");
$this->event('session_start');
}
@@ -228,7 +288,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*
* @param string $xml
*/
- public function tls_proceed_handler($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();
diff --git a/cli_longrun_example.php b/cli_longrun_example.php
index 1ccb5bf..f9381f8 100644
--- a/cli_longrun_example.php
+++ b/cli_longrun_example.php
@@ -3,31 +3,37 @@
// activate full error reporting
error_reporting(E_ALL & E_STRICT);
-include("XMPPHP/XMPP.php");
-$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'user', 'password', 'xmpphp', 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);
-$conn->connect();
-while(!$conn->isDisconnected()) {
- $payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start'));
- foreach($payloads as $event) {
- $pl = $event[1];
- switch($event[0]) {
- case 'message':
- print "---------------------------------------------------------------------------------\n";
- print "Message from: {$pl['from']}\n";
- if($pl['subject']) print "Subject: {$pl['subject']}\n";
- print $pl['body'] . "\n";
- print "---------------------------------------------------------------------------------\n";
- $conn->message($pl['from'], $body="Thanks for sending me \"{$pl['body']}\".", $type=$pl['type']);
- if($pl['body'] == 'quit') $conn->disconnect();
- if($pl['body'] == 'break') $conn->send("");
- break;
- case 'presence':
- print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n";
- break;
- case 'session_start':
- print "Session Start\n";
- $conn->presence($status="Cheese!");
- break;
- }
- }
-}
+include 'XMPPHP/XMPP.php';
+
+$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);
+
+try {
+ $conn->connect();
+ while(!$conn->isDisconnected()) {
+ $payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start'));
+ foreach($payloads as $event) {
+ $pl = $event[1];
+ switch($event[0]) {
+ case 'message':
+ print "---------------------------------------------------------------------------------\n";
+ print "Message from: {$pl['from']}\n";
+ if($pl['subject']) print "Subject: {$pl['subject']}\n";
+ print $pl['body'] . "\n";
+ print "---------------------------------------------------------------------------------\n";
+ $conn->message($pl['from'], $body="Thanks for sending me \"{$pl['body']}\".", $type=$pl['type']);
+ if($pl['body'] == 'quit') $conn->disconnect();
+ if($pl['body'] == 'break') $conn->send("");
+ break;
+ case 'presence':
+ print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n";
+ break;
+ case 'session_start':
+ print "Session Start\n";
+ $conn->presence($status="Cheese!");
+ break;
+ }
+ }
+ }
+} catch(XMPPHP_Exception $e) {
+ die($e->getMessage());
+}
\ No newline at end of file
diff --git a/sendmessage_example.php b/sendmessage_example.php
index a1d84b4..1922f44 100644
--- a/sendmessage_example.php
+++ b/sendmessage_example.php
@@ -3,11 +3,16 @@
// activate full error reporting
error_reporting(E_ALL & E_STRICT);
-include("XMPPHP/XMPP.php");
+include 'XMPPHP/XMPP.php';
$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
-$conn->connect();
-$conn->processUntil('session_start');
-$conn->presence();
-$conn->message('stephan@jabber.wentz.it', 'This is a test message!');
-$conn->disconnect();
+
+try {
+ $conn->connect();
+ $conn->processUntil('session_start');
+ $conn->presence();
+ $conn->message('stephan@jabber.wentz.it', 'This is a test message!');
+ $conn->disconnect();
+} catch(XMPPHP_Exception $e) {
+ die($e->getMessage());
+}
\ No newline at end of file