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

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package ucp
4c79b5
* @version $Id: ucp_pm_compose.php 9168 2008-12-03 16:48:06Z acydburn $
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
* Compose private message
4c79b5
* Called from ucp_pm with mode == 'compose'
4c79b5
*/
4c79b5
function compose_pm($id, $mode, $action)
4c79b5
{
4c79b5
	global $template, $db, $auth, $user;
4c79b5
	global $phpbb_root_path, $phpEx, $config;
4c79b5
4c79b5
	// Damn php and globals - i know, this is horrible
4c79b5
	// Needed for handle_message_list_actions()
4c79b5
	global $refresh, $submit, $preview;
4c79b5
4c79b5
	include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
4c79b5
	include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
4c79b5
	include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
4c79b5
4c79b5
	if (!$action)
4c79b5
	{
4c79b5
		$action = 'post';
4c79b5
	}
4c79b5
	add_form_key('ucp_pm_compose');
4c79b5
4c79b5
	// Grab only parameters needed here
4c79b5
	$to_user_id		= request_var('u', 0);
4c79b5
	$to_group_id	= request_var('g', 0);
4c79b5
	$msg_id			= request_var('p', 0);
4c79b5
	$draft_id		= request_var('d', 0);
4c79b5
	$lastclick		= request_var('lastclick', 0);
4c79b5
4c79b5
	// Do NOT use request_var or specialchars here
4c79b5
	$address_list	= isset($_REQUEST['address_list']) ? $_REQUEST['address_list'] : array();
4c79b5
4c79b5
	if (!is_array($address_list))
4c79b5
	{
4c79b5
		$address_list = array();
4c79b5
	}
4c79b5
4c79b5
	$submit		= (isset($_POST['post'])) ? true : false;
4c79b5
	$preview	= (isset($_POST['preview'])) ? true : false;
4c79b5
	$save		= (isset($_POST['save'])) ? true : false;
4c79b5
	$load		= (isset($_POST['load'])) ? true : false;
4c79b5
	$cancel		= (isset($_POST['cancel']) && !isset($_POST['save'])) ? true : false;
4c79b5
	$delete		= (isset($_POST['delete'])) ? true : false;
4c79b5
4c79b5
	$remove_u	= (isset($_REQUEST['remove_u'])) ? true : false;
4c79b5
	$remove_g	= (isset($_REQUEST['remove_g'])) ? true : false;
4c79b5
	$add_to		= (isset($_REQUEST['add_to'])) ? true : false;
4c79b5
	$add_bcc	= (isset($_REQUEST['add_bcc'])) ? true : false;
4c79b5
4c79b5
	$refresh	= isset($_POST['add_file']) || isset($_POST['delete_file']) || $save || $load
4c79b5
		|| $remove_u || $remove_g || $add_to || $add_bcc;
4c79b5
4c79b5
	$action		= ($delete && !$preview && !$refresh && $submit) ? 'delete' : $action;
4c79b5
	$select_single = ($config['allow_mass_pm'] && $auth->acl_get('u_masspm')) ? false : true;
4c79b5
4c79b5
	$error = array();
4c79b5
	$current_time = time();
4c79b5
4c79b5
	// Was cancel pressed? If so then redirect to the appropriate page
4c79b5
	if ($cancel || ($current_time - $lastclick < 2 && $submit))
4c79b5
	{
4c79b5
		if ($msg_id)
4c79b5
		{
4c79b5
			redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=view&action=view_message&p=' . $msg_id));
4c79b5
		}
4c79b5
		redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm'));
4c79b5
	}
4c79b5
4c79b5
	// Output PM_TO box if message composing
4c79b5
	if ($action != 'edit')
4c79b5
	{
4c79b5
		// Add groups to PM box
4c79b5
		if ($config['allow_mass_pm'] && $auth->acl_get('u_masspm_group'))
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 (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $user->data['user_id'] . ')';
4c79b5
			}
4c79b5
4c79b5
			$sql .= ($auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) ? ' WHERE ' : ' AND ';
4c79b5
4c79b5
			$sql .= 'g.group_receive_pm = 1
4c79b5
				ORDER BY g.group_type DESC, g.group_name ASC';
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			$group_options = '';
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '">' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
		}
4c79b5
4c79b5
		$template->assign_vars(array(
4c79b5
			'S_SHOW_PM_BOX'		=> true,
4c79b5
			'S_ALLOW_MASS_PM'	=> ($config['allow_mass_pm'] && $auth->acl_get('u_masspm')) ? true : false,
4c79b5
			'S_GROUP_OPTIONS'	=> ($config['allow_mass_pm'] && $auth->acl_get('u_masspm_group')) ? $group_options : '',
4c79b5
			'U_FIND_USERNAME'	=> append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=searchuser&form=postform&field=username_list&select_single=$select_single"),
4c79b5
		));
4c79b5
	}
4c79b5
4c79b5
	$sql = '';
4c79b5
4c79b5
	// What is all this following SQL for? Well, we need to know
4c79b5
	// some basic information in all cases before we do anything.
4c79b5
	switch ($action)
4c79b5
	{
4c79b5
		case 'post':
4c79b5
			if (!$auth->acl_get('u_sendpm'))
4c79b5
			{
4c79b5
				trigger_error('NO_AUTH_SEND_MESSAGE');
4c79b5
			}
4c79b5
		break;
4c79b5
4c79b5
		case 'reply':
4c79b5
		case 'quote':
4c79b5
		case 'forward':
4c79b5
		case 'quotepost':
4c79b5
			if (!$msg_id)
4c79b5
			{
4c79b5
				trigger_error('NO_MESSAGE');
4c79b5
			}
4c79b5
4c79b5
			if (!$auth->acl_get('u_sendpm'))
4c79b5
			{
4c79b5
				trigger_error('NO_AUTH_SEND_MESSAGE');
4c79b5
			}
4c79b5
4c79b5
			if ($action == 'quotepost')
4c79b5
			{
4c79b5
				$sql = 'SELECT p.post_id as msg_id, p.forum_id, p.post_text as message_text, p.poster_id as author_id, p.post_time as message_time, p.bbcode_bitfield, p.bbcode_uid, p.enable_sig, p.enable_smilies, p.enable_magic_url, t.topic_title as message_subject, u.username as quote_username
4c79b5
					FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . " u
4c79b5
					WHERE p.post_id = $msg_id
4c79b5
						AND t.topic_id = p.topic_id
4c79b5
						AND u.user_id = p.poster_id";
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$sql = 'SELECT t.folder_id, p.*, u.username as quote_username
4c79b5
					FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . ' u
4c79b5
					WHERE t.user_id = ' . $user->data['user_id'] . "
4c79b5
						AND p.author_id = u.user_id
4c79b5
						AND t.msg_id = p.msg_id
4c79b5
						AND p.msg_id = $msg_id";
4c79b5
			}
4c79b5
		break;
4c79b5
4c79b5
		case 'edit':
4c79b5
			if (!$msg_id)
4c79b5
			{
4c79b5
				trigger_error('NO_MESSAGE');
4c79b5
			}
4c79b5
4c79b5
			// check for outbox (not read) status, we do not allow editing if one user already having the message
4c79b5
			$sql = 'SELECT p.*, t.folder_id
4c79b5
				FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p
4c79b5
				WHERE t.user_id = ' . $user->data['user_id'] . '
4c79b5
					AND t.folder_id = ' . PRIVMSGS_OUTBOX . "
4c79b5
					AND t.msg_id = $msg_id
4c79b5
					AND t.msg_id = p.msg_id";
4c79b5
		break;
4c79b5
4c79b5
		case 'delete':
4c79b5
			if (!$auth->acl_get('u_pm_delete'))
4c79b5
			{
4c79b5
				trigger_error('NO_AUTH_DELETE_MESSAGE');
4c79b5
			}
4c79b5
4c79b5
			if (!$msg_id)
4c79b5
			{
4c79b5
				trigger_error('NO_MESSAGE');
4c79b5
			}
4c79b5
4c79b5
			$sql = 'SELECT msg_id, pm_unread, pm_new, author_id, folder_id
4c79b5
				FROM ' . PRIVMSGS_TO_TABLE . '
4c79b5
				WHERE user_id = ' . $user->data['user_id'] . "
4c79b5
					AND msg_id = $msg_id";
4c79b5
		break;
4c79b5
4c79b5
		case 'smilies':
4c79b5
			generate_smilies('window', 0);
4c79b5
		break;
4c79b5
4c79b5
		default:
4c79b5
			trigger_error('NO_ACTION_MODE', E_USER_ERROR);
4c79b5
		break;
4c79b5
	}
4c79b5
4c79b5
	if ($action == 'forward' && (!$config['forward_pm'] || !$auth->acl_get('u_pm_forward')))
4c79b5
	{
4c79b5
		trigger_error('NO_AUTH_FORWARD_MESSAGE');
4c79b5
	}
4c79b5
4c79b5
	if ($action == 'edit' && !$auth->acl_get('u_pm_edit'))
4c79b5
	{
4c79b5
		trigger_error('NO_AUTH_EDIT_MESSAGE');
4c79b5
	}
4c79b5
4c79b5
	if ($sql)
4c79b5
	{
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$post = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		if (!$post)
4c79b5
		{
4c79b5
			// If editing it could be the recipient already read the message...
4c79b5
			if ($action == 'edit')
4c79b5
			{
4c79b5
				$sql = 'SELECT p.*, t.folder_id
4c79b5
					FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p
4c79b5
					WHERE t.user_id = ' . $user->data['user_id'] . "
4c79b5
						AND t.msg_id = $msg_id
4c79b5
						AND t.msg_id = p.msg_id";
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$post = $db->sql_fetchrow($result);
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if ($post)
4c79b5
				{
4c79b5
					trigger_error('NO_EDIT_READ_MESSAGE');
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			trigger_error('NO_MESSAGE');
4c79b5
		}
4c79b5
4c79b5
		if ($action == 'quotepost')
4c79b5
		{
4c79b5
			if (($post['forum_id'] && !$auth->acl_get('f_read', $post['forum_id'])) || (!$post['forum_id'] && !$auth->acl_getf_global('f_read')))
4c79b5
			{
4c79b5
				trigger_error('NOT_AUTHORISED');
4c79b5
			}
4c79b5
4c79b5
			// Passworded forum?
4c79b5
			if ($post['forum_id'])
4c79b5
			{
4c79b5
				$sql = 'SELECT forum_password
4c79b5
					FROM ' . FORUMS_TABLE . '
4c79b5
					WHERE forum_id = ' . (int) $post['forum_id'];
4c79b5
				$result = $db->sql_query($sql);
4c79b5
				$forum_password = (string) $db->sql_fetchfield('forum_password');
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				if ($forum_password)
4c79b5
				{
4c79b5
					login_forum_box(array(
4c79b5
						'forum_id'			=> $post['forum_id'],
4c79b5
						'forum_password'	=> $forum_password,
4c79b5
					));
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$msg_id			= (int) $post['msg_id'];
4c79b5
		$folder_id		= (isset($post['folder_id'])) ? $post['folder_id'] : 0;
4c79b5
		$message_text	= (isset($post['message_text'])) ? $post['message_text'] : '';
4c79b5
4c79b5
		if ((!$post['author_id'] || ($post['author_id'] == ANONYMOUS && $action != 'delete')) && $msg_id)
4c79b5
		{
4c79b5
			trigger_error('NO_AUTHOR');
4c79b5
		}
4c79b5
4c79b5
		if ($action == 'quotepost')
4c79b5
		{
4c79b5
			// Decode text for message display
4c79b5
			decode_message($message_text, $post['bbcode_uid']);
4c79b5
		}
4c79b5
4c79b5
		if ($action != 'delete')
4c79b5
		{
4c79b5
			$enable_urls = $post['enable_magic_url'];
4c79b5
			$enable_sig = (isset($post['enable_sig'])) ? $post['enable_sig'] : 0;
4c79b5
4c79b5
			$message_attachment = (isset($post['message_attachment'])) ? $post['message_attachment'] : 0;
4c79b5
			$message_subject = $post['message_subject'];
4c79b5
			$message_time = $post['message_time'];
4c79b5
			$bbcode_uid = $post['bbcode_uid'];
4c79b5
4c79b5
			$quote_username = (isset($post['quote_username'])) ? $post['quote_username'] : '';
4c79b5
			$icon_id = (isset($post['icon_id'])) ? $post['icon_id'] : 0;
4c79b5
4c79b5
			if (($action == 'reply' || $action == 'quote' || $action == 'quotepost') && !sizeof($address_list) && !$refresh && !$submit && !$preview)
4c79b5
			{
4c79b5
				if ($action == 'quotepost')
4c79b5
				{
4c79b5
					$address_list = array('u' => array($post['author_id'] => 'to'));
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					// We try to include every previously listed member from the TO Header
4c79b5
					$address_list = rebuild_header(array('to' => $post['to_address']));
4c79b5
4c79b5
					// Add the author (if he is already listed then this is no shame (it will be overwritten))
4c79b5
					$address_list['u'][$post['author_id']] = 'to';
4c79b5
4c79b5
					// Now, make sure the user itself is not listed. ;)
4c79b5
					if (isset($address_list['u'][$user->data['user_id']]))
4c79b5
					{
4c79b5
						unset($address_list['u'][$user->data['user_id']]);
4c79b5
					}
4c79b5
				}
4c79b5
			}
4c79b5
			else if ($action == 'edit' && !sizeof($address_list) && !$refresh && !$submit && !$preview)
4c79b5
			{
4c79b5
				// Rebuild TO and BCC Header
4c79b5
				$address_list = rebuild_header(array('to' => $post['to_address'], 'bcc' => $post['bcc_address']));
4c79b5
			}
4c79b5
4c79b5
			if ($action == 'quotepost')
4c79b5
			{
4c79b5
				$check_value = 0;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$check_value = (($post['enable_bbcode']+1) << 8) + (($post['enable_smilies']+1) << 4) + (($enable_urls+1) << 2) + (($post['enable_sig']+1) << 1);
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$message_attachment = 0;
4c79b5
		$message_text = $message_subject = '';
4c79b5
4c79b5
		if ($to_user_id && $action == 'post')
4c79b5
		{
4c79b5
			$address_list['u'][$to_user_id] = 'to';
4c79b5
		}
4c79b5
		else if ($to_group_id && $action == 'post')
4c79b5
		{
4c79b5
			$address_list['g'][$to_group_id] = 'to';
4c79b5
		}
4c79b5
		$check_value = 0;
4c79b5
	}
4c79b5
4c79b5
	if (($to_group_id || isset($address_list['g'])) && (!$config['allow_mass_pm'] || !$auth->acl_get('u_masspm_group')))
4c79b5
	{
4c79b5
		trigger_error('NO_AUTH_GROUP_MESSAGE');
4c79b5
	}
4c79b5
4c79b5
	if ($action == 'edit' && !$refresh && !$preview && !$submit)
4c79b5
	{
4c79b5
		if (!($message_time > time() - ($config['pm_edit_time'] * 60) || !$config['pm_edit_time']))
4c79b5
		{
4c79b5
			trigger_error('CANNOT_EDIT_MESSAGE_TIME');
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if ($action == 'post')
4c79b5
	{
4c79b5
		$template->assign_var('S_NEW_MESSAGE', true);
4c79b5
	}
4c79b5
4c79b5
	if (!isset($icon_id))
4c79b5
	{
4c79b5
		$icon_id = 0;
4c79b5
	}
4c79b5
4c79b5
	$message_parser = new parse_message();
4c79b5
4c79b5
	$message_parser->message = ($action == 'reply') ? '' : $message_text;
4c79b5
	unset($message_text);
4c79b5
4c79b5
	$s_action = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=$mode&action=$action", true, $user->session_id);
4c79b5
	$s_action .= ($msg_id) ? "&p=$msg_id" : '';
4c79b5
4c79b5
	// Delete triggered ?
4c79b5
	if ($action == 'delete')
4c79b5
	{
4c79b5
		// Folder id has been determined by the SQL Statement
4c79b5
		// $folder_id = request_var('f', PRIVMSGS_NO_BOX);
4c79b5
4c79b5
		// Do we need to confirm ?
4c79b5
		if (confirm_box(true))
4c79b5
		{
4c79b5
			delete_pm($user->data['user_id'], $msg_id, $folder_id);
4c79b5
4c79b5
			// jump to next message in "history"? nope, not for the moment. But able to be included later.
4c79b5
			$meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&folder=$folder_id");
4c79b5
			$message = $user->lang['MESSAGE_DELETED'];
4c79b5
4c79b5
			meta_refresh(3, $meta_info);
4c79b5
			$message .= '

' . sprintf($user->lang['RETURN_FOLDER'], '', '');
4c79b5
			trigger_error($message);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$s_hidden_fields = array(
4c79b5
				'p'			=> $msg_id,
4c79b5
				'f'			=> $folder_id,
4c79b5
				'action'	=> 'delete'
4c79b5
			);
4c79b5
4c79b5
			// "{$phpbb_root_path}ucp.$phpEx?i=pm&mode=compose"
4c79b5
			confirm_box(false, 'DELETE_MESSAGE', build_hidden_fields($s_hidden_fields));
4c79b5
		}
4c79b5
4c79b5
		redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=view&action=view_message&p=' . $msg_id));
4c79b5
	}
4c79b5
4c79b5
	// Get maximum number of allowed recipients
4c79b5
	$sql = 'SELECT MAX(g.group_max_recipients) as max_recipients
4c79b5
		FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . ' ug
4c79b5
		WHERE ug.user_id = ' . $user->data['user_id'] . '
4c79b5
			AND ug.user_pending = 0
4c79b5
			AND ug.group_id = g.group_id';
4c79b5
	$result = $db->sql_query($sql);
4c79b5
	$max_recipients = (int) $db->sql_fetchfield('max_recipients');
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	$max_recipients = (!$max_recipients) ? $config['pm_max_recipients'] : $max_recipients;
4c79b5
4c79b5
	// If this is a quote/reply "to all"... we may increase the max_recpients to the number of original recipients
4c79b5
	if (($action == 'reply' || $action == 'quote') && $max_recipients)
4c79b5
	{
4c79b5
		// We try to include every previously listed member from the TO Header
4c79b5
		$list = rebuild_header(array('to' => $post['to_address']));
4c79b5
4c79b5
		// Can be an empty array too ;)
4c79b5
		$list = (!empty($list['u'])) ? $list['u'] : array();
4c79b5
		$list[$post['author_id']] = 'to';
4c79b5
4c79b5
		if (isset($list[$user->data['user_id']]))
4c79b5
		{
4c79b5
			unset($list[$user->data['user_id']]);
4c79b5
		}
4c79b5
4c79b5
		$max_recipients = ($max_recipients < sizeof($list)) ? sizeof($list) : $max_recipients;
4c79b5
4c79b5
		unset($list);
4c79b5
	}
4c79b5
4c79b5
	// Handle User/Group adding/removing
4c79b5
	handle_message_list_actions($address_list, $error, $remove_u, $remove_g, $add_to, $add_bcc);
4c79b5
4c79b5
	// Check mass pm to group permission
4c79b5
	if ((!$config['allow_mass_pm'] || !$auth->acl_get('u_masspm_group')) && !empty($address_list['g']))
4c79b5
	{
4c79b5
		$address_list = array();
4c79b5
		$error[] = $user->lang['NO_AUTH_GROUP_MESSAGE'];
4c79b5
	}
4c79b5
4c79b5
	// Check mass pm to users permission
4c79b5
	if ((!$config['allow_mass_pm'] || !$auth->acl_get('u_masspm')) && num_recipients($address_list) > 1)
4c79b5
	{
4c79b5
		$address_list = get_recipients($address_list, 1);
4c79b5
		$error[] = $user->lang('TOO_MANY_RECIPIENTS', 1);
4c79b5
	}
4c79b5
4c79b5
	// Check for too many recipients
4c79b5
	if (!empty($address_list['u']) && $max_recipients && sizeof($address_list['u']) > $max_recipients)
4c79b5
	{
4c79b5
		$address_list = get_recipients($address_list, $max_recipients);
4c79b5
		$error[] = $user->lang('TOO_MANY_RECIPIENTS', $max_recipients);
4c79b5
	}
4c79b5
4c79b5
	// Always check if the submitted attachment data is valid and belongs to the user.
4c79b5
	// Further down (especially in submit_post()) we do not check this again.
4c79b5
	$message_parser->get_submitted_attachment_data();
4c79b5
4c79b5
	if ($message_attachment && !$submit && !$refresh && !$preview && $action == 'edit')
4c79b5
	{
4c79b5
		// Do not change to SELECT *
4c79b5
		$sql = 'SELECT attach_id, is_orphan, attach_comment, real_filename
4c79b5
			FROM ' . ATTACHMENTS_TABLE . "
4c79b5
			WHERE post_msg_id = $msg_id
4c79b5
				AND in_message = 1
4c79b5
				AND is_orphan = 0
4c79b5
			ORDER BY filetime DESC";
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$message_parser->attachment_data = array_merge($message_parser->attachment_data, $db->sql_fetchrowset($result));
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	if (!in_array($action, array('quote', 'edit', 'delete', 'forward')))
4c79b5
	{
4c79b5
		$enable_sig		= ($config['allow_sig'] && $config['allow_sig_pm'] && $auth->acl_get('u_sig') && $user->optionget('attachsig'));
4c79b5
		$enable_smilies	= ($config['allow_smilies'] && $auth->acl_get('u_pm_smilies') && $user->optionget('smilies'));
4c79b5
		$enable_bbcode	= ($config['allow_bbcode'] && $auth->acl_get('u_pm_bbcode') && $user->optionget('bbcode'));
4c79b5
		$enable_urls	= true;
4c79b5
	}
4c79b5
4c79b5
	$enable_magic_url = $drafts = false;
4c79b5
4c79b5
	// User own some drafts?
4c79b5
	if ($auth->acl_get('u_savedrafts') && $action != 'delete')
4c79b5
	{
4c79b5
		$sql = 'SELECT draft_id
4c79b5
			FROM ' . DRAFTS_TABLE . '
4c79b5
			WHERE forum_id = 0
4c79b5
				AND topic_id = 0
4c79b5
				AND user_id = ' . $user->data['user_id'] .
4c79b5
				(($draft_id) ? " AND draft_id <> $draft_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
			$drafts = true;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	if ($action == 'edit')
4c79b5
	{
4c79b5
		$message_parser->bbcode_uid = $bbcode_uid;
4c79b5
	}
4c79b5
4c79b5
	$bbcode_status	= ($config['allow_bbcode'] && $config['auth_bbcode_pm'] && $auth->acl_get('u_pm_bbcode')) ? true : false;
4c79b5
	$smilies_status	= ($config['allow_smilies'] && $config['auth_smilies_pm'] && $auth->acl_get('u_pm_smilies')) ? true : false;
4c79b5
	$img_status		= ($config['auth_img_pm'] && $auth->acl_get('u_pm_img')) ? true : false;
4c79b5
	$flash_status	= ($config['auth_flash_pm'] && $auth->acl_get('u_pm_flash')) ? true : false;
4c79b5
	$url_status		= ($config['allow_post_links']) ? true : false;
4c79b5
4c79b5
	// Save Draft
4c79b5
	if ($save && $auth->acl_get('u_savedrafts'))
4c79b5
	{
4c79b5
		$subject = utf8_normalize_nfc(request_var('subject', '', true));
4c79b5
		$subject = (!$subject && $action != 'post') ? $user->lang['NEW_MESSAGE'] : $subject;
4c79b5
		$message = utf8_normalize_nfc(request_var('message', '', true));
4c79b5
4c79b5
		if ($subject && $message)
4c79b5
		{
4c79b5
			if (confirm_box(true))
4c79b5
			{
4c79b5
				$sql = 'INSERT INTO ' . DRAFTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
4c79b5
					'user_id'		=> $user->data['user_id'],
4c79b5
					'topic_id'		=> 0,
4c79b5
					'forum_id'		=> 0,
4c79b5
					'save_time'		=> $current_time,
4c79b5
					'draft_subject'	=> $subject,
4c79b5
					'draft_message'	=> $message
4c79b5
					)
4c79b5
				);
4c79b5
				$db->sql_query($sql);
4c79b5
4c79b5
				$redirect_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=$mode");
4c79b5
4c79b5
				meta_refresh(3, $redirect_url);
4c79b5
				$message = $user->lang['DRAFT_SAVED'] . '

' . sprintf($user->lang['RETURN_UCP'], '', '');
4c79b5
4c79b5
				trigger_error($message);
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$s_hidden_fields = build_hidden_fields(array(
4c79b5
					'mode'		=> $mode,
4c79b5
					'action'	=> $action,
4c79b5
					'save'		=> true,
4c79b5
					'subject'	=> $subject,
4c79b5
					'message'	=> $message,
4c79b5
					'u'			=> $to_user_id,
4c79b5
					'g'			=> $to_group_id,
4c79b5
					'p'			=> $msg_id)
4c79b5
				);
4c79b5
				$s_hidden_fields .= build_address_field($address_list);
4c79b5
4c79b5
4c79b5
				confirm_box(false, 'SAVE_DRAFT', $s_hidden_fields);
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			if (utf8_clean_string($subject) === '')
4c79b5
			{
4c79b5
				$error[] = $user->lang['EMPTY_MESSAGE_SUBJECT'];
4c79b5
			}
4c79b5
4c79b5
			if (utf8_clean_string($message) === '')
4c79b5
			{
4c79b5
				$error[] = $user->lang['TOO_FEW_CHARS'];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		unset($subject, $message);
4c79b5
	}
4c79b5
4c79b5
	// Load Draft
4c79b5
	if ($draft_id && $auth->acl_get('u_savedrafts'))
4c79b5
	{
4c79b5
		$sql = 'SELECT draft_subject, draft_message
4c79b5
			FROM ' . DRAFTS_TABLE . "
4c79b5
			WHERE draft_id = $draft_id
4c79b5
				AND topic_id = 0
4c79b5
				AND forum_id = 0
4c79b5
				AND user_id = " . $user->data['user_id'];
4c79b5
		$result = $db->sql_query_limit($sql, 1);
4c79b5
4c79b5
		if ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$message_parser->message = $row['draft_message'];
4c79b5
			$message_subject = $row['draft_subject'];
4c79b5
4c79b5
			$template->assign_var('S_DRAFT_LOADED', true);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$draft_id = 0;
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	// Load Drafts
4c79b5
	if ($load && $drafts)
4c79b5
	{
4c79b5
		load_drafts(0, 0, $id);
4c79b5
	}
4c79b5
4c79b5
	if ($submit || $preview || $refresh)
4c79b5
	{
4c79b5
		if (($submit || $preview) && !check_form_key('ucp_pm_compose'))
4c79b5
		{
4c79b5
			$error[] = $user->lang['FORM_INVALID'];
4c79b5
		}
4c79b5
		$subject = utf8_normalize_nfc(request_var('subject', '', true));
4c79b5
		$message_parser->message = utf8_normalize_nfc(request_var('message', '', true));
4c79b5
4c79b5
		$icon_id			= request_var('icon', 0);
4c79b5
4c79b5
		$enable_bbcode 		= (!$bbcode_status || isset($_POST['disable_bbcode'])) ? false : true;
4c79b5
		$enable_smilies		= (!$smilies_status || isset($_POST['disable_smilies'])) ? false : true;
4c79b5
		$enable_urls 		= (isset($_POST['disable_magic_url'])) ? 0 : 1;
4c79b5
		$enable_sig			= (!$config['allow_sig'] ||!$config['allow_sig_pm']) ? false : ((isset($_POST['attach_sig'])) ? true : false);
4c79b5
4c79b5
		if ($submit)
4c79b5
		{
4c79b5
			$status_switch	= (($enable_bbcode+1) << 8) + (($enable_smilies+1) << 4) + (($enable_urls+1) << 2) + (($enable_sig+1) << 1);
4c79b5
			$status_switch = ($status_switch != $check_value);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$status_switch = 1;
4c79b5
		}
4c79b5
4c79b5
		// Parse Attachments - before checksum is calculated
4c79b5
		$message_parser->parse_attachments('fileupload', $action, 0, $submit, $preview, $refresh, true);
4c79b5
4c79b5
		if (sizeof($message_parser->warn_msg) && !($remove_u || $remove_g || $add_to || $add_bcc))
4c79b5
		{
4c79b5
			$error[] = implode('
', $message_parser->warn_msg);
4c79b5
			$message_parser->warn_msg = array();
4c79b5
		}
4c79b5
4c79b5
		// Parse message
4c79b5
		$message_parser->parse($enable_bbcode, ($config['allow_post_links']) ? $enable_urls : false, $enable_smilies, $img_status, $flash_status, true, $config['allow_post_links']);
4c79b5
4c79b5
		// On a refresh we do not care about message parsing errors
4c79b5
		if (sizeof($message_parser->warn_msg) && !$refresh)
4c79b5
		{
4c79b5
			$error[] = implode('
', $message_parser->warn_msg);
4c79b5
		}
4c79b5
4c79b5
		if ($action != 'edit' && !$preview && !$refresh && $config['flood_interval'] && !$auth->acl_get('u_ignoreflood'))
4c79b5
		{
4c79b5
			// Flood check
4c79b5
			$last_post_time = $user->data['user_lastpost_time'];
4c79b5
4c79b5
			if ($last_post_time)
4c79b5
			{
4c79b5
				if ($last_post_time && ($current_time - $last_post_time) < intval($config['flood_interval']))
4c79b5
				{
4c79b5
					$error[] = $user->lang['FLOOD_ERROR'];
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Subject defined
4c79b5
		if ($submit)
4c79b5
		{
4c79b5
			if (utf8_clean_string($subject) === '')
4c79b5
			{
4c79b5
				$error[] = $user->lang['EMPTY_MESSAGE_SUBJECT'];
4c79b5
			}
4c79b5
4c79b5
			if (!sizeof($address_list))
4c79b5
			{
4c79b5
				$error[] = $user->lang['NO_RECIPIENT'];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Store message, sync counters
4c79b5
		if (!sizeof($error) && $submit)
4c79b5
		{
4c79b5
			$pm_data = array(
4c79b5
				'msg_id'				=> (int) $msg_id,
4c79b5
				'from_user_id'			=> $user->data['user_id'],
4c79b5
				'from_user_ip'			=> $user->ip,
4c79b5
				'from_username'			=> $user->data['username'],
4c79b5
				'reply_from_root_level'	=> (isset($post['root_level'])) ? (int) $post['root_level'] : 0,
4c79b5
				'reply_from_msg_id'		=> (int) $msg_id,
4c79b5
				'icon_id'				=> (int) $icon_id,
4c79b5
				'enable_sig'			=> (bool) $enable_sig,
4c79b5
				'enable_bbcode'			=> (bool) $enable_bbcode,
4c79b5
				'enable_smilies'		=> (bool) $enable_smilies,
4c79b5
				'enable_urls'			=> (bool) $enable_urls,
4c79b5
				'bbcode_bitfield'		=> $message_parser->bbcode_bitfield,
4c79b5
				'bbcode_uid'			=> $message_parser->bbcode_uid,
4c79b5
				'message'				=> $message_parser->message,
4c79b5
				'attachment_data'		=> $message_parser->attachment_data,
4c79b5
				'filename_data'			=> $message_parser->filename_data,
4c79b5
				'address_list'			=> $address_list
4c79b5
			);
4c79b5
4c79b5
			// ((!$message_subject) ? $subject : $message_subject)
4c79b5
			$msg_id = submit_pm($action, $subject, $pm_data);
4c79b5
4c79b5
			$return_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=view&p=' . $msg_id);
4c79b5
			$return_folder_url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=outbox');
4c79b5
			meta_refresh(3, $return_message_url);
4c79b5
4c79b5
			$message = $user->lang['MESSAGE_STORED'] . '

' . sprintf($user->lang['VIEW_PRIVATE_MESSAGE'], '', '') . '

' . sprintf($user->lang['CLICK_RETURN_FOLDER'], '', '', $user->lang['PM_OUTBOX']);
4c79b5
			trigger_error($message);
4c79b5
		}
4c79b5
4c79b5
		$message_subject = $subject;
4c79b5
	}
4c79b5
4c79b5
	// Preview
4c79b5
	if (!sizeof($error) && $preview)
4c79b5
	{
4c79b5
		$user->add_lang('viewtopic');
4c79b5
		$preview_message = $message_parser->format_display($enable_bbcode, $enable_urls, $enable_smilies, false);
4c79b5
4c79b5
		$preview_signature = $user->data['user_sig'];
4c79b5
		$preview_signature_uid = $user->data['user_sig_bbcode_uid'];
4c79b5
		$preview_signature_bitfield = $user->data['user_sig_bbcode_bitfield'];
4c79b5
4c79b5
		// Signature
4c79b5
		if ($enable_sig && $config['allow_sig'] && $preview_signature)
4c79b5
		{
4c79b5
			$parse_sig = new parse_message($preview_signature);
4c79b5
			$parse_sig->bbcode_uid = $preview_signature_uid;
4c79b5
			$parse_sig->bbcode_bitfield = $preview_signature_bitfield;
4c79b5
4c79b5
			$parse_sig->format_display($enable_bbcode, $enable_urls, $enable_smilies);
4c79b5
			$preview_signature = $parse_sig->message;
4c79b5
			unset($parse_sig);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$preview_signature = '';
4c79b5
		}
4c79b5
4c79b5
		// Attachment Preview
4c79b5
		if (sizeof($message_parser->attachment_data))
4c79b5
		{
4c79b5
			$template->assign_var('S_HAS_ATTACHMENTS', true);
4c79b5
4c79b5
			$update_count = array();
4c79b5
			$attachment_data = $message_parser->attachment_data;
4c79b5
4c79b5
			parse_attachments(false, $preview_message, $attachment_data, $update_count, true);
4c79b5
4c79b5
			foreach ($attachment_data as $i => $attachment)
4c79b5
			{
4c79b5
				$template->assign_block_vars('attachment', array(
4c79b5
					'DISPLAY_ATTACHMENT'	=> $attachment)
4c79b5
				);
4c79b5
			}
4c79b5
			unset($attachment_data);
4c79b5
		}
4c79b5
4c79b5
		$preview_subject = censor_text($subject);
4c79b5
4c79b5
		if (!sizeof($error))
4c79b5
		{
4c79b5
			$template->assign_vars(array(
4c79b5
				'PREVIEW_SUBJECT'		=> $preview_subject,
4c79b5
				'PREVIEW_MESSAGE'		=> $preview_message,
4c79b5
				'PREVIEW_SIGNATURE'		=> $preview_signature,
4c79b5
4c79b5
				'S_DISPLAY_PREVIEW'		=> true)
4c79b5
			);
4c79b5
		}
4c79b5
		unset($message_text);
4c79b5
	}
4c79b5
4c79b5
	// Decode text for message display
4c79b5
	$bbcode_uid = (($action == 'quote' || $action == 'forward') && !$preview && !$refresh && !sizeof($error)) ? $bbcode_uid : $message_parser->bbcode_uid;
4c79b5
4c79b5
	$message_parser->decode_message($bbcode_uid);
4c79b5
4c79b5
	if (($action == 'quote' || $action == 'quotepost') && !$preview && !$refresh && !$submit)
4c79b5
	{
4c79b5
		if ($action == 'quotepost')
4c79b5
		{
4c79b5
			$post_id = request_var('p', 0);
4c79b5
			if ($config['allow_post_links'])
4c79b5
			{
4c79b5
				$message_link = "[url=" . generate_board_url() . "/viewtopic.$phpEx?p={$post_id}#p{$post_id}]{$user->lang['SUBJECT']}: {$message_subject}[/url]\n\n";
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$message_link = $user->lang['SUBJECT'] . ': ' . $message_subject . " (" . generate_board_url() . "/viewtopic.$phpEx?p={$post_id}#p{$post_id})\n\n";
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$message_link = '';
4c79b5
		}
4c79b5
		$message_parser->message = $message_link . '[quote="' . $quote_username . '"]' . censor_text(trim($message_parser->message)) . "[/quote]\n";
4c79b5
	}
4c79b5
4c79b5
	if (($action == 'reply' || $action == 'quote' || $action == 'quotepost') && !$preview && !$refresh)
4c79b5
	{
4c79b5
		$message_subject = ((!preg_match('/^Re:/', $message_subject)) ? 'Re: ' : '') . censor_text($message_subject);
4c79b5
	}
4c79b5
4c79b5
	if ($action == 'forward' && !$preview && !$refresh && !$submit)
4c79b5
	{
4c79b5
		$fwd_to_field = write_pm_addresses(array('to' => $post['to_address']), 0, true);
4c79b5
4c79b5
		if ($config['allow_post_links'])
4c79b5
		{
4c79b5
			$quote_username_text = '[url=' . generate_board_url() . "/memberlist.$phpEx?mode=viewprofile&u={$post['author_id']}]{$quote_username}[/url]";
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$quote_username_text = $quote_username . ' (' . generate_board_url() . "/memberlist.$phpEx?mode=viewprofile&u={$post['author_id']})";
4c79b5
		}
4c79b5
4c79b5
		$forward_text = array();
4c79b5
		$forward_text[] = $user->lang['FWD_ORIGINAL_MESSAGE'];
4c79b5
		$forward_text[] = sprintf($user->lang['FWD_SUBJECT'], censor_text($message_subject));
4c79b5
		$forward_text[] = sprintf($user->lang['FWD_DATE'], $user->format_date($message_time));
4c79b5
		$forward_text[] = sprintf($user->lang['FWD_FROM'], $quote_username_text);
4c79b5
		$forward_text[] = sprintf($user->lang['FWD_TO'], implode(', ', $fwd_to_field['to']));
4c79b5
4c79b5
		$message_parser->message = implode("\n", $forward_text) . "\n\n[quote="{$quote_username}"]\n" . censor_text(trim($message_parser->message)) . "\n[/quote]";
4c79b5
		$message_subject = ((!preg_match('/^Fwd:/', $message_subject)) ? 'Fwd: ' : '') . censor_text($message_subject);
4c79b5
	}
4c79b5
4c79b5
	$attachment_data = $message_parser->attachment_data;
4c79b5
	$filename_data = $message_parser->filename_data;
4c79b5
	$message_text = $message_parser->message;
4c79b5
4c79b5
	// MAIN PM PAGE BEGINS HERE
4c79b5
4c79b5
	// Generate smiley listing
4c79b5
	generate_smilies('inline', 0);
4c79b5
4c79b5
	// Generate PM Icons
4c79b5
	$s_pm_icons = false;
4c79b5
	if ($config['enable_pm_icons'])
4c79b5
	{
4c79b5
		$s_pm_icons = posting_gen_topic_icons($action, $icon_id);
4c79b5
	}
4c79b5
4c79b5
	// Generate inline attachment select box
4c79b5
	posting_gen_inline_attachments($attachment_data);
4c79b5
4c79b5
	// Build address list for display
4c79b5
	// array('u' => array($author_id => 'to'));
4c79b5
	if (sizeof($address_list))
4c79b5
	{
4c79b5
		// Get Usernames and Group Names
4c79b5
		$result = array();
4c79b5
		if (!empty($address_list['u']))
4c79b5
		{
4c79b5
			$sql = 'SELECT user_id as id, username as name, user_colour as colour
4c79b5
				FROM ' . USERS_TABLE . '
4c79b5
				WHERE ' . $db->sql_in_set('user_id', array_map('intval', array_keys($address_list['u']))) . '
4c79b5
				ORDER BY username_clean ASC';
4c79b5
			$result['u'] = $db->sql_query($sql);
4c79b5
		}
4c79b5
4c79b5
		if (!empty($address_list['g']))
4c79b5
		{
4c79b5
			$sql = 'SELECT g.group_id AS id, g.group_name AS name, g.group_colour AS colour, 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 (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $user->data['user_id'] . ')';
4c79b5
			}
4c79b5
4c79b5
			$sql .= ($auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) ? ' WHERE ' : ' AND ';
4c79b5
4c79b5
			$sql .= 'g.group_receive_pm = 1
4c79b5
				AND ' . $db->sql_in_set('g.group_id', array_map('intval', array_keys($address_list['g']))) . '
4c79b5
				ORDER BY g.group_name ASC';
4c79b5
4c79b5
			$result['g'] = $db->sql_query($sql);
4c79b5
		}
4c79b5
4c79b5
		$u = $g = array();
4c79b5
		$_types = array('u', 'g');
4c79b5
		foreach ($_types as $type)
4c79b5
		{
4c79b5
			if (isset($result[$type]) && $result[$type])
4c79b5
			{
4c79b5
				while ($row = $db->sql_fetchrow($result[$type]))
4c79b5
				{
4c79b5
					if ($type == 'g')
4c79b5
					{
4c79b5
						$row['name'] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['name']] : $row['name'];
4c79b5
					}
4c79b5
4c79b5
					${$type}[$row['id']] = array('name' => $row['name'], 'colour' => $row['colour']);
4c79b5
				}
4c79b5
				$db->sql_freeresult($result[$type]);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Now Build the address list
4c79b5
		$plain_address_field = '';
4c79b5
		foreach ($address_list as $type => $adr_ary)
4c79b5
		{
4c79b5
			foreach ($adr_ary as $id => $field)
4c79b5
			{
4c79b5
				if (!isset(${$type}[$id]))
4c79b5
				{
4c79b5
					unset($address_list[$type][$id]);
4c79b5
					continue;
4c79b5
				}
4c79b5
4c79b5
				$field = ($field == 'to') ? 'to' : 'bcc';
4c79b5
				$type = ($type == 'u') ? 'u' : 'g';
4c79b5
				$id = (int) $id;
4c79b5
4c79b5
				$tpl_ary = array(
4c79b5
					'IS_GROUP'	=> ($type == 'g') ? true : false,
4c79b5
					'IS_USER'	=> ($type == 'u') ? true : false,
4c79b5
					'UG_ID'		=> $id,
4c79b5
					'NAME'		=> ${$type}[$id]['name'],
4c79b5
					'COLOUR'	=> (${$type}[$id]['colour']) ? '#' . ${$type}[$id]['colour'] : '',
4c79b5
					'TYPE'		=> $type,
4c79b5
				);
4c79b5
4c79b5
				if ($type == 'u')
4c79b5
				{
4c79b5
					$tpl_ary = array_merge($tpl_ary, array(
4c79b5
						'U_VIEW'		=> get_username_string('profile', $id, ${$type}[$id]['name'], ${$type}[$id]['colour']),
4c79b5
						'NAME_FULL'		=> get_username_string('full', $id, ${$type}[$id]['name'], ${$type}[$id]['colour']),
4c79b5
					));
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$tpl_ary = array_merge($tpl_ary, array(
4c79b5
						'U_VIEW'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&g=' . $id),
4c79b5
					));
4c79b5
				}
4c79b5
4c79b5
				$template->assign_block_vars($field . '_recipient', $tpl_ary);
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Build hidden address list
4c79b5
	$s_hidden_address_field = build_address_field($address_list);
4c79b5
4c79b5
4c79b5
	$bbcode_checked		= (isset($enable_bbcode)) ? !$enable_bbcode : (($config['allow_bbcode'] && $auth->acl_get('u_pm_bbcode')) ? !$user->optionget('bbcode') : 1);
4c79b5
	$smilies_checked	= (isset($enable_smilies)) ? !$enable_smilies : (($config['allow_smilies'] && $auth->acl_get('u_pm_smilies')) ? !$user->optionget('smilies') : 1);
4c79b5
	$urls_checked		= (isset($enable_urls)) ? !$enable_urls : 0;
4c79b5
	$sig_checked		= $enable_sig;
4c79b5
4c79b5
	switch ($action)
4c79b5
	{
4c79b5
		case 'post':
4c79b5
			$page_title = $user->lang['POST_NEW_PM'];
4c79b5
		break;
4c79b5
4c79b5
		case 'quote':
4c79b5
			$page_title = $user->lang['POST_QUOTE_PM'];
4c79b5
		break;
4c79b5
4c79b5
		case 'quotepost':
4c79b5
			$page_title = $user->lang['POST_PM_POST'];
4c79b5
		break;
4c79b5
4c79b5
		case 'reply':
4c79b5
			$page_title = $user->lang['POST_REPLY_PM'];
4c79b5
		break;
4c79b5
4c79b5
		case 'edit':
4c79b5
			$page_title = $user->lang['POST_EDIT_PM'];
4c79b5
		break;
4c79b5
4c79b5
		case 'forward':
4c79b5
			$page_title = $user->lang['POST_FORWARD_PM'];
4c79b5
		break;
4c79b5
4c79b5
		default:
4c79b5
			trigger_error('NO_ACTION_MODE', E_USER_ERROR);
4c79b5
		break;
4c79b5
	}
4c79b5
4c79b5
	$s_hidden_fields = '<input type="hidden" name="lastclick" value="' . $current_time . '" />';
4c79b5
	$s_hidden_fields .= (isset($check_value)) ? '<input type="hidden" name="status_switch" value="' . $check_value . '" />' : '';
4c79b5
	$s_hidden_fields .= ($draft_id || isset($_REQUEST['draft_loaded'])) ? '<input type="hidden" name="draft_loaded" value="' . ((isset($_REQUEST['draft_loaded'])) ? intval($_REQUEST['draft_loaded']) : $draft_id) . '" />' : '';
4c79b5
4c79b5
	$form_enctype = (@ini_get('file_uploads') == '0' || strtolower(@ini_get('file_uploads')) == 'off' || !$config['allow_pm_attach'] || !$auth->acl_get('u_pm_attach')) ? '' : ' enctype="multipart/form-data"';
4c79b5
4c79b5
	// Start assigning vars for main posting page ...
4c79b5
	$template->assign_vars(array(
4c79b5
		'L_POST_A'					=> $page_title,
4c79b5
		'L_ICON'					=> $user->lang['PM_ICON'],
4c79b5
		'L_MESSAGE_BODY_EXPLAIN'	=> (intval($config['max_post_chars'])) ? sprintf($user->lang['MESSAGE_BODY_EXPLAIN'], intval($config['max_post_chars'])) : '',
4c79b5
4c79b5
		'SUBJECT'				=> (isset($message_subject)) ? $message_subject : '',
4c79b5
		'MESSAGE'				=> $message_text,
4c79b5
		'BBCODE_STATUS'			=> ($bbcode_status) ? sprintf($user->lang['BBCODE_IS_ON'], '', '') : sprintf($user->lang['BBCODE_IS_OFF'], '', ''),
4c79b5
		'IMG_STATUS'			=> ($img_status) ? $user->lang['IMAGES_ARE_ON'] : $user->lang['IMAGES_ARE_OFF'],
4c79b5
		'FLASH_STATUS'			=> ($flash_status) ? $user->lang['FLASH_IS_ON'] : $user->lang['FLASH_IS_OFF'],
4c79b5
		'SMILIES_STATUS'		=> ($smilies_status) ? $user->lang['SMILIES_ARE_ON'] : $user->lang['SMILIES_ARE_OFF'],
4c79b5
		'URL_STATUS'			=> ($url_status) ? $user->lang['URL_IS_ON'] : $user->lang['URL_IS_OFF'],
4c79b5
		'MINI_POST_IMG'			=> $user->img('icon_post_target', $user->lang['PM']),
4c79b5
		'ERROR'					=> (sizeof($error)) ? implode('
', $error) : '',
4c79b5
		'MAX_RECIPIENTS'		=> ($config['allow_mass_pm'] && ($auth->acl_get('u_masspm') || $auth->acl_get('u_masspm_group'))) ? $max_recipients : 0,
4c79b5
4c79b5
		'S_COMPOSE_PM'			=> true,
4c79b5
		'S_EDIT_POST'			=> ($action == 'edit'),
4c79b5
		'S_SHOW_PM_ICONS'		=> $s_pm_icons,
4c79b5
		'S_BBCODE_ALLOWED'		=> $bbcode_status,
4c79b5
		'S_BBCODE_CHECKED'		=> ($bbcode_checked) ? ' checked="checked"' : '',
4c79b5
		'S_SMILIES_ALLOWED'		=> $smilies_status,
4c79b5
		'S_SMILIES_CHECKED'		=> ($smilies_checked) ? ' checked="checked"' : '',
4c79b5
		'S_SIG_ALLOWED'			=> ($config['allow_sig'] && $config['allow_sig_pm'] && $auth->acl_get('u_sig')),
4c79b5
		'S_SIGNATURE_CHECKED'	=> ($sig_checked) ? ' checked="checked"' : '',
4c79b5
		'S_LINKS_ALLOWED'		=> $url_status,
4c79b5
		'S_MAGIC_URL_CHECKED'	=> ($urls_checked) ? ' checked="checked"' : '',
4c79b5
		'S_SAVE_ALLOWED'		=> ($auth->acl_get('u_savedrafts') && $action != 'edit') ? true : false,
4c79b5
		'S_HAS_DRAFTS'			=> ($auth->acl_get('u_savedrafts') && $drafts),
4c79b5
		'S_FORM_ENCTYPE'		=> $form_enctype,
4c79b5
4c79b5
		'S_BBCODE_IMG'			=> $img_status,
4c79b5
		'S_BBCODE_FLASH'		=> $flash_status,
4c79b5
		'S_BBCODE_QUOTE'		=> true,
4c79b5
		'S_BBCODE_URL'			=> $url_status,
4c79b5
4c79b5
		'S_POST_ACTION'				=> $s_action,
4c79b5
		'S_HIDDEN_ADDRESS_FIELD'	=> $s_hidden_address_field,
4c79b5
		'S_HIDDEN_FIELDS'			=> $s_hidden_fields,
4c79b5
4c79b5
		'S_CLOSE_PROGRESS_WINDOW'	=> isset($_POST['add_file']),
4c79b5
		'U_PROGRESS_BAR'			=> append_sid("{$phpbb_root_path}posting.$phpEx", 'f=0&mode=popup'),
4c79b5
		'UA_PROGRESS_BAR'			=> addslashes(append_sid("{$phpbb_root_path}posting.$phpEx", 'f=0&mode=popup')),
4c79b5
	));
4c79b5
4c79b5
	// Build custom bbcodes array
4c79b5
	display_custom_bbcodes();
4c79b5
4c79b5
	// Show attachment box for adding attachments if true
4c79b5
	$allowed = ($auth->acl_get('u_pm_attach') && $config['allow_pm_attach'] && $form_enctype);
4c79b5
4c79b5
	// Attachment entry
4c79b5
	posting_gen_attachment_entry($attachment_data, $filename_data, $allowed);
4c79b5
4c79b5
	// Message History
4c79b5
	if ($action == 'reply' || $action == 'quote' || $action == 'forward')
4c79b5
	{
4c79b5
		if (message_history($msg_id, $user->data['user_id'], $post, array(), true))
4c79b5
		{
4c79b5
			$template->assign_var('S_DISPLAY_HISTORY', true);
4c79b5
		}
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* For composing messages, handle list actions
4c79b5
*/
4c79b5
function handle_message_list_actions(&$address_list, &$error, $remove_u, $remove_g, $add_to, $add_bcc)
4c79b5
{
4c79b5
	global $auth, $db, $user;
4c79b5
4c79b5
	// Delete User [TO/BCC]
4c79b5
	if ($remove_u && !empty($_REQUEST['remove_u']) && is_array($_REQUEST['remove_u']))
4c79b5
	{
4c79b5
		$remove_user_id = array_keys($_REQUEST['remove_u']);
4c79b5
4c79b5
		if (isset($remove_user_id[0]))
4c79b5
		{
4c79b5
			unset($address_list['u'][(int) $remove_user_id[0]]);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Delete Group [TO/BCC]
4c79b5
	if ($remove_g && !empty($_REQUEST['remove_g']) && is_array($_REQUEST['remove_g']))
4c79b5
	{
4c79b5
		$remove_group_id = array_keys($_REQUEST['remove_g']);
4c79b5
4c79b5
		if (isset($remove_group_id[0]))
4c79b5
		{
4c79b5
			unset($address_list['g'][(int) $remove_group_id[0]]);
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Add Selected Groups
4c79b5
	$group_list = request_var('group_list', array(0));
4c79b5
4c79b5
	// Build usernames to add
4c79b5
	$usernames = (isset($_REQUEST['username'])) ? array(request_var('username', '', true)) : array();
4c79b5
	$username_list = request_var('username_list', '', true);
4c79b5
	if ($username_list)
4c79b5
	{
4c79b5
		$usernames = array_merge($usernames, explode("\n", $username_list));
4c79b5
	}
4c79b5
4c79b5
	// If add to or add bcc not pressed, users could still have usernames listed they want to add...
4c79b5
	if (!$add_to && !$add_bcc && (sizeof($group_list) || sizeof($usernames)))
4c79b5
	{
4c79b5
		$add_to = true;
4c79b5
4c79b5
		global $refresh, $submit, $preview;
4c79b5
4c79b5
		$refresh = $preview = true;
4c79b5
		$submit = false;
4c79b5
	}
4c79b5
4c79b5
	// Add User/Group [TO]
4c79b5
	if ($add_to || $add_bcc)
4c79b5
	{
4c79b5
		$type = ($add_to) ? 'to' : 'bcc';
4c79b5
4c79b5
		if (sizeof($group_list))
4c79b5
		{
4c79b5
			foreach ($group_list as $group_id)
4c79b5
			{
4c79b5
				$address_list['g'][$group_id] = $type;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// User ID's to add...
4c79b5
		$user_id_ary = array();
4c79b5
4c79b5
		// Reveal the correct user_ids
4c79b5
		if (sizeof($usernames))
4c79b5
		{
4c79b5
			$user_id_ary = array();
4c79b5
			user_get_id_name($user_id_ary, $usernames, array(USER_NORMAL, USER_FOUNDER, USER_INACTIVE));
4c79b5
4c79b5
			// If there are users not existing, we will at least print a notice...
4c79b5
			if (!sizeof($user_id_ary))
4c79b5
			{
4c79b5
				$error[] = $user->lang['PM_NO_USERS'];
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// Add Friends if specified
4c79b5
		$friend_list = (isset($_REQUEST['add_' . $type]) && is_array($_REQUEST['add_' . $type])) ? array_map('intval', array_keys($_REQUEST['add_' . $type])) : array();
4c79b5
		$user_id_ary = array_merge($user_id_ary, $friend_list);
4c79b5
4c79b5
		foreach ($user_id_ary as $user_id)
4c79b5
		{
4c79b5
			if ($user_id == ANONYMOUS)
4c79b5
			{
4c79b5
				continue;
4c79b5
			}
4c79b5
4c79b5
			$address_list['u'][$user_id] = $type;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	// Check for disallowed recipients
4c79b5
	if (!empty($address_list['u']))
4c79b5
	{
4c79b5
		// We need to check their PM status (do they want to receive PM's?)
4c79b5
		// Only check if not a moderator or admin, since they are allowed to override this user setting
4c79b5
		if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
4c79b5
		{
4c79b5
			$sql = 'SELECT user_id
4c79b5
				FROM ' . USERS_TABLE . '
4c79b5
				WHERE ' . $db->sql_in_set('user_id', array_keys($address_list['u'])) . '
4c79b5
					AND user_allow_pm = 0';
4c79b5
			$result = $db->sql_query($sql);
4c79b5
4c79b5
			$removed = false;
4c79b5
			while ($row = $db->sql_fetchrow($result))
4c79b5
			{
4c79b5
				$removed = true;
4c79b5
				unset($address_list['u'][$row['user_id']]);
4c79b5
			}
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			// print a notice about users not being added who do not want to receive pms
4c79b5
			if ($removed)
4c79b5
			{
4c79b5
				$error[] = $user->lang['PM_USERS_REMOVED_NO_PM'];
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Build the hidden field for the recipients. Needed, as the variable is not read via request_var.
4c79b5
*/
4c79b5
function build_address_field($address_list)
4c79b5
{
4c79b5
	$s_hidden_address_field = '';
4c79b5
	foreach ($address_list as $type => $adr_ary)
4c79b5
	{
4c79b5
		foreach ($adr_ary as $id => $field)
4c79b5
		{
4c79b5
			$s_hidden_address_field .= '<input type="hidden" name="address_list[' . (($type == 'u') ? 'u' : 'g') . '][' . (int) $id . ']" value="' . (($field == 'to') ? 'to' : 'bcc') . '" />';
4c79b5
		}
4c79b5
	}
4c79b5
	return $s_hidden_address_field;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Return number of private message recipients
4c79b5
*/
4c79b5
function num_recipients($address_list)
4c79b5
{
4c79b5
	$num_recipients = 0;
4c79b5
4c79b5
	foreach ($address_list as $field => $adr_ary)
4c79b5
	{
4c79b5
		$num_recipients += sizeof($adr_ary);
4c79b5
	}
4c79b5
4c79b5
	return $num_recipients;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Get number of 'num_recipients' recipients from first position
4c79b5
*/
4c79b5
function get_recipients($address_list, $num_recipients = 1)
4c79b5
{
4c79b5
	$recipient = array();
4c79b5
4c79b5
	$count = 0;
4c79b5
	foreach ($address_list as $field => $adr_ary)
4c79b5
	{
4c79b5
		foreach ($adr_ary as $id => $type)
4c79b5
		{
4c79b5
			if ($count >= $num_recipients)
4c79b5
			{
4c79b5
				break 2;
4c79b5
			}
4c79b5
			$recipient[$field][$id] = $type;
4c79b5
			$count++;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	return $recipient;
4c79b5
}
4c79b5
4c79b5
?>