[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

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


Generated: Wed Oct 2 15:03:47 2013 Cross-referenced by PHPXref 0.7.1