1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/ucp/ucp_pm_viewfolder.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,556 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package ucp
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* View message folder
1.24 +* Called from ucp_pm with mode == 'view' && action == 'view_folder'
1.25 +*/
1.26 +function view_folder($id, $mode, $folder_id, $folder)
1.27 +{
1.28 + global $user, $template, $auth, $db, $cache;
1.29 + global $phpbb_root_path, $config, $phpEx;
1.30 +
1.31 + $submit_export = (isset($_POST['submit_export'])) ? true : false;
1.32 +
1.33 + $folder_info = get_pm_from($folder_id, $folder, $user->data['user_id']);
1.34 +
1.35 + if (!$submit_export)
1.36 + {
1.37 + $user->add_lang('viewforum');
1.38 +
1.39 + // Grab icons
1.40 + $icons = $cache->obtain_icons();
1.41 +
1.42 + $color_rows = array('marked', 'replied');
1.43 +
1.44 + // only show the friend/foe color rows if the module is enabled
1.45 + $zebra_enabled = false;
1.46 +
1.47 + $_module = new p_master();
1.48 + $_module->list_modules('ucp');
1.49 + $_module->set_active('zebra');
1.50 +
1.51 + $zebra_enabled = ($_module->active_module === false) ? false : true;
1.52 +
1.53 + unset($_module);
1.54 +
1.55 + if ($zebra_enabled)
1.56 + {
1.57 + $color_rows = array_merge($color_rows, array('friend', 'foe'));
1.58 + }
1.59 +
1.60 + foreach ($color_rows as $var)
1.61 + {
1.62 + $template->assign_block_vars('pm_colour_info', array(
1.63 + 'IMG' => $user->img("pm_{$var}", ''),
1.64 + 'CLASS' => "pm_{$var}_colour",
1.65 + 'LANG' => $user->lang[strtoupper($var) . '_MESSAGE'])
1.66 + );
1.67 + }
1.68 +
1.69 + $mark_options = array('mark_important', 'delete_marked');
1.70 +
1.71 + // Minimise edits
1.72 + if (!$auth->acl_get('u_pm_delete') && $key = array_search('delete_marked', $mark_options))
1.73 + {
1.74 + unset($mark_options[$key]);
1.75 + }
1.76 +
1.77 + $s_mark_options = '';
1.78 + foreach ($mark_options as $mark_option)
1.79 + {
1.80 + $s_mark_options .= '<option value="' . $mark_option . '">' . $user->lang[strtoupper($mark_option)] . '</option>';
1.81 + }
1.82 +
1.83 + // We do the folder moving options here too, for template authors to use...
1.84 + $s_folder_move_options = '';
1.85 + if ($folder_id != PRIVMSGS_NO_BOX && $folder_id != PRIVMSGS_OUTBOX)
1.86 + {
1.87 + foreach ($folder as $f_id => $folder_ary)
1.88 + {
1.89 + if ($f_id == PRIVMSGS_OUTBOX || $f_id == PRIVMSGS_SENTBOX || $f_id == $folder_id)
1.90 + {
1.91 + continue;
1.92 + }
1.93 +
1.94 + $s_folder_move_options .= '<option' . (($f_id != PRIVMSGS_INBOX) ? ' class="sep"' : '') . ' value="' . $f_id . '">';
1.95 + $s_folder_move_options .= sprintf($user->lang['MOVE_MARKED_TO_FOLDER'], $folder_ary['folder_name']);
1.96 + $s_folder_move_options .= (($folder_ary['unread_messages']) ? ' [' . $folder_ary['unread_messages'] . '] ' : '') . '</option>';
1.97 + }
1.98 + }
1.99 + $friend = $foe = array();
1.100 +
1.101 + // Get friends and foes
1.102 + $sql = 'SELECT *
1.103 + FROM ' . ZEBRA_TABLE . '
1.104 + WHERE user_id = ' . $user->data['user_id'];
1.105 + $result = $db->sql_query($sql);
1.106 +
1.107 + while ($row = $db->sql_fetchrow($result))
1.108 + {
1.109 + $friend[$row['zebra_id']] = $row['friend'];
1.110 + $foe[$row['zebra_id']] = $row['foe'];
1.111 + }
1.112 + $db->sql_freeresult($result);
1.113 +
1.114 + $template->assign_vars(array(
1.115 + 'S_MARK_OPTIONS' => $s_mark_options,
1.116 + 'S_MOVE_MARKED_OPTIONS' => $s_folder_move_options)
1.117 + );
1.118 +
1.119 + // Okay, lets dump out the page ...
1.120 + if (sizeof($folder_info['pm_list']))
1.121 + {
1.122 + $address_list = array();
1.123 +
1.124 + // Build Recipient List if in outbox/sentbox - max two additional queries
1.125 + if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
1.126 + {
1.127 + $address_list = get_recipient_strings($folder_info['rowset']);
1.128 + }
1.129 +
1.130 + foreach ($folder_info['pm_list'] as $message_id)
1.131 + {
1.132 + $row = &$folder_info['rowset'][$message_id];
1.133 +
1.134 + $folder_img = ($row['pm_unread']) ? 'pm_unread' : 'pm_read';
1.135 + $folder_alt = ($row['pm_unread']) ? 'NEW_MESSAGES' : 'NO_NEW_MESSAGES';
1.136 +
1.137 + // Generate all URIs ...
1.138 + $view_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=view&f=$folder_id&p=$message_id");
1.139 + $remove_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=compose&action=delete&p=$message_id");
1.140 +
1.141 + $row_indicator = '';
1.142 + foreach ($color_rows as $var)
1.143 + {
1.144 + if (($var != 'friend' && $var != 'foe' && $row['pm_' . $var])
1.145 + ||
1.146 + (($var == 'friend' || $var == 'foe') && isset(${$var}[$row['author_id']]) && ${$var}[$row['author_id']]))
1.147 + {
1.148 + $row_indicator = $var;
1.149 + break;
1.150 + }
1.151 + }
1.152 +
1.153 + // Send vars to template
1.154 + $template->assign_block_vars('messagerow', array(
1.155 + 'PM_CLASS' => ($row_indicator) ? 'pm_' . $row_indicator . '_colour' : '',
1.156 +
1.157 + 'MESSAGE_AUTHOR_FULL' => get_username_string('full', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
1.158 + 'MESSAGE_AUTHOR_COLOUR' => get_username_string('colour', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
1.159 + 'MESSAGE_AUTHOR' => get_username_string('username', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
1.160 + 'U_MESSAGE_AUTHOR' => get_username_string('profile', $row['author_id'], $row['username'], $row['user_colour'], $row['username']),
1.161 +
1.162 + 'FOLDER_ID' => $folder_id,
1.163 + 'MESSAGE_ID' => $message_id,
1.164 + 'SENT_TIME' => $user->format_date($row['message_time']),
1.165 + 'SUBJECT' => censor_text($row['message_subject']),
1.166 + 'FOLDER' => (isset($folder[$row['folder_id']])) ? $folder[$row['folder_id']]['folder_name'] : '',
1.167 + 'U_FOLDER' => (isset($folder[$row['folder_id']])) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'folder=' . $row['folder_id']) : '',
1.168 + '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="" />' : '',
1.169 + 'PM_ICON_URL' => (!empty($icons[$row['icon_id']])) ? $config['icons_path'] . '/' . $icons[$row['icon_id']]['img'] : '',
1.170 + 'FOLDER_IMG' => $user->img($folder_img, $folder_alt),
1.171 + 'FOLDER_IMG_SRC' => $user->img($folder_img, $folder_alt, false, '', 'src'),
1.172 + 'PM_IMG' => ($row_indicator) ? $user->img('pm_' . $row_indicator, '') : '',
1.173 + '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']) : '',
1.174 +
1.175 + 'S_PM_UNREAD' => ($row['pm_unread']) ? true : false,
1.176 + 'S_PM_DELETED' => ($row['pm_deleted']) ? true : false,
1.177 + 'S_PM_REPORTED' => (isset($row['report_id'])) ? true : false,
1.178 + 'S_AUTHOR_DELETED' => ($row['author_id'] == ANONYMOUS) ? true : false,
1.179 +
1.180 + 'U_VIEW_PM' => ($row['pm_deleted']) ? '' : $view_message_url,
1.181 + 'U_REMOVE_PM' => ($row['pm_deleted']) ? $remove_message_url : '',
1.182 + 'U_MCP_REPORT' => (isset($row['report_id'])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=pm_reports&mode=pm_report_details&r=' . $row['report_id']) : '',
1.183 + 'RECIPIENTS' => ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? implode(', ', $address_list[$message_id]) : '')
1.184 + );
1.185 + }
1.186 + unset($folder_info['rowset']);
1.187 +
1.188 + $template->assign_vars(array(
1.189 + 'S_SHOW_RECIPIENTS' => ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? true : false,
1.190 + 'S_SHOW_COLOUR_LEGEND' => true,
1.191 +
1.192 + 'REPORTED_IMG' => $user->img('icon_topic_reported', 'PM_REPORTED'),
1.193 + 'S_PM_ICONS' => ($config['enable_pm_icons']) ? true : false)
1.194 + );
1.195 + }
1.196 + }
1.197 + else
1.198 + {
1.199 + $export_type = request_var('export_option', '');
1.200 + $enclosure = request_var('enclosure', '');
1.201 + $delimiter = request_var('delimiter', '');
1.202 +
1.203 + if ($export_type == 'CSV' && ($delimiter === '' || $enclosure === ''))
1.204 + {
1.205 + $template->assign_var('PROMPT', true);
1.206 + }
1.207 + else
1.208 + {
1.209 + // Build Recipient List if in outbox/sentbox
1.210 +
1.211 + $address_temp = $address = $data = array();
1.212 +
1.213 + if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
1.214 + {
1.215 + foreach ($folder_info['rowset'] as $message_id => $row)
1.216 + {
1.217 + $address_temp[$message_id] = rebuild_header(array('to' => $row['to_address'], 'bcc' => $row['bcc_address']));
1.218 + $address[$message_id] = array();
1.219 + }
1.220 + }
1.221 +
1.222 + foreach ($folder_info['pm_list'] as $message_id)
1.223 + {
1.224 + $row = &$folder_info['rowset'][$message_id];
1.225 +
1.226 + include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
1.227 +
1.228 + $sql = 'SELECT p.message_text, p.bbcode_uid
1.229 + FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . ' u
1.230 + WHERE t.user_id = ' . $user->data['user_id'] . "
1.231 + AND p.author_id = u.user_id
1.232 + AND t.folder_id = $folder_id
1.233 + AND t.msg_id = p.msg_id
1.234 + AND p.msg_id = $message_id";
1.235 + $result = $db->sql_query_limit($sql, 1);
1.236 + $message_row = $db->sql_fetchrow($result);
1.237 + $db->sql_freeresult($result);
1.238 +
1.239 + $_types = array('u', 'g');
1.240 + foreach ($_types as $ug_type)
1.241 + {
1.242 + if (isset($address_temp[$message_id][$ug_type]) && sizeof($address_temp[$message_id][$ug_type]))
1.243 + {
1.244 + if (!isset($address[$message_id][$ug_type]))
1.245 + {
1.246 + $address[$message_id][$ug_type] = array();
1.247 + }
1.248 + if ($ug_type == 'u')
1.249 + {
1.250 + $sql = 'SELECT user_id as id, username as name
1.251 + FROM ' . USERS_TABLE . '
1.252 + WHERE ';
1.253 + }
1.254 + else
1.255 + {
1.256 + $sql = 'SELECT group_id as id, group_name as name
1.257 + FROM ' . GROUPS_TABLE . '
1.258 + WHERE ';
1.259 + }
1.260 + $sql .= $db->sql_in_set(($ug_type == 'u') ? 'user_id' : 'group_id', array_map('intval', array_keys($address_temp[$message_id][$ug_type])));
1.261 +
1.262 + $result = $db->sql_query($sql);
1.263 +
1.264 + while ($info_row = $db->sql_fetchrow($result))
1.265 + {
1.266 + $address[$message_id][$ug_type][$address_temp[$message_id][$ug_type][$info_row['id']]][] = $info_row['name'];
1.267 + unset($address_temp[$message_id][$ug_type][$info_row['id']]);
1.268 + }
1.269 + $db->sql_freeresult($result);
1.270 + }
1.271 + }
1.272 +
1.273 + // There is the chance that all recipients of the message got deleted. To avoid creating
1.274 + // exports without recipients, we add a bogus "undisclosed recipient".
1.275 + if (!(isset($address[$message_id]['g']) && sizeof($address[$message_id]['g'])) &&
1.276 + !(isset($address[$message_id]['u']) && sizeof($address[$message_id]['u'])))
1.277 + {
1.278 + $address[$message_id]['u'] = array();
1.279 + $address[$message_id]['u']['to'] = array();
1.280 + $address[$message_id]['u']['to'][] = $user->lang['UNDISCLOSED_RECIPIENT'];
1.281 + }
1.282 +
1.283 + decode_message($message_row['message_text'], $message_row['bbcode_uid']);
1.284 +
1.285 + $data[] = array(
1.286 + 'subject' => censor_text($row['message_subject']),
1.287 + 'sender' => $row['username'],
1.288 + // ISO 8601 date. For PHP4 we are able to hardcode the timezone because $user->format_date() does not set it.
1.289 + 'date' => $user->format_date($row['message_time'], (PHP_VERSION >= 5) ? 'c' : "Y-m-d\TH:i:s+00:00", true),
1.290 + 'to' => ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? $address[$message_id] : '',
1.291 + 'message' => $message_row['message_text']
1.292 + );
1.293 + }
1.294 +
1.295 + switch ($export_type)
1.296 + {
1.297 + case 'CSV':
1.298 + case 'CSV_EXCEL':
1.299 + $mimetype = 'text/csv';
1.300 + $filetype = 'csv';
1.301 +
1.302 + if ($export_type == 'CSV_EXCEL')
1.303 + {
1.304 + $enclosure = '"';
1.305 + $delimiter = ',';
1.306 + $newline = "\r\n";
1.307 + }
1.308 + else
1.309 + {
1.310 + $newline = "\n";
1.311 + }
1.312 +
1.313 + $string = '';
1.314 + foreach ($data as $value)
1.315 + {
1.316 + $recipients = $value['to'];
1.317 + $value['to'] = $value['bcc'] = '';
1.318 +
1.319 + if (is_array($recipients))
1.320 + {
1.321 + foreach ($recipients as $values)
1.322 + {
1.323 + $value['bcc'] .= (isset($values['bcc']) && is_array($values['bcc'])) ? ',' . implode(',', $values['bcc']) : '';
1.324 + $value['to'] .= (isset($values['to']) && is_array($values['to'])) ? ',' . implode(',', $values['to']) : '';
1.325 + }
1.326 +
1.327 + // Remove the commas which will appear before the first entry.
1.328 + $value['to'] = substr($value['to'], 1);
1.329 + $value['bcc'] = substr($value['bcc'], 1);
1.330 + }
1.331 +
1.332 + foreach ($value as $tag => $text)
1.333 + {
1.334 + $cell = str_replace($enclosure, $enclosure . $enclosure, $text);
1.335 +
1.336 + if (strpos($cell, $enclosure) !== false || strpos($cell, $delimiter) !== false || strpos($cell, $newline) !== false)
1.337 + {
1.338 + $string .= $enclosure . $text . $enclosure . $delimiter;
1.339 + }
1.340 + else
1.341 + {
1.342 + $string .= $cell . $delimiter;
1.343 + }
1.344 + }
1.345 + $string = substr($string, 0, -1) . $newline;
1.346 + }
1.347 + break;
1.348 +
1.349 + case 'XML':
1.350 + $mimetype = 'application/xml';
1.351 + $filetype = 'xml';
1.352 + $string = '<?xml version="1.0"?>' . "\n";
1.353 + $string .= "<phpbb>\n";
1.354 +
1.355 + foreach ($data as $value)
1.356 + {
1.357 + $string .= "\t<privmsg>\n";
1.358 +
1.359 + if (is_array($value['to']))
1.360 + {
1.361 + foreach ($value['to'] as $key => $values)
1.362 + {
1.363 + foreach ($values as $type => $types)
1.364 + {
1.365 + foreach ($types as $name)
1.366 + {
1.367 + $string .= "\t\t<recipient type=\"$type\" status=\"$key\">$name</recipient>\n";
1.368 + }
1.369 + }
1.370 + }
1.371 + }
1.372 +
1.373 + unset($value['to']);
1.374 +
1.375 + foreach ($value as $tag => $text)
1.376 + {
1.377 + $string .= "\t\t<$tag>$text</$tag>\n";
1.378 + }
1.379 +
1.380 + $string .= "\t</privmsg>\n";
1.381 + }
1.382 + $string .= '</phpbb>';
1.383 + break;
1.384 + }
1.385 +
1.386 + header('Pragma: no-cache');
1.387 + header("Content-Type: $mimetype; name=\"data.$filetype\"");
1.388 + header("Content-disposition: attachment; filename=data.$filetype");
1.389 + echo $string;
1.390 + exit;
1.391 + }
1.392 + }
1.393 +}
1.394 +
1.395 +/**
1.396 +* Get Messages from folder/user
1.397 +*/
1.398 +function get_pm_from($folder_id, $folder, $user_id)
1.399 +{
1.400 + global $user, $db, $template, $config, $auth, $phpbb_root_path, $phpEx;
1.401 +
1.402 + $start = request_var('start', 0);
1.403 +
1.404 + // Additional vars later, pm ordering is mostly different from post ordering. :/
1.405 + $sort_days = request_var('st', 0);
1.406 + $sort_key = request_var('sk', 't');
1.407 + $sort_dir = request_var('sd', 'd');
1.408 +
1.409 + // PM ordering options
1.410 + $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']);
1.411 +
1.412 + // No sort by Author for sentbox/outbox (already only author available)
1.413 + // Also, sort by msg_id for the time - private messages are not as prone to errors as posts are.
1.414 + if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
1.415 + {
1.416 + $sort_by_text = array('t' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
1.417 + $sort_by_sql = array('t' => 'p.message_time', 's' => array('p.message_subject', 'p.message_time'));
1.418 + }
1.419 + else
1.420 + {
1.421 + $sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
1.422 + $sort_by_sql = array('a' => array('u.username_clean', 'p.message_time'), 't' => 'p.message_time', 's' => array('p.message_subject', 'p.message_time'));
1.423 + }
1.424 +
1.425 + $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
1.426 + 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);
1.427 +
1.428 + $folder_sql = 't.folder_id = ' . (int) $folder_id;
1.429 +
1.430 + // Limit pms to certain time frame, obtain correct pm count
1.431 + if ($sort_days)
1.432 + {
1.433 + $min_post_time = time() - ($sort_days * 86400);
1.434 +
1.435 + if (isset($_POST['sort']))
1.436 + {
1.437 + $start = 0;
1.438 + }
1.439 +
1.440 + $sql = 'SELECT COUNT(t.msg_id) AS pm_count
1.441 + FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . " p
1.442 + WHERE $folder_sql
1.443 + AND t.user_id = $user_id
1.444 + AND t.msg_id = p.msg_id
1.445 + AND p.message_time >= $min_post_time";
1.446 + $result = $db->sql_query_limit($sql, 1);
1.447 + $pm_count = (int) $db->sql_fetchfield('pm_count');
1.448 + $db->sql_freeresult($result);
1.449 +
1.450 + $sql_limit_time = "AND p.message_time >= $min_post_time";
1.451 + }
1.452 + else
1.453 + {
1.454 + $pm_count = (!empty($folder[$folder_id]['num_messages'])) ? $folder[$folder_id]['num_messages'] : 0;
1.455 + $sql_limit_time = '';
1.456 + }
1.457 +
1.458 + $template->assign_vars(array(
1.459 + '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),
1.460 + 'PAGE_NUMBER' => on_page($pm_count, $config['topics_per_page'], $start),
1.461 + 'TOTAL_MESSAGES' => (($pm_count == 1) ? $user->lang['VIEW_PM_MESSAGE'] : sprintf($user->lang['VIEW_PM_MESSAGES'], $pm_count)),
1.462 +
1.463 + 'POST_IMG' => (!$auth->acl_get('u_sendpm')) ? $user->img('button_topic_locked', 'POST_PM_LOCKED') : $user->img('button_pm_new', 'POST_NEW_PM'),
1.464 +
1.465 + 'S_NO_AUTH_SEND_MESSAGE' => !$auth->acl_get('u_sendpm'),
1.466 +
1.467 + 'S_SELECT_SORT_DIR' => $s_sort_dir,
1.468 + 'S_SELECT_SORT_KEY' => $s_sort_key,
1.469 + 'S_SELECT_SORT_DAYS' => $s_limit_days,
1.470 + 'S_TOPIC_ICONS' => ($config['enable_pm_icons']) ? true : false,
1.471 +
1.472 + 'U_POST_NEW_TOPIC' => ($auth->acl_get('u_sendpm')) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=compose') : '',
1.473 + 'S_PM_ACTION' => append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=view&action=view_folder&f=$folder_id" . (($start !== 0) ? "&start=$start" : '')),
1.474 + ));
1.475 +
1.476 + // Grab all pm data
1.477 + $rowset = $pm_list = array();
1.478 +
1.479 + // If the user is trying to reach late pages, start searching from the end
1.480 + $store_reverse = false;
1.481 + $sql_limit = $config['topics_per_page'];
1.482 + if ($start > $pm_count / 2)
1.483 + {
1.484 + $store_reverse = true;
1.485 +
1.486 + if ($start + $config['topics_per_page'] > $pm_count)
1.487 + {
1.488 + $sql_limit = min($config['topics_per_page'], max(1, $pm_count - $start));
1.489 + }
1.490 +
1.491 + // Select the sort order
1.492 + $direction = ($sort_dir == 'd') ? 'ASC' : 'DESC';
1.493 + $sql_start = max(0, $pm_count - $sql_limit - $start);
1.494 + }
1.495 + else
1.496 + {
1.497 + // Select the sort order
1.498 + $direction = ($sort_dir == 'd') ? 'DESC' : 'ASC';
1.499 + $sql_start = $start;
1.500 + }
1.501 +
1.502 + // Sql sort order
1.503 + if (is_array($sort_by_sql[$sort_key]))
1.504 + {
1.505 + $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
1.506 + }
1.507 + else
1.508 + {
1.509 + $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
1.510 + }
1.511 +
1.512 + $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
1.513 + FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . " u
1.514 + WHERE t.user_id = $user_id
1.515 + AND p.author_id = u.user_id
1.516 + AND $folder_sql
1.517 + AND t.msg_id = p.msg_id
1.518 + $sql_limit_time
1.519 + ORDER BY $sql_sort_order";
1.520 + $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
1.521 +
1.522 + $pm_reported = array();
1.523 + while ($row = $db->sql_fetchrow($result))
1.524 + {
1.525 + $rowset[$row['msg_id']] = $row;
1.526 + $pm_list[] = $row['msg_id'];
1.527 + if ($row['message_reported'])
1.528 + {
1.529 + $pm_reported[] = $row['msg_id'];
1.530 + }
1.531 + }
1.532 + $db->sql_freeresult($result);
1.533 +
1.534 + // Fetch the report_ids, if there are any reported pms.
1.535 + if (!empty($pm_reported) && $auth->acl_getf_global('m_report'))
1.536 + {
1.537 + $sql = 'SELECT pm_id, report_id
1.538 + FROM ' . REPORTS_TABLE . '
1.539 + WHERE report_closed = 0
1.540 + AND ' . $db->sql_in_set('pm_id', $pm_reported);
1.541 + $result = $db->sql_query($sql);
1.542 +
1.543 + while ($row = $db->sql_fetchrow($result))
1.544 + {
1.545 + $rowset[$row['pm_id']]['report_id'] = $row['report_id'];
1.546 + }
1.547 + $db->sql_freeresult($result);
1.548 + }
1.549 +
1.550 + $pm_list = ($store_reverse) ? array_reverse($pm_list) : $pm_list;
1.551 +
1.552 + return array(
1.553 + 'pm_count' => $pm_count,
1.554 + 'pm_list' => $pm_list,
1.555 + 'rowset' => $rowset
1.556 + );
1.557 +}
1.558 +
1.559 +?>
1.560 \ No newline at end of file