Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 238
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
view_message
0.00% covered (danger)
0.00%
0 / 201
0.00% covered (danger)
0.00%
0 / 1
6642
get_user_information
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
342
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 private message
24*/
25function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row)
26{
27    global $user, $template, $auth, $db, $phpbb_container;
28    global $phpbb_root_path, $request, $phpEx, $config, $phpbb_dispatcher;
29
30    $user->add_lang(array('viewtopic', 'memberlist'));
31
32    $msg_id        = (int) $msg_id;
33    $folder_id    = (int) $folder_id;
34    $author_id    = (int) $message_row['author_id'];
35    $view        = $request->variable('view', '');
36
37    /**
38    * Modify private message data before it is prepared to be displayed
39    *
40    * @event core.ucp_pm_view_message_before
41    * @var int        folder_id        ID of the folder the message is in
42    * @var array    folder            Array with data of user's message folders
43    * @var int        msg_id            ID of the private message
44    * @var array    message_row        Array with message data
45    * @var int        author_id        ID of the message author
46    * @since 3.2.10-RC1
47    * @since 3.3.1-RC1
48    */
49    $vars = [
50        'folder_id',
51        'folder',
52        'msg_id',
53        'message_row',
54        'author_id',
55    ];
56    extract($phpbb_dispatcher->trigger_event('core.ucp_pm_view_message_before', compact($vars)));
57
58    // Not able to view message, it was deleted by the sender
59    if ($message_row['pm_deleted'])
60    {
61        $meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;folder=$folder_id");
62        $message = $user->lang['NO_AUTH_READ_REMOVED_MESSAGE'];
63
64        $message .= '<br /><br />' . sprintf($user->lang['RETURN_FOLDER'], '<a href="' . $meta_info . '">', '</a>');
65        send_status_line(403, 'Forbidden');
66        trigger_error($message);
67    }
68
69    // Do not allow hold messages to be seen
70    if ($folder_id == PRIVMSGS_HOLD_BOX)
71    {
72        trigger_error('NO_AUTH_READ_HOLD_MESSAGE');
73    }
74
75    // Load the custom profile fields
76    if ($config['load_cpf_pm'])
77    {
78        /* @var $cp \phpbb\profilefields\manager */
79        $cp = $phpbb_container->get('profilefields.manager');
80
81        $profile_fields = $cp->grab_profile_fields_data($author_id);
82    }
83
84    // Assign TO/BCC Addresses to template
85    write_pm_addresses(array('to' => $message_row['to_address'], 'bcc' => $message_row['bcc_address']), $author_id);
86
87    $user_info = get_user_information($author_id, $message_row);
88
89    // Parse the message and subject
90    $parse_flags = ($message_row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
91    $message = generate_text_for_display($message_row['message_text'], $message_row['bbcode_uid'], $message_row['bbcode_bitfield'], $parse_flags, true);
92
93    // Replace naughty words such as farty pants
94    $message_row['message_subject'] = censor_text($message_row['message_subject']);
95
96    // Editing information
97    if ($message_row['message_edit_count'] && $config['display_last_edited'])
98    {
99        if (!$message_row['message_edit_user'])
100        {
101            $display_username = get_username_string('full', $author_id, $user_info['username'], $user_info['user_colour']);
102        }
103        else
104        {
105            $edit_user_info = get_user_information($message_row['message_edit_user'], false);
106            $display_username = get_username_string('full', $message_row['message_edit_user'], $edit_user_info['username'], $edit_user_info['user_colour']);
107        }
108        $l_edited_by = '<br /><br />' . $user->lang('EDITED_TIMES_TOTAL', (int) $message_row['message_edit_count'], $display_username, $user->format_date($message_row['message_edit_time'], false, true));
109    }
110    else
111    {
112        $l_edited_by = '';
113    }
114
115    // Pull attachment data
116    $display_notice = false;
117    $attachments = array();
118
119    if ($message_row['message_attachment'] && $config['allow_pm_attach'])
120    {
121        if ($auth->acl_get('u_pm_download'))
122        {
123            $sql = 'SELECT *
124                FROM ' . ATTACHMENTS_TABLE . "
125                WHERE post_msg_id = $msg_id
126                    AND in_message = 1
127                ORDER BY filetime DESC, post_msg_id ASC";
128            $result = $db->sql_query($sql);
129
130            while ($row = $db->sql_fetchrow($result))
131            {
132                $attachments[] = $row;
133            }
134            $db->sql_freeresult($result);
135
136            // No attachments exist, but message table thinks they do so go ahead and reset attach flags
137            if (!count($attachments))
138            {
139                $sql = 'UPDATE ' . PRIVMSGS_TABLE . "
140                    SET message_attachment = 0
141                    WHERE msg_id = $msg_id";
142                $db->sql_query($sql);
143            }
144        }
145        else
146        {
147            $display_notice = true;
148        }
149    }
150
151    // Assign inline attachments
152    if (!empty($attachments))
153    {
154        $update_count = array();
155        parse_attachments(false, $message, $attachments, $update_count);
156
157        // Update the attachment download counts
158        if (count($update_count))
159        {
160            $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
161                SET download_count = download_count + 1
162                WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
163            $db->sql_query($sql);
164        }
165    }
166
167    $user_info['sig'] = '';
168
169    $signature = ($message_row['enable_sig'] && $config['allow_sig'] && $auth->acl_get('u_sig') && $user->optionget('viewsigs')) ? $user_info['user_sig'] : '';
170
171    // End signature parsing, only if needed
172    if ($signature)
173    {
174        $parse_flags = ($user_info['user_sig_bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
175        $signature = generate_text_for_display($signature, $user_info['user_sig_bbcode_uid'], $user_info['user_sig_bbcode_bitfield'], $parse_flags, true);
176    }
177
178    $url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm');
179
180    // Number of "to" recipients
181    $num_recipients = (int) preg_match_all('/:?(u|g)_([0-9]+):?/', $message_row['to_address'], $match);
182
183    $bbcode_status    = ($config['allow_bbcode'] && $config['auth_bbcode_pm'] && $auth->acl_get('u_pm_bbcode')) ? true : false;
184
185    // Get the profile fields template data
186    $cp_row = array();
187    if ($config['load_cpf_pm'] && isset($profile_fields[$author_id]))
188    {
189        // Filter the fields we don't want to show
190        foreach ($profile_fields[$author_id] as $used_ident => $profile_field)
191        {
192            if (!$profile_field['data']['field_show_on_pm'])
193            {
194                unset($profile_fields[$author_id][$used_ident]);
195            }
196        }
197
198        if (isset($profile_fields[$author_id]))
199        {
200            $cp_row = $cp->generate_profile_fields_template_data($profile_fields[$author_id]);
201        }
202    }
203
204    $u_pm = $u_jabber = '';
205
206    if ($config['allow_privmsg'] && $auth->acl_get('u_sendpm') && ($user_info['user_allow_pm'] || $auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_')))
207    {
208        $u_pm = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=compose&amp;u=' . $author_id);
209    }
210
211    if ($config['jab_enable'] && $user_info['user_jabber'] && $auth->acl_get('u_sendim'))
212    {
213        $u_jabber = append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=contact&amp;action=jabber&amp;u=' . $author_id);
214    }
215
216    $can_edit_pm = ($message_row['message_time'] > time() - ($config['pm_edit_time'] * 60) || !$config['pm_edit_time']) && $folder_id == PRIVMSGS_OUTBOX && $auth->acl_get('u_pm_edit');
217
218    $msg_data = array(
219        'MESSAGE_AUTHOR_FULL'        => get_username_string('full', $author_id, $user_info['username'], $user_info['user_colour'], $user_info['username']),
220        'MESSAGE_AUTHOR_COLOUR'        => get_username_string('colour', $author_id, $user_info['username'], $user_info['user_colour'], $user_info['username']),
221        'MESSAGE_AUTHOR'            => get_username_string('username', $author_id, $user_info['username'], $user_info['user_colour'], $user_info['username']),
222        'U_MESSAGE_AUTHOR'            => get_username_string('profile', $author_id, $user_info['username'], $user_info['user_colour'], $user_info['username']),
223
224        'RANK_TITLE'        => $user_info['rank_title'],
225        'RANK_IMG'            => $user_info['rank_image'],
226        'AUTHOR_JOINED'        => $user->format_date($user_info['user_regdate']),
227        'AUTHOR_POSTS'        => (int) $user_info['user_posts'],
228        'U_AUTHOR_POSTS'    => ($config['load_search'] && $auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$author_id&amp;sr=posts") : '',
229        'CONTACT_USER'        => $user->lang('CONTACT_USER', get_username_string('username', $author_id, $user_info['username'], $user_info['user_colour'], $user_info['username'])),
230
231        'ONLINE_IMG'        => (!$config['load_onlinetrack']) ? '' : ((isset($user_info['online']) && $user_info['online']) ? $user->img('icon_user_online', $user->lang['ONLINE']) : $user->img('icon_user_offline', $user->lang['OFFLINE'])),
232        'S_ONLINE'            => (!$config['load_onlinetrack']) ? false : ((isset($user_info['online']) && $user_info['online']) ? true : false),
233        'DELETE_IMG'        => $user->img('icon_post_delete', $user->lang['DELETE_MESSAGE']),
234        'INFO_IMG'            => $user->img('icon_post_info', $user->lang['VIEW_PM_INFO']),
235        'PROFILE_IMG'        => $user->img('icon_user_profile', $user->lang['READ_PROFILE']),
236        'EMAIL_IMG'            => $user->img('icon_contact_email', $user->lang['SEND_EMAIL']),
237        'QUOTE_IMG'            => $user->img('icon_post_quote', $user->lang['POST_QUOTE_PM']),
238        'REPLY_IMG'            => $user->img('button_pm_reply', $user->lang['POST_REPLY_PM']),
239        'REPORT_IMG'        => $user->img('icon_post_report', 'REPORT_PM'),
240        'EDIT_IMG'            => $user->img('icon_post_edit', $user->lang['POST_EDIT_PM']),
241        'MINI_POST_IMG'        => $user->img('icon_post_target', $user->lang['PM']),
242
243        'SENT_DATE'            => ($view == 'print') ? $user->format_date($message_row['message_time'], false, true) : $user->format_date($message_row['message_time']),
244        'SUBJECT'            => $message_row['message_subject'],
245        'MESSAGE'            => $message,
246        'SIGNATURE'            => ($message_row['enable_sig']) ? $signature : '',
247        'EDITED_MESSAGE'    => $l_edited_by,
248        'MESSAGE_ID'        => $message_row['msg_id'],
249
250        'U_PM'            =>  $u_pm,
251        'U_JABBER'        =>  $u_jabber,
252
253        'U_DELETE'            => ($auth->acl_get('u_pm_delete')) ? "$url&amp;mode=compose&amp;action=delete&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] : '',
254        'U_EMAIL'            => $user_info['email'],
255        'U_REPORT'            => ($config['allow_pm_report']) ? $phpbb_container->get('controller.helper')->route('phpbb_report_pm_controller', array('id' => $message_row['msg_id'])) : '',
256        'U_QUOTE'            => ($auth->acl_get('u_sendpm') && $author_id != ANONYMOUS) ? "$url&amp;mode=compose&amp;action=quote&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] : '',
257        'U_EDIT'            => $can_edit_pm ? "$url&amp;mode=compose&amp;action=edit&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] : '',
258        'U_POST_REPLY_PM'    => ($auth->acl_get('u_sendpm') && $author_id != ANONYMOUS) ? "$url&amp;mode=compose&amp;action=reply&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] : '',
259        'U_POST_REPLY_ALL'    => ($auth->acl_get('u_sendpm') && $author_id != ANONYMOUS) ? "$url&amp;mode=compose&amp;action=reply&amp;f=$folder_id&amp;reply_to_all=1&amp;p=" . $message_row['msg_id'] : '',
260        'U_PREVIOUS_PM'        => "$url&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] . "&amp;view=previous",
261        'U_NEXT_PM'            => "$url&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] . "&amp;view=next",
262
263        'U_PM_ACTION'        => $url . '&amp;mode=compose&amp;f=' . $folder_id . '&amp;p=' . $message_row['msg_id'],
264
265        'S_HAS_ATTACHMENTS'    => (count($attachments)) ? true : false,
266        'S_DISPLAY_NOTICE'    => $display_notice && $message_row['message_attachment'],
267        'S_AUTHOR_DELETED'    => ($author_id == ANONYMOUS) ? true : false,
268        'S_SPECIAL_FOLDER'    => in_array($folder_id, array(PRIVMSGS_NO_BOX, PRIVMSGS_OUTBOX)),
269        'S_PM_RECIPIENTS'    => $num_recipients,
270        'S_BBCODE_ALLOWED'    => ($bbcode_status) ? 1 : 0,
271        'S_CUSTOM_FIELDS'    => (!empty($cp_row['row'])) ? true : false,
272
273        'U_PRINT_PM'        => ($config['print_pm'] && $auth->acl_get('u_pm_printpm')) ? "$url&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] . "&amp;view=print" : '',
274        'U_FORWARD_PM'        => ($config['forward_pm'] && $auth->acl_get('u_sendpm') && $auth->acl_get('u_pm_forward')) ? "$url&amp;mode=compose&amp;action=forward&amp;f=$folder_id&amp;p=" . $message_row['msg_id'] : '',
275    );
276
277    if (!empty($user_info['avatar']))
278    {
279        /** @var \phpbb\avatar\helper $avatar_helper */
280        $avatar_helper = $phpbb_container->get('avatar.helper');
281
282        $avatar_data = $avatar_helper->get_template_vars($user_info['avatar'], 'AUTHOR_');
283        $msg_data = array_merge($msg_data, $avatar_data);
284    }
285
286    /**
287    * Modify pm and sender data before it is assigned to the template
288    *
289    * @event core.ucp_pm_view_messsage
290    * @var    mixed    id            Active module category (can be int or string)
291    * @var    string    mode        Active module
292    * @var    int        folder_id    ID of the folder the message is in
293    * @var    int        msg_id        ID of the private message
294    * @var    array    folder        Array with data of user's message folders
295    * @var    array    message_row    Array with message data
296    * @var    array    cp_row        Array with senders custom profile field data
297    * @var    array    msg_data    Template array with message data
298    * @var     array    user_info    User data of the sender
299    * @since 3.1.0-a1
300    * @changed 3.1.6-RC1        Added user_info into event
301    * @changed 3.2.2-RC1        Deprecated
302    * @deprecated 4.0.0            Event name is misspelled and is replaced with new event with correct name
303    */
304    $vars = array(
305        'id',
306        'mode',
307        'folder_id',
308        'msg_id',
309        'folder',
310        'message_row',
311        'cp_row',
312        'msg_data',
313        'user_info',
314    );
315    extract($phpbb_dispatcher->trigger_event('core.ucp_pm_view_messsage', compact($vars)));
316
317    /**
318     * Modify pm and sender data before it is assigned to the template
319     *
320     * @event core.ucp_pm_view_message
321     * @var    mixed    id            Active module category (can be int or string)
322     * @var    string    mode        Active module
323     * @var    int        folder_id    ID of the folder the message is in
324     * @var    int        msg_id        ID of the private message
325     * @var    array    folder        Array with data of user's message folders
326     * @var    array    message_row    Array with message data
327     * @var    array    cp_row        Array with senders custom profile field data
328     * @var    array    msg_data    Template array with message data
329     * @var array    user_info    User data of the sender
330     * @var array    attachments    Attachments data
331     * @since 3.2.2-RC1
332     * @changed 3.2.5-RC1 Added attachments
333     */
334    $vars = array(
335        'id',
336        'mode',
337        'folder_id',
338        'msg_id',
339        'folder',
340        'message_row',
341        'cp_row',
342        'msg_data',
343        'user_info',
344        'attachments',
345    );
346    extract($phpbb_dispatcher->trigger_event('core.ucp_pm_view_message', compact($vars)));
347
348    $template->assign_vars($msg_data);
349
350    $contact_fields = array(
351        array(
352            'ID'        => 'pm',
353            'NAME'        => $user->lang['SEND_PRIVATE_MESSAGE'],
354            'U_CONTACT' => $u_pm,
355        ),
356        array(
357            'ID'        => 'email',
358            'NAME'        => $user->lang['SEND_EMAIL'],
359            'U_CONTACT'    => $user_info['email'],
360        ),
361        array(
362            'ID'        => 'jabber',
363            'NAME'        => $user->lang['JABBER'],
364            'U_CONTACT'    => $u_jabber,
365        ),
366    );
367
368    foreach ($contact_fields as $field)
369    {
370        if ($field['U_CONTACT'])
371        {
372            $template->assign_block_vars('contact', $field);
373        }
374    }
375
376    // Display the custom profile fields
377    if (!empty($cp_row['row']))
378    {
379        $template->assign_vars($cp_row['row']);
380
381        foreach ($cp_row['blockrow'] as $cp_block_row)
382        {
383            $template->assign_block_vars('custom_fields', $cp_block_row);
384
385            if ($cp_block_row['S_PROFILE_CONTACT'])
386            {
387                $template->assign_block_vars('contact', array(
388                    'ID'        => $cp_block_row['PROFILE_FIELD_IDENT'],
389                    'NAME'        => $cp_block_row['PROFILE_FIELD_NAME'],
390                    'U_CONTACT'    => $cp_block_row['PROFILE_FIELD_CONTACT'],
391                ));
392            }
393        }
394    }
395
396    // Display not already displayed Attachments for this post, we already parsed them. ;)
397    if (isset($attachments) && count($attachments))
398    {
399        foreach ($attachments as $attachment)
400        {
401            $template->assign_block_vars('attachment', array(
402                'DISPLAY_ATTACHMENT'    => $attachment)
403            );
404        }
405    }
406
407    if (!isset($_REQUEST['view']) || $request->variable('view', '') != 'print')
408    {
409        // Message History
410        if (message_history($msg_id, $user->data['user_id'], $message_row, $folder))
411        {
412            $template->assign_var('S_DISPLAY_HISTORY', true);
413        }
414    }
415}
416
417/**
418* Get user information (only for message display)
419*/
420function get_user_information($user_id, $user_row)
421{
422    global $db, $auth, $user, $phpbb_container;
423    global $phpbb_root_path, $phpEx, $config;
424
425    if (!$user_id)
426    {
427        return array();
428    }
429
430    if (empty($user_row))
431    {
432        $sql = 'SELECT *
433            FROM ' . USERS_TABLE . '
434            WHERE user_id = ' . (int) $user_id;
435        $result = $db->sql_query($sql);
436        $user_row = $db->sql_fetchrow($result);
437        $db->sql_freeresult($result);
438    }
439
440    // Some standard values
441    $user_row['online'] = false;
442    $user_row['rank_title'] = $user_row['rank_image'] = $user_row['rank_image_src'] = $user_row['email'] = '';
443
444    // Generate online information for user
445    if ($config['load_onlinetrack'])
446    {
447        $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
448            FROM ' . SESSIONS_TABLE . "
449            WHERE session_user_id = $user_id
450            GROUP BY session_user_id";
451        $result = $db->sql_query_limit($sql, 1);
452        $row = $db->sql_fetchrow($result);
453        $db->sql_freeresult($result);
454
455        $update_time = $config['load_online_time'] * 60;
456        if ($row)
457        {
458            $user_row['online'] = (time() - $update_time < $row['online_time'] && ($row['viewonline'] || $auth->acl_get('u_viewonline'))) ? true : false;
459        }
460    }
461
462    /** @var \phpbb\avatar\helper $avatar_helper */
463    $avatar_helper = $phpbb_container->get('avatar.helper');
464
465    $user_row['avatar'] = ($user->optionget('viewavatars')) ? $avatar_helper->get_user_avatar($user_row) : [];
466
467    if (!function_exists('phpbb_get_user_rank'))
468    {
469        include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
470    }
471
472    $user_rank_data = phpbb_get_user_rank($user_row, $user_row['user_posts']);
473    $user_row['rank_title'] = $user_rank_data['title'];
474    $user_row['rank_image'] = $user_rank_data['img'];
475    $user_row['rank_image_src'] = $user_rank_data['img_src'];
476
477    if ((!empty($user_row['user_allow_viewemail']) && $auth->acl_get('u_sendemail')) || $auth->acl_get('a_email'))
478    {
479        $user_row['email'] = ($config['board_email_form'] && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;u=$user_id") : ((($config['board_hide_emails'] && !$auth->acl_get('a_email')) || empty($user_row['user_email'])) ? '' : 'mailto:' . $user_row['user_email']);
480    }
481
482    return $user_row;
483}