includes/functions_jabber.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_jabber.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,873 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2007 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +*
    1.24 +* Jabber class from Flyspray project
    1.25 +*
    1.26 +* @version class.jabber2.php 1595 2008-09-19 (0.9.9)
    1.27 +* @copyright 2006 Flyspray.org
    1.28 +* @author Florian Schmitz (floele)
    1.29 +*
    1.30 +* Only slightly modified by Acyd Burn
    1.31 +*
    1.32 +* @package phpBB3
    1.33 +*/
    1.34 +class jabber
    1.35 +{
    1.36 +	var $connection = null;
    1.37 +	var $session = array();
    1.38 +	var $timeout = 10;
    1.39 +
    1.40 +	var $server;
    1.41 +	var $connect_server;
    1.42 +	var $port;
    1.43 +	var $username;
    1.44 +	var $password;
    1.45 +	var $use_ssl;
    1.46 +	var $resource = 'functions_jabber.phpbb.php';
    1.47 +
    1.48 +	var $enable_logging;
    1.49 +	var $log_array;
    1.50 +
    1.51 +	var $features = array();
    1.52 +
    1.53 +	/**
    1.54 +	*/
    1.55 +	function jabber($server, $port, $username, $password, $use_ssl = false)
    1.56 +	{
    1.57 +		$this->connect_server		= ($server) ? $server : 'localhost';
    1.58 +		$this->port					= ($port) ? $port : 5222;
    1.59 +
    1.60 +		// Get the server and the username
    1.61 +		if (strpos($username, '@') === false)
    1.62 +		{
    1.63 +			$this->server = $this->connect_server;
    1.64 +			$this->username = $username;
    1.65 +		}
    1.66 +		else
    1.67 +		{
    1.68 +			$jid = explode('@', $username, 2);
    1.69 +
    1.70 +			$this->username = $jid[0];
    1.71 +			$this->server = $jid[1];
    1.72 +		}
    1.73 +
    1.74 +		$this->password				= $password;
    1.75 +		$this->use_ssl				= ($use_ssl && $this->can_use_ssl()) ? true : false;
    1.76 +
    1.77 +		// Change port if we use SSL
    1.78 +		if ($this->port == 5222 && $this->use_ssl)
    1.79 +		{
    1.80 +			$this->port = 5223;
    1.81 +		}
    1.82 +
    1.83 +		$this->enable_logging		= true;
    1.84 +		$this->log_array			= array();
    1.85 +	}
    1.86 +
    1.87 +	/**
    1.88 +	* Able to use the SSL functionality?
    1.89 +	*/
    1.90 +	function can_use_ssl()
    1.91 +	{
    1.92 +		// 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)
    1.93 +		return ((version_compare(PHP_VERSION, '5.2.1', '<') || version_compare(PHP_VERSION, '5.2.3RC2', '>=')) && @extension_loaded('openssl')) ? true : false;
    1.94 +	}
    1.95 +
    1.96 +	/**
    1.97 +	* Able to use TLS?
    1.98 +	*/
    1.99 +	function can_use_tls()
   1.100 +	{
   1.101 +		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'))
   1.102 +		{
   1.103 +			return false;
   1.104 +		}
   1.105 +
   1.106 +		/**
   1.107 +		* Make sure the encryption stream is supported
   1.108 +		* Also seem to work without the crypto stream if correctly compiled
   1.109 +
   1.110 +		$streams = stream_get_wrappers();
   1.111 +
   1.112 +		if (!in_array('streams.crypto', $streams))
   1.113 +		{
   1.114 +			return false;
   1.115 +		}
   1.116 +		*/
   1.117 +
   1.118 +		return true;
   1.119 +	}
   1.120 +
   1.121 +	/**
   1.122 +	* Sets the resource which is used. No validation is done here, only escaping.
   1.123 +	* @param string $name
   1.124 +	* @access public
   1.125 +	*/
   1.126 +	function set_resource($name)
   1.127 +	{
   1.128 +		$this->resource = $name;
   1.129 +	}
   1.130 +
   1.131 +	/**
   1.132 +	* Connect
   1.133 +	*/
   1.134 +	function connect()
   1.135 +	{
   1.136 +/*		if (!$this->check_jid($this->username . '@' . $this->server))
   1.137 +		{
   1.138 +			$this->add_to_log('Error: Jabber ID is not valid: ' . $this->username . '@' . $this->server);
   1.139 +			return false;
   1.140 +		}*/
   1.141 +
   1.142 +		$this->session['ssl'] = $this->use_ssl;
   1.143 +
   1.144 +		if ($this->open_socket($this->connect_server, $this->port, $this->use_ssl))
   1.145 +		{
   1.146 +			$this->send("<?xml version='1.0' encoding='UTF-8' ?" . ">\n");
   1.147 +			$this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
   1.148 +		}
   1.149 +		else
   1.150 +		{
   1.151 +			$this->add_to_log('Error: connect() #2');
   1.152 +			return false;
   1.153 +		}
   1.154 +
   1.155 +		// Now we listen what the server has to say...and give appropriate responses
   1.156 +		$this->response($this->listen());
   1.157 +		return true;
   1.158 +	}
   1.159 +
   1.160 +	/**
   1.161 +	* Disconnect
   1.162 +	*/
   1.163 +	function disconnect()
   1.164 +	{
   1.165 +		if ($this->connected())
   1.166 +		{
   1.167 +			// disconnect gracefully
   1.168 +			if (isset($this->session['sent_presence']))
   1.169 +			{
   1.170 +				$this->send_presence('offline', '', true);
   1.171 +			}
   1.172 +
   1.173 +			$this->send('</stream:stream>');
   1.174 +			$this->session = array();
   1.175 +			return fclose($this->connection);
   1.176 +		}
   1.177 +
   1.178 +		return false;
   1.179 +	}
   1.180 +
   1.181 +	/**
   1.182 +	* Connected?
   1.183 +	*/
   1.184 +	function connected()
   1.185 +	{
   1.186 +		return (is_resource($this->connection) && !feof($this->connection)) ? true : false;
   1.187 +	}
   1.188 +
   1.189 +
   1.190 +	/**
   1.191 +	* Initiates login (using data from contructor, after calling connect())
   1.192 +	* @access public
   1.193 +	* @return bool
   1.194 +	*/
   1.195 +	function login()
   1.196 +	{
   1.197 +		if (!sizeof($this->features))
   1.198 +		{
   1.199 +			$this->add_to_log('Error: No feature information from server available.');
   1.200 +			return false;
   1.201 +		}
   1.202 +
   1.203 +		return $this->response($this->features);
   1.204 +	}
   1.205 +
   1.206 +	/**
   1.207 +	* Send data to the Jabber server
   1.208 +	* @param string $xml
   1.209 +	* @access public
   1.210 +	* @return bool
   1.211 +	*/
   1.212 +	function send($xml)
   1.213 +	{
   1.214 +		if ($this->connected())
   1.215 +		{
   1.216 +			$xml = trim($xml);
   1.217 +			$this->add_to_log('SEND: '. $xml);
   1.218 +			return fwrite($this->connection, $xml);
   1.219 +		}
   1.220 +		else
   1.221 +		{
   1.222 +			$this->add_to_log('Error: Could not send, connection lost (flood?).');
   1.223 +			return false;
   1.224 +		}
   1.225 +	}
   1.226 +
   1.227 +	/**
   1.228 +	* OpenSocket
   1.229 +	* @param string $server host to connect to
   1.230 +	* @param int $port port number
   1.231 +	* @param bool $use_ssl use ssl or not
   1.232 +	* @access public
   1.233 +	* @return bool
   1.234 +	*/
   1.235 +	function open_socket($server, $port, $use_ssl = false)
   1.236 +	{
   1.237 +		if (@function_exists('dns_get_record'))
   1.238 +		{
   1.239 +			$record = @dns_get_record("_xmpp-client._tcp.$server", DNS_SRV);
   1.240 +			if (!empty($record) && !empty($record[0]['target']))
   1.241 +			{
   1.242 +				$server = $record[0]['target'];
   1.243 +			}
   1.244 +		}
   1.245 +
   1.246 +		$server = $use_ssl ? 'ssl://' . $server : $server;
   1.247 +
   1.248 +		if ($this->connection = @fsockopen($server, $port, $errorno, $errorstr, $this->timeout))
   1.249 +		{
   1.250 +			socket_set_blocking($this->connection, 0);
   1.251 +			socket_set_timeout($this->connection, 60);
   1.252 +
   1.253 +			return true;
   1.254 +		}
   1.255 +
   1.256 +		// Apparently an error occured...
   1.257 +		$this->add_to_log('Error: open_socket() - ' . $errorstr);
   1.258 +		return false;
   1.259 +	}
   1.260 +
   1.261 +	/**
   1.262 +	* Return log
   1.263 +	*/
   1.264 +	function get_log()
   1.265 +	{
   1.266 +		if ($this->enable_logging && sizeof($this->log_array))
   1.267 +		{
   1.268 +			return implode("<br /><br />", $this->log_array);
   1.269 +		}
   1.270 +
   1.271 +		return '';
   1.272 +	}
   1.273 +
   1.274 +	/**
   1.275 +	* Add information to log
   1.276 +	*/
   1.277 +	function add_to_log($string)
   1.278 +	{
   1.279 +		if ($this->enable_logging)
   1.280 +		{
   1.281 +			$this->log_array[] = utf8_htmlspecialchars($string);
   1.282 +		}
   1.283 +	}
   1.284 +
   1.285 +	/**
   1.286 +	* Listens to the connection until it gets data or the timeout is reached.
   1.287 +	* Thus, it should only be called if data is expected to be received.
   1.288 +	* @access public
   1.289 +	* @return mixed either false for timeout or an array with the received data
   1.290 +	*/
   1.291 +	function listen($timeout = 10, $wait = false)
   1.292 +	{
   1.293 +		if (!$this->connected())
   1.294 +		{
   1.295 +			return false;
   1.296 +		}
   1.297 +
   1.298 +		// Wait for a response until timeout is reached
   1.299 +		$start = time();
   1.300 +		$data = '';
   1.301 +
   1.302 +		do
   1.303 +		{
   1.304 +			$read = trim(fread($this->connection, 4096));
   1.305 +			$data .= $read;
   1.306 +		}
   1.307 +		while (time() <= $start + $timeout && !feof($this->connection) && ($wait || $data == '' || $read != '' || (substr(rtrim($data), -1) != '>')));
   1.308 +
   1.309 +		if ($data != '')
   1.310 +		{
   1.311 +			$this->add_to_log('RECV: '. $data);
   1.312 +			return $this->xmlize($data);
   1.313 +		}
   1.314 +		else
   1.315 +		{
   1.316 +			$this->add_to_log('Timeout, no response from server.');
   1.317 +			return false;
   1.318 +		}
   1.319 +	}
   1.320 +
   1.321 +	/**
   1.322 +	* Initiates account registration (based on data used for contructor)
   1.323 +	* @access public
   1.324 +	* @return bool
   1.325 +	*/
   1.326 +	function register()
   1.327 +	{
   1.328 +		if (!isset($this->session['id']) || isset($this->session['jid']))
   1.329 +		{
   1.330 +			$this->add_to_log('Error: Cannot initiate registration.');
   1.331 +			return false;
   1.332 +		}
   1.333 +
   1.334 +		$this->send("<iq type='get' id='reg_1'><query xmlns='jabber:iq:register'/></iq>");
   1.335 +		return $this->response($this->listen());
   1.336 +	}
   1.337 +
   1.338 +	/**
   1.339 +	* Sets account presence. No additional info required (default is "online" status)
   1.340 +	* @param $message online, offline...
   1.341 +	* @param $type dnd, away, chat, xa or nothing
   1.342 +	* @param $unavailable set this to true if you want to become unavailable
   1.343 +	* @access public
   1.344 +	* @return bool
   1.345 +	*/
   1.346 +	function send_presence($message = '', $type = '', $unavailable = false)
   1.347 +	{
   1.348 +		if (!isset($this->session['jid']))
   1.349 +		{
   1.350 +			$this->add_to_log('ERROR: send_presence() - Cannot set presence at this point, no jid given.');
   1.351 +			return false;
   1.352 +		}
   1.353 +
   1.354 +		$type = strtolower($type);
   1.355 +		$type = (in_array($type, array('dnd', 'away', 'chat', 'xa'))) ? '<show>'. $type .'</show>' : '';
   1.356 +
   1.357 +		$unavailable = ($unavailable) ? " type='unavailable'" : '';
   1.358 +		$message = ($message) ? '<status>' . utf8_htmlspecialchars($message) .'</status>' : '';
   1.359 +
   1.360 +		$this->session['sent_presence'] = !$unavailable;
   1.361 +
   1.362 +		return $this->send("<presence$unavailable>" . $type . $message . '</presence>');
   1.363 +	}
   1.364 +
   1.365 +	/**
   1.366 +	* This handles all the different XML elements
   1.367 +	* @param array $xml
   1.368 +	* @access public
   1.369 +	* @return bool
   1.370 +	*/
   1.371 +	function response($xml)
   1.372 +	{
   1.373 +		if (!is_array($xml) || !sizeof($xml))
   1.374 +		{
   1.375 +			return false;
   1.376 +		}
   1.377 +
   1.378 +		// did we get multiple elements? do one after another
   1.379 +		// array('message' => ..., 'presence' => ...)
   1.380 +		if (sizeof($xml) > 1)
   1.381 +		{
   1.382 +			foreach ($xml as $key => $value)
   1.383 +			{
   1.384 +				$this->response(array($key => $value));
   1.385 +			}
   1.386 +			return;
   1.387 +		}
   1.388 +		else
   1.389 +		{
   1.390 +			// or even multiple elements of the same type?
   1.391 +			// array('message' => array(0 => ..., 1 => ...))
   1.392 +			if (sizeof(reset($xml)) > 1)
   1.393 +			{
   1.394 +				foreach (reset($xml) as $value)
   1.395 +				{
   1.396 +					$this->response(array(key($xml) => array(0 => $value)));
   1.397 +				}
   1.398 +				return;
   1.399 +			}
   1.400 +		}
   1.401 +
   1.402 +		switch (key($xml))
   1.403 +		{
   1.404 +			case 'stream:stream':
   1.405 +				// Connection initialised (or after authentication). Not much to do here...
   1.406 +
   1.407 +				if (isset($xml['stream:stream'][0]['#']['stream:features']))
   1.408 +				{
   1.409 +					// we already got all info we need
   1.410 +					$this->features = $xml['stream:stream'][0]['#'];
   1.411 +				}
   1.412 +				else
   1.413 +				{
   1.414 +					$this->features = $this->listen();
   1.415 +				}
   1.416 +
   1.417 +				$second_time = isset($this->session['id']);
   1.418 +				$this->session['id'] = $xml['stream:stream'][0]['@']['id'];
   1.419 +
   1.420 +				if ($second_time)
   1.421 +				{
   1.422 +					// If we are here for the second time after TLS, we need to continue logging in
   1.423 +					return $this->login();
   1.424 +				}
   1.425 +
   1.426 +				// go on with authentication?
   1.427 +				if (isset($this->features['stream:features'][0]['#']['bind']) || !empty($this->session['tls']))
   1.428 +				{
   1.429 +					return $this->response($this->features);
   1.430 +				}
   1.431 +			break;
   1.432 +
   1.433 +			case 'stream:features':
   1.434 +				// Resource binding after successful authentication
   1.435 +				if (isset($this->session['authenticated']))
   1.436 +				{
   1.437 +					// session required?
   1.438 +					$this->session['sess_required'] = isset($xml['stream:features'][0]['#']['session']);
   1.439 +
   1.440 +					$this->send("<iq type='set' id='bind_1'>
   1.441 +						<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
   1.442 +							<resource>" . utf8_htmlspecialchars($this->resource) . '</resource>
   1.443 +						</bind>
   1.444 +					</iq>');
   1.445 +					return $this->response($this->listen());
   1.446 +				}
   1.447 +
   1.448 +				// Let's use TLS if SSL is not enabled and we can actually use it
   1.449 +				if (!$this->session['ssl'] && $this->can_use_tls() && $this->can_use_ssl() && isset($xml['stream:features'][0]['#']['starttls']))
   1.450 +				{
   1.451 +					$this->add_to_log('Switching to TLS.');
   1.452 +					$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>\n");
   1.453 +					return $this->response($this->listen());
   1.454 +				}
   1.455 +
   1.456 +				// Does the server support SASL authentication?
   1.457 +
   1.458 +				// I hope so, because we do (and no other method).
   1.459 +				if (isset($xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns']) && $xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns'] == 'urn:ietf:params:xml:ns:xmpp-sasl')
   1.460 +				{
   1.461 +					// Now decide on method
   1.462 +					$methods = array();
   1.463 +
   1.464 +					foreach ($xml['stream:features'][0]['#']['mechanisms'][0]['#']['mechanism'] as $value)
   1.465 +					{
   1.466 +						$methods[] = $value['#'];
   1.467 +					}
   1.468 +
   1.469 +					// we prefer DIGEST-MD5
   1.470 +					// we don't want to use plain authentication (neither does the server usually) if no encryption is in place
   1.471 +
   1.472 +					// http://www.xmpp.org/extensions/attic/jep-0078-1.7.html
   1.473 +					// The plaintext mechanism SHOULD NOT be used unless the underlying stream is encrypted (using SSL or TLS)
   1.474 +					// and the client has verified that the server certificate is signed by a trusted certificate authority.
   1.475 +
   1.476 +					if (in_array('DIGEST-MD5', $methods))
   1.477 +					{
   1.478 +						$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>");
   1.479 +					}
   1.480 +					else if (in_array('PLAIN', $methods) && ($this->session['ssl'] || !empty($this->session['tls'])))
   1.481 +					{
   1.482 +						// http://www.ietf.org/rfc/rfc4616.txt (PLAIN SASL Mechanism)
   1.483 +						$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>"
   1.484 +							. base64_encode($this->username . '@' . $this->server . chr(0) . $this->username . chr(0) . $this->password) .
   1.485 +							'</auth>');
   1.486 +					}
   1.487 +					else if (in_array('ANONYMOUS', $methods))
   1.488 +					{
   1.489 +						$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
   1.490 +					}
   1.491 +					else
   1.492 +					{
   1.493 +						// not good...
   1.494 +						$this->add_to_log('Error: No authentication method supported.');
   1.495 +						$this->disconnect();
   1.496 +						return false;
   1.497 +					}
   1.498 +
   1.499 +					return $this->response($this->listen());
   1.500 +				}
   1.501 +				else
   1.502 +				{
   1.503 +					// ok, this is it. bye.
   1.504 +					$this->add_to_log('Error: Server does not offer SASL authentication.');
   1.505 +					$this->disconnect();
   1.506 +					return false;
   1.507 +				}
   1.508 +			break;
   1.509 +
   1.510 +			case 'challenge':
   1.511 +				// continue with authentication...a challenge literally -_-
   1.512 +				$decoded = base64_decode($xml['challenge'][0]['#']);
   1.513 +				$decoded = $this->parse_data($decoded);
   1.514 +
   1.515 +				if (!isset($decoded['digest-uri']))
   1.516 +				{
   1.517 +					$decoded['digest-uri'] = 'xmpp/'. $this->server;
   1.518 +				}
   1.519 +
   1.520 +				// better generate a cnonce, maybe it's needed
   1.521 +				$decoded['cnonce'] = base64_encode(md5(uniqid(mt_rand(), true)));
   1.522 +
   1.523 +				// second challenge?
   1.524 +				if (isset($decoded['rspauth']))
   1.525 +				{
   1.526 +					$this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>");
   1.527 +				}
   1.528 +				else
   1.529 +				{
   1.530 +					// Make sure we only use 'auth' for qop (relevant for $this->encrypt_password())
   1.531 +					// If the <response> is choking up on the changed parameter we may need to adjust encrypt_password() directly
   1.532 +					if (isset($decoded['qop']) && $decoded['qop'] != 'auth' && strpos($decoded['qop'], 'auth') !== false)
   1.533 +					{
   1.534 +						$decoded['qop'] = 'auth';
   1.535 +					}
   1.536 +
   1.537 +					$response = array(
   1.538 +						'username'	=> $this->username,
   1.539 +						'response'	=> $this->encrypt_password(array_merge($decoded, array('nc' => '00000001'))),
   1.540 +						'charset'	=> 'utf-8',
   1.541 +						'nc'		=> '00000001',
   1.542 +						'qop'		=> 'auth',			// only auth being supported
   1.543 +					);
   1.544 +
   1.545 +					foreach (array('nonce', 'digest-uri', 'realm', 'cnonce') as $key)
   1.546 +					{
   1.547 +						if (isset($decoded[$key]))
   1.548 +						{
   1.549 +							$response[$key] = $decoded[$key];
   1.550 +						}
   1.551 +					}
   1.552 +
   1.553 +					$this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" . base64_encode($this->implode_data($response)) . '</response>');
   1.554 +				}
   1.555 +
   1.556 +				return $this->response($this->listen());
   1.557 +			break;
   1.558 +
   1.559 +			case 'failure':
   1.560 +				$this->add_to_log('Error: Server sent "failure".');
   1.561 +				$this->disconnect();
   1.562 +				return false;
   1.563 +			break;
   1.564 +
   1.565 +			case 'proceed':
   1.566 +				// continue switching to TLS
   1.567 +				$meta = stream_get_meta_data($this->connection);
   1.568 +				socket_set_blocking($this->connection, 1);
   1.569 +
   1.570 +				if (!stream_socket_enable_crypto($this->connection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT))
   1.571 +				{
   1.572 +					$this->add_to_log('Error: TLS mode change failed.');
   1.573 +					return false;
   1.574 +				}
   1.575 +
   1.576 +				socket_set_blocking($this->connection, $meta['blocked']);
   1.577 +				$this->session['tls'] = true;
   1.578 +
   1.579 +				// new stream
   1.580 +				$this->send("<?xml version='1.0' encoding='UTF-8' ?" . ">\n");
   1.581 +				$this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
   1.582 +
   1.583 +				return $this->response($this->listen());
   1.584 +			break;
   1.585 +
   1.586 +			case 'success':
   1.587 +				// Yay, authentication successful.
   1.588 +				$this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
   1.589 +				$this->session['authenticated'] = true;
   1.590 +
   1.591 +				// we have to wait for another response
   1.592 +				return $this->response($this->listen());
   1.593 +			break;
   1.594 +
   1.595 +			case 'iq':
   1.596 +				// we are not interested in IQs we did not expect
   1.597 +				if (!isset($xml['iq'][0]['@']['id']))
   1.598 +				{
   1.599 +					return false;
   1.600 +				}
   1.601 +
   1.602 +				// multiple possibilities here
   1.603 +				switch ($xml['iq'][0]['@']['id'])
   1.604 +				{
   1.605 +					case 'bind_1':
   1.606 +						$this->session['jid'] = $xml['iq'][0]['#']['bind'][0]['#']['jid'][0]['#'];
   1.607 +
   1.608 +						// and (maybe) yet another request to be able to send messages *finally*
   1.609 +						if ($this->session['sess_required'])
   1.610 +						{
   1.611 +							$this->send("<iq to='{$this->server}' type='set' id='sess_1'>
   1.612 +								<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>
   1.613 +								</iq>");
   1.614 +							return $this->response($this->listen());
   1.615 +						}
   1.616 +
   1.617 +						return true;
   1.618 +					break;
   1.619 +
   1.620 +					case 'sess_1':
   1.621 +						return true;
   1.622 +					break;
   1.623 +
   1.624 +					case 'reg_1':
   1.625 +						$this->send("<iq type='set' id='reg_2'>
   1.626 +								<query xmlns='jabber:iq:register'>
   1.627 +									<username>" . utf8_htmlspecialchars($this->username) . "</username>
   1.628 +									<password>" . utf8_htmlspecialchars($this->password) . "</password>
   1.629 +								</query>
   1.630 +							</iq>");
   1.631 +						return $this->response($this->listen());
   1.632 +					break;
   1.633 +
   1.634 +					case 'reg_2':
   1.635 +						// registration end
   1.636 +						if (isset($xml['iq'][0]['#']['error']))
   1.637 +						{
   1.638 +							$this->add_to_log('Warning: Registration failed.');
   1.639 +							return false;
   1.640 +						}
   1.641 +						return true;
   1.642 +					break;
   1.643 +
   1.644 +					case 'unreg_1':
   1.645 +						return true;
   1.646 +					break;
   1.647 +
   1.648 +					default:
   1.649 +						$this->add_to_log('Notice: Received unexpected IQ.');
   1.650 +						return false;
   1.651 +					break;
   1.652 +				}
   1.653 +			break;
   1.654 +
   1.655 +			case 'message':
   1.656 +				// we are only interested in content...
   1.657 +				if (!isset($xml['message'][0]['#']['body']))
   1.658 +				{
   1.659 +					return false;
   1.660 +				}
   1.661 +
   1.662 +				$message['body'] = $xml['message'][0]['#']['body'][0]['#'];
   1.663 +				$message['from'] = $xml['message'][0]['@']['from'];
   1.664 +
   1.665 +				if (isset($xml['message'][0]['#']['subject']))
   1.666 +				{
   1.667 +					$message['subject'] = $xml['message'][0]['#']['subject'][0]['#'];
   1.668 +				}
   1.669 +				$this->session['messages'][] = $message;
   1.670 +			break;
   1.671 +
   1.672 +			default:
   1.673 +				// hm...don't know this response
   1.674 +				$this->add_to_log('Notice: Unknown server response (' . key($xml) . ')');
   1.675 +				return false;
   1.676 +			break;
   1.677 +		}
   1.678 +	}
   1.679 +
   1.680 +	function send_message($to, $text, $subject = '', $type = 'normal')
   1.681 +	{
   1.682 +		if (!isset($this->session['jid']))
   1.683 +		{
   1.684 +			return false;
   1.685 +		}
   1.686 +
   1.687 +		if (!in_array($type, array('chat', 'normal', 'error', 'groupchat', 'headline')))
   1.688 +		{
   1.689 +			$type = 'normal';
   1.690 +		}
   1.691 +
   1.692 +		return $this->send("<message from='" . utf8_htmlspecialchars($this->session['jid']) . "' to='" . utf8_htmlspecialchars($to) . "' type='$type' id='" . uniqid('msg') . "'>
   1.693 +			<subject>" . utf8_htmlspecialchars($subject) . "</subject>
   1.694 +			<body>" . utf8_htmlspecialchars($text) . "</body>
   1.695 +			</message>"
   1.696 +		);
   1.697 +	}
   1.698 +
   1.699 +	/**
   1.700 +	* Encrypts a password as in RFC 2831
   1.701 +	* @param array $data Needs data from the client-server connection
   1.702 +	* @access public
   1.703 +	* @return string
   1.704 +	*/
   1.705 +	function encrypt_password($data)
   1.706 +	{
   1.707 +		// let's me think about <challenge> again...
   1.708 +		foreach (array('realm', 'cnonce', 'digest-uri') as $key)
   1.709 +		{
   1.710 +			if (!isset($data[$key]))
   1.711 +			{
   1.712 +				$data[$key] = '';
   1.713 +			}
   1.714 +		}
   1.715 +
   1.716 +		$pack = md5($this->username . ':' . $data['realm'] . ':' . $this->password);
   1.717 +
   1.718 +		if (isset($data['authzid']))
   1.719 +		{
   1.720 +			$a1 = pack('H32', $pack)  . sprintf(':%s:%s:%s', $data['nonce'], $data['cnonce'], $data['authzid']);
   1.721 +		}
   1.722 +		else
   1.723 +		{
   1.724 +			$a1 = pack('H32', $pack)  . sprintf(':%s:%s', $data['nonce'], $data['cnonce']);
   1.725 +		}
   1.726 +
   1.727 +		// should be: qop = auth
   1.728 +		$a2 = 'AUTHENTICATE:'. $data['digest-uri'];
   1.729 +
   1.730 +		return md5(sprintf('%s:%s:%s:%s:%s:%s', md5($a1), $data['nonce'], $data['nc'], $data['cnonce'], $data['qop'], md5($a2)));
   1.731 +	}
   1.732 +
   1.733 +	/**
   1.734 +	* parse_data like a="b",c="d",... or like a="a, b", c, d="e", f=g,...
   1.735 +	* @param string $data
   1.736 +	* @access public
   1.737 +	* @return array a => b ...
   1.738 +	*/
   1.739 +	function parse_data($data)
   1.740 +	{
   1.741 +		$data = explode(',', $data);
   1.742 +		$pairs = array();
   1.743 +		$key = false;
   1.744 +
   1.745 +		foreach ($data as $pair)
   1.746 +		{
   1.747 +			$dd = strpos($pair, '=');
   1.748 +
   1.749 +			if ($dd)
   1.750 +			{
   1.751 +				$key = trim(substr($pair, 0, $dd));
   1.752 +				$pairs[$key] = trim(trim(substr($pair, $dd + 1)), '"');
   1.753 +			}
   1.754 +			else if (strpos(strrev(trim($pair)), '"') === 0 && $key)
   1.755 +			{
   1.756 +				// We are actually having something left from "a, b" values, add it to the last one we handled.
   1.757 +				$pairs[$key] .= ',' . trim(trim($pair), '"');
   1.758 +				continue;
   1.759 +			}
   1.760 +		}
   1.761 +
   1.762 +		return $pairs;
   1.763 +	}
   1.764 +
   1.765 +	/**
   1.766 +	* opposite of jabber::parse_data()
   1.767 +	* @param array $data
   1.768 +	* @access public
   1.769 +	* @return string
   1.770 +	*/
   1.771 +	function implode_data($data)
   1.772 +	{
   1.773 +		$return = array();
   1.774 +		foreach ($data as $key => $value)
   1.775 +		{
   1.776 +			$return[] = $key . '="' . $value . '"';
   1.777 +		}
   1.778 +		return implode(',', $return);
   1.779 +	}
   1.780 +
   1.781 +	/**
   1.782 +	* xmlize()
   1.783 +	* @author Hans Anderson
   1.784 +	* @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
   1.785 +	*/
   1.786 +	function xmlize($data, $skip_white = 1, $encoding = 'UTF-8')
   1.787 +	{
   1.788 +		$data = trim($data);
   1.789 +
   1.790 +		if (substr($data, 0, 5) != '<?xml')
   1.791 +		{
   1.792 +			// mod
   1.793 +			$data = '<root>'. $data . '</root>';
   1.794 +		}
   1.795 +
   1.796 +		$vals = $index = $array = array();
   1.797 +		$parser = xml_parser_create($encoding);
   1.798 +		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
   1.799 +		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $skip_white);
   1.800 +		xml_parse_into_struct($parser, $data, $vals, $index);
   1.801 +		xml_parser_free($parser);
   1.802 +
   1.803 +		$i = 0;
   1.804 +		$tagname = $vals[$i]['tag'];
   1.805 +
   1.806 +		$array[$tagname][0]['@'] = (isset($vals[$i]['attributes'])) ? $vals[$i]['attributes'] : array();
   1.807 +		$array[$tagname][0]['#'] = $this->_xml_depth($vals, $i);
   1.808 +
   1.809 +		if (substr($data, 0, 5) != '<?xml')
   1.810 +		{
   1.811 +			$array = $array['root'][0]['#'];
   1.812 +		}
   1.813 +
   1.814 +		return $array;
   1.815 +	}
   1.816 +
   1.817 +	/**
   1.818 +	* _xml_depth()
   1.819 +	* @author Hans Anderson
   1.820 +	* @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
   1.821 +	*/
   1.822 +	function _xml_depth($vals, &$i)
   1.823 +	{
   1.824 +		$children = array();
   1.825 +
   1.826 +		if (isset($vals[$i]['value']))
   1.827 +		{
   1.828 +			array_push($children, $vals[$i]['value']);
   1.829 +		}
   1.830 +
   1.831 +		while (++$i < sizeof($vals))
   1.832 +		{
   1.833 +			switch ($vals[$i]['type'])
   1.834 +			{
   1.835 +				case 'open':
   1.836 +
   1.837 +					$tagname = (isset($vals[$i]['tag'])) ? $vals[$i]['tag'] : '';
   1.838 +					$size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
   1.839 +
   1.840 +					if (isset($vals[$i]['attributes']))
   1.841 +					{
   1.842 +						$children[$tagname][$size]['@'] = $vals[$i]['attributes'];
   1.843 +					}
   1.844 +
   1.845 +					$children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i);
   1.846 +
   1.847 +				break;
   1.848 +
   1.849 +				case 'cdata':
   1.850 +					array_push($children, $vals[$i]['value']);
   1.851 +				break;
   1.852 +
   1.853 +				case 'complete':
   1.854 +
   1.855 +					$tagname = $vals[$i]['tag'];
   1.856 +					$size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
   1.857 +					$children[$tagname][$size]['#'] = (isset($vals[$i]['value'])) ? $vals[$i]['value'] : array();
   1.858 +
   1.859 +					if (isset($vals[$i]['attributes']))
   1.860 +					{
   1.861 +						$children[$tagname][$size]['@'] = $vals[$i]['attributes'];
   1.862 +					}
   1.863 +
   1.864 +				break;
   1.865 +
   1.866 +				case 'close':
   1.867 +					return $children;
   1.868 +				break;
   1.869 +			}
   1.870 +		}
   1.871 +
   1.872 +		return $children;
   1.873 +	}
   1.874 +}
   1.875 +
   1.876 +?>
   1.877 \ No newline at end of file