diff --git a/extlib/XMPPHP/BOSH.php b/extlib/XMPPHP/BOSH.php new file mode 100644 index 0000000000..b147443d79 --- /dev/null +++ b/extlib/XMPPHP/BOSH.php @@ -0,0 +1,188 @@ + + * @author Stephan Wentz + * @author Michael Garvin + * @copyright 2008 Nathanael C. Fritz + */ + +/** XMPPHP_XMLStream */ +require_once "XMPP.php"; + +/** + * XMPPHP Main Class + * + * @category xmpphp + * @package XMPPHP + * @author Nathanael C. Fritz + * @author Stephan Wentz + * @author Michael Garvin + * @copyright 2008 Nathanael C. Fritz + * @version $Id$ + */ +class XMPPHP_BOSH extends XMPPHP_XMPP { + + protected $rid; + protected $sid; + protected $http_server; + protected $http_buffer = Array(); + protected $session = false; + + public function connect($server, $wait='1', $session=false) { + $this->http_server = $server; + $this->use_encryption = false; + $this->session = $session; + + $this->rid = 3001; + $this->sid = null; + if($session) + { + $this->loadSession(); + } + if(!$this->sid) { + $body = $this->__buildBody(); + $body->addAttribute('hold','1'); + $body->addAttribute('to', $this->host); + $body->addAttribute('route', "xmpp:{$this->host}:{$this->port}"); + $body->addAttribute('secure','true'); + $body->addAttribute('xmpp:version','1.6', 'urn:xmpp:xbosh'); + $body->addAttribute('wait', strval($wait)); + $body->addAttribute('ack','1'); + $body->addAttribute('xmlns:xmpp','urn:xmpp:xbosh'); + $buff = ""; + xml_parse($this->parser, $buff, false); + $response = $this->__sendBody($body); + $rxml = new SimpleXMLElement($response); + $this->sid = $rxml['sid']; + + } else { + $buff = ""; + xml_parse($this->parser, $buff, false); + } + } + + public function __sendBody($body=null, $recv=true) { + if(!$body) { + $body = $this->__buildBody(); + } + $ch = curl_init($this->http_server); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body->asXML()); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + $header = array('Accept-Encoding: gzip, deflate','Content-Type: text/xml; charset=utf-8'); + curl_setopt($ch, CURLOPT_HTTPHEADER, $header ); + curl_setopt($ch, CURLOPT_VERBOSE, 0); + $output = ''; + if($recv) { + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $output = curl_exec($ch); + $this->http_buffer[] = $output; + } + curl_close($ch); + return $output; + } + + public function __buildBody($sub=null) { + $xml = new SimpleXMLElement(""); + $xml->addAttribute('content', 'text/xml; charset=utf-8'); + $xml->addAttribute('rid', $this->rid); + $this->rid += 1; + if($this->sid) $xml->addAttribute('sid', $this->sid); + #if($this->sid) $xml->addAttribute('xmlns', 'http://jabber.org/protocol/httpbind'); + $xml->addAttribute('xml:lang', 'en'); + if($sub) { // ok, so simplexml is lame + $p = dom_import_simplexml($xml); + $c = dom_import_simplexml($sub); + $cn = $p->ownerDocument->importNode($c, true); + $p->appendChild($cn); + $xml = simplexml_import_dom($p); + } + return $xml; + } + + public function __process() { + if($this->http_buffer) { + $this->__parseBuffer(); + } else { + $this->__sendBody(); + $this->__parseBuffer(); + } + } + + public function __parseBuffer() { + while ($this->http_buffer) { + $idx = key($this->http_buffer); + $buffer = $this->http_buffer[$idx]; + unset($this->http_buffer[$idx]); + if($buffer) { + $xml = new SimpleXMLElement($buffer); + $children = $xml->xpath('child::node()'); + foreach ($children as $child) { + $buff = $child->asXML(); + $this->log->log("RECV: $buff", XMPPHP_Log::LEVEL_VERBOSE); + xml_parse($this->parser, $buff, false); + } + } + } + } + + public function send($msg) { + $this->log->log("SEND: $msg", XMPPHP_Log::LEVEL_VERBOSE); + $msg = new SimpleXMLElement($msg); + #$msg->addAttribute('xmlns', 'jabber:client'); + $this->__sendBody($this->__buildBody($msg), true); + #$this->__parseBuffer(); + } + + public function reset() { + $this->xml_depth = 0; + unset($this->xmlobj); + $this->xmlobj = array(); + $this->setupParser(); + #$this->send($this->stream_start); + $body = $this->__buildBody(); + $body->addAttribute('to', $this->host); + $body->addAttribute('xmpp:restart', 'true', 'urn:xmpp:xbosh'); + $buff = ""; + $response = $this->__sendBody($body); + $this->been_reset = true; + xml_parse($this->parser, $buff, false); + } + + public function loadSession() { + if(isset($_SESSION['XMPPHP_BOSH_RID'])) $this->rid = $_SESSION['XMPPHP_BOSH_RID']; + if(isset($_SESSION['XMPPHP_BOSH_SID'])) $this->sid = $_SESSION['XMPPHP_BOSH_SID']; + if(isset($_SESSION['XMPPHP_BOSH_authed'])) $this->authed = $_SESSION['XMPPHP_BOSH_authed']; + if(isset($_SESSION['XMPPHP_BOSH_jid'])) $this->jid = $_SESSION['XMPPHP_BOSH_jid']; + if(isset($_SESSION['XMPPHP_BOSH_fulljid'])) $this->fulljid = $_SESSION['XMPPHP_BOSH_fulljid']; + } + + public function saveSession() { + $_SESSION['XMPPHP_BOSH_RID'] = (string) $this->rid; + $_SESSION['XMPPHP_BOSH_SID'] = (string) $this->sid; + $_SESSION['XMPPHP_BOSH_authed'] = (boolean) $this->authed; + $_SESSION['XMPPHP_BOSH_jid'] = (string) $this->jid; + $_SESSION['XMPPHP_BOSH_fulljid'] = (string) $this->fulljid; + } +} diff --git a/extlib/XMPPHP/Exception.php b/extlib/XMPPHP/Exception.php index 32b2e09240..da59bc7918 100644 --- a/extlib/XMPPHP/Exception.php +++ b/extlib/XMPPHP/Exception.php @@ -22,6 +22,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz */ @@ -32,6 +33,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz * @version $Id$ */ diff --git a/extlib/XMPPHP/Log.php b/extlib/XMPPHP/Log.php index 635e68da47..a9bce3d841 100644 --- a/extlib/XMPPHP/Log.php +++ b/extlib/XMPPHP/Log.php @@ -22,6 +22,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz */ @@ -31,6 +32,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz * @version $Id$ */ @@ -82,7 +84,7 @@ class XMPPHP_Log { */ public function log($msg, $runlevel = self::LEVEL_INFO) { $time = time(); - $this->data[] = array($this->runlevel, $msg, $time); + #$this->data[] = array($this->runlevel, $msg, $time); if($this->printout and $runlevel <= $this->runlevel) { $this->writeLine($msg, $runlevel, $time); } @@ -112,5 +114,6 @@ class XMPPHP_Log { 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"; + flush(); } } diff --git a/extlib/XMPPHP/Roster.php b/extlib/XMPPHP/Roster.php new file mode 100644 index 0000000000..2e459e2a2f --- /dev/null +++ b/extlib/XMPPHP/Roster.php @@ -0,0 +1,163 @@ + + * @author Stephan Wentz + * @author Michael Garvin + * @copyright 2008 Nathanael C. Fritz + */ + +/** + * XMPPHP Roster Object + * + * @category xmpphp + * @package XMPPHP + * @author Nathanael C. Fritz + * @author Stephan Wentz + * @author Michael Garvin + * @copyright 2008 Nathanael C. Fritz + * @version $Id$ + */ + +class Roster { + /** + * Roster array, handles contacts and presence. Indexed by jid. + * Contains array with potentially two indexes 'contact' and 'presence' + * @var array + */ + protected $roster_array = array(); + /** + * Constructor + * + */ + public function __construct($roster_array = array()) { + if ($this->verifyRoster($roster_array)) { + $this->roster_array = $roster_array; //Allow for prepopulation with existing roster + } else { + $this->roster_array = array(); + } + } + + /** + * + * Check that a given roster array is of a valid structure (empty is still valid) + * + * @param array $roster_array + */ + protected function verifyRoster($roster_array) { + #TODO once we know *what* a valid roster array looks like + return True; + } + + /** + * + * Add given contact to roster + * + * @param string $jid + * @param string $subscription + * @param string $name + * @param array $groups + */ + public function addContact($jid, $subscription, $name='', $groups=array()) { + $contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups); + if ($this->isContact($jid)) { + $this->roster_array[$jid]['contact'] = $contact; + } else { + $this->roster_array[$jid] = array('contact' => $contact); + } + } + + /** + * + * Retrieve contact via jid + * + * @param string $jid + */ + public function getContact($jid) { + if ($this->isContact($jid)) { + return $this->roster_array[$jid]['contact']; + } + } + + /** + * + * Discover if a contact exists in the roster via jid + * + * @param string $jid + */ + public function isContact($jid) { + return (array_key_exists($jid, $this->roster_array)); + } + + /** + * + * Set presence + * + * @param string $presence + * @param integer $priority + * @param string $show + * @param string $status + */ + public function setPresence($presence, $priority, $show, $status) { + list($jid, $resource) = split("/", $presence); + if ($show != 'unavailable') { + if (!$this->isContact($jid)) { + $this->addContact($jid, 'not-in-roster'); + } + $resource = $resource ? $resource : ''; + $this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status); + } else { //Nuke unavailable resources to save memory + unset($this->roster_array[$jid]['resource'][$resource]); + } + } + + /* + * + * Return best presence for jid + * + * @param string $jid + */ + public function getPresence($jid) { + $split = split("/", $jid); + $jid = $split[0]; + if($this->isContact($jid)) { + $current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127 + foreach($this->roster_array[$jid]['presence'] as $resource => $presence) { + //Highest available priority or just highest priority + if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) { + $current = $presence; + $current['resource'] = $resource; + } + } + return $current; + } + } + /** + * + * Get roster + * + */ + public function getRoster() { + return $this->roster_array; + } +} +?> diff --git a/extlib/XMPPHP/XMLObj.php b/extlib/XMPPHP/XMLObj.php index 79fef9b243..0d3e219912 100644 --- a/extlib/XMPPHP/XMLObj.php +++ b/extlib/XMPPHP/XMLObj.php @@ -22,6 +22,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz */ @@ -32,6 +33,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz * @version $Id$ */ @@ -131,9 +133,9 @@ class XMPPHP_XMLObj { * @param string $name * @return boolean */ - public function hasSub($name) { + public function hasSub($name, $ns = null) { foreach($this->subs as $sub) { - if($sub->name == $name) return true; + if(($name == "*" or $sub->name == $name) and ($ns == null or $sub->ns == $ns)) return true; } return false; } @@ -146,8 +148,9 @@ class XMPPHP_XMLObj { * @param string $ns */ public function sub($name, $attrs = null, $ns = null) { + #TODO attrs is ignored foreach($this->subs as $sub) { - if($sub->name == $name) { + if($sub->name == $name and ($ns == null or $sub->ns == $ns)) { return $sub; } } diff --git a/extlib/XMPPHP/XMLStream.php b/extlib/XMPPHP/XMLStream.php index 04877fda4c..0fcfea375e 100644 --- a/extlib/XMPPHP/XMLStream.php +++ b/extlib/XMPPHP/XMLStream.php @@ -22,6 +22,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz */ @@ -41,6 +42,7 @@ require_once 'Log.php'; * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz * @version $Id$ */ @@ -101,6 +103,10 @@ class XMPPHP_XMLStream { * @var array */ protected $nshandlers = array(); + /** + * @var array + */ + protected $xpathhandlers = array(); /** * @var array */ @@ -121,6 +127,10 @@ class XMPPHP_XMLStream { * @var string */ protected $until = ''; + /** + * @var string + */ + protected $until_count = ''; /** * @var array */ @@ -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; } diff --git a/extlib/XMPPHP/XMPP.php b/extlib/XMPPHP/XMPP.php index d5b5e049b8..73fbd26584 100644 --- a/extlib/XMPPHP/XMPP.php +++ b/extlib/XMPPHP/XMPP.php @@ -22,11 +22,13 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz */ /** XMPPHP_XMLStream */ require_once "XMLStream.php"; +require_once "Roster.php"; /** * XMPPHP Main Class @@ -35,6 +37,7 @@ require_once "XMLStream.php"; * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz * @version $Id$ */ @@ -42,12 +45,12 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { /** * @var string */ - protected $server; + public $server; /** * @var string */ - protected $user; + public $user; /** * @var string @@ -73,6 +76,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { * @var boolean */ protected $authed = false; + protected $session_started = false; /** * @var boolean @@ -84,6 +88,16 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { */ protected $use_encryption = true; + /** + * @var boolean + */ + public $track_presence = true; + + /** + * @var object + */ + public $roster; + /** * Constructor * @@ -105,16 +119,20 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { if(!$server) $server = $host; $this->basejid = $this->user . '@' . $this->host; + $this->roster = new Roster(); + $this->track_presence = true; + $this->stream_start = ''; $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->addXPathHandler('{http://etherx.jabber.org/streams}features', 'features_handler'); + $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}success', 'sasl_success_handler'); + $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}failure', 'sasl_failure_handler'); + $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-tls}proceed', 'tls_proceed_handler'); + $this->addXPathHandler('{jabber:client}message', 'message_handler'); + $this->addXPathHandler('{jabber:client}presence', 'presence_handler'); + $this->addXPathHandler('iq/{jabber:iq:roster}query', 'roster_iq_handler'); } /** @@ -144,12 +162,11 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { * @param string $subject */ public function message($to, $body, $type = 'chat', $subject = null, $payload = null) { - - if(is_null($type)) - { - $type = 'chat'; - } - + if(is_null($type)) + { + $type = 'chat'; + } + $to = htmlspecialchars($to); $body = htmlspecialchars($body); $subject = htmlspecialchars($subject); @@ -160,7 +177,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { if($payload) $out .= $payload; $out .= ""; - return $this->send($out); + $this->send($out); } /** @@ -170,7 +187,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { * @param string $show * @param string $to */ - public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) { + public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=0) { if($type == 'available') $type = ''; $to = htmlspecialchars($to); $status = htmlspecialchars($status); @@ -179,17 +196,17 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { $out = "send($out); + $this->send($out); } /** @@ -205,7 +222,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { } $payload['from'] = $xml->attrs['from']; $payload['body'] = $xml->sub('body')->data; - $payload['raw'] = $xml; + $payload['xml'] = $xml; $this->log->log("Message: {$xml->sub('body')->data}", XMPPHP_Log::LEVEL_DEBUG); $this->event('message', $payload); } @@ -220,10 +237,17 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { $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 : ''; - $payload['raw'] = $xml; + $payload['priority'] = (isset($xml->sub('priority')->data)) ? intval($xml->sub('priority')->data) : 0; + $payload['xml'] = $xml; + if($this->track_presence) { + $this->roster->setPresence($payload['from'], $payload['priority'], $payload['show'], $payload['status']); + } $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG); if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') { - if($this->auto_subscribe) $this->send(""); + if($this->auto_subscribe) { + $this->send(""); + $this->send(""); + } $this->event('subscription_requested', $payload); } elseif(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribed') { $this->event('subscription_accepted', $payload); @@ -240,13 +264,17 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { protected function features_handler($xml) { if($xml->hasSub('starttls') and $this->use_encryption) { $this->send(""); - } elseif($xml->hasSub('bind')) { + } elseif($xml->hasSub('bind') and $this->authed) { $id = $this->getId(); $this->addIdHandler($id, 'resource_bind_handler'); $this->send("{$this->resource}"); } else { $this->log->log("Attempting Auth..."); + if ($this->password) { $this->send("" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . ""); + } else { + $this->send(""); + } } } @@ -282,6 +310,8 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { if($xml->attrs['type'] == 'result') { $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data); $this->fulljid = $xml->sub('bind')->sub('jid')->data; + $jidarray = explode('/',$this->fulljid); + $this->jid = $jidarray[0]; } $id = $this->getId(); $this->addIdHandler($id, 'session_start_handler'); @@ -294,17 +324,42 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { */ public function getRoster() { $id = $this->getID(); - $this->addIdHandler($id, 'roster_get_handler'); $this->send(""); } /** - * Roster retrieval handler + * Roster iq handler + * Gets all packets matching XPath "iq/{jabber:iq:roster}query' * * @param string $xml */ - protected function roster_get_handler($xml) { - // TODO: make this work + protected function roster_iq_handler($xml) { + $status = "result"; + $xmlroster = $xml->sub('query'); + foreach($xmlroster->subs as $item) { + $groups = array(); + if ($item->name == 'item') { + $jid = $item->attrs['jid']; //REQUIRED + $name = $item->attrs['name']; //MAY + $subscription = $item->attrs['subscription']; + foreach($item->subs as $subitem) { + if ($subitem->name == 'group') { + $groups[] = $subitem->data; + } + } + $contacts[] = array($jid, $subscription, $name, $groups); //Store for action if no errors happen + } else { + $status = "error"; + } + } + if ($status == "result") { //No errors, add contacts + foreach($contacts as $contact) { + $this->roster->addContact($contact[0], $contact[1], $contact[2], $contact[3]); + } + } + if ($xml->attrs['type'] == 'set') { + $this->send("attrs['id']}\" to=\"{$xml->attrs['from']}\" />"); + } } /** @@ -314,6 +369,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { */ protected function session_start_handler($xml) { $this->log->log("Session started"); + $this->session_started = true; $this->event('session_start'); } @@ -327,4 +383,41 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); $this->reset(); } + + /** + * Retrieves the vcard + * + */ + public function getVCard($jid = Null) { + $id = $this->getID(); + $this->addIdHandler($id, 'vcard_get_handler'); + if($jid) { + $this->send(""); + } else { + $this->send(""); + } + } + + /** + * VCard retrieval handler + * + * @param XML Object $xml + */ + protected function vcard_get_handler($xml) { + $vcard_array = array(); + $vcard = $xml->sub('vcard'); + // go through all of the sub elements and add them to the vcard array + foreach ($vcard->subs as $sub) { + if ($sub->subs) { + $vcard_array[$sub->name] = array(); + foreach ($sub->subs as $sub_child) { + $vcard_array[$sub->name][$sub_child->name] = $sub_child->data; + } + } else { + $vcard_array[$sub->name] = $sub->data; + } + } + $vcard_array['from'] = $xml->attrs['from']; + $this->event('vcard', $vcard_array); + } } diff --git a/extlib/XMPPHP/XMPP_Old.php b/extlib/XMPPHP/XMPP_Old.php index e5649effe3..43f56b1546 100644 --- a/extlib/XMPPHP/XMPP_Old.php +++ b/extlib/XMPPHP/XMPP_Old.php @@ -22,6 +22,7 @@ * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz + * @author Michael Garvin * @copyright 2008 Nathanael C. Fritz */