includes/functions_privmsgs.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_privmsgs.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1992 @@
     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 +*/
    1.16 +if (!defined('IN_PHPBB'))
    1.17 +{
    1.18 +	exit;
    1.19 +}
    1.20 +
    1.21 +/*
    1.22 +	Ability to simply add own rules by doing three things:
    1.23 +		1) Add an appropriate constant
    1.24 +		2) Add a new check array to the global_privmsgs_rules variable and the condition array (if one is required)
    1.25 +		3) Add a new language variable to ucp.php
    1.26 +
    1.27 +		The user is then able to select the new rule. It will be checked against and handled as specified.
    1.28 +		To add new actions (yes, checks can be added here too) to the rule management, the core code has to be modified.
    1.29 +*/
    1.30 +
    1.31 +define('RULE_IS_LIKE', 1);		// Is Like
    1.32 +define('RULE_IS_NOT_LIKE', 2);	// Is Not Like
    1.33 +define('RULE_IS', 3);			// Is
    1.34 +define('RULE_IS_NOT', 4);		// Is Not
    1.35 +define('RULE_BEGINS_WITH', 5);	// Begins with
    1.36 +define('RULE_ENDS_WITH', 6);	// Ends with
    1.37 +define('RULE_IS_FRIEND', 7);	// Is Friend
    1.38 +define('RULE_IS_FOE', 8);		// Is Foe
    1.39 +define('RULE_IS_USER', 9);		// Is User
    1.40 +define('RULE_IS_GROUP', 10);	// Is In Usergroup
    1.41 +define('RULE_ANSWERED', 11);	// Answered
    1.42 +define('RULE_FORWARDED', 12);	// Forwarded
    1.43 +define('RULE_TO_GROUP', 14);	// Usergroup
    1.44 +define('RULE_TO_ME', 15);		// Me
    1.45 +
    1.46 +define('ACTION_PLACE_INTO_FOLDER', 1);
    1.47 +define('ACTION_MARK_AS_READ', 2);
    1.48 +define('ACTION_MARK_AS_IMPORTANT', 3);
    1.49 +define('ACTION_DELETE_MESSAGE', 4);
    1.50 +
    1.51 +define('CHECK_SUBJECT', 1);
    1.52 +define('CHECK_SENDER', 2);
    1.53 +define('CHECK_MESSAGE', 3);
    1.54 +define('CHECK_STATUS', 4);
    1.55 +define('CHECK_TO', 5);
    1.56 +
    1.57 +/**
    1.58 +* Global private message rules
    1.59 +* These rules define what to do if a rule is hit
    1.60 +*/
    1.61 +$global_privmsgs_rules = array(
    1.62 +	CHECK_SUBJECT	=> array(
    1.63 +		RULE_IS_LIKE		=> array('check0' => 'message_subject', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
    1.64 +		RULE_IS_NOT_LIKE	=> array('check0' => 'message_subject', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
    1.65 +		RULE_IS				=> array('check0' => 'message_subject', 'function' => '{CHECK0} == {STRING}'),
    1.66 +		RULE_IS_NOT			=> array('check0' => 'message_subject', 'function' => '{CHECK0} != {STRING}'),
    1.67 +		RULE_BEGINS_WITH	=> array('check0' => 'message_subject', 'function' => 'preg_match("/^" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
    1.68 +		RULE_ENDS_WITH		=> array('check0' => 'message_subject', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "$/i", {CHECK0})'),
    1.69 +	),
    1.70 +
    1.71 +	CHECK_SENDER	=> array(
    1.72 +		RULE_IS_LIKE		=> array('check0' => 'username', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
    1.73 +		RULE_IS_NOT_LIKE	=> array('check0' => 'username', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
    1.74 +		RULE_IS				=> array('check0' => 'username', 'function' => '{CHECK0} == {STRING}'),
    1.75 +		RULE_IS_NOT			=> array('check0' => 'username', 'function' => '{CHECK0} != {STRING}'),
    1.76 +		RULE_BEGINS_WITH	=> array('check0' => 'username', 'function' => 'preg_match("/^" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
    1.77 +		RULE_ENDS_WITH		=> array('check0' => 'username', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "$/i", {CHECK0})'),
    1.78 +		RULE_IS_FRIEND		=> array('check0' => 'friend', 'function' => '{CHECK0} == 1'),
    1.79 +		RULE_IS_FOE			=> array('check0' => 'foe', 'function' => '{CHECK0} == 1'),
    1.80 +		RULE_IS_USER		=> array('check0' => 'author_id', 'function' => '{CHECK0} == {USER_ID}'),
    1.81 +		RULE_IS_GROUP		=> array('check0' => 'author_in_group', 'function' => 'in_array({GROUP_ID}, {CHECK0})'),
    1.82 +	),
    1.83 +
    1.84 +	CHECK_MESSAGE	=> array(
    1.85 +		RULE_IS_LIKE		=> array('check0' => 'message_text', 'function' => 'preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0})'),
    1.86 +		RULE_IS_NOT_LIKE	=> array('check0' => 'message_text', 'function' => '!(preg_match("/" . preg_quote({STRING}, "/") . "/i", {CHECK0}))'),
    1.87 +		RULE_IS				=> array('check0' => 'message_text', 'function' => '{CHECK0} == {STRING}'),
    1.88 +		RULE_IS_NOT			=> array('check0' => 'message_text', 'function' => '{CHECK0} != {STRING}'),
    1.89 +	),
    1.90 +
    1.91 +	CHECK_STATUS	=> array(
    1.92 +		RULE_ANSWERED		=> array('check0' => 'pm_replied', 'function' => '{CHECK0} == 1'),
    1.93 +		RULE_FORWARDED		=> array('check0' => 'pm_forwarded', 'function' => '{CHECK0} == 1'),
    1.94 +	),
    1.95 +
    1.96 +	CHECK_TO		=> array(
    1.97 +		RULE_TO_GROUP		=> array('check0' => 'to', 'check1' => 'bcc', 'check2' => 'user_in_group', 'function' => 'in_array("g_" . {CHECK2}, {CHECK0}) || in_array("g_" . {CHECK2}, {CHECK1})'),
    1.98 +		RULE_TO_ME			=> array('check0' => 'to', 'check1' => 'bcc', 'function' => 'in_array("u_" . $user_id, {CHECK0}) || in_array("u_" . $user_id, {CHECK1})'),
    1.99 +	)
   1.100 +);
   1.101 +
   1.102 +/**
   1.103 +* This is for defining which condition fields to show for which Rule
   1.104 +*/
   1.105 +$global_rule_conditions = array(
   1.106 +	RULE_IS_LIKE		=> 'text',
   1.107 +	RULE_IS_NOT_LIKE	=> 'text',
   1.108 +	RULE_IS				=> 'text',
   1.109 +	RULE_IS_NOT			=> 'text',
   1.110 +	RULE_BEGINS_WITH	=> 'text',
   1.111 +	RULE_ENDS_WITH		=> 'text',
   1.112 +	RULE_IS_USER		=> 'user',
   1.113 +	RULE_IS_GROUP		=> 'group'
   1.114 +);
   1.115 +
   1.116 +/**
   1.117 +* Get all folder
   1.118 +*/
   1.119 +function get_folder($user_id, $folder_id = false)
   1.120 +{
   1.121 +	global $db, $user, $template;
   1.122 +	global $phpbb_root_path, $phpEx;
   1.123 +
   1.124 +	$folder = array();
   1.125 +
   1.126 +	// Get folder information
   1.127 +	$sql = 'SELECT folder_id, COUNT(msg_id) as num_messages, SUM(pm_unread) as num_unread
   1.128 +		FROM ' . PRIVMSGS_TO_TABLE . "
   1.129 +		WHERE user_id = $user_id
   1.130 +			AND folder_id <> " . PRIVMSGS_NO_BOX . '
   1.131 +		GROUP BY folder_id';
   1.132 +	$result = $db->sql_query($sql);
   1.133 +
   1.134 +	$num_messages = $num_unread = array();
   1.135 +	while ($row = $db->sql_fetchrow($result))
   1.136 +	{
   1.137 +		$num_messages[(int) $row['folder_id']] = $row['num_messages'];
   1.138 +		$num_unread[(int) $row['folder_id']] = $row['num_unread'];
   1.139 +	}
   1.140 +	$db->sql_freeresult($result);
   1.141 +
   1.142 +	// Make sure the default boxes are defined
   1.143 +	$available_folder = array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX);
   1.144 +
   1.145 +	foreach ($available_folder as $default_folder)
   1.146 +	{
   1.147 +		if (!isset($num_messages[$default_folder]))
   1.148 +		{
   1.149 +			$num_messages[$default_folder] = 0;
   1.150 +		}
   1.151 +
   1.152 +		if (!isset($num_unread[$default_folder]))
   1.153 +		{
   1.154 +			$num_unread[$default_folder] = 0;
   1.155 +		}
   1.156 +	}
   1.157 +
   1.158 +	// Adjust unread status for outbox
   1.159 +	$num_unread[PRIVMSGS_OUTBOX] = $num_messages[PRIVMSGS_OUTBOX];
   1.160 +
   1.161 +	$folder[PRIVMSGS_INBOX] = array(
   1.162 +		'folder_name'		=> $user->lang['PM_INBOX'],
   1.163 +		'num_messages'		=> $num_messages[PRIVMSGS_INBOX],
   1.164 +		'unread_messages'	=> $num_unread[PRIVMSGS_INBOX]
   1.165 +	);
   1.166 +
   1.167 +	// Custom Folder
   1.168 +	$sql = 'SELECT folder_id, folder_name, pm_count
   1.169 +		FROM ' . PRIVMSGS_FOLDER_TABLE . "
   1.170 +			WHERE user_id = $user_id";
   1.171 +	$result = $db->sql_query($sql);
   1.172 +
   1.173 +	while ($row = $db->sql_fetchrow($result))
   1.174 +	{
   1.175 +		$folder[$row['folder_id']] = array(
   1.176 +			'folder_name'		=> $row['folder_name'],
   1.177 +			'num_messages'		=> $row['pm_count'],
   1.178 +			'unread_messages'	=> ((isset($num_unread[$row['folder_id']])) ? $num_unread[$row['folder_id']] : 0)
   1.179 +		);
   1.180 +	}
   1.181 +	$db->sql_freeresult($result);
   1.182 +
   1.183 +	$folder[PRIVMSGS_OUTBOX] = array(
   1.184 +		'folder_name'		=> $user->lang['PM_OUTBOX'],
   1.185 +		'num_messages'		=> $num_messages[PRIVMSGS_OUTBOX],
   1.186 +		'unread_messages'	=> $num_unread[PRIVMSGS_OUTBOX]
   1.187 +	);
   1.188 +
   1.189 +	$folder[PRIVMSGS_SENTBOX] = array(
   1.190 +		'folder_name'		=> $user->lang['PM_SENTBOX'],
   1.191 +		'num_messages'		=> $num_messages[PRIVMSGS_SENTBOX],
   1.192 +		'unread_messages'	=> $num_unread[PRIVMSGS_SENTBOX]
   1.193 +	);
   1.194 +
   1.195 +	// Define Folder Array for template designers (and for making custom folders usable by the template too)
   1.196 +	foreach ($folder as $f_id => $folder_ary)
   1.197 +	{
   1.198 +		$folder_id_name = ($f_id == PRIVMSGS_INBOX) ? 'inbox' : (($f_id == PRIVMSGS_OUTBOX) ? 'outbox' : 'sentbox');
   1.199 +
   1.200 +		$template->assign_block_vars('folder', array(
   1.201 +			'FOLDER_ID'			=> $f_id,
   1.202 +			'FOLDER_NAME'		=> $folder_ary['folder_name'],
   1.203 +			'NUM_MESSAGES'		=> $folder_ary['num_messages'],
   1.204 +			'UNREAD_MESSAGES'	=> $folder_ary['unread_messages'],
   1.205 +
   1.206 +			'U_FOLDER'			=> ($f_id > 0) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $f_id) : append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $folder_id_name),
   1.207 +
   1.208 +			'S_CUR_FOLDER'		=> ($f_id === $folder_id) ? true : false,
   1.209 +			'S_UNREAD_MESSAGES'	=> ($folder_ary['unread_messages']) ? true : false,
   1.210 +			'S_CUSTOM_FOLDER'	=> ($f_id > 0) ? true : false)
   1.211 +		);
   1.212 +	}
   1.213 +
   1.214 +	if ($folder_id !== false && !isset($folder[$folder_id]))
   1.215 +	{
   1.216 +		trigger_error('UNKNOWN_FOLDER');
   1.217 +	}
   1.218 +
   1.219 +	return $folder;
   1.220 +}
   1.221 +
   1.222 +/**
   1.223 +* Delete Messages From Sentbox
   1.224 +* we are doing this here because this saves us a bunch of checks and queries
   1.225 +*/
   1.226 +function clean_sentbox($num_sentbox_messages)
   1.227 +{
   1.228 +	global $db, $user, $config;
   1.229 +
   1.230 +	// Check Message Limit
   1.231 +	if ($user->data['message_limit'] && $num_sentbox_messages > $user->data['message_limit'])
   1.232 +	{
   1.233 +		// Delete old messages
   1.234 +		$sql = 'SELECT t.msg_id
   1.235 +			FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p
   1.236 +			WHERE t.msg_id = p.msg_id
   1.237 +				AND t.user_id = ' . $user->data['user_id'] . '
   1.238 +				AND t.folder_id = ' . PRIVMSGS_SENTBOX . '
   1.239 +			ORDER BY p.message_time ASC';
   1.240 +		$result = $db->sql_query_limit($sql, ($num_sentbox_messages - $user->data['message_limit']));
   1.241 +
   1.242 +		$delete_ids = array();
   1.243 +		while ($row = $db->sql_fetchrow($result))
   1.244 +		{
   1.245 +			$delete_ids[] = $row['msg_id'];
   1.246 +		}
   1.247 +		$db->sql_freeresult($result);
   1.248 +		delete_pm($user->data['user_id'], $delete_ids, PRIVMSGS_SENTBOX);
   1.249 +	}
   1.250 +}
   1.251 +
   1.252 +/**
   1.253 +* Check Rule against Message Information
   1.254 +*/
   1.255 +function check_rule(&$rules, &$rule_row, &$message_row, $user_id)
   1.256 +{
   1.257 +	global $user, $config;
   1.258 +
   1.259 +	if (!isset($rules[$rule_row['rule_check']][$rule_row['rule_connection']]))
   1.260 +	{
   1.261 +		return false;
   1.262 +	}
   1.263 +
   1.264 +	$check_ary = $rules[$rule_row['rule_check']][$rule_row['rule_connection']];
   1.265 +
   1.266 +	// Replace Check Literals
   1.267 +	$evaluate = $check_ary['function'];
   1.268 +	$evaluate = preg_replace('/{(CHECK[0-9])}/', '$message_row[$check_ary[strtolower("\1")]]', $evaluate);
   1.269 +
   1.270 +	// Replace Rule Literals
   1.271 +	$evaluate = preg_replace('/{(STRING|USER_ID|GROUP_ID)}/', '$rule_row["rule_" . strtolower("\1")]', $evaluate);
   1.272 +
   1.273 +	// Evil Statement
   1.274 +	$result = false;
   1.275 +	eval('$result = (' . $evaluate . ') ? true : false;');
   1.276 +
   1.277 +	if (!$result)
   1.278 +	{
   1.279 +		return false;
   1.280 +	}
   1.281 +
   1.282 +	switch ($rule_row['rule_action'])
   1.283 +	{
   1.284 +		case ACTION_PLACE_INTO_FOLDER:
   1.285 +			return array('action' => $rule_row['rule_action'], 'folder_id' => $rule_row['rule_folder_id']);
   1.286 +		break;
   1.287 +
   1.288 +		case ACTION_MARK_AS_READ:
   1.289 +		case ACTION_MARK_AS_IMPORTANT:
   1.290 +			return array('action' => $rule_row['rule_action'], 'pm_unread' => $message_row['pm_unread'], 'pm_marked' => $message_row['pm_marked']);
   1.291 +		break;
   1.292 +
   1.293 +		case ACTION_DELETE_MESSAGE:
   1.294 +			global $db, $auth;
   1.295 +
   1.296 +			// Check for admins/mods - users are not allowed to remove those messages...
   1.297 +			// We do the check here to make sure the data we use is consistent
   1.298 +			$sql = 'SELECT user_id, user_type, user_permissions
   1.299 +				FROM ' . USERS_TABLE . '
   1.300 +				WHERE user_id = ' . (int) $message_row['author_id'];
   1.301 +			$result = $db->sql_query($sql);
   1.302 +			$userdata = $db->sql_fetchrow($result);
   1.303 +			$db->sql_freeresult($result);
   1.304 +
   1.305 +			$auth2 = new auth();
   1.306 +			$auth2->acl($userdata);
   1.307 +
   1.308 +			if (!$auth2->acl_get('a_') && !$auth2->acl_get('m_') && !$auth2->acl_getf_global('m_'))
   1.309 +			{
   1.310 +				return array('action' => $rule_row['rule_action'], 'pm_unread' => $message_row['pm_unread'], 'pm_marked' => $message_row['pm_marked']);
   1.311 +			}
   1.312 +
   1.313 +			return false;
   1.314 +		break;
   1.315 +
   1.316 +		default:
   1.317 +			return false;
   1.318 +	}
   1.319 +
   1.320 +	return false;
   1.321 +}
   1.322 +
   1.323 +/**
   1.324 +* Update user PM count
   1.325 +*/
   1.326 +function update_pm_counts()
   1.327 +{
   1.328 +	global $user, $db;
   1.329 +
   1.330 +	// Update unread count
   1.331 +	$sql = 'SELECT COUNT(msg_id) as num_messages
   1.332 +		FROM ' . PRIVMSGS_TO_TABLE . '
   1.333 +		WHERE pm_unread = 1
   1.334 +			AND folder_id <> ' . PRIVMSGS_OUTBOX . '
   1.335 +			AND user_id = ' . $user->data['user_id'];
   1.336 +	$result = $db->sql_query($sql);
   1.337 +	$user->data['user_unread_privmsg'] = (int) $db->sql_fetchfield('num_messages');
   1.338 +	$db->sql_freeresult($result);
   1.339 +
   1.340 +	// Update new pm count
   1.341 +	$sql = 'SELECT COUNT(msg_id) as num_messages
   1.342 +		FROM ' . PRIVMSGS_TO_TABLE . '
   1.343 +		WHERE pm_new = 1
   1.344 +			AND folder_id IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ')
   1.345 +			AND user_id = ' . $user->data['user_id'];
   1.346 +	$result = $db->sql_query($sql);
   1.347 +	$user->data['user_new_privmsg'] = (int) $db->sql_fetchfield('num_messages');
   1.348 +	$db->sql_freeresult($result);
   1.349 +
   1.350 +	$db->sql_query('UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
   1.351 +		'user_unread_privmsg'	=> (int) $user->data['user_unread_privmsg'],
   1.352 +		'user_new_privmsg'		=> (int) $user->data['user_new_privmsg'],
   1.353 +	)) . ' WHERE user_id = ' . $user->data['user_id']);
   1.354 +
   1.355 +	// Ok, here we need to repair something, other boxes than privmsgs_no_box and privmsgs_hold_box should not carry the pm_new flag.
   1.356 +	if (!$user->data['user_new_privmsg'])
   1.357 +	{
   1.358 +		$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
   1.359 +			SET pm_new = 0
   1.360 +			WHERE pm_new = 1
   1.361 +				AND folder_id NOT IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ')
   1.362 +				AND user_id = ' . $user->data['user_id'];
   1.363 +		$db->sql_query($sql);
   1.364 +	}
   1.365 +}
   1.366 +
   1.367 +/**
   1.368 +* Place new messages into appropriate folder
   1.369 +*/
   1.370 +function place_pm_into_folder(&$global_privmsgs_rules, $release = false)
   1.371 +{
   1.372 +	global $db, $user, $config;
   1.373 +
   1.374 +	if (!$user->data['user_new_privmsg'])
   1.375 +	{
   1.376 +		return array('not_moved' => 0, 'removed' => 0);
   1.377 +	}
   1.378 +
   1.379 +	$user_message_rules = (int) $user->data['user_message_rules'];
   1.380 +	$user_id = (int) $user->data['user_id'];
   1.381 +
   1.382 +	$action_ary = $move_into_folder = array();
   1.383 +	$num_removed = 0;
   1.384 +
   1.385 +	// Newly processing on-hold messages
   1.386 +	if ($release)
   1.387 +	{
   1.388 +		$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
   1.389 +			SET folder_id = ' . PRIVMSGS_NO_BOX . '
   1.390 +			WHERE folder_id = ' . PRIVMSGS_HOLD_BOX . "
   1.391 +				AND user_id = $user_id";
   1.392 +		$db->sql_query($sql);
   1.393 +	}
   1.394 +
   1.395 +	// Get those messages not yet placed into any box
   1.396 +	$retrieve_sql = 'SELECT t.*, p.*, u.username, u.user_id, u.group_id
   1.397 +		FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . " u
   1.398 +		WHERE t.user_id = $user_id
   1.399 +			AND p.author_id = u.user_id
   1.400 +			AND t.folder_id = " . PRIVMSGS_NO_BOX . '
   1.401 +			AND t.msg_id = p.msg_id';
   1.402 +
   1.403 +	// Just place into the appropriate arrays if no rules need to be checked
   1.404 +	if (!$user_message_rules)
   1.405 +	{
   1.406 +		$result = $db->sql_query($retrieve_sql);
   1.407 +
   1.408 +		while ($row = $db->sql_fetchrow($result))
   1.409 +		{
   1.410 +			$action_ary[$row['msg_id']][] = array('action' => false);
   1.411 +		}
   1.412 +		$db->sql_freeresult($result);
   1.413 +	}
   1.414 +	else
   1.415 +	{
   1.416 +		$user_rules = $zebra = $check_rows = array();
   1.417 +		$user_ids = $memberships = array();
   1.418 +
   1.419 +		// First of all, grab all rules and retrieve friends/foes
   1.420 +		$sql = 'SELECT *
   1.421 +			FROM ' . PRIVMSGS_RULES_TABLE . "
   1.422 +			WHERE user_id = $user_id";
   1.423 +		$result = $db->sql_query($sql);
   1.424 +		$user_rules = $db->sql_fetchrowset($result);
   1.425 +		$db->sql_freeresult($result);
   1.426 +
   1.427 +		if (sizeof($user_rules))
   1.428 +		{
   1.429 +			$sql = 'SELECT zebra_id, friend, foe
   1.430 +				FROM ' . ZEBRA_TABLE . "
   1.431 +				WHERE user_id = $user_id";
   1.432 +			$result = $db->sql_query($sql);
   1.433 +
   1.434 +			while ($row = $db->sql_fetchrow($result))
   1.435 +			{
   1.436 +				$zebra[$row['zebra_id']] = $row;
   1.437 +			}
   1.438 +			$db->sql_freeresult($result);
   1.439 +		}
   1.440 +
   1.441 +		// Now build a bare-bone check_row array
   1.442 +		$result = $db->sql_query($retrieve_sql);
   1.443 +
   1.444 +		while ($row = $db->sql_fetchrow($result))
   1.445 +		{
   1.446 +			$check_rows[] = array_merge($row, array(
   1.447 +				'to'				=> explode(':', $row['to_address']),
   1.448 +				'bcc'				=> explode(':', $row['bcc_address']),
   1.449 +				'friend'			=> (isset($zebra[$row['author_id']])) ? $zebra[$row['author_id']]['friend'] : 0,
   1.450 +				'foe'				=> (isset($zebra[$row['author_id']])) ? $zebra[$row['author_id']]['foe'] : 0,
   1.451 +				'user_in_group'		=> array($user->data['group_id']),
   1.452 +				'author_in_group'	=> array())
   1.453 +			);
   1.454 +
   1.455 +			$user_ids[] = $row['user_id'];
   1.456 +		}
   1.457 +		$db->sql_freeresult($result);
   1.458 +
   1.459 +		// Retrieve user memberships
   1.460 +		if (sizeof($user_ids))
   1.461 +		{
   1.462 +			$sql = 'SELECT *
   1.463 +				FROM ' . USER_GROUP_TABLE . '
   1.464 +				WHERE ' . $db->sql_in_set('user_id', $user_ids) . '
   1.465 +					AND user_pending = 0';
   1.466 +			$result = $db->sql_query($sql);
   1.467 +
   1.468 +			while ($row = $db->sql_fetchrow($result))
   1.469 +			{
   1.470 +				$memberships[$row['user_id']][] = $row['group_id'];
   1.471 +			}
   1.472 +			$db->sql_freeresult($result);
   1.473 +		}
   1.474 +
   1.475 +		// Now place into the appropriate folder
   1.476 +		foreach ($check_rows as $row)
   1.477 +		{
   1.478 +			// Add membership if set
   1.479 +			if (isset($memberships[$row['author_id']]))
   1.480 +			{
   1.481 +				$row['author_in_group'] = $memberships[$row['user_id']];
   1.482 +			}
   1.483 +
   1.484 +			// Check Rule - this should be very quick since we have all information we need
   1.485 +			$is_match = false;
   1.486 +			foreach ($user_rules as $rule_row)
   1.487 +			{
   1.488 +				if (($action = check_rule($global_privmsgs_rules, $rule_row, $row, $user_id)) !== false)
   1.489 +				{
   1.490 +					$is_match = true;
   1.491 +					$action_ary[$row['msg_id']][] = $action;
   1.492 +				}
   1.493 +			}
   1.494 +
   1.495 +			if (!$is_match)
   1.496 +			{
   1.497 +				$action_ary[$row['msg_id']][] = array('action' => false);
   1.498 +			}
   1.499 +		}
   1.500 +
   1.501 +		unset($user_rules, $zebra, $check_rows, $user_ids, $memberships);
   1.502 +	}
   1.503 +
   1.504 +	// We place actions into arrays, to save queries.
   1.505 +	$sql = $unread_ids = $delete_ids = $important_ids = array();
   1.506 +
   1.507 +	foreach ($action_ary as $msg_id => $msg_ary)
   1.508 +	{
   1.509 +		// It is allowed to execute actions more than once, except placing messages into folder
   1.510 +		$folder_action = $message_removed = false;
   1.511 +
   1.512 +		foreach ($msg_ary as $pos => $rule_ary)
   1.513 +		{
   1.514 +			if ($folder_action && $rule_ary['action'] == ACTION_PLACE_INTO_FOLDER)
   1.515 +			{
   1.516 +				continue;
   1.517 +			}
   1.518 +
   1.519 +			switch ($rule_ary['action'])
   1.520 +			{
   1.521 +				case ACTION_PLACE_INTO_FOLDER:
   1.522 +					// Folder actions have precedence, so we will remove any other ones
   1.523 +					$folder_action = true;
   1.524 +					$move_into_folder[(int) $rule_ary['folder_id']][] = $msg_id;
   1.525 +				break;
   1.526 +
   1.527 +				case ACTION_MARK_AS_READ:
   1.528 +					if ($rule_ary['pm_unread'])
   1.529 +					{
   1.530 +						$unread_ids[] = $msg_id;
   1.531 +					}
   1.532 +				break;
   1.533 +
   1.534 +				case ACTION_DELETE_MESSAGE:
   1.535 +					$delete_ids[] = $msg_id;
   1.536 +					$message_removed = true;
   1.537 +				break;
   1.538 +
   1.539 +				case ACTION_MARK_AS_IMPORTANT:
   1.540 +					if (!$rule_ary['pm_marked'])
   1.541 +					{
   1.542 +						$important_ids[] = $msg_id;
   1.543 +					}
   1.544 +				break;
   1.545 +			}
   1.546 +		}
   1.547 +
   1.548 +		// We place this here because it could happen that the messages are doubled if a rule marks a message and then moves it into a specific
   1.549 +		// folder. Here we simply move the message into the INBOX if it gets not removed and also not put into a custom folder.
   1.550 +		if (!$folder_action && !$message_removed)
   1.551 +		{
   1.552 +			$move_into_folder[PRIVMSGS_INBOX][] = $msg_id;
   1.553 +		}
   1.554 +	}
   1.555 +
   1.556 +	// Do not change the order of processing
   1.557 +	// The number of queries needed to be executed here highly depends on the defined rules and are
   1.558 +	// only gone through if new messages arrive.
   1.559 +
   1.560 +	// Delete messages
   1.561 +	if (sizeof($delete_ids))
   1.562 +	{
   1.563 +		$num_removed += sizeof($delete_ids);
   1.564 +		delete_pm($user_id, $delete_ids, PRIVMSGS_NO_BOX);
   1.565 +	}
   1.566 +
   1.567 +	// Set messages to Unread
   1.568 +	if (sizeof($unread_ids))
   1.569 +	{
   1.570 +		$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
   1.571 +			SET pm_unread = 0
   1.572 +			WHERE ' . $db->sql_in_set('msg_id', $unread_ids) . "
   1.573 +				AND user_id = $user_id
   1.574 +				AND folder_id = " . PRIVMSGS_NO_BOX;
   1.575 +		$db->sql_query($sql);
   1.576 +	}
   1.577 +
   1.578 +	// mark messages as important
   1.579 +	if (sizeof($important_ids))
   1.580 +	{
   1.581 +		$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
   1.582 +			SET pm_marked = 1 - pm_marked
   1.583 +			WHERE folder_id = ' . PRIVMSGS_NO_BOX . "
   1.584 +				AND user_id = $user_id
   1.585 +				AND " . $db->sql_in_set('msg_id', $important_ids);
   1.586 +		$db->sql_query($sql);
   1.587 +	}
   1.588 +
   1.589 +	// Move into folder
   1.590 +	$folder = array();
   1.591 +
   1.592 +	if (sizeof($move_into_folder))
   1.593 +	{
   1.594 +		// Determine Full Folder Action - we need the move to folder id later eventually
   1.595 +		$full_folder_action = ($user->data['user_full_folder'] == FULL_FOLDER_NONE) ? ($config['full_folder_action'] - (FULL_FOLDER_NONE*(-1))) : $user->data['user_full_folder'];
   1.596 +
   1.597 +		$sql_folder = array_keys($move_into_folder);
   1.598 +		if ($full_folder_action >= 0)
   1.599 +		{
   1.600 +			$sql_folder[] = $full_folder_action;
   1.601 +		}
   1.602 +
   1.603 +		$sql = 'SELECT folder_id, pm_count
   1.604 +			FROM ' . PRIVMSGS_FOLDER_TABLE . '
   1.605 +			WHERE ' . $db->sql_in_set('folder_id', $sql_folder) . "
   1.606 +				AND user_id = $user_id";
   1.607 +		$result = $db->sql_query($sql);
   1.608 +
   1.609 +		while ($row = $db->sql_fetchrow($result))
   1.610 +		{
   1.611 +			$folder[(int) $row['folder_id']] = (int) $row['pm_count'];
   1.612 +		}
   1.613 +		$db->sql_freeresult($result);
   1.614 +
   1.615 +		unset($sql_folder);
   1.616 +
   1.617 +		if (isset($move_into_folder[PRIVMSGS_INBOX]))
   1.618 +		{
   1.619 +			$sql = 'SELECT COUNT(msg_id) as num_messages
   1.620 +				FROM ' . PRIVMSGS_TO_TABLE . "
   1.621 +				WHERE user_id = $user_id
   1.622 +					AND folder_id = " . PRIVMSGS_INBOX;
   1.623 +			$result = $db->sql_query($sql);
   1.624 +			$folder[PRIVMSGS_INBOX] = (int) $db->sql_fetchfield('num_messages');
   1.625 +			$db->sql_freeresult($result);
   1.626 +		}
   1.627 +	}
   1.628 +
   1.629 +	// Here we have ideally only one folder to move into
   1.630 +	foreach ($move_into_folder as $folder_id => $msg_ary)
   1.631 +	{
   1.632 +		$dest_folder = $folder_id;
   1.633 +		$full_folder_action = FULL_FOLDER_NONE;
   1.634 +
   1.635 +		// Check Message Limit - we calculate with the complete array, most of the time it is one message
   1.636 +		// But we are making sure that the other way around works too (more messages in queue than allowed to be stored)
   1.637 +		if ($user->data['message_limit'] && $folder[$folder_id] && ($folder[$folder_id] + sizeof($msg_ary)) > $user->data['message_limit'])
   1.638 +		{
   1.639 +			$full_folder_action = ($user->data['user_full_folder'] == FULL_FOLDER_NONE) ? ($config['full_folder_action'] - (FULL_FOLDER_NONE*(-1))) : $user->data['user_full_folder'];
   1.640 +
   1.641 +			// If destination folder itself is full...
   1.642 +			if ($full_folder_action >= 0 && ($folder[$full_folder_action] + sizeof($msg_ary)) > $user->data['message_limit'])
   1.643 +			{
   1.644 +				$full_folder_action = $config['full_folder_action'] - (FULL_FOLDER_NONE*(-1));
   1.645 +			}
   1.646 +
   1.647 +			// If Full Folder Action is to move to another folder, we simply adjust the destination folder
   1.648 +			if ($full_folder_action >= 0)
   1.649 +			{
   1.650 +				$dest_folder = $full_folder_action;
   1.651 +			}
   1.652 +			else if ($full_folder_action == FULL_FOLDER_DELETE)
   1.653 +			{
   1.654 +				// Delete some messages. NOTE: Ordered by msg_id here instead of message_time!
   1.655 +				$sql = 'SELECT msg_id
   1.656 +					FROM ' . PRIVMSGS_TO_TABLE . "
   1.657 +					WHERE user_id = $user_id
   1.658 +						AND folder_id = $dest_folder
   1.659 +					ORDER BY msg_id ASC";
   1.660 +				$result = $db->sql_query_limit($sql, (($folder[$dest_folder] + sizeof($msg_ary)) - $user->data['message_limit']));
   1.661 +
   1.662 +				$delete_ids = array();
   1.663 +				while ($row = $db->sql_fetchrow($result))
   1.664 +				{
   1.665 +					$delete_ids[] = $row['msg_id'];
   1.666 +				}
   1.667 +				$db->sql_freeresult($result);
   1.668 +
   1.669 +				$num_removed += sizeof($delete_ids);
   1.670 +				delete_pm($user_id, $delete_ids, $dest_folder);
   1.671 +			}
   1.672 +		}
   1.673 +
   1.674 +		//
   1.675 +		if ($full_folder_action == FULL_FOLDER_HOLD)
   1.676 +		{
   1.677 +			$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
   1.678 +				SET folder_id = ' . PRIVMSGS_HOLD_BOX . '
   1.679 +				WHERE folder_id = ' . PRIVMSGS_NO_BOX . "
   1.680 +					AND user_id = $user_id
   1.681 +					AND " . $db->sql_in_set('msg_id', $msg_ary);
   1.682 +			$db->sql_query($sql);
   1.683 +		}
   1.684 +		else
   1.685 +		{
   1.686 +			$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
   1.687 +				SET folder_id = $dest_folder, pm_new = 0
   1.688 +				WHERE folder_id = " . PRIVMSGS_NO_BOX . "
   1.689 +					AND user_id = $user_id
   1.690 +					AND pm_new = 1
   1.691 +					AND " . $db->sql_in_set('msg_id', $msg_ary);
   1.692 +			$db->sql_query($sql);
   1.693 +
   1.694 +			if ($dest_folder != PRIVMSGS_INBOX)
   1.695 +			{
   1.696 +				$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . '
   1.697 +					SET pm_count = pm_count + ' . (int) $db->sql_affectedrows() . "
   1.698 +					WHERE folder_id = $dest_folder
   1.699 +						AND user_id = $user_id";
   1.700 +				$db->sql_query($sql);
   1.701 +			}
   1.702 +		}
   1.703 +	}
   1.704 +
   1.705 +	if (sizeof($action_ary))
   1.706 +	{
   1.707 +		// Move from OUTBOX to SENTBOX
   1.708 +		// We are not checking any full folder status here... SENTBOX is a special treatment (old messages get deleted)
   1.709 +		$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
   1.710 +			SET folder_id = ' . PRIVMSGS_SENTBOX . '
   1.711 +			WHERE folder_id = ' . PRIVMSGS_OUTBOX . '
   1.712 +				AND ' . $db->sql_in_set('msg_id', array_keys($action_ary));
   1.713 +		$db->sql_query($sql);
   1.714 +	}
   1.715 +
   1.716 +	// Update new/unread count
   1.717 +	update_pm_counts();
   1.718 +
   1.719 +	// Now check how many messages got not moved...
   1.720 +	$sql = 'SELECT COUNT(msg_id) as num_messages
   1.721 +		FROM ' . PRIVMSGS_TO_TABLE . "
   1.722 +		WHERE user_id = $user_id
   1.723 +			AND folder_id = " . PRIVMSGS_HOLD_BOX;
   1.724 +	$result = $db->sql_query($sql);
   1.725 +	$num_not_moved = (int) $db->sql_fetchfield('num_messages');
   1.726 +	$db->sql_freeresult($result);
   1.727 +
   1.728 +	return array('not_moved' => $num_not_moved, 'removed' => $num_removed);
   1.729 +}
   1.730 +
   1.731 +/**
   1.732 +* Move PM from one to another folder
   1.733 +*/
   1.734 +function move_pm($user_id, $message_limit, $move_msg_ids, $dest_folder, $cur_folder_id)
   1.735 +{
   1.736 +	global $db, $user;
   1.737 +	global $phpbb_root_path, $phpEx;
   1.738 +
   1.739 +	$num_moved = 0;
   1.740 +
   1.741 +	if (!is_array($move_msg_ids))
   1.742 +	{
   1.743 +		$move_msg_ids = array($move_msg_ids);
   1.744 +	}
   1.745 +
   1.746 +	if (sizeof($move_msg_ids) && !in_array($dest_folder, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX)) &&
   1.747 +		!in_array($cur_folder_id, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX)) && $cur_folder_id != $dest_folder)
   1.748 +	{
   1.749 +		// We have to check the destination folder ;)
   1.750 +		if ($dest_folder != PRIVMSGS_INBOX)
   1.751 +		{
   1.752 +			$sql = 'SELECT folder_id, folder_name, pm_count
   1.753 +				FROM ' . PRIVMSGS_FOLDER_TABLE . "
   1.754 +				WHERE folder_id = $dest_folder
   1.755 +					AND user_id = $user_id";
   1.756 +			$result = $db->sql_query($sql);
   1.757 +			$row = $db->sql_fetchrow($result);
   1.758 +			$db->sql_freeresult($result);
   1.759 +
   1.760 +			if (!$row)
   1.761 +			{
   1.762 +				trigger_error('NOT_AUTHORISED');
   1.763 +			}
   1.764 +
   1.765 +			if ($message_limit && $row['pm_count'] + sizeof($move_msg_ids) > $message_limit)
   1.766 +			{
   1.767 +				$message = sprintf($user->lang['NOT_ENOUGH_SPACE_FOLDER'], $row['folder_name']) . '<br /><br />';
   1.768 +				$message .= sprintf($user->lang['CLICK_RETURN_FOLDER'], '<a href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $row['folder_id']) . '">', '</a>', $row['folder_name']);
   1.769 +				trigger_error($message);
   1.770 +			}
   1.771 +		}
   1.772 +		else
   1.773 +		{
   1.774 +			$sql = 'SELECT COUNT(msg_id) as num_messages
   1.775 +				FROM ' . PRIVMSGS_TO_TABLE . '
   1.776 +				WHERE folder_id = ' . PRIVMSGS_INBOX . "
   1.777 +					AND user_id = $user_id";
   1.778 +			$result = $db->sql_query($sql);
   1.779 +			$num_messages = (int) $db->sql_fetchfield('num_messages');
   1.780 +			$db->sql_freeresult($result);
   1.781 +
   1.782 +			if ($message_limit && $num_messages + sizeof($move_msg_ids) > $message_limit)
   1.783 +			{
   1.784 +				$message = sprintf($user->lang['NOT_ENOUGH_SPACE_FOLDER'], $user->lang['PM_INBOX']) . '<br /><br />';
   1.785 +				$message .= sprintf($user->lang['CLICK_RETURN_FOLDER'], '<a href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=inbox') . '">', '</a>', $user->lang['PM_INBOX']);
   1.786 +				trigger_error($message);
   1.787 +			}
   1.788 +		}
   1.789 +
   1.790 +		$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
   1.791 +			SET folder_id = $dest_folder
   1.792 +			WHERE folder_id = $cur_folder_id
   1.793 +				AND user_id = $user_id
   1.794 +				AND " . $db->sql_in_set('msg_id', $move_msg_ids);
   1.795 +		$db->sql_query($sql);
   1.796 +		$num_moved = $db->sql_affectedrows();
   1.797 +
   1.798 +		// Update pm counts
   1.799 +		if ($num_moved)
   1.800 +		{
   1.801 +			if (!in_array($cur_folder_id, array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX)))
   1.802 +			{
   1.803 +				$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
   1.804 +					SET pm_count = pm_count - $num_moved
   1.805 +					WHERE folder_id = $cur_folder_id
   1.806 +						AND user_id = $user_id";
   1.807 +				$db->sql_query($sql);
   1.808 +			}
   1.809 +
   1.810 +			if ($dest_folder != PRIVMSGS_INBOX)
   1.811 +			{
   1.812 +				$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
   1.813 +					SET pm_count = pm_count + $num_moved
   1.814 +					WHERE folder_id = $dest_folder
   1.815 +						AND user_id = $user_id";
   1.816 +				$db->sql_query($sql);
   1.817 +			}
   1.818 +		}
   1.819 +	}
   1.820 +	else if (in_array($cur_folder_id, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX)))
   1.821 +	{
   1.822 +		trigger_error('CANNOT_MOVE_SPECIAL');
   1.823 +	}
   1.824 +
   1.825 +	return $num_moved;
   1.826 +}
   1.827 +
   1.828 +/**
   1.829 +* Update unread message status
   1.830 +*/
   1.831 +function update_unread_status($unread, $msg_id, $user_id, $folder_id)
   1.832 +{
   1.833 +	if (!$unread)
   1.834 +	{
   1.835 +		return;
   1.836 +	}
   1.837 +
   1.838 +	global $db, $user;
   1.839 +
   1.840 +	$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
   1.841 +		SET pm_unread = 0
   1.842 +		WHERE msg_id = $msg_id
   1.843 +			AND user_id = $user_id
   1.844 +			AND folder_id = $folder_id";
   1.845 +	$db->sql_query($sql);
   1.846 +
   1.847 +	$sql = 'UPDATE ' . USERS_TABLE . "
   1.848 +		SET user_unread_privmsg = user_unread_privmsg - 1
   1.849 +		WHERE user_id = $user_id";
   1.850 +	$db->sql_query($sql);
   1.851 +
   1.852 +	if ($user->data['user_id'] == $user_id)
   1.853 +	{
   1.854 +		$user->data['user_unread_privmsg']--;
   1.855 +
   1.856 +		// Try to cope with previous wrong conversions...
   1.857 +		if ($user->data['user_unread_privmsg'] < 0)
   1.858 +		{
   1.859 +			$sql = 'UPDATE ' . USERS_TABLE . "
   1.860 +				SET user_unread_privmsg = 0
   1.861 +				WHERE user_id = $user_id";
   1.862 +			$db->sql_query($sql);
   1.863 +
   1.864 +			$user->data['user_unread_privmsg'] = 0;
   1.865 +		}
   1.866 +	}
   1.867 +}
   1.868 +
   1.869 +/**
   1.870 +* Handle all actions possible with marked messages
   1.871 +*/
   1.872 +function handle_mark_actions($user_id, $mark_action)
   1.873 +{
   1.874 +	global $db, $user, $phpbb_root_path, $phpEx;
   1.875 +
   1.876 +	$msg_ids		= request_var('marked_msg_id', array(0));
   1.877 +	$cur_folder_id	= request_var('cur_folder_id', PRIVMSGS_NO_BOX);
   1.878 +	$confirm		= (isset($_POST['confirm'])) ? true : false;
   1.879 +
   1.880 +	if (!sizeof($msg_ids))
   1.881 +	{
   1.882 +		return false;
   1.883 +	}
   1.884 +
   1.885 +	switch ($mark_action)
   1.886 +	{
   1.887 +		case 'mark_important':
   1.888 +
   1.889 +			$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . "
   1.890 +				SET pm_marked = 1 - pm_marked
   1.891 +				WHERE folder_id = $cur_folder_id
   1.892 +					AND user_id = $user_id
   1.893 +					AND " . $db->sql_in_set('msg_id', $msg_ids);
   1.894 +			$db->sql_query($sql);
   1.895 +
   1.896 +		break;
   1.897 +
   1.898 +		case 'delete_marked':
   1.899 +
   1.900 +			global $auth;
   1.901 +
   1.902 +			if (!$auth->acl_get('u_pm_delete'))
   1.903 +			{
   1.904 +				trigger_error('NO_AUTH_DELETE_MESSAGE');
   1.905 +			}
   1.906 +
   1.907 +			if (confirm_box(true))
   1.908 +			{
   1.909 +				delete_pm($user_id, $msg_ids, $cur_folder_id);
   1.910 +
   1.911 +				$success_msg = (sizeof($msg_ids) == 1) ? 'MESSAGE_DELETED' : 'MESSAGES_DELETED';
   1.912 +				$redirect = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $cur_folder_id);
   1.913 +
   1.914 +				meta_refresh(3, $redirect);
   1.915 +				trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_FOLDER'], '<a href="' . $redirect . '">', '</a>'));
   1.916 +			}
   1.917 +			else
   1.918 +			{
   1.919 +				$s_hidden_fields = array(
   1.920 +					'cur_folder_id'	=> $cur_folder_id,
   1.921 +					'mark_option'	=> 'delete_marked',
   1.922 +					'submit_mark'	=> true,
   1.923 +					'marked_msg_id'	=> $msg_ids
   1.924 +				);
   1.925 +
   1.926 +				confirm_box(false, 'DELETE_MARKED_PM', build_hidden_fields($s_hidden_fields));
   1.927 +			}
   1.928 +
   1.929 +		break;
   1.930 +
   1.931 +		default:
   1.932 +			return false;
   1.933 +	}
   1.934 +
   1.935 +	return true;
   1.936 +}
   1.937 +
   1.938 +/**
   1.939 +* Delete PM(s)
   1.940 +*/
   1.941 +function delete_pm($user_id, $msg_ids, $folder_id)
   1.942 +{
   1.943 +	global $db, $user, $phpbb_root_path, $phpEx;
   1.944 +
   1.945 +	$user_id	= (int) $user_id;
   1.946 +	$folder_id	= (int) $folder_id;
   1.947 +
   1.948 +	if (!$user_id)
   1.949 +	{
   1.950 +		return false;
   1.951 +	}
   1.952 +
   1.953 +	if (!is_array($msg_ids))
   1.954 +	{
   1.955 +		if (!$msg_ids)
   1.956 +		{
   1.957 +			return false;
   1.958 +		}
   1.959 +		$msg_ids = array($msg_ids);
   1.960 +	}
   1.961 +
   1.962 +	if (!sizeof($msg_ids))
   1.963 +	{
   1.964 +		return false;
   1.965 +	}
   1.966 +
   1.967 +	// Get PM Information for later deleting
   1.968 +	$sql = 'SELECT msg_id, pm_unread, pm_new
   1.969 +		FROM ' . PRIVMSGS_TO_TABLE . '
   1.970 +		WHERE ' . $db->sql_in_set('msg_id', array_map('intval', $msg_ids)) . "
   1.971 +			AND folder_id = $folder_id
   1.972 +			AND user_id = $user_id";
   1.973 +	$result = $db->sql_query($sql);
   1.974 +
   1.975 +	$delete_rows = array();
   1.976 +	$num_unread = $num_new = $num_deleted = 0;
   1.977 +	while ($row = $db->sql_fetchrow($result))
   1.978 +	{
   1.979 +		$num_unread += (int) $row['pm_unread'];
   1.980 +		$num_new += (int) $row['pm_new'];
   1.981 +
   1.982 +		$delete_rows[$row['msg_id']] = 1;
   1.983 +	}
   1.984 +	$db->sql_freeresult($result);
   1.985 +	unset($msg_ids);
   1.986 +
   1.987 +	if (!sizeof($delete_rows))
   1.988 +	{
   1.989 +		return false;
   1.990 +	}
   1.991 +
   1.992 +	$db->sql_transaction('begin');
   1.993 +
   1.994 +	// if no one has read the message yet (meaning it is in users outbox)
   1.995 +	// then mark the message as deleted...
   1.996 +	if ($folder_id == PRIVMSGS_OUTBOX)
   1.997 +	{
   1.998 +		// Remove PM from Outbox
   1.999 +		$sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . "
  1.1000 +			WHERE user_id = $user_id AND folder_id = " . PRIVMSGS_OUTBOX . '
  1.1001 +				AND ' . $db->sql_in_set('msg_id', array_keys($delete_rows));
  1.1002 +		$db->sql_query($sql);
  1.1003 +
  1.1004 +		// Update PM Information for safety
  1.1005 +		$sql = 'UPDATE ' . PRIVMSGS_TABLE . " SET message_text = ''
  1.1006 +			WHERE " . $db->sql_in_set('msg_id', array_keys($delete_rows));
  1.1007 +		$db->sql_query($sql);
  1.1008 +
  1.1009 +		// Set delete flag for those intended to receive the PM
  1.1010 +		// We do not remove the message actually, to retain some basic information (sent time for example)
  1.1011 +		$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  1.1012 +			SET pm_deleted = 1
  1.1013 +			WHERE ' . $db->sql_in_set('msg_id', array_keys($delete_rows));
  1.1014 +		$db->sql_query($sql);
  1.1015 +
  1.1016 +		$num_deleted = $db->sql_affectedrows();
  1.1017 +	}
  1.1018 +	else
  1.1019 +	{
  1.1020 +		// Delete private message data
  1.1021 +		$sql = 'DELETE FROM ' . PRIVMSGS_TO_TABLE . "
  1.1022 +			WHERE user_id = $user_id
  1.1023 +				AND folder_id = $folder_id
  1.1024 +				AND " . $db->sql_in_set('msg_id', array_keys($delete_rows));
  1.1025 +		$db->sql_query($sql);
  1.1026 +		$num_deleted = $db->sql_affectedrows();
  1.1027 +	}
  1.1028 +
  1.1029 +	// if folder id is user defined folder then decrease pm_count
  1.1030 +	if (!in_array($folder_id, array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX, PRIVMSGS_NO_BOX)))
  1.1031 +	{
  1.1032 +		$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
  1.1033 +			SET pm_count = pm_count - $num_deleted
  1.1034 +			WHERE folder_id = $folder_id";
  1.1035 +		$db->sql_query($sql);
  1.1036 +	}
  1.1037 +
  1.1038 +	// Update unread and new status field
  1.1039 +	if ($num_unread || $num_new)
  1.1040 +	{
  1.1041 +		$set_sql = ($num_unread) ? 'user_unread_privmsg = user_unread_privmsg - ' . $num_unread : '';
  1.1042 +
  1.1043 +		if ($num_new)
  1.1044 +		{
  1.1045 +			$set_sql .= ($set_sql != '') ? ', ' : '';
  1.1046 +			$set_sql .= 'user_new_privmsg = user_new_privmsg - ' . $num_new;
  1.1047 +		}
  1.1048 +
  1.1049 +		$db->sql_query('UPDATE ' . USERS_TABLE . " SET $set_sql WHERE user_id = $user_id");
  1.1050 +
  1.1051 +		$user->data['user_new_privmsg'] -= $num_new;
  1.1052 +		$user->data['user_unread_privmsg'] -= $num_unread;
  1.1053 +	}
  1.1054 +
  1.1055 +	// Now we have to check which messages we can delete completely
  1.1056 +	$sql = 'SELECT msg_id
  1.1057 +		FROM ' . PRIVMSGS_TO_TABLE . '
  1.1058 +		WHERE ' . $db->sql_in_set('msg_id', array_keys($delete_rows));
  1.1059 +	$result = $db->sql_query($sql);
  1.1060 +
  1.1061 +	while ($row = $db->sql_fetchrow($result))
  1.1062 +	{
  1.1063 +		unset($delete_rows[$row['msg_id']]);
  1.1064 +	}
  1.1065 +	$db->sql_freeresult($result);
  1.1066 +
  1.1067 +	$delete_ids = array_keys($delete_rows);
  1.1068 +
  1.1069 +	if (sizeof($delete_ids))
  1.1070 +	{
  1.1071 +		// Check if there are any attachments we need to remove
  1.1072 +		if (!function_exists('delete_attachments'))
  1.1073 +		{
  1.1074 +			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1.1075 +		}
  1.1076 +
  1.1077 +		delete_attachments('message', $delete_ids, false);
  1.1078 +
  1.1079 +		$sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
  1.1080 +			WHERE ' . $db->sql_in_set('msg_id', $delete_ids);
  1.1081 +		$db->sql_query($sql);
  1.1082 +	}
  1.1083 +
  1.1084 +	$db->sql_transaction('commit');
  1.1085 +
  1.1086 +	return true;
  1.1087 +}
  1.1088 +
  1.1089 +/**
  1.1090 +* Rebuild message header
  1.1091 +*/
  1.1092 +function rebuild_header($check_ary)
  1.1093 +{
  1.1094 +	global $db;
  1.1095 +
  1.1096 +	$address = array();
  1.1097 +
  1.1098 +	foreach ($check_ary as $check_type => $address_field)
  1.1099 +	{
  1.1100 +		// Split Addresses into users and groups
  1.1101 +		preg_match_all('/:?(u|g)_([0-9]+):?/', $address_field, $match);
  1.1102 +
  1.1103 +		$u = $g = array();
  1.1104 +		foreach ($match[1] as $id => $type)
  1.1105 +		{
  1.1106 +			${$type}[] = (int) $match[2][$id];
  1.1107 +		}
  1.1108 +
  1.1109 +		$_types = array('u', 'g');
  1.1110 +		foreach ($_types as $type)
  1.1111 +		{
  1.1112 +			if (sizeof($$type))
  1.1113 +			{
  1.1114 +				foreach ($$type as $id)
  1.1115 +				{
  1.1116 +					$address[$type][$id] = $check_type;
  1.1117 +				}
  1.1118 +			}
  1.1119 +		}
  1.1120 +	}
  1.1121 +
  1.1122 +	return $address;
  1.1123 +}
  1.1124 +
  1.1125 +/**
  1.1126 +* Print out/assign recipient information
  1.1127 +*/
  1.1128 +function write_pm_addresses($check_ary, $author_id, $plaintext = false)
  1.1129 +{
  1.1130 +	global $db, $user, $template, $phpbb_root_path, $phpEx;
  1.1131 +
  1.1132 +	$addresses = array();
  1.1133 +
  1.1134 +	foreach ($check_ary as $check_type => $address_field)
  1.1135 +	{
  1.1136 +		if (!is_array($address_field))
  1.1137 +		{
  1.1138 +			// Split Addresses into users and groups
  1.1139 +			preg_match_all('/:?(u|g)_([0-9]+):?/', $address_field, $match);
  1.1140 +
  1.1141 +			$u = $g = array();
  1.1142 +			foreach ($match[1] as $id => $type)
  1.1143 +			{
  1.1144 +				${$type}[] = (int) $match[2][$id];
  1.1145 +			}
  1.1146 +		}
  1.1147 +		else
  1.1148 +		{
  1.1149 +			$u = $address_field['u'];
  1.1150 +			$g = $address_field['g'];
  1.1151 +		}
  1.1152 +
  1.1153 +		$address = array();
  1.1154 +		if (sizeof($u))
  1.1155 +		{
  1.1156 +			$sql = 'SELECT user_id, username, user_colour
  1.1157 +				FROM ' . USERS_TABLE . '
  1.1158 +				WHERE ' . $db->sql_in_set('user_id', $u);
  1.1159 +			$result = $db->sql_query($sql);
  1.1160 +
  1.1161 +			while ($row = $db->sql_fetchrow($result))
  1.1162 +			{
  1.1163 +				if ($check_type == 'to' || $author_id == $user->data['user_id'] || $row['user_id'] == $user->data['user_id'])
  1.1164 +				{
  1.1165 +					if ($plaintext)
  1.1166 +					{
  1.1167 +						$address[] = $row['username'];
  1.1168 +					}
  1.1169 +					else
  1.1170 +					{
  1.1171 +						$address['user'][$row['user_id']] = array('name' => $row['username'], 'colour' => $row['user_colour']);
  1.1172 +					}
  1.1173 +				}
  1.1174 +			}
  1.1175 +			$db->sql_freeresult($result);
  1.1176 +		}
  1.1177 +
  1.1178 +		if (sizeof($g))
  1.1179 +		{
  1.1180 +			if ($plaintext)
  1.1181 +			{
  1.1182 +				$sql = 'SELECT group_name, group_type
  1.1183 +					FROM ' . GROUPS_TABLE . '
  1.1184 +						WHERE ' . $db->sql_in_set('group_id', $g);
  1.1185 +				$result = $db->sql_query($sql);
  1.1186 +
  1.1187 +				while ($row = $db->sql_fetchrow($result))
  1.1188 +				{
  1.1189 +					if ($check_type == 'to' || $author_id == $user->data['user_id'] || $row['user_id'] == $user->data['user_id'])
  1.1190 +					{
  1.1191 +						$address[] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
  1.1192 +					}
  1.1193 +				}
  1.1194 +				$db->sql_freeresult($result);
  1.1195 +			}
  1.1196 +			else
  1.1197 +			{
  1.1198 +				$sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, ug.user_id
  1.1199 +					FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . ' ug
  1.1200 +						WHERE ' . $db->sql_in_set('g.group_id', $g) . '
  1.1201 +						AND g.group_id = ug.group_id
  1.1202 +						AND ug.user_pending = 0';
  1.1203 +				$result = $db->sql_query($sql);
  1.1204 +
  1.1205 +				while ($row = $db->sql_fetchrow($result))
  1.1206 +				{
  1.1207 +					if (!isset($address['group'][$row['group_id']]))
  1.1208 +					{
  1.1209 +						if ($check_type == 'to' || $author_id == $user->data['user_id'] || $row['user_id'] == $user->data['user_id'])
  1.1210 +						{
  1.1211 +							$row['group_name'] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
  1.1212 +							$address['group'][$row['group_id']] = array('name' => $row['group_name'], 'colour' => $row['group_colour']);
  1.1213 +						}
  1.1214 +					}
  1.1215 +
  1.1216 +					if (isset($address['user'][$row['user_id']]))
  1.1217 +					{
  1.1218 +						$address['user'][$row['user_id']]['in_group'] = $row['group_id'];
  1.1219 +					}
  1.1220 +				}
  1.1221 +				$db->sql_freeresult($result);
  1.1222 +			}
  1.1223 +		}
  1.1224 +
  1.1225 +		if (sizeof($address) && !$plaintext)
  1.1226 +		{
  1.1227 +			$template->assign_var('S_' . strtoupper($check_type) . '_RECIPIENT', true);
  1.1228 +
  1.1229 +			foreach ($address as $type => $adr_ary)
  1.1230 +			{
  1.1231 +				foreach ($adr_ary as $id => $row)
  1.1232 +				{
  1.1233 +					$tpl_ary = array(
  1.1234 +						'IS_GROUP'	=> ($type == 'group') ? true : false,
  1.1235 +						'IS_USER'	=> ($type == 'user') ? true : false,
  1.1236 +						'UG_ID'		=> $id,
  1.1237 +						'NAME'		=> $row['name'],
  1.1238 +						'COLOUR'	=> ($row['colour']) ? '#' . $row['colour'] : '',
  1.1239 +						'TYPE'		=> $type,
  1.1240 +					);
  1.1241 +
  1.1242 +					if ($type == 'user')
  1.1243 +					{
  1.1244 +						$tpl_ary = array_merge($tpl_ary, array(
  1.1245 +							'U_VIEW'		=> get_username_string('profile', $id, $row['name'], $row['colour']),
  1.1246 +							'NAME_FULL'		=> get_username_string('full', $id, $row['name'], $row['colour']),
  1.1247 +						));
  1.1248 +					}
  1.1249 +					else
  1.1250 +					{
  1.1251 +						$tpl_ary = array_merge($tpl_ary, array(
  1.1252 +							'U_VIEW'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&amp;g=' . $id),
  1.1253 +						));
  1.1254 +					}
  1.1255 +
  1.1256 +					$template->assign_block_vars($check_type . '_recipient', $tpl_ary);
  1.1257 +				}
  1.1258 +			}
  1.1259 +		}
  1.1260 +
  1.1261 +		$addresses[$check_type] = $address;
  1.1262 +	}
  1.1263 +
  1.1264 +	return $addresses;
  1.1265 +}
  1.1266 +
  1.1267 +/**
  1.1268 +* Get folder status
  1.1269 +*/
  1.1270 +function get_folder_status($folder_id, $folder)
  1.1271 +{
  1.1272 +	global $db, $user, $config;
  1.1273 +
  1.1274 +	if (isset($folder[$folder_id]))
  1.1275 +	{
  1.1276 +		$folder = $folder[$folder_id];
  1.1277 +	}
  1.1278 +	else
  1.1279 +	{
  1.1280 +		return false;
  1.1281 +	}
  1.1282 +
  1.1283 +	$return = array(
  1.1284 +		'folder_name'	=> $folder['folder_name'],
  1.1285 +		'cur'			=> $folder['num_messages'],
  1.1286 +		'remaining'		=> ($user->data['message_limit']) ? $user->data['message_limit'] - $folder['num_messages'] : 0,
  1.1287 +		'max'			=> $user->data['message_limit'],
  1.1288 +		'percent'		=> ($user->data['message_limit']) ? (($user->data['message_limit'] > 0) ? round(($folder['num_messages'] / $user->data['message_limit']) * 100) : 100) : 0,
  1.1289 +	);
  1.1290 +
  1.1291 +	$return['message']	= sprintf($user->lang['FOLDER_STATUS_MSG'], $return['percent'], $return['cur'], $return['max']);
  1.1292 +
  1.1293 +	return $return;
  1.1294 +}
  1.1295 +
  1.1296 +//
  1.1297 +// COMPOSE MESSAGES
  1.1298 +//
  1.1299 +
  1.1300 +/**
  1.1301 +* Submit PM
  1.1302 +*/
  1.1303 +function submit_pm($mode, $subject, &$data, $put_in_outbox = true)
  1.1304 +{
  1.1305 +	global $db, $auth, $config, $phpEx, $template, $user, $phpbb_root_path;
  1.1306 +
  1.1307 +	// We do not handle erasing pms here
  1.1308 +	if ($mode == 'delete')
  1.1309 +	{
  1.1310 +		return false;
  1.1311 +	}
  1.1312 +
  1.1313 +	$current_time = time();
  1.1314 +
  1.1315 +	// Collect some basic information about which tables and which rows to update/insert
  1.1316 +	$sql_data = array();
  1.1317 +	$root_level = 0;
  1.1318 +
  1.1319 +	// Recipient Information
  1.1320 +	$recipients = $to = $bcc = array();
  1.1321 +
  1.1322 +	if ($mode != 'edit')
  1.1323 +	{
  1.1324 +		// Build Recipient List
  1.1325 +		// u|g => array($user_id => 'to'|'bcc')
  1.1326 +		$_types = array('u', 'g');
  1.1327 +		foreach ($_types as $ug_type)
  1.1328 +		{
  1.1329 +			if (isset($data['address_list'][$ug_type]) && sizeof($data['address_list'][$ug_type]))
  1.1330 +			{
  1.1331 +				foreach ($data['address_list'][$ug_type] as $id => $field)
  1.1332 +				{
  1.1333 +					$id = (int) $id;
  1.1334 +
  1.1335 +					// Do not rely on the address list being "valid"
  1.1336 +					if (!$id || ($ug_type == 'u' && $id == ANONYMOUS))
  1.1337 +					{
  1.1338 +						continue;
  1.1339 +					}
  1.1340 +
  1.1341 +					$field = ($field == 'to') ? 'to' : 'bcc';
  1.1342 +					if ($ug_type == 'u')
  1.1343 +					{
  1.1344 +						$recipients[$id] = $field;
  1.1345 +					}
  1.1346 +					${$field}[] = $ug_type . '_' . $id;
  1.1347 +				}
  1.1348 +			}
  1.1349 +		}
  1.1350 +
  1.1351 +		if (isset($data['address_list']['g']) && sizeof($data['address_list']['g']))
  1.1352 +		{
  1.1353 +			// We need to check the PM status of group members (do they want to receive PM's?)
  1.1354 +			// Only check if not a moderator or admin, since they are allowed to override this user setting
  1.1355 +			$sql_allow_pm = (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) ? ' AND u.user_allow_pm = 1' : '';
  1.1356 +
  1.1357 +			$sql = 'SELECT u.user_type, ug.group_id, ug.user_id
  1.1358 +				FROM ' . USERS_TABLE . ' u, ' . USER_GROUP_TABLE . ' ug
  1.1359 +				WHERE ' . $db->sql_in_set('ug.group_id', array_keys($data['address_list']['g'])) . '
  1.1360 +					AND ug.user_pending = 0
  1.1361 +					AND u.user_id = ug.user_id
  1.1362 +					AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')' .
  1.1363 +					$sql_allow_pm;
  1.1364 +			$result = $db->sql_query($sql);
  1.1365 +
  1.1366 +			while ($row = $db->sql_fetchrow($result))
  1.1367 +			{
  1.1368 +				// Additionally, do not include the sender if he is in the group he wants to send to. ;)
  1.1369 +				if ($row['user_id'] === $user->data['user_id'])
  1.1370 +				{
  1.1371 +					continue;
  1.1372 +				}
  1.1373 +
  1.1374 +				$field = ($data['address_list']['g'][$row['group_id']] == 'to') ? 'to' : 'bcc';
  1.1375 +				$recipients[$row['user_id']] = $field;
  1.1376 +			}
  1.1377 +			$db->sql_freeresult($result);
  1.1378 +		}
  1.1379 +
  1.1380 +		if (!sizeof($recipients))
  1.1381 +		{
  1.1382 +			trigger_error('NO_RECIPIENT');
  1.1383 +		}
  1.1384 +	}
  1.1385 +
  1.1386 +	// First of all make sure the subject are having the correct length.
  1.1387 +	$subject = truncate_string($subject);
  1.1388 +
  1.1389 +	$db->sql_transaction('begin');
  1.1390 +
  1.1391 +	$sql = '';
  1.1392 +
  1.1393 +	switch ($mode)
  1.1394 +	{
  1.1395 +		case 'reply':
  1.1396 +		case 'quote':
  1.1397 +			$root_level = ($data['reply_from_root_level']) ? $data['reply_from_root_level'] : $data['reply_from_msg_id'];
  1.1398 +
  1.1399 +			// Set message_replied switch for this user
  1.1400 +			$sql = 'UPDATE ' . PRIVMSGS_TO_TABLE . '
  1.1401 +				SET pm_replied = 1
  1.1402 +				WHERE user_id = ' . $data['from_user_id'] . '
  1.1403 +					AND msg_id = ' . $data['reply_from_msg_id'];
  1.1404 +
  1.1405 +		// no break
  1.1406 +
  1.1407 +		case 'forward':
  1.1408 +		case 'post':
  1.1409 +		case 'quotepost':
  1.1410 +			$sql_data = array(
  1.1411 +				'root_level'		=> $root_level,
  1.1412 +				'author_id'			=> $data['from_user_id'],
  1.1413 +				'icon_id'			=> $data['icon_id'],
  1.1414 +				'author_ip'			=> $data['from_user_ip'],
  1.1415 +				'message_time'		=> $current_time,
  1.1416 +				'enable_bbcode'		=> $data['enable_bbcode'],
  1.1417 +				'enable_smilies'	=> $data['enable_smilies'],
  1.1418 +				'enable_magic_url'	=> $data['enable_urls'],
  1.1419 +				'enable_sig'		=> $data['enable_sig'],
  1.1420 +				'message_subject'	=> $subject,
  1.1421 +				'message_text'		=> $data['message'],
  1.1422 +				'message_attachment'=> (!empty($data['attachment_data'])) ? 1 : 0,
  1.1423 +				'bbcode_bitfield'	=> $data['bbcode_bitfield'],
  1.1424 +				'bbcode_uid'		=> $data['bbcode_uid'],
  1.1425 +				'to_address'		=> implode(':', $to),
  1.1426 +				'bcc_address'		=> implode(':', $bcc),
  1.1427 +				'message_reported'	=> 0,
  1.1428 +			);
  1.1429 +		break;
  1.1430 +
  1.1431 +		case 'edit':
  1.1432 +			$sql_data = array(
  1.1433 +				'icon_id'			=> $data['icon_id'],
  1.1434 +				'message_edit_time'	=> $current_time,
  1.1435 +				'enable_bbcode'		=> $data['enable_bbcode'],
  1.1436 +				'enable_smilies'	=> $data['enable_smilies'],
  1.1437 +				'enable_magic_url'	=> $data['enable_urls'],
  1.1438 +				'enable_sig'		=> $data['enable_sig'],
  1.1439 +				'message_subject'	=> $subject,
  1.1440 +				'message_text'		=> $data['message'],
  1.1441 +				'message_attachment'=> (!empty($data['attachment_data'])) ? 1 : 0,
  1.1442 +				'bbcode_bitfield'	=> $data['bbcode_bitfield'],
  1.1443 +				'bbcode_uid'		=> $data['bbcode_uid']
  1.1444 +			);
  1.1445 +		break;
  1.1446 +	}
  1.1447 +
  1.1448 +	if (sizeof($sql_data))
  1.1449 +	{
  1.1450 +		$query = '';
  1.1451 +
  1.1452 +		if ($mode == 'post' || $mode == 'reply' || $mode == 'quote' || $mode == 'quotepost' || $mode == 'forward')
  1.1453 +		{
  1.1454 +			$db->sql_query('INSERT INTO ' . PRIVMSGS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data));
  1.1455 +			$data['msg_id'] = $db->sql_nextid();
  1.1456 +		}
  1.1457 +		else if ($mode == 'edit')
  1.1458 +		{
  1.1459 +			$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  1.1460 +				SET message_edit_count = message_edit_count + 1, ' . $db->sql_build_array('UPDATE', $sql_data) . '
  1.1461 +				WHERE msg_id = ' . $data['msg_id'];
  1.1462 +			$db->sql_query($sql);
  1.1463 +		}
  1.1464 +	}
  1.1465 +
  1.1466 +	if ($mode != 'edit')
  1.1467 +	{
  1.1468 +		if ($sql)
  1.1469 +		{
  1.1470 +			$db->sql_query($sql);
  1.1471 +		}
  1.1472 +		unset($sql);
  1.1473 +
  1.1474 +		$sql_ary = array();
  1.1475 +		foreach ($recipients as $user_id => $type)
  1.1476 +		{
  1.1477 +			$sql_ary[] = array(
  1.1478 +				'msg_id'		=> (int) $data['msg_id'],
  1.1479 +				'user_id'		=> (int) $user_id,
  1.1480 +				'author_id'		=> (int) $data['from_user_id'],
  1.1481 +				'folder_id'		=> PRIVMSGS_NO_BOX,
  1.1482 +				'pm_new'		=> 1,
  1.1483 +				'pm_unread'		=> 1,
  1.1484 +				'pm_forwarded'	=> ($mode == 'forward') ? 1 : 0
  1.1485 +			);
  1.1486 +		}
  1.1487 +
  1.1488 +		$db->sql_multi_insert(PRIVMSGS_TO_TABLE, $sql_ary);
  1.1489 +
  1.1490 +		$sql = 'UPDATE ' . USERS_TABLE . '
  1.1491 +			SET user_new_privmsg = user_new_privmsg + 1, user_unread_privmsg = user_unread_privmsg + 1, user_last_privmsg = ' . time() . '
  1.1492 +			WHERE ' . $db->sql_in_set('user_id', array_keys($recipients));
  1.1493 +		$db->sql_query($sql);
  1.1494 +
  1.1495 +		// Put PM into outbox
  1.1496 +		if ($put_in_outbox)
  1.1497 +		{
  1.1498 +			$db->sql_query('INSERT INTO ' . PRIVMSGS_TO_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  1.1499 +				'msg_id'		=> (int) $data['msg_id'],
  1.1500 +				'user_id'		=> (int) $data['from_user_id'],
  1.1501 +				'author_id'		=> (int) $data['from_user_id'],
  1.1502 +				'folder_id'		=> PRIVMSGS_OUTBOX,
  1.1503 +				'pm_new'		=> 0,
  1.1504 +				'pm_unread'		=> 0,
  1.1505 +				'pm_forwarded'	=> ($mode == 'forward') ? 1 : 0))
  1.1506 +			);
  1.1507 +		}
  1.1508 +	}
  1.1509 +
  1.1510 +	// Set user last post time
  1.1511 +	if ($mode == 'reply' || $mode == 'quote' || $mode == 'quotepost' || $mode == 'forward' || $mode == 'post')
  1.1512 +	{
  1.1513 +		$sql = 'UPDATE ' . USERS_TABLE . "
  1.1514 +			SET user_lastpost_time = $current_time
  1.1515 +			WHERE user_id = " . $data['from_user_id'];
  1.1516 +		$db->sql_query($sql);
  1.1517 +	}
  1.1518 +
  1.1519 +	// Submit Attachments
  1.1520 +	if (!empty($data['attachment_data']) && $data['msg_id'] && in_array($mode, array('post', 'reply', 'quote', 'quotepost', 'edit', 'forward')))
  1.1521 +	{
  1.1522 +		$space_taken = $files_added = 0;
  1.1523 +		$orphan_rows = array();
  1.1524 +
  1.1525 +		foreach ($data['attachment_data'] as $pos => $attach_row)
  1.1526 +		{
  1.1527 +			$orphan_rows[(int) $attach_row['attach_id']] = array();
  1.1528 +		}
  1.1529 +
  1.1530 +		if (sizeof($orphan_rows))
  1.1531 +		{
  1.1532 +			$sql = 'SELECT attach_id, filesize, physical_filename
  1.1533 +				FROM ' . ATTACHMENTS_TABLE . '
  1.1534 +				WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
  1.1535 +					AND in_message = 1
  1.1536 +					AND is_orphan = 1
  1.1537 +					AND poster_id = ' . $user->data['user_id'];
  1.1538 +			$result = $db->sql_query($sql);
  1.1539 +
  1.1540 +			$orphan_rows = array();
  1.1541 +			while ($row = $db->sql_fetchrow($result))
  1.1542 +			{
  1.1543 +				$orphan_rows[$row['attach_id']] = $row;
  1.1544 +			}
  1.1545 +			$db->sql_freeresult($result);
  1.1546 +		}
  1.1547 +
  1.1548 +		foreach ($data['attachment_data'] as $pos => $attach_row)
  1.1549 +		{
  1.1550 +			if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
  1.1551 +			{
  1.1552 +				continue;
  1.1553 +			}
  1.1554 +
  1.1555 +			if (!$attach_row['is_orphan'])
  1.1556 +			{
  1.1557 +				// update entry in db if attachment already stored in db and filespace
  1.1558 +				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
  1.1559 +					SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
  1.1560 +					WHERE attach_id = " . (int) $attach_row['attach_id'] . '
  1.1561 +						AND is_orphan = 0';
  1.1562 +				$db->sql_query($sql);
  1.1563 +			}
  1.1564 +			else
  1.1565 +			{
  1.1566 +				// insert attachment into db
  1.1567 +				if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
  1.1568 +				{
  1.1569 +					continue;
  1.1570 +				}
  1.1571 +
  1.1572 +				$space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
  1.1573 +				$files_added++;
  1.1574 +
  1.1575 +				$attach_sql = array(
  1.1576 +					'post_msg_id'		=> $data['msg_id'],
  1.1577 +					'topic_id'			=> 0,
  1.1578 +					'is_orphan'			=> 0,
  1.1579 +					'poster_id'			=> $data['from_user_id'],
  1.1580 +					'attach_comment'	=> $attach_row['attach_comment'],
  1.1581 +				);
  1.1582 +
  1.1583 +				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
  1.1584 +					WHERE attach_id = ' . $attach_row['attach_id'] . '
  1.1585 +						AND is_orphan = 1
  1.1586 +						AND poster_id = ' . $user->data['user_id'];
  1.1587 +				$db->sql_query($sql);
  1.1588 +			}
  1.1589 +		}
  1.1590 +
  1.1591 +		if ($space_taken && $files_added)
  1.1592 +		{
  1.1593 +			set_config_count('upload_dir_size', $space_taken, true);
  1.1594 +			set_config_count('num_files', $files_added, true);
  1.1595 +		}
  1.1596 +	}
  1.1597 +
  1.1598 +	// Delete draft if post was loaded...
  1.1599 +	$draft_id = request_var('draft_loaded', 0);
  1.1600 +	if ($draft_id)
  1.1601 +	{
  1.1602 +		$sql = 'DELETE FROM ' . DRAFTS_TABLE . "
  1.1603 +			WHERE draft_id = $draft_id
  1.1604 +				AND user_id = " . $data['from_user_id'];
  1.1605 +		$db->sql_query($sql);
  1.1606 +	}
  1.1607 +
  1.1608 +	$db->sql_transaction('commit');
  1.1609 +
  1.1610 +	// Send Notifications
  1.1611 +	if ($mode != 'edit')
  1.1612 +	{
  1.1613 +		pm_notification($mode, $data['from_username'], $recipients, $subject, $data['message'], $data['msg_id']);
  1.1614 +	}
  1.1615 +
  1.1616 +	return $data['msg_id'];
  1.1617 +}
  1.1618 +
  1.1619 +/**
  1.1620 +* PM Notification
  1.1621 +*/
  1.1622 +function pm_notification($mode, $author, $recipients, $subject, $message, $msg_id)
  1.1623 +{
  1.1624 +	global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
  1.1625 +
  1.1626 +	$subject = censor_text($subject);
  1.1627 +
  1.1628 +	unset($recipients[ANONYMOUS], $recipients[$user->data['user_id']]);
  1.1629 +
  1.1630 +	if (!sizeof($recipients))
  1.1631 +	{
  1.1632 +		return;
  1.1633 +	}
  1.1634 +
  1.1635 +	// Get banned User ID's
  1.1636 +	$sql = 'SELECT ban_userid
  1.1637 +		FROM ' . BANLIST_TABLE . '
  1.1638 +		WHERE ' . $db->sql_in_set('ban_userid', array_map('intval', array_keys($recipients))) . '
  1.1639 +			AND ban_exclude = 0';
  1.1640 +	$result = $db->sql_query($sql);
  1.1641 +
  1.1642 +	while ($row = $db->sql_fetchrow($result))
  1.1643 +	{
  1.1644 +		unset($recipients[$row['ban_userid']]);
  1.1645 +	}
  1.1646 +	$db->sql_freeresult($result);
  1.1647 +
  1.1648 +	if (!sizeof($recipients))
  1.1649 +	{
  1.1650 +		return;
  1.1651 +	}
  1.1652 +
  1.1653 +	$sql = 'SELECT user_id, username, user_email, user_lang, user_notify_pm, user_notify_type, user_jabber
  1.1654 +		FROM ' . USERS_TABLE . '
  1.1655 +		WHERE ' . $db->sql_in_set('user_id', array_map('intval', array_keys($recipients)));
  1.1656 +	$result = $db->sql_query($sql);
  1.1657 +
  1.1658 +	$msg_list_ary = array();
  1.1659 +	while ($row = $db->sql_fetchrow($result))
  1.1660 +	{
  1.1661 +		if ($row['user_notify_pm'] == 1 && trim($row['user_email']))
  1.1662 +		{
  1.1663 +			$msg_list_ary[] = array(
  1.1664 +				'method'	=> $row['user_notify_type'],
  1.1665 +				'email'		=> $row['user_email'],
  1.1666 +				'jabber'	=> $row['user_jabber'],
  1.1667 +				'name'		=> $row['username'],
  1.1668 +				'lang'		=> $row['user_lang']
  1.1669 +			);
  1.1670 +		}
  1.1671 +	}
  1.1672 +	$db->sql_freeresult($result);
  1.1673 +
  1.1674 +	if (!sizeof($msg_list_ary))
  1.1675 +	{
  1.1676 +		return;
  1.1677 +	}
  1.1678 +
  1.1679 +	include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
  1.1680 +	$messenger = new messenger();
  1.1681 +
  1.1682 +	foreach ($msg_list_ary as $pos => $addr)
  1.1683 +	{
  1.1684 +		$messenger->template('privmsg_notify', $addr['lang']);
  1.1685 +
  1.1686 +		$messenger->to($addr['email'], $addr['name']);
  1.1687 +		$messenger->im($addr['jabber'], $addr['name']);
  1.1688 +
  1.1689 +		$messenger->assign_vars(array(
  1.1690 +			'SUBJECT'		=> htmlspecialchars_decode($subject),
  1.1691 +			'AUTHOR_NAME'	=> htmlspecialchars_decode($author),
  1.1692 +			'USERNAME'		=> htmlspecialchars_decode($addr['name']),
  1.1693 +
  1.1694 +			'U_INBOX'			=> generate_board_url() . "/ucp.$phpEx?i=pm&folder=inbox",
  1.1695 +			'U_VIEW_MESSAGE'	=> generate_board_url() . "/ucp.$phpEx?i=pm&mode=view&p=$msg_id",
  1.1696 +		));
  1.1697 +
  1.1698 +		$messenger->send($addr['method']);
  1.1699 +	}
  1.1700 +	unset($msg_list_ary);
  1.1701 +
  1.1702 +	$messenger->save_queue();
  1.1703 +
  1.1704 +	unset($messenger);
  1.1705 +}
  1.1706 +
  1.1707 +/**
  1.1708 +* Display Message History
  1.1709 +*/
  1.1710 +function message_history($msg_id, $user_id, $message_row, $folder, $in_post_mode = false)
  1.1711 +{
  1.1712 +	global $db, $user, $config, $template, $phpbb_root_path, $phpEx, $auth, $bbcode;
  1.1713 +
  1.1714 +	// Select all receipts and the author from the pm we currently view, to only display their pm-history
  1.1715 +	$sql = 'SELECT author_id, user_id
  1.1716 +		FROM ' . PRIVMSGS_TO_TABLE . "
  1.1717 +		WHERE msg_id = $msg_id
  1.1718 +			AND folder_id <> " . PRIVMSGS_HOLD_BOX;
  1.1719 +	$result = $db->sql_query($sql);
  1.1720 +
  1.1721 +	$recipients = array();
  1.1722 +	while ($row = $db->sql_fetchrow($result))
  1.1723 +	{
  1.1724 +		$recipients[] = (int) $row['user_id'];
  1.1725 +		$recipients[] = (int) $row['author_id'];
  1.1726 +	}
  1.1727 +	$db->sql_freeresult($result);
  1.1728 +	$recipients = array_unique($recipients);
  1.1729 +
  1.1730 +	// Get History Messages (could be newer)
  1.1731 +	$sql = 'SELECT t.*, p.*, u.*
  1.1732 +		FROM ' . PRIVMSGS_TABLE . ' p, ' . PRIVMSGS_TO_TABLE . ' t, ' . USERS_TABLE . ' u
  1.1733 +		WHERE t.msg_id = p.msg_id
  1.1734 +			AND p.author_id = u.user_id
  1.1735 +			AND t.folder_id NOT IN (' . PRIVMSGS_NO_BOX . ', ' . PRIVMSGS_HOLD_BOX . ')
  1.1736 +			AND ' . $db->sql_in_set('t.author_id', $recipients, false, true) . "
  1.1737 +			AND t.user_id = $user_id";
  1.1738 +
  1.1739 +	// We no longer need those.
  1.1740 +	unset($recipients);
  1.1741 +
  1.1742 +	if (!$message_row['root_level'])
  1.1743 +	{
  1.1744 +		$sql .= " AND (p.root_level = $msg_id OR (p.root_level = 0 AND p.msg_id = $msg_id))";
  1.1745 +	}
  1.1746 +	else
  1.1747 +	{
  1.1748 +		$sql .= " AND (p.root_level = " . $message_row['root_level'] . ' OR p.msg_id = ' . $message_row['root_level'] . ')';
  1.1749 +	}
  1.1750 +	$sql .= ' ORDER BY p.message_time DESC';
  1.1751 +
  1.1752 +	$result = $db->sql_query($sql);
  1.1753 +	$row = $db->sql_fetchrow($result);
  1.1754 +
  1.1755 +	if (!$row)
  1.1756 +	{
  1.1757 +		$db->sql_freeresult($result);
  1.1758 +		return false;
  1.1759 +	}
  1.1760 +
  1.1761 +	$title = $row['message_subject'];
  1.1762 +
  1.1763 +	$rowset = array();
  1.1764 +	$bbcode_bitfield = '';
  1.1765 +	$folder_url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm') . '&amp;folder=';
  1.1766 +
  1.1767 +	do
  1.1768 +	{
  1.1769 +		$folder_id = (int) $row['folder_id'];
  1.1770 +
  1.1771 +		$row['folder'][] = (isset($folder[$folder_id])) ? '<a href="' . $folder_url . $folder_id . '">' . $folder[$folder_id]['folder_name'] . '</a>' : $user->lang['UNKNOWN_FOLDER'];
  1.1772 +
  1.1773 +		if (isset($rowset[$row['msg_id']]))
  1.1774 +		{
  1.1775 +			$rowset[$row['msg_id']]['folder'][] = (isset($folder[$folder_id])) ? '<a href="' . $folder_url . $folder_id . '">' . $folder[$folder_id]['folder_name'] . '</a>' : $user->lang['UNKNOWN_FOLDER'];
  1.1776 +		}
  1.1777 +		else
  1.1778 +		{
  1.1779 +			$rowset[$row['msg_id']] = $row;
  1.1780 +			$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  1.1781 +		}
  1.1782 +	}
  1.1783 +	while ($row = $db->sql_fetchrow($result));
  1.1784 +	$db->sql_freeresult($result);
  1.1785 +
  1.1786 +	if (sizeof($rowset) == 1 && !$in_post_mode)
  1.1787 +	{
  1.1788 +		return false;
  1.1789 +	}
  1.1790 +
  1.1791 +	// Instantiate BBCode class
  1.1792 +	if ((empty($bbcode) || $bbcode === false) && $bbcode_bitfield !== '')
  1.1793 +	{
  1.1794 +		if (!class_exists('bbcode'))
  1.1795 +		{
  1.1796 +			include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  1.1797 +		}
  1.1798 +		$bbcode = new bbcode(base64_encode($bbcode_bitfield));
  1.1799 +	}
  1.1800 +
  1.1801 +	$title = censor_text($title);
  1.1802 +
  1.1803 +	$url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm');
  1.1804 +	$next_history_pm = $previous_history_pm = $prev_id = 0;
  1.1805 +
  1.1806 +	// Re-order rowset to be able to get the next/prev message rows...
  1.1807 +	$rowset = array_values($rowset);
  1.1808 +
  1.1809 +	for ($i = 0, $size = sizeof($rowset); $i < $size; $i++)
  1.1810 +	{
  1.1811 +		$row = &$rowset[$i];
  1.1812 +		$id = (int) $row['msg_id'];
  1.1813 +
  1.1814 +		$author_id	= $row['author_id'];
  1.1815 +		$folder_id	= (int) $row['folder_id'];
  1.1816 +
  1.1817 +		$subject	= $row['message_subject'];
  1.1818 +		$message	= $row['message_text'];
  1.1819 +
  1.1820 +		$message = censor_text($message);
  1.1821 +
  1.1822 +		$decoded_message = false;
  1.1823 +
  1.1824 +		if ($in_post_mode && $auth->acl_get('u_sendpm') && $author_id != ANONYMOUS)
  1.1825 +		{
  1.1826 +			$decoded_message = $message;
  1.1827 +			decode_message($decoded_message, $row['bbcode_uid']);
  1.1828 +
  1.1829 +			$decoded_message = bbcode_nl2br($decoded_message);
  1.1830 +		}
  1.1831 +
  1.1832 +		if ($row['bbcode_bitfield'])
  1.1833 +		{
  1.1834 +			$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
  1.1835 +		}
  1.1836 +
  1.1837 +		$message = bbcode_nl2br($message);
  1.1838 +		$message = smiley_text($message, !$row['enable_smilies']);
  1.1839 +
  1.1840 +		$subject = censor_text($subject);
  1.1841 +
  1.1842 +		if ($id == $msg_id)
  1.1843 +		{
  1.1844 +			$next_history_pm = (isset($rowset[$i + 1])) ? (int) $rowset[$i + 1]['msg_id'] : 0;
  1.1845 +			$previous_history_pm = $prev_id;
  1.1846 +		}
  1.1847 +
  1.1848 +		$template->assign_block_vars('history_row', array(
  1.1849 +			'MESSAGE_AUTHOR_QUOTE'		=> (($decoded_message) ? addslashes(get_username_string('username', $author_id, $row['username'], $row['user_colour'], $row['username'])) : ''),
  1.1850 +			'MESSAGE_AUTHOR_FULL'		=> get_username_string('full', $author_id, $row['username'], $row['user_colour'], $row['username']),
  1.1851 +			'MESSAGE_AUTHOR_COLOUR'		=> get_username_string('colour', $author_id, $row['username'], $row['user_colour'], $row['username']),
  1.1852 +			'MESSAGE_AUTHOR'			=> get_username_string('username', $author_id, $row['username'], $row['user_colour'], $row['username']),
  1.1853 +			'U_MESSAGE_AUTHOR'			=> get_username_string('profile', $author_id, $row['username'], $row['user_colour'], $row['username']),
  1.1854 +
  1.1855 +			'SUBJECT'			=> $subject,
  1.1856 +			'SENT_DATE'			=> $user->format_date($row['message_time']),
  1.1857 +			'MESSAGE'			=> $message,
  1.1858 +			'FOLDER'			=> implode(', ', $row['folder']),
  1.1859 +			'DECODED_MESSAGE'	=> $decoded_message,
  1.1860 +
  1.1861 +			'S_CURRENT_MSG'		=> ($row['msg_id'] == $msg_id),
  1.1862 +			'S_AUTHOR_DELETED'	=> ($author_id == ANONYMOUS) ? true : false,
  1.1863 +			'S_IN_POST_MODE'	=> $in_post_mode,
  1.1864 +
  1.1865 +			'MSG_ID'			=> $row['msg_id'],
  1.1866 +			'U_VIEW_MESSAGE'	=> "$url&amp;f=$folder_id&amp;p=" . $row['msg_id'],
  1.1867 +			'U_QUOTE'			=> (!$in_post_mode && $auth->acl_get('u_sendpm') && $author_id != ANONYMOUS) ? "$url&amp;mode=compose&amp;action=quote&amp;f=" . $folder_id . "&amp;p=" . $row['msg_id'] : '',
  1.1868 +			'U_POST_REPLY_PM'	=> ($author_id != $user->data['user_id'] && $author_id != ANONYMOUS && $auth->acl_get('u_sendpm')) ? "$url&amp;mode=compose&amp;action=reply&amp;f=$folder_id&amp;p=" . $row['msg_id'] : '')
  1.1869 +		);
  1.1870 +		unset($rowset[$i]);
  1.1871 +		$prev_id = $id;
  1.1872 +	}
  1.1873 +
  1.1874 +	$template->assign_vars(array(
  1.1875 +		'QUOTE_IMG'			=> $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']),
  1.1876 +		'HISTORY_TITLE'		=> $title,
  1.1877 +
  1.1878 +		'U_VIEW_NEXT_HISTORY'		=> ($next_history_pm) ? "$url&amp;p=" . $next_history_pm : '',
  1.1879 +		'U_VIEW_PREVIOUS_HISTORY'	=> ($previous_history_pm) ? "$url&amp;p=" . $previous_history_pm : '',
  1.1880 +	));
  1.1881 +
  1.1882 +	return true;
  1.1883 +}
  1.1884 +
  1.1885 +/**
  1.1886 +* Set correct users max messages in PM folder.
  1.1887 +* If several group memberships define different amount of messages, the highest will be chosen.
  1.1888 +*/
  1.1889 +function set_user_message_limit()
  1.1890 +{
  1.1891 +	global $user, $db, $config;
  1.1892 +
  1.1893 +	// Get maximum about from user memberships - if it is 0, there is no limit set and we use the maximum value within the config.
  1.1894 +	$sql = 'SELECT MAX(g.group_message_limit) as max_message_limit
  1.1895 +		FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . ' ug
  1.1896 +		WHERE ug.user_id = ' . $user->data['user_id'] . '
  1.1897 +			AND ug.user_pending = 0
  1.1898 +			AND ug.group_id = g.group_id';
  1.1899 +	$result = $db->sql_query($sql);
  1.1900 +	$message_limit = (int) $db->sql_fetchfield('max_message_limit');
  1.1901 +	$db->sql_freeresult($result);
  1.1902 +
  1.1903 +	$user->data['message_limit'] = (!$message_limit) ? $config['pm_max_msgs'] : $message_limit;
  1.1904 +}
  1.1905 +
  1.1906 +/**
  1.1907 +* Generates an array of coloured recipient names from a list of PMs - (groups & users)
  1.1908 +*
  1.1909 +* @param	array	$pm_by_id	An array of rows from PRIVMSGS_TABLE, keys are the msg_ids.
  1.1910 +*
  1.1911 +* @return	array				2D Array: array(msg_id => array('username or group string', ...), ...)
  1.1912 +*								Usernames are generated with {@link get_username_string get_username_string}
  1.1913 +*								Groups are coloured and have a link to the membership page
  1.1914 +*/
  1.1915 +function get_recipient_strings($pm_by_id)
  1.1916 +{
  1.1917 +	global $db, $phpbb_root_path, $phpEx, $user;
  1.1918 +
  1.1919 +	$address_list = $recipient_list = $address = array();
  1.1920 +
  1.1921 +	$_types = array('u', 'g');
  1.1922 +
  1.1923 +	foreach ($pm_by_id as $message_id => $row)
  1.1924 +	{
  1.1925 +		$address[$message_id] = rebuild_header(array('to' => $row['to_address'], 'bcc' => $row['bcc_address']));
  1.1926 +
  1.1927 +		foreach ($_types as $ug_type)
  1.1928 +		{
  1.1929 +			if (isset($address[$message_id][$ug_type]) && sizeof($address[$message_id][$ug_type]))
  1.1930 +			{
  1.1931 +				foreach ($address[$message_id][$ug_type] as $ug_id => $in_to)
  1.1932 +				{
  1.1933 +					$recipient_list[$ug_type][$ug_id] = array('name' => $user->lang['NA'], 'colour' => '');
  1.1934 +				}
  1.1935 +			}
  1.1936 +		}
  1.1937 +	}
  1.1938 +
  1.1939 +	foreach ($_types as $ug_type)
  1.1940 +	{
  1.1941 +		if (!empty($recipient_list[$ug_type]))
  1.1942 +		{
  1.1943 +			if ($ug_type == 'u')
  1.1944 +			{
  1.1945 +				$sql = 'SELECT user_id as id, username as name, user_colour as colour
  1.1946 +					FROM ' . USERS_TABLE . '
  1.1947 +					WHERE ';
  1.1948 +			}
  1.1949 +			else
  1.1950 +			{
  1.1951 +				$sql = 'SELECT group_id as id, group_name as name, group_colour as colour, group_type
  1.1952 +					FROM ' . GROUPS_TABLE . '
  1.1953 +					WHERE ';
  1.1954 +			}
  1.1955 +			$sql .= $db->sql_in_set(($ug_type == 'u') ? 'user_id' : 'group_id', array_map('intval', array_keys($recipient_list[$ug_type])));
  1.1956 +
  1.1957 +			$result = $db->sql_query($sql);
  1.1958 +
  1.1959 +			while ($row = $db->sql_fetchrow($result))
  1.1960 +			{
  1.1961 +				if ($ug_type == 'g')
  1.1962 +				{
  1.1963 +					$row['name'] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['name']] : $row['name'];
  1.1964 +				}
  1.1965 +
  1.1966 +				$recipient_list[$ug_type][$row['id']] = array('name' => $row['name'], 'colour' => $row['colour']);
  1.1967 +			}
  1.1968 +			$db->sql_freeresult($result);
  1.1969 +		}
  1.1970 +	}
  1.1971 +
  1.1972 +	foreach ($address as $message_id => $adr_ary)
  1.1973 +	{
  1.1974 +		foreach ($adr_ary as $type => $id_ary)
  1.1975 +		{
  1.1976 +			foreach ($id_ary as $ug_id => $_id)
  1.1977 +			{
  1.1978 +				if ($type == 'u')
  1.1979 +				{
  1.1980 +					$address_list[$message_id][] = get_username_string('full', $ug_id, $recipient_list[$type][$ug_id]['name'], $recipient_list[$type][$ug_id]['colour']);
  1.1981 +				}
  1.1982 +				else
  1.1983 +				{
  1.1984 +					$user_colour = ($recipient_list[$type][$ug_id]['colour']) ? ' style="font-weight: bold; color:#' . $recipient_list[$type][$ug_id]['colour'] . '"' : '';
  1.1985 +					$link = '<a href="' . append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&amp;g=' . $ug_id) . '"' . $user_colour . '>';
  1.1986 +					$address_list[$message_id][] = $link . $recipient_list[$type][$ug_id]['name'] . (($link) ? '</a>' : '');
  1.1987 +				}
  1.1988 +			}
  1.1989 +		}
  1.1990 +	}
  1.1991 +
  1.1992 +	return $address_list;
  1.1993 +}
  1.1994 +
  1.1995 +?>
  1.1996 \ No newline at end of file