Blame Extras/phpBB/3.0.4/includes/functions_content.php

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