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 * @copyright 2008 Nathanael C. Fritz
28 /** XMPPHP_XMLStream */
29 require_once "XMLStream.php";
36 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
37 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
38 * @copyright 2008 Nathanael C. Fritz
41 class XMPPHP_XMPP extends XMPPHP_XMLStream {
75 protected $authed = false;
80 protected $auto_subscribe = false;
85 protected $use_encryption = true;
91 * @param integer $port
93 * @param string $password
94 * @param string $resource
95 * @param string $server
96 * @param boolean $printlog
97 * @param string $loglevel
99 public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) {
100 parent::__construct($host, $port, $printlog, $loglevel);
103 $this->password = $password;
104 $this->resource = $resource;
105 if(!$server) $server = $host;
106 $this->basejid = $this->user . '@' . $this->host;
108 $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
109 $this->stream_end = '</stream:stream>';
110 $this->default_ns = 'jabber:client';
112 $this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler');
113 $this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler');
114 $this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler');
115 $this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler');
116 $this->addHandler('message', 'jabber:client', 'message_handler');
117 $this->addHandler('presence', 'jabber:client', 'presence_handler');
121 * Turn encryption on/ff
123 * @param boolean $useEncryption
125 public function useEncryption($useEncryption = true) {
126 $this->use_encryption = $useEncryption;
130 * Turn on auto-authorization of subscription requests.
132 * @param boolean $autoSubscribe
134 public function autoSubscribe($autoSubscribe = true) {
135 $this->auto_subscribe = $autoSubscribe;
142 * @param string $body
143 * @param string $type
144 * @param string $subject
146 public function message($to, $body, $type = 'chat', $subject = null, $payload = null) {
153 $to = htmlspecialchars($to);
154 $body = htmlspecialchars($body);
155 $subject = htmlspecialchars($subject);
157 $out = "<message from='{$this->fulljid}' to='$to' type='$type'>";
158 if($subject) $out .= "<subject>$subject</subject>";
159 $out .= "<body>$body</body>";
160 if($payload) $out .= $payload;
161 $out .= "</message>";
163 return $this->send($out);
169 * @param string $status
170 * @param string $show
173 public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) {
174 if($type == 'available') $type = '';
175 $to = htmlspecialchars($to);
176 $status = htmlspecialchars($status);
177 if($show == 'unavailable') $type = 'unavailable';
180 if($to) $out .= " to='$to'";
181 if($type) $out .= " type='$type'";
182 if($show == 'available' and !$status and is_null($priority)) {
186 if($show != 'available') $out .= "<show>$show</show>";
187 if($status) $out .= "<status>$status</status>";
188 if(!is_null($priority)) $out .= "<priority>$priority</priority>";
189 $out .= "</presence>";
192 return $this->send($out);
200 public function message_handler($xml) {
201 if(isset($xml->attrs['type'])) {
202 $payload['type'] = $xml->attrs['type'];
204 $payload['type'] = 'chat';
206 $payload['from'] = $xml->attrs['from'];
207 $payload['body'] = $xml->sub('body')->data;
208 $payload['raw'] = $xml;
209 $this->log->log("Message: {$xml->sub('body')->data}", XMPPHP_Log::LEVEL_DEBUG);
210 $this->event('message', $payload);
218 public function presence_handler($xml) {
219 $payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
220 $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
221 $payload['from'] = $xml->attrs['from'];
222 $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
223 $payload['raw'] = $xml;
224 $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG);
225 if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') {
226 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}' />");
227 $this->event('subscription_requested', $payload);
228 } elseif(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribed') {
229 $this->event('subscription_accepted', $payload);
231 $this->event('presence', $payload);
240 protected function features_handler($xml) {
241 if($xml->hasSub('starttls') and $this->use_encryption) {
242 $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
243 } elseif($xml->hasSub('bind')) {
244 $id = $this->getId();
245 $this->addIdHandler($id, 'resource_bind_handler');
246 $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>");
248 $this->log->log("Attempting Auth...");
249 $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
254 * SASL success handler
258 protected function sasl_success_handler($xml) {
259 $this->log->log("Auth success!");
260 $this->authed = true;
265 * SASL feature handler
269 protected function sasl_failure_handler($xml) {
270 $this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR);
273 throw new XMPPHP_Exception('Auth failed!');
277 * Resource bind handler
281 protected function resource_bind_handler($xml) {
282 if($xml->attrs['type'] == 'result') {
283 $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
284 $this->fulljid = $xml->sub('bind')->sub('jid')->data;
286 $id = $this->getId();
287 $this->addIdHandler($id, 'session_start_handler');
288 $this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>");
292 * Retrieves the roster
295 public function getRoster() {
296 $id = $this->getID();
297 $this->addIdHandler($id, 'roster_get_handler');
298 $this->send("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>");
302 * Roster retrieval handler
306 protected function roster_get_handler($xml) {
307 // TODO: make this work
311 * Session start handler
315 protected function session_start_handler($xml) {
316 $this->log->log("Session started");
317 $this->event('session_start');
321 * TLS proceed handler
325 protected function tls_proceed_handler($xml) {
326 $this->log->log("Starting TLS encryption");
327 stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);