[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/includes/acp/ -> acp_forums.php (source)

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1