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