summaryrefslogtreecommitdiff
path: root/extlib/XMPPHP/XMLStream.php
diff options
context:
space:
mode:
Diffstat (limited to 'extlib/XMPPHP/XMLStream.php')
-rw-r--r--extlib/XMPPHP/XMLStream.php94
1 files changed, 81 insertions, 13 deletions
diff --git a/extlib/XMPPHP/XMLStream.php b/extlib/XMPPHP/XMLStream.php
index 04877fda4..0fcfea375 100644
--- a/extlib/XMPPHP/XMLStream.php
+++ b/extlib/XMPPHP/XMLStream.php
@@ -22,6 +22,7 @@
* @package XMPPHP
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
* @copyright 2008 Nathanael C. Fritz
*/
@@ -41,6 +42,7 @@ require_once 'Log.php';
* @package XMPPHP
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
+ * @author Michael Garvin <JID: gar@netflint.net>
* @copyright 2008 Nathanael C. Fritz
* @version $Id$
*/
@@ -104,6 +106,10 @@ class XMPPHP_XMLStream {
/**
* @var array
*/
+ protected $xpathhandlers = array();
+ /**
+ * @var array
+ */
protected $idhandlers = array();
/**
* @var array
@@ -122,6 +128,10 @@ class XMPPHP_XMLStream {
*/
protected $until = '';
/**
+ * @var string
+ */
+ protected $until_count = '';
+ /**
* @var array
*/
protected $until_happened = false;
@@ -228,18 +238,44 @@ class XMPPHP_XMLStream {
/**
* Add Handler
*
- * @param integer $id
+ * @param string $name
* @param string $ns
* @param string $pointer
* @param string $obj
* @param integer $depth
*/
public function addHandler($name, $ns, $pointer, $obj = null, $depth = 1) {
+ #TODO deprication warning
$this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
}
/**
- * Add Evemt Handler
+ * Add XPath Handler
+ *
+ * @param string $xpath
+ * @param string $pointer
+ * @param
+ */
+ public function addXPathHandler($xpath, $pointer, $obj = null) {
+ if (preg_match_all("/\(?{[^\}]+}\)?(\/?)[^\/]+/", $xpath, $regs)) {
+ $ns_tags = $regs[0];
+ } else {
+ $ns_tags = array($xpath);
+ }
+ foreach($ns_tags as $ns_tag) {
+ list($l, $r) = split("}", $ns_tag);
+ if ($r != null) {
+ $xpart = array(substr($l, 1), $r);
+ } else {
+ $xpart = array(null, $l);
+ }
+ $xpath_array[] = $xpart;
+ }
+ $this->xpathhandlers[] = array($xpath_array, $pointer, $obj);
+ }
+
+ /**
+ * Add Event Handler
*
* @param integer $id
* @param string $pointer
@@ -257,6 +293,7 @@ class XMPPHP_XMLStream {
* @param boolean $sendinit
*/
public function connect($timeout = 30, $persistent = false, $sendinit = true) {
+ $this->sent_disconnect = false;
$starttime = time();
do {
@@ -312,6 +349,9 @@ class XMPPHP_XMLStream {
*/
public function disconnect() {
$this->log->log("Disconnecting...", XMPPHP_Log::LEVEL_VERBOSE);
+ if(false == (bool) $this->socket) {
+ return;
+ }
$this->reconnect = false;
$this->send($this->stream_end);
$this->sent_disconnect = true;
@@ -425,16 +465,19 @@ class XMPPHP_XMLStream {
end($this->until);
$event_key = key($this->until);
reset($this->until);
+ $this->until_count[$event_key] = 0;
$updated = '';
- while(!$this->disconnected and $this->until[$event_key] and (time() - $start < $timeout or $timeout == -1)) {
- $this->__process(0);
+ while(!$this->disconnected and $this->until_count[$event_key] < 1 and (time() - $start < $timeout or $timeout == -1)) {
+ $this->__process();
}
if(array_key_exists($event_key, $this->until_payload)) {
$payload = $this->until_payload[$event_key];
+ unset($this->until_payload[$event_key]);
+ unset($this->until_count[$event_key]);
+ unset($this->until[$event_key]);
} else {
$payload = array();
}
- unset($this->until_payload[$event_key]);
return $payload;
}
@@ -504,9 +547,30 @@ class XMPPHP_XMLStream {
$this->xml_depth--;
if($this->xml_depth == 1) {
#clean-up old objects
- $found = false;
+ #$found = false; #FIXME This didn't appear to be in use --Gar
+ foreach($this->xpathhandlers as $handler) {
+ if (is_array($this->xmlobj) && array_key_exists(2, $this->xmlobj)) {
+ $searchxml = $this->xmlobj[2];
+ $nstag = array_shift($handler[0]);
+ if (($nstag[0] == null or $searchxml->ns == $nstag[0]) and ($nstag[1] == "*" or $nstag[1] == $searchxml->name)) {
+ foreach($handler[0] as $nstag) {
+ if ($searchxml !== null and $searchxml->hasSub($nstag[1], $ns=$nstag[0])) {
+ $searchxml = $searchxml->sub($nstag[1], $ns=$nstag[0]);
+ } else {
+ $searchxml = null;
+ break;
+ }
+ }
+ if ($searchxml !== null) {
+ if($handler[2] === null) $handler[2] = $this;
+ $this->log->log("Calling {$handler[1]}", XMPPHP_Log::LEVEL_DEBUG);
+ $handler[2]->$handler[1]($this->xmlobj[2]);
+ }
+ }
+ }
+ }
foreach($this->nshandlers as $handler) {
- if($handler[4] != 1 and $this->xmlobj[2]->hasSub($handler[0])) {
+ if($handler[4] != 1 and array_key_exists(2, $this->xmlobj) 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];
@@ -583,7 +647,11 @@ class XMPPHP_XMLStream {
if(is_array($until)) {
if(in_array($name, $until)) {
$this->until_payload[$key][] = array($name, $payload);
- $this->until[$key] = false;
+ if(!isset($this->until_count[$key])) {
+ $this->until_count[$key] = 0;
+ }
+ $this->until_count[$key] += 1;
+ #$this->until[$key] = false;
}
}
}
@@ -637,20 +705,20 @@ class XMPPHP_XMLStream {
# TODO: retry send here
return false;
} elseif ($select > 0) {
- $this->log->log("Socket is ready; send it.");
+ $this->log->log("Socket is ready; send it.", XMPPHP_Log::LEVEL_VERBOSE);
} else {
- $this->log->log("Socket is not ready; break.");
+ $this->log->log("Socket is not ready; break.", XMPPHP_Log::LEVEL_ERROR);
return false;
}
$sentbytes = @fwrite($this->socket, $msg);
- $this->log->log("SENT: " . mb_substr($msg, 0, $sentbytes, '8bit'), XMPPHP_Log::LEVEL_VERBOSE);
+ $this->log->log("SENT: " . mb_substr($msg, 0, $sentbytes, '8bit'), XMPPHP_Log::LEVEL_VERBOSE);
if($sentbytes === FALSE) {
- $this->log->log("ERROR sending message; reconnecting.");
+ $this->log->log("ERROR sending message; reconnecting.", XMPPHP_Log::LEVEL_ERROR);
$this->doReconnect();
return false;
}
- $this->log->log("Successfully sent $sentbytes bytes.");
+ $this->log->log("Successfully sent $sentbytes bytes.", XMPPHP_Log::LEVEL_VERBOSE);
return $sentbytes;
}