summaryrefslogtreecommitdiff
path: root/extlib/XMPPHP/XMPP.php
diff options
context:
space:
mode:
authorEvan Prodromou <evan@controlyourself.ca>2008-12-11 13:15:00 -0500
committerEvan Prodromou <evan@controlyourself.ca>2008-12-11 13:15:00 -0500
commit939a400fd3dc43216a7c43bcfb66fb18fb557279 (patch)
tree36c548b4844e64018460727f228d377ddc35c93f /extlib/XMPPHP/XMPP.php
parentfb837b86c5ab46f92fba37db0d37d49071d71a74 (diff)
upgrade XMPPHP to upstream version
darcs-hash:20081211181500-5ed1f-5042b1ff2ed5de36500200f3ea2aa91108f1f853.gz
Diffstat (limited to 'extlib/XMPPHP/XMPP.php')
-rw-r--r--extlib/XMPPHP/XMPP.php147
1 files changed, 120 insertions, 27 deletions
diff --git a/extlib/XMPPHP/XMPP.php b/extlib/XMPPHP/XMPP.php
index d5b5e049b..73fbd2658 100644
--- a/extlib/XMPPHP/XMPP.php
+++ b/extlib/XMPPHP/XMPP.php
@@ -22,11 +22,13 @@
* @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
*/
/** 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 <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$
*/
@@ -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
@@ -85,6 +89,16 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
protected $use_encryption = true;
/**
+ * @var boolean
+ */
+ public $track_presence = true;
+
+ /**
+ * @var object
+ */
+ public $roster;
+
+ /**
* Constructor
*
* @param string $host
@@ -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 = '<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->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 .= "</message>";
- 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 = "<presence";
if($to) $out .= " to='$to'";
if($type) $out .= " type='$type'";
- if($show == 'available' and !$status and is_null($priority)) {
+ if($show == 'available' and !$status) {
$out .= "/>";
} else {
$out .= ">";
if($show != 'available') $out .= "<show>$show</show>";
if($status) $out .= "<status>$status</status>";
- if(!is_null($priority)) $out .= "<priority>$priority</priority>";
+ if($priority) $out .= "<priority>$priority</priority>";
$out .= "</presence>";
}
- return $this->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("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->fulljid}' /><presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
+ if($this->auto_subscribe) {
+ $this->send("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
+ $this->send("<presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
+ }
$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("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
- } elseif($xml->hasSub('bind')) {
+ } elseif($xml->hasSub('bind') and $this->authed) {
$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...");
+ if ($this->password) {
$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
+ } else {
+ $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
+ }
}
}
@@ -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("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>");
}
/**
- * 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("<iq type=\"reply\" id=\"{$xml->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("<iq type='get' id='$id' to='$jid'><vCard xmlns='vcard-temp' /></iq>");
+ } else {
+ $this->send("<iq type='get' id='$id'><vCard xmlns='vcard-temp' /></iq>");
+ }
+ }
+
+ /**
+ * 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);
+ }
}