Blame Identity/Models/Html/phpBB/3.0.4/includes/functions_content.php

d6e8d8
d6e8d8
/**
d6e8d8
*
d6e8d8
* @package phpBB3
d6e8d8
* @version $Id: functions_content.php 9184 2008-12-11 14:46:38Z acydburn $
d6e8d8
* @copyright (c) 2005 phpBB Group
d6e8d8
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
d6e8d8
*
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
* @ignore
d6e8d8
*/
d6e8d8
if (!defined('IN_PHPBB'))
d6e8d8
{
d6e8d8
	exit;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* gen_sort_selects()
d6e8d8
* make_jumpbox()
d6e8d8
* bump_topic_allowed()
d6e8d8
* get_context()
d6e8d8
* decode_message()
d6e8d8
* strip_bbcode()
d6e8d8
* generate_text_for_display()
d6e8d8
* generate_text_for_storage()
d6e8d8
* generate_text_for_edit()
d6e8d8
* make_clickable_callback()
d6e8d8
* make_clickable()
d6e8d8
* censor_text()
d6e8d8
* bbcode_nl2br()
d6e8d8
* smiley_text()
d6e8d8
* parse_attachments()
d6e8d8
* extension_allowed()
d6e8d8
* truncate_string()
d6e8d8
* get_username_string()
d6e8d8
* class bitfield
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
* Generate sort selection fields
d6e8d8
*/
d6e8d8
function gen_sort_selects(&$limit_days, &$sort_by_text, &$sort_days, &$sort_key, &$sort_dir, &$s_limit_days, &$s_sort_key, &$s_sort_dir, &$u_sort_param, $def_st = false, $def_sk = false, $def_sd = false)
d6e8d8
{
d6e8d8
	global $user;
d6e8d8
d6e8d8
	$sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
d6e8d8
d6e8d8
	$sorts = array(
d6e8d8
		'st'	=> array(
d6e8d8
			'key'		=> 'sort_days',
d6e8d8
			'default'	=> $def_st,
d6e8d8
			'options'	=> $limit_days,
d6e8d8
			'output'	=> &$s_limit_days,
d6e8d8
		),
d6e8d8
d6e8d8
		'sk'	=> array(
d6e8d8
			'key'		=> 'sort_key',
d6e8d8
			'default'	=> $def_sk,
d6e8d8
			'options'	=> $sort_by_text,
d6e8d8
			'output'	=> &$s_sort_key,
d6e8d8
		),
d6e8d8
d6e8d8
		'sd'	=> array(
d6e8d8
			'key'		=> 'sort_dir',
d6e8d8
			'default'	=> $def_sd,
d6e8d8
			'options'	=> $sort_dir_text,
d6e8d8
			'output'	=> &$s_sort_dir,
d6e8d8
		),
d6e8d8
	);
d6e8d8
	$u_sort_param  = '';
d6e8d8
d6e8d8
	foreach ($sorts as $name => $sort_ary)
d6e8d8
	{
d6e8d8
		$key = $sort_ary['key'];
d6e8d8
		$selected = $$sort_ary['key'];
d6e8d8
d6e8d8
		// Check if the key is selectable. If not, we reset to the default or first key found.
d6e8d8
		// This ensures the values are always valid. We also set $sort_dir/sort_key/etc. to the
d6e8d8
		// correct value, else the protection is void. ;)
d6e8d8
		if (!isset($sort_ary['options'][$selected]))
d6e8d8
		{
d6e8d8
			if ($sort_ary['default'] !== false)
d6e8d8
			{
d6e8d8
				$selected = $$key = $sort_ary['default'];
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				@reset($sort_ary['options']);
d6e8d8
				$selected = $$key = key($sort_ary['options']);
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		$sort_ary['output'] = '<select name="' . $name . '" id="' . $name . '">';
d6e8d8
		foreach ($sort_ary['options'] as $option => $text)
d6e8d8
		{
d6e8d8
			$sort_ary['output'] .= '<option value="' . $option . '"' . (($selected == $option) ? ' selected="selected"' : '') . '>' . $text . '</option>';
d6e8d8
		}
d6e8d8
		$sort_ary['output'] .= '</select>';
d6e8d8
d6e8d8
		$u_sort_param .= ($selected !== $sort_ary['default']) ? ((strlen($u_sort_param)) ? '&' : '') . "{$name}={$selected}" : '';
d6e8d8
	}
d6e8d8
d6e8d8
	return;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Generate Jumpbox
d6e8d8
*/
d6e8d8
function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list = false, $force_display = false)
d6e8d8
{
d6e8d8
	global $config, $auth, $template, $user, $db;
d6e8d8
d6e8d8
	// We only return if the jumpbox is not forced to be displayed (in case it is needed for functionality)
d6e8d8
	if (!$config['load_jumpbox'] && $force_display === false)
d6e8d8
	{
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
d6e8d8
		FROM ' . FORUMS_TABLE . '
d6e8d8
		ORDER BY left_id ASC';
d6e8d8
	$result = $db->sql_query($sql, 600);
d6e8d8
d6e8d8
	$right = $padding = 0;
d6e8d8
	$padding_store = array('0' => 0);
d6e8d8
	$display_jumpbox = false;
d6e8d8
	$iteration = 0;
d6e8d8
d6e8d8
	// Sometimes it could happen that forums will be displayed here not be displayed within the index page
d6e8d8
	// This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
d6e8d8
	// If this happens, the padding could be "broken"
d6e8d8
d6e8d8
	while ($row = $db->sql_fetchrow($result))
d6e8d8
	{
d6e8d8
		if ($row['left_id'] < $right)
d6e8d8
		{
d6e8d8
			$padding++;
d6e8d8
			$padding_store[$row['parent_id']] = $padding;
d6e8d8
		}
d6e8d8
		else if ($row['left_id'] > $right + 1)
d6e8d8
		{
d6e8d8
			// Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
d6e8d8
			// @todo digging deep to find out "how" this can happen.
d6e8d8
			$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
d6e8d8
		}
d6e8d8
d6e8d8
		$right = $row['right_id'];
d6e8d8
d6e8d8
		if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
d6e8d8
		{
d6e8d8
			// Non-postable forum with no subforums, don't display
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!$auth->acl_get('f_list', $row['forum_id']))
d6e8d8
		{
d6e8d8
			// if the user does not have permissions to list this forum skip
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
d6e8d8
		{
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!$display_jumpbox)
d6e8d8
		{
d6e8d8
			$template->assign_block_vars('jumpbox_forums', array(
d6e8d8
				'FORUM_ID'		=> ($select_all) ? 0 : -1,
d6e8d8
				'FORUM_NAME'	=> ($select_all) ? $user->lang['ALL_FORUMS'] : $user->lang['SELECT_FORUM'],
d6e8d8
				'S_FORUM_COUNT'	=> $iteration)
d6e8d8
			);
d6e8d8
d6e8d8
			$iteration++;
d6e8d8
			$display_jumpbox = true;
d6e8d8
		}
d6e8d8
d6e8d8
		$template->assign_block_vars('jumpbox_forums', array(
d6e8d8
			'FORUM_ID'		=> $row['forum_id'],
d6e8d8
			'FORUM_NAME'	=> $row['forum_name'],
d6e8d8
			'SELECTED'		=> ($row['forum_id'] == $forum_id) ? ' selected="selected"' : '',
d6e8d8
			'S_FORUM_COUNT'	=> $iteration,
d6e8d8
			'S_IS_CAT'		=> ($row['forum_type'] == FORUM_CAT) ? true : false,
d6e8d8
			'S_IS_LINK'		=> ($row['forum_type'] == FORUM_LINK) ? true : false,
d6e8d8
			'S_IS_POST'		=> ($row['forum_type'] == FORUM_POST) ? true : false)
d6e8d8
		);
d6e8d8
d6e8d8
		for ($i = 0; $i < $padding; $i++)
d6e8d8
		{
d6e8d8
			$template->assign_block_vars('jumpbox_forums.level', array());
d6e8d8
		}
d6e8d8
		$iteration++;
d6e8d8
	}
d6e8d8
	$db->sql_freeresult($result);
d6e8d8
	unset($padding_store);
d6e8d8
d6e8d8
	$template->assign_vars(array(
d6e8d8
		'S_DISPLAY_JUMPBOX'	=> $display_jumpbox,
d6e8d8
		'S_JUMPBOX_ACTION'	=> $action)
d6e8d8
	);
d6e8d8
d6e8d8
	return;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Bump Topic Check - used by posting and viewtopic
d6e8d8
*/
d6e8d8
function bump_topic_allowed($forum_id, $topic_bumped, $last_post_time, $topic_poster, $last_topic_poster)
d6e8d8
{
d6e8d8
	global $config, $auth, $user;
d6e8d8
d6e8d8
	// Check permission and make sure the last post was not already bumped
d6e8d8
	if (!$auth->acl_get('f_bump', $forum_id) || $topic_bumped)
d6e8d8
	{
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	// Check bump time range, is the user really allowed to bump the topic at this time?
d6e8d8
	$bump_time = ($config['bump_type'] == 'm') ? $config['bump_interval'] * 60 : (($config['bump_type'] == 'h') ? $config['bump_interval'] * 3600 : $config['bump_interval'] * 86400);
d6e8d8
d6e8d8
	// Check bump time
d6e8d8
	if ($last_post_time + $bump_time > time())
d6e8d8
	{
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	// Check bumper, only topic poster and last poster are allowed to bump
d6e8d8
	if ($topic_poster != $user->data['user_id'] && $last_topic_poster != $user->data['user_id'])
d6e8d8
	{
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	// A bump time of 0 will completely disable the bump feature... not intended but might be useful.
d6e8d8
	return $bump_time;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Generates a text with approx. the specified length which contains the specified words and their context
d6e8d8
*
d6e8d8
* @param	string	$text	The full text from which context shall be extracted
d6e8d8
* @param	string	$words	An array of words which should be contained in the result, has to be a valid part of a PCRE pattern (escape with preg_quote!)
d6e8d8
* @param	int		$length	The desired length of the resulting text, however the result might be shorter or longer than this value
d6e8d8
*
d6e8d8
* @return	string			Context of the specified words separated by "..."
d6e8d8
*/
d6e8d8
function get_context($text, $words, $length = 400)
d6e8d8
{
d6e8d8
	// first replace all whitespaces with single spaces
d6e8d8
	$text = preg_replace('/ +/', ' ', strtr($text, "\t\n\r\x0C ", '     '));
d6e8d8
d6e8d8
	$word_indizes = array();
d6e8d8
	if (sizeof($words))
d6e8d8
	{
d6e8d8
		$match = '';
d6e8d8
		// find the starting indizes of all words
d6e8d8
		foreach ($words as $word)
d6e8d8
		{
d6e8d8
			if ($word)
d6e8d8
			{
d6e8d8
				if (preg_match('#(?:[^\w]|^)(' . $word . ')(?:[^\w]|$)#i', $text, $match))
d6e8d8
				{
d6e8d8
					$pos = utf8_strpos($text, $match[1]);
d6e8d8
					if ($pos !== false)
d6e8d8
					{
d6e8d8
						$word_indizes[] = $pos;
d6e8d8
					}
d6e8d8
				}
d6e8d8
			}
d6e8d8
		}
d6e8d8
		unset($match);
d6e8d8
d6e8d8
		if (sizeof($word_indizes))
d6e8d8
		{
d6e8d8
			$word_indizes = array_unique($word_indizes);
d6e8d8
			sort($word_indizes);
d6e8d8
d6e8d8
			$wordnum = sizeof($word_indizes);
d6e8d8
			// number of characters on the right and left side of each word
d6e8d8
			$sequence_length = (int) ($length / (2 * $wordnum)) - 2;
d6e8d8
			$final_text = '';
d6e8d8
			$word = $j = 0;
d6e8d8
			$final_text_index = -1;
d6e8d8
d6e8d8
			// cycle through every character in the original text
d6e8d8
			for ($i = $word_indizes[$word], $n = utf8_strlen($text); $i < $n; $i++)
d6e8d8
			{
d6e8d8
				// if the current position is the start of one of the words then append $sequence_length characters to the final text
d6e8d8
				if (isset($word_indizes[$word]) && ($i == $word_indizes[$word]))
d6e8d8
				{
d6e8d8
					if ($final_text_index < $i - $sequence_length - 1)
d6e8d8
					{
d6e8d8
						$final_text .= '... ' . preg_replace('#^([^ ]*)#', '', utf8_substr($text, $i - $sequence_length, $sequence_length));
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						// if the final text is already nearer to the current word than $sequence_length we only append the text
d6e8d8
						// from its current index on and distribute the unused length to all other sequenes
d6e8d8
						$sequence_length += (int) (($final_text_index - $i + $sequence_length + 1) / (2 * $wordnum));
d6e8d8
						$final_text .= utf8_substr($text, $final_text_index + 1, $i - $final_text_index - 1);
d6e8d8
					}
d6e8d8
					$final_text_index = $i - 1;
d6e8d8
d6e8d8
					// add the following characters to the final text (see below)
d6e8d8
					$word++;
d6e8d8
					$j = 1;
d6e8d8
				}
d6e8d8
d6e8d8
				if ($j > 0)
d6e8d8
				{
d6e8d8
					// add the character to the final text and increment the sequence counter
d6e8d8
					$final_text .= utf8_substr($text, $i, 1);
d6e8d8
					$final_text_index++;
d6e8d8
					$j++;
d6e8d8
d6e8d8
					// if this is a whitespace then check whether we are done with this sequence
d6e8d8
					if (utf8_substr($text, $i, 1) == ' ')
d6e8d8
					{
d6e8d8
						// only check whether we have to exit the context generation completely if we haven't already reached the end anyway
d6e8d8
						if ($i + 4 < $n)
d6e8d8
						{
d6e8d8
							if (($j > $sequence_length && $word >= $wordnum) || utf8_strlen($final_text) > $length)
d6e8d8
							{
d6e8d8
								$final_text .= ' ...';
d6e8d8
								break;
d6e8d8
							}
d6e8d8
						}
d6e8d8
						else
d6e8d8
						{
d6e8d8
							// make sure the text really reaches the end
d6e8d8
							$j -= 4;
d6e8d8
						}
d6e8d8
d6e8d8
						// stop context generation and wait for the next word
d6e8d8
						if ($j > $sequence_length)
d6e8d8
						{
d6e8d8
							$j = 0;
d6e8d8
						}
d6e8d8
					}
d6e8d8
				}
d6e8d8
			}
d6e8d8
			return $final_text;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (!sizeof($words) || !sizeof($word_indizes))
d6e8d8
	{
d6e8d8
		return (utf8_strlen($text) >= $length + 3) ? utf8_substr($text, 0, $length) . '...' : $text;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Decode text whereby text is coming from the db and expected to be pre-parsed content
d6e8d8
* We are placing this outside of the message parser because we are often in need of it...
d6e8d8
*/
d6e8d8
function decode_message(&$message, $bbcode_uid = '')
d6e8d8
{
d6e8d8
	global $config;
d6e8d8
d6e8d8
	if ($bbcode_uid)
d6e8d8
	{
d6e8d8
		$match = array('
', "[/*:m:$bbcode_uid]", ":u:$bbcode_uid", ":o:$bbcode_uid", ":$bbcode_uid");
d6e8d8
		$replace = array("\n", '', '', '', '');
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$match = array('
');
d6e8d8
		$replace = array("\n");
d6e8d8
	}
d6e8d8
d6e8d8
	$message = str_replace($match, $replace, $message);
d6e8d8
d6e8d8
	$match = get_preg_expression('bbcode_htm');
d6e8d8
	$replace = array('\1', '\1', '\2', '\1', '', '');
d6e8d8
d6e8d8
	$message = preg_replace($match, $replace, $message);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Strips all bbcode from a text and returns the plain content
d6e8d8
*/
d6e8d8
function strip_bbcode(&$text, $uid = '')
d6e8d8
{
d6e8d8
	if (!$uid)
d6e8d8
	{
d6e8d8
		$uid = '[0-9a-z]{5,}';
d6e8d8
	}
d6e8d8
d6e8d8
	$text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:".*"|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);
d6e8d8
d6e8d8
	$match = get_preg_expression('bbcode_htm');
d6e8d8
	$replace = array('\1', '\1', '\2', '\1', '', '');
d6e8d8
d6e8d8
	$text = preg_replace($match, $replace, $text);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* For display of custom parsed text on user-facing pages
d6e8d8
* Expects $text to be the value directly from the database (stored value)
d6e8d8
*/
d6e8d8
function generate_text_for_display($text, $uid, $bitfield, $flags)
d6e8d8
{
d6e8d8
	static $bbcode;
d6e8d8
d6e8d8
	if (!$text)
d6e8d8
	{
d6e8d8
		return '';
d6e8d8
	}
d6e8d8
d6e8d8
	$text = censor_text($text);
d6e8d8
d6e8d8
	// Parse bbcode if bbcode uid stored and bbcode enabled
d6e8d8
	if ($uid && ($flags & OPTION_FLAG_BBCODE))
d6e8d8
	{
d6e8d8
		if (!class_exists('bbcode'))
d6e8d8
		{
d6e8d8
			global $phpbb_root_path, $phpEx;
d6e8d8
			include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
d6e8d8
		}
d6e8d8
d6e8d8
		if (empty($bbcode))
d6e8d8
		{
d6e8d8
			$bbcode = new bbcode($bitfield);
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			$bbcode->bbcode($bitfield);
d6e8d8
		}
d6e8d8
d6e8d8
		$bbcode->bbcode_second_pass($text, $uid);
d6e8d8
	}
d6e8d8
d6e8d8
	$text = bbcode_nl2br($text);
d6e8d8
	$text = smiley_text($text, !($flags & OPTION_FLAG_SMILIES));
d6e8d8
d6e8d8
	return $text;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* For parsing custom parsed text to be stored within the database.
d6e8d8
* This function additionally returns the uid and bitfield that needs to be stored.
d6e8d8
* Expects $text to be the value directly from request_var() and in it's non-parsed form
d6e8d8
*/
d6e8d8
function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bbcode = false, $allow_urls = false, $allow_smilies = false)
d6e8d8
{
d6e8d8
	global $phpbb_root_path, $phpEx;
d6e8d8
d6e8d8
	$uid = $bitfield = '';
d6e8d8
	$flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);
d6e8d8
d6e8d8
	if (!$text)
d6e8d8
	{
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	if (!class_exists('parse_message'))
d6e8d8
	{
d6e8d8
		include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
d6e8d8
	}
d6e8d8
d6e8d8
	$message_parser = new parse_message($text);
d6e8d8
	$message_parser->parse($allow_bbcode, $allow_urls, $allow_smilies);
d6e8d8
d6e8d8
	$text = $message_parser->message;
d6e8d8
	$uid = $message_parser->bbcode_uid;
d6e8d8
d6e8d8
	// If the bbcode_bitfield is empty, there is no need for the uid to be stored.
d6e8d8
	if (!$message_parser->bbcode_bitfield)
d6e8d8
	{
d6e8d8
		$uid = '';
d6e8d8
	}
d6e8d8
d6e8d8
	$bitfield = $message_parser->bbcode_bitfield;
d6e8d8
d6e8d8
	return;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* For decoding custom parsed text for edits as well as extracting the flags
d6e8d8
* Expects $text to be the value directly from the database (pre-parsed content)
d6e8d8
*/
d6e8d8
function generate_text_for_edit($text, $uid, $flags)
d6e8d8
{
d6e8d8
	global $phpbb_root_path, $phpEx;
d6e8d8
d6e8d8
	decode_message($text, $uid);
d6e8d8
d6e8d8
	return array(
d6e8d8
		'allow_bbcode'	=> ($flags & OPTION_FLAG_BBCODE) ? 1 : 0,
d6e8d8
		'allow_smilies'	=> ($flags & OPTION_FLAG_SMILIES) ? 1 : 0,
d6e8d8
		'allow_urls'	=> ($flags & OPTION_FLAG_LINKS) ? 1 : 0,
d6e8d8
		'text'			=> $text
d6e8d8
	);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* A subroutine of make_clickable used with preg_replace
d6e8d8
* It places correct HTML around an url, shortens the displayed text
d6e8d8
* and makes sure no entities are inside URLs
d6e8d8
*/
d6e8d8
function make_clickable_callback($type, $whitespace, $url, $relative_url, $class)
d6e8d8
{
d6e8d8
	$orig_url		= $url;
d6e8d8
	$orig_relative	= $relative_url;
d6e8d8
	$append			= '';
d6e8d8
	$url			= htmlspecialchars_decode($url);
d6e8d8
	$relative_url	= htmlspecialchars_decode($relative_url);
d6e8d8
d6e8d8
	// make sure no HTML entities were matched
d6e8d8
	$chars = array('<', '>', '"');
d6e8d8
	$split = false;
d6e8d8
d6e8d8
	foreach ($chars as $char)
d6e8d8
	{
d6e8d8
		$next_split = strpos($url, $char);
d6e8d8
		if ($next_split !== false)
d6e8d8
		{
d6e8d8
			$split = ($split !== false) ? min($split, $next_split) : $next_split;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if ($split !== false)
d6e8d8
	{
d6e8d8
		// an HTML entity was found, so the URL has to end before it
d6e8d8
		$append			= substr($url, $split) . $relative_url;
d6e8d8
		$url			= substr($url, 0, $split);
d6e8d8
		$relative_url	= '';
d6e8d8
	}
d6e8d8
	else if ($relative_url)
d6e8d8
	{
d6e8d8
		// same for $relative_url
d6e8d8
		$split = false;
d6e8d8
		foreach ($chars as $char)
d6e8d8
		{
d6e8d8
			$next_split = strpos($relative_url, $char);
d6e8d8
			if ($next_split !== false)
d6e8d8
			{
d6e8d8
				$split = ($split !== false) ? min($split, $next_split) : $next_split;
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		if ($split !== false)
d6e8d8
		{
d6e8d8
			$append			= substr($relative_url, $split);
d6e8d8
			$relative_url	= substr($relative_url, 0, $split);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	// if the last character of the url is a punctuation mark, exclude it from the url
d6e8d8
	$last_char = ($relative_url) ? $relative_url[strlen($relative_url) - 1] : $url[strlen($url) - 1];
d6e8d8
d6e8d8
	switch ($last_char)
d6e8d8
	{
d6e8d8
		case '.':
d6e8d8
		case '?':
d6e8d8
		case '!':
d6e8d8
		case ':':
d6e8d8
		case ',':
d6e8d8
			$append = $last_char;
d6e8d8
			if ($relative_url)
d6e8d8
			{
d6e8d8
				$relative_url = substr($relative_url, 0, -1);
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				$url = substr($url, 0, -1);
d6e8d8
			}
d6e8d8
		break;
d6e8d8
d6e8d8
		// set last_char to empty here, so the variable can be used later to
d6e8d8
		// check whether a character was removed
d6e8d8
		default:
d6e8d8
			$last_char = '';
d6e8d8
		break;
d6e8d8
	}
d6e8d8
d6e8d8
	$short_url = (strlen($url) > 55) ? substr($url, 0, 39) . ' ... ' . substr($url, -10) : $url;
d6e8d8
d6e8d8
	switch ($type)
d6e8d8
	{
d6e8d8
		case MAGIC_URL_LOCAL:
d6e8d8
			$tag			= 'l';
d6e8d8
			$relative_url	= preg_replace('/[&?]sid=[0-9a-f]{32}$/', '', preg_replace('/([&?])sid=[0-9a-f]{32}&/', '$1', $relative_url));
d6e8d8
			$url			= $url . '/' . $relative_url;
d6e8d8
			$text			= $relative_url;
d6e8d8
d6e8d8
			// this url goes to http://domain.tld/path/to/board/ which
d6e8d8
			// would result in an empty link if treated as local so
d6e8d8
			// don't touch it and let MAGIC_URL_FULL take care of it.
d6e8d8
			if (!$relative_url)
d6e8d8
			{
d6e8d8
				return $whitespace . $orig_url . '/' . $orig_relative; // slash is taken away by relative url pattern
d6e8d8
			}
d6e8d8
		break;
d6e8d8
d6e8d8
		case MAGIC_URL_FULL:
d6e8d8
			$tag	= 'm';
d6e8d8
			$text	= $short_url;
d6e8d8
		break;
d6e8d8
d6e8d8
		case MAGIC_URL_WWW:
d6e8d8
			$tag	= 'w';
d6e8d8
			$url	= 'http://' . $url;
d6e8d8
			$text	= $short_url;
d6e8d8
		break;
d6e8d8
d6e8d8
		case MAGIC_URL_EMAIL:
d6e8d8
			$tag	= 'e';
d6e8d8
			$text	= $short_url;
d6e8d8
			$url	= 'mailto:' . $url;
d6e8d8
		break;
d6e8d8
	}
d6e8d8
d6e8d8
	$url	= htmlspecialchars($url);
d6e8d8
	$text	= htmlspecialchars($text);
d6e8d8
	$append	= htmlspecialchars($append);
d6e8d8
d6e8d8
	$html	= "$whitespace<a$class href=\"$url\">$text$append";
d6e8d8
d6e8d8
	return $html;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* make_clickable function
d6e8d8
*
d6e8d8
* Replace magic urls of form http://xxx.xxx., www.xxx. and xxx@xxx.xxx.
d6e8d8
* Cuts down displayed size of link if over 50 chars, turns absolute links
d6e8d8
* into relative versions when the server/script path matches the link
d6e8d8
*/
d6e8d8
function make_clickable($text, $server_url = false, $class = 'postlink')
d6e8d8
{
d6e8d8
	if ($server_url === false)
d6e8d8
	{
d6e8d8
		$server_url = generate_board_url();
d6e8d8
	}
d6e8d8
d6e8d8
	static $magic_url_match;
d6e8d8
	static $magic_url_replace;
d6e8d8
	static $static_class;
d6e8d8
d6e8d8
	if (!is_array($magic_url_match) || $static_class != $class)
d6e8d8
	{
d6e8d8
		$static_class = $class;
d6e8d8
		$class = ($static_class) ? ' class="' . $static_class . '"' : '';
d6e8d8
		$local_class = ($static_class) ? ' class="' . $static_class . '-local"' : '';
d6e8d8
d6e8d8
		$magic_url_match = $magic_url_replace = array();
d6e8d8
		// Be sure to not let the matches cross over. ;)
d6e8d8
d6e8d8
		// relative urls for this board
d6e8d8
		$magic_url_match[] = '#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#ie';
d6e8d8
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_LOCAL, '\$1', '\$2', '\$3', '$local_class')";
d6e8d8
d6e8d8
		// matches a xxxx://aaaaa.bbb.cccc. ...
d6e8d8
		$magic_url_match[] = '#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#ie';
d6e8d8
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_FULL, '\$1', '\$2', '', '$class')";
d6e8d8
d6e8d8
		// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
d6e8d8
		$magic_url_match[] = '#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#ie';
d6e8d8
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_WWW, '\$1', '\$2', '', '$class')";
d6e8d8
d6e8d8
		// matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode.
d6e8d8
		$magic_url_match[] = '/(^|[\n\t (>])(' . get_preg_expression('email') . ')/ie';
d6e8d8
		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_EMAIL, '\$1', '\$2', '', '')";
d6e8d8
	}
d6e8d8
d6e8d8
	return preg_replace($magic_url_match, $magic_url_replace, $text);
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Censoring
d6e8d8
*/
d6e8d8
function censor_text($text)
d6e8d8
{
d6e8d8
	static $censors;
d6e8d8
d6e8d8
	// We moved the word censor checks in here because we call this function quite often - and then only need to do the check once
d6e8d8
	if (!isset($censors) || !is_array($censors))
d6e8d8
	{
d6e8d8
		global $config, $user, $auth, $cache;
d6e8d8
d6e8d8
		// We check here if the user is having viewing censors disabled (and also allowed to do so).
d6e8d8
		if (!$user->optionget('viewcensors') && $config['allow_nocensors'] && $auth->acl_get('u_chgcensors'))
d6e8d8
		{
d6e8d8
			$censors = array();
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			$censors = $cache->obtain_word_list();
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	if (sizeof($censors))
d6e8d8
	{
d6e8d8
		return preg_replace($censors['match'], $censors['replace'], $text);
d6e8d8
	}
d6e8d8
d6e8d8
	return $text;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* custom version of nl2br which takes custom BBCodes into account
d6e8d8
*/
d6e8d8
function bbcode_nl2br($text)
d6e8d8
{
d6e8d8
	// custom BBCodes might contain carriage returns so they
d6e8d8
	// are not converted into 
so now revert that
d6e8d8
	$text = str_replace(array("\n", "\r"), array('
', "\n"), $text);
d6e8d8
	return $text;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Smiley processing
d6e8d8
*/
d6e8d8
function smiley_text($text, $force_option = false)
d6e8d8
{
d6e8d8
	global $config, $user, $phpbb_root_path;
d6e8d8
d6e8d8
	if ($force_option || !$config['allow_smilies'] || !$user->optionget('viewsmilies'))
d6e8d8
	{
d6e8d8
		return preg_replace('#
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		return preg_replace('#', $text);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* General attachment parsing
d6e8d8
*
d6e8d8
* @param mixed $forum_id The forum id the attachments are displayed in (false if in private message)
d6e8d8
* @param string &$message The post/private message
d6e8d8
* @param array &$attachments The attachments to parse for (inline) display. The attachments array will hold templated data after parsing.
d6e8d8
* @param array &$update_count The attachment counts to be updated - will be filled
d6e8d8
* @param bool $preview If set to true the attachments are parsed for preview. Within preview mode the comments are fetched from the given $attachments array and not fetched from the database.
d6e8d8
*/
d6e8d8
function parse_attachments($forum_id, &$message, &$attachments, &$update_count, $preview = false)
d6e8d8
{
d6e8d8
	if (!sizeof($attachments))
d6e8d8
	{
d6e8d8
		return;
d6e8d8
	}
d6e8d8
d6e8d8
	global $template, $cache, $user;
d6e8d8
	global $extensions, $config, $phpbb_root_path, $phpEx;
d6e8d8
d6e8d8
	//
d6e8d8
	$compiled_attachments = array();
d6e8d8
d6e8d8
	if (!isset($template->filename['attachment_tpl']))
d6e8d8
	{
d6e8d8
		$template->set_filenames(array(
d6e8d8
			'attachment_tpl'	=> 'attachment.html')
d6e8d8
		);
d6e8d8
	}
d6e8d8
d6e8d8
	if (empty($extensions) || !is_array($extensions))
d6e8d8
	{
d6e8d8
		$extensions = $cache->obtain_attach_extensions($forum_id);
d6e8d8
	}
d6e8d8
d6e8d8
	// Look for missing attachment information...
d6e8d8
	$attach_ids = array();
d6e8d8
	foreach ($attachments as $pos => $attachment)
d6e8d8
	{
d6e8d8
		// If is_orphan is set, we need to retrieve the attachments again...
d6e8d8
		if (!isset($attachment['extension']) && !isset($attachment['physical_filename']))
d6e8d8
		{
d6e8d8
			$attach_ids[(int) $attachment['attach_id']] = $pos;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	// Grab attachments (security precaution)
d6e8d8
	if (sizeof($attach_ids))
d6e8d8
	{
d6e8d8
		global $db;
d6e8d8
d6e8d8
		$new_attachment_data = array();
d6e8d8
d6e8d8
		$sql = 'SELECT *
d6e8d8
			FROM ' . ATTACHMENTS_TABLE . '
d6e8d8
			WHERE ' . $db->sql_in_set('attach_id', array_keys($attach_ids));
d6e8d8
		$result = $db->sql_query($sql);
d6e8d8
d6e8d8
		while ($row = $db->sql_fetchrow($result))
d6e8d8
		{
d6e8d8
			if (!isset($attach_ids[$row['attach_id']]))
d6e8d8
			{
d6e8d8
				continue;
d6e8d8
			}
d6e8d8
d6e8d8
			// If we preview attachments we will set some retrieved values here
d6e8d8
			if ($preview)
d6e8d8
			{
d6e8d8
				$row['attach_comment'] = $attachments[$attach_ids[$row['attach_id']]]['attach_comment'];
d6e8d8
			}
d6e8d8
d6e8d8
			$new_attachment_data[$attach_ids[$row['attach_id']]] = $row;
d6e8d8
		}
d6e8d8
		$db->sql_freeresult($result);
d6e8d8
d6e8d8
		$attachments = $new_attachment_data;
d6e8d8
		unset($new_attachment_data);
d6e8d8
	}
d6e8d8
d6e8d8
	// Sort correctly
d6e8d8
	if ($config['display_order'])
d6e8d8
	{
d6e8d8
		// Ascending sort
d6e8d8
		krsort($attachments);
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		// Descending sort
d6e8d8
		ksort($attachments);
d6e8d8
	}
d6e8d8
d6e8d8
	foreach ($attachments as $attachment)
d6e8d8
	{
d6e8d8
		if (!sizeof($attachment))
d6e8d8
		{
d6e8d8
			continue;
d6e8d8
		}
d6e8d8
d6e8d8
		// We need to reset/empty the _file block var, because this function might be called more than once
d6e8d8
		$template->destroy_block_vars('_file');
d6e8d8
d6e8d8
		$block_array = array();
d6e8d8
d6e8d8
		// Some basics...
d6e8d8
		$attachment['extension'] = strtolower(trim($attachment['extension']));
d6e8d8
		$filename = $phpbb_root_path . $config['upload_path'] . '/' . basename($attachment['physical_filename']);
d6e8d8
		$thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/thumb_' . basename($attachment['physical_filename']);
d6e8d8
d6e8d8
		$upload_icon = '';
d6e8d8
d6e8d8
		if (isset($extensions[$attachment['extension']]))
d6e8d8
		{
d6e8d8
			if ($user->img('icon_topic_attach', '') && !$extensions[$attachment['extension']]['upload_icon'])
d6e8d8
			{
d6e8d8
				$upload_icon = $user->img('icon_topic_attach', '');
d6e8d8
			}
d6e8d8
			else if ($extensions[$attachment['extension']]['upload_icon'])
d6e8d8
			{
d6e8d8
				$upload_icon = '';
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		$filesize = $attachment['filesize'];
d6e8d8
		$size_lang = ($filesize >= 1048576) ? $user->lang['MIB'] : (($filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']);
d6e8d8
		$filesize = get_formatted_filesize($filesize, false);
d6e8d8
d6e8d8
		$comment = bbcode_nl2br(censor_text($attachment['attach_comment']));
d6e8d8
d6e8d8
		$block_array += array(
d6e8d8
			'UPLOAD_ICON'		=> $upload_icon,
d6e8d8
			'FILESIZE'			=> $filesize,
d6e8d8
			'SIZE_LANG'			=> $size_lang,
d6e8d8
			'DOWNLOAD_NAME'		=> basename($attachment['real_filename']),
d6e8d8
			'COMMENT'			=> $comment,
d6e8d8
		);
d6e8d8
d6e8d8
		$denied = false;
d6e8d8
d6e8d8
		if (!extension_allowed($forum_id, $attachment['extension'], $extensions))
d6e8d8
		{
d6e8d8
			$denied = true;
d6e8d8
d6e8d8
			$block_array += array(
d6e8d8
				'S_DENIED'			=> true,
d6e8d8
				'DENIED_MESSAGE'	=> sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension'])
d6e8d8
			);
d6e8d8
		}
d6e8d8
d6e8d8
		if (!$denied)
d6e8d8
		{
d6e8d8
			$l_downloaded_viewed = $download_link = '';
d6e8d8
			$display_cat = $extensions[$attachment['extension']]['display_cat'];
d6e8d8
d6e8d8
			if ($display_cat == ATTACHMENT_CATEGORY_IMAGE)
d6e8d8
			{
d6e8d8
				if ($attachment['thumbnail'])
d6e8d8
				{
d6e8d8
					$display_cat = ATTACHMENT_CATEGORY_THUMB;
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					if ($config['img_display_inlined'])
d6e8d8
					{
d6e8d8
						if ($config['img_link_width'] || $config['img_link_height'])
d6e8d8
						{
d6e8d8
							$dimension = @getimagesize($filename);
d6e8d8
d6e8d8
							// If the dimensions could not be determined or the image being 0x0 we display it as a link for safety purposes
d6e8d8
							if ($dimension === false || empty($dimension[0]) || empty($dimension[1]))
d6e8d8
							{
d6e8d8
								$display_cat = ATTACHMENT_CATEGORY_NONE;
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								$display_cat = ($dimension[0] <= $config['img_link_width'] && $dimension[1] <= $config['img_link_height']) ? ATTACHMENT_CATEGORY_IMAGE : ATTACHMENT_CATEGORY_NONE;
d6e8d8
							}
d6e8d8
						}
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						$display_cat = ATTACHMENT_CATEGORY_NONE;
d6e8d8
					}
d6e8d8
				}
d6e8d8
			}
d6e8d8
d6e8d8
			// Make some descisions based on user options being set.
d6e8d8
			if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
d6e8d8
			{
d6e8d8
				$display_cat = ATTACHMENT_CATEGORY_NONE;
d6e8d8
			}
d6e8d8
d6e8d8
			if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
d6e8d8
			{
d6e8d8
				$display_cat = ATTACHMENT_CATEGORY_NONE;
d6e8d8
			}
d6e8d8
d6e8d8
			$download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
d6e8d8
d6e8d8
			switch ($display_cat)
d6e8d8
			{
d6e8d8
				// Images
d6e8d8
				case ATTACHMENT_CATEGORY_IMAGE:
d6e8d8
					$l_downloaded_viewed = 'VIEWED_COUNT';
d6e8d8
					$inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
d6e8d8
					$download_link .= '&mode=view';
d6e8d8
d6e8d8
					$block_array += array(
d6e8d8
						'S_IMAGE'		=> true,
d6e8d8
						'U_INLINE_LINK'		=> $inline_link,
d6e8d8
					);
d6e8d8
d6e8d8
					$update_count[] = $attachment['attach_id'];
d6e8d8
				break;
d6e8d8
d6e8d8
				// Images, but display Thumbnail
d6e8d8
				case ATTACHMENT_CATEGORY_THUMB:
d6e8d8
					$l_downloaded_viewed = 'VIEWED_COUNT';
d6e8d8
					$thumbnail_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id'] . '&t=1');
d6e8d8
					$download_link .= '&mode=view';
d6e8d8
d6e8d8
					$block_array += array(
d6e8d8
						'S_THUMBNAIL'		=> true,
d6e8d8
						'THUMB_IMAGE'		=> $thumbnail_link,
d6e8d8
					);
d6e8d8
				break;
d6e8d8
d6e8d8
				// Windows Media Streams
d6e8d8
				case ATTACHMENT_CATEGORY_WM:
d6e8d8
					$l_downloaded_viewed = 'VIEWED_COUNT';
d6e8d8
d6e8d8
					// Giving the filename directly because within the wm object all variables are in local context making it impossible
d6e8d8
					// to validate against a valid session (all params can differ)
d6e8d8
					// $download_link = $filename;
d6e8d8
d6e8d8
					$block_array += array(
d6e8d8
						'U_FORUM'		=> generate_board_url(),
d6e8d8
						'ATTACH_ID'		=> $attachment['attach_id'],
d6e8d8
						'S_WM_FILE'		=> true,
d6e8d8
					);
d6e8d8
d6e8d8
					// Viewed/Heared File ... update the download count
d6e8d8
					$update_count[] = $attachment['attach_id'];
d6e8d8
				break;
d6e8d8
d6e8d8
				// Real Media Streams
d6e8d8
				case ATTACHMENT_CATEGORY_RM:
d6e8d8
				case ATTACHMENT_CATEGORY_QUICKTIME:
d6e8d8
					$l_downloaded_viewed = 'VIEWED_COUNT';
d6e8d8
d6e8d8
					$block_array += array(
d6e8d8
						'S_RM_FILE'			=> ($display_cat == ATTACHMENT_CATEGORY_RM) ? true : false,
d6e8d8
						'S_QUICKTIME_FILE'	=> ($display_cat == ATTACHMENT_CATEGORY_QUICKTIME) ? true : false,
d6e8d8
						'U_FORUM'			=> generate_board_url(),
d6e8d8
						'ATTACH_ID'			=> $attachment['attach_id'],
d6e8d8
					);
d6e8d8
d6e8d8
					// Viewed/Heared File ... update the download count
d6e8d8
					$update_count[] = $attachment['attach_id'];
d6e8d8
				break;
d6e8d8
d6e8d8
				// Macromedia Flash Files
d6e8d8
				case ATTACHMENT_CATEGORY_FLASH:
d6e8d8
					list($width, $height) = @getimagesize($filename);
d6e8d8
d6e8d8
					$l_downloaded_viewed = 'VIEWED_COUNT';
d6e8d8
d6e8d8
					$block_array += array(
d6e8d8
						'S_FLASH_FILE'	=> true,
d6e8d8
						'WIDTH'			=> $width,
d6e8d8
						'HEIGHT'		=> $height,
d6e8d8
					);
d6e8d8
d6e8d8
					// Viewed/Heared File ... update the download count
d6e8d8
					$update_count[] = $attachment['attach_id'];
d6e8d8
				break;
d6e8d8
d6e8d8
				default:
d6e8d8
					$l_downloaded_viewed = 'DOWNLOAD_COUNT';
d6e8d8
d6e8d8
					$block_array += array(
d6e8d8
						'S_FILE'		=> true,
d6e8d8
					);
d6e8d8
				break;
d6e8d8
			}
d6e8d8
d6e8d8
			$l_download_count = (!isset($attachment['download_count']) || $attachment['download_count'] == 0) ? $user->lang[$l_downloaded_viewed . '_NONE'] : (($attachment['download_count'] == 1) ? sprintf($user->lang[$l_downloaded_viewed], $attachment['download_count']) : sprintf($user->lang[$l_downloaded_viewed . 'S'], $attachment['download_count']));
d6e8d8
d6e8d8
			$block_array += array(
d6e8d8
				'U_DOWNLOAD_LINK'		=> $download_link,
d6e8d8
				'L_DOWNLOAD_COUNT'		=> $l_download_count
d6e8d8
			);
d6e8d8
		}
d6e8d8
d6e8d8
		$template->assign_block_vars('_file', $block_array);
d6e8d8
d6e8d8
		$compiled_attachments[] = $template->assign_display('attachment_tpl');
d6e8d8
	}
d6e8d8
d6e8d8
	$attachments = $compiled_attachments;
d6e8d8
	unset($compiled_attachments);
d6e8d8
d6e8d8
	$tpl_size = sizeof($attachments);
d6e8d8
d6e8d8
	$unset_tpl = array();
d6e8d8
d6e8d8
	preg_match_all('#(.*?)#', $message, $matches, PREG_PATTERN_ORDER);
d6e8d8
d6e8d8
	$replace = array();
d6e8d8
	foreach ($matches[0] as $num => $capture)
d6e8d8
	{
d6e8d8
		// Flip index if we are displaying the reverse way
d6e8d8
		$index = ($config['display_order']) ? ($tpl_size-($matches[1][$num] + 1)) : $matches[1][$num];
d6e8d8
d6e8d8
		$replace['from'][] = $matches[0][$num];
d6e8d8
		$replace['to'][] = (isset($attachments[$index])) ? $attachments[$index] : sprintf($user->lang['MISSING_INLINE_ATTACHMENT'], $matches[2][array_search($index, $matches[1])]);
d6e8d8
d6e8d8
		$unset_tpl[] = $index;
d6e8d8
	}
d6e8d8
d6e8d8
	if (isset($replace['from']))
d6e8d8
	{
d6e8d8
		$message = str_replace($replace['from'], $replace['to'], $message);
d6e8d8
	}
d6e8d8
d6e8d8
	$unset_tpl = array_unique($unset_tpl);
d6e8d8
d6e8d8
	// Needed to let not display the inlined attachments at the end of the post again
d6e8d8
	foreach ($unset_tpl as $index)
d6e8d8
	{
d6e8d8
		unset($attachments[$index]);
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Check if extension is allowed to be posted.
d6e8d8
*
d6e8d8
* @param mixed $forum_id The forum id to check or false if private message
d6e8d8
* @param string $extension The extension to check, for example zip.
d6e8d8
* @param array &$extensions The extension array holding the information from the cache (will be obtained if empty)
d6e8d8
*
d6e8d8
* @return bool False if the extension is not allowed to be posted, else true.
d6e8d8
*/
d6e8d8
function extension_allowed($forum_id, $extension, &$extensions)
d6e8d8
{
d6e8d8
	if (empty($extensions))
d6e8d8
	{
d6e8d8
		global $cache;
d6e8d8
		$extensions = $cache->obtain_attach_extensions($forum_id);
d6e8d8
	}
d6e8d8
d6e8d8
	return (!isset($extensions['_allowed_'][$extension])) ? false : true;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Truncates string while retaining special characters if going over the max length
d6e8d8
* The default max length is 60 at the moment
d6e8d8
* The maximum storage length is there to fit the string within the given length. The string may be further truncated due to html entities.
d6e8d8
* For example: string given is 'a "quote"' (length: 9), would be a stored as 'a "quote"' (length: 19)
d6e8d8
*
d6e8d8
* @param string $string The text to truncate to the given length. String is specialchared.
d6e8d8
* @param int $max_length Maximum length of string (multibyte character count as 1 char / Html entity count as 1 char)
d6e8d8
* @param int $max_store_length Maximum character length of string (multibyte character count as 1 char / Html entity count as entity chars).
d6e8d8
* @param bool $allow_reply Allow Re: in front of string
d6e8d8
* @param string $append String to be appended
d6e8d8
*/
d6e8d8
function truncate_string($string, $max_length = 60, $max_store_length = 255, $allow_reply = true, $append = '')
d6e8d8
{
d6e8d8
	$chars = array();
d6e8d8
d6e8d8
	$strip_reply = false;
d6e8d8
	$stripped = false;
d6e8d8
	if ($allow_reply && strpos($string, 'Re: ') === 0)
d6e8d8
	{
d6e8d8
		$strip_reply = true;
d6e8d8
		$string = substr($string, 4);
d6e8d8
	}
d6e8d8
d6e8d8
	$_chars = utf8_str_split(htmlspecialchars_decode($string));
d6e8d8
	$chars = array_map('utf8_htmlspecialchars', $_chars);
d6e8d8
d6e8d8
	// Now check the length ;)
d6e8d8
	if (sizeof($chars) > $max_length)
d6e8d8
	{
d6e8d8
		// Cut off the last elements from the array
d6e8d8
		$string = implode('', array_slice($chars, 0, $max_length - utf8_strlen($append)));
d6e8d8
		$stripped = true;
d6e8d8
	}
d6e8d8
d6e8d8
	// Due to specialchars, we may not be able to store the string...
d6e8d8
	if (utf8_strlen($string) > $max_store_length)
d6e8d8
	{
d6e8d8
		// let's split again, we do not want half-baked strings where entities are split
d6e8d8
		$_chars = utf8_str_split(htmlspecialchars_decode($string));
d6e8d8
		$chars = array_map('utf8_htmlspecialchars', $_chars);
d6e8d8
d6e8d8
		do
d6e8d8
		{
d6e8d8
			array_pop($chars);
d6e8d8
			$string = implode('', $chars);
d6e8d8
		}
d6e8d8
		while (utf8_strlen($string) > $max_store_length || !sizeof($chars));
d6e8d8
	}
d6e8d8
d6e8d8
	if ($strip_reply)
d6e8d8
	{
d6e8d8
		$string = 'Re: ' . $string;
d6e8d8
	}
d6e8d8
d6e8d8
	if ($append != '' && $stripped)
d6e8d8
	{
d6e8d8
		$string = $string . $append;
d6e8d8
	}
d6e8d8
d6e8d8
	return $string;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Get username details for placing into templates.
d6e8d8
* This function caches all modes on first call, except for no_profile and anonymous user - determined by $user_id.
d6e8d8
*
d6e8d8
* @param string $mode Can be profile (for getting an url to the profile), username (for obtaining the username), colour (for obtaining the user colour), full (for obtaining a html string representing a coloured link to the users profile) or no_profile (the same as full but forcing no profile link)
d6e8d8
* @param int $user_id The users id
d6e8d8
* @param string $username The users name
d6e8d8
* @param string $username_colour The users colour
d6e8d8
* @param string $guest_username optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then.
d6e8d8
* @param string $custom_profile_url optional parameter to specify a profile url. The user id get appended to this url as &u={user_id}
d6e8d8
*
d6e8d8
* @return string A string consisting of what is wanted based on $mode.
d6e8d8
* @author BartVB, Acyd Burn
d6e8d8
*/
d6e8d8
function get_username_string($mode, $user_id, $username, $username_colour = '', $guest_username = false, $custom_profile_url = false)
d6e8d8
{
d6e8d8
	static $_profile_cache;
d6e8d8
	static $_base_profile_url;
d6e8d8
d6e8d8
	$cache_key = $user_id;
d6e8d8
d6e8d8
	// If the get_username_string() function had been executed once with an (to us) unkown mode, all modes are pre-filled and we can just grab it.
d6e8d8
	if ($user_id && $user_id != ANONYMOUS && isset($_profile_cache[$cache_key][$mode]))
d6e8d8
	{
d6e8d8
		// If the mode is 'no_profile', we simply construct the TPL code due to calls to this mode being very very rare
d6e8d8
		if ($mode == 'no_profile')
d6e8d8
		{
d6e8d8
			$tpl = (!$_profile_cache[$cache_key]['colour']) ? '{USERNAME}' : '{USERNAME}';
d6e8d8
			return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($_profile_cache[$cache_key]['colour'], $_profile_cache[$cache_key]['username']), $tpl);
d6e8d8
		}
d6e8d8
d6e8d8
		return $_profile_cache[$cache_key][$mode];
d6e8d8
	}
d6e8d8
d6e8d8
	global $phpbb_root_path, $phpEx, $user, $auth;
d6e8d8
d6e8d8
	$username_colour = ($username_colour) ? '#' . $username_colour : '';
d6e8d8
d6e8d8
	if ($guest_username === false)
d6e8d8
	{
d6e8d8
		$username = ($username) ? $username : $user->lang['GUEST'];
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$username = ($user_id && $user_id != ANONYMOUS) ? $username : ((!empty($guest_username)) ? $guest_username : $user->lang['GUEST']);
d6e8d8
	}
d6e8d8
d6e8d8
	// Build cache for all modes
d6e8d8
	$_profile_cache[$cache_key]['colour'] = $username_colour;
d6e8d8
	$_profile_cache[$cache_key]['username'] = $username;
d6e8d8
	$_profile_cache[$cache_key]['no_profile'] = true;
d6e8d8
d6e8d8
	// Profile url - only show if not anonymous and permission to view profile if registered user
d6e8d8
	// For anonymous the link leads to a login page.
d6e8d8
	if ($user_id && $user_id != ANONYMOUS && ($user->data['user_id'] == ANONYMOUS || $auth->acl_get('u_viewprofile')))
d6e8d8
	{
d6e8d8
		if (empty($_base_profile_url))
d6e8d8
		{
d6e8d8
			$_base_profile_url = append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u={USER_ID}');
d6e8d8
		}
d6e8d8
d6e8d8
		$profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&u=' . (int) $user_id : str_replace('={USER_ID}', '=' . (int) $user_id, $_base_profile_url);
d6e8d8
		$tpl = (!$username_colour) ? '{USERNAME}' : '{USERNAME}';
d6e8d8
		$_profile_cache[$cache_key]['full'] = str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), $tpl);
d6e8d8
	}
d6e8d8
	else
d6e8d8
	{
d6e8d8
		$tpl = (!$username_colour) ? '{USERNAME}' : '{USERNAME}';
d6e8d8
		$_profile_cache[$cache_key]['full'] = str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($username_colour, $username), $tpl);
d6e8d8
		$profile_url = '';
d6e8d8
	}
d6e8d8
d6e8d8
	// Use the profile url from above
d6e8d8
	$_profile_cache[$cache_key]['profile'] = $profile_url;
d6e8d8
d6e8d8
	// If - by any chance - no_profile is called before any other mode, we need to do the calculation here
d6e8d8
	if ($mode == 'no_profile')
d6e8d8
	{
d6e8d8
		$tpl = (!$_profile_cache[$cache_key]['colour']) ? '{USERNAME}' : '{USERNAME}';
d6e8d8
		return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($_profile_cache[$cache_key]['colour'], $_profile_cache[$cache_key]['username']), $tpl);
d6e8d8
	}
d6e8d8
d6e8d8
	return $_profile_cache[$cache_key][$mode];
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* @package phpBB3
d6e8d8
*/
d6e8d8
class bitfield
d6e8d8
{
d6e8d8
	var $data;
d6e8d8
d6e8d8
	function bitfield($bitfield = '')
d6e8d8
	{
d6e8d8
		$this->data = base64_decode($bitfield);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	*/
d6e8d8
	function get($n)
d6e8d8
	{
d6e8d8
		// Get the ($n / 8)th char
d6e8d8
		$byte = $n >> 3;
d6e8d8
d6e8d8
		if (strlen($this->data) >= $byte + 1)
d6e8d8
		{
d6e8d8
			$c = $this->data[$byte];
d6e8d8
d6e8d8
			// Lookup the ($n % 8)th bit of the byte
d6e8d8
			$bit = 7 - ($n & 7);
d6e8d8
			return (bool) (ord($c) & (1 << $bit));
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	function set($n)
d6e8d8
	{
d6e8d8
		$byte = $n >> 3;
d6e8d8
		$bit = 7 - ($n & 7);
d6e8d8
d6e8d8
		if (strlen($this->data) >= $byte + 1)
d6e8d8
		{
d6e8d8
			$this->data[$byte] = $this->data[$byte] | chr(1 << $bit);
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			$this->data .= str_repeat("\0", $byte - strlen($this->data));
d6e8d8
			$this->data .= chr(1 << $bit);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	function clear($n)
d6e8d8
	{
d6e8d8
		$byte = $n >> 3;
d6e8d8
d6e8d8
		if (strlen($this->data) >= $byte + 1)
d6e8d8
		{
d6e8d8
			$bit = 7 - ($n & 7);
d6e8d8
			$this->data[$byte] = $this->data[$byte] &~ chr(1 << $bit);
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	function get_blob()
d6e8d8
	{
d6e8d8
		return $this->data;
d6e8d8
	}
d6e8d8
d6e8d8
	function get_base64()
d6e8d8
	{
d6e8d8
		return base64_encode($this->data);
d6e8d8
	}
d6e8d8
d6e8d8
	function get_bin()
d6e8d8
	{
d6e8d8
		$bin = '';
d6e8d8
		$len = strlen($this->data);
d6e8d8
d6e8d8
		for ($i = 0; $i < $len; ++$i)
d6e8d8
		{
d6e8d8
			$bin .= str_pad(decbin(ord($this->data[$i])), 8, '0', STR_PAD_LEFT);
d6e8d8
		}
d6e8d8
d6e8d8
		return $bin;
d6e8d8
	}
d6e8d8
d6e8d8
	function get_all_set()
d6e8d8
	{
d6e8d8
		return array_keys(array_filter(str_split($this->get_bin())));
d6e8d8
	}
d6e8d8
d6e8d8
	function merge($bitfield)
d6e8d8
	{
d6e8d8
		$this->data = $this->data | $bitfield->get_blob();
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
?>