phpBB
Statistics
| Revision:

root / trunk / phpBB / includes / functions_jabber.php

History | View | Annotate | Download (21.3 kB)

1 4141 psotfx
<?php
2 7736 acydburn
/**
3 5114 acydburn
*
4 5114 acydburn
* @package phpBB3
5 7736 acydburn
* @copyright (c) 2007 phpBB Group
6 11653 git-gate
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7 5114 acydburn
*
8 5114 acydburn
*/
9 4141 psotfx
10 5114 acydburn
/**
11 8146 acydburn
* @ignore
12 8146 acydburn
*/
13 8146 acydburn
if (!defined('IN_PHPBB'))
14 8146 acydburn
{
15 8146 acydburn
        exit;
16 8146 acydburn
}
17 8146 acydburn
18 8146 acydburn
/**
19 5114 acydburn
*
20 7687 acydburn
* Jabber class from Flyspray project
21 7699 acydburn
*
22 8979 acydburn
* @version class.jabber2.php 1595 2008-09-19 (0.9.9)
23 7687 acydburn
* @copyright 2006 Flyspray.org
24 8049 acydburn
* @author Florian Schmitz (floele)
25 5114 acydburn
*
26 8306 acydburn
* Only slightly modified by Acyd Burn
27 5114 acydburn
*
28 6058 acydburn
* @package phpBB3
29 4141 psotfx
*/
30 5116 acydburn
class jabber
31 4141 psotfx
{
32 7687 acydburn
        var $connection = null;
33 7687 acydburn
        var $session = array();
34 7687 acydburn
        var $timeout = 10;
35 7687 acydburn
36 4141 psotfx
        var $server;
37 8979 acydburn
        var $connect_server;
38 4141 psotfx
        var $port;
39 4141 psotfx
        var $username;
40 4141 psotfx
        var $password;
41 7687 acydburn
        var $use_ssl;
42 7836 acydburn
        var $resource = 'functions_jabber.phpbb.php';
43 4141 psotfx
44 6436 acydburn
        var $enable_logging;
45 6436 acydburn
        var $log_array;
46 6436 acydburn
47 7687 acydburn
        var $features = array();
48 4141 psotfx
49 7699 acydburn
        /**
50 7699 acydburn
        */
51 7687 acydburn
        function jabber($server, $port, $username, $password, $use_ssl = false)
52 4141 psotfx
        {
53 8979 acydburn
                $this->connect_server                = ($server) ? $server : 'localhost';
54 7687 acydburn
                $this->port                                        = ($port) ? $port : 5222;
55 8979 acydburn
56 8979 acydburn
                // Get the server and the username
57 8979 acydburn
                if (strpos($username, '@') === false)
58 8979 acydburn
                {
59 8979 acydburn
                        $this->server = $this->connect_server;
60 8979 acydburn
                        $this->username = $username;
61 8979 acydburn
                }
62 8979 acydburn
                else
63 8979 acydburn
                {
64 8979 acydburn
                        $jid = explode('@', $username, 2);
65 8979 acydburn
66 8979 acydburn
                        $this->username = $jid[0];
67 8979 acydburn
                        $this->server = $jid[1];
68 8979 acydburn
                }
69 8979 acydburn
70 6436 acydburn
                $this->password                                = $password;
71 7688 acydburn
                $this->use_ssl                                = ($use_ssl && $this->can_use_ssl()) ? true : false;
72 6436 acydburn
73 7687 acydburn
                // Change port if we use SSL
74 7687 acydburn
                if ($this->port == 5222 && $this->use_ssl)
75 7687 acydburn
                {
76 7687 acydburn
                        $this->port = 5223;
77 7687 acydburn
                }
78 7687 acydburn
79 6436 acydburn
                $this->enable_logging                = true;
80 6436 acydburn
                $this->log_array                        = array();
81 4141 psotfx
        }
82 4141 psotfx
83 6015 acydburn
        /**
84 7687 acydburn
        * Able to use the SSL functionality?
85 6015 acydburn
        */
86 7687 acydburn
        function can_use_ssl()
87 4141 psotfx
        {
88 7699 acydburn
                // Will not work with PHP >= 5.2.1 or < 5.2.3RC2 until timeout problem with ssl hasn't been fixed (http://bugs.php.net/41236)
89 7702 acydburn
                return ((version_compare(PHP_VERSION, '5.2.1', '<') || version_compare(PHP_VERSION, '5.2.3RC2', '>=')) && @extension_loaded('openssl')) ? true : false;
90 4141 psotfx
        }
91 4141 psotfx
92 6015 acydburn
        /**
93 7687 acydburn
        * Able to use TLS?
94 6015 acydburn
        */
95 7687 acydburn
        function can_use_tls()
96 4141 psotfx
        {
97 7687 acydburn
                if (!@extension_loaded('openssl') || !function_exists('stream_socket_enable_crypto') || !function_exists('stream_get_meta_data') || !function_exists('socket_set_blocking') || !function_exists('stream_get_wrappers'))
98 4141 psotfx
                {
99 7687 acydburn
                        return false;
100 4141 psotfx
                }
101 4141 psotfx
102 7836 acydburn
                /**
103 7836 acydburn
                * Make sure the encryption stream is supported
104 7836 acydburn
                * Also seem to work without the crypto stream if correctly compiled
105 7836 acydburn
106 7687 acydburn
                $streams = stream_get_wrappers();
107 4141 psotfx
108 7687 acydburn
                if (!in_array('streams.crypto', $streams))
109 4141 psotfx
                {
110 5116 acydburn
                        return false;
111 4141 psotfx
                }
112 7836 acydburn
                */
113 4141 psotfx
114 7687 acydburn
                return true;
115 4588 psotfx
        }
116 4147 psotfx
117 6015 acydburn
        /**
118 7836 acydburn
        * Sets the resource which is used. No validation is done here, only escaping.
119 7836 acydburn
        * @param string $name
120 7836 acydburn
        * @access public
121 7836 acydburn
        */
122 7836 acydburn
        function set_resource($name)
123 7836 acydburn
        {
124 7836 acydburn
                $this->resource = $name;
125 7836 acydburn
        }
126 7836 acydburn
127 7836 acydburn
        /**
128 7687 acydburn
        * Connect
129 6015 acydburn
        */
130 7687 acydburn
        function connect()
131 4588 psotfx
        {
132 7687 acydburn
/*                if (!$this->check_jid($this->username . '@' . $this->server))
133 4588 psotfx
                {
134 7687 acydburn
                        $this->add_to_log('Error: Jabber ID is not valid: ' . $this->username . '@' . $this->server);
135 7687 acydburn
                        return false;
136 7687 acydburn
                }*/
137 4588 psotfx
138 7687 acydburn
                $this->session['ssl'] = $this->use_ssl;
139 4588 psotfx
140 8979 acydburn
                if ($this->open_socket($this->connect_server, $this->port, $this->use_ssl))
141 4147 psotfx
                {
142 7687 acydburn
                        $this->send("<?xml version='1.0' encoding='UTF-8' ?" . ">\n");
143 7687 acydburn
                        $this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
144 4147 psotfx
                }
145 4475 psotfx
                else
146 4475 psotfx
                {
147 7687 acydburn
                        $this->add_to_log('Error: connect() #2');
148 6436 acydburn
                        return false;
149 4475 psotfx
                }
150 4475 psotfx
151 7687 acydburn
                // Now we listen what the server has to say...and give appropriate responses
152 7687 acydburn
                $this->response($this->listen());
153 5116 acydburn
                return true;
154 4141 psotfx
        }
155 4141 psotfx
156 6015 acydburn
        /**
157 7687 acydburn
        * Disconnect
158 6015 acydburn
        */
159 7687 acydburn
        function disconnect()
160 4141 psotfx
        {
161 7687 acydburn
                if ($this->connected())
162 4141 psotfx
                {
163 7687 acydburn
                        // disconnect gracefully
164 7687 acydburn
                        if (isset($this->session['sent_presence']))
165 4141 psotfx
                        {
166 7836 acydburn
                                $this->send_presence('offline', '', true);
167 4141 psotfx
                        }
168 4141 psotfx
169 7687 acydburn
                        $this->send('</stream:stream>');
170 7687 acydburn
                        $this->session = array();
171 7687 acydburn
                        return fclose($this->connection);
172 4141 psotfx
                }
173 4141 psotfx
174 7687 acydburn
                return false;
175 4440 psotfx
        }
176 4440 psotfx
177 6015 acydburn
        /**
178 7687 acydburn
        * Connected?
179 6015 acydburn
        */
180 7687 acydburn
        function connected()
181 4141 psotfx
        {
182 7687 acydburn
                return (is_resource($this->connection) && !feof($this->connection)) ? true : false;
183 4141 psotfx
        }
184 4141 psotfx
185 4141 psotfx
186 6015 acydburn
        /**
187 7687 acydburn
        * Initiates login (using data from contructor, after calling connect())
188 7687 acydburn
        * @access public
189 7687 acydburn
        * @return bool
190 6015 acydburn
        */
191 7687 acydburn
        function login()
192 4141 psotfx
        {
193 7687 acydburn
                if (!sizeof($this->features))
194 4141 psotfx
                {
195 7687 acydburn
                        $this->add_to_log('Error: No feature information from server available.');
196 7687 acydburn
                        return false;
197 4141 psotfx
                }
198 4141 psotfx
199 7687 acydburn
                return $this->response($this->features);
200 4141 psotfx
        }
201 4141 psotfx
202 6015 acydburn
        /**
203 7687 acydburn
        * Send data to the Jabber server
204 7687 acydburn
        * @param string $xml
205 7687 acydburn
        * @access public
206 7687 acydburn
        * @return bool
207 6015 acydburn
        */
208 7687 acydburn
        function send($xml)
209 4141 psotfx
        {
210 7687 acydburn
                if ($this->connected())
211 4141 psotfx
                {
212 7687 acydburn
                        $xml = trim($xml);
213 7687 acydburn
                        $this->add_to_log('SEND: '. $xml);
214 7687 acydburn
                        return fwrite($this->connection, $xml);
215 4141 psotfx
                }
216 4141 psotfx
                else
217 4141 psotfx
                {
218 7687 acydburn
                        $this->add_to_log('Error: Could not send, connection lost (flood?).');
219 5116 acydburn
                        return false;
220 4141 psotfx
                }
221 4141 psotfx
        }
222 4141 psotfx
223 6436 acydburn
        /**
224 7687 acydburn
        * OpenSocket
225 7687 acydburn
        * @param string $server host to connect to
226 7687 acydburn
        * @param int $port port number
227 7687 acydburn
        * @param bool $use_ssl use ssl or not
228 7687 acydburn
        * @access public
229 7687 acydburn
        * @return bool
230 6436 acydburn
        */
231 7687 acydburn
        function open_socket($server, $port, $use_ssl = false)
232 6436 acydburn
        {
233 7687 acydburn
                if (@function_exists('dns_get_record'))
234 6436 acydburn
                {
235 7696 acydburn
                        $record = @dns_get_record("_xmpp-client._tcp.$server", DNS_SRV);
236 7696 acydburn
                        if (!empty($record) && !empty($record[0]['target']))
237 6436 acydburn
                        {
238 7687 acydburn
                                $server = $record[0]['target'];
239 6436 acydburn
                        }
240 6436 acydburn
                }
241 6436 acydburn
242 7687 acydburn
                $server = $use_ssl ? 'ssl://' . $server : $server;
243 6436 acydburn
244 7687 acydburn
                if ($this->connection = @fsockopen($server, $port, $errorno, $errorstr, $this->timeout))
245 6436 acydburn
                {
246 7687 acydburn
                        socket_set_blocking($this->connection, 0);
247 7687 acydburn
                        socket_set_timeout($this->connection, 60);
248 6436 acydburn
249 7687 acydburn
                        return true;
250 6436 acydburn
                }
251 7687 acydburn
252 7687 acydburn
                // Apparently an error occured...
253 7687 acydburn
                $this->add_to_log('Error: open_socket() - ' . $errorstr);
254 7687 acydburn
                return false;
255 6436 acydburn
        }
256 6436 acydburn
257 6436 acydburn
        /**
258 6436 acydburn
        * Return log
259 6436 acydburn
        */
260 6436 acydburn
        function get_log()
261 6436 acydburn
        {
262 6436 acydburn
                if ($this->enable_logging && sizeof($this->log_array))
263 6436 acydburn
                {
264 7696 acydburn
                        return implode("<br /><br />", $this->log_array);
265 6436 acydburn
                }
266 6436 acydburn
267 6436 acydburn
                return '';
268 6436 acydburn
        }
269 6436 acydburn
270 6436 acydburn
        /**
271 6436 acydburn
        * Add information to log
272 6436 acydburn
        */
273 6436 acydburn
        function add_to_log($string)
274 6436 acydburn
        {
275 6436 acydburn
                if ($this->enable_logging)
276 6436 acydburn
                {
277 7687 acydburn
                        $this->log_array[] = utf8_htmlspecialchars($string);
278 6436 acydburn
                }
279 6436 acydburn
        }
280 6436 acydburn
281 6015 acydburn
        /**
282 7687 acydburn
        * Listens to the connection until it gets data or the timeout is reached.
283 7687 acydburn
        * Thus, it should only be called if data is expected to be received.
284 7687 acydburn
        * @access public
285 7687 acydburn
        * @return mixed either false for timeout or an array with the received data
286 6015 acydburn
        */
287 7687 acydburn
        function listen($timeout = 10, $wait = false)
288 4141 psotfx
        {
289 7687 acydburn
                if (!$this->connected())
290 4141 psotfx
                {
291 7687 acydburn
                        return false;
292 4141 psotfx
                }
293 4141 psotfx
294 7687 acydburn
                // Wait for a response until timeout is reached
295 7687 acydburn
                $start = time();
296 7687 acydburn
                $data = '';
297 4141 psotfx
298 7687 acydburn
                do
299 6436 acydburn
                {
300 7687 acydburn
                        $read = trim(fread($this->connection, 4096));
301 7687 acydburn
                        $data .= $read;
302 6436 acydburn
                }
303 8306 acydburn
                while (time() <= $start + $timeout && !feof($this->connection) && ($wait || $data == '' || $read != '' || (substr(rtrim($data), -1) != '>')));
304 4141 psotfx
305 7687 acydburn
                if ($data != '')
306 6436 acydburn
                {
307 7687 acydburn
                        $this->add_to_log('RECV: '. $data);
308 7687 acydburn
                        return $this->xmlize($data);
309 6436 acydburn
                }
310 6436 acydburn
                else
311 6436 acydburn
                {
312 7687 acydburn
                        $this->add_to_log('Timeout, no response from server.');
313 6436 acydburn
                        return false;
314 6436 acydburn
                }
315 4141 psotfx
        }
316 4141 psotfx
317 6015 acydburn
        /**
318 7687 acydburn
        * Initiates account registration (based on data used for contructor)
319 7687 acydburn
        * @access public
320 7687 acydburn
        * @return bool
321 6015 acydburn
        */
322 7687 acydburn
        function register()
323 4141 psotfx
        {
324 7687 acydburn
                if (!isset($this->session['id']) || isset($this->session['jid']))
325 6436 acydburn
                {
326 7687 acydburn
                        $this->add_to_log('Error: Cannot initiate registration.');
327 6436 acydburn
                        return false;
328 6436 acydburn
                }
329 7687 acydburn
330 7687 acydburn
                $this->send("<iq type='get' id='reg_1'><query xmlns='jabber:iq:register'/></iq>");
331 7687 acydburn
                return $this->response($this->listen());
332 4141 psotfx
        }
333 4141 psotfx
334 6015 acydburn
        /**
335 7687 acydburn
        * Sets account presence. No additional info required (default is "online" status)
336 7687 acydburn
        * @param $message online, offline...
337 7687 acydburn
        * @param $type dnd, away, chat, xa or nothing
338 7699 acydburn
        * @param $unavailable set this to true if you want to become unavailable
339 7687 acydburn
        * @access public
340 7687 acydburn
        * @return bool
341 6015 acydburn
        */
342 7687 acydburn
        function send_presence($message = '', $type = '', $unavailable = false)
343 4141 psotfx
        {
344 7687 acydburn
                if (!isset($this->session['jid']))
345 4141 psotfx
                {
346 7687 acydburn
                        $this->add_to_log('ERROR: send_presence() - Cannot set presence at this point, no jid given.');
347 7687 acydburn
                        return false;
348 4141 psotfx
                }
349 4141 psotfx
350 7687 acydburn
                $type = strtolower($type);
351 7687 acydburn
                $type = (in_array($type, array('dnd', 'away', 'chat', 'xa'))) ? '<show>'. $type .'</show>' : '';
352 6436 acydburn
353 7687 acydburn
                $unavailable = ($unavailable) ? " type='unavailable'" : '';
354 7687 acydburn
                $message = ($message) ? '<status>' . utf8_htmlspecialchars($message) .'</status>' : '';
355 6436 acydburn
356 7687 acydburn
                $this->session['sent_presence'] = !$unavailable;
357 4141 psotfx
358 7687 acydburn
                return $this->send("<presence$unavailable>" . $type . $message . '</presence>');
359 4141 psotfx
        }
360 4141 psotfx
361 6015 acydburn
        /**
362 7687 acydburn
        * This handles all the different XML elements
363 7687 acydburn
        * @param array $xml
364 7687 acydburn
        * @access public
365 7687 acydburn
        * @return bool
366 6015 acydburn
        */
367 7687 acydburn
        function response($xml)
368 4141 psotfx
        {
369 7687 acydburn
                if (!is_array($xml) || !sizeof($xml))
370 6436 acydburn
                {
371 6436 acydburn
                        return false;
372 4141 psotfx
                }
373 4141 psotfx
374 7687 acydburn
                // did we get multiple elements? do one after another
375 7687 acydburn
                // array('message' => ..., 'presence' => ...)
376 7687 acydburn
                if (sizeof($xml) > 1)
377 6436 acydburn
                {
378 7687 acydburn
                        foreach ($xml as $key => $value)
379 7687 acydburn
                        {
380 7687 acydburn
                                $this->response(array($key => $value));
381 7687 acydburn
                        }
382 7687 acydburn
                        return;
383 6436 acydburn
                }
384 7687 acydburn
                else
385 6436 acydburn
                {
386 7687 acydburn
                        // or even multiple elements of the same type?
387 7687 acydburn
                        // array('message' => array(0 => ..., 1 => ...))
388 7687 acydburn
                        if (sizeof(reset($xml)) > 1)
389 7687 acydburn
                        {
390 7687 acydburn
                                foreach (reset($xml) as $value)
391 7687 acydburn
                                {
392 7687 acydburn
                                        $this->response(array(key($xml) => array(0 => $value)));
393 7687 acydburn
                                }
394 7687 acydburn
                                return;
395 7687 acydburn
                        }
396 6436 acydburn
                }
397 6771 acydburn
398 7687 acydburn
                switch (key($xml))
399 6436 acydburn
                {
400 7687 acydburn
                        case 'stream:stream':
401 7687 acydburn
                                // Connection initialised (or after authentication). Not much to do here...
402 6436 acydburn
403 7687 acydburn
                                if (isset($xml['stream:stream'][0]['#']['stream:features']))
404 6034 naderman
                                {
405 7687 acydburn
                                        // we already got all info we need
406 7687 acydburn
                                        $this->features = $xml['stream:stream'][0]['#'];
407 6034 naderman
                                }
408 6034 naderman
                                else
409 6034 naderman
                                {
410 7687 acydburn
                                        $this->features = $this->listen();
411 6034 naderman
                                }
412 4141 psotfx
413 8306 acydburn
                                $second_time = isset($this->session['id']);
414 8306 acydburn
                                $this->session['id'] = $xml['stream:stream'][0]['@']['id'];
415 8306 acydburn
416 8306 acydburn
                                if ($second_time)
417 8306 acydburn
                                {
418 8306 acydburn
                                        // If we are here for the second time after TLS, we need to continue logging in
419 8694 acydburn
                                        return $this->login();
420 8694 acydburn
                                }
421 8306 acydburn
422 7687 acydburn
                                // go on with authentication?
423 7860 acydburn
                                if (isset($this->features['stream:features'][0]['#']['bind']) || !empty($this->session['tls']))
424 7687 acydburn
                                {
425 7687 acydburn
                                        return $this->response($this->features);
426 7687 acydburn
                                }
427 7687 acydburn
                        break;
428 4141 psotfx
429 7687 acydburn
                        case 'stream:features':
430 7687 acydburn
                                // Resource binding after successful authentication
431 7687 acydburn
                                if (isset($this->session['authenticated']))
432 7687 acydburn
                                {
433 7687 acydburn
                                        // session required?
434 7687 acydburn
                                        $this->session['sess_required'] = isset($xml['stream:features'][0]['#']['session']);
435 4141 psotfx
436 7687 acydburn
                                        $this->send("<iq type='set' id='bind_1'>
437 7687 acydburn
                                                <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
438 7836 acydburn
                                                        <resource>" . utf8_htmlspecialchars($this->resource) . '</resource>
439 7687 acydburn
                                                </bind>
440 7836 acydburn
                                        </iq>');
441 7687 acydburn
                                        return $this->response($this->listen());
442 7687 acydburn
                                }
443 4141 psotfx
444 7687 acydburn
                                // Let's use TLS if SSL is not enabled and we can actually use it
445 8025 acydburn
                                if (!$this->session['ssl'] && $this->can_use_tls() && $this->can_use_ssl() && isset($xml['stream:features'][0]['#']['starttls']))
446 7687 acydburn
                                {
447 7687 acydburn
                                        $this->add_to_log('Switching to TLS.');
448 7687 acydburn
                                        $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>\n");
449 7687 acydburn
                                        return $this->response($this->listen());
450 7687 acydburn
                                }
451 4141 psotfx
452 7687 acydburn
                                // Does the server support SASL authentication?
453 7699 acydburn
454 7687 acydburn
                                // I hope so, because we do (and no other method).
455 7687 acydburn
                                if (isset($xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns']) && $xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns'] == 'urn:ietf:params:xml:ns:xmpp-sasl')
456 7687 acydburn
                                {
457 7687 acydburn
                                        // Now decide on method
458 7687 acydburn
                                        $methods = array();
459 4141 psotfx
460 7687 acydburn
                                        foreach ($xml['stream:features'][0]['#']['mechanisms'][0]['#']['mechanism'] as $value)
461 7687 acydburn
                                        {
462 7687 acydburn
                                                $methods[] = $value['#'];
463 7687 acydburn
                                        }
464 4141 psotfx
465 7699 acydburn
                                        // we prefer DIGEST-MD5
466 7699 acydburn
                                        // we don't want to use plain authentication (neither does the server usually) if no encryption is in place
467 7699 acydburn
468 7699 acydburn
                                        // http://www.xmpp.org/extensions/attic/jep-0078-1.7.html
469 7699 acydburn
                                        // The plaintext mechanism SHOULD NOT be used unless the underlying stream is encrypted (using SSL or TLS)
470 7699 acydburn
                                        // and the client has verified that the server certificate is signed by a trusted certificate authority.
471 7699 acydburn
472 7687 acydburn
                                        if (in_array('DIGEST-MD5', $methods))
473 7687 acydburn
                                        {
474 7687 acydburn
                                                $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>");
475 7687 acydburn
                                        }
476 7699 acydburn
                                        else if (in_array('PLAIN', $methods) && ($this->session['ssl'] || !empty($this->session['tls'])))
477 7687 acydburn
                                        {
478 10342 acydburn
                                                // http://www.ietf.org/rfc/rfc4616.txt (PLAIN SASL Mechanism)
479 7687 acydburn
                                                $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>"
480 10342 acydburn
                                                        . base64_encode($this->username . '@' . $this->server . chr(0) . $this->username . chr(0) . $this->password) .
481 7687 acydburn
                                                        '</auth>');
482 7687 acydburn
                                        }
483 7699 acydburn
                                        else if (in_array('ANONYMOUS', $methods))
484 7699 acydburn
                                        {
485 7699 acydburn
                                                $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
486 7699 acydburn
                                        }
487 7687 acydburn
                                        else
488 7687 acydburn
                                        {
489 7687 acydburn
                                                // not good...
490 7687 acydburn
                                                $this->add_to_log('Error: No authentication method supported.');
491 7687 acydburn
                                                $this->disconnect();
492 7687 acydburn
                                                return false;
493 7687 acydburn
                                        }
494 4141 psotfx
495 7687 acydburn
                                        return $this->response($this->listen());
496 7687 acydburn
                                }
497 7687 acydburn
                                else
498 7687 acydburn
                                {
499 7687 acydburn
                                        // ok, this is it. bye.
500 7687 acydburn
                                        $this->add_to_log('Error: Server does not offer SASL authentication.');
501 7687 acydburn
                                        $this->disconnect();
502 7687 acydburn
                                        return false;
503 7687 acydburn
                                }
504 7687 acydburn
                        break;
505 4141 psotfx
506 7687 acydburn
                        case 'challenge':
507 7687 acydburn
                                // continue with authentication...a challenge literally -_-
508 7687 acydburn
                                $decoded = base64_decode($xml['challenge'][0]['#']);
509 7687 acydburn
                                $decoded = $this->parse_data($decoded);
510 6436 acydburn
511 7687 acydburn
                                if (!isset($decoded['digest-uri']))
512 7687 acydburn
                                {
513 7687 acydburn
                                        $decoded['digest-uri'] = 'xmpp/'. $this->server;
514 7687 acydburn
                                }
515 4141 psotfx
516 7687 acydburn
                                // better generate a cnonce, maybe it's needed
517 8979 acydburn
                                $decoded['cnonce'] = base64_encode(md5(uniqid(mt_rand(), true)));
518 4141 psotfx
519 7687 acydburn
                                // second challenge?
520 7687 acydburn
                                if (isset($decoded['rspauth']))
521 7687 acydburn
                                {
522 7687 acydburn
                                        $this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>");
523 7687 acydburn
                                }
524 7687 acydburn
                                else
525 7687 acydburn
                                {
526 8090 acydburn
                                        // Make sure we only use 'auth' for qop (relevant for $this->encrypt_password())
527 8090 acydburn
                                        // If the <response> is choking up on the changed parameter we may need to adjust encrypt_password() directly
528 8090 acydburn
                                        if (isset($decoded['qop']) && $decoded['qop'] != 'auth' && strpos($decoded['qop'], 'auth') !== false)
529 8090 acydburn
                                        {
530 8090 acydburn
                                                $decoded['qop'] = 'auth';
531 8090 acydburn
                                        }
532 8090 acydburn
533 7687 acydburn
                                        $response = array(
534 7687 acydburn
                                                'username'        => $this->username,
535 7687 acydburn
                                                'response'        => $this->encrypt_password(array_merge($decoded, array('nc' => '00000001'))),
536 7687 acydburn
                                                'charset'        => 'utf-8',
537 7687 acydburn
                                                'nc'                => '00000001',
538 8306 acydburn
                                                'qop'                => 'auth',                        // only auth being supported
539 7687 acydburn
                                        );
540 4141 psotfx
541 8306 acydburn
                                        foreach (array('nonce', 'digest-uri', 'realm', 'cnonce') as $key)
542 7687 acydburn
                                        {
543 7687 acydburn
                                                if (isset($decoded[$key]))
544 7687 acydburn
                                                {
545 7687 acydburn
                                                        $response[$key] = $decoded[$key];
546 7687 acydburn
                                                }
547 7687 acydburn
                                        }
548 4141 psotfx
549 7687 acydburn
                                        $this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" . base64_encode($this->implode_data($response)) . '</response>');
550 7687 acydburn
                                }
551 4141 psotfx
552 7687 acydburn
                                return $this->response($this->listen());
553 7687 acydburn
                        break;
554 4141 psotfx
555 7687 acydburn
                        case 'failure':
556 7687 acydburn
                                $this->add_to_log('Error: Server sent "failure".');
557 7687 acydburn
                                $this->disconnect();
558 7687 acydburn
                                return false;
559 7687 acydburn
                        break;
560 4141 psotfx
561 7687 acydburn
                        case 'proceed':
562 7687 acydburn
                                // continue switching to TLS
563 7687 acydburn
                                $meta = stream_get_meta_data($this->connection);
564 7687 acydburn
                                socket_set_blocking($this->connection, 1);
565 4141 psotfx
566 7687 acydburn
                                if (!stream_socket_enable_crypto($this->connection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT))
567 7687 acydburn
                                {
568 7687 acydburn
                                        $this->add_to_log('Error: TLS mode change failed.');
569 7687 acydburn
                                        return false;
570 7687 acydburn
                                }
571 6436 acydburn
572 7687 acydburn
                                socket_set_blocking($this->connection, $meta['blocked']);
573 7687 acydburn
                                $this->session['tls'] = true;
574 6436 acydburn
575 7687 acydburn
                                // new stream
576 7687 acydburn
                                $this->send("<?xml version='1.0' encoding='UTF-8' ?" . ">\n");
577 7687 acydburn
                                $this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
578 6436 acydburn
579 7687 acydburn
                                return $this->response($this->listen());
580 7687 acydburn
                        break;
581 4141 psotfx
582 7687 acydburn
                        case 'success':
583 7687 acydburn
                                // Yay, authentication successful.
584 7687 acydburn
                                $this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
585 7687 acydburn
                                $this->session['authenticated'] = true;
586 4141 psotfx
587 7687 acydburn
                                // we have to wait for another response
588 7687 acydburn
                                return $this->response($this->listen());
589 7687 acydburn
                        break;
590 6436 acydburn
591 7687 acydburn
                        case 'iq':
592 7687 acydburn
                                // we are not interested in IQs we did not expect
593 7687 acydburn
                                if (!isset($xml['iq'][0]['@']['id']))
594 7687 acydburn
                                {
595 7687 acydburn
                                        return false;
596 7687 acydburn
                                }
597 6436 acydburn
598 7687 acydburn
                                // multiple possibilities here
599 7687 acydburn
                                switch ($xml['iq'][0]['@']['id'])
600 7687 acydburn
                                {
601 7687 acydburn
                                        case 'bind_1':
602 7687 acydburn
                                                $this->session['jid'] = $xml['iq'][0]['#']['bind'][0]['#']['jid'][0]['#'];
603 6436 acydburn
604 7687 acydburn
                                                // and (maybe) yet another request to be able to send messages *finally*
605 7687 acydburn
                                                if ($this->session['sess_required'])
606 7687 acydburn
                                                {
607 7687 acydburn
                                                        $this->send("<iq to='{$this->server}' type='set' id='sess_1'>
608 7687 acydburn
                                                                <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>
609 7687 acydburn
                                                                </iq>");
610 7687 acydburn
                                                        return $this->response($this->listen());
611 7687 acydburn
                                                }
612 6436 acydburn
613 7687 acydburn
                                                return true;
614 7687 acydburn
                                        break;
615 6436 acydburn
616 7687 acydburn
                                        case 'sess_1':
617 7687 acydburn
                                                return true;
618 7687 acydburn
                                        break;
619 6436 acydburn
620 7687 acydburn
                                        case 'reg_1':
621 7687 acydburn
                                                $this->send("<iq type='set' id='reg_2'>
622 7687 acydburn
                                                                <query xmlns='jabber:iq:register'>
623 7687 acydburn
                                                                        <username>" . utf8_htmlspecialchars($this->username) . "</username>
624 7687 acydburn
                                                                        <password>" . utf8_htmlspecialchars($this->password) . "</password>
625 7687 acydburn
                                                                </query>
626 7687 acydburn
                                                        </iq>");
627 7687 acydburn
                                                return $this->response($this->listen());
628 7687 acydburn
                                        break;
629 4141 psotfx
630 7687 acydburn
                                        case 'reg_2':
631 7687 acydburn
                                                // registration end
632 7687 acydburn
                                                if (isset($xml['iq'][0]['#']['error']))
633 7687 acydburn
                                                {
634 7687 acydburn
                                                        $this->add_to_log('Warning: Registration failed.');
635 7687 acydburn
                                                        return false;
636 7687 acydburn
                                                }
637 7687 acydburn
                                                return true;
638 7687 acydburn
                                        break;
639 6436 acydburn
640 7687 acydburn
                                        case 'unreg_1':
641 7687 acydburn
                                                return true;
642 7687 acydburn
                                        break;
643 6436 acydburn
644 7687 acydburn
                                        default:
645 7687 acydburn
                                                $this->add_to_log('Notice: Received unexpected IQ.');
646 7687 acydburn
                                                return false;
647 7687 acydburn
                                        break;
648 7687 acydburn
                                }
649 7687 acydburn
                        break;
650 6436 acydburn
651 7687 acydburn
                        case 'message':
652 7687 acydburn
                                // we are only interested in content...
653 7687 acydburn
                                if (!isset($xml['message'][0]['#']['body']))
654 7687 acydburn
                                {
655 7687 acydburn
                                        return false;
656 7687 acydburn
                                }
657 6436 acydburn
658 7687 acydburn
                                $message['body'] = $xml['message'][0]['#']['body'][0]['#'];
659 7687 acydburn
                                $message['from'] = $xml['message'][0]['@']['from'];
660 4141 psotfx
661 7687 acydburn
                                if (isset($xml['message'][0]['#']['subject']))
662 7687 acydburn
                                {
663 7687 acydburn
                                        $message['subject'] = $xml['message'][0]['#']['subject'][0]['#'];
664 7687 acydburn
                                }
665 7687 acydburn
                                $this->session['messages'][] = $message;
666 7687 acydburn
                        break;
667 4141 psotfx
668 7687 acydburn
                        default:
669 7687 acydburn
                                // hm...don't know this response
670 7687 acydburn
                                $this->add_to_log('Notice: Unknown server response (' . key($xml) . ')');
671 7687 acydburn
                                return false;
672 7687 acydburn
                        break;
673 7687 acydburn
                }
674 6436 acydburn
        }
675 6436 acydburn
676 7687 acydburn
        function send_message($to, $text, $subject = '', $type = 'normal')
677 6436 acydburn
        {
678 7687 acydburn
                if (!isset($this->session['jid']))
679 7687 acydburn
                {
680 7687 acydburn
                        return false;
681 7687 acydburn
                }
682 6436 acydburn
683 7687 acydburn
                if (!in_array($type, array('chat', 'normal', 'error', 'groupchat', 'headline')))
684 7687 acydburn
                {
685 7687 acydburn
                        $type = 'normal';
686 7687 acydburn
                }
687 7687 acydburn
688 7687 acydburn
                return $this->send("<message from='" . utf8_htmlspecialchars($this->session['jid']) . "' to='" . utf8_htmlspecialchars($to) . "' type='$type' id='" . uniqid('msg') . "'>
689 7687 acydburn
                        <subject>" . utf8_htmlspecialchars($subject) . "</subject>
690 7687 acydburn
                        <body>" . utf8_htmlspecialchars($text) . "</body>
691 7687 acydburn
                        </message>"
692 7687 acydburn
                );
693 6436 acydburn
        }
694 6436 acydburn
695 6436 acydburn
        /**
696 7687 acydburn
        * Encrypts a password as in RFC 2831
697 7687 acydburn
        * @param array $data Needs data from the client-server connection
698 7687 acydburn
        * @access public
699 7687 acydburn
        * @return string
700 6436 acydburn
        */
701 7687 acydburn
        function encrypt_password($data)
702 6436 acydburn
        {
703 7687 acydburn
                // let's me think about <challenge> again...
704 7687 acydburn
                foreach (array('realm', 'cnonce', 'digest-uri') as $key)
705 4141 psotfx
                {
706 7687 acydburn
                        if (!isset($data[$key]))
707 7687 acydburn
                        {
708 7687 acydburn
                                $data[$key] = '';
709 7687 acydburn
                        }
710 4141 psotfx
                }
711 6436 acydburn
712 7687 acydburn
                $pack = md5($this->username . ':' . $data['realm'] . ':' . $this->password);
713 6436 acydburn
714 7687 acydburn
                if (isset($data['authzid']))
715 6436 acydburn
                {
716 7687 acydburn
                        $a1 = pack('H32', $pack)  . sprintf(':%s:%s:%s', $data['nonce'], $data['cnonce'], $data['authzid']);
717 6436 acydburn
                }
718 7687 acydburn
                else
719 7687 acydburn
                {
720 7687 acydburn
                        $a1 = pack('H32', $pack)  . sprintf(':%s:%s', $data['nonce'], $data['cnonce']);
721 7687 acydburn
                }
722 6436 acydburn
723 7687 acydburn
                // should be: qop = auth
724 7687 acydburn
                $a2 = 'AUTHENTICATE:'. $data['digest-uri'];
725 6436 acydburn
726 7687 acydburn
                return md5(sprintf('%s:%s:%s:%s:%s:%s', md5($a1), $data['nonce'], $data['nc'], $data['cnonce'], $data['qop'], md5($a2)));
727 6436 acydburn
        }
728 6436 acydburn
729 6436 acydburn
        /**
730 8087 acydburn
        * parse_data like a="b",c="d",... or like a="a, b", c, d="e", f=g,...
731 7687 acydburn
        * @param string $data
732 7687 acydburn
        * @access public
733 7687 acydburn
        * @return array a => b ...
734 6436 acydburn
        */
735 7687 acydburn
        function parse_data($data)
736 6436 acydburn
        {
737 7687 acydburn
                $data = explode(',', $data);
738 7687 acydburn
                $pairs = array();
739 8087 acydburn
                $key = false;
740 6436 acydburn
741 7687 acydburn
                foreach ($data as $pair)
742 6436 acydburn
                {
743 7687 acydburn
                        $dd = strpos($pair, '=');
744 8087 acydburn
745 7687 acydburn
                        if ($dd)
746 7687 acydburn
                        {
747 8087 acydburn
                                $key = trim(substr($pair, 0, $dd));
748 8087 acydburn
                                $pairs[$key] = trim(trim(substr($pair, $dd + 1)), '"');
749 7687 acydburn
                        }
750 8087 acydburn
                        else if (strpos(strrev(trim($pair)), '"') === 0 && $key)
751 8087 acydburn
                        {
752 8087 acydburn
                                // We are actually having something left from "a, b" values, add it to the last one we handled.
753 8087 acydburn
                                $pairs[$key] .= ',' . trim(trim($pair), '"');
754 8087 acydburn
                                continue;
755 8087 acydburn
                        }
756 6436 acydburn
                }
757 8087 acydburn
758 7687 acydburn
                return $pairs;
759 6436 acydburn
        }
760 6436 acydburn
761 6015 acydburn
        /**
762 7687 acydburn
        * opposite of jabber::parse_data()
763 7687 acydburn
        * @param array $data
764 7687 acydburn
        * @access public
765 7687 acydburn
        * @return string
766 6015 acydburn
        */
767 7687 acydburn
        function implode_data($data)
768 4141 psotfx
        {
769 7687 acydburn
                $return = array();
770 7687 acydburn
                foreach ($data as $key => $value)
771 7687 acydburn
                {
772 7687 acydburn
                        $return[] = $key . '="' . $value . '"';
773 7687 acydburn
                }
774 7687 acydburn
                return implode(',', $return);
775 4141 psotfx
        }
776 4141 psotfx
777 6015 acydburn
        /**
778 6015 acydburn
        * xmlize()
779 6436 acydburn
        * @author Hans Anderson
780 6436 acydburn
        * @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
781 6015 acydburn
        */
782 6436 acydburn
        function xmlize($data, $skip_white = 1, $encoding = 'UTF-8')
783 4141 psotfx
        {
784 6436 acydburn
                $data = trim($data);
785 6436 acydburn
786 7687 acydburn
                if (substr($data, 0, 5) != '<?xml')
787 7687 acydburn
                {
788 7687 acydburn
                        // mod
789 7687 acydburn
                        $data = '<root>'. $data . '</root>';
790 7687 acydburn
                }
791 7687 acydburn
792 4141 psotfx
                $vals = $index = $array = array();
793 6436 acydburn
                $parser = xml_parser_create($encoding);
794 6436 acydburn
                xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
795 6436 acydburn
                xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $skip_white);
796 6436 acydburn
                xml_parse_into_struct($parser, $data, $vals, $index);
797 6436 acydburn
                xml_parser_free($parser);
798 4141 psotfx
799 4141 psotfx
                $i = 0;
800 6436 acydburn
                $tagname = $vals[$i]['tag'];
801 4141 psotfx
802 7687 acydburn
                $array[$tagname][0]['@'] = (isset($vals[$i]['attributes'])) ? $vals[$i]['attributes'] : array();
803 7687 acydburn
                $array[$tagname][0]['#'] = $this->_xml_depth($vals, $i);
804 4141 psotfx
805 7687 acydburn
                if (substr($data, 0, 5) != '<?xml')
806 7687 acydburn
                {
807 7687 acydburn
                        $array = $array['root'][0]['#'];
808 7687 acydburn
                }
809 7687 acydburn
810 4141 psotfx
                return $array;
811 4141 psotfx
        }
812 4141 psotfx
813 6015 acydburn
        /**
814 6015 acydburn
        * _xml_depth()
815 6436 acydburn
        * @author Hans Anderson
816 6436 acydburn
        * @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
817 6015 acydburn
        */
818 4141 psotfx
        function _xml_depth($vals, &$i)
819 4141 psotfx
        {
820 4141 psotfx
                $children = array();
821 4141 psotfx
822 6436 acydburn
                if (isset($vals[$i]['value']))
823 4141 psotfx
                {
824 6436 acydburn
                        array_push($children, $vals[$i]['value']);
825 4141 psotfx
                }
826 4141 psotfx
827 5116 acydburn
                while (++$i < sizeof($vals))
828 4141 psotfx
                {
829 4141 psotfx
                        switch ($vals[$i]['type'])
830 4141 psotfx
                        {
831 6436 acydburn
                                case 'open':
832 4141 psotfx
833 6436 acydburn
                                        $tagname = (isset($vals[$i]['tag'])) ? $vals[$i]['tag'] : '';
834 5116 acydburn
                                        $size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
835 6436 acydburn
836 6436 acydburn
                                        if (isset($vals[$i]['attributes']))
837 4141 psotfx
                                        {
838 4141 psotfx
                                                $children[$tagname][$size]['@'] = $vals[$i]['attributes'];
839 4141 psotfx
                                        }
840 6436 acydburn
841 6436 acydburn
                                        $children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i);
842 6436 acydburn
843 6015 acydburn
                                break;
844 4141 psotfx
845 6436 acydburn
                                case 'cdata':
846 6436 acydburn
                                        array_push($children, $vals[$i]['value']);
847 6436 acydburn
                                break;
848 6436 acydburn
849 6436 acydburn
                                case 'complete':
850 6436 acydburn
851 4141 psotfx
                                        $tagname = $vals[$i]['tag'];
852 5116 acydburn
                                        $size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
853 6436 acydburn
                                        $children[$tagname][$size]['#'] = (isset($vals[$i]['value'])) ? $vals[$i]['value'] : array();
854 6436 acydburn
855 6436 acydburn
                                        if (isset($vals[$i]['attributes']))
856 4141 psotfx
                                        {
857 4141 psotfx
                                                $children[$tagname][$size]['@'] = $vals[$i]['attributes'];
858 4141 psotfx
                                        }
859 6436 acydburn
860 6015 acydburn
                                break;
861 4141 psotfx
862 4141 psotfx
                                case 'close':
863 4141 psotfx
                                        return $children;
864 6015 acydburn
                                break;
865 4141 psotfx
                        }
866 4141 psotfx
                }
867 4141 psotfx
868 4141 psotfx
                return $children;
869 4141 psotfx
        }
870 4141 psotfx
}