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