6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
21 * Called from ucp_pm with mode == 'view' && action == 'view_folder'
23 function view_folder($id, $mode, $folder_id, $folder)
25 global $user, $template, $auth, $db, $cache;
26 global $phpbb_root_path, $config, $phpEx;
28 $submit_export = (isset($_POST['submit_export'])) ? true : false;
30 $folder_info = get_pm_from($folder_id, $folder, $user->data['user_id']);
34 $user->add_lang('viewforum');
37 $icons = $cache->obtain_icons();
39 $color_rows = array('marked', 'replied');
41 // only show the friend/foe color rows if the module is enabled
42 $zebra_enabled = false;
44 $_module = new p_master();
45 $_module->list_modules('ucp');
46 $_module->set_active('zebra');
48 $zebra_enabled = ($_module->active_module === false) ? false : true;
54 $color_rows = array_merge($color_rows, array('friend', 'foe'));
57 foreach ($color_rows as $var)
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'])
66 $mark_options = array('mark_important', 'delete_marked');
69 if (!$auth->acl_get('u_pm_delete') && $key = array_search('delete_marked', $mark_options))
71 unset($mark_options[$key]);
75 foreach ($mark_options as $mark_option)
77 $s_mark_options .= '<option value="' . $mark_option . '">' . $user->lang[strtoupper($mark_option)] . '</option>';
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)
84 foreach ($folder as $f_id => $folder_ary)
86 if ($f_id == PRIVMSGS_OUTBOX || $f_id == PRIVMSGS_SENTBOX || $f_id == $folder_id)
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>';
96 $friend = $foe = array();
98 // Get friends and foes
100 FROM ' . ZEBRA_TABLE . '
101 WHERE user_id = ' . $user->data['user_id'];
102 $result = $db->sql_query($sql);
104 while ($row = $db->sql_fetchrow($result))
106 $friend[$row['zebra_id']] = $row['friend'];
107 $foe[$row['zebra_id']] = $row['foe'];
109 $db->sql_freeresult($result);
111 $template->assign_vars(array(
112 'S_MARK_OPTIONS' => $s_mark_options,
113 'S_MOVE_MARKED_OPTIONS' => $s_folder_move_options)
116 // Okay, lets dump out the page ...
117 if (sizeof($folder_info['pm_list']))
119 $address_list = array();
121 // Build Recipient List if in outbox/sentbox - max two additional queries
122 if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
124 $address_list = get_recipient_strings($folder_info['rowset']);
127 foreach ($folder_info['pm_list'] as $message_id)
129 $row = &$folder_info['rowset'][$message_id];
131 $folder_img = ($row['pm_unread']) ? 'pm_unread' : 'pm_read';
132 $folder_alt = ($row['pm_unread']) ? 'NEW_MESSAGES' : 'NO_NEW_MESSAGES';
134 // Generate all URIs ...
135 $view_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=view&f=$folder_id&p=$message_id");
136 $remove_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&mode=compose&action=delete&p=$message_id");
139 foreach ($color_rows as $var)
141 if (($var != 'friend' && $var != 'foe' && $row['pm_' . $var])
143 (($var == 'friend' || $var == 'foe') && isset(${$var}[$row['author_id']]) && ${$var}[$row['author_id']]))
145 $row_indicator = $var;
150 // Send vars to template
151 $template->assign_block_vars('messagerow', array(
152 'PM_CLASS' => ($row_indicator) ? 'pm_' . $row_indicator . '_colour' : '',
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']),
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']) : '',
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,
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&mode=pm_report_details&r=' . $row['report_id']) : '',
180 'RECIPIENTS' => ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX) ? implode(', ', $address_list[$message_id]) : '')
183 unset($folder_info['rowset']);
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,
189 'REPORTED_IMG' => $user->img('icon_topic_reported', 'PM_REPORTED'),
190 'S_PM_ICONS' => ($config['enable_pm_icons']) ? true : false)
196 $export_type = request_var('export_option', '');
197 $enclosure = request_var('enclosure', '');
198 $delimiter = request_var('delimiter', '');
200 if ($export_type == 'CSV' && ($delimiter === '' || $enclosure === ''))
202 $template->assign_var('PROMPT', true);
206 // Build Recipient List if in outbox/sentbox
208 $address_temp = $address = $data = array();
210 if ($folder_id == PRIVMSGS_OUTBOX || $folder_id == PRIVMSGS_SENTBOX)
212 foreach ($folder_info['rowset'] as $message_id => $row)
214 $address_temp[$message_id] = rebuild_header(array('to' => $row['to_address'], 'bcc' => $row['bcc_address']));
215 $address[$message_id] = array();
219 foreach ($folder_info['pm_list'] as $message_id)
221 $row = &$folder_info['rowset'][$message_id];
223 include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
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);
236 $_types = array('u', 'g');
237 foreach ($_types as $ug_type)
239 if (isset($address_temp[$message_id][$ug_type]) && sizeof($address_temp[$message_id][$ug_type]))
241 if (!isset($address[$message_id][$ug_type]))
243 $address[$message_id][$ug_type] = array();
247 $sql = 'SELECT user_id as id, username as name
248 FROM ' . USERS_TABLE . '
253 $sql = 'SELECT group_id as id, group_name as name
254 FROM ' . GROUPS_TABLE . '
257 $sql .= $db->sql_in_set(($ug_type == 'u') ? 'user_id' : 'group_id', array_map('intval', array_keys($address_temp[$message_id][$ug_type])));
259 $result = $db->sql_query($sql);
261 while ($info_row = $db->sql_fetchrow($result))
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']]);
266 $db->sql_freeresult($result);
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'])))
275 $address[$message_id]['u'] = array();
276 $address[$message_id]['u']['to'] = array();
277 $address[$message_id]['u']['to'][] = $user->lang['UNDISCLOSED_RECIPIENT'];
280 decode_message($message_row['message_text'], $message_row['bbcode_uid']);
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']
292 switch ($export_type)
296 $mimetype = 'text/csv';
299 if ($export_type == 'CSV_EXCEL')
311 foreach ($data as $value)
313 $recipients = $value['to'];
314 $value['to'] = $value['bcc'] = '';
316 if (is_array($recipients))
318 foreach ($recipients as $values)
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']) : '';
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);
329 foreach ($value as $tag => $text)
331 $cell = str_replace($enclosure, $enclosure . $enclosure, $text);
333 if (strpos($cell, $enclosure) !== false || strpos($cell, $delimiter) !== false || strpos($cell, $newline) !== false)
335 $string .= $enclosure . $text . $enclosure . $delimiter;
339 $string .= $cell . $delimiter;
342 $string = substr($string, 0, -1) . $newline;
347 $mimetype = 'application/xml';
349 $string = '<?xml version="1.0"?>' . "\n";
350 $string .= "<phpbb>\n";
352 foreach ($data as $value)
354 $string .= "\t<privmsg>\n";
356 if (is_array($value['to']))
358 foreach ($value['to'] as $key => $values)
360 foreach ($values as $type => $types)
362 foreach ($types as $name)
364 $string .= "\t\t<recipient type=\"$type\" status=\"$key\">$name</recipient>\n";
372 foreach ($value as $tag => $text)
374 $string .= "\t\t<$tag>$text</$tag>\n";
377 $string .= "\t</privmsg>\n";
379 $string .= '</phpbb>';
383 header('Pragma: no-cache');
384 header("Content-Type: $mimetype; name=\"data.$filetype\"");
385 header("Content-disposition: attachment; filename=data.$filetype");
393 * Get Messages from folder/user
395 function get_pm_from($folder_id, $folder, $user_id)
397 global $user, $db, $template, $config, $auth, $phpbb_root_path, $phpEx;
399 $start = request_var('start', 0);
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');
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']);
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)
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'));
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'));
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);
425 $folder_sql = 't.folder_id = ' . (int) $folder_id;
427 // Limit pms to certain time frame, obtain correct pm count
430 $min_post_time = time() - ($sort_days * 86400);
432 if (isset($_POST['sort']))
437 $sql = 'SELECT COUNT(t.msg_id) AS pm_count
438 FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . " p
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);
447 $sql_limit_time = "AND p.message_time >= $min_post_time";
451 $pm_count = (!empty($folder[$folder_id]['num_messages'])) ? $folder[$folder_id]['num_messages'] : 0;
452 $sql_limit_time = '';
455 $template->assign_vars(array(
456 '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),
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)),
460 'POST_IMG' => (!$auth->acl_get('u_sendpm')) ? $user->img('button_topic_locked', 'POST_PM_LOCKED') : $user->img('button_pm_new', 'POST_NEW_PM'),
462 'S_NO_AUTH_SEND_MESSAGE' => !$auth->acl_get('u_sendpm'),
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,
469 'U_POST_NEW_TOPIC' => ($auth->acl_get('u_sendpm')) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&mode=compose') : '',
470 'S_PM_ACTION' => append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&mode=view&action=view_folder&f=$folder_id" . (($start !== 0) ? "&start=$start" : '')),
474 $rowset = $pm_list = array();
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)
481 $store_reverse = true;
483 if ($start + $config['topics_per_page'] > $pm_count)
485 $sql_limit = min($config['topics_per_page'], max(1, $pm_count - $start));
488 // Select the sort order
489 $direction = ($sort_dir == 'd') ? 'ASC' : 'DESC';
490 $sql_start = max(0, $pm_count - $sql_limit - $start);
494 // Select the sort order
495 $direction = ($sort_dir == 'd') ? 'DESC' : 'ASC';
500 if (is_array($sort_by_sql[$sort_key]))
502 $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
506 $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
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
514 AND t.msg_id = p.msg_id
516 ORDER BY $sql_sort_order";
517 $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
519 $pm_reported = array();
520 while ($row = $db->sql_fetchrow($result))
522 $rowset[$row['msg_id']] = $row;
523 $pm_list[] = $row['msg_id'];
524 if ($row['message_reported'])
526 $pm_reported[] = $row['msg_id'];
529 $db->sql_freeresult($result);
531 // Fetch the report_ids, if there are any reported pms.
532 if (!empty($pm_reported) && $auth->acl_getf_global('m_report'))
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);
540 while ($row = $db->sql_fetchrow($result))
542 $rowset[$row['pm_id']]['report_id'] = $row['report_id'];
544 $db->sql_freeresult($result);
547 $pm_list = ($store_reverse) ? array_reverse($pm_list) : $pm_list;
550 'pm_count' => $pm_count,
551 'pm_list' => $pm_list,