phpBB
Statistics
| Revision:

root / branches / phpBB-3_0_0 / phpBB / includes / functions_jabber.php

History | View | Annotate | Download (21.4 kB)

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