Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 758
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
mcp_queue
0.00% covered (danger)
0.00%
0 / 756
0.00% covered (danger)
0.00%
0 / 5
43890
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 main
0.00% covered (danger)
0.00%
0 / 342
0.00% covered (danger)
0.00%
0 / 1
8556
 approve_posts
0.00% covered (danger)
0.00%
0 / 124
0.00% covered (danger)
0.00%
0 / 1
1332
 approve_topics
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 1
462
 disapprove_posts
0.00% covered (danger)
0.00%
0 / 187
0.00% covered (danger)
0.00%
0 / 1
3540
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* mcp_queue
24* Handling the moderation queue
25*/
26class mcp_queue
27{
28    var $p_master;
29    var $u_action;
30
31    public function __construct($p_master)
32    {
33        $this->p_master = $p_master;
34    }
35
36    public function main($id, $mode)
37    {
38        global $auth, $db, $user, $template, $request;
39        global $config, $phpbb_root_path, $phpEx, $action, $phpbb_container;
40        global $phpbb_dispatcher;
41
42        include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
43
44        $forum_id = $request->variable('f', 0);
45        $start = $request->variable('start', 0);
46
47        $this->page_title = 'MCP_QUEUE';
48
49        switch ($action)
50        {
51            case 'approve':
52            case 'restore':
53                include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
54
55                $post_id_list = $request->variable('post_id_list', array(0));
56                $topic_id_list = $request->variable('topic_id_list', array(0));
57
58                if (!empty($post_id_list))
59                {
60                    self::approve_posts($action, $post_id_list, 'queue', $mode);
61                }
62                else if (!empty($topic_id_list))
63                {
64                    self::approve_topics($action, $topic_id_list, 'queue', $mode);
65                }
66                else
67                {
68                    trigger_error('NO_POST_SELECTED');
69                }
70            break;
71
72            case 'delete':
73                $post_id_list = $request->variable('post_id_list', array(0));
74                $topic_id_list = $request->variable('topic_id_list', array(0));
75                $delete_reason = $request->variable('delete_reason', '', true);
76
77                if (!empty($post_id_list))
78                {
79                    if (!function_exists('mcp_delete_post'))
80                    {
81                        global $phpbb_root_path, $phpEx;
82                        include($phpbb_root_path . 'includes/mcp/mcp_main.' . $phpEx);
83                    }
84                    mcp_delete_post($post_id_list, false, $delete_reason, $action);
85                }
86                else if (!empty($topic_id_list))
87                {
88                    if (!function_exists('mcp_delete_topic'))
89                    {
90                        global $phpbb_root_path, $phpEx;
91                        include($phpbb_root_path . 'includes/mcp/mcp_main.' . $phpEx);
92                    }
93                    mcp_delete_topic($topic_id_list, false, $delete_reason, $action);
94                }
95                else
96                {
97                    trigger_error('NO_POST_SELECTED');
98                }
99            break;
100
101            case 'disapprove':
102                $post_id_list = $request->variable('post_id_list', array(0));
103                $topic_id_list = $request->variable('topic_id_list', array(0));
104
105                if (!empty($topic_id_list) && $mode == 'deleted_topics')
106                {
107                    if (!function_exists('mcp_delete_topic'))
108                    {
109                        global $phpbb_root_path, $phpEx;
110                        include($phpbb_root_path . 'includes/mcp/mcp_main.' . $phpEx);
111                    }
112                    mcp_delete_topic($topic_id_list, false, '', 'disapprove');
113                    return;
114                }
115
116                if (!class_exists('messenger'))
117                {
118                    include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
119                }
120
121                if (!empty($topic_id_list))
122                {
123                    $post_visibility = ($mode == 'deleted_topics') ? ITEM_DELETED : array(ITEM_UNAPPROVED, ITEM_REAPPROVE);
124                    $sql = 'SELECT post_id
125                        FROM ' . POSTS_TABLE . '
126                        WHERE ' . $db->sql_in_set('post_visibility', $post_visibility) . '
127                            AND ' . $db->sql_in_set('topic_id', $topic_id_list);
128                    $result = $db->sql_query($sql);
129
130                    $post_id_list = array();
131                    while ($row = $db->sql_fetchrow($result))
132                    {
133                        $post_id_list[] = (int) $row['post_id'];
134                    }
135                    $db->sql_freeresult($result);
136                }
137
138                if (!empty($post_id_list))
139                {
140                    self::disapprove_posts($post_id_list, 'queue', $mode);
141                }
142                else
143                {
144                    trigger_error('NO_POST_SELECTED');
145                }
146            break;
147        }
148
149        switch ($mode)
150        {
151            case 'approve_details':
152
153                $this->tpl_name = 'mcp_post';
154
155                $user->add_lang(array('posting', 'viewtopic'));
156
157                $post_id = $request->variable('p', 0);
158                $topic_id = $request->variable('t', 0);
159                $topic_info = [];
160
161                /* @var $phpbb_notifications \phpbb\notification\manager */
162                $phpbb_notifications = $phpbb_container->get('notification_manager');
163
164                if ($topic_id)
165                {
166                    $topic_info = phpbb_get_topic_data(array($topic_id), 'm_approve');
167                    if (isset($topic_info[$topic_id]['topic_first_post_id']))
168                    {
169                        $post_id = (int) $topic_info[$topic_id]['topic_first_post_id'];
170
171                        $phpbb_notifications->mark_notifications('topic_in_queue', $topic_id, $user->data['user_id']);
172                    }
173                    else
174                    {
175                        $topic_id = 0;
176                    }
177                }
178
179                $phpbb_notifications->mark_notifications('post_in_queue', $post_id, $user->data['user_id']);
180
181                $post_info = phpbb_get_post_data(array($post_id), 'm_approve', true);
182
183                if (!count($post_info))
184                {
185                    trigger_error('NO_POST_SELECTED');
186                }
187
188                $post_info = $post_info[$post_id];
189
190                if ($post_info['topic_first_post_id'] != $post_id && topic_review($post_info['topic_id'], $post_info['forum_id'], 'topic_review', 0, false))
191                {
192                    $template->assign_vars(array(
193                        'S_TOPIC_REVIEW'    => true,
194                        'S_BBCODE_ALLOWED'    => $post_info['enable_bbcode'],
195                        'TOPIC_TITLE'        => $post_info['topic_title'],
196                    ));
197                }
198
199                $attachments = $topic_tracking_info = array();
200
201                // Get topic tracking info
202                if ($config['load_db_lastread'])
203                {
204                    $tmp_topic_data = array($post_info['topic_id'] => $post_info);
205                    $topic_tracking_info = get_topic_tracking($post_info['forum_id'], $post_info['topic_id'], $tmp_topic_data, array($post_info['forum_id'] => $post_info['forum_mark_time']));
206                    unset($tmp_topic_data);
207                }
208                else
209                {
210                    $topic_tracking_info = get_complete_topic_tracking($post_info['forum_id'], $post_info['topic_id']);
211                }
212
213                $post_unread = (isset($topic_tracking_info[$post_info['topic_id']]) && $post_info['post_time'] > $topic_tracking_info[$post_info['topic_id']]) ? true : false;
214
215                // Process message, leave it uncensored
216                $parse_flags = ($post_info['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
217                $message = generate_text_for_display($post_info['post_text'], $post_info['bbcode_uid'], $post_info['bbcode_bitfield'], $parse_flags, false);
218
219                if ($post_info['post_attachment'] && $auth->acl_get('u_download') && $auth->acl_get('f_download', $post_info['forum_id']))
220                {
221                    $sql = 'SELECT *
222                        FROM ' . ATTACHMENTS_TABLE . '
223                        WHERE post_msg_id = ' . $post_id . '
224                            AND in_message = 0
225                        ORDER BY filetime DESC, post_msg_id ASC';
226                    $result = $db->sql_query($sql);
227
228                    while ($row = $db->sql_fetchrow($result))
229                    {
230                        $attachments[] = $row;
231                    }
232                    $db->sql_freeresult($result);
233
234                    if (count($attachments))
235                    {
236                        $update_count = array();
237                        parse_attachments($post_info['forum_id'], $message, $attachments, $update_count);
238                    }
239
240                    // Display not already displayed Attachments for this post, we already parsed them. ;)
241                    if (!empty($attachments))
242                    {
243                        $template->assign_var('S_HAS_ATTACHMENTS', true);
244
245                        foreach ($attachments as $attachment)
246                        {
247                            $template->assign_block_vars('attachment', array(
248                                'DISPLAY_ATTACHMENT'    => $attachment,
249                            ));
250                        }
251                    }
252                }
253
254                // Deleting information
255                if ($post_info['post_visibility'] == ITEM_DELETED && $post_info['post_delete_user'])
256                {
257                    // User having deleted the post also being the post author?
258                    if (!$post_info['post_delete_user'] || $post_info['post_delete_user'] == $post_info['poster_id'])
259                    {
260                        $display_username = get_username_string('full', $post_info['poster_id'], $post_info['username'], $post_info['user_colour'], $post_info['post_username']);
261                    }
262                    else
263                    {
264                        $sql = 'SELECT u.user_id, u.username, u.user_colour
265                            FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
266                            WHERE p.post_id =  ' . $post_info['post_id'] . '
267                                AND p.post_delete_user = u.user_id';
268                        $result = $db->sql_query($sql);
269                        $post_delete_userinfo = $db->sql_fetchrow($result);
270                        $db->sql_freeresult($result);
271                        $display_username = get_username_string('full', $post_info['post_delete_user'], $post_delete_userinfo['username'], $post_delete_userinfo['user_colour']);
272                    }
273
274                    $l_deleted_by = $user->lang('DELETED_INFORMATION', $display_username, $user->format_date($post_info['post_delete_time'], false, true));
275                }
276                else
277                {
278                    $l_deleted_by = '';
279                }
280
281                $post_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $post_info['post_id'] . '#p' . $post_info['post_id']);
282                $topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $post_info['topic_id']);
283
284                $post_data = array(
285                    'S_MCP_QUEUE'            => true,
286                    'U_APPROVE_ACTION'        => append_sid("{$phpbb_root_path}mcp.$phpEx", "i=queue&amp;p=$post_id"),
287                    'S_CAN_DELETE_POST'        => $auth->acl_get('m_delete', $post_info['forum_id']),
288                    'S_CAN_VIEWIP'            => $auth->acl_get('m_info', $post_info['forum_id']),
289                    'S_POST_REPORTED'        => $post_info['post_reported'],
290                    'S_POST_UNAPPROVED'        => $post_info['post_visibility'] == ITEM_UNAPPROVED || $post_info['post_visibility'] == ITEM_REAPPROVE,
291                    'S_POST_LOCKED'            => $post_info['post_edit_locked'],
292                    'S_USER_NOTES'            => true,
293                    'S_POST_DELETED'        => ($post_info['post_visibility'] == ITEM_DELETED),
294                    'DELETED_MESSAGE'        => $l_deleted_by,
295                    'DELETE_REASON'            => $post_info['post_delete_reason'],
296
297                    'U_EDIT'                => ($auth->acl_get('m_edit', $post_info['forum_id'])) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=edit&amp;p={$post_info['post_id']}") : '',
298                    'U_MCP_APPROVE'            => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;p=' . $post_id),
299                    'U_MCP_REPORT'            => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=report_details&amp;p=' . $post_id),
300                    'U_MCP_USER_NOTES'        => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&amp;mode=user_notes&amp;u=' . $post_info['user_id']),
301                    'U_MCP_WARN_USER'        => ($auth->acl_get('m_warn')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=warn&amp;mode=warn_user&amp;u=' . $post_info['user_id']) : '',
302                    'U_VIEW_POST'            => $post_url,
303                    'U_VIEW_TOPIC'            => $topic_url,
304
305                    'MINI_POST_IMG'            => ($post_unread) ? $user->img('icon_post_target_unread', 'UNREAD_POST') : $user->img('icon_post_target', 'POST'),
306
307                    'RETURN_QUEUE'            => sprintf($user->lang['RETURN_QUEUE'], '<a href="' . append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue' . (($topic_id) ? '&amp;mode=unapproved_topics' : '&amp;mode=unapproved_posts')) . '&amp;start=' . $start . '">', '</a>'),
308                    'RETURN_POST'            => sprintf($user->lang['RETURN_POST'], '<a href="' . $post_url . '">', '</a>'),
309                    'RETURN_TOPIC_SIMPLE'    => sprintf($user->lang['RETURN_TOPIC_SIMPLE'], '<a href="' . $topic_url . '">', '</a>'),
310                    'REPORTED_IMG'            => $user->img('icon_topic_reported', $user->lang['POST_REPORTED']),
311                    'UNAPPROVED_IMG'        => $user->img('icon_topic_unapproved', $user->lang['POST_UNAPPROVED']),
312                    'EDIT_IMG'                => $user->img('icon_post_edit', $user->lang['EDIT_POST']),
313
314                    'POST_AUTHOR_FULL'        => get_username_string('full', $post_info['user_id'], $post_info['username'], $post_info['user_colour'], $post_info['post_username']),
315                    'POST_AUTHOR_COLOUR'    => get_username_string('colour', $post_info['user_id'], $post_info['username'], $post_info['user_colour'], $post_info['post_username']),
316                    'POST_AUTHOR'            => get_username_string('username', $post_info['user_id'], $post_info['username'], $post_info['user_colour'], $post_info['post_username']),
317                    'U_POST_AUTHOR'            => get_username_string('profile', $post_info['user_id'], $post_info['username'], $post_info['user_colour'], $post_info['post_username']),
318
319                    'POST_PREVIEW'            => $message,
320                    'POST_SUBJECT'            => $post_info['post_subject'],
321                    'POST_DATE'                => $user->format_date($post_info['post_time']),
322                    'POST_IP'                => $post_info['poster_ip'],
323                    'POST_IPADDR'            => ($auth->acl_get('m_info', $post_info['forum_id']) && $request->variable('lookup', '')) ? @gethostbyaddr($post_info['poster_ip']) : '',
324                    'POST_ID'                => $post_info['post_id'],
325                    'S_FIRST_POST'            => ($post_info['topic_first_post_id'] == $post_id),
326
327                    'U_LOOKUP_IP'            => ($auth->acl_get('m_info', $post_info['forum_id'])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;p=' . $post_id . '&amp;lookup=' . $post_info['poster_ip']) . '#ip' : '',
328                );
329
330                /**
331                * Alter post awaiting approval template before it is rendered
332                *
333                * @event core.mcp_queue_approve_details_template
334                * @var    int        post_id        Post ID
335                * @var    int        topic_id    Topic ID
336                * @var    array    topic_info    Topic data
337                * @var    array    post_info    Post data
338                * @var array    post_data    Post template data
339                * @var    string    message        Post message
340                * @var    string    post_url    Post URL
341                * @var    string    topic_url    Topic URL
342                * @since 3.2.2-RC1
343                */
344                $vars = array(
345                    'post_id',
346                    'topic_id',
347                    'topic_info',
348                    'post_info',
349                    'post_data',
350                    'message',
351                    'post_url',
352                    'topic_url',
353                );
354                extract($phpbb_dispatcher->trigger_event('core.mcp_queue_approve_details_template', compact($vars)));
355
356                $template->assign_vars($post_data);
357
358            break;
359
360            case 'unapproved_topics':
361            case 'unapproved_posts':
362            case 'deleted_topics':
363            case 'deleted_posts':
364                $m_perm = 'm_approve';
365                $is_topics = ($mode == 'unapproved_topics' || $mode == 'deleted_topics') ? true : false;
366                $is_restore = ($mode == 'deleted_posts' || $mode == 'deleted_topics') ? true : false;
367                $visibility_const = (!$is_restore) ? array(ITEM_UNAPPROVED, ITEM_REAPPROVE) : ITEM_DELETED;
368
369                $user->add_lang(array('viewtopic', 'viewforum'));
370
371                $topic_id = $request->variable('t', 0);
372
373                // If 'sort' is set, "Go" was pressed which is located behind the forums <select> box
374                // Then, unset the topic id so it does not override the forum id
375                $topic_id = $request->is_set_post('sort') ? 0 : $topic_id;
376
377                if ($topic_id)
378                {
379                    $topic_info = phpbb_get_topic_data(array($topic_id));
380
381                    if (!count($topic_info))
382                    {
383                        trigger_error('TOPIC_NOT_EXIST');
384                    }
385
386                    $topic_info = $topic_info[$topic_id];
387                    $forum_id = $topic_info['forum_id'];
388                }
389
390                $forum_list_approve = get_forum_list($m_perm, false, true);
391                $forum_list_read = array_flip(get_forum_list('f_read', true, true)); // Flipped so we can isset() the forum IDs
392
393                // Remove forums we cannot read
394                foreach ($forum_list_approve as $k => $forum_data)
395                {
396                    if (!isset($forum_list_read[$forum_data['forum_id']]))
397                    {
398                        unset($forum_list_approve[$k]);
399                    }
400                }
401                unset($forum_list_read);
402
403                if (!$forum_id)
404                {
405                    $forum_list = array();
406                    foreach ($forum_list_approve as $row)
407                    {
408                        $forum_list[] = $row['forum_id'];
409                    }
410
411                    if (!count($forum_list))
412                    {
413                        trigger_error('NOT_MODERATOR');
414                    }
415                }
416                else
417                {
418                    $forum_info = phpbb_get_forum_data(array($forum_id), $m_perm);
419
420                    if (!count($forum_info))
421                    {
422                        trigger_error('NOT_MODERATOR');
423                    }
424
425                    $forum_list = $forum_id;
426                }
427
428                $forum_options = '<option value="0"' . (($forum_id == 0) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_FORUMS'] . '</option>';
429                foreach ($forum_list_approve as $row)
430                {
431                    $forum_options .= '<option value="' . $row['forum_id'] . '"' . (($forum_id == $row['forum_id']) ? ' selected="selected"' : '') . '>' . str_repeat('&nbsp; &nbsp;', $row['padding']) . truncate_string($row['forum_name'], 30, 255, false, $user->lang['ELLIPSIS']) . '</option>';
432                }
433
434                $sort_days = $total = 0;
435                $sort_key = $sort_dir = '';
436                $sort_by_sql = $sort_order_sql = array();
437                phpbb_mcp_sorting($mode, $sort_days, $sort_key, $sort_dir, $sort_by_sql, $sort_order_sql, $total, $forum_id, $topic_id);
438
439                $limit_time_sql = ($sort_days) ? 'AND t.topic_last_post_time >= ' . (time() - ($sort_days * 86400)) : '';
440
441                $forum_names = array();
442
443                if (!$is_topics)
444                {
445                    $sql = 'SELECT p.post_id
446                        FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t' . (($sort_order_sql[0] == 'u') ? ', ' . USERS_TABLE . ' u' : '') . '
447                        WHERE ' . $db->sql_in_set('p.forum_id', $forum_list) . '
448                            AND ' . $db->sql_in_set('p.post_visibility', $visibility_const) . '
449                            ' . (($sort_order_sql[0] == 'u') ? 'AND u.user_id = p.poster_id' : '') . '
450                            ' . (($topic_id) ? 'AND p.topic_id = ' . $topic_id : '') . "
451                            AND t.topic_id = p.topic_id
452                            AND (t.topic_visibility <> p.post_visibility
453                                OR t.topic_delete_user = 0)
454                            $limit_time_sql
455                        ORDER BY $sort_order_sql";
456
457                    /**
458                    * Alter sql query to get posts in queue to be accepted
459                    *
460                    * @event core.mcp_queue_get_posts_query_before
461                    * @var    string    sql                        Associative array with the query to be executed
462                    * @var    array    forum_list                List of forums that contain the posts
463                    * @var    int        visibility_const        Integer with one of the possible ITEM_* constant values
464                    * @var    int        topic_id                If topic_id not equal to 0, the topic id to filter the posts to display
465                    * @var    string    limit_time_sql            String with the SQL code to limit the time interval of the post (Note: May be empty string)
466                    * @var    string    sort_order_sql            String with the ORDER BY SQL code used in this query
467                    * @since 3.1.0-RC3
468                    */
469                    $vars = array(
470                        'sql',
471                        'forum_list',
472                        'visibility_const',
473                        'topic_id',
474                        'limit_time_sql',
475                        'sort_order_sql',
476                    );
477                    extract($phpbb_dispatcher->trigger_event('core.mcp_queue_get_posts_query_before', compact($vars)));
478
479                    $result = $db->sql_query_limit($sql, $config['topics_per_page'], $start);
480
481                    $post_ids = array();
482                    while ($row = $db->sql_fetchrow($result))
483                    {
484                        $post_ids[] = $row['post_id'];
485                    }
486                    $db->sql_freeresult($result);
487
488                    if (count($post_ids))
489                    {
490                        $sql = 'SELECT t.topic_id, t.topic_title, t.forum_id, p.post_id, p.post_subject, p.post_username, p.poster_id, p.post_time, p.post_attachment, u.username, u.username_clean, u.user_colour
491                            FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . ' u
492                            WHERE ' . $db->sql_in_set('p.post_id', $post_ids) . '
493                                AND t.topic_id = p.topic_id
494                                AND u.user_id = p.poster_id
495                            ORDER BY ' . $sort_order_sql;
496
497                        /**
498                        * Alter sql query to get information on all posts in queue
499                        *
500                        * @event core.mcp_queue_get_posts_for_posts_query_before
501                        * @var    string    sql                        String with the query to be executed
502                        * @var    array    forum_list                List of forums that contain the posts
503                        * @var    int        visibility_const        Integer with one of the possible ITEM_* constant values
504                        * @var    int        topic_id                topic_id in the page request
505                        * @var    string    limit_time_sql            String with the SQL code to limit the time interval of the post (Note: May be empty string)
506                        * @var    string    sort_order_sql            String with the ORDER BY SQL code used in this query
507                        * @since 3.2.3-RC2
508                        */
509                        $vars = array(
510                            'sql',
511                            'forum_list',
512                            'visibility_const',
513                            'topic_id',
514                            'limit_time_sql',
515                            'sort_order_sql',
516                        );
517                        extract($phpbb_dispatcher->trigger_event('core.mcp_queue_get_posts_for_posts_query_before', compact($vars)));
518
519                        $result = $db->sql_query($sql);
520
521                        $post_data = $rowset = array();
522                        while ($row = $db->sql_fetchrow($result))
523                        {
524                            $forum_names[] = $row['forum_id'];
525                            $post_data[$row['post_id']] = $row;
526                        }
527                        $db->sql_freeresult($result);
528
529                        foreach ($post_ids as $post_id)
530                        {
531                            $rowset[] = $post_data[$post_id];
532                        }
533                        unset($post_data, $post_ids);
534                    }
535                    else
536                    {
537                        $rowset = array();
538                    }
539                }
540                else
541                {
542                    $sql = 'SELECT t.forum_id, t.topic_id, t.topic_title, t.topic_title AS post_subject, t.topic_time AS post_time, t.topic_poster AS poster_id, t.topic_first_post_id AS post_id, t.topic_attachment AS post_attachment, t.topic_first_poster_name AS username, t.topic_first_poster_colour AS user_colour
543                        FROM ' . TOPICS_TABLE . ' t
544                        WHERE ' . $db->sql_in_set('forum_id', $forum_list) . '
545                            AND  ' . $db->sql_in_set('topic_visibility', $visibility_const) . "
546                            AND topic_delete_user <> 0
547                            $limit_time_sql
548                        ORDER BY $sort_order_sql";
549
550                    /**
551                    * Alter sql query to get information on all topics in the list of forums provided.
552                    *
553                    * @event core.mcp_queue_get_posts_for_topics_query_before
554                    * @var    string    sql                        String with the query to be executed
555                    * @var    array    forum_list                List of forums that contain the posts
556                    * @var    int        visibility_const        Integer with one of the possible ITEM_* constant values
557                    * @var    int        topic_id                topic_id in the page request
558                    * @var    string    limit_time_sql            String with the SQL code to limit the time interval of the post (Note: May be empty string)
559                    * @var    string    sort_order_sql            String with the ORDER BY SQL code used in this query
560                    * @since 3.1.0-RC3
561                    */
562                    $vars = array(
563                        'sql',
564                        'forum_list',
565                        'visibility_const',
566                        'topic_id',
567                        'limit_time_sql',
568                        'sort_order_sql',
569                    );
570                    extract($phpbb_dispatcher->trigger_event('core.mcp_queue_get_posts_for_topics_query_before', compact($vars)));
571
572                    $result = $db->sql_query_limit($sql, $config['topics_per_page'], $start);
573
574                    $rowset = array();
575                    while ($row = $db->sql_fetchrow($result))
576                    {
577                        $forum_names[] = $row['forum_id'];
578                        $rowset[] = $row;
579                    }
580                    $db->sql_freeresult($result);
581                }
582
583                if (count($forum_names))
584                {
585                    // Select the names for the forum_ids
586                    $sql = 'SELECT forum_id, forum_name
587                        FROM ' . FORUMS_TABLE . '
588                        WHERE ' . $db->sql_in_set('forum_id', $forum_names);
589                    $result = $db->sql_query($sql, 3600);
590
591                    $forum_names = array();
592                    while ($row = $db->sql_fetchrow($result))
593                    {
594                        $forum_names[$row['forum_id']] = $row['forum_name'];
595                    }
596                    $db->sql_freeresult($result);
597                }
598
599                foreach ($rowset as $row)
600                {
601                    if (empty($row['post_username']))
602                    {
603                        $row['post_username'] = $row['username'] ?: $user->lang['GUEST'];
604                    }
605
606                    $post_row = array(
607                        'U_TOPIC'            => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $row['topic_id']),
608                        'U_VIEWFORUM'        => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']),
609                        'U_VIEWPOST'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . (($mode == 'unapproved_posts') ? '#p' . $row['post_id'] : ''),
610                        'U_VIEW_DETAILS'    => append_sid("{$phpbb_root_path}mcp.$phpEx", "i=queue&amp;start=$start&amp;mode=approve_details&amp;p={$row['post_id']}" . (($mode == 'unapproved_topics') ? "&amp;t={$row['topic_id']}" : '')),
611
612                        'POST_AUTHOR_FULL'        => get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
613                        'POST_AUTHOR_COLOUR'    => get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
614                        'POST_AUTHOR'            => get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
615                        'U_POST_AUTHOR'            => get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
616
617                        'POST_ID'        => $row['post_id'],
618                        'TOPIC_ID'        => $row['topic_id'],
619                        'FORUM_NAME'    => $forum_names[$row['forum_id']],
620                        'POST_SUBJECT'    => ($row['post_subject'] != '') ? $row['post_subject'] : $user->lang['NO_SUBJECT'],
621                        'TOPIC_TITLE'    => $row['topic_title'],
622                        'POST_TIME'        => $user->format_date($row['post_time']),
623                        'S_HAS_ATTACHMENTS'    => $auth->acl_get('u_download') && $auth->acl_get('f_download', $row['forum_id']) && $row['post_attachment'],
624                    );
625
626                    /**
627                    * Alter sql query to get information on all topics in the list of forums provided.
628                    *
629                    * @event core.mcp_queue_get_posts_modify_post_row
630                    * @var    array    post_row    Template variables for current post
631                    * @var    array    row            Post data
632                    * @var    array    forum_names    Forum names
633                    * @since 3.2.3-RC2
634                    */
635                    $vars = array(
636                        'post_row',
637                        'row',
638                        'forum_names',
639                    );
640                    extract($phpbb_dispatcher->trigger_event('core.mcp_queue_get_posts_modify_post_row', compact($vars)));
641
642                    $template->assign_block_vars('postrow', $post_row);
643                }
644                unset($rowset, $forum_names);
645
646                /* @var \phpbb\pagination $pagination */
647                $pagination = $phpbb_container->get('pagination');
648
649                $base_url = $this->u_action . "&amp;f=$forum_id&amp;st=$sort_days&amp;sk=$sort_key&amp;sd=$sort_dir";
650                $pagination->generate_template_pagination($base_url, 'pagination', 'start', $total, $config['topics_per_page'], $start);
651
652                // Now display the page
653                $template->assign_vars(array(
654                    'L_DISPLAY_ITEMS'        => (!$is_topics) ? $user->lang['DISPLAY_POSTS'] : $user->lang['DISPLAY_TOPICS'],
655                    'L_EXPLAIN'                => $user->lang['MCP_QUEUE_' . strtoupper($mode) . '_EXPLAIN'],
656                    'L_TITLE'                => $user->lang['MCP_QUEUE_' . strtoupper($mode)],
657                    'L_ONLY_TOPIC'            => ($topic_id) ? sprintf($user->lang['ONLY_TOPIC'], $topic_info['topic_title']) : '',
658
659                    'S_FORUM_OPTIONS'        => $forum_options,
660                    'S_MCP_ACTION'            => build_url(array('t', 'f', 'sd', 'st', 'sk')),
661                    'S_TOPICS'                => $is_topics,
662                    'S_RESTORE'                => $is_restore,
663
664                    'TOPIC_ID'                => $topic_id,
665                    'TOTAL'                    => $user->lang(((!$is_topics) ? 'VIEW_TOPIC_POSTS' : 'VIEW_FORUM_TOPICS'), (int) $total),
666                ));
667
668                $this->tpl_name = 'mcp_queue';
669            break;
670        }
671    }
672
673    /**
674    * Approve/Restore posts
675    *
676    * @param $action        string    Action we perform on the posts ('approve' or 'restore')
677    * @param $post_id_list    array    IDs of the posts to approve/restore
678    * @param $id            mixed    Category of the current active module
679    * @param $mode            string    Active module
680    * @return void|never
681    */
682    public static function approve_posts($action, $post_id_list, $id, $mode)
683    {
684        global $template, $user, $request, $phpbb_container, $phpbb_dispatcher;
685        global $phpEx, $phpbb_root_path, $phpbb_log;
686
687        if (!phpbb_check_ids($post_id_list, POSTS_TABLE, 'post_id', array('m_approve')))
688        {
689            send_status_line(403, 'Forbidden');
690            trigger_error('NOT_AUTHORISED');
691        }
692
693        $redirect = $request->variable('redirect', build_url(array('quickmod')));
694        $redirect = reapply_sid($redirect);
695        $post_url = '';
696        $approve_log = array();
697        $num_topics = 0;
698
699        $s_hidden_fields = build_hidden_fields(array(
700            'i'                => $id,
701            'mode'            => $mode,
702            'post_id_list'    => $post_id_list,
703            'action'        => $action,
704            'redirect'        => $redirect,
705        ));
706
707        $post_info = phpbb_get_post_data($post_id_list, 'm_approve');
708
709        if (confirm_box(true))
710        {
711            $notify_poster = ($action == 'approve' && isset($_REQUEST['notify_poster']));
712
713            $topic_info = array();
714
715            // Group the posts by topic_id
716            foreach ($post_info as $post_id => $post_data)
717            {
718                if ($post_data['post_visibility'] == ITEM_APPROVED)
719                {
720                    continue;
721                }
722                $topic_id = (int) $post_data['topic_id'];
723
724                $topic_info[$topic_id]['posts'][] = (int) $post_id;
725                $topic_info[$topic_id]['forum_id'] = (int) $post_data['forum_id'];
726
727                // Refresh the first post, if the time or id is older then the current one
728                if ($post_id <= $post_data['topic_first_post_id'] || $post_data['post_time'] <= $post_data['topic_time'])
729                {
730                    $topic_info[$topic_id]['first_post'] = true;
731                }
732
733                // Refresh the last post, if the time or id is newer then the current one
734                if ($post_id >= $post_data['topic_last_post_id'] || $post_data['post_time'] >= $post_data['topic_last_post_time'])
735                {
736                    $topic_info[$topic_id]['last_post'] = true;
737                }
738
739                $post_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p={$post_data['post_id']}") . '#p' . $post_data['post_id'];
740
741                $approve_log[] = array(
742                    'forum_id'        => $post_data['forum_id'],
743                    'topic_id'        => $post_data['topic_id'],
744                    'post_id'        => $post_id,
745                    'post_subject'    => $post_data['post_subject'],
746                );
747            }
748
749            /* @var $phpbb_content_visibility \phpbb\content_visibility */
750            $phpbb_content_visibility = $phpbb_container->get('content.visibility');
751            foreach ($topic_info as $topic_id => $topic_data)
752            {
753                $phpbb_content_visibility->set_post_visibility(ITEM_APPROVED, $topic_data['posts'], $topic_id, $topic_data['forum_id'], $user->data['user_id'], time(), '', isset($topic_data['first_post']), isset($topic_data['last_post']));
754            }
755
756            foreach ($approve_log as $log_data)
757            {
758                $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_POST_' . strtoupper($action) . 'D', false, array(
759                    'forum_id' => $log_data['forum_id'],
760                    'topic_id' => $log_data['topic_id'],
761                    'post_id'  => $log_data['post_id'],
762                    $log_data['post_subject']
763                ));
764            }
765
766            // Only send out the mails, when the posts are being approved
767            if ($action == 'approve')
768            {
769                /* @var $phpbb_notifications \phpbb\notification\manager */
770                $phpbb_notifications = $phpbb_container->get('notification_manager');
771
772                // Handle notifications
773                foreach ($post_info as $post_id => $post_data)
774                {
775                    // A single topic approval may also happen here, so handle deleting the respective notification.
776                    if (!$post_data['topic_posts_approved'])
777                    {
778                        $phpbb_notifications->delete_notifications('notification.type.topic_in_queue', $post_data['topic_id']);
779
780                        if ($post_data['post_visibility'] == ITEM_UNAPPROVED)
781                        {
782                            $phpbb_notifications->add_notifications(array('notification.type.topic'), $post_data);
783                        }
784                        if ($post_data['post_visibility'] != ITEM_APPROVED)
785                        {
786                            $num_topics++;
787                        }
788                    }
789                    else
790                    {
791                        // Only add notifications, if we are not reapproving post
792                        // When the topic was already approved, but was edited and
793                        // now needs re-approval, we don't want to notify the users
794                        // again.
795                        if ($post_data['post_visibility'] == ITEM_UNAPPROVED)
796                        {
797                            $phpbb_notifications->add_notifications(array(
798                                'notification.type.bookmark',
799                                'notification.type.post',
800                            ), $post_data);
801                        }
802                    }
803                    $phpbb_notifications->add_notifications(array(
804                        'notification.type.mention',
805                        'notification.type.quote',
806                    ), $post_data);
807                    $phpbb_notifications->delete_notifications('notification.type.post_in_queue', $post_id);
808
809                    $phpbb_notifications->mark_notifications(array(
810                        'notification.type.mention',
811                        'notification.type.quote',
812                        'notification.type.bookmark',
813                        'notification.type.post',
814                    ), $post_data['post_id'], $user->data['user_id']);
815
816                    // Notify Poster?
817                    if ($notify_poster)
818                    {
819                        if ($post_data['poster_id'] == ANONYMOUS)
820                        {
821                            continue;
822                        }
823
824                        if (!$post_data['topic_posts_approved'])
825                        {
826                            $phpbb_notifications->add_notifications('notification.type.approve_topic', $post_data);
827                        }
828                        else
829                        {
830                            $phpbb_notifications->add_notifications('notification.type.approve_post', $post_data);
831                        }
832                    }
833                }
834            }
835
836            if ($num_topics >= 1)
837            {
838                $success_msg = ($num_topics == 1) ? 'TOPIC_' . strtoupper($action) . 'D_SUCCESS' : 'TOPICS_' . strtoupper($action) . 'D_SUCCESS';
839            }
840            else
841            {
842                $success_msg = (count($post_info) == 1) ? 'POST_' . strtoupper($action) . 'D_SUCCESS' : 'POSTS_' . strtoupper($action) . 'D_SUCCESS';
843            }
844
845            /**
846             * Perform additional actions during post(s) approval
847             *
848             * @event core.approve_posts_after
849             * @var    string    action                Variable containing the action we perform on the posts ('approve' or 'restore')
850             * @var    array    post_info            Array containing info for all posts being approved
851             * @var    array    topic_info            Array containing info for all parent topics of the posts
852             * @var    int        num_topics            Variable containing number of topics
853             * @var bool    notify_poster        Variable telling if the post should be notified or not
854             * @var    string    success_msg            Variable containing the language key for the success message
855             * @var string    redirect            Variable containing the redirect url
856             * @since 3.1.4-RC1
857             */
858            $vars = array(
859                'action',
860                'post_info',
861                'topic_info',
862                'num_topics',
863                'notify_poster',
864                'success_msg',
865                'redirect',
866            );
867            extract($phpbb_dispatcher->trigger_event('core.approve_posts_after', compact($vars)));
868
869            meta_refresh(3, $redirect);
870            $message = $user->lang[$success_msg];
871
872            if ($request->is_ajax())
873            {
874                $json_response = new \phpbb\json_response;
875                $json_response->send(array(
876                    'MESSAGE_TITLE'        => $user->lang['INFORMATION'],
877                    'MESSAGE_TEXT'        => $message,
878                    'REFRESH_DATA'        => null,
879                    'visible'            => true,
880                ));
881            }
882            $message .= '<br /><br />' . $user->lang('RETURN_PAGE', '<a href="' . $redirect . '">', '</a>');
883
884            // If approving one post, also give links back to post...
885            if (count($post_info) == 1 && $post_url)
886            {
887                $message .= '<br /><br />' . $user->lang('RETURN_POST', '<a href="' . $post_url . '">', '</a>');
888            }
889            trigger_error($message);
890        }
891        else
892        {
893            $show_notify = false;
894
895            if ($action == 'approve')
896            {
897                foreach ($post_info as $post_data)
898                {
899                    if (!$post_data['topic_posts_approved'])
900                    {
901                        $num_topics++;
902                    }
903
904                    if (!$show_notify && $post_data['poster_id'] != ANONYMOUS)
905                    {
906                        $show_notify = true;
907                    }
908                }
909            }
910
911            $template->assign_vars(array(
912                'S_NOTIFY_POSTER'            => $show_notify,
913                'S_' . strtoupper($action)    => true,
914            ));
915
916            // Create the confirm box message
917            $action_msg = strtoupper($action);
918            $num_posts = count($post_id_list) - $num_topics;
919            if ($num_topics > 0 && $num_posts <= 0)
920            {
921                $action_msg .= '_TOPIC' . (($num_topics == 1) ? '' : 'S');
922            }
923            else
924            {
925                $action_msg .= '_POST' . ((count($post_id_list) == 1) ? '' : 'S');
926            }
927            confirm_box(false, $action_msg, $s_hidden_fields, 'mcp_approve.html');
928        }
929
930        redirect($redirect);
931    }
932
933    /**
934    * Approve/Restore topics
935    *
936    * @param $action        string    Action we perform on the posts ('approve' or 'restore')
937    * @param $topic_id_list    array    IDs of the topics to approve/restore
938    * @param $id            mixed    Category of the current active module
939    * @param $mode            string    Active module
940    * @return void|never
941    */
942    public static function approve_topics($action, $topic_id_list, $id, $mode)
943    {
944        global $db, $template, $user, $phpbb_log;
945        global $phpEx, $phpbb_root_path, $request, $phpbb_container, $phpbb_dispatcher;
946
947        if (!phpbb_check_ids($topic_id_list, TOPICS_TABLE, 'topic_id', array('m_approve')))
948        {
949            send_status_line(403, 'Forbidden');
950            trigger_error('NOT_AUTHORISED');
951        }
952
953        $redirect = $request->variable('redirect', build_url(array('quickmod')));
954        $redirect = reapply_sid($redirect);
955        $success_msg = $topic_url = '';
956        $approve_log = array();
957
958        $s_hidden_fields = build_hidden_fields(array(
959            'i'                => $id,
960            'mode'            => $mode,
961            'topic_id_list'    => $topic_id_list,
962            'action'        => $action,
963            'redirect'        => $redirect,
964        ));
965
966        $topic_info = phpbb_get_topic_data($topic_id_list, 'm_approve');
967
968        if (confirm_box(true))
969        {
970            $notify_poster = ($action == 'approve' && isset($_REQUEST['notify_poster'])) ? true : false;
971
972            /* @var $phpbb_content_visibility \phpbb\content_visibility */
973            $phpbb_content_visibility = $phpbb_container->get('content.visibility');
974            $first_post_ids = array();
975
976            foreach ($topic_info as $topic_id => $topic_data)
977            {
978                $phpbb_content_visibility->set_topic_visibility(ITEM_APPROVED, $topic_id, $topic_data['forum_id'], $user->data['user_id'], time(), '');
979                $first_post_ids[$topic_id] = (int) $topic_data['topic_first_post_id'];
980
981                $topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t={$topic_id}");
982
983                $approve_log[] = array(
984                    'forum_id'        => $topic_data['forum_id'],
985                    'topic_id'        => $topic_data['topic_id'],
986                    'topic_title'    => $topic_data['topic_title'],
987                );
988            }
989
990            if (count($topic_info) >= 1)
991            {
992                $success_msg = (count($topic_info) == 1) ? 'TOPIC_' . strtoupper($action) . 'D_SUCCESS' : 'TOPICS_' . strtoupper($action) . 'D_SUCCESS';
993            }
994
995            foreach ($approve_log as $log_data)
996            {
997                $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_TOPIC_' . strtoupper($action) . 'D', false, array(
998                    'forum_id' => $log_data['forum_id'],
999                    'topic_id' => $log_data['topic_id'],
1000                    $log_data['topic_title']
1001                ));
1002            }
1003
1004            // Only send out the mails, when the posts are being approved
1005            if ($action == 'approve')
1006            {
1007                // Grab the first post text as it's needed for the quote notification.
1008                $sql = 'SELECT topic_id, post_text
1009                    FROM ' . POSTS_TABLE . '
1010                    WHERE ' . $db->sql_in_set('post_id', $first_post_ids);
1011                $result = $db->sql_query($sql);
1012
1013                while ($row = $db->sql_fetchrow($result))
1014                {
1015                    $topic_info[$row['topic_id']]['post_text'] = $row['post_text'];
1016                }
1017                $db->sql_freeresult($result);
1018
1019                // Handle notifications
1020                /* @var $phpbb_notifications \phpbb\notification\manager */
1021                $phpbb_notifications = $phpbb_container->get('notification_manager');
1022
1023                foreach ($topic_info as $topic_id => $topic_data)
1024                {
1025                    $topic_data = array_merge($topic_data, array(
1026                        'post_id'        => $topic_data['topic_first_post_id'],
1027                        'post_subject'    => $topic_data['topic_title'],
1028                        'post_time'        => $topic_data['topic_time'],
1029                        'poster_id'        => $topic_data['topic_poster'],
1030                        'post_username'    => $topic_data['topic_first_poster_name'],
1031                    ));
1032
1033                    $phpbb_notifications->delete_notifications('notification.type.topic_in_queue', $topic_id);
1034
1035                    // Only add notifications, if we are not reapproving post
1036                    // When the topic was already approved, but was edited and
1037                    // now needs re-approval, we don't want to notify the users
1038                    // again.
1039                    if ($topic_data['topic_visibility'] == ITEM_UNAPPROVED)
1040                    {
1041                        $phpbb_notifications->add_notifications(array(
1042                            'notification.type.mention',
1043                            'notification.type.quote',
1044                            'notification.type.topic',
1045                        ), $topic_data);
1046                    }
1047
1048                    $phpbb_notifications->mark_notifications(array('mention', 'quote'), $topic_data['post_id'], $user->data['user_id']);
1049                    $phpbb_notifications->mark_notifications('topic', $topic_id, $user->data['user_id']);
1050
1051                    if ($notify_poster)
1052                    {
1053                        $phpbb_notifications->add_notifications('notification.type.approve_topic', $topic_data);
1054                    }
1055                }
1056            }
1057
1058            /**
1059             * Perform additional actions during topics(s) approval
1060             *
1061             * @event core.approve_topics_after
1062             * @var    string    action                Variable containing the action we perform on the posts ('approve' or 'restore')
1063             * @var    mixed    topic_info            Array containing info for all topics being approved
1064             * @var    array    first_post_ids        Array containing ids of all first posts
1065             * @var bool    notify_poster        Variable telling if the poster should be notified or not
1066             * @var    string    success_msg            Variable containing the language key for the success message
1067             * @var string    redirect            Variable containing the redirect url
1068             * @since 3.1.4-RC1
1069             */
1070            $vars = array(
1071                'action',
1072                'topic_info',
1073                'first_post_ids',
1074                'notify_poster',
1075                'success_msg',
1076                'redirect',
1077            );
1078            extract($phpbb_dispatcher->trigger_event('core.approve_topics_after', compact($vars)));
1079
1080            meta_refresh(3, $redirect);
1081            $message = $user->lang[$success_msg];
1082
1083            if ($request->is_ajax())
1084            {
1085                $json_response = new \phpbb\json_response;
1086                $json_response->send(array(
1087                    'MESSAGE_TITLE'        => $user->lang['INFORMATION'],
1088                    'MESSAGE_TEXT'        => $message,
1089                    'REFRESH_DATA'        => null,
1090                    'visible'            => true,
1091                ));
1092            }
1093            $message .= '<br /><br />' . $user->lang('RETURN_PAGE', '<a href="' . $redirect . '">', '</a>');
1094
1095            // If approving one topic, also give links back to topic...
1096            if (count($topic_info) == 1 && $topic_url)
1097            {
1098                $message .= '<br /><br />' . $user->lang('RETURN_TOPIC', '<a href="' . $topic_url . '">', '</a>');
1099            }
1100            trigger_error($message);
1101        }
1102        else
1103        {
1104            $show_notify = false;
1105
1106            if ($action == 'approve')
1107            {
1108                foreach ($topic_info as $topic_data)
1109                {
1110                    if ($topic_data['topic_poster'] == ANONYMOUS)
1111                    {
1112                        continue;
1113                    }
1114                    else
1115                    {
1116                        $show_notify = true;
1117                        break;
1118                    }
1119                }
1120            }
1121
1122            $template->assign_vars(array(
1123                'S_NOTIFY_POSTER'            => $show_notify,
1124                'S_' . strtoupper($action)    => true,
1125            ));
1126
1127            confirm_box(false, strtoupper($action) . '_TOPIC' . ((count($topic_id_list) == 1) ? '' : 'S'), $s_hidden_fields, 'mcp_approve.html');
1128        }
1129
1130        redirect($redirect);
1131    }
1132
1133    /**
1134    * Disapprove Post
1135    *
1136    * @param $post_id_list    array    IDs of the posts to disapprove/delete
1137    * @param $id            mixed    Category of the current active module
1138    * @param $mode            string    Active module
1139    * @return void|never
1140    */
1141    public static function disapprove_posts($post_id_list, $id, $mode)
1142    {
1143        global $db, $template, $user, $phpbb_container, $phpbb_dispatcher;
1144        global $phpEx, $phpbb_root_path, $request, $phpbb_log;
1145
1146        if (!phpbb_check_ids($post_id_list, POSTS_TABLE, 'post_id', array('m_approve')))
1147        {
1148            send_status_line(403, 'Forbidden');
1149            trigger_error('NOT_AUTHORISED');
1150        }
1151
1152        $redirect = $request->variable('redirect', build_url(array('t', 'mode', 'quickmod')) . "&amp;mode=$mode");
1153        $redirect = reapply_sid($redirect);
1154        $reason = $request->variable('reason', '', true);
1155        $reason_id = $request->variable('reason_id', 0);
1156        $additional_msg = '';
1157
1158        $s_hidden_fields = build_hidden_fields(array(
1159            'i'                => $id,
1160            'mode'            => $mode,
1161            'post_id_list'    => $post_id_list,
1162            'action'        => 'disapprove',
1163            'redirect'        => $redirect,
1164        ));
1165
1166        $notify_poster = $request->is_set('notify_poster');
1167        $disapprove_reason = '';
1168
1169        if ($reason_id)
1170        {
1171            $sql = 'SELECT reason_title, reason_description
1172                FROM ' . REPORTS_REASONS_TABLE . "
1173                WHERE reason_id = $reason_id";
1174            $result = $db->sql_query($sql);
1175            $row = $db->sql_fetchrow($result);
1176            $db->sql_freeresult($result);
1177
1178            if (!$row || (!$reason && strtolower($row['reason_title']) == 'other'))
1179            {
1180                $additional_msg = $user->lang['NO_REASON_DISAPPROVAL'];
1181
1182                $request->overwrite('confirm', null, \phpbb\request\request_interface::POST);
1183                $request->overwrite('confirm_key', null, \phpbb\request\request_interface::POST);
1184                $request->overwrite('confirm_key', null, \phpbb\request\request_interface::REQUEST);
1185            }
1186            else
1187            {
1188                // If the reason is defined within the language file, we will use the localized version, else just use the database entry...
1189                $disapprove_reason = (strtolower($row['reason_title']) != 'other') ? ((isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])])) ? $user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])] : $row['reason_description']) : '';
1190                $disapprove_reason .= ($reason) ? "\n\n" . $reason : '';
1191
1192                if (isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])]))
1193                {
1194                    $disapprove_reason_lang = strtoupper($row['reason_title']);
1195                }
1196            }
1197        }
1198
1199        $post_info = phpbb_get_post_data($post_id_list, 'm_approve');
1200
1201        $is_disapproving = false;
1202        foreach ($post_info as $post_id => $post_data)
1203        {
1204            if ($post_data['post_visibility'] == ITEM_DELETED)
1205            {
1206                continue;
1207            }
1208
1209            $is_disapproving = true;
1210        }
1211
1212        if (confirm_box(true))
1213        {
1214            $disapprove_log_topics = $disapprove_log_posts = array();
1215            $topic_posts_unapproved = $post_disapprove_list = $topic_information = array();
1216
1217            // Build a list of posts to be disapproved and get the related topics real replies count
1218            foreach ($post_info as $post_id => $post_data)
1219            {
1220                if ($mode === 'unapproved_topics' && $post_data['post_visibility'] == ITEM_APPROVED)
1221                {
1222                    continue;
1223                }
1224
1225                $post_disapprove_list[$post_id] = $post_data['topic_id'];
1226                if (!isset($topic_posts_unapproved[$post_data['topic_id']]))
1227                {
1228                    $topic_information[$post_data['topic_id']] = $post_data;
1229                    $topic_posts_unapproved[$post_data['topic_id']] = 0;
1230                }
1231                $topic_posts_unapproved[$post_data['topic_id']]++;
1232            }
1233
1234            // Do not try to disapprove if no posts are selected
1235            if (empty($post_disapprove_list))
1236            {
1237                trigger_error('NO_POST_SELECTED');
1238            }
1239
1240            // Now we build the log array
1241            foreach ($post_disapprove_list as $post_id => $topic_id)
1242            {
1243                // If the count of disapproved posts for the topic is equal
1244                // to the number of unapproved posts in the topic, and there are no different
1245                // posts, we disapprove the hole topic
1246                if ($topic_information[$topic_id]['topic_posts_approved'] == 0 &&
1247                    $topic_information[$topic_id]['topic_posts_softdeleted'] == 0 &&
1248                    $topic_information[$topic_id]['topic_posts_unapproved'] == $topic_posts_unapproved[$topic_id])
1249                {
1250                    // Don't write the log more than once for every topic
1251                    if (!isset($disapprove_log_topics[$topic_id]))
1252                    {
1253                        // Build disapproved topics log
1254                        $disapprove_log_topics[$topic_id] = array(
1255                            'type'            => 'topic',
1256                            'post_subject'    => $post_info[$post_id]['topic_title'],
1257                            'forum_id'        => $post_info[$post_id]['forum_id'],
1258                            'topic_id'        => 0, // useless to log a topic id, as it will be deleted
1259                            'post_username'    => ($post_info[$post_id]['poster_id'] == ANONYMOUS && !empty($post_info[$post_id]['post_username'])) ? $post_info[$post_id]['post_username'] : $post_info[$post_id]['username'],
1260                        );
1261                    }
1262                }
1263                else
1264                {
1265                    // Build disapproved posts log
1266                    $disapprove_log_posts[] = array(
1267                        'type'            => 'post',
1268                        'post_subject'    => $post_info[$post_id]['post_subject'],
1269                        'forum_id'        => $post_info[$post_id]['forum_id'],
1270                        'topic_id'        => $post_info[$post_id]['topic_id'],
1271                        'post_username'    => ($post_info[$post_id]['poster_id'] == ANONYMOUS && !empty($post_info[$post_id]['post_username'])) ? $post_info[$post_id]['post_username'] : $post_info[$post_id]['username'],
1272                    );
1273
1274                }
1275            }
1276
1277            // Get disapproved posts/topics counts separately
1278            $num_disapproved_topics = count($disapprove_log_topics);
1279            $num_disapproved_posts = count($disapprove_log_posts);
1280
1281            // Build the whole log
1282            $disapprove_log = array_merge($disapprove_log_topics, $disapprove_log_posts);
1283
1284            // Unset unneeded arrays
1285            unset($post_data, $disapprove_log_topics, $disapprove_log_posts);
1286
1287            // Let's do the job - delete disapproved posts
1288            if (count($post_disapprove_list))
1289            {
1290                if (!function_exists('delete_posts'))
1291                {
1292                    include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
1293                }
1294
1295                // We do not check for permissions here, because the moderator allowed approval/disapproval should be allowed to delete the disapproved posts
1296                // Note: function delete_posts triggers related forums/topics sync,
1297                // so we don't need to call update_post_information later and to adjust real topic replies or forum topics count manually
1298                delete_posts('post_id', array_keys($post_disapprove_list));
1299
1300                foreach ($disapprove_log as $log_data)
1301                {
1302                    if ($is_disapproving)
1303                    {
1304                        $l_log_message = ($log_data['type'] == 'topic') ? 'LOG_TOPIC_DISAPPROVED' : 'LOG_POST_DISAPPROVED';
1305                        $phpbb_log->add('mod', $user->data['user_id'], $user->ip, $l_log_message, false, array(
1306                            'forum_id' => $log_data['forum_id'],
1307                            'topic_id' => $log_data['topic_id'],
1308                            $log_data['post_subject'],
1309                            $disapprove_reason,
1310                            $log_data['post_username']
1311                        ));
1312                    }
1313                    else
1314                    {
1315                        $l_log_message = ($log_data['type'] == 'topic') ? 'LOG_DELETE_TOPIC' : 'LOG_DELETE_POST';
1316                        $phpbb_log->add('mod', $user->data['user_id'], $user->ip, $l_log_message, false, array(
1317                            'forum_id' => $log_data['forum_id'],
1318                            'topic_id' => $log_data['topic_id'],
1319                            $log_data['post_subject'],
1320                            $log_data['post_username']
1321                        ));
1322                    }
1323                }
1324            }
1325
1326            /* @var $phpbb_notifications \phpbb\notification\manager */
1327            $phpbb_notifications = $phpbb_container->get('notification_manager');
1328
1329            $lang_reasons = array();
1330
1331            foreach ($post_info as $post_id => $post_data)
1332            {
1333                $disapprove_all_posts_in_topic = $topic_information[$topic_id]['topic_posts_approved'] == 0 &&
1334                    $topic_information[$topic_id]['topic_posts_softdeleted'] == 0 &&
1335                    $topic_information[$topic_id]['topic_posts_unapproved'] == $topic_posts_unapproved[$topic_id];
1336
1337                $phpbb_notifications->delete_notifications('notification.type.post_in_queue', $post_id);
1338
1339                // Do we disapprove the whole topic? Remove potential notifications
1340                if ($disapprove_all_posts_in_topic)
1341                {
1342                    $phpbb_notifications->delete_notifications('notification.type.topic_in_queue', $post_data['topic_id']);
1343                }
1344
1345                // Notify Poster?
1346                if ($notify_poster)
1347                {
1348                    if ($post_data['poster_id'] == ANONYMOUS)
1349                    {
1350                        continue;
1351                    }
1352
1353                    $post_data['disapprove_reason'] = $disapprove_reason;
1354                    if (isset($disapprove_reason_lang))
1355                    {
1356                        // Okay we need to get the reason from the posters language
1357                        if (!isset($lang_reasons[$post_data['user_lang']]))
1358                        {
1359                            // Assign the current users translation as the default, this is not ideal but getting the board default adds another layer of complexity.
1360                            $lang_reasons[$post_data['user_lang']] = $user->lang['report_reasons']['DESCRIPTION'][$disapprove_reason_lang];
1361
1362                            // Only load up the language pack if the language is different to the current one
1363                            if ($post_data['user_lang'] != $user->lang_name && file_exists($phpbb_root_path . '/language/' . $post_data['user_lang'] . '/mcp.' . $phpEx))
1364                            {
1365                                // Load up the language pack
1366                                $lang = array();
1367                                @include($phpbb_root_path . '/language/' . basename($post_data['user_lang']) . '/mcp.' . $phpEx);
1368
1369                                // If we find the reason in this language pack use it
1370                                if (isset($lang['report_reasons']['DESCRIPTION'][$disapprove_reason_lang]))
1371                                {
1372                                    $lang_reasons[$post_data['user_lang']] = $lang['report_reasons']['DESCRIPTION'][$disapprove_reason_lang];
1373                                }
1374
1375                                unset($lang); // Free memory
1376                            }
1377                        }
1378
1379                        $post_data['disapprove_reason'] = $lang_reasons[$post_data['user_lang']];
1380                        $post_data['disapprove_reason'] .= ($reason) ? "\n\n" . $reason : '';
1381                    }
1382
1383                    if ($disapprove_all_posts_in_topic && $topic_information[$topic_id]['topic_posts_unapproved'] == 1)
1384                    {
1385                        // If there is only 1 post when disapproving the topic,
1386                        // we send the user a "disapprove topic" notification...
1387                        $phpbb_notifications->add_notifications('notification.type.disapprove_topic', $post_data);
1388                    }
1389                    else
1390                    {
1391                        // ... otherwise there are multiple unapproved posts and
1392                        // all of them are disapproved as posts.
1393                        $phpbb_notifications->add_notifications('notification.type.disapprove_post', $post_data);
1394                    }
1395                }
1396            }
1397
1398            if ($num_disapproved_topics)
1399            {
1400                $success_msg = ($num_disapproved_topics == 1) ? 'TOPIC' : 'TOPICS';
1401            }
1402            else
1403            {
1404                $success_msg = ($num_disapproved_posts == 1) ? 'POST' : 'POSTS';
1405            }
1406
1407            if ($is_disapproving)
1408            {
1409                $success_msg .= '_DISAPPROVED_SUCCESS';
1410            }
1411            else
1412            {
1413                $success_msg .= '_DELETED_SUCCESS';
1414            }
1415
1416            // If we came from viewtopic, we try to go back to it.
1417            if (strpos($redirect, $phpbb_root_path . 'viewtopic.' . $phpEx) === 0)
1418            {
1419                if ($num_disapproved_topics == 0)
1420                {
1421                    // So we need to remove the post id part from the Url
1422                    $redirect = str_replace("&amp;p={$post_id_list[0]}#p{$post_id_list[0]}", '', $redirect);
1423                }
1424                else
1425                {
1426                    // However this is only possible if the topic still exists,
1427                    // Otherwise we go back to the viewforum page
1428                    $redirect = append_sid($phpbb_root_path . 'viewforum.' . $phpEx, 'f=' . $post_data['forum_id']);
1429                }
1430            }
1431
1432            $disapprove_reason_lang = $disapprove_reason_lang ?? '';
1433
1434            /**
1435             * Perform additional actions during post(s) disapproval
1436             *
1437             * @event core.disapprove_posts_after
1438             * @var    array    post_info                    Array containing info for all posts being disapproved
1439             * @var    array    topic_information            Array containing information for the topics
1440             * @var    array    topic_posts_unapproved        Array containing list of topic ids and the count of disapproved posts in them
1441             * @var    array    post_disapprove_list        Array containing list of posts and their topic id
1442             * @var    int        num_disapproved_topics        Variable containing the number of disapproved topics
1443             * @var    int        num_disapproved_posts        Variable containing the number of disapproved posts
1444             * @var array    lang_reasons                Array containing the language keys for reasons
1445             * @var    string    disapprove_reason            Variable containing the language key for the success message
1446             * @var    string    disapprove_reason_lang        Variable containing the language key for the success message
1447             * @var bool    is_disapproving                Variable telling if anything is going to be disapproved
1448             * @var bool    notify_poster                Variable telling if the post should be notified or not
1449             * @var    string    success_msg                    Variable containing the language key for the success message
1450             * @var string    redirect                    Variable containing the redirect url
1451             * @since 3.1.4-RC1
1452             */
1453            $vars = array(
1454                'post_info',
1455                'topic_information',
1456                'topic_posts_unapproved',
1457                'post_disapprove_list',
1458                'num_disapproved_topics',
1459                'num_disapproved_posts',
1460                'lang_reasons',
1461                'disapprove_reason',
1462                'disapprove_reason_lang',
1463                'is_disapproving',
1464                'notify_poster',
1465                'success_msg',
1466                'redirect',
1467            );
1468            extract($phpbb_dispatcher->trigger_event('core.disapprove_posts_after', compact($vars)));
1469
1470            unset($lang_reasons, $post_info, $disapprove_reason, $disapprove_reason_lang);
1471
1472            meta_refresh(3, $redirect);
1473            $message = $user->lang[$success_msg];
1474
1475            if ($request->is_ajax())
1476            {
1477                $json_response = new \phpbb\json_response;
1478                $json_response->send(array(
1479                    'MESSAGE_TITLE'        => $user->lang['INFORMATION'],
1480                    'MESSAGE_TEXT'        => $message,
1481                    'REFRESH_DATA'        => null,
1482                    'visible'            => false,
1483                ));
1484            }
1485            $message .= '<br /><br />' . $user->lang('RETURN_PAGE', '<a href="' . $redirect . '">', '</a>');
1486            trigger_error($message);
1487        }
1488        else
1489        {
1490            $show_notify = false;
1491
1492            foreach ($post_info as $post_data)
1493            {
1494                if ($post_data['poster_id'] == ANONYMOUS)
1495                {
1496                    continue;
1497                }
1498                else
1499                {
1500                    $show_notify = true;
1501                    break;
1502                }
1503            }
1504
1505            $l_confirm_msg = 'DISAPPROVE_POST';
1506            $confirm_template = 'mcp_approve.html';
1507            if ($is_disapproving)
1508            {
1509                $phpbb_container->get('phpbb.report.report_reason_list_provider')->display_reasons($reason_id);
1510            }
1511            else
1512            {
1513                $user->add_lang('posting');
1514
1515                $l_confirm_msg = 'DELETE_POST_PERMANENTLY';
1516                $confirm_template = 'confirm_delete_body.html';
1517            }
1518            $l_confirm_msg .= ((count($post_id_list) == 1) ? '' : 'S');
1519
1520            $template->assign_vars(array(
1521                'S_NOTIFY_POSTER'    => $show_notify,
1522                'S_APPROVE'            => false,
1523                'REASON'            => ($is_disapproving) ? $reason : '',
1524                'ADDITIONAL_MSG'    => $additional_msg,
1525            ));
1526
1527            confirm_box(false, $l_confirm_msg, $s_hidden_fields, $confirm_template);
1528        }
1529
1530        redirect($redirect);
1531    }
1532}