includes/ucp/ucp_pm_viewfolder.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Thu, 08 Mar 2012 01:33:19 +0400
changeset 11 0a7892c58331
parent 0 ceef6d64c21d
permissions -rw-r--r--
Style fix
     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 * View message folder
    21 * Called from ucp_pm with mode == 'view' && action == 'view_folder'
    22 */
    23 function view_folder($id, $mode, $folder_id, $folder)
    24 {
    25 	global $user, $template, $auth, $db, $cache;
    26 	global $phpbb_root_path, $config, $phpEx;
    27 
    28 	$submit_export = (isset($_POST['submit_export'])) ? true : false;
    29 
    30 	$folder_info = get_pm_from($folder_id, $folder, $user->data['user_id']);
    31 
    32 	if (!$submit_export)
    33 	{
    34 		$user->add_lang('viewforum');
    35 
    36 		// Grab icons
    37 		$icons = $cache->obtain_icons();
    38 
    39 		$color_rows = array('marked', 'replied');
    40 
    41 		// only show the friend/foe color rows if the module is enabled
    42 		$zebra_enabled = false;
    43 
    44 		$_module = new p_master();
    45 		$_module->list_modules('ucp');
    46 		$_module->set_active('zebra');
    47 
    48 		$zebra_enabled = ($_module->active_module === false) ? false : true;
    49 
    50 		unset($_module);
    51 
    52 		if ($zebra_enabled)
    53 		{
    54 			$color_rows = array_merge($color_rows, array('friend', 'foe'));
    55 		}
    56 
    57 		foreach ($color_rows as $var)
    58 		{
    59 			$template->assign_block_vars('pm_colour_info', array(
    60 				'IMG'	=> $user->img("pm_{$var}", ''),
    61 				'CLASS'	=> "pm_{$var}_colour",
    62 				'LANG'	=> $user->lang[strtoupper($var) . '_MESSAGE'])
    63 			);
    64 		}
    65 
    66 		$mark_options = array('mark_important', 'delete_marked');
    67 
    68 		// Minimise edits
    69 		if (!$auth->acl_get('u_pm_delete') && $key = array_search('delete_marked', $mark_options))
    70 		{
    71 			unset($mark_options[$key]);
    72 		}
    73 
    74 		$s_mark_options = '';
    75 		foreach ($mark_options as $mark_option)
    76 		{
    77 			$s_mark_options .= '<option value="' . $mark_option . '">' . $user->lang[strtoupper($mark_option)] . '</option>';
    78 		}
    79 
    80 		// We do the folder moving options here too, for template authors to use...
    81 		$s_folder_move_options = '';
    82 		if ($folder_id != PRIVMSGS_NO_BOX && $folder_id != PRIVMSGS_OUTBOX)
    83 		{
    84 			foreach ($folder as $f_id => $folder_ary)
    85 			{
    86 				if ($f_id == PRIVMSGS_OUTBOX || $f_id == PRIVMSGS_SENTBOX || $f_id == $folder_id)
    87 				{
    88 					continue;
    89 				}
    90 
    91 				$s_folder_move_options .= '<option' . (($f_id != PRIVMSGS_INBOX) ? ' class="sep"' : '') . ' value="' . $f_id . '">';
    92 				$s_folder_move_options .= sprintf($user->lang['MOVE_MARKED_TO_FOLDER'], $folder_ary['folder_name']);
    93 				$s_folder_move_options .= (($folder_ary['unread_messages']) ? ' [' . $folder_ary['unread_messages'] . '] ' : '') . '</option>';
    94 			}
    95 		}
    96 		$friend = $foe = array();
    97 
    98 		// Get friends and foes
    99 		$sql = 'SELECT *
   100 			FROM ' . ZEBRA_TABLE . '
   101 			WHERE user_id = ' . $user->data['user_id'];
   102 		$result = $db->sql_query($sql);
   103 
   104 		while ($row = $db->sql_fetchrow($result))
   105 		{
   106 			$friend[$row['zebra_id']] = $row['friend'];
   107 			$foe[$row['zebra_id']] = $row['foe'];
   108 		}
   109 		$db->sql_freeresult($result);
   110 
   111 		$template->assign_vars(array(
   112 			'S_MARK_OPTIONS'		=> $s_mark_options,
   113 			'S_MOVE_MARKED_OPTIONS'	=> $s_folder_move_options)
   114 		);
   115 
   116 		// Okay, lets dump out the page ...
   117 		if (sizeof($folder_info['pm_list']))
   118 		{
   119 			$address_list = array();
   120 
   121 			// Build Recipient List if in outbox/sentbox - max two additional queries
   122 			if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
   123 			{
   124 				$address_list = get_recipient_strings($folder_info['rowset']);
   125 			}
   126 
   127 			foreach ($folder_info['pm_list'] as $message_id)
   128 			{
   129 				$row = &$folder_info['rowset'][$message_id];
   130 
   131 				$folder_img = ($row['pm_unread']) ? 'pm_unread' : 'pm_read';
   132 				$folder_alt = ($row['pm_unread']) ? 'NEW_MESSAGES' : 'NO_NEW_MESSAGES';
   133 
   134 				// Generate all URIs ...
   135 				$view_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=view&amp;f=$folder_id&amp;p=$message_id");
   136 				$remove_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=compose&amp;action=delete&amp;p=$message_id");
   137 
   138 				$row_indicator = '';
   139 				foreach ($color_rows as $var)
   140 				{
   141 					if (($var != 'friend' && $var != 'foe' && $row['pm_' . $var])
   142 						||
   143 						(($var == 'friend' || $var == 'foe') && isset(${$var}[$row['author_id']]) && ${$var}[$row['author_id']]))
   144 					{
   145 						$row_indicator = $var;
   146 						break;
   147 					}
   148 				}
   149 
   150 				// Send vars to template
   151 				$template->assign_block_vars('messagerow', array(
   152 					'PM_CLASS'			=> ($row_indicator) ? 'pm_' . $row_indicator . '_colour' : '',
   153 
   154 					'MESSAGE_AUTHOR_FULL'		=> get_username_string('full', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
   155 					'MESSAGE_AUTHOR_COLOUR'		=> get_username_string('colour', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
   156 					'MESSAGE_AUTHOR'			=> get_username_string('username', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
   157 					'U_MESSAGE_AUTHOR'			=> get_username_string('profile', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
   158 
   159 					'FOLDER_ID'			=> $folder_id,
   160 					'MESSAGE_ID'		=> $message_id,
   161 					'SENT_TIME'			=> $user->format_date($row['message_time']),
   162 					'SUBJECT'			=> censor_text($row['message_subject']),
   163 					'FOLDER'			=> (isset($folder[$row['folder_id']])) ? $folder[$row['folder_id']]['folder_name'] : '',
   164 					'U_FOLDER'			=> (isset($folder[$row['folder_id']])) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'folder=' . $row['folder_id']) : '',
   165 					'PM_ICON_IMG'		=> (!empty($icons[$row['icon_id']])) ? '<img src="' . $config['icons_path'] . '/' . $icons[$row['icon_id']]['img'] . '" width="' . $icons[$row['icon_id']]['width'] . '" height="' . $icons[$row['icon_id']]['height'] . '" alt="" title="" />' : '',
   166 					'PM_ICON_URL'		=> (!empty($icons[$row['icon_id']])) ? $config['icons_path'] . '/' . $icons[$row['icon_id']]['img'] : '',
   167 					'FOLDER_IMG'		=> $user->img($folder_img, $folder_alt),
   168 					'FOLDER_IMG_SRC'	=> $user->img($folder_img, $folder_alt, false, '', 'src'),
   169 					'PM_IMG'			=> ($row_indicator) ? $user->img('pm_' . $row_indicator, '') : '',
   170 					'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']) : '',
   171 
   172 					'S_PM_UNREAD'		=> ($row['pm_unread']) ? true : false,
   173 					'S_PM_DELETED'		=> ($row['pm_deleted']) ? true : false,
   174 					'S_PM_REPORTED'		=> (isset($row['report_id'])) ? true : false,
   175 					'S_AUTHOR_DELETED'	=> ($row['author_id'] == ANONYMOUS) ? true : false,
   176 
   177 					'U_VIEW_PM'			=> ($row['pm_deleted']) ? '' : $view_message_url,
   178 					'U_REMOVE_PM'		=> ($row['pm_deleted']) ? $remove_message_url : '',
   179 					'U_MCP_REPORT'		=> (isset($row['report_id'])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=pm_reports&amp;mode=pm_report_details&amp;r=' . $row['report_id']) : '',
   180 					'RECIPIENTS'		=> ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? implode(', ', $address_list[$message_id]) : '')
   181 				);
   182 			}
   183 			unset($folder_info['rowset']);
   184 
   185 			$template->assign_vars(array(
   186 				'S_SHOW_RECIPIENTS'		=> ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? true : false,
   187 				'S_SHOW_COLOUR_LEGEND'	=> true,
   188 
   189 				'REPORTED_IMG'			=> $user->img('icon_topic_reported', 'PM_REPORTED'),
   190 				'S_PM_ICONS'			=> ($config['enable_pm_icons']) ? true : false)
   191 			);
   192 		}
   193 	}
   194 	else
   195 	{
   196 		$export_type = request_var('export_option', '');
   197 		$enclosure = request_var('enclosure', '');
   198 		$delimiter = request_var('delimiter', '');
   199 
   200 		if ($export_type == 'CSV' && ($delimiter === '' || $enclosure === ''))
   201 		{
   202 			$template->assign_var('PROMPT', true);
   203 		}
   204 		else
   205 		{
   206 			// Build Recipient List if in outbox/sentbox
   207 
   208 			$address_temp = $address = $data = array();
   209 
   210 			if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
   211 			{
   212 				foreach ($folder_info['rowset'] as $message_id => $row)
   213 				{
   214 					$address_temp[$message_id] = rebuild_header(array('to' => $row['to_address'], 'bcc' => $row['bcc_address']));
   215 					$address[$message_id] = array();
   216 				}
   217 			}
   218 
   219 			foreach ($folder_info['pm_list'] as $message_id)
   220 			{
   221 				$row = &$folder_info['rowset'][$message_id];
   222 
   223 				include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
   224 
   225 				$sql = 'SELECT p.message_text, p.bbcode_uid
   226 					FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . ' u
   227 					WHERE t.user_id = ' . $user->data['user_id'] . "
   228 						AND p.author_id = u.user_id
   229 						AND t.folder_id = $folder_id
   230 						AND t.msg_id = p.msg_id
   231 						AND p.msg_id = $message_id";
   232 				$result = $db->sql_query_limit($sql, 1);
   233 				$message_row = $db->sql_fetchrow($result);
   234 				$db->sql_freeresult($result);
   235 
   236 				$_types = array('u', 'g');
   237 				foreach ($_types as $ug_type)
   238 				{
   239 					if (isset($address_temp[$message_id][$ug_type]) && sizeof($address_temp[$message_id][$ug_type]))
   240 					{
   241 						if (!isset($address[$message_id][$ug_type]))
   242 						{
   243 							$address[$message_id][$ug_type] = array();
   244 						}
   245 						if ($ug_type == 'u')
   246 						{
   247 							$sql = 'SELECT user_id as id, username as name
   248 								FROM ' . USERS_TABLE . '
   249 								WHERE ';
   250 						}
   251 						else
   252 						{
   253 							$sql = 'SELECT group_id as id, group_name as name
   254 								FROM ' . GROUPS_TABLE . '
   255 								WHERE ';
   256 						}
   257 						$sql .= $db->sql_in_set(($ug_type == 'u') ? 'user_id' : 'group_id', array_map('intval', array_keys($address_temp[$message_id][$ug_type])));
   258 
   259 						$result = $db->sql_query($sql);
   260 
   261 						while ($info_row = $db->sql_fetchrow($result))
   262 						{
   263 							$address[$message_id][$ug_type][$address_temp[$message_id][$ug_type][$info_row['id']]][] = $info_row['name'];
   264 							unset($address_temp[$message_id][$ug_type][$info_row['id']]);
   265 						}
   266 						$db->sql_freeresult($result);
   267 					}
   268 				}
   269 
   270 				// There is the chance that all recipients of the message got deleted. To avoid creating 
   271 				// exports without recipients, we add a bogus "undisclosed recipient".
   272 				if (!(isset($address[$message_id]['g']) && sizeof($address[$message_id]['g'])) && 
   273 				    !(isset($address[$message_id]['u']) && sizeof($address[$message_id]['u'])))
   274 				{
   275 					$address[$message_id]['u'] = array();
   276 					$address[$message_id]['u']['to'] = array();
   277 					$address[$message_id]['u']['to'][] = $user->lang['UNDISCLOSED_RECIPIENT'];
   278 				}
   279 
   280 				decode_message($message_row['message_text'], $message_row['bbcode_uid']);
   281 				
   282 				$data[] = array(
   283 					'subject'	=> censor_text($row['message_subject']),
   284 					'sender'	=> $row['username'],
   285 					// ISO 8601 date. For PHP4 we are able to hardcode the timezone because $user->format_date() does not set it.
   286 					'date'		=> $user->format_date($row['message_time'], (PHP_VERSION >= 5) ? 'c' : "Y-m-d\TH:i:s+00:00", true),
   287 					'to'		=> ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? $address[$message_id] : '',
   288 					'message'	=> $message_row['message_text']
   289 				);
   290 			}
   291 
   292 			switch ($export_type)
   293 			{
   294 				case 'CSV':
   295 				case 'CSV_EXCEL':
   296 					$mimetype = 'text/csv';
   297 					$filetype = 'csv';
   298 
   299 					if ($export_type == 'CSV_EXCEL')
   300 					{
   301 						$enclosure = '"';
   302 						$delimiter = ',';
   303 						$newline = "\r\n";
   304 					}
   305 					else
   306 					{
   307 						$newline = "\n";
   308 					}
   309 
   310 					$string = '';
   311 					foreach ($data as $value)
   312 					{
   313 						$recipients = $value['to'];
   314 						$value['to'] = $value['bcc'] = '';
   315 
   316 						if (is_array($recipients))
   317 						{
   318 							foreach ($recipients as $values)
   319 							{
   320 								$value['bcc'] .= (isset($values['bcc']) && is_array($values['bcc'])) ? ',' . implode(',', $values['bcc']) : '';
   321 								$value['to'] .= (isset($values['to']) && is_array($values['to'])) ? ',' . implode(',', $values['to']) : '';
   322 							}
   323 
   324 							// Remove the commas which will appear before the first entry.
   325 							$value['to'] = substr($value['to'], 1);
   326 							$value['bcc'] = substr($value['bcc'], 1);
   327 						}
   328 
   329 						foreach ($value as $tag => $text)
   330 						{
   331 							$cell = str_replace($enclosure, $enclosure . $enclosure, $text);
   332 
   333 							if (strpos($cell, $enclosure) !== false || strpos($cell, $delimiter) !== false || strpos($cell, $newline) !== false)
   334 							{
   335 								$string .= $enclosure . $text . $enclosure . $delimiter;
   336 							}
   337 							else
   338 							{
   339 								$string .= $cell . $delimiter;
   340 							}
   341 						}
   342 						$string = substr($string, 0, -1) . $newline;
   343 					}
   344 				break;
   345 
   346 				case 'XML':
   347 					$mimetype = 'application/xml';
   348 					$filetype = 'xml';
   349 					$string = '<?xml version="1.0"?>' . "\n";
   350 					$string .= "<phpbb>\n";
   351 
   352 					foreach ($data as $value)
   353 					{
   354 						$string .= "\t<privmsg>\n";
   355 
   356 						if (is_array($value['to']))
   357 						{
   358 							foreach ($value['to'] as $key => $values)
   359 							{
   360 								foreach ($values as $type => $types)
   361 								{
   362 									foreach ($types as $name)
   363 									{
   364 										$string .= "\t\t<recipient type=\"$type\" status=\"$key\">$name</recipient>\n";
   365 									}
   366 								}
   367 							}
   368 						}
   369 
   370 						unset($value['to']);
   371 
   372 						foreach ($value as $tag => $text)
   373 						{
   374 							$string .= "\t\t<$tag>$text</$tag>\n";
   375 						}
   376 
   377 						$string .= "\t</privmsg>\n";
   378 					}
   379 					$string .= '</phpbb>';
   380 				break;
   381 			}
   382 
   383 			header('Pragma: no-cache');
   384 			header("Content-Type: $mimetype; name=\"data.$filetype\"");
   385 			header("Content-disposition: attachment; filename=data.$filetype");
   386 			echo $string;
   387 			exit;
   388 		}
   389 	}
   390 }
   391 
   392 /**
   393 * Get Messages from folder/user
   394 */
   395 function get_pm_from($folder_id, $folder, $user_id)
   396 {
   397 	global $user, $db, $template, $config, $auth, $phpbb_root_path, $phpEx;
   398 
   399 	$start = request_var('start', 0);
   400 
   401 	// Additional vars later, pm ordering is mostly different from post ordering. :/
   402 	$sort_days	= request_var('st', 0);
   403 	$sort_key	= request_var('sk', 't');
   404 	$sort_dir	= request_var('sd', 'd');
   405 
   406 	// PM ordering options
   407 	$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']);
   408 
   409 	// No sort by Author for sentbox/outbox (already only author available)
   410 	// Also, sort by msg_id for the time - private messages are not as prone to errors as posts are.
   411 	if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
   412 	{
   413 		$sort_by_text = array('t' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
   414 		$sort_by_sql = array('t' => 'p.message_time', 's' => array('p.message_subject', 'p.message_time'));
   415 	}
   416 	else
   417 	{
   418 		$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
   419 		$sort_by_sql = array('a' => array('u.username_clean', 'p.message_time'), 't' => 'p.message_time', 's' => array('p.message_subject', 'p.message_time'));
   420 	}
   421 
   422 	$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
   423 	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);
   424 
   425 	$folder_sql = 't.folder_id = ' . (int) $folder_id;
   426 
   427 	// Limit pms to certain time frame, obtain correct pm count
   428 	if ($sort_days)
   429 	{
   430 		$min_post_time = time() - ($sort_days * 86400);
   431 
   432 		if (isset($_POST['sort']))
   433 		{
   434 			$start = 0;
   435 		}
   436 
   437 		$sql = 'SELECT COUNT(t.msg_id) AS pm_count
   438 			FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . " p
   439 			WHERE $folder_sql
   440 				AND t.user_id = $user_id
   441 				AND t.msg_id = p.msg_id
   442 				AND p.message_time >= $min_post_time";
   443 		$result = $db->sql_query_limit($sql, 1);
   444 		$pm_count = (int) $db->sql_fetchfield('pm_count');
   445 		$db->sql_freeresult($result);
   446 
   447 		$sql_limit_time = "AND p.message_time >= $min_post_time";
   448 	}
   449 	else
   450 	{
   451 		$pm_count = (!empty($folder[$folder_id]['num_messages'])) ? $folder[$folder_id]['num_messages'] : 0;
   452 		$sql_limit_time = '';
   453 	}
   454 
   455 	$template->assign_vars(array(
   456 		'PAGINATION'		=> generate_pagination(append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;mode=view&amp;action=view_folder&amp;f=$folder_id&amp;$u_sort_param"), $pm_count, $config['topics_per_page'], $start),
   457 		'PAGE_NUMBER'		=> on_page($pm_count, $config['topics_per_page'], $start),
   458 		'TOTAL_MESSAGES'	=> (($pm_count == 1) ? $user->lang['VIEW_PM_MESSAGE'] : sprintf($user->lang['VIEW_PM_MESSAGES'], $pm_count)),
   459 
   460 		'POST_IMG'		=> (!$auth->acl_get('u_sendpm')) ? $user->img('button_topic_locked', 'POST_PM_LOCKED') : $user->img('button_pm_new', 'POST_NEW_PM'),
   461 
   462 		'S_NO_AUTH_SEND_MESSAGE'	=> !$auth->acl_get('u_sendpm'),
   463 
   464 		'S_SELECT_SORT_DIR'		=> $s_sort_dir,
   465 		'S_SELECT_SORT_KEY'		=> $s_sort_key,
   466 		'S_SELECT_SORT_DAYS'	=> $s_limit_days,
   467 		'S_TOPIC_ICONS'			=> ($config['enable_pm_icons']) ? true : false,
   468 
   469 		'U_POST_NEW_TOPIC'	=> ($auth->acl_get('u_sendpm')) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=compose') : '',
   470 		'S_PM_ACTION'		=> append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;mode=view&amp;action=view_folder&amp;f=$folder_id" . (($start !== 0) ? "&amp;start=$start" : '')),
   471 	));
   472 
   473 	// Grab all pm data
   474 	$rowset = $pm_list = array();
   475 
   476 	// If the user is trying to reach late pages, start searching from the end
   477 	$store_reverse = false;
   478 	$sql_limit = $config['topics_per_page'];
   479 	if ($start > $pm_count / 2)
   480 	{
   481 		$store_reverse = true;
   482 
   483 		if ($start + $config['topics_per_page'] > $pm_count)
   484 		{
   485 			$sql_limit = min($config['topics_per_page'], max(1, $pm_count - $start));
   486 		}
   487 
   488 		// Select the sort order
   489 		$direction = ($sort_dir == 'd') ? 'ASC' : 'DESC';
   490 		$sql_start = max(0, $pm_count - $sql_limit - $start);
   491 	}
   492 	else
   493 	{
   494 		// Select the sort order
   495 		$direction = ($sort_dir == 'd') ? 'DESC' : 'ASC';
   496 		$sql_start = $start;
   497 	}
   498 
   499 	// Sql sort order
   500 	if (is_array($sort_by_sql[$sort_key]))
   501 	{
   502 		$sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
   503 	}
   504 	else
   505 	{
   506 		$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
   507 	}
   508 
   509 	$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, p.message_reported
   510 		FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . " u
   511 		WHERE t.user_id = $user_id
   512 			AND p.author_id = u.user_id
   513 			AND $folder_sql
   514 			AND t.msg_id = p.msg_id
   515 			$sql_limit_time
   516 		ORDER BY $sql_sort_order";
   517 	$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
   518 
   519 	$pm_reported = array();
   520 	while ($row = $db->sql_fetchrow($result))
   521 	{
   522 		$rowset[$row['msg_id']] = $row;
   523 		$pm_list[] = $row['msg_id'];
   524 		if ($row['message_reported'])
   525 		{
   526 			$pm_reported[] = $row['msg_id'];
   527 		}
   528 	}
   529 	$db->sql_freeresult($result);
   530 
   531 	// Fetch the report_ids, if there are any reported pms.
   532 	if (!empty($pm_reported) && $auth->acl_getf_global('m_report'))
   533 	{
   534 		$sql = 'SELECT pm_id, report_id
   535 			FROM ' . REPORTS_TABLE . '
   536 			WHERE report_closed = 0
   537 				AND ' . $db->sql_in_set('pm_id', $pm_reported);
   538 		$result = $db->sql_query($sql);
   539 
   540 		while ($row = $db->sql_fetchrow($result))
   541 		{
   542 			$rowset[$row['pm_id']]['report_id'] = $row['report_id'];
   543 		}
   544 		$db->sql_freeresult($result);
   545 	}
   546 
   547 	$pm_list = ($store_reverse) ? array_reverse($pm_list) : $pm_list;
   548 
   549 	return array(
   550 		'pm_count'	=> $pm_count,
   551 		'pm_list'	=> $pm_list,
   552 		'rowset'	=> $rowset
   553 	);
   554 }
   555 
   556 ?>