}
/**
* 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 : '';
$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}' />");
$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);
} 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
arams:xml:ns:xmpp-tls'><required /></starttls>");
} 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
arams: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
arams:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
} else {
$this->send("<auth xmlns='urn:ietf
arams:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
}
}
}
/**
* SASL success handler
*
* @param string $xml
*/
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();
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;
$jidarray = explode('/',$this->fulljid);
$this->jid = $jidarray[0];
}
$id = $this->getId();
$this->addIdHandler($id, 'session_start_handler');
$this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf
arams:xml:ns:xmpp-session' /></iq>");
}
/**
* Retrieves the roster
*
*/
public function getRoster($jid) {
$id = $this->getID();
$this->send("<iq to='{$jid}' id='$id' type='result'><query xmlns='jabber:iq:roster'></query></iq>");
}
/**
* Roster iq handler
* Gets all packets matching XPath "iq/{jabber:iq:roster}query'
*
* @param string $xml
*/
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']}\" />");
}
}
/**
* Session start handler
*
* @param string $xml
*/
protected function session_start_handler($xml) {
$this->log->log("Session started");
$this->session_started = true;
$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();
}
/**
* 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);
}
}