includes/ucp/ucp_pm_options.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package ucp
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 if (!defined('IN_PHPBB'))
       
    15 {
       
    16 	exit;
       
    17 }
       
    18 
       
    19 /**
       
    20 * Execute message options
       
    21 */
       
    22 function message_options($id, $mode, $global_privmsgs_rules, $global_rule_conditions)
       
    23 {
       
    24 	global $phpbb_root_path, $phpEx, $user, $template, $auth, $config, $db;
       
    25 
       
    26 	$redirect_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;mode=options");
       
    27 
       
    28 	add_form_key('ucp_pm_options');
       
    29 	// Change "full folder" setting - what to do if folder is full
       
    30 	if (isset($_POST['fullfolder']))
       
    31 	{
       
    32 		check_form_key('ucp_pm_options', $config['form_token_lifetime'], $redirect_url);
       
    33 		$full_action = request_var('full_action', 0);
       
    34 
       
    35 		$set_folder_id = 0;
       
    36 		switch ($full_action)
       
    37 		{
       
    38 			case 1:
       
    39 				$set_folder_id = FULL_FOLDER_DELETE;
       
    40 			break;
       
    41 
       
    42 			case 2:
       
    43 				$set_folder_id = request_var('full_move_to', PRIVMSGS_INBOX);
       
    44 			break;
       
    45 
       
    46 			case 3:
       
    47 				$set_folder_id = FULL_FOLDER_HOLD;
       
    48 			break;
       
    49 
       
    50 			default:
       
    51 				$full_action = 0;
       
    52 			break;
       
    53 		}
       
    54 
       
    55 		if ($full_action)
       
    56 		{
       
    57 			$sql = 'UPDATE ' . USERS_TABLE . '
       
    58 				SET user_full_folder = ' . $set_folder_id . '
       
    59 				WHERE user_id = ' . $user->data['user_id'];
       
    60 			$db->sql_query($sql);
       
    61 
       
    62 			$user->data['user_full_folder'] = $set_folder_id;
       
    63 
       
    64 			$message = $user->lang['FULL_FOLDER_OPTION_CHANGED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
       
    65 			meta_refresh(3, $redirect_url);
       
    66 			trigger_error($message);
       
    67 		}
       
    68 	}
       
    69 	
       
    70 	// Add Folder
       
    71 	if (isset($_POST['addfolder']))
       
    72 	{
       
    73 		if (check_form_key('ucp_pm_options'))
       
    74 		{
       
    75 			$folder_name = utf8_normalize_nfc(request_var('foldername', '', true));
       
    76 			$msg = '';
       
    77 
       
    78 			if ($folder_name)
       
    79 			{
       
    80 				$sql = 'SELECT folder_name
       
    81 					FROM ' . PRIVMSGS_FOLDER_TABLE . "
       
    82 					WHERE folder_name = '" . $db->sql_escape($folder_name) . "'
       
    83 						AND user_id = " . $user->data['user_id'];
       
    84 				$result = $db->sql_query_limit($sql, 1);
       
    85 				$row = $db->sql_fetchrow($result);
       
    86 				$db->sql_freeresult($result);
       
    87 
       
    88 				if ($row)
       
    89 				{
       
    90 					trigger_error(sprintf($user->lang['FOLDER_NAME_EXIST'], $folder_name));
       
    91 				}
       
    92 
       
    93 				$sql = 'SELECT COUNT(folder_id) as num_folder
       
    94 					FROM ' . PRIVMSGS_FOLDER_TABLE . '
       
    95 						WHERE user_id = ' . $user->data['user_id'];
       
    96 				$result = $db->sql_query($sql);
       
    97 				$num_folder = (int) $db->sql_fetchfield('num_folder');
       
    98 				$db->sql_freeresult($result);
       
    99 
       
   100 				if ($num_folder >= $config['pm_max_boxes'])
       
   101 				{
       
   102 					trigger_error('MAX_FOLDER_REACHED');
       
   103 				}
       
   104 
       
   105 				$sql = 'INSERT INTO ' . PRIVMSGS_FOLDER_TABLE . ' ' . $db->sql_build_array('INSERT', array(
       
   106 					'user_id'		=> (int) $user->data['user_id'],
       
   107 					'folder_name'	=> $folder_name)
       
   108 				);
       
   109 				$db->sql_query($sql);
       
   110 				$msg = $user->lang['FOLDER_ADDED'];
       
   111 			}
       
   112 			else
       
   113 			{
       
   114 				$msg = $user->lang['FOLDER_NAME_EMPTY'];
       
   115 			}
       
   116 		}
       
   117 		else
       
   118 		{
       
   119 			$msg = $user->lang['FORM_INVALID'];
       
   120 		}
       
   121 		$message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
       
   122 		meta_refresh(3, $redirect_url);
       
   123 		trigger_error($message);
       
   124 	}
       
   125 
       
   126 	// Rename folder
       
   127 	if (isset($_POST['rename_folder']))
       
   128 	{
       
   129 		if (check_form_key('ucp_pm_options'))
       
   130 		{
       
   131 			$new_folder_name = utf8_normalize_nfc(request_var('new_folder_name', '', true));
       
   132 			$rename_folder_id= request_var('rename_folder_id', 0);
       
   133 
       
   134 			if (!$new_folder_name)
       
   135 			{
       
   136 				trigger_error('NO_NEW_FOLDER_NAME');
       
   137 			}
       
   138 
       
   139 			// Select custom folder
       
   140 			$sql = 'SELECT folder_name, pm_count
       
   141 				FROM ' . PRIVMSGS_FOLDER_TABLE . "
       
   142 				WHERE user_id = {$user->data['user_id']}
       
   143 					AND folder_id = $rename_folder_id";
       
   144 			$result = $db->sql_query_limit($sql, 1);
       
   145 			$folder_row = $db->sql_fetchrow($result);
       
   146 			$db->sql_freeresult($result);
       
   147 
       
   148 			if (!$folder_row)
       
   149 			{
       
   150 				trigger_error('CANNOT_RENAME_FOLDER');
       
   151 			}
       
   152 
       
   153 			$sql = 'UPDATE ' . PRIVMSGS_FOLDER_TABLE . "
       
   154 				SET folder_name = '" . $db->sql_escape($new_folder_name) . "'
       
   155 				WHERE folder_id = $rename_folder_id
       
   156 					AND user_id = {$user->data['user_id']}";
       
   157 			$db->sql_query($sql);
       
   158 			$msg = $user->lang['FOLDER_RENAMED'];
       
   159 		}
       
   160 		else
       
   161 		{
       
   162 			$msg = $user->lang['FORM_INVALID'];
       
   163 		}
       
   164 
       
   165 		$message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
       
   166 
       
   167 		meta_refresh(3, $redirect_url);
       
   168 		trigger_error($message);
       
   169 	}
       
   170 
       
   171 	// Remove Folder
       
   172 	if (isset($_POST['remove_folder']))
       
   173 	{
       
   174 		$remove_folder_id = request_var('remove_folder_id', 0);
       
   175 
       
   176 		// Default to "move all messages to inbox"
       
   177 		$remove_action = request_var('remove_action', 1);
       
   178 		$move_to = request_var('move_to', PRIVMSGS_INBOX);
       
   179 
       
   180 		// Move to same folder?
       
   181 		if ($remove_action == 1 && $remove_folder_id == $move_to)
       
   182 		{
       
   183 			trigger_error('CANNOT_MOVE_TO_SAME_FOLDER');
       
   184 		}
       
   185 
       
   186 		// Select custom folder
       
   187 		$sql = 'SELECT folder_name, pm_count
       
   188 			FROM ' . PRIVMSGS_FOLDER_TABLE . "
       
   189 			WHERE user_id = {$user->data['user_id']}
       
   190 				AND folder_id = $remove_folder_id";
       
   191 		$result = $db->sql_query_limit($sql, 1);
       
   192 		$folder_row = $db->sql_fetchrow($result);
       
   193 		$db->sql_freeresult($result);
       
   194 
       
   195 		if (!$folder_row)
       
   196 		{
       
   197 			trigger_error('CANNOT_REMOVE_FOLDER');
       
   198 		}
       
   199 
       
   200 		$s_hidden_fields = array(
       
   201 			'remove_folder_id'	=> $remove_folder_id,
       
   202 			'remove_action'		=> $remove_action,
       
   203 			'move_to'			=> $move_to,
       
   204 			'remove_folder'		=> 1
       
   205 		);
       
   206 
       
   207 		// Do we need to confirm?
       
   208 		if (confirm_box(true))
       
   209 		{
       
   210 			// Gather message ids
       
   211 			$sql = 'SELECT msg_id
       
   212 				FROM ' . PRIVMSGS_TO_TABLE . '
       
   213 				WHERE user_id = ' . $user->data['user_id'] . "
       
   214 					AND folder_id = $remove_folder_id";
       
   215 			$result = $db->sql_query($sql);
       
   216 
       
   217 			$msg_ids = array();
       
   218 			while ($row = $db->sql_fetchrow($result))
       
   219 			{
       
   220 				$msg_ids[] = (int) $row['msg_id'];
       
   221 			}
       
   222 			$db->sql_freeresult($result);
       
   223 
       
   224 			// First of all, copy all messages to another folder... or delete all messages
       
   225 			switch ($remove_action)
       
   226 			{
       
   227 				// Move Messages
       
   228 				case 1:
       
   229 					$num_moved = move_pm($user->data['user_id'], $user->data['message_limit'], $msg_ids, $move_to, $remove_folder_id);
       
   230 					
       
   231 					// Something went wrong, only partially moved?
       
   232 					if ($num_moved != $folder_row['pm_count'])
       
   233 					{
       
   234 						trigger_error(sprintf($user->lang['MOVE_PM_ERROR'], $num_moved, $folder_row['pm_count']));
       
   235 					}
       
   236 				break;
       
   237 
       
   238 				// Remove Messages
       
   239 				case 2:
       
   240 					delete_pm($user->data['user_id'], $msg_ids, $remove_folder_id);
       
   241 				break;
       
   242 			}
       
   243 
       
   244 			// Remove folder
       
   245 			$sql = 'DELETE FROM ' . PRIVMSGS_FOLDER_TABLE . "
       
   246 				WHERE user_id = {$user->data['user_id']}
       
   247 					AND folder_id = $remove_folder_id";
       
   248 			$db->sql_query($sql);
       
   249 
       
   250 			// Check full folder option. If the removed folder has been specified as destination switch back to inbox
       
   251 			if ($user->data['user_full_folder'] == $remove_folder_id)
       
   252 			{
       
   253 				$sql = 'UPDATE ' . USERS_TABLE . '
       
   254 					SET user_full_folder = ' . PRIVMSGS_INBOX . '
       
   255 					WHERE user_id = ' . $user->data['user_id'];
       
   256 				$db->sql_query($sql);
       
   257 
       
   258 				$user->data['user_full_folder'] = PRIVMSGS_INBOX;
       
   259 			}
       
   260 
       
   261 			// Now make sure the folder is not used for rules
       
   262 			// We assign another folder id (the one the messages got moved to) or assign the INBOX (to not have to remove any rule)
       
   263 			$sql = 'UPDATE ' . PRIVMSGS_RULES_TABLE . ' SET rule_folder_id = ';
       
   264 			$sql .= ($remove_action == 1) ? $move_to : PRIVMSGS_INBOX;
       
   265 			$sql .= ' WHERE rule_folder_id = ' . $remove_folder_id;
       
   266 
       
   267 			$db->sql_query($sql);
       
   268 
       
   269 			$meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;mode=$mode");
       
   270 			$message = $user->lang['FOLDER_REMOVED'];
       
   271 
       
   272 			meta_refresh(3, $meta_info);
       
   273 			$message .= '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>');
       
   274 			trigger_error($message);
       
   275 		}
       
   276 		else
       
   277 		{
       
   278 			confirm_box(false, 'REMOVE_FOLDER', build_hidden_fields($s_hidden_fields));
       
   279 		}
       
   280 	}
       
   281 
       
   282 	// Add Rule
       
   283 	if (isset($_POST['add_rule']))
       
   284 	{
       
   285 		if (check_form_key('ucp_pm_options'))
       
   286 		{
       
   287 			$check_option	= request_var('check_option', 0);
       
   288 			$rule_option	= request_var('rule_option', 0);
       
   289 			$cond_option	= request_var('cond_option', '');
       
   290 			$action_option	= explode('|', request_var('action_option', ''));
       
   291 			$rule_string	= ($cond_option != 'none') ? utf8_normalize_nfc(request_var('rule_string', '', true)) : '';
       
   292 			$rule_user_id	= ($cond_option != 'none') ? request_var('rule_user_id', 0) : 0;
       
   293 			$rule_group_id	= ($cond_option != 'none') ? request_var('rule_group_id', 0) : 0;
       
   294 
       
   295 			$action = (int) $action_option[0];
       
   296 			$folder_id = (int) $action_option[1];
       
   297 
       
   298 			if (!$action || !$check_option || !$rule_option || !$cond_option || ($cond_option != 'none' && !$rule_string))
       
   299 			{
       
   300 				trigger_error('RULE_NOT_DEFINED');
       
   301 			}
       
   302 
       
   303 			if (($cond_option == 'user' && !$rule_user_id) || ($cond_option == 'group' && !$rule_group_id))
       
   304 			{
       
   305 				trigger_error('RULE_NOT_DEFINED');
       
   306 			}
       
   307 
       
   308 			$rule_ary = array(
       
   309 				'user_id'			=> $user->data['user_id'],
       
   310 				'rule_check'		=> $check_option,
       
   311 				'rule_connection'	=> $rule_option,
       
   312 				'rule_string'		=> $rule_string,
       
   313 				'rule_user_id'		=> $rule_user_id,
       
   314 				'rule_group_id'		=> $rule_group_id,
       
   315 				'rule_action'		=> $action,
       
   316 				'rule_folder_id'	=> $folder_id
       
   317 			);
       
   318 
       
   319 			$sql = 'SELECT rule_id
       
   320 				FROM ' . PRIVMSGS_RULES_TABLE . '
       
   321 				WHERE ' . $db->sql_build_array('SELECT', $rule_ary);
       
   322 			$result = $db->sql_query($sql);
       
   323 			$row = $db->sql_fetchrow($result);
       
   324 			$db->sql_freeresult($result);
       
   325 
       
   326 			if ($row)
       
   327 			{
       
   328 				trigger_error('RULE_ALREADY_DEFINED');
       
   329 			}
       
   330 
       
   331 			$sql = 'INSERT INTO ' . PRIVMSGS_RULES_TABLE . ' ' . $db->sql_build_array('INSERT', $rule_ary);
       
   332 			$db->sql_query($sql);
       
   333 
       
   334 			// Update users message rules
       
   335 			$sql = 'UPDATE ' . USERS_TABLE . '
       
   336 				SET user_message_rules = 1
       
   337 				WHERE user_id = ' . $user->data['user_id'];
       
   338 			$db->sql_query($sql);
       
   339 
       
   340 			$msg = $user->lang['RULE_ADDED'];
       
   341 		}
       
   342 		else
       
   343 		{
       
   344 			$msg = $user->lang['FORM_INVALID'];
       
   345 		}
       
   346 		$message = $msg . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
       
   347 		meta_refresh(3, $redirect_url);
       
   348 		trigger_error($message);
       
   349 	}
       
   350 
       
   351 	// Remove Rule
       
   352 	if (isset($_POST['delete_rule']) && !isset($_POST['cancel']))
       
   353 	{
       
   354 		$delete_id = array_keys(request_var('delete_rule', array(0 => 0)));
       
   355 		$delete_id = (!empty($delete_id[0])) ? $delete_id[0] : 0;
       
   356 
       
   357 		if (!$delete_id)
       
   358 		{
       
   359 			redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=' . $mode));
       
   360 		}
       
   361 
       
   362 		// Do we need to confirm?
       
   363 		if (confirm_box(true))
       
   364 		{
       
   365 			$sql = 'DELETE FROM ' . PRIVMSGS_RULES_TABLE . '
       
   366 				WHERE user_id = ' . $user->data['user_id'] . "
       
   367 					AND rule_id = $delete_id";
       
   368 			$db->sql_query($sql);
       
   369 
       
   370 			$meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=' . $mode);
       
   371 			$message = $user->lang['RULE_DELETED'];
       
   372 
       
   373 			// Reset user_message_rules if no more assigned
       
   374 			$sql = 'SELECT rule_id
       
   375 				FROM ' . PRIVMSGS_RULES_TABLE . '
       
   376 				WHERE user_id = ' . $user->data['user_id'];
       
   377 			$result = $db->sql_query_limit($sql, 1);
       
   378 			$row = $db->sql_fetchrow($result);
       
   379 			$db->sql_freeresult($result);
       
   380 
       
   381 			// Update users message rules
       
   382 			if (!$row)
       
   383 			{
       
   384 				$sql = 'UPDATE ' . USERS_TABLE . '
       
   385 					SET user_message_rules = 0
       
   386 					WHERE user_id = ' . $user->data['user_id'];
       
   387 				$db->sql_query($sql);
       
   388 			}
       
   389 
       
   390 			meta_refresh(3, $meta_info);
       
   391 			$message .= '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>');
       
   392 			trigger_error($message);
       
   393 		}
       
   394 		else
       
   395 		{
       
   396 			confirm_box(false, 'DELETE_RULE', build_hidden_fields(array('delete_rule' => array($delete_id => 1))));
       
   397 		}
       
   398 	}
       
   399 
       
   400 	$folder = array();
       
   401 
       
   402 	$sql = 'SELECT COUNT(msg_id) as num_messages
       
   403 		FROM ' . PRIVMSGS_TO_TABLE . '
       
   404 		WHERE user_id = ' . $user->data['user_id'] . '
       
   405 			AND folder_id = ' . PRIVMSGS_INBOX;
       
   406 	$result = $db->sql_query($sql);
       
   407 	$num_messages = (int) $db->sql_fetchfield('num_messages');
       
   408 	$db->sql_freeresult($result);
       
   409 	
       
   410 	$folder[PRIVMSGS_INBOX] = array(
       
   411 		'folder_name'		=> $user->lang['PM_INBOX'],
       
   412 		'message_status'	=> sprintf($user->lang['FOLDER_MESSAGE_STATUS'], $num_messages, $user->data['message_limit'])
       
   413 	);
       
   414 
       
   415 	$sql = 'SELECT folder_id, folder_name, pm_count
       
   416 		FROM ' . PRIVMSGS_FOLDER_TABLE . '
       
   417 			WHERE user_id = ' . $user->data['user_id'];
       
   418 	$result = $db->sql_query($sql);
       
   419 
       
   420 	$num_user_folder = 0;
       
   421 	while ($row = $db->sql_fetchrow($result))
       
   422 	{
       
   423 		$num_user_folder++;
       
   424 		$folder[$row['folder_id']] = array(
       
   425 			'folder_name'		=> $row['folder_name'],
       
   426 			'message_status'	=> sprintf($user->lang['FOLDER_MESSAGE_STATUS'], $row['pm_count'], $user->data['message_limit'])
       
   427 		);
       
   428 	}
       
   429 	$db->sql_freeresult($result);
       
   430 
       
   431 	$s_full_folder_options = $s_to_folder_options = $s_folder_options = '';
       
   432 
       
   433 	if ($user->data['user_full_folder'] == FULL_FOLDER_NONE)
       
   434 	{
       
   435 		// -3 here to let the correct folder id be selected
       
   436 		$to_folder_id = $config['full_folder_action'] - 3;
       
   437 	}
       
   438 	else
       
   439 	{
       
   440 		$to_folder_id = $user->data['user_full_folder'];
       
   441 	}
       
   442 
       
   443 	foreach ($folder as $folder_id => $folder_ary)
       
   444 	{
       
   445 		$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>';
       
   446 		$s_to_folder_options .= '<option value="' . $folder_id . '"' . (($to_folder_id == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
       
   447 
       
   448 		if ($folder_id != PRIVMSGS_INBOX)
       
   449 		{
       
   450 			$s_folder_options .= '<option value="' . $folder_id . '">' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
       
   451 		}
       
   452 	}
       
   453 
       
   454 	$s_delete_checked = ($user->data['user_full_folder'] == FULL_FOLDER_DELETE) ? ' checked="checked"' : '';
       
   455 	$s_hold_checked = ($user->data['user_full_folder'] == FULL_FOLDER_HOLD) ? ' checked="checked"' : '';
       
   456 	$s_move_checked = ($user->data['user_full_folder'] >= 0) ? ' checked="checked"' : '';
       
   457 
       
   458 	if ($user->data['user_full_folder'] == FULL_FOLDER_NONE)
       
   459 	{
       
   460 		switch ($config['full_folder_action'])
       
   461 		{
       
   462 			case 1:
       
   463 				$s_delete_checked = ' checked="checked"';
       
   464 			break;
       
   465 
       
   466 			case 2:
       
   467 				$s_hold_checked = ' checked="checked"';
       
   468 			break;
       
   469 		}
       
   470 	}
       
   471 
       
   472 	$template->assign_vars(array(
       
   473 		'S_FULL_FOLDER_OPTIONS'	=> $s_full_folder_options,
       
   474 		'S_TO_FOLDER_OPTIONS'	=> $s_to_folder_options,
       
   475 		'S_FOLDER_OPTIONS'		=> $s_folder_options,
       
   476 		'S_DELETE_CHECKED'		=> $s_delete_checked,
       
   477 		'S_HOLD_CHECKED'		=> $s_hold_checked,
       
   478 		'S_MOVE_CHECKED'		=> $s_move_checked,
       
   479 		'S_MAX_FOLDER_REACHED'	=> ($num_user_folder >= $config['pm_max_boxes']) ? true : false,
       
   480 		'S_MAX_FOLDER_ZERO'		=> ($config['pm_max_boxes'] == 0) ? true : false,
       
   481 
       
   482 		'DEFAULT_ACTION'		=> ($config['full_folder_action'] == 1) ? $user->lang['DELETE_OLDEST_MESSAGES'] : $user->lang['HOLD_NEW_MESSAGES'],
       
   483 
       
   484 		'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=ucp&amp;field=rule_string&amp;select_single=true'),
       
   485 	));
       
   486 
       
   487 	$rule_lang = $action_lang = $check_lang = array();
       
   488 
       
   489 	// Build all three language arrays
       
   490 	preg_replace('#^((RULE|ACTION|CHECK)_([A-Z0-9_]+))$#e', "\${strtolower('\\2') . '_lang'}[constant('\\1')] = \$user->lang['PM_\\2']['\\3']", array_keys(get_defined_constants()));
       
   491 
       
   492 	/*
       
   493 		Rule Ordering:
       
   494 			-> CHECK_* -> RULE_* [IN $global_privmsgs_rules:CHECK_*] -> [IF $rule_conditions[RULE_*] [|text|bool|user|group|own_group]] -> ACTION_*
       
   495 	*/
       
   496 
       
   497 	$check_option	= request_var('check_option', 0);
       
   498 	$rule_option	= request_var('rule_option', 0);
       
   499 	$cond_option	= request_var('cond_option', '');
       
   500 	$action_option	= request_var('action_option', '');
       
   501 	$back = (isset($_REQUEST['back'])) ? request_var('back', array('' => 0)) : array();
       
   502 
       
   503 	if (sizeof($back))
       
   504 	{
       
   505 		if ($action_option)
       
   506 		{
       
   507 			$action_option = '';
       
   508 		}
       
   509 		else if ($cond_option)
       
   510 		{
       
   511 			$cond_option = '';
       
   512 		}
       
   513 		else if ($rule_option)
       
   514 		{
       
   515 			$rule_option = 0;
       
   516 		}
       
   517 		else if ($check_option)
       
   518 		{
       
   519 			$check_option = 0;
       
   520 		}
       
   521 	}
       
   522 
       
   523 	if (isset($back['action']) && $cond_option == 'none')
       
   524 	{
       
   525 		$back['cond'] = true;
       
   526 	}
       
   527 
       
   528 	// Check
       
   529 	if (!isset($global_privmsgs_rules[$check_option]))
       
   530 	{
       
   531 		$check_option = 0;
       
   532 	}
       
   533 
       
   534 	define_check_option(($check_option && !isset($back['rule'])) ? true : false, $check_option, $check_lang);
       
   535 
       
   536 	if ($check_option && !isset($back['rule']))
       
   537 	{
       
   538 		define_rule_option(($rule_option && !isset($back['cond'])) ? true : false, $rule_option, $rule_lang, $global_privmsgs_rules[$check_option]);
       
   539 	}
       
   540 
       
   541 	if ($rule_option && !isset($back['cond']))
       
   542 	{
       
   543 		if (!isset($global_rule_conditions[$rule_option]))
       
   544 		{
       
   545 			$cond_option = 'none';
       
   546 			$template->assign_var('NONE_CONDITION', true);
       
   547 		}
       
   548 		else
       
   549 		{
       
   550 			define_cond_option(($cond_option && !isset($back['action'])) ? true : false, $cond_option, $rule_option, $global_rule_conditions);
       
   551 		}
       
   552 	}
       
   553 
       
   554 	if ($cond_option && !isset($back['action']))
       
   555 	{
       
   556 		define_action_option(false, $action_option, $action_lang, $folder);
       
   557 	}
       
   558 
       
   559 	show_defined_rules($user->data['user_id'], $check_lang, $rule_lang, $action_lang, $folder);
       
   560 }
       
   561 
       
   562 /**
       
   563 * Defining check option for message rules
       
   564 */
       
   565 function define_check_option($hardcoded, $check_option, $check_lang)
       
   566 {
       
   567 	global $template;
       
   568 
       
   569 	$s_check_options = '';
       
   570 	if (!$hardcoded)
       
   571 	{
       
   572 		foreach ($check_lang as $value => $lang)
       
   573 		{
       
   574 			$s_check_options .= '<option value="' . $value . '"' . (($value == $check_option) ? ' selected="selected"' : '') . '>' . $lang . '</option>';
       
   575 		}
       
   576 	}
       
   577 
       
   578 	$template->assign_vars(array(
       
   579 		'S_CHECK_DEFINED'	=> true,
       
   580 		'S_CHECK_SELECT'	=> ($hardcoded) ? false : true,
       
   581 		'CHECK_CURRENT'		=> isset($check_lang[$check_option]) ? $check_lang[$check_option] : '',
       
   582 		'S_CHECK_OPTIONS'	=> $s_check_options,
       
   583 		'CHECK_OPTION'		=> $check_option)
       
   584 	);
       
   585 }
       
   586 
       
   587 /**
       
   588 * Defining action option for message rules
       
   589 */
       
   590 function define_action_option($hardcoded, $action_option, $action_lang, $folder)
       
   591 {
       
   592 	global $db, $template, $user;
       
   593 
       
   594 	$l_action = $s_action_options = '';
       
   595 	if ($hardcoded)
       
   596 	{
       
   597 		$option = explode('|', $action_option);
       
   598 		$action = (int) $option[0];
       
   599 		$folder_id = (int) $option[1];
       
   600 
       
   601 		$l_action = $action_lang[$action];
       
   602 		if ($action == ACTION_PLACE_INTO_FOLDER)
       
   603 		{
       
   604 			$l_action .= ' -> ' . $folder[$folder_id]['folder_name'];
       
   605 		}
       
   606 	}
       
   607 	else
       
   608 	{
       
   609 		foreach ($action_lang as $action => $lang)
       
   610 		{
       
   611 			if ($action == ACTION_PLACE_INTO_FOLDER)
       
   612 			{
       
   613 				foreach ($folder as $folder_id => $folder_ary)
       
   614 				{
       
   615 					$s_action_options .= '<option value="' . $action . '|' . $folder_id . '"' . (($action_option == $action . '|' . $folder_id) ? ' selected="selected"' : '') . '>' . $lang . ' -> ' . $folder_ary['folder_name'] . '</option>';
       
   616 				}
       
   617 			}
       
   618 			else
       
   619 			{
       
   620 				$s_action_options .= '<option value="' . $action . '|0"' . (($action_option == $action . '|0') ? ' selected="selected"' : '') . '>' . $lang . '</option>';
       
   621 			}
       
   622 		}
       
   623 	}
       
   624 
       
   625 	$template->assign_vars(array(
       
   626 		'S_ACTION_DEFINED'	=> true,
       
   627 		'S_ACTION_SELECT'	=> ($hardcoded) ? false : true,
       
   628 		'ACTION_CURRENT'	=> $l_action,
       
   629 		'S_ACTION_OPTIONS'	=> $s_action_options,
       
   630 		'ACTION_OPTION'		=> $action_option)
       
   631 	);
       
   632 }
       
   633 
       
   634 /**
       
   635 * Defining rule option for message rules
       
   636 */
       
   637 function define_rule_option($hardcoded, $rule_option, $rule_lang, $check_ary)
       
   638 {
       
   639 	global $template;
       
   640 	global $module;
       
   641 
       
   642 	$exclude = array();
       
   643 
       
   644 	if (!$module->loaded('zebra', 'friends'))
       
   645 	{
       
   646 		$exclude[RULE_IS_FRIEND] = true;
       
   647 	}
       
   648 
       
   649 	if (!$module->loaded('zebra', 'foes'))
       
   650 	{
       
   651 		$exclude[RULE_IS_FOE] = true;
       
   652 	}
       
   653 
       
   654 	$s_rule_options = '';
       
   655 	if (!$hardcoded)
       
   656 	{
       
   657 		foreach ($check_ary as $value => $_check)
       
   658 		{
       
   659 			if (isset($exclude[$value]))
       
   660 			{
       
   661 				continue;
       
   662 			}
       
   663 			$s_rule_options .= '<option value="' . $value . '"' . (($value == $rule_option) ? ' selected="selected"' : '') . '>' . $rule_lang[$value] . '</option>';
       
   664 		}
       
   665 	}
       
   666 
       
   667 	$template->assign_vars(array(
       
   668 		'S_RULE_DEFINED'	=> true,
       
   669 		'S_RULE_SELECT'		=> !$hardcoded,
       
   670 		'RULE_CURRENT'		=> isset($rule_lang[$rule_option]) ? $rule_lang[$rule_option] : '',
       
   671 		'S_RULE_OPTIONS'	=> $s_rule_options,
       
   672 		'RULE_OPTION'		=> $rule_option)
       
   673 	);
       
   674 }
       
   675 
       
   676 /**
       
   677 * Defining condition option for message rules
       
   678 */
       
   679 function define_cond_option($hardcoded, $cond_option, $rule_option, $global_rule_conditions)
       
   680 {
       
   681 	global $db, $template, $auth, $user;
       
   682 	
       
   683 	$template->assign_vars(array(
       
   684 		'S_COND_DEFINED'	=> true,
       
   685 		'S_COND_SELECT'		=> (!$hardcoded && isset($global_rule_conditions[$rule_option])) ? true : false)
       
   686 	);
       
   687 
       
   688 	// Define COND_OPTION
       
   689 	if (!isset($global_rule_conditions[$rule_option]))
       
   690 	{
       
   691 		$template->assign_vars(array(
       
   692 			'COND_OPTION'	=> 'none',
       
   693 			'COND_CURRENT'	=> false)
       
   694 		);
       
   695 		return;
       
   696 	}
       
   697 
       
   698 	// Define Condition
       
   699 	$condition = $global_rule_conditions[$rule_option];
       
   700 	$current_value = '';
       
   701 
       
   702 	switch ($condition)
       
   703 	{
       
   704 		case 'text':
       
   705 			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
       
   706 			
       
   707 			$template->assign_vars(array(
       
   708 				'S_TEXT_CONDITION'	=> true,
       
   709 				'CURRENT_STRING'	=> $rule_string,
       
   710 				'CURRENT_USER_ID'	=> 0,
       
   711 				'CURRENT_GROUP_ID'	=> 0)
       
   712 			);
       
   713 
       
   714 			$current_value = $rule_string;
       
   715 		break;
       
   716 
       
   717 		case 'user':
       
   718 			$rule_user_id = request_var('rule_user_id', 0);
       
   719 			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
       
   720 			
       
   721 			if ($rule_string && !$rule_user_id)
       
   722 			{
       
   723 				$sql = 'SELECT user_id
       
   724 					FROM ' . USERS_TABLE . "
       
   725 					WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($rule_string)) . "'";
       
   726 				$result = $db->sql_query($sql);
       
   727 				$rule_user_id = (int) $db->sql_fetchfield('user_id');
       
   728 				$db->sql_freeresult($result);
       
   729 
       
   730 				if (!$rule_user_id)
       
   731 				{
       
   732 					$rule_string = '';
       
   733 				}
       
   734 			}
       
   735 			else if (!$rule_string && $rule_user_id)
       
   736 			{
       
   737 				$sql = 'SELECT username
       
   738 					FROM ' . USERS_TABLE . "
       
   739 					WHERE user_id = $rule_user_id";
       
   740 				$result = $db->sql_query($sql);
       
   741 				$rule_string = $db->sql_fetchfield('username');
       
   742 				$db->sql_freeresult($result);
       
   743 
       
   744 				if (!$rule_string)
       
   745 				{
       
   746 					$rule_user_id = 0;
       
   747 				}
       
   748 			}
       
   749 
       
   750 			$template->assign_vars(array(
       
   751 				'S_USER_CONDITION'	=> true,
       
   752 				'CURRENT_STRING'	=> $rule_string,
       
   753 				'CURRENT_USER_ID'	=> $rule_user_id,
       
   754 				'CURRENT_GROUP_ID'	=> 0)
       
   755 			);
       
   756 
       
   757 			$current_value = $rule_string;
       
   758 		break;
       
   759 
       
   760 		case 'group':
       
   761 			$rule_group_id = request_var('rule_group_id', 0);
       
   762 			$rule_string = utf8_normalize_nfc(request_var('rule_string', '', true));
       
   763 
       
   764 			$sql = 'SELECT g.group_id, g.group_name, g.group_type
       
   765 					FROM ' . GROUPS_TABLE . ' g ';
       
   766 
       
   767 			if (!$auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
       
   768 			{
       
   769 				$sql .= 'LEFT JOIN ' . USER_GROUP_TABLE . ' ug
       
   770 					ON (
       
   771 						g.group_id = ug.group_id
       
   772 						AND ug.user_id = ' . $user->data['user_id'] . '
       
   773 						AND ug.user_pending = 0
       
   774 					)
       
   775 					WHERE (ug.user_id = ' . $user->data['user_id'] . ' OR g.group_type <> ' . GROUP_HIDDEN . ')
       
   776 					AND';
       
   777 			}
       
   778 			else
       
   779 			{
       
   780 				$sql .= 'WHERE';
       
   781 			}
       
   782 			
       
   783 			$sql .= " (g.group_name NOT IN ('GUESTS', 'BOTS') OR g.group_type <> " . GROUP_SPECIAL . ')	
       
   784 				ORDER BY g.group_type DESC, g.group_name ASC';
       
   785 			
       
   786 			$result = $db->sql_query($sql);
       
   787 
       
   788 			$s_group_options = '';
       
   789 			while ($row = $db->sql_fetchrow($result))
       
   790 			{
       
   791 				if ($rule_group_id && ($row['group_id'] == $rule_group_id))
       
   792 				{
       
   793 					$rule_string = (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']);
       
   794 				}
       
   795 
       
   796 				$s_class	= ($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '';
       
   797 				$s_selected	= ($row['group_id'] == $rule_group_id) ? ' selected="selected"' : '';
       
   798 				
       
   799 				$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>';
       
   800 			}
       
   801 			$db->sql_freeresult($result);
       
   802 
       
   803 			$template->assign_vars(array(
       
   804 				'S_GROUP_CONDITION'	=> true,
       
   805 				'S_GROUP_OPTIONS'	=> $s_group_options,
       
   806 				'CURRENT_STRING'	=> $rule_string,
       
   807 				'CURRENT_USER_ID'	=> 0,
       
   808 				'CURRENT_GROUP_ID'	=> $rule_group_id)
       
   809 			);
       
   810 
       
   811 			$current_value = $rule_string;
       
   812 		break;
       
   813 
       
   814 		default:
       
   815 			return;
       
   816 	}
       
   817 
       
   818 	$template->assign_vars(array(
       
   819 		'COND_OPTION'	=> $condition,
       
   820 		'COND_CURRENT'	=> $current_value)
       
   821 	);
       
   822 }
       
   823 
       
   824 /**
       
   825 * Display defined message rules
       
   826 */
       
   827 function show_defined_rules($user_id, $check_lang, $rule_lang, $action_lang, $folder)
       
   828 {
       
   829 	global $db, $template;
       
   830 
       
   831 	$sql = 'SELECT *
       
   832 		FROM ' . PRIVMSGS_RULES_TABLE . '
       
   833 		WHERE user_id = ' . $user_id . '
       
   834 		ORDER BY rule_id ASC';
       
   835 	$result = $db->sql_query($sql);
       
   836 	
       
   837 	$count = 0;
       
   838 	while ($row = $db->sql_fetchrow($result))
       
   839 	{
       
   840 		$template->assign_block_vars('rule', array(
       
   841 			'COUNT'		=> ++$count,
       
   842 			'RULE_ID'	=> $row['rule_id'],
       
   843 			'CHECK'		=> $check_lang[$row['rule_check']],
       
   844 			'RULE'		=> $rule_lang[$row['rule_connection']],
       
   845 			'STRING'	=> $row['rule_string'],
       
   846 			'ACTION'	=> $action_lang[$row['rule_action']],
       
   847 			'FOLDER'	=> ($row['rule_action'] == ACTION_PLACE_INTO_FOLDER) ? $folder[$row['rule_folder_id']]['folder_name'] : '')
       
   848 		);
       
   849 	}
       
   850 	$db->sql_freeresult($result);
       
   851 }
       
   852 
       
   853 ?>