Blame Extras/phpBB/3.0.4/includes/ucp/ucp_pm_options.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package ucp
4c79b5
* @version $Id: ucp_pm_options.php 8479 2008-03-29 00:22:48Z naderman $
4c79b5
* @copyright (c) 2005 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Execute message options
4c79b5
*/
4c79b5
function message_options($id, $mode, $global_privmsgs_rules, $global_rule_conditions)
4c79b5
{
4c79b5
	global $phpbb_root_path, $phpEx, $user, $template, $auth, $config, $db;
4c79b5
4c79b5
	$redirect_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=options");
4c79b5
4c79b5
	add_form_key('ucp_pm_options');
4c79b5
	// Change "full folder" setting - what to do if folder is full
4c79b5
	if (isset($_POST['fullfolder']))
4c79b5
	{
4c79b5
		check_form_key('ucp_pm_options', $config['form_token_lifetime'], $redirect_url);
4c79b5
		$full_action = request_var('full_action', 0);
4c79b5
4c79b5
		$set_folder_id = 0;
4c79b5
		switch ($full_action)
4c79b5
		{
4c79b5
			case 1:
4c79b5
				$set_folder_id = FULL_FOLDER_DELETE;
4c79b5
			break;
4c79b5
4c79b5
			case 2:
4c79b5
				$set_folder_id = request_var('full_move_to', PRIVMSGS_INBOX);
4c79b5
			break;
4c79b5
4c79b5
			case 3:
4c79b5
				$set_folder_id = FULL_FOLDER_HOLD;
4c79b5
			break;
4c79b5
4c79b5
			default:
4c79b5
				$full_action = 0;
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		if ($full_action)
4c79b5
		{
4c79b5
			$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
				SET user_full_folder = ' . $set_folder_id . '
4c79b5
				WHERE user_id = ' . $user->data['user_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
4c79b5
			$user->data['user_full_folder'] = $set_folder_id;
4c79b5
4c79b5
			$message = $user->lang['FULL_FOLDER_OPTION_CHANGED'] . '

' . sprintf($user->lang['RETURN_UCP'], '', '');
4c79b5
			meta_refresh(3, $redirect_url);
4c79b5
			trigger_error($message);
4c79b5
		}
4c79b5
	}
4c79b5
	
4c79b5
	// Add Folder
4c79b5
	if (isset($_POST['addfolder']))
4c79b5
	{
4c79b5
		if (check_form_key('ucp_pm_options'))
4c79b5
		{
4c79b5
			$folder_name = utf8_normalize_nfc(request_var('foldername', '', true));
4c79b5
			$msg = '';
4c79b5
4c79b5
			if ($folder_name)
4c79b5
			{
4c79b5
				$sql = 'SELECT folder_name
4c79b5
					FROM ' . PRIVMSGS_FOLDER_TABLE . "
4c79b5
					WHERE folder_name = '" . $db->sql_escape($folder_name) . "'
4c79b5
						AND user_id = " . $user->data['user_id'];
4c79b5
				$result = $db->sql_query_limit($sql, 1);
4c79b5
				$row = $db->sql_fetchrow($result);
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if ($row)
4c79b5
				{
4c79b5
					trigger_error(sprintf($user->lang['FOLDER_NAME_EXIST'], $folder_name));
4c79b5
				}
4c79b5
4c79b5
				$sql = 'SELECT COUNT(folder_id) as num_folder
4c79b5
					FROM ' . PRIVMSGS_FOLDER_TABLE . '
4c79b5
						WHERE user_id = ' . $user->data['user_id'];
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$num_folder = (int) $db->sql_fetchfield('num_folder');
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if ($num_folder >= $config['pm_max_boxes'])
4c79b5
				{
4c79b5
					trigger_error('MAX_FOLDER_REACHED');
4c79b5
				}
4c79b5
4c79b5
				$sql = 'INSERT INTO ' . PRIVMSGS_FOLDER_TABLE . ' ' . $db->sql_build_array('INSERT', array(
4c79b5
					'user_id'		=> (int) $user->data['user_id'],
4c79b5
					'folder_name'	=> $folder_name)
4c79b5
				);
4c79b5
				$db->sql_query($sql);
4c79b5
				$msg = $user->lang['FOLDER_ADDED'];
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$msg = $user->lang['FORM_INVALID'];
4c79b5
		}
4c79b5
		$message = $msg . '

' . sprintf($user->lang['RETURN_UCP'], '', '');
4c79b5
		meta_refresh(3, $redirect_url);
4c79b5
		trigger_error($message);
4c79b5
	}
4c79b5
4c79b5
	// Rename folder
4c79b5
	if (isset($_POST['rename_folder']))
4c79b5
	{
4c79b5
		if (check_form_key('ucp_pm_options'))
4c79b5
		{
4c79b5
			$new_folder_name = utf8_normalize_nfc(request_var('new_folder_name', '', true));
4c79b5
			$rename_folder_id= request_var('rename_folder_id', 0);
4c79b5
4c79b5
			if (!$new_folder_name)
4c79b5
			{
4c79b5
				trigger_error('NO_NEW_FOLDER_NAME');
4c79b5
			}
4c79b5
4c79b5
			// Select custom folder
4c79b5
			$sql = 'SELECT folder_name, pm_count
4c79b5
				FROM ' . PRIVMSGS_FOLDER_TABLE . "
4c79b5
				WHERE user_id = {$user->data['user_id']}
4c79b5
					AND folder_id = $rename_folder_id";
4c79b5
			$result = $db->sql_query_limit($sql, 1);
4c79b5
			$folder_row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			if (!$folder_row)
4c79b5
			{
4c79b5
				trigger_error('CANNOT_RENAME_FOLDER');
4c79b5
			}
4c79b5
4c79b5
			$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
4c79b5
				SET folder_name = '" . $db->sql_escape($new_folder_name) . "'
4c79b5
				WHERE folder_id = $rename_folder_id
4c79b5
					AND user_id = {$user->data['user_id']}";
4c79b5
			$db->sql_query($sql);
4c79b5
			$msg = $user->lang['FOLDER_RENAMED'];
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$msg = $user->lang['FORM_INVALID'];
4c79b5
		}
4c79b5
4c79b5
		$message = $msg . '

' . sprintf($user->lang['RETURN_UCP'], '', '');
4c79b5
4c79b5
		meta_refresh(3, $redirect_url);
4c79b5
		trigger_error($message);
4c79b5
	}
4c79b5
4c79b5
	// Remove Folder
4c79b5
	if (isset($_POST['remove_folder']))
4c79b5
	{
4c79b5
		$remove_folder_id = request_var('remove_folder_id', 0);
4c79b5
4c79b5
		// Default to "move all messages to inbox"
4c79b5
		$remove_action = request_var('remove_action', 1);
4c79b5
		$move_to = request_var('move_to', PRIVMSGS_INBOX);
4c79b5
4c79b5
		// Move to same folder?
4c79b5
		if ($remove_action == 1 && $remove_folder_id == $move_to)
4c79b5
		{
4c79b5
			trigger_error('CANNOT_MOVE_TO_SAME_FOLDER');
4c79b5
		}
4c79b5
4c79b5
		// Select custom folder
4c79b5
		$sql = 'SELECT folder_name, pm_count
4c79b5
			FROM ' . PRIVMSGS_FOLDER_TABLE . "
4c79b5
			WHERE user_id = {$user->data['user_id']}
4c79b5
				AND folder_id = $remove_folder_id";
4c79b5
		$result = $db->sql_query_limit($sql, 1);
4c79b5
		$folder_row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		if (!$folder_row)
4c79b5
		{
4c79b5
			trigger_error('CANNOT_REMOVE_FOLDER');
4c79b5
		}
4c79b5
4c79b5
		$s_hidden_fields = array(
4c79b5
			'remove_folder_id'	=> $remove_folder_id,
4c79b5
			'remove_action'		=> $remove_action,
4c79b5
			'move_to'			=> $move_to,
4c79b5
			'remove_folder'		=> 1
4c79b5
		);
4c79b5
4c79b5
		// Do we need to confirm?
4c79b5
		if (confirm_box(true))
4c79b5
		{
4c79b5
			// Gather message ids
4c79b5
			$sql = 'SELECT msg_id
4c79b5
				FROM ' . PRIVMSGS_TO_TABLE . '
4c79b5
				WHERE user_id = ' . $user->data['user_id'] . "
4c79b5
					AND folder_id = $remove_folder_id";
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			$msg_ids = array();
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$msg_ids[] = (int) $row['msg_id'];
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			// First of all, copy all messages to another folder... or delete all messages
4c79b5
			switch ($remove_action)
4c79b5
			{
4c79b5
				// Move Messages
4c79b5
				case 1:
4c79b5
					$num_moved = move_pm($user->data['user_id'], $user->data['message_limit'], $msg_ids, $move_to, $remove_folder_id);
4c79b5
					
4c79b5
					// Something went wrong, only partially moved?
4c79b5
					if ($num_moved != $folder_row['pm_count'])
4c79b5
					{
4c79b5
						trigger_error(sprintf($user->lang['MOVE_PM_ERROR'], $num_moved, $folder_row['pm_count']));
4c79b5
					}
4c79b5
				break;
4c79b5
4c79b5
				// Remove Messages
4c79b5
				case 2:
4c79b5
					delete_pm($user->data['user_id'], $msg_ids, $remove_folder_id);
4c79b5
				break;
4c79b5
			}
4c79b5
4c79b5
			// Remove folder
4c79b5
			$sql = 'DELETE FROM ' . PRIVMSGS_FOLDER_TABLE . "
4c79b5
				WHERE user_id = {$user->data['user_id']}
4c79b5
					AND folder_id = $remove_folder_id";
4c79b5
			$db->sql_query($sql);
4c79b5
4c79b5
			// Check full folder option. If the removed folder has been specified as destination switch back to inbox
4c79b5
			if ($user->data['user_full_folder'] == $remove_folder_id)
4c79b5
			{
4c79b5
				$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
					SET user_full_folder = ' . PRIVMSGS_INBOX . '
4c79b5
					WHERE user_id = ' . $user->data['user_id'];
4c79b5
				$db->sql_query($sql);
4c79b5
4c79b5
				$user->data['user_full_folder'] = PRIVMSGS_INBOX;
4c79b5
			}
4c79b5
4c79b5
			// Now make sure the folder is not used for rules
4c79b5
			// We assign another folder id (the one the messages got moved to) or assign the INBOX (to not have to remove any rule)
4c79b5
			$sql = 'UPDATE ' . PRIVMSGS_RULES_TABLE . ' SET rule_folder_id = ';
4c79b5
			$sql .= ($remove_action == 1) ? $move_to : PRIVMSGS_INBOX;
4c79b5
			$sql .= ' WHERE rule_folder_id = ' . $remove_folder_id;
4c79b5
4c79b5
			$db->sql_query($sql);
4c79b5
4c79b5
			$meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=$mode");
4c79b5
			$message = $user->lang['FOLDER_REMOVED'];
4c79b5
4c79b5
			meta_refresh(3, $meta_info);
4c79b5
			$message .= '

' . sprintf($user->lang['RETURN_UCP'], '', '');
4c79b5
			trigger_error($message);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			confirm_box(false, 'REMOVE_FOLDER', build_hidden_fields($s_hidden_fields));
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Add Rule
4c79b5
	if (isset($_POST['add_rule']))
4c79b5
	{
4c79b5
		if (check_form_key('ucp_pm_options'))
4c79b5
		{
4c79b5
			$check_option	= request_var('check_option', 0);
4c79b5
			$rule_option	= request_var('rule_option', 0);
4c79b5
			$cond_option	= request_var('cond_option', '');
4c79b5
			$action_option	= explode('|', request_var('action_option', ''));
4c79b5
			$rule_string	= ($cond_option != 'none') ? utf8_normalize_nfc(request_var('rule_string', '', true)) : '';
4c79b5
			$rule_user_id	= ($cond_option != 'none') ? request_var('rule_user_id', 0) : 0;
4c79b5
			$rule_group_id	= ($cond_option != 'none') ? request_var('rule_group_id', 0) : 0;
4c79b5
4c79b5
			$action = (int) $action_option[0];
4c79b5
			$folder_id = (int) $action_option[1];
4c79b5
4c79b5
			if (!$action || !$check_option || !$rule_option || !$cond_option || ($cond_option != 'none' && !$rule_string))
4c79b5
			{
4c79b5
				trigger_error('RULE_NOT_DEFINED');
4c79b5
			}
4c79b5
4c79b5
			if (($cond_option == 'user' && !$rule_user_id) || ($cond_option == 'group' && !$rule_group_id))
4c79b5
			{
4c79b5
				trigger_error('RULE_NOT_DEFINED');
4c79b5
			}
4c79b5
4c79b5
			$rule_ary = array(
4c79b5
				'user_id'			=> $user->data['user_id'],
4c79b5
				'rule_check'		=> $check_option,
4c79b5
				'rule_connection'	=> $rule_option,
4c79b5
				'rule_string'		=> $rule_string,
4c79b5
				'rule_user_id'		=> $rule_user_id,
4c79b5
				'rule_group_id'		=> $rule_group_id,
4c79b5
				'rule_action'		=> $action,
4c79b5
				'rule_folder_id'	=> $folder_id
4c79b5
			);
4c79b5
4c79b5
			$sql = 'SELECT rule_id
4c79b5
				FROM ' . PRIVMSGS_RULES_TABLE . '
4c79b5
				WHERE ' . $db->sql_build_array('SELECT', $rule_ary);
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			if ($row)
4c79b5
			{
4c79b5
				trigger_error('RULE_ALREADY_DEFINED');
4c79b5
			}
4c79b5
4c79b5
			$sql = 'INSERT INTO ' . PRIVMSGS_RULES_TABLE . ' ' . $db->sql_build_array('INSERT', $rule_ary);
4c79b5
			$db->sql_query($sql);
4c79b5
4c79b5
			// Update users message rules
4c79b5
			$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
				SET user_message_rules = 1
4c79b5
				WHERE user_id = ' . $user->data['user_id'];
4c79b5
			$db->sql_query($sql);
4c79b5
4c79b5
			$msg = $user->lang['RULE_ADDED'];
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$msg = $user->lang['FORM_INVALID'];
4c79b5
		}
4c79b5
		$message = $msg . '

' . sprintf($user->lang['RETURN_UCP'], '', '');
4c79b5
		meta_refresh(3, $redirect_url);
4c79b5
		trigger_error($message);
4c79b5
	}
4c79b5
4c79b5
	// Remove Rule
4c79b5
	if (isset($_POST['delete_rule']) && !isset($_POST['cancel']))
4c79b5
	{
4c79b5
		$delete_id = array_keys(request_var('delete_rule', array(0 => 0)));
4c79b5
		$delete_id = (!empty($delete_id[0])) ? $delete_id[0] : 0;
4c79b5
4c79b5
		if (!$delete_id)
4c79b5
		{
4c79b5
			redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=' . $mode));
4c79b5
		}
4c79b5
4c79b5
		// Do we need to confirm?
4c79b5
		if (confirm_box(true))
4c79b5
		{
4c79b5
			$sql = 'DELETE FROM ' . PRIVMSGS_RULES_TABLE . '
4c79b5
				WHERE user_id = ' . $user->data['user_id'] . "
4c79b5
					AND rule_id = $delete_id";
4c79b5
			$db->sql_query($sql);
4c79b5
4c79b5
			$meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=' . $mode);
4c79b5
			$message = $user->lang['RULE_DELETED'];
4c79b5
4c79b5
			// Reset user_message_rules if no more assigned
4c79b5
			$sql = 'SELECT rule_id
4c79b5
				FROM ' . PRIVMSGS_RULES_TABLE . '
4c79b5
				WHERE user_id = ' . $user->data['user_id'];
4c79b5
			$result = $db->sql_query_limit($sql, 1);
4c79b5
			$row = $db->sql_fetchrow($result);
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			// Update users message rules
4c79b5
			if (!$row)
4c79b5
			{
4c79b5
				$sql = 'UPDATE ' . USERS_TABLE . '
4c79b5
					SET user_message_rules = 0
4c79b5
					WHERE user_id = ' . $user->data['user_id'];
4c79b5
				$db->sql_query($sql);
4c79b5
			}
4c79b5
4c79b5
			meta_refresh(3, $meta_info);
4c79b5
			$message .= '

' . sprintf($user->lang['RETURN_UCP'], '', '');
4c79b5
			trigger_error($message);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			confirm_box(false, 'DELETE_RULE', build_hidden_fields(array('delete_rule' => array($delete_id => 1))));
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	$folder = array();
4c79b5
4c79b5
	$sql = 'SELECT COUNT(msg_id) as num_messages
4c79b5
		FROM ' . PRIVMSGS_TO_TABLE . '
4c79b5
		WHERE user_id = ' . $user->data['user_id'] . '
4c79b5
			AND folder_id = ' . PRIVMSGS_INBOX;
4c79b5
	$result = $db->sql_query($sql);
4c79b5
	$num_messages = (int) $db->sql_fetchfield('num_messages');
4c79b5
	$db->sql_freeresult($result);
4c79b5
	
4c79b5
	$folder[PRIVMSGS_INBOX] = array(
4c79b5
		'folder_name'		=> $user->lang['PM_INBOX'],
4c79b5
		'message_status'	=> sprintf($user->lang['FOLDER_MESSAGE_STATUS'], $num_messages, $user->data['message_limit'])
4c79b5
	);
4c79b5
4c79b5
	$sql = 'SELECT folder_id, folder_name, pm_count
4c79b5
		FROM ' . PRIVMSGS_FOLDER_TABLE . '
4c79b5
			WHERE user_id = ' . $user->data['user_id'];
4c79b5
	$result = $db->sql_query($sql);
4c79b5
4c79b5
	$num_user_folder = 0;
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$num_user_folder++;
4c79b5
		$folder[$row['folder_id']] = array(
4c79b5
			'folder_name'		=> $row['folder_name'],
4c79b5
			'message_status'	=> sprintf($user->lang['FOLDER_MESSAGE_STATUS'], $row['pm_count'], $user->data['message_limit'])
4c79b5
		);
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	$s_full_folder_options = $s_to_folder_options = $s_folder_options = '';
4c79b5
4c79b5
	if ($user->data['user_full_folder'] == FULL_FOLDER_NONE)
4c79b5
	{
4c79b5
		// -3 here to let the correct folder id be selected
4c79b5
		$to_folder_id = $config['full_folder_action'] - 3;
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$to_folder_id = $user->data['user_full_folder'];
4c79b5
	}
4c79b5
4c79b5
	foreach ($folder as $folder_id => $folder_ary)
4c79b5
	{
4c79b5
		$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>';
4c79b5
		$s_to_folder_options .= '<option value="' . $folder_id . '"' . (($to_folder_id == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
4c79b5
4c79b5
		if ($folder_id != PRIVMSGS_INBOX)
4c79b5
		{
4c79b5
			$s_folder_options .= '<option value="' . $folder_id . '">' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	$s_delete_checked = ($user->data['user_full_folder'] == FULL_FOLDER_DELETE) ? ' checked="checked"' : '';
4c79b5
	$s_hold_checked = ($user->data['user_full_folder'] == FULL_FOLDER_HOLD) ? ' checked="checked"' : '';
4c79b5
	$s_move_checked = ($user->data['user_full_folder'] >= 0) ? ' checked="checked"' : '';
4c79b5
4c79b5
	if ($user->data['user_full_folder'] == FULL_FOLDER_NONE)
4c79b5
	{
4c79b5
		switch ($config['full_folder_action'])
4c79b5
		{
4c79b5
			case 1:
4c79b5
				$s_delete_checked = ' checked="checked"';
4c79b5
			break;
4c79b5
4c79b5
			case 2:
4c79b5
				$s_hold_checked = ' checked="checked"';
4c79b5
			break;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	$template->assign_vars(array(
4c79b5
		'S_FULL_FOLDER_OPTIONS'	=> $s_full_folder_options,
4c79b5
		'S_TO_FOLDER_OPTIONS'	=> $s_to_folder_options,
4c79b5
		'S_FOLDER_OPTIONS'		=> $s_folder_options,
4c79b5
		'S_DELETE_CHECKED'		=> $s_delete_checked,
4c79b5
		'S_HOLD_CHECKED'		=> $s_hold_checked,
4c79b5
		'S_MOVE_CHECKED'		=> $s_move_checked,
4c79b5
		'S_MAX_FOLDER_REACHED'	=> ($num_user_folder >= $config['pm_max_boxes']) ? true : false,
4c79b5
		'S_MAX_FOLDER_ZERO'		=> ($config['pm_max_boxes'] == 0) ? true : false,
4c79b5
4c79b5
		'DEFAULT_ACTION'		=> ($config['full_folder_action'] == 1) ? $user->lang['DELETE_OLDEST_MESSAGES'] : $user->lang['HOLD_NEW_MESSAGES'],
4c79b5
4c79b5
		'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=ucp&field=rule_string&select_single=true'),
4c79b5
	));
4c79b5
4c79b5
	$rule_lang = $action_lang = $check_lang = array();
4c79b5
4c79b5
	// Build all three language arrays
4c79b5
	preg_replace('#^((RULE|ACTION|CHECK)_([A-Z0-9_]+))$#e', "\${strtolower('\\2') . '_lang'}[constant('\\1')] = \$user->lang['PM_\\2']['\\3']", array_keys(get_defined_constants()));
4c79b5
4c79b5
	/*
4c79b5
		Rule Ordering:
4c79b5
			-> CHECK_* -> RULE_* [IN $global_privmsgs_rules:CHECK_*] -> [IF $rule_conditions[RULE_*] [|text|bool|user|group|own_group]] -> ACTION_*
4c79b5
	*/
4c79b5
4c79b5
	$check_option	= request_var('check_option', 0);
4c79b5
	$rule_option	= request_var('rule_option', 0);
4c79b5
	$cond_option	= request_var('cond_option', '');
4c79b5
	$action_option	= request_var('action_option', '');
4c79b5
	$back = (isset($_REQUEST['back'])) ? request_var('back', array('' => 0)) : array();
4c79b5
4c79b5
	if (sizeof($back))
4c79b5
	{
4c79b5
		if ($action_option)
4c79b5
		{
4c79b5
			$action_option = '';
4c79b5
		}
4c79b5
		else if ($cond_option)
4c79b5
		{
4c79b5
			$cond_option = '';
4c79b5
		}
4c79b5
		else if ($rule_option)
4c79b5
		{
4c79b5
			$rule_option = 0;
4c79b5
		}
4c79b5
		else if ($check_option)
4c79b5
		{
4c79b5
			$check_option = 0;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if (isset($back['action']) && $cond_option == 'none')
4c79b5
	{
4c79b5
		$back['cond'] = true;
4c79b5
	}
4c79b5
4c79b5
	// Check
4c79b5
	if (!isset($global_privmsgs_rules[$check_option]))
4c79b5
	{
4c79b5
		$check_option = 0;
4c79b5
	}
4c79b5
4c79b5
	define_check_option(($check_option && !isset($back['rule'])) ? true : false, $check_option, $check_lang);
4c79b5
4c79b5
	if ($check_option && !isset($back['rule']))
4c79b5
	{
4c79b5
		define_rule_option(($rule_option && !isset($back['cond'])) ? true : false, $rule_option, $rule_lang, $global_privmsgs_rules[$check_option]);
4c79b5
	}
4c79b5
4c79b5
	if ($rule_option && !isset($back['cond']))
4c79b5
	{
4c79b5
		if (!isset($global_rule_conditions[$rule_option]))
4c79b5
		{
4c79b5
			$cond_option = 'none';
4c79b5
			$template->assign_var('NONE_CONDITION', true);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			define_cond_option(($cond_option && !isset($back['action'])) ? true : false, $cond_option, $rule_option, $global_rule_conditions);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if ($cond_option && !isset($back['action']))
4c79b5
	{
4c79b5
		define_action_option(false, $action_option, $action_lang, $folder);
4c79b5
	}
4c79b5
4c79b5
	show_defined_rules($user->data['user_id'], $check_lang, $rule_lang, $action_lang, $folder);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Defining check option for message rules
4c79b5
*/
4c79b5
function define_check_option($hardcoded, $check_option, $check_lang)
4c79b5
{
4c79b5
	global $template;
4c79b5
4c79b5
	$s_check_options = '';
4c79b5
	if (!$hardcoded)
4c79b5
	{
4c79b5
		foreach ($check_lang as $value => $lang)
4c79b5
		{
4c79b5
			$s_check_options .= '<option value="' . $value . '"' . (($value == $check_option) ? ' selected="selected"' : '') . '>' . $lang . '</option>';
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	$template->assign_vars(array(
4c79b5
		'S_CHECK_DEFINED'	=> true,
4c79b5
		'S_CHECK_SELECT'	=> ($hardcoded) ? false : true,
4c79b5
		'CHECK_CURRENT'		=> isset($check_lang[$check_option]) ? $check_lang[$check_option] : '',
4c79b5
		'S_CHECK_OPTIONS'	=> $s_check_options,
4c79b5
		'CHECK_OPTION'		=> $check_option)
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Defining action option for message rules
4c79b5
*/
4c79b5
function define_action_option($hardcoded, $action_option, $action_lang, $folder)
4c79b5
{
4c79b5
	global $db, $template, $user;
4c79b5
4c79b5
	$l_action = $s_action_options = '';
4c79b5
	if ($hardcoded)
4c79b5
	{
4c79b5
		$option = explode('|', $action_option);
4c79b5
		$action = (int) $option[0];
4c79b5
		$folder_id = (int) $option[1];
4c79b5
4c79b5
		$l_action = $action_lang[$action];
4c79b5
		if ($action == ACTION_PLACE_INTO_FOLDER)
4c79b5
		{
4c79b5
			$l_action .= ' -> ' . $folder[$folder_id]['folder_name'];
4c79b5
		}
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		foreach ($action_lang as $action => $lang)
4c79b5
		{
4c79b5
			if ($action == ACTION_PLACE_INTO_FOLDER)
4c79b5
			{
4c79b5
				foreach ($folder as $folder_id => $folder_ary)
4c79b5
				{
4c79b5
					$s_action_options .= '<option value="' . $action . '|' . $folder_id . '"' . (($action_option == $action . '|' . $folder_id) ? ' selected="selected"' : '') . '>' . $lang . ' -> ' . $folder_ary['folder_name'] . '</option>';
4c79b5
				}
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$s_action_options .= '<option value="' . $action . '|0"' . (($action_option == $action . '|0') ? ' selected="selected"' : '') . '>' . $lang . '</option>';
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	$template->assign_vars(array(
4c79b5
		'S_ACTION_DEFINED'	=> true,
4c79b5
		'S_ACTION_SELECT'	=> ($hardcoded) ? false : true,
4c79b5
		'ACTION_CURRENT'	=> $l_action,
4c79b5
		'S_ACTION_OPTIONS'	=> $s_action_options,
4c79b5
		'ACTION_OPTION'		=> $action_option)
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Defining rule option for message rules
4c79b5
*/
4c79b5
function define_rule_option($hardcoded, $rule_option, $rule_lang, $check_ary)
4c79b5
{
4c79b5
	global $template;
4c79b5
4c79b5
	$s_rule_options = '';
4c79b5
	if (!$hardcoded)
4c79b5
	{
4c79b5
		foreach ($check_ary as $value => $_check)
4c79b5
		{
4c79b5
			$s_rule_options .= '<option value="' . $value . '"' . (($value == $rule_option) ? ' selected="selected"' : '') . '>' . $rule_lang[$value] . '</option>';
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	$template->assign_vars(array(
4c79b5
		'S_RULE_DEFINED'	=> true,
4c79b5
		'S_RULE_SELECT'		=> !$hardcoded,
4c79b5
		'RULE_CURRENT'		=> isset($rule_lang[$rule_option]) ? $rule_lang[$rule_option] : '',
4c79b5
		'S_RULE_OPTIONS'	=> $s_rule_options,
4c79b5
		'RULE_OPTION'		=> $rule_option)
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Defining condition option for message rules
4c79b5
*/
4c79b5
function define_cond_option($hardcoded, $cond_option, $rule_option, $global_rule_conditions)
4c79b5
{
4c79b5
	global $db, $template, $auth, $user;
4c79b5
	
4c79b5
	$template->assign_vars(array(
4c79b5
		'S_COND_DEFINED'	=> true,
4c79b5
		'S_COND_SELECT'		=> (!$hardcoded && isset($global_rule_conditions[$rule_option])) ? true : false)
4c79b5
	);
4c79b5
4c79b5
	// Define COND_OPTION
4c79b5
	if (!isset($global_rule_conditions[$rule_option]))
4c79b5
	{
4c79b5
		$template->assign_vars(array(
4c79b5
			'COND_OPTION'	=> 'none',
4c79b5
			'COND_CURRENT'	=> false)
4c79b5
		);
4c79b5
		return;
4c79b5
	}
4c79b5
4c79b5
	// Define Condition
4c79b5
	$condition = $global_rule_conditions[$rule_option];
4c79b5
	$current_value = '';
4c79b5
4c79b5
	switch ($condition)
4c79b5
	{
4c79b5
		case 'text':
4c79b5
			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
4c79b5
			
4c79b5
			$template->assign_vars(array(
4c79b5
				'S_TEXT_CONDITION'	=> true,
4c79b5
				'CURRENT_STRING'	=> $rule_string,
4c79b5
				'CURRENT_USER_ID'	=> 0,
4c79b5
				'CURRENT_GROUP_ID'	=> 0)
4c79b5
			);
4c79b5
4c79b5
			$current_value = $rule_string;
4c79b5
		break;
4c79b5
4c79b5
		case 'user':
4c79b5
			$rule_user_id = request_var('rule_user_id', 0);
4c79b5
			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
4c79b5
			
4c79b5
			if ($rule_string && !$rule_user_id)
4c79b5
			{
4c79b5
				$sql = 'SELECT user_id
4c79b5
					FROM ' . USERS_TABLE . "
4c79b5
					WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($rule_string)) . "'";
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$rule_user_id = (int) $db->sql_fetchfield('user_id');
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if (!$rule_user_id)
4c79b5
				{
4c79b5
					$rule_string = '';
4c79b5
				}
4c79b5
			}
4c79b5
			else if (!$rule_string && $rule_user_id)
4c79b5
			{
4c79b5
				$sql = 'SELECT username
4c79b5
					FROM ' . USERS_TABLE . "
4c79b5
					WHERE user_id = $rule_user_id";
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$rule_string = $db->sql_fetchfield('username');
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if (!$rule_string)
4c79b5
				{
4c79b5
					$rule_user_id = 0;
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			$template->assign_vars(array(
4c79b5
				'S_USER_CONDITION'	=> true,
4c79b5
				'CURRENT_STRING'	=> $rule_string,
4c79b5
				'CURRENT_USER_ID'	=> $rule_user_id,
4c79b5
				'CURRENT_GROUP_ID'	=> 0)
4c79b5
			);
4c79b5
4c79b5
			$current_value = $rule_string;
4c79b5
		break;
4c79b5
4c79b5
		case 'group':
4c79b5
			$rule_group_id = request_var('rule_group_id', 0);
4c79b5
			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
4c79b5
4c79b5
			$sql = 'SELECT g.group_id, g.group_name, g.group_type
4c79b5
					FROM ' . GROUPS_TABLE . ' g ';
4c79b5
4c79b5
			if (!$auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
4c79b5
			{
4c79b5
				$sql .= 'LEFT JOIN ' . USER_GROUP_TABLE . ' ug
4c79b5
					ON (
4c79b5
						g.group_id = ug.group_id
4c79b5
						AND ug.user_id = ' . $user->data['user_id'] . '
4c79b5
						AND ug.user_pending = 0
4c79b5
					)
4c79b5
					WHERE (ug.user_id = ' . $user->data['user_id'] . ' OR g.group_type <> ' . GROUP_HIDDEN . ')
4c79b5
					AND';
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$sql .= 'WHERE';
4c79b5
			}
4c79b5
			
4c79b5
			$sql .= " (g.group_name NOT IN ('GUESTS', 'BOTS') OR g.group_type <> " . GROUP_SPECIAL . ')	
4c79b5
				ORDER BY g.group_type DESC, g.group_name ASC';
4c79b5
			
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			$s_group_options = '';
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				if ($rule_group_id && ($row['group_id'] == $rule_group_id))
4c79b5
				{
4c79b5
					$rule_string = (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']);
4c79b5
				}
4c79b5
4c79b5
				$s_class	= ($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '';
4c79b5
				$s_selected	= ($row['group_id'] == $rule_group_id) ? ' selected="selected"' : '';
4c79b5
				
4c79b5
				$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>';
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			$template->assign_vars(array(
4c79b5
				'S_GROUP_CONDITION'	=> true,
4c79b5
				'S_GROUP_OPTIONS'	=> $s_group_options,
4c79b5
				'CURRENT_STRING'	=> $rule_string,
4c79b5
				'CURRENT_USER_ID'	=> 0,
4c79b5
				'CURRENT_GROUP_ID'	=> $rule_group_id)
4c79b5
			);
4c79b5
4c79b5
			$current_value = $rule_string;
4c79b5
		break;
4c79b5
4c79b5
		default:
4c79b5
			return;
4c79b5
	}
4c79b5
4c79b5
	$template->assign_vars(array(
4c79b5
		'COND_OPTION'	=> $condition,
4c79b5
		'COND_CURRENT'	=> $current_value)
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Display defined message rules
4c79b5
*/
4c79b5
function show_defined_rules($user_id, $check_lang, $rule_lang, $action_lang, $folder)
4c79b5
{
4c79b5
	global $db, $template;
4c79b5
4c79b5
	$sql = 'SELECT *
4c79b5
		FROM ' . PRIVMSGS_RULES_TABLE . '
4c79b5
		WHERE user_id = ' . $user_id . '
4c79b5
		ORDER BY rule_id ASC';
4c79b5
	$result = $db->sql_query($sql);
4c79b5
	
4c79b5
	$count = 0;
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$template->assign_block_vars('rule', array(
4c79b5
			'COUNT'		=> ++$count,
4c79b5
			'RULE_ID'	=> $row['rule_id'],
4c79b5
			'CHECK'		=> $check_lang[$row['rule_check']],
4c79b5
			'RULE'		=> $rule_lang[$row['rule_connection']],
4c79b5
			'STRING'	=> $row['rule_string'],
4c79b5
			'ACTION'	=> $action_lang[$row['rule_action']],
4c79b5
			'FOLDER'	=> ($row['rule_action'] == ACTION_PLACE_INTO_FOLDER) ? $folder[$row['rule_folder_id']]['folder_name'] : '')
4c79b5
		);
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
}
4c79b5
4c79b5
?>