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

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package ucp
4c79b5
* @version $Id: ucp_pm_viewfolder.php 8795 2008-08-29 11:50:01Z Kellanved $
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
* View message folder
4c79b5
* Called from ucp_pm with mode == 'view' && action == 'view_folder'
4c79b5
*/
4c79b5
function view_folder($id, $mode, $folder_id, $folder)
4c79b5
{
4c79b5
	global $user, $template, $auth, $db, $cache;
4c79b5
	global $phpbb_root_path, $config, $phpEx;
4c79b5
4c79b5
	$submit_export = (isset($_POST['submit_export'])) ? true : false;
4c79b5
4c79b5
	$folder_info = get_pm_from($folder_id, $folder, $user->data['user_id']);
4c79b5
4c79b5
	if (!$submit_export)
4c79b5
	{
4c79b5
		$user->add_lang('viewforum');
4c79b5
4c79b5
		// Grab icons
4c79b5
		$icons = $cache->obtain_icons();
4c79b5
4c79b5
		$color_rows = array('marked', 'replied');
4c79b5
4c79b5
		// only show the friend/foe color rows if the module is enabled
4c79b5
		$zebra_enabled = false;
4c79b5
4c79b5
		$_module = new p_master();
4c79b5
		$_module->list_modules('ucp');
4c79b5
		$_module->set_active('zebra');
4c79b5
4c79b5
		$zebra_enabled = ($_module->active_module === false) ? false : true;
4c79b5
4c79b5
		unset($_module);
4c79b5
4c79b5
		if ($zebra_enabled)
4c79b5
		{
4c79b5
			$color_rows = array_merge($color_rows, array('friend', 'foe'));
4c79b5
		}
4c79b5
4c79b5
		foreach ($color_rows as $var)
4c79b5
		{
4c79b5
			$template->assign_block_vars('pm_colour_info', array(
4c79b5
				'IMG'	=> $user->img("pm_{$var}", ''),
4c79b5
				'CLASS'	=> "pm_{$var}_colour",
4c79b5
				'LANG'	=> $user->lang[strtoupper($var) . '_MESSAGE'])
4c79b5
			);
4c79b5
		}
4c79b5
4c79b5
		$mark_options = array('mark_important', 'delete_marked');
4c79b5
4c79b5
		$s_mark_options = '';
4c79b5
		foreach ($mark_options as $mark_option)
4c79b5
		{
4c79b5
			$s_mark_options .= '<option value="' . $mark_option . '">' . $user->lang[strtoupper($mark_option)] . '</option>';
4c79b5
		}
4c79b5
4c79b5
		// We do the folder moving options here too, for template authors to use...
4c79b5
		$s_folder_move_options = '';
4c79b5
		if ($folder_id != PRIVMSGS_NO_BOX && $folder_id != PRIVMSGS_OUTBOX)
4c79b5
		{
4c79b5
			foreach ($folder as $f_id => $folder_ary)
4c79b5
			{
4c79b5
				if ($f_id == PRIVMSGS_OUTBOX || $f_id == PRIVMSGS_SENTBOX || $f_id == $folder_id)
4c79b5
				{
4c79b5
					continue;
4c79b5
				}
4c79b5
4c79b5
				$s_folder_move_options .= '<option' . (($f_id != PRIVMSGS_INBOX) ? ' class="sep"' : '') . ' value="' . $f_id . '">';
4c79b5
				$s_folder_move_options .= sprintf($user->lang['MOVE_MARKED_TO_FOLDER'], $folder_ary['folder_name']);
4c79b5
				$s_folder_move_options .= (($folder_ary['unread_messages']) ? ' [' . $folder_ary['unread_messages'] . '] ' : '') . '</option>';
4c79b5
			}
4c79b5
		}
4c79b5
		$friend = $foe = array();
4c79b5
4c79b5
		// Get friends and foes
4c79b5
		$sql = 'SELECT *
4c79b5
			FROM ' . ZEBRA_TABLE . '
4c79b5
			WHERE user_id = ' . $user->data['user_id'];
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$friend[$row['zebra_id']] = $row['friend'];
4c79b5
			$foe[$row['zebra_id']] = $row['foe'];
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		$template->assign_vars(array(
4c79b5
			'S_MARK_OPTIONS'		=> $s_mark_options,
4c79b5
			'S_MOVE_MARKED_OPTIONS'	=> $s_folder_move_options)
4c79b5
		);
4c79b5
4c79b5
		// Okay, lets dump out the page ...
4c79b5
		if (sizeof($folder_info['pm_list']))
4c79b5
		{
4c79b5
			$address_list = array();
4c79b5
4c79b5
			// Build Recipient List if in outbox/sentbox - max two additional queries
4c79b5
			if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
4c79b5
			{
4c79b5
				$recipient_list = $address = array();
4c79b5
4c79b5
				foreach ($folder_info['rowset'] as $message_id => $row)
4c79b5
				{
4c79b5
					$address[$message_id] = rebuild_header(array('to' => $row['to_address'], 'bcc' => $row['bcc_address']));
4c79b5
					$_save = array('u', 'g');
4c79b5
					foreach ($_save as $save)
4c79b5
					{
4c79b5
						if (isset($address[$message_id][$save]) && sizeof($address[$message_id][$save]))
4c79b5
						{
4c79b5
							foreach (array_keys($address[$message_id][$save]) as $ug_id)
4c79b5
							{
4c79b5
								$recipient_list[$save][$ug_id] = array('name' => $user->lang['NA'], 'colour' => '');
4c79b5
							}
4c79b5
						}
4c79b5
					}
4c79b5
				}
4c79b5
4c79b5
				$_types = array('u', 'g');
4c79b5
				foreach ($_types as $ug_type)
4c79b5
				{
4c79b5
					if (!empty($recipient_list[$ug_type]))
4c79b5
					{
4c79b5
						if ($ug_type == 'u')
4c79b5
						{
4c79b5
							$sql = 'SELECT user_id as id, username as name, user_colour as colour
4c79b5
								FROM ' . USERS_TABLE . '
4c79b5
								WHERE ';
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							$sql = 'SELECT group_id as id, group_name as name, group_colour as colour, group_type
4c79b5
								FROM ' . GROUPS_TABLE . '
4c79b5
								WHERE ';
4c79b5
						}
4c79b5
						$sql .= $db->sql_in_set(($ug_type == 'u') ? 'user_id' : 'group_id', array_map('intval', array_keys($recipient_list[$ug_type])));
4c79b5
4c79b5
						$result = $db->sql_query($sql);
4c79b5
4c79b5
						while ($row = $db->sql_fetchrow($result))
4c79b5
						{
4c79b5
							if ($ug_type == 'g')
4c79b5
							{
4c79b5
								$row['name'] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['name']] : $row['name'];
4c79b5
							}
4c79b5
4c79b5
							$recipient_list[$ug_type][$row['id']] = array('name' => $row['name'], 'colour' => $row['colour']);
4c79b5
						}
4c79b5
						$db->sql_freeresult($result);
4c79b5
					}
4c79b5
				}
4c79b5
4c79b5
				foreach ($address as $message_id => $adr_ary)
4c79b5
				{
4c79b5
					foreach ($adr_ary as $type => $id_ary)
4c79b5
					{
4c79b5
						foreach ($id_ary as $ug_id => $_id)
4c79b5
						{
4c79b5
							if ($type == 'u')
4c79b5
							{
4c79b5
								$address_list[$message_id][] = get_username_string('full', $ug_id, $recipient_list[$type][$ug_id]['name'], $recipient_list[$type][$ug_id]['colour']);
4c79b5
							}
4c79b5
							else
4c79b5
							{
4c79b5
								$user_colour = ($recipient_list[$type][$ug_id]['colour']) ? ' style="font-weight: bold; color:#' . $recipient_list[$type][$ug_id]['colour'] . '"' : '';
4c79b5
								$link = '';
4c79b5
								$address_list[$message_id][] = $link . $recipient_list[$type][$ug_id]['name'] . (($link) ? '' : '');
4c79b5
							}
4c79b5
						}
4c79b5
					}
4c79b5
				}
4c79b5
				unset($recipient_list, $address);
4c79b5
			}
4c79b5
4c79b5
			$data = array();
4c79b5
4c79b5
			foreach ($folder_info['pm_list'] as $message_id)
4c79b5
			{
4c79b5
				$row = &$folder_info['rowset'][$message_id];
4c79b5
4c79b5
				$folder_img = ($row['pm_unread']) ? 'pm_unread' : 'pm_read';
4c79b5
				$folder_alt = ($row['pm_unread']) ? 'NEW_MESSAGES' : 'NO_NEW_MESSAGES';
4c79b5
4c79b5
				// Generate all URIs ...
4c79b5
				$view_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=view&f=$folder_id&p=$message_id");
4c79b5
				$remove_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=compose&action=delete&p=$message_id");
4c79b5
4c79b5
				$row_indicator = '';
4c79b5
				foreach ($color_rows as $var)
4c79b5
				{
4c79b5
					if (($var != 'friend' && $var != 'foe' && $row['pm_' . $var])
4c79b5
						||
4c79b5
						(($var == 'friend' || $var == 'foe') && isset(${$var}[$row['author_id']]) && ${$var}[$row['author_id']]))
4c79b5
					{
4c79b5
						$row_indicator = $var;
4c79b5
						break;
4c79b5
					}
4c79b5
				}
4c79b5
4c79b5
				// Send vars to template
4c79b5
				$template->assign_block_vars('messagerow', array(
4c79b5
					'PM_CLASS'			=> ($row_indicator) ? 'pm_' . $row_indicator . '_colour' : '',
4c79b5
4c79b5
					'MESSAGE_AUTHOR_FULL'		=> get_username_string('full', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
4c79b5
					'MESSAGE_AUTHOR_COLOUR'		=> get_username_string('colour', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
4c79b5
					'MESSAGE_AUTHOR'			=> get_username_string('username', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
4c79b5
					'U_MESSAGE_AUTHOR'			=> get_username_string('profile', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
4c79b5
4c79b5
					'FOLDER_ID'			=> $folder_id,
4c79b5
					'MESSAGE_ID'		=> $message_id,
4c79b5
					'SENT_TIME'			=> $user->format_date($row['message_time']),
4c79b5
					'SUBJECT'			=> censor_text($row['message_subject']),
4c79b5
					'FOLDER'			=> (isset($folder[$row['folder_id']])) ? $folder[$row['folder_id']]['folder_name'] : '',
4c79b5
					'U_FOLDER'			=> (isset($folder[$row['folder_id']])) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'folder=' . $row['folder_id']) : '',
4c79b5
					'PM_ICON_IMG'		=> (!empty($icons[$row['icon_id']])) ? '' : '',
4c79b5
					'PM_ICON_URL'		=> (!empty($icons[$row['icon_id']])) ? $config['icons_path'] . '/' . $icons[$row['icon_id']]['img'] : '',
4c79b5
					'FOLDER_IMG'		=> $user->img($folder_img, $folder_alt),
4c79b5
					'FOLDER_IMG_SRC'	=> $user->img($folder_img, $folder_alt, false, '', 'src'),
4c79b5
					'PM_IMG'			=> ($row_indicator) ? $user->img('pm_' . $row_indicator, '') : '',
4c79b5
					'ATTACH_ICON_IMG'	=> ($auth->acl_get('u_pm_download') && $row['message_attachment'] && $config['allow_pm_attach']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
4c79b5
4c79b5
					'S_PM_DELETED'		=> ($row['pm_deleted']) ? true : false,
4c79b5
					'S_AUTHOR_DELETED'	=> ($row['author_id'] == ANONYMOUS) ? true : false,
4c79b5
4c79b5
					'U_VIEW_PM'			=> ($row['pm_deleted']) ? '' : $view_message_url,
4c79b5
					'U_REMOVE_PM'		=> ($row['pm_deleted']) ? $remove_message_url : '',
4c79b5
					'RECIPIENTS'		=> ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? implode(', ', $address_list[$message_id]) : '')
4c79b5
				);
4c79b5
			}
4c79b5
			unset($folder_info['rowset']);
4c79b5
4c79b5
			$template->assign_vars(array(
4c79b5
				'S_SHOW_RECIPIENTS'		=> ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? true : false,
4c79b5
				'S_SHOW_COLOUR_LEGEND'	=> true,
4c79b5
4c79b5
				'S_PM_ICONS'			=> ($config['enable_pm_icons']) ? true : false)
4c79b5
			);
4c79b5
		}
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$export_type = request_var('export_option', '');
4c79b5
		$enclosure = request_var('enclosure', '');
4c79b5
		$delimiter = request_var('delimiter', '');
4c79b5
4c79b5
		if ($export_type == 'CSV' && ($delimiter === '' || $enclosure === ''))
4c79b5
		{
4c79b5
			$template->assign_var('PROMPT', true);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			// Build Recipient List if in outbox/sentbox
4c79b5
			$address = array();
4c79b5
			if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
4c79b5
			{
4c79b5
				foreach ($folder_info['rowset'] as $message_id => $row)
4c79b5
				{
4c79b5
					$address[$message_id] = rebuild_header(array('to' => $row['to_address'], 'bcc' => $row['bcc_address']));
4c79b5
				}
4c79b5
			}
4c79b5
4c79b5
			foreach ($folder_info['pm_list'] as $message_id)
4c79b5
			{
4c79b5
				$row = &$folder_info['rowset'][$message_id];
4c79b5
4c79b5
				include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
4c79b5
4c79b5
				$sql = 'SELECT p.message_text, p.bbcode_uid
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.folder_id = $folder_id
4c79b5
						AND t.msg_id = p.msg_id
4c79b5
						AND p.msg_id = $message_id";
4c79b5
				$result = $db->sql_query_limit($sql, 1);
4c79b5
				$message_row = $db->sql_fetchrow($result);
4c79b5
				$db->sql_freeresult($result);
4c79b5
4c79b5
				$_types = array('u', 'g');
4c79b5
				foreach ($_types as $ug_type)
4c79b5
				{
4c79b5
					if (isset($address[$message_id][$ug_type]) && sizeof($address[$message_id][$ug_type]))
4c79b5
					{
4c79b5
						if ($ug_type == 'u')
4c79b5
						{
4c79b5
							$sql = 'SELECT user_id as id, username as name
4c79b5
								FROM ' . USERS_TABLE . '
4c79b5
								WHERE ';
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							$sql = 'SELECT group_id as id, group_name as name
4c79b5
								FROM ' . GROUPS_TABLE . '
4c79b5
								WHERE ';
4c79b5
						}
4c79b5
						$sql .= $db->sql_in_set(($ug_type == 'u') ? 'user_id' : 'group_id', array_map('intval', array_keys($address[$message_id][$ug_type])));
4c79b5
4c79b5
						$result = $db->sql_query($sql);
4c79b5
4c79b5
						while ($info_row = $db->sql_fetchrow($result))
4c79b5
						{
4c79b5
							$address[$message_id][$ug_type][$address[$message_id][$ug_type][$info_row['id']]][] = $info_row['name'];
4c79b5
							unset($address[$message_id][$ug_type][$info_row['id']]);
4c79b5
						}
4c79b5
						$db->sql_freeresult($result);
4c79b5
					}
4c79b5
				}
4c79b5
4c79b5
				decode_message($message_row['message_text'], $message_row['bbcode_uid']);
4c79b5
4c79b5
				$data[] = array(
4c79b5
					'subject'	=> censor_text($row['message_subject']),
4c79b5
					'sender'	=> $row['username'],
4c79b5
					'date'		=> $user->format_date($row['message_time']),
4c79b5
					'to'		=> ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? $address[$message_id] : '',
4c79b5
					'message'	=> $message_row['message_text']
4c79b5
				);
4c79b5
			}
4c79b5
4c79b5
			switch ($export_type)
4c79b5
			{
4c79b5
				case 'CSV':
4c79b5
				case 'CSV_EXCEL':
4c79b5
					$mimetype = 'text/csv';
4c79b5
					$filetype = 'csv';
4c79b5
4c79b5
					if ($export_type == 'CSV_EXCEL')
4c79b5
					{
4c79b5
						$enclosure = '"';
4c79b5
						$delimiter = ',';
4c79b5
						$newline = "\r\n";
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						$newline = "\n";
4c79b5
					}
4c79b5
4c79b5
					$string = '';
4c79b5
					foreach ($data as $value)
4c79b5
					{
4c79b5
						$recipients = $value['to'];
4c79b5
						$value['to'] = $value['bcc'] = '';
4c79b5
4c79b5
						if (is_array($recipients))
4c79b5
						{
4c79b5
							foreach ($recipients as $values)
4c79b5
							{
4c79b5
								$value['bcc'] .= (isset($values['bcc']) && is_array($values['bcc'])) ? ',' . implode(',', $values['bcc']) : '';
4c79b5
								$value['to'] .= (isset($values['to']) && is_array($values['to'])) ? ',' . implode(',', $values['to']) : '';
4c79b5
							}
4c79b5
4c79b5
							// Remove the commas which will appear before the first entry.
4c79b5
							$value['to'] = substr($value['to'], 1);
4c79b5
							$value['bcc'] = substr($value['bcc'], 1);
4c79b5
						}
4c79b5
4c79b5
						foreach ($value as $tag => $text)
4c79b5
						{
4c79b5
							$cell = str_replace($enclosure, $enclosure . $enclosure, $text);
4c79b5
4c79b5
							if (strpos($cell, $enclosure) !== false || strpos($cell, $delimiter) !== false || strpos($cell, $newline) !== false)
4c79b5
							{
4c79b5
								$string .= $enclosure . $text . $enclosure . $delimiter;
4c79b5
							}
4c79b5
							else
4c79b5
							{
4c79b5
								$string .= $cell . $delimiter;
4c79b5
							}
4c79b5
						}
4c79b5
						$string = substr($string, 0, -1) . $newline;
4c79b5
					}
4c79b5
				break;
4c79b5
4c79b5
				case 'XML':
4c79b5
					$mimetype = 'application/xml';
4c79b5
					$filetype = 'xml';
4c79b5
					$string = '' . "\n";
4c79b5
					$string .= "<phpbb>\n";
4c79b5
4c79b5
					foreach ($data as $value)
4c79b5
					{
4c79b5
						$string .= "\t<privmsg>\n";
4c79b5
4c79b5
						if (is_array($value['to']))
4c79b5
						{
4c79b5
							foreach ($value['to'] as $key => $values)
4c79b5
							{
4c79b5
								foreach ($values as $type => $types)
4c79b5
								{
4c79b5
									foreach ($types as $name)
4c79b5
									{
4c79b5
										$string .= "\t\t<recipient type=\"$type\" status=\"$key\">$name</recipient>\n";
4c79b5
									}
4c79b5
								}
4c79b5
							}
4c79b5
						}
4c79b5
4c79b5
						unset($value['to']);
4c79b5
4c79b5
						foreach ($value as $tag => $text)
4c79b5
						{
4c79b5
							$string .= "\t\t<$tag>$text</$tag>\n";
4c79b5
						}
4c79b5
4c79b5
						$string .= "\t</privmsg>\n";
4c79b5
					}
4c79b5
					$string .= '</phpbb>';
4c79b5
				break;
4c79b5
			}
4c79b5
4c79b5
			header('Pragma: no-cache');
4c79b5
			header("Content-Type: $mimetype; name=\"data.$filetype\"");
4c79b5
			header("Content-disposition: attachment; filename=data.$filetype");
4c79b5
			echo $string;
4c79b5
			exit;
4c79b5
		}
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Get Messages from folder/user
4c79b5
*/
4c79b5
function get_pm_from($folder_id, $folder, $user_id)
4c79b5
{
4c79b5
	global $user, $db, $template, $config, $auth, $phpbb_root_path, $phpEx;
4c79b5
4c79b5
	$start = request_var('start', 0);
4c79b5
4c79b5
	// Additional vars later, pm ordering is mostly different from post ordering. :/
4c79b5
	$sort_days	= request_var('st', 0);
4c79b5
	$sort_key	= request_var('sk', 't');
4c79b5
	$sort_dir	= request_var('sd', 'd');
4c79b5
4c79b5
	// PM ordering options
4c79b5
	$limit_days = array(0 => $user->lang['ALL_MESSAGES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
4c79b5
4c79b5
	// No sort by Author for sentbox/outbox (already only author available)
4c79b5
	// Also, sort by msg_id for the time - private messages are not as prone to errors as posts are.
4c79b5
	if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
4c79b5
	{
4c79b5
		$sort_by_text = array('t' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
4c79b5
		$sort_by_sql = array('t' => 'p.msg_id', 's' => 'p.message_subject');
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
4c79b5
		$sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.msg_id', 's' => 'p.message_subject');
4c79b5
	}
4c79b5
4c79b5
	$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
4c79b5
	gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
4c79b5
4c79b5
	$folder_sql = 't.folder_id = ' . (int) $folder_id;
4c79b5
4c79b5
	// Limit pms to certain time frame, obtain correct pm count
4c79b5
	if ($sort_days)
4c79b5
	{
4c79b5
		$min_post_time = time() - ($sort_days * 86400);
4c79b5
4c79b5
		if (isset($_POST['sort']))
4c79b5
		{
4c79b5
			$start = 0;
4c79b5
		}
4c79b5
4c79b5
		$sql = 'SELECT COUNT(t.msg_id) AS pm_count
4c79b5
			FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . " p
4c79b5
			WHERE $folder_sql
4c79b5
				AND t.user_id = $user_id
4c79b5
				AND t.msg_id = p.msg_id
4c79b5
				AND p.message_time >= $min_post_time";
4c79b5
		$result = $db->sql_query_limit($sql, 1);
4c79b5
		$pm_count = (int) $db->sql_fetchfield('pm_count');
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		$sql_limit_time = "AND p.message_time >= $min_post_time";
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		$pm_count = (!empty($folder[$folder_id]['num_messages'])) ? $folder[$folder_id]['num_messages'] : 0;
4c79b5
		$sql_limit_time = '';
4c79b5
	}
4c79b5
4c79b5
	$template->assign_vars(array(
4c79b5
		'PAGINATION'		=> generate_pagination(append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=view&action=view_folder&f=$folder_id&$u_sort_param"), $pm_count, $config['topics_per_page'], $start),
4c79b5
		'PAGE_NUMBER'		=> on_page($pm_count, $config['topics_per_page'], $start),
4c79b5
		'TOTAL_MESSAGES'	=> (($pm_count == 1) ? $user->lang['VIEW_PM_MESSAGE'] : sprintf($user->lang['VIEW_PM_MESSAGES'], $pm_count)),
4c79b5
4c79b5
		'POST_IMG'		=> (!$auth->acl_get('u_sendpm')) ? $user->img('button_topic_locked', 'PM_LOCKED') : $user->img('button_pm_new', 'POST_PM'),
4c79b5
4c79b5
		'L_NO_MESSAGES'	=> (!$auth->acl_get('u_sendpm')) ? $user->lang['POST_PM_LOCKED'] : $user->lang['NO_MESSAGES'],
4c79b5
4c79b5
		'S_SELECT_SORT_DIR'		=> $s_sort_dir,
4c79b5
		'S_SELECT_SORT_KEY'		=> $s_sort_key,
4c79b5
		'S_SELECT_SORT_DAYS'	=> $s_limit_days,
4c79b5
		'S_TOPIC_ICONS'			=> ($config['enable_pm_icons']) ? true : false,
4c79b5
4c79b5
		'U_POST_NEW_TOPIC'	=> ($auth->acl_get('u_sendpm')) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=compose') : '', 
4c79b5
		'S_PM_ACTION'		=> append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=view&action=view_folder&f=$folder_id" . (($start !== 0) ? "&start=$start" : '')),
4c79b5
	));
4c79b5
4c79b5
	// Grab all pm data
4c79b5
	$rowset = $pm_list = array();
4c79b5
4c79b5
	// If the user is trying to reach late pages, start searching from the end
4c79b5
	$store_reverse = false;
4c79b5
	$sql_limit = $config['topics_per_page'];
4c79b5
	if ($start > $pm_count / 2)
4c79b5
	{
4c79b5
		$store_reverse = true;
4c79b5
4c79b5
		if ($start + $config['topics_per_page'] > $pm_count)
4c79b5
		{
4c79b5
			$sql_limit = min($config['topics_per_page'], max(1, $pm_count - $start));
4c79b5
		}
4c79b5
4c79b5
		// Select the sort order
4c79b5
		$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
4c79b5
		$sql_start = max(0, $pm_count - $sql_limit - $start);
4c79b5
	}
4c79b5
	else
4c79b5
	{
4c79b5
		// Select the sort order
4c79b5
		$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
4c79b5
		$sql_start = $start;
4c79b5
	}
4c79b5
4c79b5
	$sql = 'SELECT t.*, p.root_level, p.message_time, p.message_subject, p.icon_id, p.to_address, p.message_attachment, p.bcc_address, u.username, u.username_clean, u.user_colour
4c79b5
		FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . " u
4c79b5
		WHERE t.user_id = $user_id
4c79b5
			AND p.author_id = u.user_id
4c79b5
			AND $folder_sql
4c79b5
			AND t.msg_id = p.msg_id
4c79b5
			$sql_limit_time
4c79b5
		ORDER BY $sql_sort_order";
4c79b5
	$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
4c79b5
4c79b5
	while ($row = $db->sql_fetchrow($result))
4c79b5
	{
4c79b5
		$rowset[$row['msg_id']] = $row;
4c79b5
		$pm_list[] = $row['msg_id'];
4c79b5
	}
4c79b5
	$db->sql_freeresult($result);
4c79b5
4c79b5
	$pm_list = ($store_reverse) ? array_reverse($pm_list) : $pm_list;
4c79b5
4c79b5
	return array(
4c79b5
		'pm_count'	=> $pm_count,
4c79b5
		'pm_list'	=> $pm_list,
4c79b5
		'rowset'	=> $rowset
4c79b5
	);
4c79b5
}
4c79b5
4c79b5
?>