Blame Identity/Webenv/phpBB/3.0.4/includes/functions_jabber.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package phpBB3
ef5584
* @version $Id: functions_jabber.php 8979 2008-10-08 12:44:23Z acydburn $
ef5584
* @copyright (c) 2007 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
*
ef5584
* Jabber class from Flyspray project
ef5584
*
ef5584
* @version class.jabber2.php 1595 2008-09-19 (0.9.9)
ef5584
* @copyright 2006 Flyspray.org
ef5584
* @author Florian Schmitz (floele)
ef5584
*
ef5584
* Only slightly modified by Acyd Burn
ef5584
*
ef5584
* @package phpBB3
ef5584
*/
ef5584
class jabber
ef5584
{
ef5584
	var $connection = null;
ef5584
	var $session = array();
ef5584
	var $timeout = 10;
ef5584
ef5584
	var $server;
ef5584
	var $connect_server;
ef5584
	var $port;
ef5584
	var $username;
ef5584
	var $password;
ef5584
	var $use_ssl;
ef5584
	var $resource = 'functions_jabber.phpbb.php';
ef5584
ef5584
	var $enable_logging;
ef5584
	var $log_array;
ef5584
ef5584
	var $features = array();
ef5584
ef5584
	/**
ef5584
	*/
ef5584
	function jabber($server, $port, $username, $password, $use_ssl = false)
ef5584
	{
ef5584
		$this->connect_server		= ($server) ? $server : 'localhost';
ef5584
		$this->port					= ($port) ? $port : 5222;
ef5584
ef5584
		// Get the server and the username
ef5584
		if (strpos($username, '@') === false)
ef5584
		{
ef5584
			$this->server = $this->connect_server;
ef5584
			$this->username = $username;
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$jid = explode('@', $username, 2);
ef5584
ef5584
			$this->username = $jid[0];
ef5584
			$this->server = $jid[1];
ef5584
		}
ef5584
ef5584
		$this->password				= $password;
ef5584
		$this->use_ssl				= ($use_ssl && $this->can_use_ssl()) ? true : false;
ef5584
ef5584
		// Change port if we use SSL
ef5584
		if ($this->port == 5222 && $this->use_ssl)
ef5584
		{
ef5584
			$this->port = 5223;
ef5584
		}
ef5584
ef5584
		$this->enable_logging		= true;
ef5584
		$this->log_array			= array();
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Able to use the SSL functionality?
ef5584
	*/
ef5584
	function can_use_ssl()
ef5584
	{
ef5584
		// 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)
ef5584
		return ((version_compare(PHP_VERSION, '5.2.1', '<') || version_compare(PHP_VERSION, '5.2.3RC2', '>=')) && @extension_loaded('openssl')) ? true : false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Able to use TLS?
ef5584
	*/
ef5584
	function can_use_tls()
ef5584
	{
ef5584
		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'))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		/**
ef5584
		* Make sure the encryption stream is supported
ef5584
		* Also seem to work without the crypto stream if correctly compiled
ef5584
ef5584
		$streams = stream_get_wrappers();
ef5584
ef5584
		if (!in_array('streams.crypto', $streams))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
		*/
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Sets the resource which is used. No validation is done here, only escaping.
ef5584
	* @param string $name
ef5584
	* @access public
ef5584
	*/
ef5584
	function set_resource($name)
ef5584
	{
ef5584
		$this->resource = $name;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Connect
ef5584
	*/
ef5584
	function connect()
ef5584
	{
ef5584
/*		if (!$this->check_jid($this->username . '@' . $this->server))
ef5584
		{
ef5584
			$this->add_to_log('Error: Jabber ID is not valid: ' . $this->username . '@' . $this->server);
ef5584
			return false;
ef5584
		}*/
ef5584
ef5584
		$this->session['ssl'] = $this->use_ssl;
ef5584
ef5584
		if ($this->open_socket($this->connect_server, $this->port, $this->use_ssl))
ef5584
		{
ef5584
			$this->send("\n");
ef5584
			$this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$this->add_to_log('Error: connect() #2');
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		// Now we listen what the server has to say...and give appropriate responses
ef5584
		$this->response($this->listen());
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Disconnect
ef5584
	*/
ef5584
	function disconnect()
ef5584
	{
ef5584
		if ($this->connected())
ef5584
		{
ef5584
			// disconnect gracefully
ef5584
			if (isset($this->session['sent_presence']))
ef5584
			{
ef5584
				$this->send_presence('offline', '', true);
ef5584
			}
ef5584
ef5584
			$this->send('</stream:stream>');
ef5584
			$this->session = array();
ef5584
			return fclose($this->connection);
ef5584
		}
ef5584
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Connected?
ef5584
	*/
ef5584
	function connected()
ef5584
	{
ef5584
		return (is_resource($this->connection) && !feof($this->connection)) ? true : false;
ef5584
	}
ef5584
ef5584
ef5584
	/**
ef5584
	* Initiates login (using data from contructor, after calling connect())
ef5584
	* @access public
ef5584
	* @return bool
ef5584
	*/
ef5584
	function login()
ef5584
	{
ef5584
		if (!sizeof($this->features))
ef5584
		{
ef5584
			$this->add_to_log('Error: No feature information from server available.');
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		return $this->response($this->features);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Send data to the Jabber server
ef5584
	* @param string $xml
ef5584
	* @access public
ef5584
	* @return bool
ef5584
	*/
ef5584
	function send($xml)
ef5584
	{
ef5584
		if ($this->connected())
ef5584
		{
ef5584
			$xml = trim($xml);
ef5584
			$this->add_to_log('SEND: '. $xml);
ef5584
			return fwrite($this->connection, $xml);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$this->add_to_log('Error: Could not send, connection lost (flood?).');
ef5584
			return false;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* OpenSocket
ef5584
	* @param string $server host to connect to
ef5584
	* @param int $port port number
ef5584
	* @param bool $use_ssl use ssl or not
ef5584
	* @access public
ef5584
	* @return bool
ef5584
	*/
ef5584
	function open_socket($server, $port, $use_ssl = false)
ef5584
	{
ef5584
		if (@function_exists('dns_get_record'))
ef5584
		{
ef5584
			$record = @dns_get_record("_xmpp-client._tcp.$server", DNS_SRV);
ef5584
			if (!empty($record) && !empty($record[0]['target']))
ef5584
			{
ef5584
				$server = $record[0]['target'];
ef5584
			}
ef5584
		}
ef5584
ef5584
		$server = $use_ssl ? 'ssl://' . $server : $server;
ef5584
ef5584
		if ($this->connection = @fsockopen($server, $port, $errorno, $errorstr, $this->timeout))
ef5584
		{
ef5584
			socket_set_blocking($this->connection, 0);
ef5584
			socket_set_timeout($this->connection, 60);
ef5584
ef5584
			return true;
ef5584
		}
ef5584
ef5584
		// Apparently an error occured...
ef5584
		$this->add_to_log('Error: open_socket() - ' . $errorstr);
ef5584
		return false;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Return log
ef5584
	*/
ef5584
	function get_log()
ef5584
	{
ef5584
		if ($this->enable_logging && sizeof($this->log_array))
ef5584
		{
ef5584
			return implode("

", $this->log_array);
ef5584
		}
ef5584
ef5584
		return '';
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Add information to log
ef5584
	*/
ef5584
	function add_to_log($string)
ef5584
	{
ef5584
		if ($this->enable_logging)
ef5584
		{
ef5584
			$this->log_array[] = utf8_htmlspecialchars($string);
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Listens to the connection until it gets data or the timeout is reached.
ef5584
	* Thus, it should only be called if data is expected to be received.
ef5584
	* @access public
ef5584
	* @return mixed either false for timeout or an array with the received data
ef5584
	*/
ef5584
	function listen($timeout = 10, $wait = false)
ef5584
	{
ef5584
		if (!$this->connected())
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		// Wait for a response until timeout is reached
ef5584
		$start = time();
ef5584
		$data = '';
ef5584
ef5584
		do
ef5584
		{
ef5584
			$read = trim(fread($this->connection, 4096));
ef5584
			$data .= $read;
ef5584
		}
ef5584
		while (time() <= $start + $timeout && !feof($this->connection) && ($wait || $data == '' || $read != '' || (substr(rtrim($data), -1) != '>')));
ef5584
ef5584
		if ($data != '')
ef5584
		{
ef5584
			$this->add_to_log('RECV: '. $data);
ef5584
			return $this->xmlize($data);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$this->add_to_log('Timeout, no response from server.');
ef5584
			return false;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Initiates account registration (based on data used for contructor)
ef5584
	* @access public
ef5584
	* @return bool
ef5584
	*/
ef5584
	function register()
ef5584
	{
ef5584
		if (!isset($this->session['id']) || isset($this->session['jid']))
ef5584
		{
ef5584
			$this->add_to_log('Error: Cannot initiate registration.');
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$this->send("<iq type='get' id='reg_1'><query xmlns='jabber:iq:register'/></iq>");
ef5584
		return $this->response($this->listen());
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Sets account presence. No additional info required (default is "online" status)
ef5584
	* @param $message online, offline...
ef5584
	* @param $type dnd, away, chat, xa or nothing
ef5584
	* @param $unavailable set this to true if you want to become unavailable
ef5584
	* @access public
ef5584
	* @return bool
ef5584
	*/
ef5584
	function send_presence($message = '', $type = '', $unavailable = false)
ef5584
	{
ef5584
		if (!isset($this->session['jid']))
ef5584
		{
ef5584
			$this->add_to_log('ERROR: send_presence() - Cannot set presence at this point, no jid given.');
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$type = strtolower($type);
ef5584
		$type = (in_array($type, array('dnd', 'away', 'chat', 'xa'))) ? '<show>'. $type .'</show>' : '';
ef5584
ef5584
		$unavailable = ($unavailable) ? " type='unavailable'" : '';
ef5584
		$message = ($message) ? '<status>' . utf8_htmlspecialchars($message) .'</status>' : '';
ef5584
ef5584
		$this->session['sent_presence'] = !$unavailable;
ef5584
ef5584
		return $this->send("<presence$unavailable>" . $type . $message . '</presence>');
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* This handles all the different XML elements
ef5584
	* @param array $xml
ef5584
	* @access public
ef5584
	* @return bool
ef5584
	*/
ef5584
	function response($xml)
ef5584
	{
ef5584
		if (!is_array($xml) || !sizeof($xml))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		// did we get multiple elements? do one after another
ef5584
		// array('message' => ..., 'presence' => ...)
ef5584
		if (sizeof($xml) > 1)
ef5584
		{
ef5584
			foreach ($xml as $key => $value)
ef5584
			{
ef5584
				$this->response(array($key => $value));
ef5584
			}
ef5584
			return;
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			// or even multiple elements of the same type?
ef5584
			// array('message' => array(0 => ..., 1 => ...))
ef5584
			if (sizeof(reset($xml)) > 1)
ef5584
			{
ef5584
				foreach (reset($xml) as $value)
ef5584
				{
ef5584
					$this->response(array(key($xml) => array(0 => $value)));
ef5584
				}
ef5584
				return;
ef5584
			}
ef5584
		}
ef5584
ef5584
		switch (key($xml))
ef5584
		{
ef5584
			case 'stream:stream':
ef5584
				// Connection initialised (or after authentication). Not much to do here...
ef5584
ef5584
				if (isset($xml['stream:stream'][0]['#']['stream:features']))
ef5584
				{
ef5584
					// we already got all info we need
ef5584
					$this->features = $xml['stream:stream'][0]['#'];
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$this->features = $this->listen();
ef5584
				}
ef5584
ef5584
				$second_time = isset($this->session['id']);
ef5584
				$this->session['id'] = $xml['stream:stream'][0]['@']['id'];
ef5584
ef5584
				if ($second_time)
ef5584
				{
ef5584
					// If we are here for the second time after TLS, we need to continue logging in
ef5584
					return $this->login();
ef5584
				}
ef5584
ef5584
				// go on with authentication?
ef5584
				if (isset($this->features['stream:features'][0]['#']['bind']) || !empty($this->session['tls']))
ef5584
				{
ef5584
					return $this->response($this->features);
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'stream:features':
ef5584
				// Resource binding after successful authentication
ef5584
				if (isset($this->session['authenticated']))
ef5584
				{
ef5584
					// session required?
ef5584
					$this->session['sess_required'] = isset($xml['stream:features'][0]['#']['session']);
ef5584
ef5584
					$this->send("<iq type='set' id='bind_1'>
ef5584
						<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
ef5584
							<resource>" . utf8_htmlspecialchars($this->resource) . '</resource>
ef5584
						</bind>
ef5584
					</iq>');
ef5584
					return $this->response($this->listen());
ef5584
				}
ef5584
ef5584
				// Let's use TLS if SSL is not enabled and we can actually use it
ef5584
				if (!$this->session['ssl'] && $this->can_use_tls() && $this->can_use_ssl() && isset($xml['stream:features'][0]['#']['starttls']))
ef5584
				{
ef5584
					$this->add_to_log('Switching to TLS.');
ef5584
					$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>\n");
ef5584
					return $this->response($this->listen());
ef5584
				}
ef5584
ef5584
				// Does the server support SASL authentication?
ef5584
ef5584
				// I hope so, because we do (and no other method).
ef5584
				if (isset($xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns']) && $xml['stream:features'][0]['#']['mechanisms'][0]['@']['xmlns'] == 'urn:ietf:params:xml:ns:xmpp-sasl')
ef5584
				{
ef5584
					// Now decide on method
ef5584
					$methods = array();
ef5584
ef5584
					foreach ($xml['stream:features'][0]['#']['mechanisms'][0]['#']['mechanism'] as $value)
ef5584
					{
ef5584
						$methods[] = $value['#'];
ef5584
					}
ef5584
ef5584
					// we prefer DIGEST-MD5
ef5584
					// we don't want to use plain authentication (neither does the server usually) if no encryption is in place
ef5584
ef5584
					// http://www.xmpp.org/extensions/attic/jep-0078-1.7.html
ef5584
					// The plaintext mechanism SHOULD NOT be used unless the underlying stream is encrypted (using SSL or TLS)
ef5584
					// and the client has verified that the server certificate is signed by a trusted certificate authority.
ef5584
ef5584
					if (in_array('DIGEST-MD5', $methods))
ef5584
					{
ef5584
						$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>");
ef5584
					}
ef5584
					else if (in_array('PLAIN', $methods) && ($this->session['ssl'] || !empty($this->session['tls'])))
ef5584
					{
ef5584
						$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>"
ef5584
							. base64_encode(chr(0) . $this->username . '@' . $this->server . chr(0) . $this->password) .
ef5584
							'</auth>');
ef5584
					}
ef5584
					else if (in_array('ANONYMOUS', $methods))
ef5584
					{
ef5584
						$this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>");
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						// not good...
ef5584
						$this->add_to_log('Error: No authentication method supported.');
ef5584
						$this->disconnect();
ef5584
						return false;
ef5584
					}
ef5584
ef5584
					return $this->response($this->listen());
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					// ok, this is it. bye.
ef5584
					$this->add_to_log('Error: Server does not offer SASL authentication.');
ef5584
					$this->disconnect();
ef5584
					return false;
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'challenge':
ef5584
				// continue with authentication...a challenge literally -_-
ef5584
				$decoded = base64_decode($xml['challenge'][0]['#']);
ef5584
				$decoded = $this->parse_data($decoded);
ef5584
ef5584
				if (!isset($decoded['digest-uri']))
ef5584
				{
ef5584
					$decoded['digest-uri'] = 'xmpp/'. $this->server;
ef5584
				}
ef5584
ef5584
				// better generate a cnonce, maybe it's needed
ef5584
				$decoded['cnonce'] = base64_encode(md5(uniqid(mt_rand(), true)));
ef5584
ef5584
				// second challenge?
ef5584
				if (isset($decoded['rspauth']))
ef5584
				{
ef5584
					$this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>");
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					// Make sure we only use 'auth' for qop (relevant for $this->encrypt_password())
ef5584
					// If the <response> is choking up on the changed parameter we may need to adjust encrypt_password() directly
ef5584
					if (isset($decoded['qop']) && $decoded['qop'] != 'auth' && strpos($decoded['qop'], 'auth') !== false)
ef5584
					{
ef5584
						$decoded['qop'] = 'auth';
ef5584
					}
ef5584
ef5584
					$response = array(
ef5584
						'username'	=> $this->username,
ef5584
						'response'	=> $this->encrypt_password(array_merge($decoded, array('nc' => '00000001'))),
ef5584
						'charset'	=> 'utf-8',
ef5584
						'nc'		=> '00000001',
ef5584
						'qop'		=> 'auth',			// only auth being supported
ef5584
					);
ef5584
ef5584
					foreach (array('nonce', 'digest-uri', 'realm', 'cnonce') as $key)
ef5584
					{
ef5584
						if (isset($decoded[$key]))
ef5584
						{
ef5584
							$response[$key] = $decoded[$key];
ef5584
						}
ef5584
					}
ef5584
ef5584
					$this->send("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" . base64_encode($this->implode_data($response)) . '</response>');
ef5584
				}
ef5584
ef5584
				return $this->response($this->listen());
ef5584
			break;
ef5584
ef5584
			case 'failure':
ef5584
				$this->add_to_log('Error: Server sent "failure".');
ef5584
				$this->disconnect();
ef5584
				return false;
ef5584
			break;
ef5584
ef5584
			case 'proceed':
ef5584
				// continue switching to TLS
ef5584
				$meta = stream_get_meta_data($this->connection);
ef5584
				socket_set_blocking($this->connection, 1);
ef5584
ef5584
				if (!stream_socket_enable_crypto($this->connection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT))
ef5584
				{
ef5584
					$this->add_to_log('Error: TLS mode change failed.');
ef5584
					return false;
ef5584
				}
ef5584
ef5584
				socket_set_blocking($this->connection, $meta['blocked']);
ef5584
				$this->session['tls'] = true;
ef5584
ef5584
				// new stream
ef5584
				$this->send("\n");
ef5584
				$this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
ef5584
ef5584
				return $this->response($this->listen());
ef5584
			break;
ef5584
ef5584
			case 'success':
ef5584
				// Yay, authentication successful.
ef5584
				$this->send("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>\n");
ef5584
				$this->session['authenticated'] = true;
ef5584
ef5584
				// we have to wait for another response
ef5584
				return $this->response($this->listen());
ef5584
			break;
ef5584
ef5584
			case 'iq':
ef5584
				// we are not interested in IQs we did not expect
ef5584
				if (!isset($xml['iq'][0]['@']['id']))
ef5584
				{
ef5584
					return false;
ef5584
				}
ef5584
ef5584
				// multiple possibilities here
ef5584
				switch ($xml['iq'][0]['@']['id'])
ef5584
				{
ef5584
					case 'bind_1':
ef5584
						$this->session['jid'] = $xml['iq'][0]['#']['bind'][0]['#']['jid'][0]['#'];
ef5584
ef5584
						// and (maybe) yet another request to be able to send messages *finally*
ef5584
						if ($this->session['sess_required'])
ef5584
						{
ef5584
							$this->send("<iq to='{$this->server}' type='set' id='sess_1'>
ef5584
								<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>
ef5584
								</iq>");
ef5584
							return $this->response($this->listen());
ef5584
						}
ef5584
ef5584
						return true;
ef5584
					break;
ef5584
ef5584
					case 'sess_1':
ef5584
						return true;
ef5584
					break;
ef5584
ef5584
					case 'reg_1':
ef5584
						$this->send("<iq type='set' id='reg_2'>
ef5584
								<query xmlns='jabber:iq:register'>
ef5584
									<username>" . utf8_htmlspecialchars($this->username) . "</username>
ef5584
									<password>" . utf8_htmlspecialchars($this->password) . "</password>
ef5584
								</query>
ef5584
							</iq>");
ef5584
						return $this->response($this->listen());
ef5584
					break;
ef5584
ef5584
					case 'reg_2':
ef5584
						// registration end
ef5584
						if (isset($xml['iq'][0]['#']['error']))
ef5584
						{
ef5584
							$this->add_to_log('Warning: Registration failed.');
ef5584
							return false;
ef5584
						}
ef5584
						return true;
ef5584
					break;
ef5584
ef5584
					case 'unreg_1':
ef5584
						return true;
ef5584
					break;
ef5584
ef5584
					default:
ef5584
						$this->add_to_log('Notice: Received unexpected IQ.');
ef5584
						return false;
ef5584
					break;
ef5584
				}
ef5584
			break;
ef5584
ef5584
			case 'message':
ef5584
				// we are only interested in content...
ef5584
				if (!isset($xml['message'][0]['#']['body']))
ef5584
				{
ef5584
					return false;
ef5584
				}
ef5584
ef5584
				$message['body'] = $xml['message'][0]['#']['body'][0]['#'];
ef5584
				$message['from'] = $xml['message'][0]['@']['from'];
ef5584
ef5584
				if (isset($xml['message'][0]['#']['subject']))
ef5584
				{
ef5584
					$message['subject'] = $xml['message'][0]['#']['subject'][0]['#'];
ef5584
				}
ef5584
				$this->session['messages'][] = $message;
ef5584
			break;
ef5584
ef5584
			default:
ef5584
				// hm...don't know this response
ef5584
				$this->add_to_log('Notice: Unknown server response (' . key($xml) . ')');
ef5584
				return false;
ef5584
			break;
ef5584
		}
ef5584
	}
ef5584
ef5584
	function send_message($to, $text, $subject = '', $type = 'normal')
ef5584
	{
ef5584
		if (!isset($this->session['jid']))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		if (!in_array($type, array('chat', 'normal', 'error', 'groupchat', 'headline')))
ef5584
		{
ef5584
			$type = 'normal';
ef5584
		}
ef5584
ef5584
		return $this->send("<message from='" . utf8_htmlspecialchars($this->session['jid']) . "' to='" . utf8_htmlspecialchars($to) . "' type='$type' id='" . uniqid('msg') . "'>
ef5584
			<subject>" . utf8_htmlspecialchars($subject) . "</subject>
ef5584
			<body>" . utf8_htmlspecialchars($text) . "</body>
ef5584
			</message>"
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Encrypts a password as in RFC 2831
ef5584
	* @param array $data Needs data from the client-server connection
ef5584
	* @access public
ef5584
	* @return string
ef5584
	*/
ef5584
	function encrypt_password($data)
ef5584
	{
ef5584
		// let's me think about <challenge> again...
ef5584
		foreach (array('realm', 'cnonce', 'digest-uri') as $key)
ef5584
		{
ef5584
			if (!isset($data[$key]))
ef5584
			{
ef5584
				$data[$key] = '';
ef5584
			}
ef5584
		}
ef5584
ef5584
		$pack = md5($this->username . ':' . $data['realm'] . ':' . $this->password);
ef5584
ef5584
		if (isset($data['authzid']))
ef5584
		{
ef5584
			$a1 = pack('H32', $pack)  . sprintf(':%s:%s:%s', $data['nonce'], $data['cnonce'], $data['authzid']);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$a1 = pack('H32', $pack)  . sprintf(':%s:%s', $data['nonce'], $data['cnonce']);
ef5584
		}
ef5584
ef5584
		// should be: qop = auth
ef5584
		$a2 = 'AUTHENTICATE:'. $data['digest-uri'];
ef5584
ef5584
		return md5(sprintf('%s:%s:%s:%s:%s:%s', md5($a1), $data['nonce'], $data['nc'], $data['cnonce'], $data['qop'], md5($a2)));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* parse_data like a="b",c="d",... or like a="a, b", c, d="e", f=g,...
ef5584
	* @param string $data
ef5584
	* @access public
ef5584
	* @return array a => b ...
ef5584
	*/
ef5584
	function parse_data($data)
ef5584
	{
ef5584
		$data = explode(',', $data);
ef5584
		$pairs = array();
ef5584
		$key = false;
ef5584
ef5584
		foreach ($data as $pair)
ef5584
		{
ef5584
			$dd = strpos($pair, '=');
ef5584
ef5584
			if ($dd)
ef5584
			{
ef5584
				$key = trim(substr($pair, 0, $dd));
ef5584
				$pairs[$key] = trim(trim(substr($pair, $dd + 1)), '"');
ef5584
			}
ef5584
			else if (strpos(strrev(trim($pair)), '"') === 0 && $key)
ef5584
			{
ef5584
				// We are actually having something left from "a, b" values, add it to the last one we handled.
ef5584
				$pairs[$key] .= ',' . trim(trim($pair), '"');
ef5584
				continue;
ef5584
			}
ef5584
		}
ef5584
ef5584
		return $pairs;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* opposite of jabber::parse_data()
ef5584
	* @param array $data
ef5584
	* @access public
ef5584
	* @return string
ef5584
	*/
ef5584
	function implode_data($data)
ef5584
	{
ef5584
		$return = array();
ef5584
		foreach ($data as $key => $value)
ef5584
		{
ef5584
			$return[] = $key . '="' . $value . '"';
ef5584
		}
ef5584
		return implode(',', $return);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* xmlize()
ef5584
	* @author Hans Anderson
ef5584
	* @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
ef5584
	*/
ef5584
	function xmlize($data, $skip_white = 1, $encoding = 'UTF-8')
ef5584
	{
ef5584
		$data = trim($data);
ef5584
ef5584
		if (substr($data, 0, 5) != '
ef5584
		{
ef5584
			// mod
ef5584
			$data = '<root>'. $data . '</root>';
ef5584
		}
ef5584
ef5584
		$vals = $index = $array = array();
ef5584
		$parser = xml_parser_create($encoding);
ef5584
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
ef5584
		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $skip_white);
ef5584
		xml_parse_into_struct($parser, $data, $vals, $index);
ef5584
		xml_parser_free($parser);
ef5584
ef5584
		$i = 0;
ef5584
		$tagname = $vals[$i]['tag'];
ef5584
ef5584
		$array[$tagname][0]['@'] = (isset($vals[$i]['attributes'])) ? $vals[$i]['attributes'] : array();
ef5584
		$array[$tagname][0]['#'] = $this->_xml_depth($vals, $i);
ef5584
ef5584
		if (substr($data, 0, 5) != '
ef5584
		{
ef5584
			$array = $array['root'][0]['#'];
ef5584
		}
ef5584
ef5584
		return $array;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* _xml_depth()
ef5584
	* @author Hans Anderson
ef5584
	* @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
ef5584
	*/
ef5584
	function _xml_depth($vals, &$i)
ef5584
	{
ef5584
		$children = array();
ef5584
ef5584
		if (isset($vals[$i]['value']))
ef5584
		{
ef5584
			array_push($children, $vals[$i]['value']);
ef5584
		}
ef5584
ef5584
		while (++$i < sizeof($vals))
ef5584
		{
ef5584
			switch ($vals[$i]['type'])
ef5584
			{
ef5584
				case 'open':
ef5584
ef5584
					$tagname = (isset($vals[$i]['tag'])) ? $vals[$i]['tag'] : '';
ef5584
					$size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
ef5584
ef5584
					if (isset($vals[$i]['attributes']))
ef5584
					{
ef5584
						$children[$tagname][$size]['@'] = $vals[$i]['attributes'];
ef5584
					}
ef5584
ef5584
					$children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i);
ef5584
ef5584
				break;
ef5584
ef5584
				case 'cdata':
ef5584
					array_push($children, $vals[$i]['value']);
ef5584
				break;
ef5584
ef5584
				case 'complete':
ef5584
ef5584
					$tagname = $vals[$i]['tag'];
ef5584
					$size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
ef5584
					$children[$tagname][$size]['#'] = (isset($vals[$i]['value'])) ? $vals[$i]['value'] : array();
ef5584
ef5584
					if (isset($vals[$i]['attributes']))
ef5584
					{
ef5584
						$children[$tagname][$size]['@'] = $vals[$i]['attributes'];
ef5584
					}
ef5584
ef5584
				break;
ef5584
ef5584
				case 'close':
ef5584
					return $children;
ef5584
				break;
ef5584
			}
ef5584
		}
ef5584
ef5584
		return $children;
ef5584
	}
ef5584
}
ef5584
ef5584
?>