\n");
		sleep(2);
		if (!$this->_check_connected())
		{
			$this->add_to_log('ERROR: _starttls() #4');
			return false;
		}
		return true;
	}
	/**
	* Get packet type
	* @access private
	*/
	function _get_packet_type($packet = NULL)
	{
		if (is_array($packet))
		{
			reset($packet);
			$packet_type = key($packet);
		}
		return ($packet_type) ? $packet_type : false;
	}
	/**
	* Split incoming packet
	* @access private
	*/
	function _split_incoming($incoming)
	{
		$temp = preg_split('#<(message|iq|presence|stream)#', $incoming, -1, PREG_SPLIT_DELIM_CAPTURE);
		$array = array();
		for ($i = 1, $size = sizeof($temp); $i < $size; $i += 2)
		{
			$array[] = '<' . $temp[$i] . $temp[($i + 1)];
		}
		return $array;
	}
	/**
	* Recursively prepares the strings in an array to be used in XML data.
	* @access private
	*/
	function _array_xmlspecialchars(&$array)
	{
		if (is_array($array))
		{
			foreach ($array as $k => $v)
			{
				if (is_array($v))
				{
					$this->_array_xmlspecialchars($array[$k]);
				}
				else
				{
					$this->_xmlspecialchars($array[$k]);
				}
			}
		}
	}
	/**
	* Prepares a string for usage in XML data.
	* @access private
	*/
	function _xmlspecialchars(&$string)
	{
		// we only have a few entities in xml
		$string = str_replace(array('&', '>', '<', '"', '\''), array('&', '>', '<', '"', '''), $string);
	}
	// ======================================================================
	//  parsers
	// ======================================================================
	/**
	* Get info from message (from)
	*/
	function get_info_from_message_from($packet = NULL)
	{
		return (is_array($packet)) ? $packet['message']['@']['from'] : false;
	}
	/**
	* Get info from message (type)
	*/
	function get_info_from_message_type($packet = NULL)
	{
		return (is_array($packet)) ? $packet['message']['@']['type'] : false;
	}
	/**
	* Get info from message (id)
	*/
	function get_info_from_message_id($packet = NULL)
	{
		return (is_array($packet)) ? $packet['message']['@']['id'] : false;
	}
	/**
	* Get info from message (thread)
	*/
	function get_info_from_message_thread($packet = NULL)
	{
		return (is_array($packet)) ? $packet['message']['#']['thread'][0]['#'] : false;
	}
	/**
	* Get info from message (subject)
	*/
	function get_info_from_message_subject($packet = NULL)
	{
		return (is_array($packet)) ? $packet['message']['#']['subject'][0]['#'] : false;
	}
	/**
	* Get info from message (body)
	*/
	function get_info_from_message_body($packet = NULL)
	{
		return (is_array($packet)) ? $packet['message']['#']['body'][0]['#'] : false;
	}
	/**
	* Get info from message (xmlns)
	*/
	function get_info_from_message_xmlns($packet = NULL)
	{
		return (is_array($packet)) ? $packet['message']['#']['x'] : false;
	}
	/**
	* Get info from message (error)
	*/
	function get_info_from_message_error($packet = NULL)
	{
		$error = preg_replace('#^\/$#', '', ($packet['message']['#']['error'][0]['@']['code'] . '/' . $packet['message']['#']['error'][0]['#']));
		return (is_array($packet)) ? $error : false;
	}
	// ======================================================================
	//  parsers
	// ======================================================================
	/**
	* Get info from iq (from)
	*/
	function get_info_from_iq_from($packet = NULL)
	{
		return (is_array($packet)) ? $packet['iq']['@']['from'] : false;
	}
	/**
	* Get info from iq (type)
	*/
	function get_info_from_iq_type($packet = NULL)
	{
		return (is_array($packet)) ? $packet['iq']['@']['type'] : false;
	}
	/**
	* Get info from iq (id)
	*/
	function get_info_from_iq_id($packet = NULL)
	{
		return (is_array($packet)) ? $packet['iq']['@']['id'] : false;
	}
	/**
	* Get info from iq (key)
	*/
	function get_info_from_iq_key($packet = NULL)
	{
		return (is_array($packet) && isset($packet['iq']['#']['query'][0]['#']['key'][0]['#'])) ? $packet['iq']['#']['query'][0]['#']['key'][0]['#'] : false;
	}
	/**
	* Get info from iq (error)
	*/
	function get_info_from_iq_error($packet = NULL)
	{
		$error = preg_replace('#^\/$#', '', ($packet['iq']['#']['error'][0]['@']['code'] . '/' . $packet['iq']['#']['error'][0]['#']));
		return (is_array($packet)) ? $error : false;
	}
	// ======================================================================
	//  handlers
	// ======================================================================
	/**
	* Message type normal
	*/
	function handler_message_normal($packet)
	{
		$from = $packet['message']['@']['from'];
		$this->add_to_log("EVENT: Message (type normal) from $from");
	}
	/**
	* Message type chat
	*/
	function handler_message_chat($packet)
	{
		$from = $packet['message']['@']['from'];
		$this->add_to_log("EVENT: Message (type chat) from $from");
	}
	/**
	* Message type groupchat
	*/
	function handler_message_groupchat($packet)
	{
		$from = $packet['message']['@']['from'];
		$this->add_to_log("EVENT: Message (type groupchat) from $from");
	}
	/**
	* Message type headline
	*/
	function handler_message_headline($packet)
	{
		$from = $packet['message']['@']['from'];
		$this->add_to_log("EVENT: Message (type headline) from $from");
	}
	/**
	* Message type error
	*/
	function handler_message_error($packet)
	{
		$from = $packet['message']['@']['from'];
		$this->add_to_log("EVENT: Message (type error) from $from");
	}
	// ======================================================================
	//  handlers
	// ======================================================================
	/**
	* application version updates
	*/
	function handler_iq_jabber_iq_autoupdate($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:autoupdate from $from");
	}
	/**
	* interactive server component properties
	*/
	function handler_iq_jabber_iq_agent($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:agent from $from");
	}
	/**
	* method to query interactive server components
	*/
	function handler_iq_jabber_iq_agents($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:agents from $from");
	}
	/**
	* simple client authentication
	*/
	function handler_iq_jabber_iq_auth($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:auth from $from");
	}
	/**
	* out of band data
	*/
	function handler_iq_jabber_iq_oob($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:oob from $from");
	}
	/**
	* method to store private data on the server
	*/
	function handler_iq_jabber_iq_private($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:private from $from");
	}
	/**
	* method for interactive registration
	*/
	function handler_iq_jabber_iq_register($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:register from $from");
	}
	/**
	* client roster management
	*/
	function handler_iq_jabber_iq_roster($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:roster from $from");
	}
	/**
	* method for searching a user database
	*/
	function handler_iq_jabber_iq_search($packet)
	{
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: jabber:iq:search from $from");
	}
	/**
	* method for requesting the current time
	*/
	function handler_iq_jabber_iq_time($packet)
	{
		if ($this->keep_alive_id == $this->get_info_from_iq_id($packet))
		{
			$this->returned_keep_alive = true;
			$this->connected = true;
			$this->add_to_log('EVENT: Keep-Alive returned, connection alive.');
		}
		$type	= $this->get_info_from_iq_type($packet);
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$id		= ($id != '') ? $id : 'time_' . time();
		if ($type == 'get')
		{
			$payload = '' . gmdate("Ydm\TH:i:s") . '' . date('T') . '' . date("Y/d/m h:i:s A") . '';
			$this->send_iq($from, 'result', $id, 'jabber:iq:time', $payload);
		}
		$this->add_to_log("EVENT: jabber:iq:time (type $type) from $from");
	}
	/**
	*/
	function handler_iq_error($packet)
	{
		// We'll do something with these later. This is a placeholder so that errors don't bounce back and forth.
	}
	/**
	* method for requesting version
	*/
	function handler_iq_jabber_iq_version($packet)
	{
		$type	= $this->get_info_from_iq_type($packet);
		$from	= $this->get_info_from_iq_from($packet);
		$id		= $this->get_info_from_iq_id($packet);
		$id		= ($id != '') ? $id : 'version_' . time();
		if ($type == 'get')
		{
			$payload = "{$this->iq_version_name}
				{$this->iq_version_os}
				{$this->iq_version_version}";
			//$this->SendIq($from, 'result', $id, "jabber:iq:version", $payload);
		}
		$this->add_to_log("EVENT: jabber:iq:version (type $type) from $from -- DISABLED");
	}
	// ======================================================================
	// Generic handlers
	// ======================================================================
	/**
	* Generic handler for unsupported requests
	*/
	function handler_not_implemented($packet)
	{
		$packet_type	= $this->_get_packet_type($packet);
		$from			= call_user_func(array(&$this, 'get_info_from_' . strtolower($packet_type) . '_from'), $packet);
		$id				= call_user_func(array(&$this, 'get_info_from_' . strtolower($packet_type) . '_id'), $packet);
		$this->send_error($from, $id, 501);
		$this->add_to_log("EVENT: Unrecognized <$packet_type/> from $from");
	}
	// ======================================================================
	// Third party code
	// m@d pr0ps to the coders ;)
	// ======================================================================
	/**
	* xmlize()
	* @author Hans Anderson
	* @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
	*/
	function xmlize($data, $skip_white = 1, $encoding = 'UTF-8')
	{
		$data = trim($data);
		$vals = $index = $array = array();
		$parser = xml_parser_create($encoding);
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $skip_white);
		xml_parse_into_struct($parser, $data, $vals, $index);
		xml_parser_free($parser);
		$i = 0;
		$tagname = $vals[$i]['tag'];
		$array[$tagname]['@'] = (isset($vals[$i]['attributes'])) ? $vals[$i]['attributes'] : array();
		$array[$tagname]['#'] = $this->_xml_depth($vals, $i);
		return $array;
	}
	/**
	* _xml_depth()
	* @author Hans Anderson
	* @copyright Hans Anderson / http://www.hansanderson.com/php/xml/
	*/
	function _xml_depth($vals, &$i)
	{
		$children = array();
		if (isset($vals[$i]['value']))
		{
			array_push($children, $vals[$i]['value']);
		}
		while (++$i < sizeof($vals))
		{
			switch ($vals[$i]['type'])
			{
				case 'open':
					$tagname = (isset($vals[$i]['tag'])) ? $vals[$i]['tag'] : '';
					$size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
					if (isset($vals[$i]['attributes']))
					{
						$children[$tagname][$size]['@'] = $vals[$i]['attributes'];
					}
					$children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i);
				break;
				case 'cdata':
					array_push($children, $vals[$i]['value']);
				break;
				case 'complete':
					$tagname = $vals[$i]['tag'];
					$size = (isset($children[$tagname])) ? sizeof($children[$tagname]) : 0;
					$children[$tagname][$size]['#'] = (isset($vals[$i]['value'])) ? $vals[$i]['value'] : array();
					if (isset($vals[$i]['attributes']))
					{
						$children[$tagname][$size]['@'] = $vals[$i]['attributes'];
					}
				break;
				case 'close':
					return $children;
				break;
			}
		}
		return $children;
	}
	/**
	* TraverseXMLize()
	* @author acebone@f2s.com
	* @copyright acebone@f2s.com, a HUGE help!
	*/
	function traverse_xmlize($array, $arr_name = 'array', $level = 0)
	{
		if ($level == 0)
		{
			echo '';
		}
		foreach ($array as $key => $val)
		{
			if (is_array($val))
			{
				$this->traverse_xmlize($val, $arr_name . '[' . $key . ']', $level + 1);
			}
			else
			{
				$GLOBALS['traverse_array'][] = '$' . $arr_name . '[' . $key . '] = "' . $val . "\"\n";
			}
		}
		if ($level == 0)
		{
			echo '';
		}
		return 1;
	}
}
/**
* Jabber Connector
* @package phpBB3
*/
class cjp_standard_connector
{
	var $active_socket;
	/**
	* Open socket
	*/
	function open_socket($server, $port)
	{
		if (function_exists('dns_get_record'))
		{
			$record = dns_get_record("_xmpp-client._tcp.$server", DNS_SRV);
			if (!empty($record))
			{
				$server = $record[0]['target'];
				$port = $record[0]['port'];
			}
		}
		$errno = 0;
		$errstr = '';
		if ($this->active_socket = @fsockopen($server, $port, $errno, $errstr, 5))
		{
			@socket_set_blocking($this->active_socket, 0);
			@socket_set_timeout($this->active_socket, 31536000);
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	* Close socket
	*/
	function close_socket()
	{
		return @fclose($this->active_socket);
	}
	/**
	* Write to socket
	*/
	function write_to_socket($data)
	{
		return @fwrite($this->active_socket, $data);
	}
	/**
	* Read from socket
	*/
	function read_from_socket($chunksize)
	{
		$buffer = @fread($this->active_socket, $chunksize);
		$buffer = (STRIP) ? stripslashes($buffer) : $buffer;
		return $buffer;
	}
}
?>