Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 1144
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
acp_forums
0.00% covered (danger)
0.00%
0 / 1142
0.00% covered (danger)
0.00%
0 / 9
75350
0.00% covered (danger)
0.00%
0 / 1
 main
0.00% covered (danger)
0.00%
0 / 524
0.00% covered (danger)
0.00%
0 / 1
24180
 get_forum_info
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 update_forum_data
0.00% covered (danger)
0.00%
0 / 214
0.00% covered (danger)
0.00%
0 / 1
3192
 move_forum
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 1
56
 move_forum_content
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
30
 delete_forum
0.00% covered (danger)
0.00%
0 / 139
0.00% covered (danger)
0.00%
0 / 1
702
 delete_forum_content
0.00% covered (danger)
0.00%
0 / 130
0.00% covered (danger)
0.00%
0 / 1
240
 move_forum_by
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
30
 display_progress_bar
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
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
22class acp_forums
23{
24    var $u_action;
25    var $parent_id = 0;
26
27    function main($id, $mode)
28    {
29        global $db, $user, $auth, $template, $cache, $request, $phpbb_dispatcher;
30        global $phpbb_admin_path, $phpbb_container, $phpbb_root_path, $phpEx, $phpbb_log;
31
32        $user->add_lang('acp/forums');
33        $this->tpl_name = 'acp_forums';
34        $this->page_title = 'ACP_MANAGE_FORUMS';
35
36        $form_key = 'acp_forums';
37        add_form_key($form_key);
38
39        $action        = $request->variable('action', '');
40        $update        = (isset($_POST['update'])) ? true : false;
41        $forum_id    = $request->variable('f', 0);
42
43        $this->parent_id    = $request->variable('parent_id', 0);
44        $forum_data = $errors = array();
45        if ($update && !check_form_key($form_key))
46        {
47            $update = false;
48            $errors[] = $user->lang['FORM_INVALID'];
49        }
50
51        // Check additional permissions
52        switch ($action)
53        {
54            case 'progress_bar':
55                $start = $request->variable('start', 0);
56                $total = $request->variable('total', 0);
57
58                $this->display_progress_bar($start, $total);
59            break;
60
61            case 'delete':
62
63                if (!$auth->acl_get('a_forumdel'))
64                {
65                    trigger_error($user->lang['NO_PERMISSION_FORUM_DELETE'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
66                }
67
68            break;
69
70            case 'add':
71
72                if (!$auth->acl_get('a_forumadd'))
73                {
74                    trigger_error($user->lang['NO_PERMISSION_FORUM_ADD'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
75                }
76
77            break;
78        }
79
80        // Major routines
81        if ($update)
82        {
83            switch ($action)
84            {
85                case 'delete':
86                    $action_subforums    = $request->variable('action_subforums', '');
87                    $subforums_to_id    = $request->variable('subforums_to_id', 0);
88                    $action_posts        = $request->variable('action_posts', '');
89                    $posts_to_id        = $request->variable('posts_to_id', 0);
90
91                    $errors = $this->delete_forum($forum_id, $action_posts, $action_subforums, $posts_to_id, $subforums_to_id);
92
93                    if (count($errors))
94                    {
95                        break;
96                    }
97
98                    $auth->acl_clear_prefetch();
99                    $cache->destroy('sql', FORUMS_TABLE);
100
101                    trigger_error($user->lang['FORUM_DELETED'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id));
102
103                break;
104
105                case 'edit':
106                    $forum_data = array(
107                        'forum_id'        =>    $forum_id
108                    );
109
110                // No break here
111
112                case 'add':
113
114                    $forum_data += array(
115                        'parent_id'                => $request->variable('forum_parent_id', $this->parent_id),
116                        'forum_type'            => $request->variable('forum_type', FORUM_POST),
117                        'type_action'            => $request->variable('type_action', ''),
118                        'forum_status'            => $request->variable('forum_status', ITEM_UNLOCKED),
119                        'forum_parents'            => '',
120                        'forum_name'            => $request->variable('forum_name', '', true),
121                        'forum_link'            => $request->variable('forum_link', ''),
122                        'forum_link_track'        => $request->variable('forum_link_track', false),
123                        'forum_desc'            => $request->variable('forum_desc', '', true),
124                        'forum_desc_uid'        => '',
125                        'forum_desc_options'    => 7,
126                        'forum_desc_bitfield'    => '',
127                        'forum_rules'            => $request->variable('forum_rules', '', true),
128                        'forum_rules_uid'        => '',
129                        'forum_rules_options'    => 7,
130                        'forum_rules_bitfield'    => '',
131                        'forum_rules_link'        => $request->variable('forum_rules_link', ''),
132                        'forum_image'            => $request->variable('forum_image', ''),
133                        'forum_style'            => $request->variable('forum_style', 0),
134                        'display_subforum_list'    => $request->variable('display_subforum_list', true),
135                        'display_subforum_limit'=> $request->variable('display_subforum_limit', false),
136                        'display_on_index'        => $request->variable('display_on_index', true),
137                        'forum_topics_per_page'    => $request->variable('topics_per_page', 0),
138                        'enable_indexing'        => $request->variable('enable_indexing', true),
139                        'enable_icons'            => $request->variable('enable_icons', true),
140                        'enable_prune'            => $request->variable('enable_prune', false),
141                        'enable_post_review'    => $request->variable('enable_post_review', true),
142                        'enable_quick_reply'    => $request->variable('enable_quick_reply', false),
143                        'enable_shadow_prune'    => $request->variable('enable_shadow_prune', false),
144                        'prune_days'            => $request->variable('prune_days', 7),
145                        'prune_viewed'            => $request->variable('prune_viewed', 7),
146                        'prune_freq'            => $request->variable('prune_freq', 1),
147                        'prune_old_polls'        => $request->variable('prune_old_polls', false),
148                        'prune_announce'        => $request->variable('prune_announce', false),
149                        'prune_sticky'            => $request->variable('prune_sticky', false),
150                        'prune_shadow_days'        => $request->variable('prune_shadow_days', 7),
151                        'prune_shadow_freq'        => $request->variable('prune_shadow_freq', 1),
152                        'forum_password'        => $request->variable('forum_password', '', true),
153                        'forum_password_confirm'=> $request->variable('forum_password_confirm', '', true),
154                        'forum_password_unset'    => $request->variable('forum_password_unset', false),
155                    );
156
157                    /**
158                    * Request forum data and operate on it (parse texts, etc.)
159                    *
160                    * @event core.acp_manage_forums_request_data
161                    * @var    string    action        Type of the action: add|edit
162                    * @var    array    forum_data    Array with new forum data
163                    * @since 3.1.0-a1
164                    */
165                    $vars = array('action', 'forum_data');
166                    extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_request_data', compact($vars)));
167
168                    // On add, add empty forum_options... else do not consider it (not updating it)
169                    if ($action == 'add')
170                    {
171                        $forum_data['forum_options'] = 0;
172                    }
173
174                    // Use link_display_on_index setting if forum type is link
175                    if ($forum_data['forum_type'] == FORUM_LINK)
176                    {
177                        $forum_data['display_on_index'] = $request->variable('link_display_on_index', false);
178                    }
179
180                    // Linked forums and categories are not able to be locked...
181                    if ($forum_data['forum_type'] == FORUM_LINK || $forum_data['forum_type'] == FORUM_CAT)
182                    {
183                        $forum_data['forum_status'] = ITEM_UNLOCKED;
184                    }
185
186                    $forum_data['show_active'] = ($forum_data['forum_type'] == FORUM_POST) ? $request->variable('display_recent', true) : $request->variable('display_active', false);
187
188                    // Get data for forum rules if specified...
189                    if ($forum_data['forum_rules'])
190                    {
191                        generate_text_for_storage($forum_data['forum_rules'], $forum_data['forum_rules_uid'], $forum_data['forum_rules_bitfield'], $forum_data['forum_rules_options'], $request->variable('rules_parse_bbcode', false), $request->variable('rules_parse_urls', false), $request->variable('rules_parse_smilies', false));
192                    }
193
194                    // Get data for forum description if specified
195                    if ($forum_data['forum_desc'])
196                    {
197                        generate_text_for_storage($forum_data['forum_desc'], $forum_data['forum_desc_uid'], $forum_data['forum_desc_bitfield'], $forum_data['forum_desc_options'], $request->variable('desc_parse_bbcode', false), $request->variable('desc_parse_urls', false), $request->variable('desc_parse_smilies', false));
198                    }
199
200                    $errors = $this->update_forum_data($forum_data);
201
202                    if (!count($errors))
203                    {
204                        $forum_perm_from = $request->variable('forum_perm_from', 0);
205                        $cache->destroy('sql', FORUMS_TABLE);
206
207                        $copied_permissions = false;
208                        // Copy permissions?
209                        if ($forum_perm_from && $forum_perm_from != $forum_data['forum_id'] &&
210                            ($action != 'edit' || empty($forum_id) || ($auth->acl_get('a_fauth') && $auth->acl_get('a_authusers') && $auth->acl_get('a_authgroups') && $auth->acl_get('a_mauth'))))
211                        {
212                            copy_forum_permissions($forum_perm_from, $forum_data['forum_id'], ($action == 'edit') ? true : false);
213                            phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
214                            $copied_permissions = true;
215                        }
216
217                        $auth->acl_clear_prefetch();
218
219                        $acl_url = '&amp;mode=setting_forum_local&amp;forum_id[]=' . $forum_data['forum_id'];
220
221                        $message = ($action == 'add') ? $user->lang['FORUM_CREATED'] : $user->lang['FORUM_UPDATED'];
222
223                        // redirect directly to permission settings screen if authed
224                        if ($action == 'add' && !$copied_permissions && $auth->acl_get('a_fauth'))
225                        {
226                            $message .= '<br /><br />' . sprintf($user->lang['REDIRECT_ACL'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=permissions' . $acl_url) . '">', '</a>');
227
228                            meta_refresh(4, append_sid("{$phpbb_admin_path}index.$phpEx", 'i=permissions' . $acl_url));
229                        }
230
231                        trigger_error($message . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id));
232                    }
233
234                break;
235            }
236        }
237
238        switch ($action)
239        {
240            case 'move_up':
241            case 'move_down':
242
243                if (!$forum_id)
244                {
245                    trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
246                }
247
248                $sql = 'SELECT *
249                    FROM ' . FORUMS_TABLE . "
250                    WHERE forum_id = $forum_id";
251                $result = $db->sql_query($sql);
252                $row = $db->sql_fetchrow($result);
253                $db->sql_freeresult($result);
254
255                if (!$row)
256                {
257                    trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
258                }
259
260                $move_forum_name = $this->move_forum_by($row, $action, 1);
261
262                if ($move_forum_name !== false)
263                {
264                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_' . strtoupper($action), false, array($row['forum_name'], $move_forum_name));
265                    $cache->destroy('sql', FORUMS_TABLE);
266                }
267
268                if ($request->is_ajax())
269                {
270                    $json_response = new \phpbb\json_response;
271                    $json_response->send(array('success' => ($move_forum_name !== false)));
272                }
273
274            break;
275
276            case 'sync':
277                if (!$forum_id)
278                {
279                    trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
280                }
281
282                @set_time_limit(0);
283
284                $sql = 'SELECT forum_name, (forum_topics_approved + forum_topics_unapproved + forum_topics_softdeleted) AS total_topics
285                    FROM ' . FORUMS_TABLE . "
286                    WHERE forum_id = $forum_id";
287                $result = $db->sql_query($sql);
288                $row = $db->sql_fetchrow($result);
289                $db->sql_freeresult($result);
290
291                if (!$row)
292                {
293                    trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
294                }
295
296                if ($row['total_topics'])
297                {
298                    $sql = 'SELECT MIN(topic_id) as min_topic_id, MAX(topic_id) as max_topic_id
299                        FROM ' . TOPICS_TABLE . '
300                        WHERE forum_id = ' . $forum_id;
301                    $result = $db->sql_query($sql);
302                    $row2 = $db->sql_fetchrow($result);
303                    $db->sql_freeresult($result);
304
305                    // Typecast to int if there is no data available
306                    $row2['min_topic_id'] = (int) $row2['min_topic_id'];
307                    $row2['max_topic_id'] = (int) $row2['max_topic_id'];
308
309                    $start = $request->variable('start', $row2['min_topic_id']);
310
311                    $batch_size = 2000;
312                    $end = $start + $batch_size;
313
314                    // Sync all topics in batch mode...
315                    sync('topic', 'range', 'topic_id BETWEEN ' . $start . ' AND ' . $end, true, true);
316
317                    if ($end < $row2['max_topic_id'])
318                    {
319                        // We really need to find a way of showing statistics... no progress here
320                        $sql = 'SELECT COUNT(topic_id) as num_topics
321                            FROM ' . TOPICS_TABLE . '
322                            WHERE forum_id = ' . $forum_id . '
323                                AND topic_id BETWEEN ' . $start . ' AND ' . $end;
324                        $result = $db->sql_query($sql);
325                        $topics_done = $request->variable('topics_done', 0) + (int) $db->sql_fetchfield('num_topics');
326                        $db->sql_freeresult($result);
327
328                        $start += $batch_size;
329
330                        $url = $this->u_action . "&amp;parent_id={$this->parent_id}&amp;f=$forum_id&amp;action=sync&amp;start=$start&amp;topics_done=$topics_done&amp;total={$row['total_topics']}";
331
332                        meta_refresh(0, $url);
333
334                        $template->assign_vars(array(
335                            'U_PROGRESS_BAR'        => $this->u_action . "&amp;action=progress_bar&amp;start=$topics_done&amp;total={$row['total_topics']}",
336                            'UA_PROGRESS_BAR'        => addslashes($this->u_action . "&amp;action=progress_bar&amp;start=$topics_done&amp;total={$row['total_topics']}"),
337                            'S_CONTINUE_SYNC'        => true,
338                            'L_PROGRESS_EXPLAIN'    => sprintf($user->lang['SYNC_IN_PROGRESS_EXPLAIN'], $topics_done, $row['total_topics']))
339                        );
340
341                        return;
342                    }
343                }
344
345                $url = $this->u_action . "&amp;parent_id={$this->parent_id}&amp;f=$forum_id&amp;action=sync_forum";
346                meta_refresh(0, $url);
347
348                $template->assign_vars(array(
349                    'U_PROGRESS_BAR'        => $this->u_action . '&amp;action=progress_bar',
350                    'UA_PROGRESS_BAR'        => addslashes($this->u_action . '&amp;action=progress_bar'),
351                    'S_CONTINUE_SYNC'        => true,
352                    'L_PROGRESS_EXPLAIN'    => sprintf($user->lang['SYNC_IN_PROGRESS_EXPLAIN'], 0, $row['total_topics']))
353                );
354
355                return;
356
357            break;
358
359            case 'sync_forum':
360
361                $sql = 'SELECT forum_name, forum_type
362                    FROM ' . FORUMS_TABLE . "
363                    WHERE forum_id = $forum_id";
364                $result = $db->sql_query($sql);
365                $row = $db->sql_fetchrow($result);
366                $db->sql_freeresult($result);
367
368                if (!$row)
369                {
370                    trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
371                }
372
373                sync('forum', 'forum_id', $forum_id, false, true);
374
375                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_SYNC', false, array($row['forum_name']));
376
377                $cache->destroy('sql', FORUMS_TABLE);
378
379                $template->assign_var('L_FORUM_RESYNCED', sprintf($user->lang['FORUM_RESYNCED'], $row['forum_name']));
380
381            break;
382
383            case 'add':
384            case 'edit':
385
386                if ($update)
387                {
388                    $forum_data['forum_flags'] = 0;
389                    $forum_data['forum_flags'] += ($request->variable('forum_link_track', false)) ? FORUM_FLAG_LINK_TRACK : 0;
390                    $forum_data['forum_flags'] += ($request->variable('prune_old_polls', false)) ? FORUM_FLAG_PRUNE_POLL : 0;
391                    $forum_data['forum_flags'] += ($request->variable('prune_announce', false)) ? FORUM_FLAG_PRUNE_ANNOUNCE : 0;
392                    $forum_data['forum_flags'] += ($request->variable('prune_sticky', false)) ? FORUM_FLAG_PRUNE_STICKY : 0;
393                    $forum_data['forum_flags'] += ($forum_data['show_active']) ? FORUM_FLAG_ACTIVE_TOPICS : 0;
394                    $forum_data['forum_flags'] += ($request->variable('enable_post_review', true)) ? FORUM_FLAG_POST_REVIEW : 0;
395                    $forum_data['forum_flags'] += ($request->variable('enable_quick_reply', false)) ? FORUM_FLAG_QUICK_REPLY : 0;
396                }
397
398                // Initialise $row, so we always have it in the event
399                $row = array();
400
401                // Show form to create/modify a forum
402                if ($action == 'edit')
403                {
404                    $this->page_title = 'EDIT_FORUM';
405                    $row = $this->get_forum_info($forum_id);
406                    $old_forum_type = $row['forum_type'];
407
408                    if (!$update)
409                    {
410                        $forum_data = $row;
411                    }
412                    else
413                    {
414                        $forum_data['left_id'] = $row['left_id'];
415                        $forum_data['right_id'] = $row['right_id'];
416                    }
417
418                    // Make sure no direct child forums are able to be selected as parents.
419                    $exclude_forums = array();
420                    foreach (get_forum_branch($forum_id, 'children') as $row)
421                    {
422                        $exclude_forums[] = $row['forum_id'];
423                    }
424
425                    $parents_list = make_forum_select($forum_data['parent_id'], $exclude_forums, false, false, false);
426
427                    $forum_data['forum_password_confirm'] = $forum_data['forum_password'];
428                }
429                else
430                {
431                    $this->page_title = 'CREATE_FORUM';
432
433                    $forum_id = $this->parent_id;
434                    $parents_list = make_forum_select($this->parent_id, false, false, false, false);
435
436                    // Fill forum data with default values
437                    if (!$update)
438                    {
439                        $forum_data = array(
440                            'parent_id'                => $this->parent_id,
441                            'forum_type'            => FORUM_POST,
442                            'forum_status'            => ITEM_UNLOCKED,
443                            'forum_name'            => $request->variable('forum_name', '', true),
444                            'forum_link'            => '',
445                            'forum_link_track'        => false,
446                            'forum_desc'            => '',
447                            'forum_rules'            => '',
448                            'forum_rules_link'        => '',
449                            'forum_image'            => '',
450                            'forum_style'            => 0,
451                            'display_subforum_list'    => true,
452                            'display_subforum_limit'    => false,
453                            'display_on_index'        => true,
454                            'forum_topics_per_page'    => 0,
455                            'enable_indexing'        => true,
456                            'enable_icons'            => true,
457                            'enable_prune'            => false,
458                            'prune_days'            => 7,
459                            'prune_viewed'            => 7,
460                            'prune_freq'            => 1,
461                            'enable_shadow_prune'        => false,
462                            'prune_shadow_days'        => 7,
463                            'prune_shadow_freq'        => 1,
464                            'forum_flags'            => FORUM_FLAG_POST_REVIEW + FORUM_FLAG_ACTIVE_TOPICS,
465                            'forum_options'            => 0,
466                            'forum_password'        => '',
467                            'forum_password_confirm'=> '',
468                        );
469                    }
470                }
471
472                /**
473                * Initialise data before we display the add/edit form
474                *
475                * @event core.acp_manage_forums_initialise_data
476                * @var    string    action        Type of the action: add|edit
477                * @var    bool    update        Do we display the form only
478                *                            or did the user press submit
479                * @var    int        forum_id    When editing: the forum id,
480                *                            when creating: the parent forum id
481                * @var    array    row            Array with current forum data
482                *                            empty when creating new forum
483                * @var    array    forum_data    Array with new forum data
484                * @var    string    parents_list    List of parent options
485                * @since 3.1.0-a1
486                */
487                $vars = array('action', 'update', 'forum_id', 'row', 'forum_data', 'parents_list');
488                extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_initialise_data', compact($vars)));
489
490                $forum_rules_data = array(
491                    'text'            => $forum_data['forum_rules'],
492                    'allow_bbcode'    => true,
493                    'allow_smilies'    => true,
494                    'allow_urls'    => true
495                );
496
497                $forum_desc_data = array(
498                    'text'            => $forum_data['forum_desc'],
499                    'allow_bbcode'    => true,
500                    'allow_smilies'    => true,
501                    'allow_urls'    => true
502                );
503
504                $forum_rules_preview = '';
505
506                // Parse rules if specified
507                if ($forum_data['forum_rules'])
508                {
509                    if (!isset($forum_data['forum_rules_uid']))
510                    {
511                        // Before we are able to display the preview and plane text, we need to parse our $request->variable()'d value...
512                        $forum_data['forum_rules_uid'] = '';
513                        $forum_data['forum_rules_bitfield'] = '';
514                        $forum_data['forum_rules_options'] = 0;
515
516                        generate_text_for_storage($forum_data['forum_rules'], $forum_data['forum_rules_uid'], $forum_data['forum_rules_bitfield'], $forum_data['forum_rules_options'], $request->variable('rules_allow_bbcode', false), $request->variable('rules_allow_urls', false), $request->variable('rules_allow_smilies', false));
517                    }
518
519                    // Generate preview content
520                    $forum_rules_preview = generate_text_for_display($forum_data['forum_rules'], $forum_data['forum_rules_uid'], $forum_data['forum_rules_bitfield'], $forum_data['forum_rules_options']);
521
522                    // decode...
523                    $forum_rules_data = generate_text_for_edit($forum_data['forum_rules'], $forum_data['forum_rules_uid'], $forum_data['forum_rules_options']);
524                }
525
526                // Parse desciption if specified
527                if ($forum_data['forum_desc'])
528                {
529                    if (!isset($forum_data['forum_desc_uid']))
530                    {
531                        // Before we are able to display the preview and plane text, we need to parse our $request->variable()'d value...
532                        $forum_data['forum_desc_uid'] = '';
533                        $forum_data['forum_desc_bitfield'] = '';
534                        $forum_data['forum_desc_options'] = 0;
535
536                        generate_text_for_storage($forum_data['forum_desc'], $forum_data['forum_desc_uid'], $forum_data['forum_desc_bitfield'], $forum_data['forum_desc_options'], $request->variable('desc_allow_bbcode', false), $request->variable('desc_allow_urls', false), $request->variable('desc_allow_smilies', false));
537                    }
538
539                    // decode...
540                    $forum_desc_data = generate_text_for_edit($forum_data['forum_desc'], $forum_data['forum_desc_uid'], $forum_data['forum_desc_options']);
541                }
542
543                $forum_type_options = '';
544                $forum_type_ary = array(FORUM_CAT => 'CAT', FORUM_POST => 'FORUM', FORUM_LINK => 'LINK');
545
546                foreach ($forum_type_ary as $value => $lang)
547                {
548                    $forum_type_options .= '<option value="' . $value . '"' . (($value == $forum_data['forum_type']) ? ' selected="selected"' : '') . '>' . $user->lang['TYPE_' . $lang] . '</option>';
549                }
550
551                $styles_list = style_select($forum_data['forum_style'], true);
552
553                $statuslist = '<option value="' . ITEM_UNLOCKED . '"' . (($forum_data['forum_status'] == ITEM_UNLOCKED) ? ' selected="selected"' : '') . '>' . $user->lang['UNLOCKED'] . '</option><option value="' . ITEM_LOCKED . '"' . (($forum_data['forum_status'] == ITEM_LOCKED) ? ' selected="selected"' : '') . '>' . $user->lang['LOCKED'] . '</option>';
554
555                $sql = 'SELECT forum_id
556                    FROM ' . FORUMS_TABLE . '
557                    WHERE forum_type = ' . FORUM_POST . "
558                        AND forum_id <> $forum_id";
559                $result = $db->sql_query_limit($sql, 1);
560
561                $postable_forum_exists = false;
562                if ($db->sql_fetchrow($result))
563                {
564                    $postable_forum_exists = true;
565                }
566                $db->sql_freeresult($result);
567
568                // Subforum move options
569                if ($action == 'edit' && $forum_data['forum_type'] == FORUM_CAT)
570                {
571                    $subforums_id = array();
572                    $subforums = get_forum_branch($forum_id, 'children');
573
574                    foreach ($subforums as $row)
575                    {
576                        $subforums_id[] = $row['forum_id'];
577                    }
578
579                    $forums_list = make_forum_select($forum_data['parent_id'], $subforums_id);
580
581                    if ($postable_forum_exists)
582                    {
583                        $template->assign_vars(array(
584                            'S_MOVE_FORUM_OPTIONS'        => make_forum_select($forum_data['parent_id'], $subforums_id)) // , false, true, false???
585                        );
586                    }
587
588                    $template->assign_vars(array(
589                        'S_HAS_SUBFORUMS'        => ($forum_data['right_id'] - $forum_data['left_id'] > 1) ? true : false,
590                        'S_FORUMS_LIST'            => $forums_list)
591                    );
592                }
593                else if ($postable_forum_exists)
594                {
595                    $template->assign_vars(array(
596                        'S_MOVE_FORUM_OPTIONS'        => make_forum_select($forum_data['parent_id'], $forum_id, false, true, false))
597                    );
598                }
599
600                $s_show_display_on_index = false;
601
602                if ($forum_data['parent_id'] > 0)
603                {
604                    // if this forum is a subforum put the "display on index" checkbox
605                    if ($parent_info = $this->get_forum_info($forum_data['parent_id']))
606                    {
607                        if ($parent_info['parent_id'] > 0 || $parent_info['forum_type'] == FORUM_CAT)
608                        {
609                            $s_show_display_on_index = true;
610                        }
611                    }
612                }
613
614                if (strlen($forum_data['forum_password']) == 32)
615                {
616                    $errors[] = $user->lang['FORUM_PASSWORD_OLD'];
617                }
618
619                $template_data = array(
620                    'S_EDIT_FORUM'        => true,
621                    'S_ERROR'            => (count($errors)) ? true : false,
622                    'S_PARENT_ID'        => $this->parent_id,
623                    'S_FORUM_PARENT_ID'    => $forum_data['parent_id'],
624                    'S_ADD_ACTION'        => ($action == 'add') ? true : false,
625
626                    'U_BACK'        => $this->u_action . '&amp;parent_id=' . $this->parent_id,
627                    'U_EDIT_ACTION'    => $this->u_action . "&amp;parent_id={$this->parent_id}&amp;action=$action&amp;f=$forum_id",
628
629                    'L_COPY_PERMISSIONS_EXPLAIN'    => $user->lang['COPY_PERMISSIONS_' . strtoupper($action) . '_EXPLAIN'],
630                    'L_TITLE'                        => $user->lang[$this->page_title],
631                    'ERROR_MSG'                        => (count($errors)) ? implode('<br />', $errors) : '',
632
633                    'FORUM_NAME'                => $forum_data['forum_name'],
634                    'FORUM_DATA_LINK'            => $forum_data['forum_link'],
635                    'FORUM_IMAGE'                => $forum_data['forum_image'],
636                    'FORUM_IMAGE_SRC'            => ($forum_data['forum_image']) ? $phpbb_root_path . $forum_data['forum_image'] : '',
637                    'FORUM_POST'                => FORUM_POST,
638                    'FORUM_LINK'                => FORUM_LINK,
639                    'FORUM_CAT'                    => FORUM_CAT,
640                    'PRUNE_FREQ'                => $forum_data['prune_freq'],
641                    'PRUNE_DAYS'                => $forum_data['prune_days'],
642                    'PRUNE_VIEWED'                => $forum_data['prune_viewed'],
643                    'PRUNE_SHADOW_FREQ'            => $forum_data['prune_shadow_freq'],
644                    'PRUNE_SHADOW_DAYS'            => $forum_data['prune_shadow_days'],
645                    'TOPICS_PER_PAGE'            => $forum_data['forum_topics_per_page'],
646                    'FORUM_RULES_LINK'            => $forum_data['forum_rules_link'],
647                    'FORUM_RULES'                => $forum_data['forum_rules'],
648                    'FORUM_RULES_PREVIEW'        => $forum_rules_preview,
649                    'FORUM_RULES_PLAIN'            => $forum_rules_data['text'],
650                    'S_BBCODE_CHECKED'            => ($forum_rules_data['allow_bbcode']) ? true : false,
651                    'S_SMILIES_CHECKED'            => ($forum_rules_data['allow_smilies']) ? true : false,
652                    'S_URLS_CHECKED'            => ($forum_rules_data['allow_urls']) ? true : false,
653                    'S_FORUM_PASSWORD_SET'        => (empty($forum_data['forum_password'])) ? false : true,
654
655                    'FORUM_DESC'                => $forum_desc_data['text'],
656                    'S_DESC_BBCODE_CHECKED'        => ($forum_desc_data['allow_bbcode']) ? true : false,
657                    'S_DESC_SMILIES_CHECKED'    => ($forum_desc_data['allow_smilies']) ? true : false,
658                    'S_DESC_URLS_CHECKED'        => ($forum_desc_data['allow_urls']) ? true : false,
659
660                    'S_FORUM_TYPE_OPTIONS'        => $forum_type_options,
661                    'S_STATUS_OPTIONS'            => $statuslist,
662                    'S_PARENT_OPTIONS'            => $parents_list,
663                    'S_STYLES_OPTIONS'            => $styles_list,
664                    'S_FORUM_OPTIONS'            => make_forum_select(($action == 'add') ? $forum_data['parent_id'] : false, ($action == 'edit') ? $forum_data['forum_id'] : false, false, false, false),
665                    'S_SHOW_DISPLAY_ON_INDEX'    => $s_show_display_on_index,
666                    'S_FORUM_POST'                => ($forum_data['forum_type'] == FORUM_POST) ? true : false,
667                    'S_FORUM_ORIG_POST'            => (isset($old_forum_type) && $old_forum_type == FORUM_POST) ? true : false,
668                    'S_FORUM_ORIG_CAT'            => (isset($old_forum_type) && $old_forum_type == FORUM_CAT) ? true : false,
669                    'S_FORUM_ORIG_LINK'            => (isset($old_forum_type) && $old_forum_type == FORUM_LINK) ? true : false,
670                    'S_FORUM_LINK'                => ($forum_data['forum_type'] == FORUM_LINK) ? true : false,
671                    'S_FORUM_CAT'                => ($forum_data['forum_type'] == FORUM_CAT) ? true : false,
672                    'S_ENABLE_INDEXING'            => ($forum_data['enable_indexing']) ? true : false,
673                    'S_TOPIC_ICONS'                => ($forum_data['enable_icons']) ? true : false,
674                    'S_DISPLAY_SUBFORUM_LIST'    => ($forum_data['display_subforum_list']) ? true : false,
675                    'S_DISPLAY_SUBFORUM_LIMIT'    => ($forum_data['display_subforum_limit']) ? true : false,
676                    'S_DISPLAY_ON_INDEX'        => ($forum_data['display_on_index']) ? true : false,
677                    'S_PRUNE_ENABLE'            => ($forum_data['enable_prune']) ? true : false,
678                    'S_PRUNE_SHADOW_ENABLE'            => ($forum_data['enable_shadow_prune']) ? true : false,
679                    'S_FORUM_LINK_TRACK'        => ($forum_data['forum_flags'] & FORUM_FLAG_LINK_TRACK) ? true : false,
680                    'S_PRUNE_OLD_POLLS'            => ($forum_data['forum_flags'] & FORUM_FLAG_PRUNE_POLL) ? true : false,
681                    'S_PRUNE_ANNOUNCE'            => ($forum_data['forum_flags'] & FORUM_FLAG_PRUNE_ANNOUNCE) ? true : false,
682                    'S_PRUNE_STICKY'            => ($forum_data['forum_flags'] & FORUM_FLAG_PRUNE_STICKY) ? true : false,
683                    'S_DISPLAY_ACTIVE_TOPICS'    => ($forum_data['forum_type'] == FORUM_POST) ? ($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) : true,
684                    'S_ENABLE_ACTIVE_TOPICS'    => ($forum_data['forum_type'] == FORUM_CAT) ? ($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) : false,
685                    'S_ENABLE_POST_REVIEW'        => ($forum_data['forum_flags'] & FORUM_FLAG_POST_REVIEW) ? true : false,
686                    'S_ENABLE_QUICK_REPLY'        => ($forum_data['forum_flags'] & FORUM_FLAG_QUICK_REPLY) ? true : false,
687                    'S_CAN_COPY_PERMISSIONS'    => ($action != 'edit' || empty($forum_id) || ($auth->acl_get('a_fauth') && $auth->acl_get('a_authusers') && $auth->acl_get('a_authgroups') && $auth->acl_get('a_mauth'))) ? true : false,
688                );
689
690                /**
691                * Modify forum template data before we display the form
692                *
693                * @event core.acp_manage_forums_display_form
694                * @var    string    action        Type of the action: add|edit
695                * @var    bool    update        Do we display the form only
696                *                            or did the user press submit
697                * @var    int        forum_id    When editing: the forum id,
698                *                            when creating: the parent forum id
699                * @var    array    row            Array with current forum data
700                *                            empty when creating new forum
701                * @var    array    forum_data    Array with new forum data
702                * @var    string    parents_list    List of parent options
703                * @var    array    errors        Array of errors, if you add errors
704                *                    ensure to update the template variables
705                *                    S_ERROR and ERROR_MSG to display it
706                * @var    array    template_data    Array with new forum data
707                * @since 3.1.0-a1
708                */
709                $vars = array(
710                    'action',
711                    'update',
712                    'forum_id',
713                    'row',
714                    'forum_data',
715                    'parents_list',
716                    'errors',
717                    'template_data',
718                );
719                extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_display_form', compact($vars)));
720
721                $template->assign_vars($template_data);
722
723                return;
724
725            break;
726
727            case 'delete':
728
729                if (!$forum_id)
730                {
731                    trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
732                }
733
734                $forum_data = $this->get_forum_info($forum_id);
735
736                $subforums_id = array();
737                $subforums = get_forum_branch($forum_id, 'children');
738
739                foreach ($subforums as $row)
740                {
741                    $subforums_id[] = $row['forum_id'];
742                }
743
744                $forums_list = make_forum_select($forum_data['parent_id'], $subforums_id);
745
746                $sql = 'SELECT forum_id
747                    FROM ' . FORUMS_TABLE . '
748                    WHERE forum_type = ' . FORUM_POST . "
749                        AND forum_id <> $forum_id";
750                $result = $db->sql_query_limit($sql, 1);
751
752                if ($db->sql_fetchrow($result))
753                {
754                    $template->assign_vars(array(
755                        'S_MOVE_FORUM_OPTIONS'        => make_forum_select($forum_data['parent_id'], $subforums_id, false, true)) // , false, true, false???
756                    );
757                }
758                $db->sql_freeresult($result);
759
760                $parent_id = ($this->parent_id == $forum_id) ? 0 : $this->parent_id;
761
762                $template->assign_vars(array(
763                    'S_DELETE_FORUM'        => true,
764                    'U_ACTION'                => $this->u_action . "&amp;parent_id={$parent_id}&amp;action=delete&amp;f=$forum_id",
765                    'U_BACK'                => $this->u_action . '&amp;parent_id=' . $this->parent_id,
766
767                    'FORUM_NAME'            => $forum_data['forum_name'],
768                    'S_FORUM_POST'            => ($forum_data['forum_type'] == FORUM_POST) ? true : false,
769                    'S_FORUM_LINK'            => ($forum_data['forum_type'] == FORUM_LINK) ? true : false,
770                    'S_HAS_SUBFORUMS'        => ($forum_data['right_id'] - $forum_data['left_id'] > 1) ? true : false,
771                    'S_FORUMS_LIST'            => $forums_list,
772                    'S_ERROR'                => (count($errors)) ? true : false,
773                    'ERROR_MSG'                => (count($errors)) ? implode('<br />', $errors) : '')
774                );
775
776                return;
777            break;
778
779            case 'copy_perm':
780                $forum_perm_from = $request->variable('forum_perm_from', 0);
781
782                // Copy permissions?
783                if (!empty($forum_perm_from) && $forum_perm_from != $forum_id)
784                {
785                    copy_forum_permissions($forum_perm_from, $forum_id, true);
786                    phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
787                    $auth->acl_clear_prefetch();
788                    $cache->destroy('sql', FORUMS_TABLE);
789
790                    $acl_url = '&amp;mode=setting_forum_local&amp;forum_id[]=' . $forum_id;
791
792                    $message = $user->lang['FORUM_UPDATED'];
793
794                    // Redirect to permissions
795                    if ($auth->acl_get('a_fauth'))
796                    {
797                        $message .= '<br /><br />' . sprintf($user->lang['REDIRECT_ACL'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=permissions' . $acl_url) . '">', '</a>');
798                    }
799
800                    trigger_error($message . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id));
801                }
802
803            break;
804        }
805
806        // Default management page
807        if (!$this->parent_id)
808        {
809            $navigation = $user->lang['FORUM_INDEX'];
810        }
811        else
812        {
813            $navigation = '<a href="' . $this->u_action . '">' . $user->lang['FORUM_INDEX'] . '</a>';
814
815            $forums_nav = get_forum_branch($this->parent_id, 'parents', 'descending');
816            foreach ($forums_nav as $row)
817            {
818                if ($row['forum_id'] == $this->parent_id)
819                {
820                    $navigation .= ' -&gt; ' . $row['forum_name'];
821                }
822                else
823                {
824                    $navigation .= ' -&gt; <a href="' . $this->u_action . '&amp;parent_id=' . $row['forum_id'] . '">' . $row['forum_name'] . '</a>';
825                }
826            }
827        }
828
829        // Jumpbox
830        $forum_box = make_forum_select($this->parent_id, false, false, false, false); //make_forum_select($this->parent_id);
831
832        if ($action == 'sync' || $action == 'sync_forum')
833        {
834            $template->assign_var('S_RESYNCED', true);
835        }
836
837        $sql = 'SELECT *
838            FROM ' . FORUMS_TABLE . "
839            WHERE parent_id = $this->parent_id
840            ORDER BY left_id";
841        $result = $db->sql_query($sql);
842
843        $rowset = array();
844        while ($row = $db->sql_fetchrow($result))
845        {
846            $rowset[(int) $row['forum_id']] = $row;
847        }
848        $db->sql_freeresult($result);
849
850        /**
851        * Modify the forum list data
852        *
853        * @event core.acp_manage_forums_modify_forum_list
854        * @var    array    rowset    Array with the forums list data
855        * @since 3.1.10-RC1
856        */
857        $vars = array('rowset');
858        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_modify_forum_list', compact($vars)));
859
860        if (!empty($rowset))
861        {
862            foreach ($rowset as $row)
863            {
864                $forum_type = $row['forum_type'];
865
866                $url = $this->u_action . "&amp;parent_id=$this->parent_id&amp;f={$row['forum_id']}";
867
868                $template->assign_block_vars('forums', array(
869                    'FORUM_IMAGE'        => ($row['forum_image']) ? '<img src="' . $phpbb_root_path . $row['forum_image'] . '" alt="" />' : '',
870                    'FORUM_IMAGE_SRC'    => ($row['forum_image']) ? $phpbb_root_path . $row['forum_image'] : '',
871                    'FORUM_NAME'        => $row['forum_name'],
872                    'FORUM_DESCRIPTION'    => generate_text_for_display($row['forum_desc'], $row['forum_desc_uid'], $row['forum_desc_bitfield'], $row['forum_desc_options']),
873                    'FORUM_TOPICS'        => $row['forum_topics_approved'],
874                    'FORUM_POSTS'        => $row['forum_posts_approved'],
875
876                    'S_FORUM_LINK'        => ($forum_type == FORUM_LINK) ? true : false,
877                    'S_FORUM_POST'        => ($forum_type == FORUM_POST) ? true : false,
878                    'S_FORUM_LOCKED'    => ($row['forum_status'] == ITEM_LOCKED) ? true : false,
879                    'S_SUBFORUMS'        => ($row['left_id'] + 1 != $row['right_id']) ? true : false,
880
881                    'U_FORUM'            => $this->u_action . '&amp;parent_id=' . $row['forum_id'],
882                    'U_MOVE_UP'            => $url . '&amp;action=move_up',
883                    'U_MOVE_DOWN'        => $url . '&amp;action=move_down',
884                    'U_EDIT'            => $url . '&amp;action=edit',
885                    'U_DELETE'            => $url . '&amp;action=delete',
886                    'U_SYNC'            => $url . '&amp;action=sync')
887                );
888            }
889        }
890        else if ($this->parent_id)
891        {
892            $row = $this->get_forum_info($this->parent_id);
893
894            $url = $this->u_action . '&amp;parent_id=' . $this->parent_id . '&amp;f=' . $row['forum_id'];
895
896            $template->assign_vars(array(
897                'S_NO_FORUMS'        => true,
898
899                'U_EDIT'            => $url . '&amp;action=edit',
900                'U_DELETE'            => $url . '&amp;action=delete',
901                'U_SYNC'            => $url . '&amp;action=sync')
902            );
903        }
904        unset($rowset);
905
906        $template->assign_vars(array(
907            'ERROR_MSG'        => (count($errors)) ? implode('<br />', $errors) : '',
908            'NAVIGATION'    => $navigation,
909            'FORUM_BOX'        => $forum_box,
910            'U_SEL_ACTION'    => $this->u_action,
911            'U_ACTION'        => $this->u_action . '&amp;parent_id=' . $this->parent_id,
912
913            'U_PROGRESS_BAR'    => $this->u_action . '&amp;action=progress_bar',
914            'UA_PROGRESS_BAR'    => addslashes($this->u_action . '&amp;action=progress_bar'),
915        ));
916    }
917
918    /**
919    * Get forum details
920    */
921    function get_forum_info($forum_id)
922    {
923        global $db;
924
925        $sql = 'SELECT *
926            FROM ' . FORUMS_TABLE . "
927            WHERE forum_id = $forum_id";
928        $result = $db->sql_query($sql);
929        $row = $db->sql_fetchrow($result);
930        $db->sql_freeresult($result);
931
932        if (!$row)
933        {
934            trigger_error("Forum #$forum_id does not exist", E_USER_ERROR);
935        }
936
937        return $row;
938    }
939
940    /**
941    * Update forum data
942    */
943    function update_forum_data(&$forum_data_ary)
944    {
945        global $db, $user, $cache, $phpbb_root_path, $phpbb_container, $phpbb_dispatcher, $phpbb_log, $request;
946
947        $errors = array();
948
949        $forum_data = $forum_data_ary;
950        /**
951        * Validate the forum data before we create/update the forum
952        *
953        * @event core.acp_manage_forums_validate_data
954        * @var    array    forum_data    Array with new forum data
955        * @var    array    errors        Array of errors, should be strings and not
956        *                            language key.
957        * @since 3.1.0-a1
958        */
959        $vars = array('forum_data', 'errors');
960        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_validate_data', compact($vars)));
961        $forum_data_ary = $forum_data;
962        unset($forum_data);
963
964        if ($forum_data_ary['forum_name'] == '')
965        {
966            $errors[] = $user->lang['FORUM_NAME_EMPTY'];
967        }
968
969        /**
970         * Replace Emojis and other 4bit UTF-8 chars not allowed by MySql to UCR / NCR.
971         * Using their Numeric Character Reference's Hexadecimal notation.
972         */
973        $forum_data_ary['forum_name'] = utf8_encode_ucr($forum_data_ary['forum_name']);
974
975        /**
976         * This should never happen again.
977         * Leaving the fallback here just in case there will be the need of it.
978         */
979        if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $forum_data_ary['forum_name'], $matches))
980        {
981            $character_list = implode('<br>', $matches[0]);
982
983            $errors[] = $user->lang('FORUM_NAME_EMOJI', $character_list);
984        }
985
986        if (utf8_strlen($forum_data_ary['forum_desc']) > 4000)
987        {
988            $errors[] = $user->lang['FORUM_DESC_TOO_LONG'];
989        }
990
991        if (utf8_strlen($forum_data_ary['forum_rules']) > 4000)
992        {
993            $errors[] = $user->lang['FORUM_RULES_TOO_LONG'];
994        }
995
996        if ($forum_data_ary['forum_password'] || $forum_data_ary['forum_password_confirm'])
997        {
998            if ($forum_data_ary['forum_password'] != $forum_data_ary['forum_password_confirm'])
999            {
1000                $forum_data_ary['forum_password'] = $forum_data_ary['forum_password_confirm'] = '';
1001                $errors[] = $user->lang['FORUM_PASSWORD_MISMATCH'];
1002            }
1003        }
1004
1005        if ($forum_data_ary['prune_days'] < 0 || $forum_data_ary['prune_viewed'] < 0 || $forum_data_ary['prune_freq'] < 0)
1006        {
1007            $forum_data_ary['prune_days'] = $forum_data_ary['prune_viewed'] = $forum_data_ary['prune_freq'] = 0;
1008            $errors[] = $user->lang['FORUM_DATA_NEGATIVE'];
1009        }
1010
1011        $range_test_ary = array(
1012            array('lang' => 'FORUM_TOPICS_PAGE', 'value' => $forum_data_ary['forum_topics_per_page'], 'column_type' => 'USINT:0'),
1013        );
1014
1015        if (!empty($forum_data_ary['forum_image']) && !file_exists($phpbb_root_path . $forum_data_ary['forum_image']))
1016        {
1017            $errors[] = $user->lang['FORUM_IMAGE_NO_EXIST'];
1018        }
1019
1020        validate_range($range_test_ary, $errors);
1021
1022        // Set forum flags
1023        // 1 = link tracking
1024        // 2 = prune old polls
1025        // 4 = prune announcements
1026        // 8 = prune stickies
1027        // 16 = show active topics
1028        // 32 = enable post review
1029        $forum_data_ary['forum_flags'] = 0;
1030        $forum_data_ary['forum_flags'] += ($forum_data_ary['forum_link_track']) ? FORUM_FLAG_LINK_TRACK : 0;
1031        $forum_data_ary['forum_flags'] += ($forum_data_ary['prune_old_polls']) ? FORUM_FLAG_PRUNE_POLL : 0;
1032        $forum_data_ary['forum_flags'] += ($forum_data_ary['prune_announce']) ? FORUM_FLAG_PRUNE_ANNOUNCE : 0;
1033        $forum_data_ary['forum_flags'] += ($forum_data_ary['prune_sticky']) ? FORUM_FLAG_PRUNE_STICKY : 0;
1034        $forum_data_ary['forum_flags'] += ($forum_data_ary['show_active']) ? FORUM_FLAG_ACTIVE_TOPICS : 0;
1035        $forum_data_ary['forum_flags'] += ($forum_data_ary['enable_post_review']) ? FORUM_FLAG_POST_REVIEW : 0;
1036        $forum_data_ary['forum_flags'] += ($forum_data_ary['enable_quick_reply']) ? FORUM_FLAG_QUICK_REPLY : 0;
1037
1038        // Unset data that are not database fields
1039        $forum_data_sql = $forum_data_ary;
1040
1041        unset($forum_data_sql['forum_link_track']);
1042        unset($forum_data_sql['prune_old_polls']);
1043        unset($forum_data_sql['prune_announce']);
1044        unset($forum_data_sql['prune_sticky']);
1045        unset($forum_data_sql['show_active']);
1046        unset($forum_data_sql['enable_post_review']);
1047        unset($forum_data_sql['enable_quick_reply']);
1048        unset($forum_data_sql['forum_password_confirm']);
1049
1050        // What are we going to do tonight Brain? The same thing we do everynight,
1051        // try to take over the world ... or decide whether to continue update
1052        // and if so, whether it's a new forum/cat/link or an existing one
1053        if (count($errors))
1054        {
1055            return $errors;
1056        }
1057
1058        // As we don't know the old password, it's kinda tricky to detect changes
1059        if ($forum_data_sql['forum_password_unset'])
1060        {
1061            $forum_data_sql['forum_password'] = '';
1062        }
1063        else if (empty($forum_data_sql['forum_password']))
1064        {
1065            unset($forum_data_sql['forum_password']);
1066        }
1067        else
1068        {
1069            // Instantiate passwords manager
1070            /* @var $passwords_manager \phpbb\passwords\manager */
1071            $passwords_manager = $phpbb_container->get('passwords.manager');
1072
1073            $forum_data_sql['forum_password'] = $passwords_manager->hash($forum_data_sql['forum_password']);
1074        }
1075        unset($forum_data_sql['forum_password_unset']);
1076
1077        $forum_data = $forum_data_ary;
1078        /**
1079        * Remove invalid values from forum_data_sql that should not be updated
1080        *
1081        * @event core.acp_manage_forums_update_data_before
1082        * @var    array    forum_data        Array with forum data
1083        * @var    array    forum_data_sql    Array with data we are going to update
1084        *                        If forum_data_sql[forum_id] is set, we update
1085        *                        that forum, otherwise a new one is created.
1086        * @since 3.1.0-a1
1087        */
1088        $vars = array('forum_data', 'forum_data_sql');
1089        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_update_data_before', compact($vars)));
1090        $forum_data_ary = $forum_data;
1091        unset($forum_data);
1092
1093        $is_new_forum = !isset($forum_data_sql['forum_id']);
1094
1095        if ($is_new_forum)
1096        {
1097            // no forum_id means we're creating a new forum
1098            unset($forum_data_sql['type_action']);
1099
1100            if ($forum_data_sql['parent_id'])
1101            {
1102                $sql = 'SELECT left_id, right_id, forum_type
1103                    FROM ' . FORUMS_TABLE . '
1104                    WHERE forum_id = ' . $forum_data_sql['parent_id'];
1105                $result = $db->sql_query($sql);
1106                $row = $db->sql_fetchrow($result);
1107                $db->sql_freeresult($result);
1108
1109                if (!$row)
1110                {
1111                    trigger_error($user->lang['PARENT_NOT_EXIST'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
1112                }
1113
1114                if ($row['forum_type'] == FORUM_LINK)
1115                {
1116                    $errors[] = $user->lang['PARENT_IS_LINK_FORUM'];
1117                    return $errors;
1118                }
1119
1120                $sql = 'UPDATE ' . FORUMS_TABLE . '
1121                    SET left_id = left_id + 2, right_id = right_id + 2
1122                    WHERE left_id > ' . $row['right_id'];
1123                $db->sql_query($sql);
1124
1125                $sql = 'UPDATE ' . FORUMS_TABLE . '
1126                    SET right_id = right_id + 2
1127                    WHERE ' . $row['left_id'] . ' BETWEEN left_id AND right_id';
1128                $db->sql_query($sql);
1129
1130                $forum_data_sql['left_id'] = $row['right_id'];
1131                $forum_data_sql['right_id'] = $row['right_id'] + 1;
1132            }
1133            else
1134            {
1135                $sql = 'SELECT MAX(right_id) AS right_id
1136                    FROM ' . FORUMS_TABLE;
1137                $result = $db->sql_query($sql);
1138                $row = $db->sql_fetchrow($result);
1139                $db->sql_freeresult($result);
1140
1141                $forum_data_sql['left_id'] = $row['right_id'] + 1;
1142                $forum_data_sql['right_id'] = $row['right_id'] + 2;
1143            }
1144
1145            $sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $forum_data_sql);
1146            $db->sql_query($sql);
1147
1148            $forum_data_ary['forum_id'] = $db->sql_nextid();
1149
1150            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_ADD', false, array($forum_data_ary['forum_name']));
1151        }
1152        else
1153        {
1154            $row = $this->get_forum_info($forum_data_sql['forum_id']);
1155
1156            if ($row['forum_type'] == FORUM_POST && $row['forum_type'] != $forum_data_sql['forum_type'])
1157            {
1158                // Has subforums and want to change into a link?
1159                if ($row['right_id'] - $row['left_id'] > 1 && $forum_data_sql['forum_type'] == FORUM_LINK)
1160                {
1161                    $errors[] = $user->lang['FORUM_WITH_SUBFORUMS_NOT_TO_LINK'];
1162                    return $errors;
1163                }
1164
1165                // we're turning a postable forum into a non-postable forum
1166                if ($forum_data_sql['type_action'] == 'move')
1167                {
1168                    $to_forum_id = $request->variable('to_forum_id', 0);
1169
1170                    if ($to_forum_id)
1171                    {
1172                        $errors = $this->move_forum_content($forum_data_sql['forum_id'], $to_forum_id);
1173                    }
1174                    else
1175                    {
1176                        return array($user->lang['NO_DESTINATION_FORUM']);
1177                    }
1178                }
1179                else if ($forum_data_sql['type_action'] == 'delete')
1180                {
1181                    $errors = $this->delete_forum_content($forum_data_sql['forum_id']);
1182                }
1183                else
1184                {
1185                    return array($user->lang['NO_FORUM_ACTION']);
1186                }
1187
1188                $forum_data_sql['forum_posts_approved'] = $forum_data_sql['forum_posts_unapproved'] = $forum_data_sql['forum_posts_softdeleted'] = $forum_data_sql['forum_topics_approved'] = $forum_data_sql['forum_topics_unapproved'] = $forum_data_sql['forum_topics_softdeleted'] = 0;
1189                $forum_data_sql['forum_last_post_id'] = $forum_data_sql['forum_last_poster_id'] = $forum_data_sql['forum_last_post_time'] = 0;
1190                $forum_data_sql['forum_last_poster_name'] = $forum_data_sql['forum_last_poster_colour'] = '';
1191            }
1192            else if ($row['forum_type'] == FORUM_CAT && $forum_data_sql['forum_type'] == FORUM_LINK)
1193            {
1194                // Has subforums?
1195                if ($row['right_id'] - $row['left_id'] > 1)
1196                {
1197                    // We are turning a category into a link - but need to decide what to do with the subforums.
1198                    $action_subforums = $request->variable('action_subforums', '');
1199                    $subforums_to_id = $request->variable('subforums_to_id', 0);
1200
1201                    if ($action_subforums == 'delete')
1202                    {
1203                        $rows = get_forum_branch($row['forum_id'], 'children', 'descending', false);
1204                        $forum_ids = [];
1205                        foreach ($rows as $_row)
1206                        {
1207                            // Do not remove the forum id we are about to change. ;)
1208                            if ($_row['forum_id'] == $row['forum_id'])
1209                            {
1210                                continue;
1211                            }
1212
1213                            $forum_ids[] = $_row['forum_id'];
1214                            $errors = array_merge($errors, $this->delete_forum_content($_row['forum_id']));
1215                        }
1216
1217                        if (count($errors))
1218                        {
1219                            return $errors;
1220                        }
1221
1222                        if (count($forum_ids))
1223                        {
1224                            $sql = 'DELETE FROM ' . FORUMS_TABLE . '
1225                                WHERE ' . $db->sql_in_set('forum_id', $forum_ids);
1226                            $db->sql_query($sql);
1227
1228                            $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
1229                                WHERE ' . $db->sql_in_set('forum_id', $forum_ids);
1230                            $db->sql_query($sql);
1231
1232                            $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
1233                                WHERE ' . $db->sql_in_set('forum_id', $forum_ids);
1234                            $db->sql_query($sql);
1235
1236                            // Delete forum ids from extension groups table
1237                            $sql = 'SELECT group_id, allowed_forums
1238                                FROM ' . EXTENSION_GROUPS_TABLE;
1239                            $result = $db->sql_query($sql);
1240
1241                            while ($_row = $db->sql_fetchrow($result))
1242                            {
1243                                if (!$_row['allowed_forums'])
1244                                {
1245                                    continue;
1246                                }
1247
1248                                $allowed_forums = unserialize(trim($_row['allowed_forums']));
1249                                $allowed_forums = array_diff($allowed_forums, $forum_ids);
1250
1251                                $sql = 'UPDATE ' . EXTENSION_GROUPS_TABLE . "
1252                                    SET allowed_forums = '" . ((count($allowed_forums)) ? serialize($allowed_forums) : '') . "'
1253                                    WHERE group_id = {$_row['group_id']}";
1254                                $db->sql_query($sql);
1255                            }
1256                            $db->sql_freeresult($result);
1257
1258                            $cache->destroy('_extensions');
1259                        }
1260                    }
1261                    else if ($action_subforums == 'move')
1262                    {
1263                        if (!$subforums_to_id)
1264                        {
1265                            return array($user->lang['NO_DESTINATION_FORUM']);
1266                        }
1267
1268                        $sql = 'SELECT forum_name
1269                            FROM ' . FORUMS_TABLE . '
1270                            WHERE forum_id = ' . $subforums_to_id;
1271                        $result = $db->sql_query($sql);
1272                        $_row = $db->sql_fetchrow($result);
1273                        $db->sql_freeresult($result);
1274
1275                        if (!$_row)
1276                        {
1277                            return array($user->lang['NO_FORUM']);
1278                        }
1279
1280                        $sql = 'SELECT forum_id
1281                            FROM ' . FORUMS_TABLE . "
1282                            WHERE parent_id = {$row['forum_id']}";
1283                        $result = $db->sql_query($sql);
1284
1285                        while ($_row = $db->sql_fetchrow($result))
1286                        {
1287                            $this->move_forum($_row['forum_id'], $subforums_to_id);
1288                        }
1289                        $db->sql_freeresult($result);
1290
1291                        $sql = 'UPDATE ' . FORUMS_TABLE . "
1292                            SET parent_id = $subforums_to_id
1293                            WHERE parent_id = {$row['forum_id']}";
1294                        $db->sql_query($sql);
1295                    }
1296
1297                    // Adjust the left/right id
1298                    $sql = 'UPDATE ' . FORUMS_TABLE . '
1299                        SET right_id = left_id + 1
1300                        WHERE forum_id = ' . $row['forum_id'];
1301                    $db->sql_query($sql);
1302                }
1303            }
1304            else if ($row['forum_type'] == FORUM_CAT && $forum_data_sql['forum_type'] == FORUM_POST)
1305            {
1306                // Changing a category to a forum? Reset the data (you can't post directly in a cat, you must use a forum)
1307                $forum_data_sql['forum_posts_approved'] = 0;
1308                $forum_data_sql['forum_posts_unapproved'] = 0;
1309                $forum_data_sql['forum_posts_softdeleted'] = 0;
1310                $forum_data_sql['forum_topics_approved'] = 0;
1311                $forum_data_sql['forum_topics_unapproved'] = 0;
1312                $forum_data_sql['forum_topics_softdeleted'] = 0;
1313                $forum_data_sql['forum_last_post_id'] = 0;
1314                $forum_data_sql['forum_last_post_subject'] = '';
1315                $forum_data_sql['forum_last_post_time'] = 0;
1316                $forum_data_sql['forum_last_poster_id'] = 0;
1317                $forum_data_sql['forum_last_poster_name'] = '';
1318                $forum_data_sql['forum_last_poster_colour'] = '';
1319            }
1320
1321            if (count($errors))
1322            {
1323                return $errors;
1324            }
1325
1326            if ($row['parent_id'] != $forum_data_sql['parent_id'])
1327            {
1328                if ($row['forum_id'] != $forum_data_sql['parent_id'])
1329                {
1330                    $errors = $this->move_forum($forum_data_sql['forum_id'], $forum_data_sql['parent_id']);
1331                }
1332                else
1333                {
1334                    $forum_data_sql['parent_id'] = $row['parent_id'];
1335                }
1336            }
1337
1338            if (count($errors))
1339            {
1340                return $errors;
1341            }
1342
1343            unset($forum_data_sql['type_action']);
1344
1345            if ($row['forum_name'] != $forum_data_sql['forum_name'])
1346            {
1347                // the forum name has changed, clear the parents list of all forums (for safety)
1348                $sql = 'UPDATE ' . FORUMS_TABLE . "
1349                    SET forum_parents = ''";
1350                $db->sql_query($sql);
1351            }
1352
1353            // Setting the forum id to the forum id is not really received well by some dbs. ;)
1354            $forum_id = $forum_data_sql['forum_id'];
1355            unset($forum_data_sql['forum_id']);
1356
1357            $sql = 'UPDATE ' . FORUMS_TABLE . '
1358                SET ' . $db->sql_build_array('UPDATE', $forum_data_sql) . '
1359                WHERE forum_id = ' . $forum_id;
1360            $db->sql_query($sql);
1361
1362            // Add it back
1363            $forum_data_ary['forum_id'] = $forum_id;
1364
1365            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_EDIT', false, array($forum_data_ary['forum_name']));
1366        }
1367
1368        $forum_data = $forum_data_ary;
1369        /**
1370        * Event after a forum was updated or created
1371        *
1372        * @event core.acp_manage_forums_update_data_after
1373        * @var    array    forum_data        Array with forum data
1374        * @var    array    forum_data_sql    Array with data we updated
1375        * @var    bool    is_new_forum    Did we create a forum or update one
1376        *                                If you want to overwrite this value,
1377        *                                ensure to set forum_data_sql[forum_id]
1378        * @var    array    errors        Array of errors, should be strings and not
1379        *                            language key.
1380        * @since 3.1.0-a1
1381        */
1382        $vars = array('forum_data', 'forum_data_sql', 'is_new_forum', 'errors');
1383        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_update_data_after', compact($vars)));
1384        $forum_data_ary = $forum_data;
1385        unset($forum_data);
1386
1387        return $errors;
1388    }
1389
1390    /**
1391    * Move forum
1392    */
1393    function move_forum($from_id, $to_id)
1394    {
1395        global $db, $user, $phpbb_dispatcher;
1396
1397        $errors = array();
1398
1399        // Check if we want to move to a parent with link type
1400        if ($to_id > 0)
1401        {
1402            $to_data = $this->get_forum_info($to_id);
1403
1404            if ($to_data['forum_type'] == FORUM_LINK)
1405            {
1406                $errors[] = $user->lang['PARENT_IS_LINK_FORUM'];
1407            }
1408        }
1409
1410        /**
1411        * Event when we move all children of one forum to another
1412        *
1413        * This event may be triggered, when a forum is deleted
1414        *
1415        * @event core.acp_manage_forums_move_children
1416        * @var    int        from_id        Id of the current parent forum
1417        * @var    int        to_id        Id of the new parent forum
1418        * @var    array    errors        Array of errors, should be strings and not
1419        *                            language key.
1420        * @since 3.1.0-a1
1421        */
1422        $vars = array('from_id', 'to_id', 'errors');
1423        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_move_children', compact($vars)));
1424
1425        // Return if there were errors
1426        if (!empty($errors))
1427        {
1428            return $errors;
1429        }
1430
1431        $db->sql_transaction('begin');
1432
1433        $moved_forums = get_forum_branch($from_id, 'children', 'descending');
1434        $from_data = $moved_forums[0];
1435        $diff = count($moved_forums) * 2;
1436
1437        $moved_ids = array();
1438        for ($i = 0, $size = count($moved_forums); $i < $size; ++$i)
1439        {
1440            $moved_ids[] = $moved_forums[$i]['forum_id'];
1441        }
1442
1443        // Resync parents
1444        $sql = 'UPDATE ' . FORUMS_TABLE . "
1445            SET right_id = right_id - $diff, forum_parents = ''
1446            WHERE left_id < " . $from_data['right_id'] . "
1447                AND right_id > " . $from_data['right_id'];
1448        $db->sql_query($sql);
1449
1450        // Resync righthand side of tree
1451        $sql = 'UPDATE ' . FORUMS_TABLE . "
1452            SET left_id = left_id - $diff, right_id = right_id - $diff, forum_parents = ''
1453            WHERE left_id > " . $from_data['right_id'];
1454        $db->sql_query($sql);
1455
1456        if ($to_id > 0)
1457        {
1458            // Retrieve $to_data again, it may have been changed...
1459            $to_data = $this->get_forum_info($to_id);
1460
1461            // Resync new parents
1462            $sql = 'UPDATE ' . FORUMS_TABLE . "
1463                SET right_id = right_id + $diff, forum_parents = ''
1464                WHERE " . $to_data['right_id'] . ' BETWEEN left_id AND right_id
1465                    AND ' . $db->sql_in_set('forum_id', $moved_ids, true);
1466            $db->sql_query($sql);
1467
1468            // Resync the righthand side of the tree
1469            $sql = 'UPDATE ' . FORUMS_TABLE . "
1470                SET left_id = left_id + $diff, right_id = right_id + $diff, forum_parents = ''
1471                WHERE left_id > " . $to_data['right_id'] . '
1472                    AND ' . $db->sql_in_set('forum_id', $moved_ids, true);
1473            $db->sql_query($sql);
1474
1475            // Resync moved branch
1476            $to_data['right_id'] += $diff;
1477
1478            if ($to_data['right_id'] > $from_data['right_id'])
1479            {
1480                $diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
1481            }
1482            else
1483            {
1484                $diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
1485            }
1486        }
1487        else
1488        {
1489            $sql = 'SELECT MAX(right_id) AS right_id
1490                FROM ' . FORUMS_TABLE . '
1491                WHERE ' . $db->sql_in_set('forum_id', $moved_ids, true);
1492            $result = $db->sql_query($sql);
1493            $row = $db->sql_fetchrow($result);
1494            $db->sql_freeresult($result);
1495
1496            $diff = '+ ' . ($row['right_id'] - $from_data['left_id'] + 1);
1497        }
1498
1499        $sql = 'UPDATE ' . FORUMS_TABLE . "
1500            SET left_id = left_id $diff, right_id = right_id $diff, forum_parents = ''
1501            WHERE " . $db->sql_in_set('forum_id', $moved_ids);
1502        $db->sql_query($sql);
1503
1504        $db->sql_transaction('commit');
1505
1506        return $errors;
1507    }
1508
1509    /**
1510    * Move forum content from one to another forum
1511    */
1512    function move_forum_content($from_id, $to_id, $sync = true)
1513    {
1514        global $db, $phpbb_dispatcher;
1515
1516        $errors = array();
1517
1518        /**
1519        * Event when we move content from one forum to another
1520        *
1521        * @event core.acp_manage_forums_move_content
1522        * @var    int        from_id        Id of the current parent forum
1523        * @var    int        to_id        Id of the new parent forum
1524        * @var    bool    sync        Shall we sync the "to"-forum's data
1525        * @var    array    errors        Array of errors, should be strings and not
1526        *                            language key. If this array is not empty,
1527        *                            The content will not be moved.
1528        * @since 3.1.0-a1
1529        */
1530        $vars = array('from_id', 'to_id', 'sync', 'errors');
1531        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_move_content', compact($vars)));
1532
1533        // Return if there were errors
1534        if (!empty($errors))
1535        {
1536            return $errors;
1537        }
1538
1539        $table_ary = array(LOG_TABLE, POSTS_TABLE, TOPICS_TABLE, DRAFTS_TABLE, TOPICS_TRACK_TABLE);
1540
1541        /**
1542         * Perform additional actions before move forum content
1543         *
1544         * @event core.acp_manage_forums_move_content_sql_before
1545         * @var    array    table_ary    Array of tables from which forum_id will be updated
1546         * @since 3.2.4-RC1
1547         */
1548        $vars = array('table_ary');
1549        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_move_content_sql_before', compact($vars)));
1550
1551        foreach ($table_ary as $table)
1552        {
1553            $sql = "UPDATE $table
1554                SET forum_id = $to_id
1555                WHERE forum_id = $from_id";
1556            $db->sql_query($sql);
1557        }
1558        unset($table_ary);
1559
1560        $table_ary = array(FORUMS_ACCESS_TABLE, FORUMS_TRACK_TABLE, FORUMS_WATCH_TABLE, MODERATOR_CACHE_TABLE);
1561
1562        foreach ($table_ary as $table)
1563        {
1564            $sql = "DELETE FROM $table
1565                WHERE forum_id = $from_id";
1566            $db->sql_query($sql);
1567        }
1568
1569        /**
1570         * Event when content has been moved from one forum to another
1571         *
1572         * @event core.acp_manage_forums_move_content_after
1573         * @var    int        from_id        Id of the current parent forum
1574         * @var    int        to_id        Id of the new parent forum
1575         * @var    bool    sync        Shall we sync the "to"-forum's data
1576         *
1577         * @since 3.2.9-RC1
1578         */
1579        $vars = array('from_id', 'to_id', 'sync');
1580        extract($phpbb_dispatcher->trigger_event('core.acp_manage_forums_move_content_after', compact($vars)));
1581
1582        if ($sync)
1583        {
1584            // Delete ghost topics that link back to the same forum then resync counters
1585            sync('topic_moved');
1586            sync('forum', 'forum_id', $to_id, false, true);
1587        }
1588
1589        return array();
1590    }
1591
1592    /**
1593    * Remove complete forum
1594    */
1595    function delete_forum($forum_id, $action_posts = 'delete', $action_subforums = 'delete', $posts_to_id = 0, $subforums_to_id = 0)
1596    {
1597        global $db, $user, $cache, $phpbb_log;
1598
1599        $forum_data = $this->get_forum_info($forum_id);
1600
1601        $errors = array();
1602        $log_action_posts = $log_action_forums = $posts_to_name = $subforums_to_name = '';
1603        $forum_ids = array($forum_id);
1604
1605        if ($action_posts == 'delete')
1606        {
1607            $log_action_posts = 'POSTS';
1608            $errors = array_merge($errors, $this->delete_forum_content($forum_id));
1609        }
1610        else if ($action_posts == 'move')
1611        {
1612            if (!$posts_to_id)
1613            {
1614                $errors[] = $user->lang['NO_DESTINATION_FORUM'];
1615            }
1616            else
1617            {
1618                $log_action_posts = 'MOVE_POSTS';
1619
1620                $sql = 'SELECT forum_name
1621                    FROM ' . FORUMS_TABLE . '
1622                    WHERE forum_id = ' . $posts_to_id;
1623                $result = $db->sql_query($sql);
1624                $row = $db->sql_fetchrow($result);
1625                $db->sql_freeresult($result);
1626
1627                if (!$row)
1628                {
1629                    $errors[] = $user->lang['NO_FORUM'];
1630                }
1631                else
1632                {
1633                    $posts_to_name = $row['forum_name'];
1634                    $errors = array_merge($errors, $this->move_forum_content($forum_id, $posts_to_id));
1635                }
1636            }
1637        }
1638
1639        if (count($errors))
1640        {
1641            return $errors;
1642        }
1643
1644        if ($action_subforums == 'delete')
1645        {
1646            $log_action_forums = 'FORUMS';
1647            $rows = get_forum_branch($forum_id, 'children', 'descending', false);
1648
1649            foreach ($rows as $row)
1650            {
1651                $forum_ids[] = $row['forum_id'];
1652                $errors = array_merge($errors, $this->delete_forum_content($row['forum_id']));
1653            }
1654
1655            if (count($errors))
1656            {
1657                return $errors;
1658            }
1659
1660            $diff = count($forum_ids) * 2;
1661
1662            $sql = 'DELETE FROM ' . FORUMS_TABLE . '
1663                WHERE ' . $db->sql_in_set('forum_id', $forum_ids);
1664            $db->sql_query($sql);
1665
1666            $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
1667                WHERE ' . $db->sql_in_set('forum_id', $forum_ids);
1668            $db->sql_query($sql);
1669
1670            $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
1671                WHERE ' . $db->sql_in_set('forum_id', $forum_ids);
1672            $db->sql_query($sql);
1673        }
1674        else if ($action_subforums == 'move')
1675        {
1676            if (!$subforums_to_id)
1677            {
1678                $errors[] = $user->lang['NO_DESTINATION_FORUM'];
1679            }
1680            else
1681            {
1682                $log_action_forums = 'MOVE_FORUMS';
1683
1684                $sql = 'SELECT forum_name
1685                    FROM ' . FORUMS_TABLE . '
1686                    WHERE forum_id = ' . $subforums_to_id;
1687                $result = $db->sql_query($sql);
1688                $row = $db->sql_fetchrow($result);
1689                $db->sql_freeresult($result);
1690
1691                if (!$row)
1692                {
1693                    $errors[] = $user->lang['NO_FORUM'];
1694                }
1695                else
1696                {
1697                    $subforums_to_name = $row['forum_name'];
1698
1699                    $sql = 'SELECT forum_id
1700                        FROM ' . FORUMS_TABLE . "
1701                        WHERE parent_id = $forum_id";
1702                    $result = $db->sql_query($sql);
1703
1704                    while ($row = $db->sql_fetchrow($result))
1705                    {
1706                        $this->move_forum($row['forum_id'], $subforums_to_id);
1707                    }
1708                    $db->sql_freeresult($result);
1709
1710                    // Grab new forum data for correct tree updating later
1711                    $forum_data = $this->get_forum_info($forum_id);
1712
1713                    $sql = 'UPDATE ' . FORUMS_TABLE . "
1714                        SET parent_id = $subforums_to_id
1715                        WHERE parent_id = $forum_id";
1716                    $db->sql_query($sql);
1717
1718                    $diff = 2;
1719                    $sql = 'DELETE FROM ' . FORUMS_TABLE . "
1720                        WHERE forum_id = $forum_id";
1721                    $db->sql_query($sql);
1722
1723                    $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . "
1724                        WHERE forum_id = $forum_id";
1725                    $db->sql_query($sql);
1726
1727                    $sql = 'DELETE FROM ' . ACL_USERS_TABLE . "
1728                        WHERE forum_id = $forum_id";
1729                    $db->sql_query($sql);
1730                }
1731            }
1732
1733            if (count($errors))
1734            {
1735                return $errors;
1736            }
1737        }
1738        else
1739        {
1740            $diff = 2;
1741            $sql = 'DELETE FROM ' . FORUMS_TABLE . "
1742                WHERE forum_id = $forum_id";
1743            $db->sql_query($sql);
1744
1745            $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . "
1746                WHERE forum_id = $forum_id";
1747            $db->sql_query($sql);
1748
1749            $sql = 'DELETE FROM ' . ACL_USERS_TABLE . "
1750                WHERE forum_id = $forum_id";
1751            $db->sql_query($sql);
1752        }
1753
1754        // Resync tree
1755        $sql = 'UPDATE ' . FORUMS_TABLE . "
1756            SET right_id = right_id - $diff
1757            WHERE left_id < {$forum_data['right_id']} AND right_id > {$forum_data['right_id']}";
1758        $db->sql_query($sql);
1759
1760        $sql = 'UPDATE ' . FORUMS_TABLE . "
1761            SET left_id = left_id - $diff, right_id = right_id - $diff
1762            WHERE left_id > {$forum_data['right_id']}";
1763        $db->sql_query($sql);
1764
1765        // Delete forum ids from extension groups table
1766        $sql = 'SELECT group_id, allowed_forums
1767            FROM ' . EXTENSION_GROUPS_TABLE;
1768        $result = $db->sql_query($sql);
1769
1770        while ($row = $db->sql_fetchrow($result))
1771        {
1772            if (!$row['allowed_forums'])
1773            {
1774                continue;
1775            }
1776
1777            $allowed_forums = unserialize(trim($row['allowed_forums']));
1778            $allowed_forums = array_diff($allowed_forums, $forum_ids);
1779
1780            $sql = 'UPDATE ' . EXTENSION_GROUPS_TABLE . "
1781                SET allowed_forums = '" . ((count($allowed_forums)) ? serialize($allowed_forums) : '') . "'
1782                WHERE group_id = {$row['group_id']}";
1783            $db->sql_query($sql);
1784        }
1785        $db->sql_freeresult($result);
1786
1787        $cache->destroy('_extensions');
1788
1789        $log_action = implode('_', array($log_action_posts, $log_action_forums));
1790
1791        switch ($log_action)
1792        {
1793            case 'MOVE_POSTS_MOVE_FORUMS':
1794                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_MOVE_POSTS_MOVE_FORUMS', false, array($posts_to_name, $subforums_to_name, $forum_data['forum_name']));
1795            break;
1796
1797            case 'MOVE_POSTS_FORUMS':
1798                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_MOVE_POSTS_FORUMS', false, array($posts_to_name, $forum_data['forum_name']));
1799            break;
1800
1801            case 'POSTS_MOVE_FORUMS':
1802                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_POSTS_MOVE_FORUMS', false, array($subforums_to_name, $forum_data['forum_name']));
1803            break;
1804
1805            case '_MOVE_FORUMS':
1806                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_MOVE_FORUMS', false, array($subforums_to_name, $forum_data['forum_name']));
1807            break;
1808
1809            case 'MOVE_POSTS_':
1810                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_MOVE_POSTS', false, array($posts_to_name, $forum_data['forum_name']));
1811            break;
1812
1813            case 'POSTS_FORUMS':
1814                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_POSTS_FORUMS', false, array($forum_data['forum_name']));
1815            break;
1816
1817            case '_FORUMS':
1818                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_FORUMS', false, array($forum_data['forum_name']));
1819            break;
1820
1821            case 'POSTS_':
1822                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_POSTS', false, array($forum_data['forum_name']));
1823            break;
1824
1825            default:
1826                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_FORUM_DEL_FORUM', false, array($forum_data['forum_name']));
1827            break;
1828        }
1829
1830        return $errors;
1831    }
1832
1833    /**
1834    * Delete forum content
1835    */
1836    function delete_forum_content($forum_id)
1837    {
1838        global $db, $config, $phpbb_root_path, $phpEx, $phpbb_container, $phpbb_dispatcher;
1839
1840        include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
1841
1842        $db->sql_transaction('begin');
1843
1844        // Select then delete all attachments
1845        $sql = 'SELECT a.topic_id
1846            FROM ' . POSTS_TABLE . ' p, ' . ATTACHMENTS_TABLE . " a
1847            WHERE p.forum_id = $forum_id
1848                AND a.in_message = 0
1849                AND a.topic_id = p.topic_id";
1850        $result = $db->sql_query($sql);
1851
1852        $topic_ids = array();
1853        while ($row = $db->sql_fetchrow($result))
1854        {
1855            $topic_ids[] = $row['topic_id'];
1856        }
1857        $db->sql_freeresult($result);
1858
1859        /** @var \phpbb\attachment\manager $attachment_manager */
1860        $attachment_manager = $phpbb_container->get('attachment.manager');
1861        $attachment_manager->delete('topic', $topic_ids, false);
1862        unset($attachment_manager);
1863
1864        // Delete shadow topics pointing to topics in this forum
1865        delete_topic_shadows($forum_id);
1866
1867        // Before we remove anything we make sure we are able to adjust the post counts later. ;)
1868        $sql = 'SELECT poster_id
1869            FROM ' . POSTS_TABLE . '
1870            WHERE forum_id = ' . $forum_id . '
1871                AND post_postcount = 1
1872                AND post_visibility = ' . ITEM_APPROVED;
1873        $result = $db->sql_query($sql);
1874
1875        $post_counts = array();
1876        while ($row = $db->sql_fetchrow($result))
1877        {
1878            $post_counts[$row['poster_id']] = (!empty($post_counts[$row['poster_id']])) ? $post_counts[$row['poster_id']] + 1 : 1;
1879        }
1880        $db->sql_freeresult($result);
1881
1882        switch ($db->get_sql_layer())
1883        {
1884            case 'mysqli':
1885
1886                // Delete everything else and thank MySQL for offering multi-table deletion
1887                $tables_ary = array(
1888                    SEARCH_WORDMATCH_TABLE    => 'post_id',
1889                    REPORTS_TABLE            => 'post_id',
1890                    WARNINGS_TABLE            => 'post_id',
1891                    BOOKMARKS_TABLE            => 'topic_id',
1892                    TOPICS_WATCH_TABLE        => 'topic_id',
1893                    TOPICS_POSTED_TABLE        => 'topic_id',
1894                    POLL_OPTIONS_TABLE        => 'topic_id',
1895                    POLL_VOTES_TABLE        => 'topic_id',
1896                );
1897
1898                $sql = 'DELETE ' . POSTS_TABLE;
1899                $sql_using = "\nFROM " . POSTS_TABLE;
1900                $sql_where = "\nWHERE " . POSTS_TABLE . ".forum_id = $forum_id\n";
1901
1902                foreach ($tables_ary as $table => $field)
1903                {
1904                    $sql .= "$table ";
1905                    $sql_using .= "$table ";
1906                    $sql_where .= "\nAND $table.$field = " . POSTS_TABLE . ".$field";
1907                }
1908
1909                $db->sql_query($sql . $sql_using . $sql_where);
1910
1911            break;
1912
1913            default:
1914
1915                // Delete everything else and curse your DB for not offering multi-table deletion
1916                $tables_ary = array(
1917                    'post_id'    =>    array(
1918                        SEARCH_WORDMATCH_TABLE,
1919                        REPORTS_TABLE,
1920                        WARNINGS_TABLE,
1921                    ),
1922
1923                    'topic_id'    =>    array(
1924                        BOOKMARKS_TABLE,
1925                        TOPICS_WATCH_TABLE,
1926                        TOPICS_POSTED_TABLE,
1927                        POLL_OPTIONS_TABLE,
1928                        POLL_VOTES_TABLE,
1929                    )
1930                );
1931
1932                // Amount of rows we select and delete in one iteration.
1933                $batch_size = 500;
1934
1935                foreach ($tables_ary as $field => $tables)
1936                {
1937                    $start = 0;
1938
1939                    do
1940                    {
1941                        $sql = "SELECT $field
1942                            FROM " . POSTS_TABLE . '
1943                            WHERE forum_id = ' . $forum_id;
1944                        $result = $db->sql_query_limit($sql, $batch_size, $start);
1945
1946                        $ids = array();
1947                        while ($row = $db->sql_fetchrow($result))
1948                        {
1949                            $ids[] = $row[$field];
1950                        }
1951                        $db->sql_freeresult($result);
1952
1953                        if (count($ids))
1954                        {
1955                            $start += count($ids);
1956
1957                            foreach ($tables as $table)
1958                            {
1959                                $db->sql_query("DELETE FROM $table WHERE " . $db->sql_in_set($field, $ids));
1960                            }
1961                        }
1962                    }
1963                    while (count($ids) == $batch_size);
1964                }
1965                unset($ids);
1966
1967            break;
1968        }
1969
1970        $table_ary = array(FORUMS_ACCESS_TABLE, FORUMS_TRACK_TABLE, FORUMS_WATCH_TABLE, LOG_TABLE, MODERATOR_CACHE_TABLE, POSTS_TABLE, TOPICS_TABLE, TOPICS_TRACK_TABLE);
1971
1972        /**
1973         * Perform additional actions before forum content deletion
1974         *
1975         * @event core.delete_forum_content_before_query
1976         * @var    array    table_ary    Array of tables from which all rows will be deleted that hold the forum_id
1977         * @var    int        forum_id    the forum id
1978         * @var    array    topic_ids    Array of the topic ids from the forum to be deleted
1979         * @var    array    post_counts    Array of counts of posts in the forum, by poster_id
1980         * @since 3.1.6-RC1
1981         */
1982        $vars = array(
1983                'table_ary',
1984                'forum_id',
1985                'topic_ids',
1986                'post_counts',
1987        );
1988        extract($phpbb_dispatcher->trigger_event('core.delete_forum_content_before_query', compact($vars)));
1989
1990        foreach ($table_ary as $table)
1991        {
1992            $db->sql_query("DELETE FROM $table WHERE forum_id = $forum_id");
1993        }
1994
1995        // Set forum ids to 0
1996        $table_ary = array(DRAFTS_TABLE);
1997
1998        foreach ($table_ary as $table)
1999        {
2000            $db->sql_query("UPDATE $table SET forum_id = 0 WHERE forum_id = $forum_id");
2001        }
2002
2003        // Adjust users post counts
2004        if (count($post_counts))
2005        {
2006            foreach ($post_counts as $poster_id => $substract)
2007            {
2008                $sql = 'UPDATE ' . USERS_TABLE . '
2009                    SET user_posts = 0
2010                    WHERE user_id = ' . $poster_id . '
2011                    AND user_posts < ' . $substract;
2012                $db->sql_query($sql);
2013
2014                $sql = 'UPDATE ' . USERS_TABLE . '
2015                    SET user_posts = user_posts - ' . $substract . '
2016                    WHERE user_id = ' . $poster_id . '
2017                    AND user_posts >= ' . $substract;
2018                $db->sql_query($sql);
2019            }
2020        }
2021
2022        $db->sql_transaction('commit');
2023
2024        // Make sure the overall post/topic count is correct...
2025        $sql = 'SELECT COUNT(post_id) AS stat
2026            FROM ' . POSTS_TABLE . '
2027            WHERE post_visibility = ' . ITEM_APPROVED;
2028        $result = $db->sql_query($sql);
2029        $row = $db->sql_fetchrow($result);
2030        $db->sql_freeresult($result);
2031
2032        $config->set('num_posts', (int) $row['stat'], false);
2033
2034        $sql = 'SELECT COUNT(topic_id) AS stat
2035            FROM ' . TOPICS_TABLE . '
2036            WHERE topic_visibility = ' . ITEM_APPROVED;
2037        $result = $db->sql_query($sql);
2038        $row = $db->sql_fetchrow($result);
2039        $db->sql_freeresult($result);
2040
2041        $config->set('num_topics', (int) $row['stat'], false);
2042
2043        $sql = 'SELECT COUNT(attach_id) as stat
2044            FROM ' . ATTACHMENTS_TABLE;
2045        $result = $db->sql_query($sql);
2046        $row = $db->sql_fetchrow($result);
2047        $db->sql_freeresult($result);
2048
2049        $config->set('num_files', (int) $row['stat'], false);
2050
2051        $sql = 'SELECT SUM(filesize) as stat
2052            FROM ' . ATTACHMENTS_TABLE;
2053        $result = $db->sql_query($sql);
2054        $row = $db->sql_fetchrow($result);
2055        $db->sql_freeresult($result);
2056
2057        $config->set('upload_dir_size', (float) $row['stat'], false);
2058
2059        return array();
2060    }
2061
2062    /**
2063    * Move forum position by $steps up/down
2064    */
2065    function move_forum_by($forum_row, $action = 'move_up', $steps = 1)
2066    {
2067        global $db;
2068
2069        /**
2070        * Fetch all the siblings between the module's current spot
2071        * and where we want to move it to. If there are less than $steps
2072        * siblings between the current spot and the target then the
2073        * module will move as far as possible
2074        */
2075        $sql = 'SELECT forum_id, forum_name, left_id, right_id
2076            FROM ' . FORUMS_TABLE . "
2077            WHERE parent_id = {$forum_row['parent_id']}
2078                AND " . (($action == 'move_up') ? "right_id < {$forum_row['right_id']} ORDER BY right_id DESC" : "left_id > {$forum_row['left_id']} ORDER BY left_id ASC");
2079        $result = $db->sql_query_limit($sql, $steps);
2080
2081        $target = array();
2082        while ($row = $db->sql_fetchrow($result))
2083        {
2084            $target = $row;
2085        }
2086        $db->sql_freeresult($result);
2087
2088        if (!count($target))
2089        {
2090            // The forum is already on top or bottom
2091            return false;
2092        }
2093
2094        /**
2095        * $left_id and $right_id define the scope of the nodes that are affected by the move.
2096        * $diff_up and $diff_down are the values to substract or add to each node's left_id
2097        * and right_id in order to move them up or down.
2098        * $move_up_left and $move_up_right define the scope of the nodes that are moving
2099        * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
2100        */
2101        if ($action == 'move_up')
2102        {
2103            $left_id = $target['left_id'];
2104            $right_id = $forum_row['right_id'];
2105
2106            $diff_up = $forum_row['left_id'] - $target['left_id'];
2107            $diff_down = $forum_row['right_id'] + 1 - $forum_row['left_id'];
2108
2109            $move_up_left = $forum_row['left_id'];
2110            $move_up_right = $forum_row['right_id'];
2111        }
2112        else
2113        {
2114            $left_id = $forum_row['left_id'];
2115            $right_id = $target['right_id'];
2116
2117            $diff_up = $forum_row['right_id'] + 1 - $forum_row['left_id'];
2118            $diff_down = $target['right_id'] - $forum_row['right_id'];
2119
2120            $move_up_left = $forum_row['right_id'] + 1;
2121            $move_up_right = $target['right_id'];
2122        }
2123
2124        // Now do the dirty job
2125        $sql = 'UPDATE ' . FORUMS_TABLE . "
2126            SET left_id = left_id + CASE
2127                WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
2128                ELSE {$diff_down}
2129            END,
2130            right_id = right_id + CASE
2131                WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
2132                ELSE {$diff_down}
2133            END,
2134            forum_parents = ''
2135            WHERE
2136                left_id BETWEEN {$left_id} AND {$right_id}
2137                AND right_id BETWEEN {$left_id} AND {$right_id}";
2138        $db->sql_query($sql);
2139
2140        return $target['forum_name'];
2141    }
2142
2143    /**
2144    * Display progress bar for syncinc forums
2145    */
2146    function display_progress_bar($start, $total)
2147    {
2148        global $template, $user;
2149
2150        adm_page_header($user->lang['SYNC_IN_PROGRESS']);
2151
2152        $template->set_filenames(array(
2153            'body'    => 'progress_bar.html')
2154        );
2155
2156        $template->assign_vars(array(
2157            'L_PROGRESS'            => $user->lang['SYNC_IN_PROGRESS'],
2158            'L_PROGRESS_EXPLAIN'    => ($start && $total) ? sprintf($user->lang['SYNC_IN_PROGRESS_EXPLAIN'], $start, $total) : $user->lang['SYNC_IN_PROGRESS'])
2159        );
2160
2161        adm_page_footer();
2162    }
2163}