includes/functions_content.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_content.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1358 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* gen_sort_selects()
    1.24 +* make_jumpbox()
    1.25 +* bump_topic_allowed()
    1.26 +* get_context()
    1.27 +* decode_message()
    1.28 +* strip_bbcode()
    1.29 +* generate_text_for_display()
    1.30 +* generate_text_for_storage()
    1.31 +* generate_text_for_edit()
    1.32 +* make_clickable_callback()
    1.33 +* make_clickable()
    1.34 +* censor_text()
    1.35 +* bbcode_nl2br()
    1.36 +* smiley_text()
    1.37 +* parse_attachments()
    1.38 +* extension_allowed()
    1.39 +* truncate_string()
    1.40 +* get_username_string()
    1.41 +* class bitfield
    1.42 +*/
    1.43 +
    1.44 +/**
    1.45 +* Generate sort selection fields
    1.46 +*/
    1.47 +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)
    1.48 +{
    1.49 +	global $user;
    1.50 +
    1.51 +	$sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
    1.52 +
    1.53 +	$sorts = array(
    1.54 +		'st'	=> array(
    1.55 +			'key'		=> 'sort_days',
    1.56 +			'default'	=> $def_st,
    1.57 +			'options'	=> $limit_days,
    1.58 +			'output'	=> &$s_limit_days,
    1.59 +		),
    1.60 +
    1.61 +		'sk'	=> array(
    1.62 +			'key'		=> 'sort_key',
    1.63 +			'default'	=> $def_sk,
    1.64 +			'options'	=> $sort_by_text,
    1.65 +			'output'	=> &$s_sort_key,
    1.66 +		),
    1.67 +
    1.68 +		'sd'	=> array(
    1.69 +			'key'		=> 'sort_dir',
    1.70 +			'default'	=> $def_sd,
    1.71 +			'options'	=> $sort_dir_text,
    1.72 +			'output'	=> &$s_sort_dir,
    1.73 +		),
    1.74 +	);
    1.75 +	$u_sort_param  = '';
    1.76 +
    1.77 +	foreach ($sorts as $name => $sort_ary)
    1.78 +	{
    1.79 +		$key = $sort_ary['key'];
    1.80 +		$selected = $$sort_ary['key'];
    1.81 +
    1.82 +		// Check if the key is selectable. If not, we reset to the default or first key found.
    1.83 +		// This ensures the values are always valid. We also set $sort_dir/sort_key/etc. to the
    1.84 +		// correct value, else the protection is void. ;)
    1.85 +		if (!isset($sort_ary['options'][$selected]))
    1.86 +		{
    1.87 +			if ($sort_ary['default'] !== false)
    1.88 +			{
    1.89 +				$selected = $$key = $sort_ary['default'];
    1.90 +			}
    1.91 +			else
    1.92 +			{
    1.93 +				@reset($sort_ary['options']);
    1.94 +				$selected = $$key = key($sort_ary['options']);
    1.95 +			}
    1.96 +		}
    1.97 +
    1.98 +		$sort_ary['output'] = '<select name="' . $name . '" id="' . $name . '">';
    1.99 +		foreach ($sort_ary['options'] as $option => $text)
   1.100 +		{
   1.101 +			$sort_ary['output'] .= '<option value="' . $option . '"' . (($selected == $option) ? ' selected="selected"' : '') . '>' . $text . '</option>';
   1.102 +		}
   1.103 +		$sort_ary['output'] .= '</select>';
   1.104 +
   1.105 +		$u_sort_param .= ($selected !== $sort_ary['default']) ? ((strlen($u_sort_param)) ? '&amp;' : '') . "{$name}={$selected}" : '';
   1.106 +	}
   1.107 +
   1.108 +	return;
   1.109 +}
   1.110 +
   1.111 +/**
   1.112 +* Generate Jumpbox
   1.113 +*/
   1.114 +function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list = false, $force_display = false)
   1.115 +{
   1.116 +	global $config, $auth, $template, $user, $db;
   1.117 +
   1.118 +	// We only return if the jumpbox is not forced to be displayed (in case it is needed for functionality)
   1.119 +	if (!$config['load_jumpbox'] && $force_display === false)
   1.120 +	{
   1.121 +		return;
   1.122 +	}
   1.123 +
   1.124 +	$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
   1.125 +		FROM ' . FORUMS_TABLE . '
   1.126 +		ORDER BY left_id ASC';
   1.127 +	$result = $db->sql_query($sql, 600);
   1.128 +
   1.129 +	$right = $padding = 0;
   1.130 +	$padding_store = array('0' => 0);
   1.131 +	$display_jumpbox = false;
   1.132 +	$iteration = 0;
   1.133 +
   1.134 +	// Sometimes it could happen that forums will be displayed here not be displayed within the index page
   1.135 +	// This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
   1.136 +	// If this happens, the padding could be "broken"
   1.137 +
   1.138 +	while ($row = $db->sql_fetchrow($result))
   1.139 +	{
   1.140 +		if ($row['left_id'] < $right)
   1.141 +		{
   1.142 +			$padding++;
   1.143 +			$padding_store[$row['parent_id']] = $padding;
   1.144 +		}
   1.145 +		else if ($row['left_id'] > $right + 1)
   1.146 +		{
   1.147 +			// Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
   1.148 +			// @todo digging deep to find out "how" this can happen.
   1.149 +			$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
   1.150 +		}
   1.151 +
   1.152 +		$right = $row['right_id'];
   1.153 +
   1.154 +		if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
   1.155 +		{
   1.156 +			// Non-postable forum with no subforums, don't display
   1.157 +			continue;
   1.158 +		}
   1.159 +
   1.160 +		if (!$auth->acl_get('f_list', $row['forum_id']))
   1.161 +		{
   1.162 +			// if the user does not have permissions to list this forum skip
   1.163 +			continue;
   1.164 +		}
   1.165 +
   1.166 +		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
   1.167 +		{
   1.168 +			continue;
   1.169 +		}
   1.170 +
   1.171 +		if (!$display_jumpbox)
   1.172 +		{
   1.173 +			$template->assign_block_vars('jumpbox_forums', array(
   1.174 +				'FORUM_ID'		=> ($select_all) ? 0 : -1,
   1.175 +				'FORUM_NAME'	=> ($select_all) ? $user->lang['ALL_FORUMS'] : $user->lang['SELECT_FORUM'],
   1.176 +				'S_FORUM_COUNT'	=> $iteration)
   1.177 +			);
   1.178 +
   1.179 +			$iteration++;
   1.180 +			$display_jumpbox = true;
   1.181 +		}
   1.182 +
   1.183 +		$template->assign_block_vars('jumpbox_forums', array(
   1.184 +			'FORUM_ID'		=> $row['forum_id'],
   1.185 +			'FORUM_NAME'	=> $row['forum_name'],
   1.186 +			'SELECTED'		=> ($row['forum_id'] == $forum_id) ? ' selected="selected"' : '',
   1.187 +			'S_FORUM_COUNT'	=> $iteration,
   1.188 +			'S_IS_CAT'		=> ($row['forum_type'] == FORUM_CAT) ? true : false,
   1.189 +			'S_IS_LINK'		=> ($row['forum_type'] == FORUM_LINK) ? true : false,
   1.190 +			'S_IS_POST'		=> ($row['forum_type'] == FORUM_POST) ? true : false)
   1.191 +		);
   1.192 +
   1.193 +		for ($i = 0; $i < $padding; $i++)
   1.194 +		{
   1.195 +			$template->assign_block_vars('jumpbox_forums.level', array());
   1.196 +		}
   1.197 +		$iteration++;
   1.198 +	}
   1.199 +	$db->sql_freeresult($result);
   1.200 +	unset($padding_store);
   1.201 +
   1.202 +	$template->assign_vars(array(
   1.203 +		'S_DISPLAY_JUMPBOX'	=> $display_jumpbox,
   1.204 +		'S_JUMPBOX_ACTION'	=> $action)
   1.205 +	);
   1.206 +
   1.207 +	return;
   1.208 +}
   1.209 +
   1.210 +/**
   1.211 +* Bump Topic Check - used by posting and viewtopic
   1.212 +*/
   1.213 +function bump_topic_allowed($forum_id, $topic_bumped, $last_post_time, $topic_poster, $last_topic_poster)
   1.214 +{
   1.215 +	global $config, $auth, $user;
   1.216 +
   1.217 +	// Check permission and make sure the last post was not already bumped
   1.218 +	if (!$auth->acl_get('f_bump', $forum_id) || $topic_bumped)
   1.219 +	{
   1.220 +		return false;
   1.221 +	}
   1.222 +
   1.223 +	// Check bump time range, is the user really allowed to bump the topic at this time?
   1.224 +	$bump_time = ($config['bump_type'] == 'm') ? $config['bump_interval'] * 60 : (($config['bump_type'] == 'h') ? $config['bump_interval'] * 3600 : $config['bump_interval'] * 86400);
   1.225 +
   1.226 +	// Check bump time
   1.227 +	if ($last_post_time + $bump_time > time())
   1.228 +	{
   1.229 +		return false;
   1.230 +	}
   1.231 +
   1.232 +	// Check bumper, only topic poster and last poster are allowed to bump
   1.233 +	if ($topic_poster != $user->data['user_id'] && $last_topic_poster != $user->data['user_id'])
   1.234 +	{
   1.235 +		return false;
   1.236 +	}
   1.237 +
   1.238 +	// A bump time of 0 will completely disable the bump feature... not intended but might be useful.
   1.239 +	return $bump_time;
   1.240 +}
   1.241 +
   1.242 +/**
   1.243 +* Generates a text with approx. the specified length which contains the specified words and their context
   1.244 +*
   1.245 +* @param	string	$text	The full text from which context shall be extracted
   1.246 +* @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!)
   1.247 +* @param	int		$length	The desired length of the resulting text, however the result might be shorter or longer than this value
   1.248 +*
   1.249 +* @return	string			Context of the specified words separated by "..."
   1.250 +*/
   1.251 +function get_context($text, $words, $length = 400)
   1.252 +{
   1.253 +	// first replace all whitespaces with single spaces
   1.254 +	$text = preg_replace('/ +/', ' ', strtr($text, "\t\n\r\x0C ", '     '));
   1.255 +
   1.256 +	// we need to turn the entities back into their original form, to not cut the message in between them
   1.257 +	$entities = array('&lt;', '&gt;', '&#91;', '&#93;', '&#46;', '&#58;', '&#058;');
   1.258 +	$characters = array('<', '>', '[', ']', '.', ':', ':');
   1.259 +	$text = str_replace($entities, $characters, $text);
   1.260 +
   1.261 +	$word_indizes = array();
   1.262 +	if (sizeof($words))
   1.263 +	{
   1.264 +		$match = '';
   1.265 +		// find the starting indizes of all words
   1.266 +		foreach ($words as $word)
   1.267 +		{
   1.268 +			if ($word)
   1.269 +			{
   1.270 +				if (preg_match('#(?:[^\w]|^)(' . $word . ')(?:[^\w]|$)#i', $text, $match))
   1.271 +				{
   1.272 +					if (empty($match[1]))
   1.273 +					{
   1.274 +						continue;
   1.275 +					}
   1.276 +
   1.277 +					$pos = utf8_strpos($text, $match[1]);
   1.278 +					if ($pos !== false)
   1.279 +					{
   1.280 +						$word_indizes[] = $pos;
   1.281 +					}
   1.282 +				}
   1.283 +			}
   1.284 +		}
   1.285 +		unset($match);
   1.286 +
   1.287 +		if (sizeof($word_indizes))
   1.288 +		{
   1.289 +			$word_indizes = array_unique($word_indizes);
   1.290 +			sort($word_indizes);
   1.291 +
   1.292 +			$wordnum = sizeof($word_indizes);
   1.293 +			// number of characters on the right and left side of each word
   1.294 +			$sequence_length = (int) ($length / (2 * $wordnum)) - 2;
   1.295 +			$final_text = '';
   1.296 +			$word = $j = 0;
   1.297 +			$final_text_index = -1;
   1.298 +
   1.299 +			// cycle through every character in the original text
   1.300 +			for ($i = $word_indizes[$word], $n = utf8_strlen($text); $i < $n; $i++)
   1.301 +			{
   1.302 +				// if the current position is the start of one of the words then append $sequence_length characters to the final text
   1.303 +				if (isset($word_indizes[$word]) && ($i == $word_indizes[$word]))
   1.304 +				{
   1.305 +					if ($final_text_index < $i - $sequence_length - 1)
   1.306 +					{
   1.307 +						$final_text .= '... ' . preg_replace('#^([^ ]*)#', '', utf8_substr($text, $i - $sequence_length, $sequence_length));
   1.308 +					}
   1.309 +					else
   1.310 +					{
   1.311 +						// if the final text is already nearer to the current word than $sequence_length we only append the text
   1.312 +						// from its current index on and distribute the unused length to all other sequenes
   1.313 +						$sequence_length += (int) (($final_text_index - $i + $sequence_length + 1) / (2 * $wordnum));
   1.314 +						$final_text .= utf8_substr($text, $final_text_index + 1, $i - $final_text_index - 1);
   1.315 +					}
   1.316 +					$final_text_index = $i - 1;
   1.317 +
   1.318 +					// add the following characters to the final text (see below)
   1.319 +					$word++;
   1.320 +					$j = 1;
   1.321 +				}
   1.322 +
   1.323 +				if ($j > 0)
   1.324 +				{
   1.325 +					// add the character to the final text and increment the sequence counter
   1.326 +					$final_text .= utf8_substr($text, $i, 1);
   1.327 +					$final_text_index++;
   1.328 +					$j++;
   1.329 +
   1.330 +					// if this is a whitespace then check whether we are done with this sequence
   1.331 +					if (utf8_substr($text, $i, 1) == ' ')
   1.332 +					{
   1.333 +						// only check whether we have to exit the context generation completely if we haven't already reached the end anyway
   1.334 +						if ($i + 4 < $n)
   1.335 +						{
   1.336 +							if (($j > $sequence_length && $word >= $wordnum) || utf8_strlen($final_text) > $length)
   1.337 +							{
   1.338 +								$final_text .= ' ...';
   1.339 +								break;
   1.340 +							}
   1.341 +						}
   1.342 +						else
   1.343 +						{
   1.344 +							// make sure the text really reaches the end
   1.345 +							$j -= 4;
   1.346 +						}
   1.347 +
   1.348 +						// stop context generation and wait for the next word
   1.349 +						if ($j > $sequence_length)
   1.350 +						{
   1.351 +							$j = 0;
   1.352 +						}
   1.353 +					}
   1.354 +				}
   1.355 +			}
   1.356 +			return str_replace($characters, $entities, $final_text);
   1.357 +		}
   1.358 +	}
   1.359 +
   1.360 +	if (!sizeof($words) || !sizeof($word_indizes))
   1.361 +	{
   1.362 +		return str_replace($characters, $entities, ((utf8_strlen($text) >= $length + 3) ? utf8_substr($text, 0, $length) . '...' : $text));
   1.363 +	}
   1.364 +}
   1.365 +
   1.366 +/**
   1.367 +* Decode text whereby text is coming from the db and expected to be pre-parsed content
   1.368 +* We are placing this outside of the message parser because we are often in need of it...
   1.369 +*/
   1.370 +function decode_message(&$message, $bbcode_uid = '')
   1.371 +{
   1.372 +	global $config;
   1.373 +
   1.374 +	if ($bbcode_uid)
   1.375 +	{
   1.376 +		$match = array('<br />', "[/*:m:$bbcode_uid]", ":u:$bbcode_uid", ":o:$bbcode_uid", ":$bbcode_uid");
   1.377 +		$replace = array("\n", '', '', '', '');
   1.378 +	}
   1.379 +	else
   1.380 +	{
   1.381 +		$match = array('<br />');
   1.382 +		$replace = array("\n");
   1.383 +	}
   1.384 +
   1.385 +	$message = str_replace($match, $replace, $message);
   1.386 +
   1.387 +	$match = get_preg_expression('bbcode_htm');
   1.388 +	$replace = array('\1', '\1', '\2', '\1', '', '');
   1.389 +
   1.390 +	$message = preg_replace($match, $replace, $message);
   1.391 +}
   1.392 +
   1.393 +/**
   1.394 +* Strips all bbcode from a text and returns the plain content
   1.395 +*/
   1.396 +function strip_bbcode(&$text, $uid = '')
   1.397 +{
   1.398 +	if (!$uid)
   1.399 +	{
   1.400 +		$uid = '[0-9a-z]{5,}';
   1.401 +	}
   1.402 +
   1.403 +	$text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);
   1.404 +
   1.405 +	$match = get_preg_expression('bbcode_htm');
   1.406 +	$replace = array('\1', '\1', '\2', '\1', '', '');
   1.407 +
   1.408 +	$text = preg_replace($match, $replace, $text);
   1.409 +}
   1.410 +
   1.411 +/**
   1.412 +* For display of custom parsed text on user-facing pages
   1.413 +* Expects $text to be the value directly from the database (stored value)
   1.414 +*/
   1.415 +function generate_text_for_display($text, $uid, $bitfield, $flags)
   1.416 +{
   1.417 +	static $bbcode;
   1.418 +
   1.419 +	if (!$text)
   1.420 +	{
   1.421 +		return '';
   1.422 +	}
   1.423 +
   1.424 +	$text = censor_text($text);
   1.425 +
   1.426 +	// Parse bbcode if bbcode uid stored and bbcode enabled
   1.427 +	if ($uid && ($flags & OPTION_FLAG_BBCODE))
   1.428 +	{
   1.429 +		if (!class_exists('bbcode'))
   1.430 +		{
   1.431 +			global $phpbb_root_path, $phpEx;
   1.432 +			include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
   1.433 +		}
   1.434 +
   1.435 +		if (empty($bbcode))
   1.436 +		{
   1.437 +			$bbcode = new bbcode($bitfield);
   1.438 +		}
   1.439 +		else
   1.440 +		{
   1.441 +			$bbcode->bbcode($bitfield);
   1.442 +		}
   1.443 +
   1.444 +		$bbcode->bbcode_second_pass($text, $uid);
   1.445 +	}
   1.446 +
   1.447 +	$text = bbcode_nl2br($text);
   1.448 +	$text = smiley_text($text, !($flags & OPTION_FLAG_SMILIES));
   1.449 +
   1.450 +	return $text;
   1.451 +}
   1.452 +
   1.453 +/**
   1.454 +* For parsing custom parsed text to be stored within the database.
   1.455 +* This function additionally returns the uid and bitfield that needs to be stored.
   1.456 +* Expects $text to be the value directly from request_var() and in it's non-parsed form
   1.457 +*/
   1.458 +function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bbcode = false, $allow_urls = false, $allow_smilies = false)
   1.459 +{
   1.460 +	global $phpbb_root_path, $phpEx;
   1.461 +
   1.462 +	$uid = $bitfield = '';
   1.463 +	$flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);
   1.464 +
   1.465 +	if (!$text)
   1.466 +	{
   1.467 +		return;
   1.468 +	}
   1.469 +
   1.470 +	if (!class_exists('parse_message'))
   1.471 +	{
   1.472 +		include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
   1.473 +	}
   1.474 +
   1.475 +	$message_parser = new parse_message($text);
   1.476 +	$message_parser->parse($allow_bbcode, $allow_urls, $allow_smilies);
   1.477 +
   1.478 +	$text = $message_parser->message;
   1.479 +	$uid = $message_parser->bbcode_uid;
   1.480 +
   1.481 +	// If the bbcode_bitfield is empty, there is no need for the uid to be stored.
   1.482 +	if (!$message_parser->bbcode_bitfield)
   1.483 +	{
   1.484 +		$uid = '';
   1.485 +	}
   1.486 +
   1.487 +	$bitfield = $message_parser->bbcode_bitfield;
   1.488 +
   1.489 +	return;
   1.490 +}
   1.491 +
   1.492 +/**
   1.493 +* For decoding custom parsed text for edits as well as extracting the flags
   1.494 +* Expects $text to be the value directly from the database (pre-parsed content)
   1.495 +*/
   1.496 +function generate_text_for_edit($text, $uid, $flags)
   1.497 +{
   1.498 +	global $phpbb_root_path, $phpEx;
   1.499 +
   1.500 +	decode_message($text, $uid);
   1.501 +
   1.502 +	return array(
   1.503 +		'allow_bbcode'	=> ($flags & OPTION_FLAG_BBCODE) ? 1 : 0,
   1.504 +		'allow_smilies'	=> ($flags & OPTION_FLAG_SMILIES) ? 1 : 0,
   1.505 +		'allow_urls'	=> ($flags & OPTION_FLAG_LINKS) ? 1 : 0,
   1.506 +		'text'			=> $text
   1.507 +	);
   1.508 +}
   1.509 +
   1.510 +/**
   1.511 +* A subroutine of make_clickable used with preg_replace
   1.512 +* It places correct HTML around an url, shortens the displayed text
   1.513 +* and makes sure no entities are inside URLs
   1.514 +*/
   1.515 +function make_clickable_callback($type, $whitespace, $url, $relative_url, $class)
   1.516 +{
   1.517 +	$orig_url		= $url;
   1.518 +	$orig_relative	= $relative_url;
   1.519 +	$append			= '';
   1.520 +	$url			= htmlspecialchars_decode($url);
   1.521 +	$relative_url	= htmlspecialchars_decode($relative_url);
   1.522 +
   1.523 +	// make sure no HTML entities were matched
   1.524 +	$chars = array('<', '>', '"');
   1.525 +	$split = false;
   1.526 +
   1.527 +	foreach ($chars as $char)
   1.528 +	{
   1.529 +		$next_split = strpos($url, $char);
   1.530 +		if ($next_split !== false)
   1.531 +		{
   1.532 +			$split = ($split !== false) ? min($split, $next_split) : $next_split;
   1.533 +		}
   1.534 +	}
   1.535 +
   1.536 +	if ($split !== false)
   1.537 +	{
   1.538 +		// an HTML entity was found, so the URL has to end before it
   1.539 +		$append			= substr($url, $split) . $relative_url;
   1.540 +		$url			= substr($url, 0, $split);
   1.541 +		$relative_url	= '';
   1.542 +	}
   1.543 +	else if ($relative_url)
   1.544 +	{
   1.545 +		// same for $relative_url
   1.546 +		$split = false;
   1.547 +		foreach ($chars as $char)
   1.548 +		{
   1.549 +			$next_split = strpos($relative_url, $char);
   1.550 +			if ($next_split !== false)
   1.551 +			{
   1.552 +				$split = ($split !== false) ? min($split, $next_split) : $next_split;
   1.553 +			}
   1.554 +		}
   1.555 +
   1.556 +		if ($split !== false)
   1.557 +		{
   1.558 +			$append			= substr($relative_url, $split);
   1.559 +			$relative_url	= substr($relative_url, 0, $split);
   1.560 +		}
   1.561 +	}
   1.562 +
   1.563 +	// if the last character of the url is a punctuation mark, exclude it from the url
   1.564 +	$last_char = ($relative_url) ? $relative_url[strlen($relative_url) - 1] : $url[strlen($url) - 1];
   1.565 +
   1.566 +	switch ($last_char)
   1.567 +	{
   1.568 +		case '.':
   1.569 +		case '?':
   1.570 +		case '!':
   1.571 +		case ':':
   1.572 +		case ',':
   1.573 +			$append = $last_char;
   1.574 +			if ($relative_url)
   1.575 +			{
   1.576 +				$relative_url = substr($relative_url, 0, -1);
   1.577 +			}
   1.578 +			else
   1.579 +			{
   1.580 +				$url = substr($url, 0, -1);
   1.581 +			}
   1.582 +		break;
   1.583 +
   1.584 +		// set last_char to empty here, so the variable can be used later to
   1.585 +		// check whether a character was removed
   1.586 +		default:
   1.587 +			$last_char = '';
   1.588 +		break;
   1.589 +	}
   1.590 +
   1.591 +	$short_url = (strlen($url) > 55) ? substr($url, 0, 39) . ' ... ' . substr($url, -10) : $url;
   1.592 +
   1.593 +	switch ($type)
   1.594 +	{
   1.595 +		case MAGIC_URL_LOCAL:
   1.596 +			$tag			= 'l';
   1.597 +			$relative_url	= preg_replace('/[&?]sid=[0-9a-f]{32}$/', '', preg_replace('/([&?])sid=[0-9a-f]{32}&/', '$1', $relative_url));
   1.598 +			$url			= $url . '/' . $relative_url;
   1.599 +			$text			= $relative_url;
   1.600 +
   1.601 +			// this url goes to http://domain.tld/path/to/board/ which
   1.602 +			// would result in an empty link if treated as local so
   1.603 +			// don't touch it and let MAGIC_URL_FULL take care of it.
   1.604 +			if (!$relative_url)
   1.605 +			{
   1.606 +				return $whitespace . $orig_url . '/' . $orig_relative; // slash is taken away by relative url pattern
   1.607 +			}
   1.608 +		break;
   1.609 +
   1.610 +		case MAGIC_URL_FULL:
   1.611 +			$tag	= 'm';
   1.612 +			$text	= $short_url;
   1.613 +		break;
   1.614 +
   1.615 +		case MAGIC_URL_WWW:
   1.616 +			$tag	= 'w';
   1.617 +			$url	= 'http://' . $url;
   1.618 +			$text	= $short_url;
   1.619 +		break;
   1.620 +
   1.621 +		case MAGIC_URL_EMAIL:
   1.622 +			$tag	= 'e';
   1.623 +			$text	= $short_url;
   1.624 +			$url	= 'mailto:' . $url;
   1.625 +		break;
   1.626 +	}
   1.627 +
   1.628 +	$url	= htmlspecialchars($url);
   1.629 +	$text	= htmlspecialchars($text);
   1.630 +	$append	= htmlspecialchars($append);
   1.631 +
   1.632 +	$html	= "$whitespace<!-- $tag --><a$class href=\"$url\">$text</a><!-- $tag -->$append";
   1.633 +
   1.634 +	return $html;
   1.635 +}
   1.636 +
   1.637 +/**
   1.638 +* make_clickable function
   1.639 +*
   1.640 +* Replace magic urls of form http://xxx.xxx., www.xxx. and xxx@xxx.xxx.
   1.641 +* Cuts down displayed size of link if over 50 chars, turns absolute links
   1.642 +* into relative versions when the server/script path matches the link
   1.643 +*/
   1.644 +function make_clickable($text, $server_url = false, $class = 'postlink')
   1.645 +{
   1.646 +	if ($server_url === false)
   1.647 +	{
   1.648 +		$server_url = generate_board_url();
   1.649 +	}
   1.650 +
   1.651 +	static $magic_url_match;
   1.652 +	static $magic_url_replace;
   1.653 +	static $static_class;
   1.654 +
   1.655 +	if (!is_array($magic_url_match) || $static_class != $class)
   1.656 +	{
   1.657 +		$static_class = $class;
   1.658 +		$class = ($static_class) ? ' class="' . $static_class . '"' : '';
   1.659 +		$local_class = ($static_class) ? ' class="' . $static_class . '-local"' : '';
   1.660 +
   1.661 +		$magic_url_match = $magic_url_replace = array();
   1.662 +		// Be sure to not let the matches cross over. ;)
   1.663 +
   1.664 +		// relative urls for this board
   1.665 +		$magic_url_match[] = '#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#ie';
   1.666 +		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_LOCAL, '\$1', '\$2', '\$3', '$local_class')";
   1.667 +
   1.668 +		// matches a xxxx://aaaaa.bbb.cccc. ...
   1.669 +		$magic_url_match[] = '#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#ie';
   1.670 +		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_FULL, '\$1', '\$2', '', '$class')";
   1.671 +
   1.672 +		// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
   1.673 +		$magic_url_match[] = '#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#ie';
   1.674 +		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_WWW, '\$1', '\$2', '', '$class')";
   1.675 +
   1.676 +		// matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode.
   1.677 +		$magic_url_match[] = '/(^|[\n\t (>])(' . get_preg_expression('email') . ')/ie';
   1.678 +		$magic_url_replace[] = "make_clickable_callback(MAGIC_URL_EMAIL, '\$1', '\$2', '', '')";
   1.679 +	}
   1.680 +
   1.681 +	return preg_replace($magic_url_match, $magic_url_replace, $text);
   1.682 +}
   1.683 +
   1.684 +/**
   1.685 +* Censoring
   1.686 +*/
   1.687 +function censor_text($text)
   1.688 +{
   1.689 +	static $censors;
   1.690 +
   1.691 +	// Nothing to do?
   1.692 +	if ($text === '')
   1.693 +	{
   1.694 +		return '';
   1.695 +	}
   1.696 +
   1.697 +	// We moved the word censor checks in here because we call this function quite often - and then only need to do the check once
   1.698 +	if (!isset($censors) || !is_array($censors))
   1.699 +	{
   1.700 +		global $config, $user, $auth, $cache;
   1.701 +
   1.702 +		// We check here if the user is having viewing censors disabled (and also allowed to do so).
   1.703 +		if (!$user->optionget('viewcensors') && $config['allow_nocensors'] && $auth->acl_get('u_chgcensors'))
   1.704 +		{
   1.705 +			$censors = array();
   1.706 +		}
   1.707 +		else
   1.708 +		{
   1.709 +			$censors = $cache->obtain_word_list();
   1.710 +		}
   1.711 +	}
   1.712 +
   1.713 +	if (sizeof($censors))
   1.714 +	{
   1.715 +		return preg_replace($censors['match'], $censors['replace'], $text);
   1.716 +	}
   1.717 +
   1.718 +	return $text;
   1.719 +}
   1.720 +
   1.721 +/**
   1.722 +* custom version of nl2br which takes custom BBCodes into account
   1.723 +*/
   1.724 +function bbcode_nl2br($text)
   1.725 +{
   1.726 +	// custom BBCodes might contain carriage returns so they
   1.727 +	// are not converted into <br /> so now revert that
   1.728 +	$text = str_replace(array("\n", "\r"), array('<br />', "\n"), $text);
   1.729 +	return $text;
   1.730 +}
   1.731 +
   1.732 +/**
   1.733 +* Smiley processing
   1.734 +*/
   1.735 +function smiley_text($text, $force_option = false)
   1.736 +{
   1.737 +	global $config, $user, $phpbb_root_path;
   1.738 +
   1.739 +	if ($force_option || !$config['allow_smilies'] || !$user->optionget('viewsmilies'))
   1.740 +	{
   1.741 +		return preg_replace('#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/.*? \/><!\-\- s\1 \-\->#', '\1', $text);
   1.742 +	}
   1.743 +	else
   1.744 +	{
   1.745 +		$root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
   1.746 +		return preg_replace('#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/(.*?) \/><!\-\- s\1 \-\->#', '<img src="' . $root_path . $config['smilies_path'] . '/\2 />', $text);
   1.747 +	}
   1.748 +}
   1.749 +
   1.750 +/**
   1.751 +* General attachment parsing
   1.752 +*
   1.753 +* @param mixed $forum_id The forum id the attachments are displayed in (false if in private message)
   1.754 +* @param string &$message The post/private message
   1.755 +* @param array &$attachments The attachments to parse for (inline) display. The attachments array will hold templated data after parsing.
   1.756 +* @param array &$update_count The attachment counts to be updated - will be filled
   1.757 +* @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.
   1.758 +*/
   1.759 +function parse_attachments($forum_id, &$message, &$attachments, &$update_count, $preview = false)
   1.760 +{
   1.761 +	if (!sizeof($attachments))
   1.762 +	{
   1.763 +		return;
   1.764 +	}
   1.765 +
   1.766 +	global $template, $cache, $user;
   1.767 +	global $extensions, $config, $phpbb_root_path, $phpEx;
   1.768 +
   1.769 +	//
   1.770 +	$compiled_attachments = array();
   1.771 +
   1.772 +	if (!isset($template->filename['attachment_tpl']))
   1.773 +	{
   1.774 +		$template->set_filenames(array(
   1.775 +			'attachment_tpl'	=> 'attachment.html')
   1.776 +		);
   1.777 +	}
   1.778 +
   1.779 +	if (empty($extensions) || !is_array($extensions))
   1.780 +	{
   1.781 +		$extensions = $cache->obtain_attach_extensions($forum_id);
   1.782 +	}
   1.783 +
   1.784 +	// Look for missing attachment information...
   1.785 +	$attach_ids = array();
   1.786 +	foreach ($attachments as $pos => $attachment)
   1.787 +	{
   1.788 +		// If is_orphan is set, we need to retrieve the attachments again...
   1.789 +		if (!isset($attachment['extension']) && !isset($attachment['physical_filename']))
   1.790 +		{
   1.791 +			$attach_ids[(int) $attachment['attach_id']] = $pos;
   1.792 +		}
   1.793 +	}
   1.794 +
   1.795 +	// Grab attachments (security precaution)
   1.796 +	if (sizeof($attach_ids))
   1.797 +	{
   1.798 +		global $db;
   1.799 +
   1.800 +		$new_attachment_data = array();
   1.801 +
   1.802 +		$sql = 'SELECT *
   1.803 +			FROM ' . ATTACHMENTS_TABLE . '
   1.804 +			WHERE ' . $db->sql_in_set('attach_id', array_keys($attach_ids));
   1.805 +		$result = $db->sql_query($sql);
   1.806 +
   1.807 +		while ($row = $db->sql_fetchrow($result))
   1.808 +		{
   1.809 +			if (!isset($attach_ids[$row['attach_id']]))
   1.810 +			{
   1.811 +				continue;
   1.812 +			}
   1.813 +
   1.814 +			// If we preview attachments we will set some retrieved values here
   1.815 +			if ($preview)
   1.816 +			{
   1.817 +				$row['attach_comment'] = $attachments[$attach_ids[$row['attach_id']]]['attach_comment'];
   1.818 +			}
   1.819 +
   1.820 +			$new_attachment_data[$attach_ids[$row['attach_id']]] = $row;
   1.821 +		}
   1.822 +		$db->sql_freeresult($result);
   1.823 +
   1.824 +		$attachments = $new_attachment_data;
   1.825 +		unset($new_attachment_data);
   1.826 +	}
   1.827 +
   1.828 +	// Sort correctly
   1.829 +	if ($config['display_order'])
   1.830 +	{
   1.831 +		// Ascending sort
   1.832 +		krsort($attachments);
   1.833 +	}
   1.834 +	else
   1.835 +	{
   1.836 +		// Descending sort
   1.837 +		ksort($attachments);
   1.838 +	}
   1.839 +
   1.840 +	foreach ($attachments as $attachment)
   1.841 +	{
   1.842 +		if (!sizeof($attachment))
   1.843 +		{
   1.844 +			continue;
   1.845 +		}
   1.846 +
   1.847 +		// We need to reset/empty the _file block var, because this function might be called more than once
   1.848 +		$template->destroy_block_vars('_file');
   1.849 +
   1.850 +		$block_array = array();
   1.851 +
   1.852 +		// Some basics...
   1.853 +		$attachment['extension'] = strtolower(trim($attachment['extension']));
   1.854 +		$filename = $phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($attachment['physical_filename']);
   1.855 +		$thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/thumb_' . utf8_basename($attachment['physical_filename']);
   1.856 +
   1.857 +		$upload_icon = '';
   1.858 +
   1.859 +		if (isset($extensions[$attachment['extension']]))
   1.860 +		{
   1.861 +			if ($user->img('icon_topic_attach', '') && !$extensions[$attachment['extension']]['upload_icon'])
   1.862 +			{
   1.863 +				$upload_icon = $user->img('icon_topic_attach', '');
   1.864 +			}
   1.865 +			else if ($extensions[$attachment['extension']]['upload_icon'])
   1.866 +			{
   1.867 +				$upload_icon = '<img src="' . $phpbb_root_path . $config['upload_icons_path'] . '/' . trim($extensions[$attachment['extension']]['upload_icon']) . '" alt="" />';
   1.868 +			}
   1.869 +		}
   1.870 +
   1.871 +		$filesize = get_formatted_filesize($attachment['filesize'], false);
   1.872 +
   1.873 +		$comment = bbcode_nl2br(censor_text($attachment['attach_comment']));
   1.874 +
   1.875 +		$block_array += array(
   1.876 +			'UPLOAD_ICON'		=> $upload_icon,
   1.877 +			'FILESIZE'			=> $filesize['value'],
   1.878 +			'SIZE_LANG'			=> $filesize['unit'],
   1.879 +			'DOWNLOAD_NAME'		=> utf8_basename($attachment['real_filename']),
   1.880 +			'COMMENT'			=> $comment,
   1.881 +		);
   1.882 +
   1.883 +		$denied = false;
   1.884 +
   1.885 +		if (!extension_allowed($forum_id, $attachment['extension'], $extensions))
   1.886 +		{
   1.887 +			$denied = true;
   1.888 +
   1.889 +			$block_array += array(
   1.890 +				'S_DENIED'			=> true,
   1.891 +				'DENIED_MESSAGE'	=> sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension'])
   1.892 +			);
   1.893 +		}
   1.894 +
   1.895 +		if (!$denied)
   1.896 +		{
   1.897 +			$l_downloaded_viewed = $download_link = '';
   1.898 +			$display_cat = $extensions[$attachment['extension']]['display_cat'];
   1.899 +
   1.900 +			if ($display_cat == ATTACHMENT_CATEGORY_IMAGE)
   1.901 +			{
   1.902 +				if ($attachment['thumbnail'])
   1.903 +				{
   1.904 +					$display_cat = ATTACHMENT_CATEGORY_THUMB;
   1.905 +				}
   1.906 +				else
   1.907 +				{
   1.908 +					if ($config['img_display_inlined'])
   1.909 +					{
   1.910 +						if ($config['img_link_width'] || $config['img_link_height'])
   1.911 +						{
   1.912 +							$dimension = @getimagesize($filename);
   1.913 +
   1.914 +							// If the dimensions could not be determined or the image being 0x0 we display it as a link for safety purposes
   1.915 +							if ($dimension === false || empty($dimension[0]) || empty($dimension[1]))
   1.916 +							{
   1.917 +								$display_cat = ATTACHMENT_CATEGORY_NONE;
   1.918 +							}
   1.919 +							else
   1.920 +							{
   1.921 +								$display_cat = ($dimension[0] <= $config['img_link_width'] && $dimension[1] <= $config['img_link_height']) ? ATTACHMENT_CATEGORY_IMAGE : ATTACHMENT_CATEGORY_NONE;
   1.922 +							}
   1.923 +						}
   1.924 +					}
   1.925 +					else
   1.926 +					{
   1.927 +						$display_cat = ATTACHMENT_CATEGORY_NONE;
   1.928 +					}
   1.929 +				}
   1.930 +			}
   1.931 +
   1.932 +			// Make some descisions based on user options being set.
   1.933 +			if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
   1.934 +			{
   1.935 +				$display_cat = ATTACHMENT_CATEGORY_NONE;
   1.936 +			}
   1.937 +
   1.938 +			if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
   1.939 +			{
   1.940 +				$display_cat = ATTACHMENT_CATEGORY_NONE;
   1.941 +			}
   1.942 +
   1.943 +			$download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
   1.944 +
   1.945 +			switch ($display_cat)
   1.946 +			{
   1.947 +				// Images
   1.948 +				case ATTACHMENT_CATEGORY_IMAGE:
   1.949 +					$l_downloaded_viewed = 'VIEWED_COUNT';
   1.950 +					$inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
   1.951 +					$download_link .= '&amp;mode=view';
   1.952 +
   1.953 +					$block_array += array(
   1.954 +						'S_IMAGE'		=> true,
   1.955 +						'U_INLINE_LINK'		=> $inline_link,
   1.956 +					);
   1.957 +
   1.958 +					$update_count[] = $attachment['attach_id'];
   1.959 +				break;
   1.960 +
   1.961 +				// Images, but display Thumbnail
   1.962 +				case ATTACHMENT_CATEGORY_THUMB:
   1.963 +					$l_downloaded_viewed = 'VIEWED_COUNT';
   1.964 +					$thumbnail_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id'] . '&amp;t=1');
   1.965 +					$download_link .= '&amp;mode=view';
   1.966 +
   1.967 +					$block_array += array(
   1.968 +						'S_THUMBNAIL'		=> true,
   1.969 +						'THUMB_IMAGE'		=> $thumbnail_link,
   1.970 +					);
   1.971 +
   1.972 +					$update_count[] = $attachment['attach_id'];
   1.973 +				break;
   1.974 +
   1.975 +				// Windows Media Streams
   1.976 +				case ATTACHMENT_CATEGORY_WM:
   1.977 +					$l_downloaded_viewed = 'VIEWED_COUNT';
   1.978 +
   1.979 +					// Giving the filename directly because within the wm object all variables are in local context making it impossible
   1.980 +					// to validate against a valid session (all params can differ)
   1.981 +					// $download_link = $filename;
   1.982 +
   1.983 +					$block_array += array(
   1.984 +						'U_FORUM'		=> generate_board_url(),
   1.985 +						'ATTACH_ID'		=> $attachment['attach_id'],
   1.986 +						'S_WM_FILE'		=> true,
   1.987 +					);
   1.988 +
   1.989 +					// Viewed/Heared File ... update the download count
   1.990 +					$update_count[] = $attachment['attach_id'];
   1.991 +				break;
   1.992 +
   1.993 +				// Real Media Streams
   1.994 +				case ATTACHMENT_CATEGORY_RM:
   1.995 +				case ATTACHMENT_CATEGORY_QUICKTIME:
   1.996 +					$l_downloaded_viewed = 'VIEWED_COUNT';
   1.997 +
   1.998 +					$block_array += array(
   1.999 +						'S_RM_FILE'			=> ($display_cat == ATTACHMENT_CATEGORY_RM) ? true : false,
  1.1000 +						'S_QUICKTIME_FILE'	=> ($display_cat == ATTACHMENT_CATEGORY_QUICKTIME) ? true : false,
  1.1001 +						'U_FORUM'			=> generate_board_url(),
  1.1002 +						'ATTACH_ID'			=> $attachment['attach_id'],
  1.1003 +					);
  1.1004 +
  1.1005 +					// Viewed/Heared File ... update the download count
  1.1006 +					$update_count[] = $attachment['attach_id'];
  1.1007 +				break;
  1.1008 +
  1.1009 +				// Macromedia Flash Files
  1.1010 +				case ATTACHMENT_CATEGORY_FLASH:
  1.1011 +					list($width, $height) = @getimagesize($filename);
  1.1012 +
  1.1013 +					$l_downloaded_viewed = 'VIEWED_COUNT';
  1.1014 +
  1.1015 +					$block_array += array(
  1.1016 +						'S_FLASH_FILE'	=> true,
  1.1017 +						'WIDTH'			=> $width,
  1.1018 +						'HEIGHT'		=> $height,
  1.1019 +						'U_VIEW_LINK'	=> $download_link . '&amp;view=1',
  1.1020 +					);
  1.1021 +
  1.1022 +					// Viewed/Heared File ... update the download count
  1.1023 +					$update_count[] = $attachment['attach_id'];
  1.1024 +				break;
  1.1025 +
  1.1026 +				default:
  1.1027 +					$l_downloaded_viewed = 'DOWNLOAD_COUNT';
  1.1028 +
  1.1029 +					$block_array += array(
  1.1030 +						'S_FILE'		=> true,
  1.1031 +					);
  1.1032 +				break;
  1.1033 +			}
  1.1034 +
  1.1035 +			$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']));
  1.1036 +
  1.1037 +			$block_array += array(
  1.1038 +				'U_DOWNLOAD_LINK'		=> $download_link,
  1.1039 +				'L_DOWNLOAD_COUNT'		=> $l_download_count
  1.1040 +			);
  1.1041 +		}
  1.1042 +
  1.1043 +		$template->assign_block_vars('_file', $block_array);
  1.1044 +
  1.1045 +		$compiled_attachments[] = $template->assign_display('attachment_tpl');
  1.1046 +	}
  1.1047 +
  1.1048 +	$attachments = $compiled_attachments;
  1.1049 +	unset($compiled_attachments);
  1.1050 +
  1.1051 +	$tpl_size = sizeof($attachments);
  1.1052 +
  1.1053 +	$unset_tpl = array();
  1.1054 +
  1.1055 +	preg_match_all('#<!\-\- ia([0-9]+) \-\->(.*?)<!\-\- ia\1 \-\->#', $message, $matches, PREG_PATTERN_ORDER);
  1.1056 +
  1.1057 +	$replace = array();
  1.1058 +	foreach ($matches[0] as $num => $capture)
  1.1059 +	{
  1.1060 +		// Flip index if we are displaying the reverse way
  1.1061 +		$index = ($config['display_order']) ? ($tpl_size-($matches[1][$num] + 1)) : $matches[1][$num];
  1.1062 +
  1.1063 +		$replace['from'][] = $matches[0][$num];
  1.1064 +		$replace['to'][] = (isset($attachments[$index])) ? $attachments[$index] : sprintf($user->lang['MISSING_INLINE_ATTACHMENT'], $matches[2][array_search($index, $matches[1])]);
  1.1065 +
  1.1066 +		$unset_tpl[] = $index;
  1.1067 +	}
  1.1068 +
  1.1069 +	if (isset($replace['from']))
  1.1070 +	{
  1.1071 +		$message = str_replace($replace['from'], $replace['to'], $message);
  1.1072 +	}
  1.1073 +
  1.1074 +	$unset_tpl = array_unique($unset_tpl);
  1.1075 +
  1.1076 +	// Needed to let not display the inlined attachments at the end of the post again
  1.1077 +	foreach ($unset_tpl as $index)
  1.1078 +	{
  1.1079 +		unset($attachments[$index]);
  1.1080 +	}
  1.1081 +}
  1.1082 +
  1.1083 +/**
  1.1084 +* Check if extension is allowed to be posted.
  1.1085 +*
  1.1086 +* @param mixed $forum_id The forum id to check or false if private message
  1.1087 +* @param string $extension The extension to check, for example zip.
  1.1088 +* @param array &$extensions The extension array holding the information from the cache (will be obtained if empty)
  1.1089 +*
  1.1090 +* @return bool False if the extension is not allowed to be posted, else true.
  1.1091 +*/
  1.1092 +function extension_allowed($forum_id, $extension, &$extensions)
  1.1093 +{
  1.1094 +	if (empty($extensions))
  1.1095 +	{
  1.1096 +		global $cache;
  1.1097 +		$extensions = $cache->obtain_attach_extensions($forum_id);
  1.1098 +	}
  1.1099 +
  1.1100 +	return (!isset($extensions['_allowed_'][$extension])) ? false : true;
  1.1101 +}
  1.1102 +
  1.1103 +/**
  1.1104 +* Truncates string while retaining special characters if going over the max length
  1.1105 +* The default max length is 60 at the moment
  1.1106 +* The maximum storage length is there to fit the string within the given length. The string may be further truncated due to html entities.
  1.1107 +* For example: string given is 'a "quote"' (length: 9), would be a stored as 'a &quot;quote&quot;' (length: 19)
  1.1108 +*
  1.1109 +* @param string $string The text to truncate to the given length. String is specialchared.
  1.1110 +* @param int $max_length Maximum length of string (multibyte character count as 1 char / Html entity count as 1 char)
  1.1111 +* @param int $max_store_length Maximum character length of string (multibyte character count as 1 char / Html entity count as entity chars).
  1.1112 +* @param bool $allow_reply Allow Re: in front of string 
  1.1113 +* 	NOTE: This parameter can cause undesired behavior (returning strings longer than $max_store_length) and is deprecated. 
  1.1114 +* @param string $append String to be appended
  1.1115 +*/
  1.1116 +function truncate_string($string, $max_length = 60, $max_store_length = 255, $allow_reply = false, $append = '')
  1.1117 +{
  1.1118 +	$chars = array();
  1.1119 +
  1.1120 +	$strip_reply = false;
  1.1121 +	$stripped = false;
  1.1122 +	if ($allow_reply && strpos($string, 'Re: ') === 0)
  1.1123 +	{
  1.1124 +		$strip_reply = true;
  1.1125 +		$string = substr($string, 4);
  1.1126 +	}
  1.1127 +
  1.1128 +	$_chars = utf8_str_split(htmlspecialchars_decode($string));
  1.1129 +	$chars = array_map('utf8_htmlspecialchars', $_chars);
  1.1130 +
  1.1131 +	// Now check the length ;)
  1.1132 +	if (sizeof($chars) > $max_length)
  1.1133 +	{
  1.1134 +		// Cut off the last elements from the array
  1.1135 +		$string = implode('', array_slice($chars, 0, $max_length - utf8_strlen($append)));
  1.1136 +		$stripped = true;
  1.1137 +	}
  1.1138 +
  1.1139 +	// Due to specialchars, we may not be able to store the string...
  1.1140 +	if (utf8_strlen($string) > $max_store_length)
  1.1141 +	{
  1.1142 +		// let's split again, we do not want half-baked strings where entities are split
  1.1143 +		$_chars = utf8_str_split(htmlspecialchars_decode($string));
  1.1144 +		$chars = array_map('utf8_htmlspecialchars', $_chars);
  1.1145 +
  1.1146 +		do
  1.1147 +		{
  1.1148 +			array_pop($chars);
  1.1149 +			$string = implode('', $chars);
  1.1150 +		}
  1.1151 +		while (!empty($chars) && utf8_strlen($string) > $max_store_length);
  1.1152 +	}
  1.1153 +
  1.1154 +	if ($strip_reply)
  1.1155 +	{
  1.1156 +		$string = 'Re: ' . $string;
  1.1157 +	}
  1.1158 +
  1.1159 +	if ($append != '' && $stripped)
  1.1160 +	{
  1.1161 +		$string = $string . $append;
  1.1162 +	}
  1.1163 +
  1.1164 +	return $string;
  1.1165 +}
  1.1166 +
  1.1167 +/**
  1.1168 +* Get username details for placing into templates.
  1.1169 +* This function caches all modes on first call, except for no_profile and anonymous user - determined by $user_id.
  1.1170 +*
  1.1171 +* @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)
  1.1172 +* @param int $user_id The users id
  1.1173 +* @param string $username The users name
  1.1174 +* @param string $username_colour The users colour
  1.1175 +* @param string $guest_username optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then.
  1.1176 +* @param string $custom_profile_url optional parameter to specify a profile url. The user id get appended to this url as &amp;u={user_id}
  1.1177 +*
  1.1178 +* @return string A string consisting of what is wanted based on $mode.
  1.1179 +* @author BartVB, Acyd Burn
  1.1180 +*/
  1.1181 +function get_username_string($mode, $user_id, $username, $username_colour = '', $guest_username = false, $custom_profile_url = false)
  1.1182 +{
  1.1183 +	static $_profile_cache;
  1.1184 +
  1.1185 +	// We cache some common variables we need within this function
  1.1186 +	if (empty($_profile_cache))
  1.1187 +	{
  1.1188 +		global $phpbb_root_path, $phpEx;
  1.1189 +
  1.1190 +		$_profile_cache['base_url'] = append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&amp;u={USER_ID}');
  1.1191 +		$_profile_cache['tpl_noprofile'] = '{USERNAME}';
  1.1192 +		$_profile_cache['tpl_noprofile_colour'] = '<span style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</span>';
  1.1193 +		$_profile_cache['tpl_profile'] = '<a href="{PROFILE_URL}">{USERNAME}</a>';
  1.1194 +		$_profile_cache['tpl_profile_colour'] = '<a href="{PROFILE_URL}" style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</a>';
  1.1195 +	}
  1.1196 +
  1.1197 +	global $user, $auth;
  1.1198 +
  1.1199 +	// This switch makes sure we only run code required for the mode
  1.1200 +	switch ($mode)
  1.1201 +	{
  1.1202 +		case 'full':
  1.1203 +		case 'no_profile':
  1.1204 +		case 'colour':
  1.1205 +
  1.1206 +			// Build correct username colour
  1.1207 +			$username_colour = ($username_colour) ? '#' . $username_colour : '';
  1.1208 +
  1.1209 +			// Return colour
  1.1210 +			if ($mode == 'colour')
  1.1211 +			{
  1.1212 +				return $username_colour;
  1.1213 +			}
  1.1214 +
  1.1215 +		// no break;
  1.1216 +
  1.1217 +		case 'username':
  1.1218 +
  1.1219 +			// Build correct username
  1.1220 +			if ($guest_username === false)
  1.1221 +			{
  1.1222 +				$username = ($username) ? $username : $user->lang['GUEST'];
  1.1223 +			}
  1.1224 +			else
  1.1225 +			{
  1.1226 +				$username = ($user_id && $user_id != ANONYMOUS) ? $username : ((!empty($guest_username)) ? $guest_username : $user->lang['GUEST']);
  1.1227 +			}
  1.1228 +
  1.1229 +			// Return username
  1.1230 +			if ($mode == 'username')
  1.1231 +			{
  1.1232 +				return $username;
  1.1233 +			}
  1.1234 +
  1.1235 +		// no break;
  1.1236 +
  1.1237 +		case 'profile':
  1.1238 +
  1.1239 +			// Build correct profile url - only show if not anonymous and permission to view profile if registered user
  1.1240 +			// For anonymous the link leads to a login page.
  1.1241 +			if ($user_id && $user_id != ANONYMOUS && ($user->data['user_id'] == ANONYMOUS || $auth->acl_get('u_viewprofile')))
  1.1242 +			{
  1.1243 +				$profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&amp;u=' . (int) $user_id : str_replace(array('={USER_ID}', '=%7BUSER_ID%7D'), '=' . (int) $user_id, $_profile_cache['base_url']);
  1.1244 +			}
  1.1245 +			else
  1.1246 +			{
  1.1247 +				$profile_url = '';
  1.1248 +			}
  1.1249 +
  1.1250 +			// Return profile
  1.1251 +			if ($mode == 'profile')
  1.1252 +			{
  1.1253 +				return $profile_url;
  1.1254 +			}
  1.1255 +
  1.1256 +		// no break;
  1.1257 +	}
  1.1258 +
  1.1259 +	if (($mode == 'full' && !$profile_url) || $mode == 'no_profile')
  1.1260 +	{
  1.1261 +		return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_noprofile'] : $_profile_cache['tpl_noprofile_colour']);
  1.1262 +	}
  1.1263 +
  1.1264 +	return str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_profile'] : $_profile_cache['tpl_profile_colour']);
  1.1265 +}
  1.1266 +
  1.1267 +/**
  1.1268 +* @package phpBB3
  1.1269 +*/
  1.1270 +class bitfield
  1.1271 +{
  1.1272 +	var $data;
  1.1273 +
  1.1274 +	function bitfield($bitfield = '')
  1.1275 +	{
  1.1276 +		$this->data = base64_decode($bitfield);
  1.1277 +	}
  1.1278 +
  1.1279 +	/**
  1.1280 +	*/
  1.1281 +	function get($n)
  1.1282 +	{
  1.1283 +		// Get the ($n / 8)th char
  1.1284 +		$byte = $n >> 3;
  1.1285 +
  1.1286 +		if (strlen($this->data) >= $byte + 1)
  1.1287 +		{
  1.1288 +			$c = $this->data[$byte];
  1.1289 +
  1.1290 +			// Lookup the ($n % 8)th bit of the byte
  1.1291 +			$bit = 7 - ($n & 7);
  1.1292 +			return (bool) (ord($c) & (1 << $bit));
  1.1293 +		}
  1.1294 +		else
  1.1295 +		{
  1.1296 +			return false;
  1.1297 +		}
  1.1298 +	}
  1.1299 +
  1.1300 +	function set($n)
  1.1301 +	{
  1.1302 +		$byte = $n >> 3;
  1.1303 +		$bit = 7 - ($n & 7);
  1.1304 +
  1.1305 +		if (strlen($this->data) >= $byte + 1)
  1.1306 +		{
  1.1307 +			$this->data[$byte] = $this->data[$byte] | chr(1 << $bit);
  1.1308 +		}
  1.1309 +		else
  1.1310 +		{
  1.1311 +			$this->data .= str_repeat("\0", $byte - strlen($this->data));
  1.1312 +			$this->data .= chr(1 << $bit);
  1.1313 +		}
  1.1314 +	}
  1.1315 +
  1.1316 +	function clear($n)
  1.1317 +	{
  1.1318 +		$byte = $n >> 3;
  1.1319 +
  1.1320 +		if (strlen($this->data) >= $byte + 1)
  1.1321 +		{
  1.1322 +			$bit = 7 - ($n & 7);
  1.1323 +			$this->data[$byte] = $this->data[$byte] &~ chr(1 << $bit);
  1.1324 +		}
  1.1325 +	}
  1.1326 +
  1.1327 +	function get_blob()
  1.1328 +	{
  1.1329 +		return $this->data;
  1.1330 +	}
  1.1331 +
  1.1332 +	function get_base64()
  1.1333 +	{
  1.1334 +		return base64_encode($this->data);
  1.1335 +	}
  1.1336 +
  1.1337 +	function get_bin()
  1.1338 +	{
  1.1339 +		$bin = '';
  1.1340 +		$len = strlen($this->data);
  1.1341 +
  1.1342 +		for ($i = 0; $i < $len; ++$i)
  1.1343 +		{
  1.1344 +			$bin .= str_pad(decbin(ord($this->data[$i])), 8, '0', STR_PAD_LEFT);
  1.1345 +		}
  1.1346 +
  1.1347 +		return $bin;
  1.1348 +	}
  1.1349 +
  1.1350 +	function get_all_set()
  1.1351 +	{
  1.1352 +		return array_keys(array_filter(str_split($this->get_bin())));
  1.1353 +	}
  1.1354 +
  1.1355 +	function merge($bitfield)
  1.1356 +	{
  1.1357 +		$this->data = $this->data | $bitfield->get_blob();
  1.1358 +	}
  1.1359 +}
  1.1360 +
  1.1361 +?>
  1.1362 \ No newline at end of file