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

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