4 * XMPPHP: The PHP XMPP Library
5 * Copyright (C) 2008 Nathanael C. Fritz
6 * This file is part of SleekXMPP.
8 * XMPPHP is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * XMPPHP is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with XMPPHP; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
25 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
26 * @author Michael Garvin <JID: gar@netflint.net>
27 * @copyright 2008 Nathanael C. Fritz
29 /** XMPPHP_XMLStream */
30 require_once 'Net/XMPP/XMLStream.php';
31 require_once 'Net/XMPP/Roster.php';
38 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
39 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
40 * @author Michael Garvin <JID: gar@netflint.net>
41 * @copyright 2008 Nathanael C. Fritz
44 class Net_XMPP extends Net_XMPP_XMLStream
79 protected $authed = false;
80 protected $session_started = false;
85 protected $auto_subscribe = false;
90 protected $use_encryption = true;
95 public $track_presence = true;
101 public $xoauth = false;
106 * @param string $host
107 * @param integer $port
108 * @param string $user
109 * @param string $password
110 * @param string $resource
111 * @param string $server
112 * @param boolean $printlog
113 * @param string $loglevel
115 public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null, $xoauth = false)
117 parent::__construct($host, $port, $printlog, $loglevel);
120 $this->password = $password;
121 $this->resource = $resource;
124 $this->basejid = $this->user . '@' . $this->host;
126 $this->xoauth = $xoauth;
128 $this->roster = new Net_XMPP_Roster();
129 $this->track_presence = true;
131 $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
132 $this->stream_end = '</stream:stream>';
133 $this->default_ns = 'jabber:client';
135 $this->addXPathHandler('{http://etherx.jabber.org/streams}features', 'features_handler');
136 $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}success', 'sasl_success_handler');
137 $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}failure', 'sasl_failure_handler');
138 $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-tls}proceed', 'tls_proceed_handler');
139 $this->addXPathHandler('{jabber:client}message', 'message_handler');
140 $this->addXPathHandler('{jabber:client}presence', 'presence_handler');
141 $this->addXPathHandler('iq/{jabber:iq:roster}query', 'roster_iq_handler');
145 * Turn encryption on/ff
147 * @param boolean $useEncryption
149 public function useEncryption($useEncryption = true)
151 $this->use_encryption = $useEncryption;
155 * Turn on auto-authorization of subscription requests.
157 * @param boolean $autoSubscribe
159 public function autoSubscribe($autoSubscribe = true)
161 $this->auto_subscribe = $autoSubscribe;
168 * @param string $body
169 * @param string $type
170 * @param string $subject
172 public function message($to, $body, $type = 'chat', $subject = null, $payload = null)
174 if (is_null($type)) {
178 $to = htmlspecialchars($to);
179 $body = htmlspecialchars($body);
180 $subject = htmlspecialchars($subject);
182 $out = "<message from=\"{$this->fulljid}\" to=\"$to\" type='$type'>";
184 $out .= "<subject>$subject</subject>";
185 $out .= "<body>$body</body>";
188 $out .= "</message>";
196 * @param string $status
197 * @param string $show
200 public function presence($status = null, $show = 'available', $to = null, $type = 'available', $priority = 0)
202 if ($type == 'available')
204 $to = htmlspecialchars($to);
205 $status = htmlspecialchars($status);
206 if ($show == 'unavailable')
207 $type = 'unavailable';
211 $out .= " to=\"$to\"";
213 $out .= " type='$type'";
214 if ($show == 'available' and ! $status) {
218 if ($show != 'available')
219 $out .= "<show>$show</show>";
221 $out .= "<status>$status</status>";
223 $out .= "<priority>$priority</priority>";
224 $out .= "</presence>";
235 public function subscribe($jid)
237 $this->send("<presence type='subscribe' to='{$jid}' from='{$this->fulljid}' />");
238 #$this->send("<presence type='subscribed' to='{$jid}' from='{$this->fulljid}' />");
246 public function message_handler($xml)
248 if (isset($xml->attrs['type'])) {
249 $payload['type'] = $xml->attrs['type'];
251 $payload['type'] = 'chat';
253 $payload['from'] = $xml->attrs['from'];
255 if (is_object($xml->sub('body')))
256 $payload['body'] = $xml->sub('body')->data;
258 $payload['xml'] = $xml;
260 if (is_object($xml->sub('body')))
261 $this->log->log("Message: {$xml->sub('body')->data}", Net_XMPP_Log::LEVEL_DEBUG);
262 $this->event('message', $payload);
270 public function presence_handler($xml)
272 $payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
273 $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
274 $payload['from'] = $xml->attrs['from'];
275 $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
276 $payload['priority'] = (isset($xml->sub('priority')->data)) ? intval($xml->sub('priority')->data) : 0;
277 $payload['xml'] = $xml;
278 if ($this->track_presence) {
279 $this->roster->setPresence($payload['from'], $payload['priority'], $payload['show'], $payload['status']);
281 $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", Net_XMPP_Log::LEVEL_DEBUG);
282 if (array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') {
283 if ($this->auto_subscribe) {
284 $this->send("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
285 $this->send("<presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
287 $this->event('subscription_requested', $payload);
288 } elseif (array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribed') {
289 $this->event('subscription_accepted', $payload);
291 $this->event('presence', $payload);
300 protected function features_handler($xml)
302 if ($xml->hasSub('starttls') and $this->use_encryption) {
303 $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
304 } elseif ($xml->hasSub('bind') and $this->authed) {
305 $id = $this->getId();
306 $this->addIdHandler($id, 'resource_bind_handler');
307 $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>");
309 $this->log->log("Attempting Auth...");
311 if ($this->xoauth == true) {
312 $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-OAUTH2'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
314 if ($this->password) {
315 $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
317 $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
324 * SASL success handler
328 protected function sasl_success_handler($xml)
330 $this->log->log("Auth success!");
331 $this->authed = true;
336 * SASL feature handler
340 protected function sasl_failure_handler($xml)
342 $this->log->log("Auth failed!", Net_XMPP_Log::LEVEL_ERROR);
345 throw new Net_XMPP_Exception('Auth failed!');
349 * Resource bind handler
353 protected function resource_bind_handler($xml)
355 if ($xml->attrs['type'] == 'result') {
356 $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
357 $this->fulljid = $xml->sub('bind')->sub('jid')->data;
358 $jidarray = explode('/', $this->fulljid);
359 $this->jid = $jidarray[0];
361 $id = $this->getId();
362 $this->addIdHandler($id, 'session_start_handler');
363 $this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>");
367 * Retrieves the roster
370 public function getRoster()
372 $id = $this->getID();
373 $this->send("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>");
378 * Gets all packets matching XPath "iq/{jabber:iq:roster}query'
382 protected function roster_iq_handler($xml)
385 $xmlroster = $xml->sub('query');
386 foreach ($xmlroster->subs as $item) {
388 if ($item->name == 'item') {
389 $jid = $item->attrs['jid']; //REQUIRED
390 $name = $item->attrs['name']; //MAY
391 $subscription = $item->attrs['subscription'];
392 foreach ($item->subs as $subitem) {
393 if ($subitem->name == 'group') {
394 $groups[] = $subitem->data;
397 $contacts[] = array($jid, $subscription, $name, $groups); //Store for action if no errors happen
402 if ($status == "result") { //No errors, add contacts
403 foreach ($contacts as $contact) {
404 $this->roster->addContact($contact[0], $contact[1], $contact[2], $contact[3]);
407 if ($xml->attrs['type'] == 'set') {
408 $this->send("<iq type=\"reply\" id=\"{$xml->attrs['id']}\" to=\"{$xml->attrs['from']}\" />");
413 * Session start handler
417 protected function session_start_handler($xml)
419 $this->log->log("Session started");
420 $this->session_started = true;
421 $this->event('session_start');
425 * TLS proceed handler
429 protected function tls_proceed_handler($xml)
431 $this->log->log("Starting TLS encryption");
432 stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
437 * Retrieves the vcard
440 public function getVCard($jid = Null)
442 $id = $this->getID();
443 $this->addIdHandler($id, 'vcard_get_handler');
445 $this->send("<iq type='get' id='$id' to='$jid'><vCard xmlns='vcard-temp' /></iq>");
447 $this->send("<iq type='get' id='$id'><vCard xmlns='vcard-temp' /></iq>");
452 * VCard retrieval handler
454 * @param XML Object $xml
456 protected function vcard_get_handler($xml)
458 $vcard_array = array();
459 $vcard = $xml->sub('vcard');
460 // go through all of the sub elements and add them to the vcard array
461 foreach ($vcard->subs as $sub) {
463 $vcard_array[$sub->name] = array();
464 foreach ($sub->subs as $sub_child) {
465 $vcard_array[$sub->name][$sub_child->name] = $sub_child->data;
468 $vcard_array[$sub->name] = $sub->data;
471 $vcard_array['from'] = $xml->attrs['from'];
472 $this->event('vcard', $vcard_array);