<?php
//
//	file: includes/class_message.php
//	author: ptirhiik
//	begin: 20/01/2006
//	version: 1.6.1 - 27/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

class ranks
{
	var $data_time;
	var $from_cache;

	function read($force=false)
	{
		global $config;

		$db_cached = new cache('dta_ranks', $config->data['cache_path'], $config->data['cache_disabled_ranks']);
		$sql = 'SELECT *
					FROM ' . RANKS_TABLE . '
					ORDER BY rank_special, rank_min';
		$data = $db_cached->sql_query($sql, __LINE__, __FILE__, $force);
		$this->data_time = $db_cached->data_time;
		$this->from_cache = $db_cached->from_cache;

		return $data;
	}
}

class smilies
{
	var $data;
	var $data_time;
	var $from_cache;
	var $data_flag;

	function read($force=false)
	{
		global $config;

		if ( !$force && $this->data_flag )
		{
			return $this->data;
		}
		$db_cached = new cache('dta_smilies', $config->data['cache_path'], $config->data['cache_disabled_smilies']);
		$sql = 'SELECT * 
					FROM ' . SMILIES_TABLE . '
					ORDER BY smilies_id';
		$this->data = $db_cached->sql_query($sql, __LINE__, __FILE__, $force);
		$this->data_flag = true;
		$this->data_time = $db_cached->data_time;
		$this->from_cache = $db_cached->from_cache;

		return $this->data;
	}
}

class message
{
	function get_options()
	{
		global $config;

		return array(
			'html' => $config->data['allow_html'],
			'bbcode' => $config->data['allow_bbcode'],
			'smilies' => $config->data['allow_smilies'],
			'magic_url' => !isset($config->data['allow_magic_url']) || $config->data['allow_magicurl'],
			'censor' => !isset($config->data['allow_censor']) || $config->data['allow_censor'],
		);
	}

	function parse(&$message, $bbcode_uid='', $switches='', $highlight='', $max_chars=0)
	{
		global $config, $user;

		if ( empty($message) )
		{
			return;
		}

		$options = $this->get_options();
		foreach ( $options as $var => $dft )
		{
			$$var = $dft && (empty($switches) || !isset($switches[$var]) || $switches[$var]);
		}
		unset($options);

		if ( intval($max_chars) > 0 )
		{
			// remove html escape
			$message = unprepare_message($message);
			$message = str_replace('<br />', "\n", $message);

			// remove bbcode uid
			if ( !empty($bbcode_uid) )
			{
				$message = preg_replace('/\:(([a-z0-9]:)?)' . $bbcode_uid . '/s', '', $message);

				// replace img with url
				$message = str_replace(array('[img]', '[/img]'), array('[url]', '[/url]'), $message);
			}

			// short the message
			if ( strlen($message) + 3 > $max_chars )
			{
				$message = substr($message, 0, $max_chars) . '...';
			}

			// re-add bbCode
			$message = stripslashes(prepare_message(addslashes($message), $switches['html'], $switches['bbcode'], $switches['smilies'], $bbcode_uid));
		}

		// If the board has HTML off but the post has HTML on then we process it, else leave it alone
		if ( $html && !$user->data['user_allowhtml'] )
		{
			$message = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $message);
		}

		// Parse message for BBCode if reqd
		if ( $bbcode && $bbcode_uid && (!isset($user->data['user_allowbbcode']) || $user->data['user_allowbbcode']) )
		{
			$message = bbencode_second_pass($message, $bbcode_uid);
		}
		else if ( $bbcode_uid )
		{
			$message = preg_replace('/\:' . $bbcode_uid . '/si', '', $message);
		}

		// magic-url
		if ( $magic_url && (!isset($user->data['user_allowmagicurl']) || $user->data['user_allowmagicurl']) )
		{
			$message = make_clickable($message);
		}

		// Parse smilies
		if ( $smilies && (!isset($user->data['user_allowsmile']) || $user->data['user_allowsmile']) )
		{
			$message = smilies_pass($message);
		}

		// highlight/censor
		$do_censor = $censor && (!isset($user->data['user_allowcensor']) || $user->data['user_allowcensor']);
		if ( $highlight || $do_censor )
		{
			$html_mask = '';
			$seed_mask = '';
			_html_mask($message, $html_mask, $seed_mask);
			if ( $do_censor )
			{
				$message = _censor($message);
			}
			if ( $highlight )
			{
				$message = _highlight($message, $highlight);
			}
			_html_unmask($message, $html_mask, $seed_mask);
		}

		// Replace newlines
		$message = str_replace("\n", "\n<br />\n", $message);
	}
}

// init
if ( !isset($smilies) || !is_object($smilies) )
{
	$smilies = new smilies();
}

?>