Blame Identity/Webenv/App/phpBB/3.0.4/includes/functions_content.php

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