includes/ucp/ucp_pm_options.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/ucp/ucp_pm_options.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,853 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package ucp
     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 +* Execute message options
    1.24 +*/
    1.25 +function message_options($id, $mode, $global_privmsgs_rules, $global_rule_conditions)
    1.26 +{
    1.27 +	global $phpbb_root_path, $phpEx, $user, $template, $auth, $config, $db;
    1.28 +
    1.29 +	$redirect_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;mode=options");
    1.30 +
    1.31 +	add_form_key('ucp_pm_options');
    1.32 +	// Change "full folder" setting - what to do if folder is full
    1.33 +	if (isset($_POST['fullfolder']))
    1.34 +	{
    1.35 +		check_form_key('ucp_pm_options', $config['form_token_lifetime'], $redirect_url);
    1.36 +		$full_action = request_var('full_action', 0);
    1.37 +
    1.38 +		$set_folder_id = 0;
    1.39 +		switch ($full_action)
    1.40 +		{
    1.41 +			case 1:
    1.42 +				$set_folder_id = FULL_FOLDER_DELETE;
    1.43 +			break;
    1.44 +
    1.45 +			case 2:
    1.46 +				$set_folder_id = request_var('full_move_to', PRIVMSGS_INBOX);
    1.47 +			break;
    1.48 +
    1.49 +			case 3:
    1.50 +				$set_folder_id = FULL_FOLDER_HOLD;
    1.51 +			break;
    1.52 +
    1.53 +			default:
    1.54 +				$full_action = 0;
    1.55 +			break;
    1.56 +		}
    1.57 +
    1.58 +		if ($full_action)
    1.59 +		{
    1.60 +			$sql = 'UPDATE ' . USERS_TABLE . '
    1.61 +				SET user_full_folder = ' . $set_folder_id . '
    1.62 +				WHERE user_id = ' . $user->data['user_id'];
    1.63 +			$db->sql_query($sql);
    1.64 +
    1.65 +			$user->data['user_full_folder'] = $set_folder_id;
    1.66 +
    1.67 +			$message = $user->lang['FULL_FOLDER_OPTION_CHANGED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
    1.68 +			meta_refresh(3, $redirect_url);
    1.69 +			trigger_error($message);
    1.70 +		}
    1.71 +	}
    1.72 +	
    1.73 +	// Add Folder
    1.74 +	if (isset($_POST['addfolder']))
    1.75 +	{
    1.76 +		if (check_form_key('ucp_pm_options'))
    1.77 +		{
    1.78 +			$folder_name = utf8_normalize_nfc(request_var('foldername', '', true));
    1.79 +			$msg = '';
    1.80 +
    1.81 +			if ($folder_name)
    1.82 +			{
    1.83 +				$sql = 'SELECT folder_name
    1.84 +					FROM ' . PRIVMSGS_FOLDER_TABLE . "
    1.85 +					WHERE folder_name = '" . $db->sql_escape($folder_name) . "'
    1.86 +						AND user_id = " . $user->data['user_id'];
    1.87 +				$result = $db->sql_query_limit($sql, 1);
    1.88 +				$row = $db->sql_fetchrow($result);
    1.89 +				$db->sql_freeresult($result);
    1.90 +
    1.91 +				if ($row)
    1.92 +				{
    1.93 +					trigger_error(sprintf($user->lang['FOLDER_NAME_EXIST'], $folder_name));
    1.94 +				}
    1.95 +
    1.96 +				$sql = 'SELECT COUNT(folder_id) as num_folder
    1.97 +					FROM ' . PRIVMSGS_FOLDER_TABLE . '
    1.98 +						WHERE user_id = ' . $user->data['user_id'];
    1.99 +				$result = $db->sql_query($sql);
   1.100 +				$num_folder = (int) $db->sql_fetchfield('num_folder');
   1.101 +				$db->sql_freeresult($result);
   1.102 +
   1.103 +				if ($num_folder >= $config['pm_max_boxes'])
   1.104 +				{
   1.105 +					trigger_error('MAX_FOLDER_REACHED');
   1.106 +				}
   1.107 +
   1.108 +				$sql = 'INSERT INTO ' . PRIVMSGS_FOLDER_TABLE . ' ' . $db->sql_build_array('INSERT', array(
   1.109 +					'user_id'		=> (int) $user->data['user_id'],
   1.110 +					'folder_name'	=> $folder_name)
   1.111 +				);
   1.112 +				$db->sql_query($sql);
   1.113 +				$msg = $user->lang['FOLDER_ADDED'];
   1.114 +			}
   1.115 +			else
   1.116 +			{
   1.117 +				$msg = $user->lang['FOLDER_NAME_EMPTY'];
   1.118 +			}
   1.119 +		}
   1.120 +		else
   1.121 +		{
   1.122 +			$msg = $user->lang['FORM_INVALID'];
   1.123 +		}
   1.124 +		$message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
   1.125 +		meta_refresh(3, $redirect_url);
   1.126 +		trigger_error($message);
   1.127 +	}
   1.128 +
   1.129 +	// Rename folder
   1.130 +	if (isset($_POST['rename_folder']))
   1.131 +	{
   1.132 +		if (check_form_key('ucp_pm_options'))
   1.133 +		{
   1.134 +			$new_folder_name = utf8_normalize_nfc(request_var('new_folder_name', '', true));
   1.135 +			$rename_folder_id= request_var('rename_folder_id', 0);
   1.136 +
   1.137 +			if (!$new_folder_name)
   1.138 +			{
   1.139 +				trigger_error('NO_NEW_FOLDER_NAME');
   1.140 +			}
   1.141 +
   1.142 +			// Select custom folder
   1.143 +			$sql = 'SELECT folder_name, pm_count
   1.144 +				FROM ' . PRIVMSGS_FOLDER_TABLE . "
   1.145 +				WHERE user_id = {$user->data['user_id']}
   1.146 +					AND folder_id = $rename_folder_id";
   1.147 +			$result = $db->sql_query_limit($sql, 1);
   1.148 +			$folder_row = $db->sql_fetchrow($result);
   1.149 +			$db->sql_freeresult($result);
   1.150 +
   1.151 +			if (!$folder_row)
   1.152 +			{
   1.153 +				trigger_error('CANNOT_RENAME_FOLDER');
   1.154 +			}
   1.155 +
   1.156 +			$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
   1.157 +				SET folder_name = '" . $db->sql_escape($new_folder_name) . "'
   1.158 +				WHERE folder_id = $rename_folder_id
   1.159 +					AND user_id = {$user->data['user_id']}";
   1.160 +			$db->sql_query($sql);
   1.161 +			$msg = $user->lang['FOLDER_RENAMED'];
   1.162 +		}
   1.163 +		else
   1.164 +		{
   1.165 +			$msg = $user->lang['FORM_INVALID'];
   1.166 +		}
   1.167 +
   1.168 +		$message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
   1.169 +
   1.170 +		meta_refresh(3, $redirect_url);
   1.171 +		trigger_error($message);
   1.172 +	}
   1.173 +
   1.174 +	// Remove Folder
   1.175 +	if (isset($_POST['remove_folder']))
   1.176 +	{
   1.177 +		$remove_folder_id = request_var('remove_folder_id', 0);
   1.178 +
   1.179 +		// Default to "move all messages to inbox"
   1.180 +		$remove_action = request_var('remove_action', 1);
   1.181 +		$move_to = request_var('move_to', PRIVMSGS_INBOX);
   1.182 +
   1.183 +		// Move to same folder?
   1.184 +		if ($remove_action == 1 && $remove_folder_id == $move_to)
   1.185 +		{
   1.186 +			trigger_error('CANNOT_MOVE_TO_SAME_FOLDER');
   1.187 +		}
   1.188 +
   1.189 +		// Select custom folder
   1.190 +		$sql = 'SELECT folder_name, pm_count
   1.191 +			FROM ' . PRIVMSGS_FOLDER_TABLE . "
   1.192 +			WHERE user_id = {$user->data['user_id']}
   1.193 +				AND folder_id = $remove_folder_id";
   1.194 +		$result = $db->sql_query_limit($sql, 1);
   1.195 +		$folder_row = $db->sql_fetchrow($result);
   1.196 +		$db->sql_freeresult($result);
   1.197 +
   1.198 +		if (!$folder_row)
   1.199 +		{
   1.200 +			trigger_error('CANNOT_REMOVE_FOLDER');
   1.201 +		}
   1.202 +
   1.203 +		$s_hidden_fields = array(
   1.204 +			'remove_folder_id'	=> $remove_folder_id,
   1.205 +			'remove_action'		=> $remove_action,
   1.206 +			'move_to'			=> $move_to,
   1.207 +			'remove_folder'		=> 1
   1.208 +		);
   1.209 +
   1.210 +		// Do we need to confirm?
   1.211 +		if (confirm_box(true))
   1.212 +		{
   1.213 +			// Gather message ids
   1.214 +			$sql = 'SELECT msg_id
   1.215 +				FROM ' . PRIVMSGS_TO_TABLE . '
   1.216 +				WHERE user_id = ' . $user->data['user_id'] . "
   1.217 +					AND folder_id = $remove_folder_id";
   1.218 +			$result = $db->sql_query($sql);
   1.219 +
   1.220 +			$msg_ids = array();
   1.221 +			while ($row = $db->sql_fetchrow($result))
   1.222 +			{
   1.223 +				$msg_ids[] = (int) $row['msg_id'];
   1.224 +			}
   1.225 +			$db->sql_freeresult($result);
   1.226 +
   1.227 +			// First of all, copy all messages to another folder... or delete all messages
   1.228 +			switch ($remove_action)
   1.229 +			{
   1.230 +				// Move Messages
   1.231 +				case 1:
   1.232 +					$num_moved = move_pm($user->data['user_id'], $user->data['message_limit'], $msg_ids, $move_to, $remove_folder_id);
   1.233 +					
   1.234 +					// Something went wrong, only partially moved?
   1.235 +					if ($num_moved != $folder_row['pm_count'])
   1.236 +					{
   1.237 +						trigger_error(sprintf($user->lang['MOVE_PM_ERROR'], $num_moved, $folder_row['pm_count']));
   1.238 +					}
   1.239 +				break;
   1.240 +
   1.241 +				// Remove Messages
   1.242 +				case 2:
   1.243 +					delete_pm($user->data['user_id'], $msg_ids, $remove_folder_id);
   1.244 +				break;
   1.245 +			}
   1.246 +
   1.247 +			// Remove folder
   1.248 +			$sql = 'DELETE FROM ' . PRIVMSGS_FOLDER_TABLE . "
   1.249 +				WHERE user_id = {$user->data['user_id']}
   1.250 +					AND folder_id = $remove_folder_id";
   1.251 +			$db->sql_query($sql);
   1.252 +
   1.253 +			// Check full folder option. If the removed folder has been specified as destination switch back to inbox
   1.254 +			if ($user->data['user_full_folder'] == $remove_folder_id)
   1.255 +			{
   1.256 +				$sql = 'UPDATE ' . USERS_TABLE . '
   1.257 +					SET user_full_folder = ' . PRIVMSGS_INBOX . '
   1.258 +					WHERE user_id = ' . $user->data['user_id'];
   1.259 +				$db->sql_query($sql);
   1.260 +
   1.261 +				$user->data['user_full_folder'] = PRIVMSGS_INBOX;
   1.262 +			}
   1.263 +
   1.264 +			// Now make sure the folder is not used for rules
   1.265 +			// We assign another folder id (the one the messages got moved to) or assign the INBOX (to not have to remove any rule)
   1.266 +			$sql = 'UPDATE ' . PRIVMSGS_RULES_TABLE . ' SET rule_folder_id = ';
   1.267 +			$sql .= ($remove_action == 1) ? $move_to : PRIVMSGS_INBOX;
   1.268 +			$sql .= ' WHERE rule_folder_id = ' . $remove_folder_id;
   1.269 +
   1.270 +			$db->sql_query($sql);
   1.271 +
   1.272 +			$meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;mode=$mode");
   1.273 +			$message = $user->lang['FOLDER_REMOVED'];
   1.274 +
   1.275 +			meta_refresh(3, $meta_info);
   1.276 +			$message .= '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>');
   1.277 +			trigger_error($message);
   1.278 +		}
   1.279 +		else
   1.280 +		{
   1.281 +			confirm_box(false, 'REMOVE_FOLDER', build_hidden_fields($s_hidden_fields));
   1.282 +		}
   1.283 +	}
   1.284 +
   1.285 +	// Add Rule
   1.286 +	if (isset($_POST['add_rule']))
   1.287 +	{
   1.288 +		if (check_form_key('ucp_pm_options'))
   1.289 +		{
   1.290 +			$check_option	= request_var('check_option', 0);
   1.291 +			$rule_option	= request_var('rule_option', 0);
   1.292 +			$cond_option	= request_var('cond_option', '');
   1.293 +			$action_option	= explode('|', request_var('action_option', ''));
   1.294 +			$rule_string	= ($cond_option != 'none') ? utf8_normalize_nfc(request_var('rule_string', '', true)) : '';
   1.295 +			$rule_user_id	= ($cond_option != 'none') ? request_var('rule_user_id', 0) : 0;
   1.296 +			$rule_group_id	= ($cond_option != 'none') ? request_var('rule_group_id', 0) : 0;
   1.297 +
   1.298 +			$action = (int) $action_option[0];
   1.299 +			$folder_id = (int) $action_option[1];
   1.300 +
   1.301 +			if (!$action || !$check_option || !$rule_option || !$cond_option || ($cond_option != 'none' && !$rule_string))
   1.302 +			{
   1.303 +				trigger_error('RULE_NOT_DEFINED');
   1.304 +			}
   1.305 +
   1.306 +			if (($cond_option == 'user' && !$rule_user_id) || ($cond_option == 'group' && !$rule_group_id))
   1.307 +			{
   1.308 +				trigger_error('RULE_NOT_DEFINED');
   1.309 +			}
   1.310 +
   1.311 +			$rule_ary = array(
   1.312 +				'user_id'			=> $user->data['user_id'],
   1.313 +				'rule_check'		=> $check_option,
   1.314 +				'rule_connection'	=> $rule_option,
   1.315 +				'rule_string'		=> $rule_string,
   1.316 +				'rule_user_id'		=> $rule_user_id,
   1.317 +				'rule_group_id'		=> $rule_group_id,
   1.318 +				'rule_action'		=> $action,
   1.319 +				'rule_folder_id'	=> $folder_id
   1.320 +			);
   1.321 +
   1.322 +			$sql = 'SELECT rule_id
   1.323 +				FROM ' . PRIVMSGS_RULES_TABLE . '
   1.324 +				WHERE ' . $db->sql_build_array('SELECT', $rule_ary);
   1.325 +			$result = $db->sql_query($sql);
   1.326 +			$row = $db->sql_fetchrow($result);
   1.327 +			$db->sql_freeresult($result);
   1.328 +
   1.329 +			if ($row)
   1.330 +			{
   1.331 +				trigger_error('RULE_ALREADY_DEFINED');
   1.332 +			}
   1.333 +
   1.334 +			$sql = 'INSERT INTO ' . PRIVMSGS_RULES_TABLE . ' ' . $db->sql_build_array('INSERT', $rule_ary);
   1.335 +			$db->sql_query($sql);
   1.336 +
   1.337 +			// Update users message rules
   1.338 +			$sql = 'UPDATE ' . USERS_TABLE . '
   1.339 +				SET user_message_rules = 1
   1.340 +				WHERE user_id = ' . $user->data['user_id'];
   1.341 +			$db->sql_query($sql);
   1.342 +
   1.343 +			$msg = $user->lang['RULE_ADDED'];
   1.344 +		}
   1.345 +		else
   1.346 +		{
   1.347 +			$msg = $user->lang['FORM_INVALID'];
   1.348 +		}
   1.349 +		$message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
   1.350 +		meta_refresh(3, $redirect_url);
   1.351 +		trigger_error($message);
   1.352 +	}
   1.353 +
   1.354 +	// Remove Rule
   1.355 +	if (isset($_POST['delete_rule']) && !isset($_POST['cancel']))
   1.356 +	{
   1.357 +		$delete_id = array_keys(request_var('delete_rule', array(0 => 0)));
   1.358 +		$delete_id = (!empty($delete_id[0])) ? $delete_id[0] : 0;
   1.359 +
   1.360 +		if (!$delete_id)
   1.361 +		{
   1.362 +			redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=' . $mode));
   1.363 +		}
   1.364 +
   1.365 +		// Do we need to confirm?
   1.366 +		if (confirm_box(true))
   1.367 +		{
   1.368 +			$sql = 'DELETE FROM ' . PRIVMSGS_RULES_TABLE . '
   1.369 +				WHERE user_id = ' . $user->data['user_id'] . "
   1.370 +					AND rule_id = $delete_id";
   1.371 +			$db->sql_query($sql);
   1.372 +
   1.373 +			$meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=' . $mode);
   1.374 +			$message = $user->lang['RULE_DELETED'];
   1.375 +
   1.376 +			// Reset user_message_rules if no more assigned
   1.377 +			$sql = 'SELECT rule_id
   1.378 +				FROM ' . PRIVMSGS_RULES_TABLE . '
   1.379 +				WHERE user_id = ' . $user->data['user_id'];
   1.380 +			$result = $db->sql_query_limit($sql, 1);
   1.381 +			$row = $db->sql_fetchrow($result);
   1.382 +			$db->sql_freeresult($result);
   1.383 +
   1.384 +			// Update users message rules
   1.385 +			if (!$row)
   1.386 +			{
   1.387 +				$sql = 'UPDATE ' . USERS_TABLE . '
   1.388 +					SET user_message_rules = 0
   1.389 +					WHERE user_id = ' . $user->data['user_id'];
   1.390 +				$db->sql_query($sql);
   1.391 +			}
   1.392 +
   1.393 +			meta_refresh(3, $meta_info);
   1.394 +			$message .= '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>');
   1.395 +			trigger_error($message);
   1.396 +		}
   1.397 +		else
   1.398 +		{
   1.399 +			confirm_box(false, 'DELETE_RULE', build_hidden_fields(array('delete_rule' => array($delete_id => 1))));
   1.400 +		}
   1.401 +	}
   1.402 +
   1.403 +	$folder = array();
   1.404 +
   1.405 +	$sql = 'SELECT COUNT(msg_id) as num_messages
   1.406 +		FROM ' . PRIVMSGS_TO_TABLE . '
   1.407 +		WHERE user_id = ' . $user->data['user_id'] . '
   1.408 +			AND folder_id = ' . PRIVMSGS_INBOX;
   1.409 +	$result = $db->sql_query($sql);
   1.410 +	$num_messages = (int) $db->sql_fetchfield('num_messages');
   1.411 +	$db->sql_freeresult($result);
   1.412 +	
   1.413 +	$folder[PRIVMSGS_INBOX] = array(
   1.414 +		'folder_name'		=> $user->lang['PM_INBOX'],
   1.415 +		'message_status'	=> sprintf($user->lang['FOLDER_MESSAGE_STATUS'], $num_messages, $user->data['message_limit'])
   1.416 +	);
   1.417 +
   1.418 +	$sql = 'SELECT folder_id, folder_name, pm_count
   1.419 +		FROM ' . PRIVMSGS_FOLDER_TABLE . '
   1.420 +			WHERE user_id = ' . $user->data['user_id'];
   1.421 +	$result = $db->sql_query($sql);
   1.422 +
   1.423 +	$num_user_folder = 0;
   1.424 +	while ($row = $db->sql_fetchrow($result))
   1.425 +	{
   1.426 +		$num_user_folder++;
   1.427 +		$folder[$row['folder_id']] = array(
   1.428 +			'folder_name'		=> $row['folder_name'],
   1.429 +			'message_status'	=> sprintf($user->lang['FOLDER_MESSAGE_STATUS'], $row['pm_count'], $user->data['message_limit'])
   1.430 +		);
   1.431 +	}
   1.432 +	$db->sql_freeresult($result);
   1.433 +
   1.434 +	$s_full_folder_options = $s_to_folder_options = $s_folder_options = '';
   1.435 +
   1.436 +	if ($user->data['user_full_folder'] == FULL_FOLDER_NONE)
   1.437 +	{
   1.438 +		// -3 here to let the correct folder id be selected
   1.439 +		$to_folder_id = $config['full_folder_action'] - 3;
   1.440 +	}
   1.441 +	else
   1.442 +	{
   1.443 +		$to_folder_id = $user->data['user_full_folder'];
   1.444 +	}
   1.445 +
   1.446 +	foreach ($folder as $folder_id => $folder_ary)
   1.447 +	{
   1.448 +		$s_full_folder_options .= '<option value="' . $folder_id . '"' . (($user->data['user_full_folder'] == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
   1.449 +		$s_to_folder_options .= '<option value="' . $folder_id . '"' . (($to_folder_id == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
   1.450 +
   1.451 +		if ($folder_id != PRIVMSGS_INBOX)
   1.452 +		{
   1.453 +			$s_folder_options .= '<option value="' . $folder_id . '">' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
   1.454 +		}
   1.455 +	}
   1.456 +
   1.457 +	$s_delete_checked = ($user->data['user_full_folder'] == FULL_FOLDER_DELETE) ? ' checked="checked"' : '';
   1.458 +	$s_hold_checked = ($user->data['user_full_folder'] == FULL_FOLDER_HOLD) ? ' checked="checked"' : '';
   1.459 +	$s_move_checked = ($user->data['user_full_folder'] >= 0) ? ' checked="checked"' : '';
   1.460 +
   1.461 +	if ($user->data['user_full_folder'] == FULL_FOLDER_NONE)
   1.462 +	{
   1.463 +		switch ($config['full_folder_action'])
   1.464 +		{
   1.465 +			case 1:
   1.466 +				$s_delete_checked = ' checked="checked"';
   1.467 +			break;
   1.468 +
   1.469 +			case 2:
   1.470 +				$s_hold_checked = ' checked="checked"';
   1.471 +			break;
   1.472 +		}
   1.473 +	}
   1.474 +
   1.475 +	$template->assign_vars(array(
   1.476 +		'S_FULL_FOLDER_OPTIONS'	=> $s_full_folder_options,
   1.477 +		'S_TO_FOLDER_OPTIONS'	=> $s_to_folder_options,
   1.478 +		'S_FOLDER_OPTIONS'		=> $s_folder_options,
   1.479 +		'S_DELETE_CHECKED'		=> $s_delete_checked,
   1.480 +		'S_HOLD_CHECKED'		=> $s_hold_checked,
   1.481 +		'S_MOVE_CHECKED'		=> $s_move_checked,
   1.482 +		'S_MAX_FOLDER_REACHED'	=> ($num_user_folder >= $config['pm_max_boxes']) ? true : false,
   1.483 +		'S_MAX_FOLDER_ZERO'		=> ($config['pm_max_boxes'] == 0) ? true : false,
   1.484 +
   1.485 +		'DEFAULT_ACTION'		=> ($config['full_folder_action'] == 1) ? $user->lang['DELETE_OLDEST_MESSAGES'] : $user->lang['HOLD_NEW_MESSAGES'],
   1.486 +
   1.487 +		'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=ucp&amp;field=rule_string&amp;select_single=true'),
   1.488 +	));
   1.489 +
   1.490 +	$rule_lang = $action_lang = $check_lang = array();
   1.491 +
   1.492 +	// Build all three language arrays
   1.493 +	preg_replace('#^((RULE|ACTION|CHECK)_([A-Z0-9_]+))$#e', "\${strtolower('\\2') . '_lang'}[constant('\\1')] = \$user->lang['PM_\\2']['\\3']", array_keys(get_defined_constants()));
   1.494 +
   1.495 +	/*
   1.496 +		Rule Ordering:
   1.497 +			-> CHECK_* -> RULE_* [IN $global_privmsgs_rules:CHECK_*] -> [IF $rule_conditions[RULE_*] [|text|bool|user|group|own_group]] -> ACTION_*
   1.498 +	*/
   1.499 +
   1.500 +	$check_option	= request_var('check_option', 0);
   1.501 +	$rule_option	= request_var('rule_option', 0);
   1.502 +	$cond_option	= request_var('cond_option', '');
   1.503 +	$action_option	= request_var('action_option', '');
   1.504 +	$back = (isset($_REQUEST['back'])) ? request_var('back', array('' => 0)) : array();
   1.505 +
   1.506 +	if (sizeof($back))
   1.507 +	{
   1.508 +		if ($action_option)
   1.509 +		{
   1.510 +			$action_option = '';
   1.511 +		}
   1.512 +		else if ($cond_option)
   1.513 +		{
   1.514 +			$cond_option = '';
   1.515 +		}
   1.516 +		else if ($rule_option)
   1.517 +		{
   1.518 +			$rule_option = 0;
   1.519 +		}
   1.520 +		else if ($check_option)
   1.521 +		{
   1.522 +			$check_option = 0;
   1.523 +		}
   1.524 +	}
   1.525 +
   1.526 +	if (isset($back['action']) && $cond_option == 'none')
   1.527 +	{
   1.528 +		$back['cond'] = true;
   1.529 +	}
   1.530 +
   1.531 +	// Check
   1.532 +	if (!isset($global_privmsgs_rules[$check_option]))
   1.533 +	{
   1.534 +		$check_option = 0;
   1.535 +	}
   1.536 +
   1.537 +	define_check_option(($check_option && !isset($back['rule'])) ? true : false, $check_option, $check_lang);
   1.538 +
   1.539 +	if ($check_option && !isset($back['rule']))
   1.540 +	{
   1.541 +		define_rule_option(($rule_option && !isset($back['cond'])) ? true : false, $rule_option, $rule_lang, $global_privmsgs_rules[$check_option]);
   1.542 +	}
   1.543 +
   1.544 +	if ($rule_option && !isset($back['cond']))
   1.545 +	{
   1.546 +		if (!isset($global_rule_conditions[$rule_option]))
   1.547 +		{
   1.548 +			$cond_option = 'none';
   1.549 +			$template->assign_var('NONE_CONDITION', true);
   1.550 +		}
   1.551 +		else
   1.552 +		{
   1.553 +			define_cond_option(($cond_option && !isset($back['action'])) ? true : false, $cond_option, $rule_option, $global_rule_conditions);
   1.554 +		}
   1.555 +	}
   1.556 +
   1.557 +	if ($cond_option && !isset($back['action']))
   1.558 +	{
   1.559 +		define_action_option(false, $action_option, $action_lang, $folder);
   1.560 +	}
   1.561 +
   1.562 +	show_defined_rules($user->data['user_id'], $check_lang, $rule_lang, $action_lang, $folder);
   1.563 +}
   1.564 +
   1.565 +/**
   1.566 +* Defining check option for message rules
   1.567 +*/
   1.568 +function define_check_option($hardcoded, $check_option, $check_lang)
   1.569 +{
   1.570 +	global $template;
   1.571 +
   1.572 +	$s_check_options = '';
   1.573 +	if (!$hardcoded)
   1.574 +	{
   1.575 +		foreach ($check_lang as $value => $lang)
   1.576 +		{
   1.577 +			$s_check_options .= '<option value="' . $value . '"' . (($value == $check_option) ? ' selected="selected"' : '') . '>' . $lang . '</option>';
   1.578 +		}
   1.579 +	}
   1.580 +
   1.581 +	$template->assign_vars(array(
   1.582 +		'S_CHECK_DEFINED'	=> true,
   1.583 +		'S_CHECK_SELECT'	=> ($hardcoded) ? false : true,
   1.584 +		'CHECK_CURRENT'		=> isset($check_lang[$check_option]) ? $check_lang[$check_option] : '',
   1.585 +		'S_CHECK_OPTIONS'	=> $s_check_options,
   1.586 +		'CHECK_OPTION'		=> $check_option)
   1.587 +	);
   1.588 +}
   1.589 +
   1.590 +/**
   1.591 +* Defining action option for message rules
   1.592 +*/
   1.593 +function define_action_option($hardcoded, $action_option, $action_lang, $folder)
   1.594 +{
   1.595 +	global $db, $template, $user;
   1.596 +
   1.597 +	$l_action = $s_action_options = '';
   1.598 +	if ($hardcoded)
   1.599 +	{
   1.600 +		$option = explode('|', $action_option);
   1.601 +		$action = (int) $option[0];
   1.602 +		$folder_id = (int) $option[1];
   1.603 +
   1.604 +		$l_action = $action_lang[$action];
   1.605 +		if ($action == ACTION_PLACE_INTO_FOLDER)
   1.606 +		{
   1.607 +			$l_action .= ' -> ' . $folder[$folder_id]['folder_name'];
   1.608 +		}
   1.609 +	}
   1.610 +	else
   1.611 +	{
   1.612 +		foreach ($action_lang as $action => $lang)
   1.613 +		{
   1.614 +			if ($action == ACTION_PLACE_INTO_FOLDER)
   1.615 +			{
   1.616 +				foreach ($folder as $folder_id => $folder_ary)
   1.617 +				{
   1.618 +					$s_action_options .= '<option value="' . $action . '|' . $folder_id . '"' . (($action_option == $action . '|' . $folder_id) ? ' selected="selected"' : '') . '>' . $lang . ' -> ' . $folder_ary['folder_name'] . '</option>';
   1.619 +				}
   1.620 +			}
   1.621 +			else
   1.622 +			{
   1.623 +				$s_action_options .= '<option value="' . $action . '|0"' . (($action_option == $action . '|0') ? ' selected="selected"' : '') . '>' . $lang . '</option>';
   1.624 +			}
   1.625 +		}
   1.626 +	}
   1.627 +
   1.628 +	$template->assign_vars(array(
   1.629 +		'S_ACTION_DEFINED'	=> true,
   1.630 +		'S_ACTION_SELECT'	=> ($hardcoded) ? false : true,
   1.631 +		'ACTION_CURRENT'	=> $l_action,
   1.632 +		'S_ACTION_OPTIONS'	=> $s_action_options,
   1.633 +		'ACTION_OPTION'		=> $action_option)
   1.634 +	);
   1.635 +}
   1.636 +
   1.637 +/**
   1.638 +* Defining rule option for message rules
   1.639 +*/
   1.640 +function define_rule_option($hardcoded, $rule_option, $rule_lang, $check_ary)
   1.641 +{
   1.642 +	global $template;
   1.643 +	global $module;
   1.644 +
   1.645 +	$exclude = array();
   1.646 +
   1.647 +	if (!$module->loaded('zebra', 'friends'))
   1.648 +	{
   1.649 +		$exclude[RULE_IS_FRIEND] = true;
   1.650 +	}
   1.651 +
   1.652 +	if (!$module->loaded('zebra', 'foes'))
   1.653 +	{
   1.654 +		$exclude[RULE_IS_FOE] = true;
   1.655 +	}
   1.656 +
   1.657 +	$s_rule_options = '';
   1.658 +	if (!$hardcoded)
   1.659 +	{
   1.660 +		foreach ($check_ary as $value => $_check)
   1.661 +		{
   1.662 +			if (isset($exclude[$value]))
   1.663 +			{
   1.664 +				continue;
   1.665 +			}
   1.666 +			$s_rule_options .= '<option value="' . $value . '"' . (($value == $rule_option) ? ' selected="selected"' : '') . '>' . $rule_lang[$value] . '</option>';
   1.667 +		}
   1.668 +	}
   1.669 +
   1.670 +	$template->assign_vars(array(
   1.671 +		'S_RULE_DEFINED'	=> true,
   1.672 +		'S_RULE_SELECT'		=> !$hardcoded,
   1.673 +		'RULE_CURRENT'		=> isset($rule_lang[$rule_option]) ? $rule_lang[$rule_option] : '',
   1.674 +		'S_RULE_OPTIONS'	=> $s_rule_options,
   1.675 +		'RULE_OPTION'		=> $rule_option)
   1.676 +	);
   1.677 +}
   1.678 +
   1.679 +/**
   1.680 +* Defining condition option for message rules
   1.681 +*/
   1.682 +function define_cond_option($hardcoded, $cond_option, $rule_option, $global_rule_conditions)
   1.683 +{
   1.684 +	global $db, $template, $auth, $user;
   1.685 +	
   1.686 +	$template->assign_vars(array(
   1.687 +		'S_COND_DEFINED'	=> true,
   1.688 +		'S_COND_SELECT'		=> (!$hardcoded && isset($global_rule_conditions[$rule_option])) ? true : false)
   1.689 +	);
   1.690 +
   1.691 +	// Define COND_OPTION
   1.692 +	if (!isset($global_rule_conditions[$rule_option]))
   1.693 +	{
   1.694 +		$template->assign_vars(array(
   1.695 +			'COND_OPTION'	=> 'none',
   1.696 +			'COND_CURRENT'	=> false)
   1.697 +		);
   1.698 +		return;
   1.699 +	}
   1.700 +
   1.701 +	// Define Condition
   1.702 +	$condition = $global_rule_conditions[$rule_option];
   1.703 +	$current_value = '';
   1.704 +
   1.705 +	switch ($condition)
   1.706 +	{
   1.707 +		case 'text':
   1.708 +			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
   1.709 +			
   1.710 +			$template->assign_vars(array(
   1.711 +				'S_TEXT_CONDITION'	=> true,
   1.712 +				'CURRENT_STRING'	=> $rule_string,
   1.713 +				'CURRENT_USER_ID'	=> 0,
   1.714 +				'CURRENT_GROUP_ID'	=> 0)
   1.715 +			);
   1.716 +
   1.717 +			$current_value = $rule_string;
   1.718 +		break;
   1.719 +
   1.720 +		case 'user':
   1.721 +			$rule_user_id = request_var('rule_user_id', 0);
   1.722 +			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
   1.723 +			
   1.724 +			if ($rule_string && !$rule_user_id)
   1.725 +			{
   1.726 +				$sql = 'SELECT user_id
   1.727 +					FROM ' . USERS_TABLE . "
   1.728 +					WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($rule_string)) . "'";
   1.729 +				$result = $db->sql_query($sql);
   1.730 +				$rule_user_id = (int) $db->sql_fetchfield('user_id');
   1.731 +				$db->sql_freeresult($result);
   1.732 +
   1.733 +				if (!$rule_user_id)
   1.734 +				{
   1.735 +					$rule_string = '';
   1.736 +				}
   1.737 +			}
   1.738 +			else if (!$rule_string && $rule_user_id)
   1.739 +			{
   1.740 +				$sql = 'SELECT username
   1.741 +					FROM ' . USERS_TABLE . "
   1.742 +					WHERE user_id = $rule_user_id";
   1.743 +				$result = $db->sql_query($sql);
   1.744 +				$rule_string = $db->sql_fetchfield('username');
   1.745 +				$db->sql_freeresult($result);
   1.746 +
   1.747 +				if (!$rule_string)
   1.748 +				{
   1.749 +					$rule_user_id = 0;
   1.750 +				}
   1.751 +			}
   1.752 +
   1.753 +			$template->assign_vars(array(
   1.754 +				'S_USER_CONDITION'	=> true,
   1.755 +				'CURRENT_STRING'	=> $rule_string,
   1.756 +				'CURRENT_USER_ID'	=> $rule_user_id,
   1.757 +				'CURRENT_GROUP_ID'	=> 0)
   1.758 +			);
   1.759 +
   1.760 +			$current_value = $rule_string;
   1.761 +		break;
   1.762 +
   1.763 +		case 'group':
   1.764 +			$rule_group_id = request_var('rule_group_id', 0);
   1.765 +			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
   1.766 +
   1.767 +			$sql = 'SELECT g.group_id, g.group_name, g.group_type
   1.768 +					FROM ' . GROUPS_TABLE . ' g ';
   1.769 +
   1.770 +			if (!$auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
   1.771 +			{
   1.772 +				$sql .= 'LEFT JOIN ' . USER_GROUP_TABLE . ' ug
   1.773 +					ON (
   1.774 +						g.group_id = ug.group_id
   1.775 +						AND ug.user_id = ' . $user->data['user_id'] . '
   1.776 +						AND ug.user_pending = 0
   1.777 +					)
   1.778 +					WHERE (ug.user_id = ' . $user->data['user_id'] . ' OR g.group_type <> ' . GROUP_HIDDEN . ')
   1.779 +					AND';
   1.780 +			}
   1.781 +			else
   1.782 +			{
   1.783 +				$sql .= 'WHERE';
   1.784 +			}
   1.785 +			
   1.786 +			$sql .= " (g.group_name NOT IN ('GUESTS', 'BOTS') OR g.group_type <> " . GROUP_SPECIAL . ')	
   1.787 +				ORDER BY g.group_type DESC, g.group_name ASC';
   1.788 +			
   1.789 +			$result = $db->sql_query($sql);
   1.790 +
   1.791 +			$s_group_options = '';
   1.792 +			while ($row = $db->sql_fetchrow($result))
   1.793 +			{
   1.794 +				if ($rule_group_id && ($row['group_id'] == $rule_group_id))
   1.795 +				{
   1.796 +					$rule_string = (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']);
   1.797 +				}
   1.798 +
   1.799 +				$s_class	= ($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '';
   1.800 +				$s_selected	= ($row['group_id'] == $rule_group_id) ? ' selected="selected"' : '';
   1.801 +				
   1.802 +				$s_group_options .= '<option value="' . $row['group_id'] . '"' . $s_class . $s_selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
   1.803 +			}
   1.804 +			$db->sql_freeresult($result);
   1.805 +
   1.806 +			$template->assign_vars(array(
   1.807 +				'S_GROUP_CONDITION'	=> true,
   1.808 +				'S_GROUP_OPTIONS'	=> $s_group_options,
   1.809 +				'CURRENT_STRING'	=> $rule_string,
   1.810 +				'CURRENT_USER_ID'	=> 0,
   1.811 +				'CURRENT_GROUP_ID'	=> $rule_group_id)
   1.812 +			);
   1.813 +
   1.814 +			$current_value = $rule_string;
   1.815 +		break;
   1.816 +
   1.817 +		default:
   1.818 +			return;
   1.819 +	}
   1.820 +
   1.821 +	$template->assign_vars(array(
   1.822 +		'COND_OPTION'	=> $condition,
   1.823 +		'COND_CURRENT'	=> $current_value)
   1.824 +	);
   1.825 +}
   1.826 +
   1.827 +/**
   1.828 +* Display defined message rules
   1.829 +*/
   1.830 +function show_defined_rules($user_id, $check_lang, $rule_lang, $action_lang, $folder)
   1.831 +{
   1.832 +	global $db, $template;
   1.833 +
   1.834 +	$sql = 'SELECT *
   1.835 +		FROM ' . PRIVMSGS_RULES_TABLE . '
   1.836 +		WHERE user_id = ' . $user_id . '
   1.837 +		ORDER BY rule_id ASC';
   1.838 +	$result = $db->sql_query($sql);
   1.839 +	
   1.840 +	$count = 0;
   1.841 +	while ($row = $db->sql_fetchrow($result))
   1.842 +	{
   1.843 +		$template->assign_block_vars('rule', array(
   1.844 +			'COUNT'		=> ++$count,
   1.845 +			'RULE_ID'	=> $row['rule_id'],
   1.846 +			'CHECK'		=> $check_lang[$row['rule_check']],
   1.847 +			'RULE'		=> $rule_lang[$row['rule_connection']],
   1.848 +			'STRING'	=> $row['rule_string'],
   1.849 +			'ACTION'	=> $action_lang[$row['rule_action']],
   1.850 +			'FOLDER'	=> ($row['rule_action'] == ACTION_PLACE_INTO_FOLDER) ? $folder[$row['rule_folder_id']]['folder_name'] : '')
   1.851 +		);
   1.852 +	}
   1.853 +	$db->sql_freeresult($result);
   1.854 +}
   1.855 +
   1.856 +?>
   1.857 \ No newline at end of file