3 * XMPPHP: The PHP XMPP Library
4 * Copyright (C) 2008 Nathanael C. Fritz
5 * This file is part of SleekXMPP.
7 * XMPPHP is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * XMPPHP is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XMPPHP; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
24 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
25 * @author Michael Garvin <JID: gar@netflint.net>
26 * @copyright 2008 Nathanael C. Fritz
29 /** XMPPHP_XMLStream */
30 require_once "XMLStream.php";
31 require_once "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 XMPPHP_XMPP extends XMPPHP_XMLStream {
78 protected $authed = false;
79 protected $session_started = false;
84 protected $auto_subscribe = false;
89 protected $use_encryption = true;
94 public $track_presence = true;
104 * @param string $host
105 * @param integer $port
106 * @param string $user
107 * @param string $password
108 * @param string $resource
109 * @param string $server
110 * @param boolean $printlog
111 * @param string $loglevel
113 public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) {
114 parent::__construct($host, $port, $printlog, $loglevel);
117 $this->password = $password;
118 $this->resource = $resource;
119 if(!$server) $server = $host;
120 $this->basejid = $this->user . '@' . $this->host;
122 $this->roster = new Roster();
123 $this->track_presence = true;
125 $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
126 $this->stream_end = '</stream:stream>';
127 $this->default_ns = 'jabber:client';
129 $this->addXPathHandler('{http://etherx.jabber.org/streams}features', 'features_handler');
130 $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}success', 'sasl_success_handler');
131 $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}failure', 'sasl_failure_handler');
132 $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-tls}proceed', 'tls_proceed_handler');
133 $this->addXPathHandler('{jabber:client}message', 'message_handler');
134 $this->addXPathHandler('{jabber:client}presence', 'presence_handler');
135 $this->addXPathHandler('iq/{jabber:iq:roster}query', 'roster_iq_handler');
139 * Turn encryption on/ff
141 * @param boolean $useEncryption
143 public function useEncryption($useEncryption = true) {
144 $this->use_encryption = $useEncryption;
148 * Turn on auto-authorization of subscription requests.
150 * @param boolean $autoSubscribe
152 public function autoSubscribe($autoSubscribe = true) {
153 $this->auto_subscribe = $autoSubscribe;
160 * @param string $body
161 * @param string $type
162 * @param string $subject
164 public function message($to, $body, $type = 'chat', $subject = null, $payload = null) {
170 $to = htmlspecialchars($to);
171 $body = htmlspecialchars($body);
172 $subject = htmlspecialchars($subject);
174 $out = "<message from='{$this->fulljid}' to='$to' type='$type'>";
175 if($subject) $out .= "<subject>$subject</subject>";
176 $out .= "<body>$body</body>";
177 if($payload) $out .= $payload;
178 $out .= "</message>";
186 * @param string $status
187 * @param string $show
190 public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=0) {
191 if($type == 'available') $type = '';
192 $to = htmlspecialchars($to);
193 $status = htmlspecialchars($status);
194 if($show == 'unavailable') $type = 'unavailable';
197 if($to) $out .= " to='$to'";
198 if($type) $out .= " type='$type'";
199 if($show == 'available' and !$status) {
203 if($show != 'available') $out .= "<show>$show</show>";
204 if($status) $out .= "<status>$status</status>";
205 if($priority) $out .= "<priority>$priority</priority>";
206 $out .= "</presence>";
217 public function message_handler($xml) {
218 if(isset($xml->attrs['type'])) {
219 $payload['type'] = $xml->attrs['type'];
221 $payload['type'] = 'chat';
223 $payload['from'] = $xml->attrs['from'];
224 $payload['body'] = $xml->sub('body')->data;
225 $payload['xml'] = $xml;
226 $this->log->log("Message: {$xml->sub('body')->data}", XMPPHP_Log::LEVEL_DEBUG);
227 $this->event('message', $payload);
235 public function presence_handler($xml) {
236 $payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
237 $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
238 $payload['from'] = $xml->attrs['from'];
239 $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
240 $payload['priority'] = (isset($xml->sub('priority')->data)) ? intval($xml->sub('priority')->data) : 0;
241 $payload['xml'] = $xml;
242 if($this->track_presence) {
243 $this->roster->setPresence($payload['from'], $payload['priority'], $payload['show'], $payload['status']);
245 $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG);
246 if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') {
247 if($this->auto_subscribe) {
248 $this->send("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
249 $this->send("<presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
251 $this->event('subscription_requested', $payload);
252 } elseif(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribed') {
253 $this->event('subscription_accepted', $payload);
255 $this->event('presence', $payload);
264 protected function features_handler($xml) {
265 if($xml->hasSub('starttls') and $this->use_encryption) {
266 $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
267 } elseif($xml->hasSub('bind') and $this->authed) {
268 $id = $this->getId();
269 $this->addIdHandler($id, 'resource_bind_handler');
270 $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>");
272 $this->log->log("Attempting Auth...");
273 if ($this->password) {
274 $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
276 $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
282 * SASL success handler
286 protected function sasl_success_handler($xml) {
287 $this->log->log("Auth success!");
288 $this->authed = true;
293 * SASL feature handler
297 protected function sasl_failure_handler($xml) {
298 $this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR);
301 throw new XMPPHP_Exception('Auth failed!');
305 * Resource bind handler
309 protected function resource_bind_handler($xml) {
310 if($xml->attrs['type'] == 'result') {
311 $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
312 $this->fulljid = $xml->sub('bind')->sub('jid')->data;
313 $jidarray = explode('/',$this->fulljid);
314 $this->jid = $jidarray[0];
316 $id = $this->getId();
317 $this->addIdHandler($id, 'session_start_handler');
318 $this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>");
322 * Retrieves the roster
325 public function getRoster() {
326 $id = $this->getID();
327 $this->send("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>");
332 * Gets all packets matching XPath "iq/{jabber:iq:roster}query'
336 protected function roster_iq_handler($xml) {
338 $xmlroster = $xml->sub('query');
339 foreach($xmlroster->subs as $item) {
341 if ($item->name == 'item') {
342 $jid = $item->attrs['jid']; //REQUIRED
343 $name = $item->attrs['name']; //MAY
344 $subscription = $item->attrs['subscription'];
345 foreach($item->subs as $subitem) {
346 if ($subitem->name == 'group') {
347 $groups[] = $subitem->data;
350 $contacts[] = array($jid, $subscription, $name, $groups); //Store for action if no errors happen
355 if ($status == "result") { //No errors, add contacts
356 foreach($contacts as $contact) {
357 $this->roster->addContact($contact[0], $contact[1], $contact[2], $contact[3]);
360 if ($xml->attrs['type'] == 'set') {
361 $this->send("<iq type=\"reply\" id=\"{$xml->attrs['id']}\" to=\"{$xml->attrs['from']}\" />");
366 * Session start handler
370 protected function session_start_handler($xml) {
371 $this->log->log("Session started");
372 $this->session_started = true;
373 $this->event('session_start');
377 * TLS proceed handler
381 protected function tls_proceed_handler($xml) {
382 $this->log->log("Starting TLS encryption");
383 stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
388 * Retrieves the vcard
391 public function getVCard($jid = Null) {
392 $id = $this->getID();
393 $this->addIdHandler($id, 'vcard_get_handler');
395 $this->send("<iq type='get' id='$id' to='$jid'><vCard xmlns='vcard-temp' /></iq>");
397 $this->send("<iq type='get' id='$id'><vCard xmlns='vcard-temp' /></iq>");
402 * VCard retrieval handler
404 * @param XML Object $xml
406 protected function vcard_get_handler($xml) {
407 $vcard_array = array();
408 $vcard = $xml->sub('vcard');
409 // go through all of the sub elements and add them to the vcard array
410 foreach ($vcard->subs as $sub) {
412 $vcard_array[$sub->name] = array();
413 foreach ($sub->subs as $sub_child) {
414 $vcard_array[$sub->name][$sub_child->name] = $sub_child->data;
417 $vcard_array[$sub->name] = $sub->data;
420 $vcard_array['from'] = $xml->attrs['from'];
421 $this->event('vcard', $vcard_array);