Blame Extras/phpBB/3.0.4/includes/functions_jabber.php

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

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