[ Index ]

PHP Cross Reference of MyBB 1.8.22

title

Body

[close]

/admin/modules/forum/ -> management.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  // Disallow direct access to this file for security reasons
  12  if(!defined("IN_MYBB"))
  13  {
  14      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  15  }
  16  
  17  $page->add_breadcrumb_item($lang->forum_management, "index.php?module=forum-management");
  18  
  19  if($mybb->input['action'] == "add" || $mybb->input['action'] == "edit" || $mybb->input['action'] == "copy" || $mybb->input['action'] == "permissions" || !$mybb->input['action'])
  20  {
  21      if(!empty($mybb->input['fid']) && ($mybb->input['action'] == "management" || $mybb->input['action'] == "edit" || $mybb->input['action'] == "copy" || !$mybb->input['action']))
  22      {
  23          $sub_tabs['view_forum'] = array(
  24              'title' => $lang->view_forum,
  25              'link' => "index.php?module=forum-management&amp;fid=".$mybb->input['fid'],
  26              'description' => $lang->view_forum_desc
  27          );
  28  
  29          $sub_tabs['add_child_forum'] = array(
  30              'title' => $lang->add_child_forum,
  31              'link' => "index.php?module=forum-management&amp;action=add&amp;pid=".$mybb->input['fid'],
  32              'description' => $lang->view_forum_desc
  33          );
  34  
  35          $sub_tabs['edit_forum_settings'] = array(
  36              'title' => $lang->edit_forum_settings,
  37              'link' => "index.php?module=forum-management&amp;action=edit&amp;fid=".$mybb->input['fid'],
  38              'description' => $lang->edit_forum_settings_desc
  39          );
  40  
  41          $sub_tabs['copy_forum'] = array(
  42              'title' => $lang->copy_forum,
  43              'link' => "index.php?module=forum-management&amp;action=copy&amp;fid=".$mybb->input['fid'],
  44              'description' => $lang->copy_forum_desc
  45          );
  46      }
  47      else
  48      {
  49          $sub_tabs['forum_management'] = array(
  50              'title' => $lang->forum_management,
  51              'link' => "index.php?module=forum-management",
  52              'description' => $lang->forum_management_desc
  53          );
  54  
  55          $sub_tabs['add_forum'] = array(
  56              'title' => $lang->add_forum,
  57              'link' => "index.php?module=forum-management&amp;action=add",
  58              'description' => $lang->add_forum_desc
  59          );
  60      }
  61  }
  62  
  63  $plugins->run_hooks("admin_forum_management_begin");
  64  
  65  if($mybb->input['action'] == "copy")
  66  {
  67      $plugins->run_hooks("admin_forum_management_copy");
  68  
  69      if($mybb->request_method == "post")
  70      {
  71          $from = $mybb->get_input('from', MyBB::INPUT_INT);
  72          $to = $mybb->get_input('to', MyBB::INPUT_INT);
  73  
  74          // Find the source forum
  75          $query = $db->simple_select("forums", '*', "fid='{$from}'");
  76          $from_forum = $db->fetch_array($query);
  77          if(!$db->num_rows($query))
  78          {
  79              $errors[] = $lang->error_invalid_source_forum;
  80          }
  81  
  82          if($to == -1)
  83          {
  84              // Create a new forum
  85              if(empty($mybb->input['title']))
  86              {
  87                  $errors[] = $lang->error_new_forum_needs_name;
  88              }
  89  
  90              if($mybb->input['pid'] == -1 && $mybb->input['type'] == 'f')
  91              {
  92                  $errors[] = $lang->error_no_parent;
  93              }
  94  
  95              if(!$errors)
  96              {
  97                  $new_forum = $from_forum;
  98                  unset($new_forum['fid'], $new_forum['threads'], $new_forum['posts'], $new_forum['lastpost'], $new_forum['lastposter'], $new_forum['lastposteruid'], $new_forum['lastposttid'], $new_forum['lastpostsubject'], $new_forum['unapprovedthreads'], $new_forum['unapprovedposts']);
  99                  $new_forum['name'] = $db->escape_string($mybb->input['title']);
 100                  $new_forum['description'] = $db->escape_string($mybb->input['description']);
 101                  $new_forum['type'] = $db->escape_string($mybb->input['type']);
 102                  $new_forum['pid'] = $mybb->get_input('pid', MyBB::INPUT_INT);
 103                  $new_forum['rulestitle'] = $db->escape_string($new_forum['rulestitle']);
 104                  $new_forum['rules'] = $db->escape_string($new_forum['rules']);
 105                  $new_forum['parentlist'] = '';
 106  
 107                  $to = $db->insert_query("forums", $new_forum);
 108  
 109                  // Generate parent list
 110                  $parentlist = make_parent_list($to);
 111                  $updatearray = array(
 112                      'parentlist' => $parentlist
 113                  );
 114                  $db->update_query("forums", $updatearray, "fid='{$to}'");
 115              }
 116          }
 117          elseif($mybb->input['copyforumsettings'] == 1)
 118          {
 119              // Copy settings to existing forum
 120              $query = $db->simple_select("forums", '*', "fid='{$to}'");
 121              $to_forum = $db->fetch_array($query);
 122              if(!$db->num_rows($query))
 123              {
 124                  $errors[] = $lang->error_invalid_destination_forum;
 125              }
 126  
 127              if(!$errors)
 128              {
 129                  $new_forum = $from_forum;
 130                  unset($new_forum['fid'], $new_forum['threads'], $new_forum['posts'], $new_forum['lastpost'], $new_forum['lastposter'], $new_forum['lastposteruid'], $new_forum['lastposttid'], $new_forum['lastpostsubject'], $new_forum['unapprovedthreads'], $new_forum['unapprovedposts']);
 131                  $new_forum['name'] = $db->escape_string($to_forum['name']);
 132                  $new_forum['description'] = $db->escape_string($to_forum['description']);
 133                  $new_forum['pid'] = $db->escape_string($to_forum['pid']);
 134                  $new_forum['parentlist'] = $db->escape_string($to_forum['parentlist']);
 135                  $new_forum['rulestitle'] = $db->escape_string($new_forum['rulestitle']);
 136                  $new_forum['rules'] = $db->escape_string($new_forum['rules']);
 137  
 138                  $db->update_query("forums", $new_forum, "fid='{$to}'");
 139              }
 140          }
 141  
 142          if(!$errors)
 143          {
 144              // Copy permissions
 145              if(is_array($mybb->input['copygroups']) && count($mybb->input['copygroups'] > 0))
 146              {
 147                  foreach($mybb->input['copygroups'] as $gid)
 148                  {
 149                      $groups[] = (int)$gid;
 150                  }
 151                  $groups = implode(',', $groups);
 152                  $query = $db->simple_select("forumpermissions", '*', "fid='{$from}' AND gid IN ({$groups})");
 153                  $db->delete_query("forumpermissions", "fid='{$to}' AND gid IN ({$groups})", 1);
 154                  while($permissions = $db->fetch_array($query))
 155                  {
 156                      unset($permissions['pid']);
 157                      $permissions['fid'] = $to;
 158  
 159                      $db->insert_query("forumpermissions", $permissions);
 160                  }
 161  
 162                  // Log admin action
 163                  log_admin_action($from, $from_forum['name'], $to, $new_forum['name'], $groups);
 164              }
 165              else
 166              {
 167                  // Log admin action (no group permissions)
 168                  log_admin_action($from, $from_forum['name'], $to, $new_forum['name']);
 169              }
 170  
 171              $plugins->run_hooks("admin_forum_management_copy_commit");
 172  
 173              $cache->update_forums();
 174              $cache->update_forumpermissions();
 175  
 176              flash_message($lang->success_forum_copied, 'success');
 177              admin_redirect("index.php?module=forum-management&action=edit&fid={$to}");
 178          }
 179      }
 180  
 181      $page->add_breadcrumb_item($lang->copy_forum);
 182      $page->output_header($lang->copy_forum);
 183      $page->output_nav_tabs($sub_tabs, 'copy_forum');
 184  
 185      $form = new Form("index.php?module=forum-management&amp;action=copy", "post");
 186  
 187      if($errors)
 188      {
 189          $page->output_inline_error($errors);
 190          $copy_data = $mybb->input;
 191      }
 192      else
 193      {
 194          $copy_data['type'] = "f";
 195          $copy_data['title'] = "";
 196          $copy_data['description'] = "";
 197  
 198          if(!$mybb->input['pid'])
 199          {
 200              $copy_data['pid'] = "-1";
 201          }
 202          else
 203          {
 204              $copy_data['pid'] = $mybb->get_input('pid', MyBB::INPUT_INT);
 205          }
 206          $copy_data['disporder'] = "1";
 207          $copy_data['from'] = $mybb->input['fid'];
 208          $copy_data['copyforumsettings'] = 0;
 209          $copy_data['pid'] = 0;
 210      }
 211  
 212      $types = array(
 213          'f' => $lang->forum,
 214          'c' => $lang->category
 215      );
 216  
 217      $create_a_options_f = array(
 218          'id' => 'forum'
 219      );
 220  
 221      $create_a_options_c = array(
 222          'id' => 'category'
 223      );
 224  
 225      if($copy_data['type'] == "f")
 226      {
 227          $create_a_options_f['checked'] = true;
 228      }
 229      else
 230      {
 231          $create_a_options_c['checked'] = true;
 232      }
 233  
 234      $usergroups = array();
 235  
 236      $query = $db->simple_select("usergroups", "gid, title", "gid != '1'", array('order_by' => 'title'));
 237      while($usergroup = $db->fetch_array($query))
 238      {
 239          $usergroups[$usergroup['gid']] = htmlspecialchars_uni($usergroup['title']);
 240      }
 241  
 242      $form_container = new FormContainer($lang->copy_forum);
 243      $form_container->output_row($lang->source_forum." <em>*</em>", $lang->source_forum_desc, $form->generate_forum_select('from', $copy_data['from'], array('id' => 'from')), 'from');
 244      $form_container->output_row($lang->destination_forum." <em>*</em>", $lang->destination_forum_desc, $form->generate_forum_select('to', $copy_data['to'], array('id' => 'to', 'main_option' => $lang->copy_to_new_forum)), 'to');
 245      $form_container->output_row($lang->copy_settings_and_properties, $lang->copy_settings_and_properties_desc, $form->generate_yes_no_radio('copyforumsettings', $copy_data['copyforumsettings']));
 246      $form_container->output_row($lang->copy_user_group_permissions, $lang->copy_user_group_permissions_desc, $form->generate_select_box('copygroups[]', $usergroups, $mybb->input['copygroups'], array('id' => 'copygroups', 'multiple' => true, 'size' => 5)), 'copygroups');
 247  
 248      $form_container->end();
 249  
 250      $form_container = new FormContainer($lang->new_forum_settings);
 251      $form_container->output_row($lang->forum_type, $lang->forum_type_desc, $form->generate_radio_button('type', 'f', $lang->forum, $create_a_options_f)."<br />\n".$form->generate_radio_button('type', 'c', $lang->category, $create_a_options_c));
 252      $form_container->output_row($lang->title." <em>*</em>", "", $form->generate_text_box('title', $copy_data['title'], array('id' => 'title')), 'title');
 253      $form_container->output_row($lang->description, "", $form->generate_text_area('description', $copy_data['description'], array('id' => 'description')), 'description');
 254      $form_container->output_row($lang->parent_forum." <em>*</em>", $lang->parent_forum_desc, $form->generate_forum_select('pid', $copy_data['pid'], array('id' => 'pid', 'main_option' => $lang->none)), 'pid');
 255  
 256      $form_container->end();
 257  
 258      $buttons[] = $form->generate_submit_button($lang->copy_forum);
 259      $form->output_submit_wrapper($buttons);
 260      $form->end();
 261  
 262      $page->output_footer();
 263  }
 264  
 265  if($mybb->input['action'] == "editmod")
 266  {
 267      $query = $db->simple_select("moderators", "*", "mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
 268      $mod_data = $db->fetch_array($query);
 269  
 270      if(!$mod_data['id'])
 271      {
 272          flash_message($lang->error_incorrect_moderator, 'error');
 273          admin_redirect("index.php?module=forum-management");
 274      }
 275  
 276      $plugins->run_hooks("admin_forum_management_editmod");
 277  
 278      if($mod_data['isgroup'])
 279      {
 280          $fieldname = "title";
 281      }
 282      else
 283      {
 284          $fieldname = "username";
 285      }
 286  
 287      if($mybb->request_method == "post")
 288      {
 289          $mid = $mybb->get_input('mid', MyBB::INPUT_INT);
 290          if(!$mid)
 291          {
 292              flash_message($lang->error_incorrect_moderator, 'error');
 293              admin_redirect("index.php?module=forum-management");
 294          }
 295  
 296          if(!$errors)
 297          {
 298              $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
 299              $forum = get_forum($fid);
 300              if($mod_data['isgroup'])
 301              {
 302                  $mod = $groupscache[$mod_data['id']];
 303              }
 304              else
 305              {
 306                  $mod = get_user($mod_data['id']);
 307              }
 308              $update_array = array(
 309                  'fid' => (int)$fid,
 310                  'caneditposts' => $mybb->get_input('caneditposts', MyBB::INPUT_INT),
 311                  'cansoftdeleteposts' => $mybb->get_input('cansoftdeleteposts', MyBB::INPUT_INT),
 312                  'canrestoreposts' => $mybb->get_input('canrestoreposts', MyBB::INPUT_INT),
 313                  'candeleteposts' => $mybb->get_input('candeleteposts', MyBB::INPUT_INT),
 314                  'cansoftdeletethreads' => $mybb->get_input('cansoftdeletethreads', MyBB::INPUT_INT),
 315                  'canrestorethreads' => $mybb->get_input('canrestorethreads', MyBB::INPUT_INT),
 316                  'candeletethreads' => $mybb->get_input('candeletethreads', MyBB::INPUT_INT),
 317                  'canviewips' => $mybb->get_input('canviewips', MyBB::INPUT_INT),
 318                  'canviewunapprove' => $mybb->get_input('canviewunapprove', MyBB::INPUT_INT),
 319                  'canviewdeleted' => $mybb->get_input('canviewdeleted', MyBB::INPUT_INT),
 320                  'canopenclosethreads' => $mybb->get_input('canopenclosethreads', MyBB::INPUT_INT),
 321                  'canstickunstickthreads' => $mybb->get_input('canstickunstickthreads', MyBB::INPUT_INT),
 322                  'canapproveunapprovethreads' => $mybb->get_input('canapproveunapprovethreads', MyBB::INPUT_INT),
 323                  'canapproveunapproveposts' => $mybb->get_input('canapproveunapproveposts', MyBB::INPUT_INT),
 324                  'canapproveunapproveattachs' => $mybb->get_input('canapproveunapproveattachs', MyBB::INPUT_INT),
 325                  'canmanagethreads' => $mybb->get_input('canmanagethreads', MyBB::INPUT_INT),
 326                  'canmanagepolls' => $mybb->get_input('canmanagepolls', MyBB::INPUT_INT),
 327                  'canpostclosedthreads' => $mybb->get_input('canpostclosedthreads', MyBB::INPUT_INT),
 328                  'canmovetononmodforum' => $mybb->get_input('canmovetononmodforum', MyBB::INPUT_INT),
 329                  'canusecustomtools' => $mybb->get_input('canusecustomtools', MyBB::INPUT_INT),
 330                  'canmanageannouncements' => $mybb->get_input('canmanageannouncements', MyBB::INPUT_INT),
 331                  'canmanagereportedposts' => $mybb->get_input('canmanagereportedposts', MyBB::INPUT_INT),
 332                  'canviewmodlog' => $mybb->get_input('canviewmodlog', MyBB::INPUT_INT)
 333              );
 334  
 335              $plugins->run_hooks("admin_forum_management_editmod_commit");
 336  
 337              $db->update_query("moderators", $update_array, "mid='".$mybb->get_input('mid', MyBB::INPUT_INT)."'");
 338  
 339              $cache->update_moderators();
 340  
 341              // Log admin action
 342              log_admin_action($fid, $forum['name'], $mid, $mod[$fieldname]);
 343  
 344              flash_message($lang->success_moderator_updated, 'success');
 345              admin_redirect("index.php?module=forum-management&fid=".$mybb->get_input('fid', MyBB::INPUT_INT)."#tab_moderators");
 346          }
 347      }
 348  
 349      if($mod_data['isgroup'])
 350      {
 351          $query = $db->simple_select("usergroups", "title", "gid='{$mod_data['id']}'");
 352          $mod_data[$fieldname] = $db->fetch_field($query, 'title');
 353      }
 354      else
 355      {
 356          $query = $db->simple_select("users", "username", "uid='{$mod_data['id']}'");
 357          $mod_data[$fieldname] = $db->fetch_field($query, 'username');
 358      }
 359  
 360      $sub_tabs = array();
 361  
 362      $sub_tabs['edit_mod'] = array(
 363          'title' => $lang->edit_mod,
 364          'link' => "index.php?module=forum-management&amp;action=editmod&amp;mid=".$mybb->input['mid'],
 365          'description' => $lang->edit_mod_desc
 366      );
 367  
 368      $page->add_breadcrumb_item($lang->forum_moderators, "index.php?module=forum-management&amp;fid={$mod_data['fid']}#tab_moderators");
 369      $page->add_breadcrumb_item($lang->edit_forum);
 370      $page->output_header($lang->edit_mod);
 371      $page->output_nav_tabs($sub_tabs, 'edit_mod');
 372  
 373      $form = new Form("index.php?module=forum-management&amp;action=editmod", "post");
 374      echo $form->generate_hidden_field("mid", $mod_data['mid']);
 375  
 376      if($errors)
 377      {
 378          $page->output_inline_error($errors);
 379          $mod_data = $mybb->input;
 380      }
 381  
 382      $form_container = new FormContainer($lang->sprintf($lang->edit_mod_for, $mod_data[$fieldname]));
 383      $form_container->output_row($lang->forum, $lang->forum_desc, $form->generate_forum_select('fid', $mod_data['fid'], array('id' => 'fid')), 'fid');
 384  
 385      $moderator_permissions = array(
 386          $form->generate_check_box('caneditposts', 1, $lang->can_edit_posts, array('checked' => $mod_data['caneditposts'], 'id' => 'caneditposts')),
 387          $form->generate_check_box('cansoftdeleteposts', 1, $lang->can_soft_delete_posts, array('checked' => $mod_data['cansoftdeleteposts'], 'id' => 'cansoftdeleteposts')),
 388          $form->generate_check_box('canrestoreposts', 1, $lang->can_restore_posts, array('checked' => $mod_data['canrestoreposts'], 'id' => 'canrestoreposts')),
 389          $form->generate_check_box('candeleteposts', 1, $lang->can_delete_posts, array('checked' => $mod_data['candeleteposts'], 'id' => 'candeleteposts')),
 390          $form->generate_check_box('cansoftdeletethreads', 1, $lang->can_soft_delete_threads, array('checked' => $mod_data['cansoftdeletethreads'], 'id' => 'cansoftdeletethreads')),
 391          $form->generate_check_box('canrestorethreads', 1, $lang->can_restore_threads, array('checked' => $mod_data['canrestorethreads'], 'id' => 'canrestorethreads')),
 392          $form->generate_check_box('candeletethreads', 1, $lang->can_delete_threads, array('checked' => $mod_data['candeletethreads'], 'id' => 'candeletethreads')),
 393          $form->generate_check_box('canviewips', 1, $lang->can_view_ips, array('checked' => $mod_data['canviewips'], 'id' => 'canviewips')),
 394          $form->generate_check_box('canviewunapprove', 1, $lang->can_view_unapprove, array('checked' => $mod_data['canviewunapprove'], 'id' => 'canviewunapprove')),
 395          $form->generate_check_box('canviewdeleted', 1, $lang->can_view_deleted, array('checked' => $mod_data['canviewdeleted'], 'id' => 'canviewdeleted')),
 396          $form->generate_check_box('canopenclosethreads', 1, $lang->can_open_close_threads, array('checked' => $mod_data['canopenclosethreads'], 'id' => 'canopenclosethreads')),
 397          $form->generate_check_box('canstickunstickthreads', 1, $lang->can_stick_unstick_threads, array('checked' => $mod_data['canstickunstickthreads'], 'id' => 'canstickunstickthreads')),
 398          $form->generate_check_box('canapproveunapprovethreads', 1, $lang->can_approve_unapprove_threads, array('checked' => $mod_data['canapproveunapprovethreads'], 'id' => 'canapproveunapprovethreads')),
 399          $form->generate_check_box('canapproveunapproveposts', 1, $lang->can_approve_unapprove_posts, array('checked' => $mod_data['canapproveunapproveposts'], 'id' => 'canapproveunapproveposts')),
 400          $form->generate_check_box('canapproveunapproveattachs', 1, $lang->can_approve_unapprove_attachments, array('checked' => $mod_data['canapproveunapproveattachs'], 'id' => 'canapproveunapproveattachs')),
 401          $form->generate_check_box('canmanagethreads', 1, $lang->can_manage_threads, array('checked' => $mod_data['canmanagethreads'], 'id' => 'canmanagethreads')),
 402          $form->generate_check_box('canmanagepolls', 1, $lang->can_manage_polls, array('checked' => $mod_data['canmanagepolls'], 'id' => 'canmanagepolls')),
 403          $form->generate_check_box('canpostclosedthreads', 1, $lang->can_post_closed_threads, array('checked' => $mod_data['canpostclosedthreads'], 'id' => 'canpostclosedthreads')),
 404          $form->generate_check_box('canmovetononmodforum', 1, $lang->can_move_to_other_forums, array('checked' => $mod_data['canmovetononmodforum'], 'id' => 'canmovetononmodforum')),
 405          $form->generate_check_box('canusecustomtools', 1, $lang->can_use_custom_tools, array('checked' => $mod_data['canusecustomtools'], 'id' => 'canusecustomtools'))
 406      );
 407      $form_container->output_row($lang->moderator_permissions, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $moderator_permissions)."</div>");
 408  
 409      $moderator_cp_permissions = array(
 410          $form->generate_check_box('canmanageannouncements', 1, $lang->can_manage_announcements, array('checked' => $mod_data['canmanageannouncements'], 'id' => 'canmanageannouncements')),
 411          $form->generate_check_box('canmanagereportedposts', 1, $lang->can_manage_reported_posts, array('checked' => $mod_data['canmanagereportedposts'], 'id' => 'canmanagereportedposts')),
 412          $form->generate_check_box('canviewmodlog', 1, $lang->can_view_mod_log, array('checked' => $mod_data['canviewmodlog'], 'id' => 'canviewmodlog'))
 413      );
 414      $form_container->output_row($lang->moderator_cp_permissions, $lang->moderator_cp_permissions_desc, "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $moderator_cp_permissions)."</div>");
 415  
 416      $form_container->end();
 417  
 418      $buttons[] = $form->generate_submit_button($lang->save_mod);
 419      $form->output_submit_wrapper($buttons);
 420      $form->end();
 421  
 422      $page->output_footer();
 423  }
 424  
 425  if($mybb->input['action'] == "clear_permission")
 426  {
 427      $pid = $mybb->get_input('pid', MyBB::INPUT_INT);
 428      $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
 429      $gid = $mybb->get_input('gid', MyBB::INPUT_INT);
 430  
 431      // User clicked no
 432      if($mybb->input['no'])
 433      {
 434          admin_redirect("index.php?module=forum-management&fid={$fid}");
 435      }
 436  
 437      $plugins->run_hooks("admin_forum_management_clear_permission");
 438  
 439      if($mybb->request_method == "post")
 440      {
 441          if((!$fid || !$gid) && $pid)
 442          {
 443              $query = $db->simple_select("forumpermissions", "fid, gid", "pid='{$pid}'");
 444              $result = $db->fetch_array($query);
 445              $fid = $result['fid'];
 446              $gid = $result['gid'];
 447          }
 448  
 449          if($pid)
 450          {
 451              $db->delete_query("forumpermissions", "pid='{$pid}'");
 452          }
 453          else
 454          {
 455              $db->delete_query("forumpermissions", "gid='{$gid}' AND fid='{$fid}'");
 456          }
 457  
 458          $plugins->run_hooks('admin_forum_management_clear_permission_commit');
 459  
 460          $cache->update_forumpermissions();
 461  
 462          flash_message($lang->success_custom_permission_cleared, 'success');
 463          admin_redirect("index.php?module=forum-management&fid={$fid}#tab_permissions");
 464      }
 465      else
 466      {
 467          $page->output_confirm_action("index.php?module=forum-management&amp;action=clear_permission&amp;pid={$pid}&amp;gid={$gid}&amp;fid={$fid}", $lang->confirm_clear_custom_permission);
 468      }
 469  }
 470  
 471  if($mybb->input['action'] == "permissions")
 472  {
 473      $plugins->run_hooks("admin_forum_management_permissions");
 474  
 475      if($mybb->request_method == "post")
 476      {
 477          $pid = $mybb->get_input('pid', MyBB::INPUT_INT);
 478          $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
 479          $gid = $mybb->get_input('gid', MyBB::INPUT_INT);
 480          $forum = get_forum($fid);
 481  
 482          if((!$fid || !$gid) && $pid)
 483          {
 484              $query = $db->simple_select("forumpermissions", "fid, gid", "pid='{$pid}'");
 485              $result = $db->fetch_array($query);
 486              $fid = $result['fid'];
 487              $gid = $result['gid'];
 488              $forum = get_forum($fid);
 489          }
 490  
 491          $field_list = array();
 492          $fields_array = $db->show_fields_from("forumpermissions");
 493          if(is_array($mybb->input['permissions']))
 494          {
 495              // User has set permissions for this group...
 496              foreach($fields_array as $field)
 497              {
 498                  if(strpos($field['Field'], 'can') !== false || strpos($field['Field'], 'mod') !== false)
 499                  {
 500                      if(array_key_exists($field['Field'], $mybb->input['permissions']))
 501                      {
 502                          $update_array[$db->escape_string($field['Field'])] = (int)$mybb->input['permissions'][$field['Field']];
 503                      }
 504                      else
 505                      {
 506                          $update_array[$db->escape_string($field['Field'])] = 0;
 507                      }
 508                  }
 509              }
 510          }
 511          else
 512          {
 513              // Else, we assume that the group has no permissions...
 514              foreach($fields_array as $field)
 515              {
 516                  if(strpos($field['Field'], 'can') !== false || strpos($field['Field'], 'mod') !== false)
 517                  {
 518                      $update_array[$db->escape_string($field['Field'])] = 0;
 519                  }
 520              }
 521          }
 522  
 523          if($fid && !$pid)
 524          {
 525              $update_array['fid'] = $fid;
 526              $update_array['gid'] = $mybb->get_input('gid', MyBB::INPUT_INT);
 527              $db->insert_query("forumpermissions", $update_array);
 528          }
 529  
 530          $plugins->run_hooks("admin_forum_management_permissions_commit");
 531  
 532          if(!($fid && !$pid))
 533          {
 534              $db->update_query("forumpermissions", $update_array, "pid='{$pid}'");
 535          }
 536  
 537          $cache->update_forumpermissions();
 538  
 539          // Log admin action
 540          log_admin_action($fid, $forum['name']);
 541  
 542          if($mybb->input['ajax'] == 1)
 543          {
 544              echo json_encode("<script type=\"text/javascript\">$('#row_{$gid}').html('".str_replace(array("'", "\t", "\n"), array("\\'", "", ""), retrieve_single_permissions_row($gid, $fid))."'); QuickPermEditor.init({$gid});</script>");
 545              die;
 546          }
 547          else
 548          {
 549              flash_message($lang->success_forum_permissions_saved, 'success');
 550              admin_redirect("index.php?module=forum-management&fid={$fid}#tab_permissions");
 551          }
 552      }
 553  
 554      if($mybb->input['ajax'] != 1)
 555      {
 556          $sub_tabs = array();
 557  
 558          if($mybb->input['fid'] && $mybb->input['gid'])
 559          {
 560              $sub_tabs['edit_permissions'] = array(
 561                  'title' => $lang->forum_permissions,
 562                  'link' => "index.php?module=forum-management&amp;action=permissions&amp;fid=".$mybb->input['fid']."&amp;gid=".$mybb->input['gid'],
 563                  'description' => $lang->forum_permissions_desc
 564              );
 565  
 566              $page->add_breadcrumb_item($lang->forum_permissions2, "index.php?module=forum-management&amp;fid=".$mybb->input['fid']."#tab_permissions");
 567          }
 568          else
 569          {
 570              $query = $db->simple_select("forumpermissions", "fid", "pid='".$mybb->get_input('pid', MyBB::INPUT_INT)."'");
 571              $mybb->input['fid'] = $db->fetch_field($query, "fid");
 572  
 573              $sub_tabs['edit_permissions'] = array(
 574                  'title' => $lang->forum_permissions,
 575                  'link' => "index.php?module=forum-management&amp;action=permissions&amp;pid=".$mybb->get_input('pid', MyBB::INPUT_INT),
 576                  'description' => $lang->forum_permissions_desc
 577              );
 578  
 579              $page->add_breadcrumb_item($lang->forum_permissions2, "index.php?module=forum-management&amp;fid=".$mybb->input['fid']."#tab_permissions");
 580          }
 581  
 582          $page->add_breadcrumb_item($lang->forum_permissions);
 583          $page->output_header($lang->forum_permissions);
 584          $page->output_nav_tabs($sub_tabs, 'edit_permissions');
 585      }
 586      else
 587      {
 588          echo "
 589          <div class=\"modal\" style=\"width: auto\">
 590          <script src=\"jscripts/tabs.js\" type=\"text/javascript\"></script>\n
 591          <script type=\"text/javascript\">
 592  <!--
 593  $(function() {
 594      $(\"#modal_form\").on(\"click\", \"#savePermissions\", function(e) {
 595          e.preventDefault();
 596          
 597          var datastring = $(\"#modal_form\").serialize();
 598          $.ajax({
 599              type: \"POST\",
 600              url: $(\"#modal_form\").attr('action'),
 601              data: datastring,
 602              dataType: \"json\",
 603              success: function(data) {
 604                  $(data).filter(\"script\").each(function(e) {
 605                      eval($(this).text());
 606                  });
 607                  $.modal.close();
 608              },
 609              error: function(){
 610              }
 611          });
 612      });
 613  });
 614  // -->
 615          </script>
 616          <div style=\"overflow-y: auto; max-height: 400px\">";
 617      }
 618  
 619      if($mybb->input['pid'] || ($mybb->input['gid'] && $mybb->input['fid']))
 620      {
 621          if($mybb->input['ajax'] != 1)
 622          {
 623              $form = new Form("index.php?module=forum-management&amp;action=permissions", "post");
 624          }
 625          else
 626          {
 627              $form = new Form("index.php?module=forum-management&amp;action=permissions&amp;ajax=1&amp;pid=".$mybb->get_input('pid', MyBB::INPUT_INT)."&amp;gid=".$mybb->get_input('gid', MyBB::INPUT_INT)."&amp;fid=".$mybb->get_input('gid', MyBB::INPUT_INT), "post", "modal_form");
 628          }
 629          echo $form->generate_hidden_field("usecustom", "1");
 630  
 631          if($errors)
 632          {
 633              $page->output_inline_error($errors);
 634              $permission_data = $mybb->input;
 635  
 636              $query = $db->simple_select("usergroups", "*", "gid='{$permission_data['gid']}'");
 637              $usergroup = $db->fetch_array($query);
 638  
 639              $query = $db->simple_select("forums", "*", "fid='{$permission_data['fid']}'");
 640              $forum = $db->fetch_array($query);
 641          }
 642          else
 643          {
 644              $pid = $mybb->get_input('pid', MyBB::INPUT_INT);
 645              $gid = $mybb->get_input('gid', MyBB::INPUT_INT);
 646              $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
 647  
 648              if($pid)
 649              {
 650                  $query = $db->simple_select("forumpermissions", "*", "pid='{$pid}'");
 651              }
 652              else
 653              {
 654                  $query = $db->simple_select("forumpermissions", "*", "fid='{$fid}' AND gid='{$gid}'", array('limit' => 1));
 655              }
 656  
 657              $permission_data = $db->fetch_array($query);
 658  
 659              if(!$fid)
 660              {
 661                  $fid = $permission_data['fid'];
 662              }
 663  
 664              if(!$gid)
 665              {
 666                  $gid = $permission_data['gid'];
 667              }
 668  
 669              if(!$pid)
 670              {
 671                  $pid = $permission_data['pid'];
 672              }
 673  
 674              $query = $db->simple_select("usergroups", "*", "gid='$gid'");
 675              $usergroup = $db->fetch_array($query);
 676  
 677              $query = $db->simple_select("forums", "*", "fid='$fid'");
 678              $forum = $db->fetch_array($query);
 679  
 680              $sperms = $permission_data;
 681  
 682              $sql = build_parent_list($fid);
 683              $query = $db->simple_select("forumpermissions", "*", "$sql AND gid='$gid'");
 684              $customperms = $db->fetch_array($query);
 685  
 686              if($permission_data['pid'])
 687              {
 688                  $permission_data['usecustom'] = 1;
 689                  echo $form->generate_hidden_field("pid", $pid);
 690              }
 691              else
 692              {
 693                  echo $form->generate_hidden_field("fid", $fid);
 694                  echo $form->generate_hidden_field("gid", $gid);
 695                  if(!$customperms['pid'])
 696                  {
 697                      $permission_data = usergroup_permissions($gid);
 698                  }
 699                  else
 700                  {
 701                      $permission_data = forum_permissions($fid, 0, $gid);
 702                  }
 703              }
 704          }
 705  
 706          $groups = array(
 707              'canviewthreads' => 'viewing',
 708              'canview' => 'viewing',
 709              'canonlyviewownthreads' => 'viewing',
 710              'candlattachments' => 'viewing',
 711  
 712              'canpostthreads' => 'posting_rating',
 713              'canpostreplys' => 'posting_rating',
 714              'canonlyreplyownthreads' => 'posting_rating',
 715              'canpostattachments' => 'posting_rating',
 716              'canratethreads' => 'posting_rating',
 717  
 718              'caneditposts' => 'editing',
 719              'candeleteposts' => 'editing',
 720              'candeletethreads' => 'editing',
 721              'caneditattachments' => 'editing',
 722              'canviewdeletionnotice' => 'editing',
 723  
 724              'modposts' => 'moderate',
 725              'modthreads' => 'moderate',
 726              'modattachments' => 'moderate',
 727              'mod_edit_posts' => 'moderate',
 728  
 729              'canpostpolls' => 'polls',
 730              'canvotepolls' => 'polls',
 731              'cansearch' => 'misc',
 732          );
 733  
 734          $groups = $plugins->run_hooks("admin_forum_management_permission_groups", $groups);
 735  
 736          $tabs = array();
 737          foreach(array_unique(array_values($groups)) as $group)
 738          {
 739              $lang_group = "group_".$group;
 740              $tabs[$group] = $lang->$lang_group;
 741          }
 742  
 743          if($mybb->input['ajax'] == 1)
 744          {
 745              $page->output_tab_control($tabs, false, "tabs2");
 746          }
 747          else
 748          {
 749              $page->output_tab_control($tabs);
 750          }
 751  
 752          $field_list = array();
 753          $fields_array = $db->show_fields_from("forumpermissions");
 754          foreach($fields_array as $field)
 755          {
 756              if(strpos($field['Field'], 'can') !== false || strpos($field['Field'], 'mod') !== false)
 757              {
 758                  if(array_key_exists($field['Field'], $groups))
 759                  {
 760                      $field_list[$groups[$field['Field']]][] = $field['Field'];
 761                  }
 762                  else
 763                  {
 764                      $field_list['misc'][] = $field['Field'];
 765                  }
 766              }
 767          }
 768  
 769          foreach(array_unique(array_values($groups)) as $group)
 770          {
 771              $lang_group = "group_".$group;
 772              echo "<div id=\"tab_".$group."\">\n";
 773              $form_container = new FormContainer("\"".htmlspecialchars_uni($usergroup['title'])."\" {$lang->custom_permissions_for} \"".htmlspecialchars_uni($forum['name'])."\"");
 774              $fields = array();
 775              foreach($field_list[$group] as $field)
 776              {
 777                  $lang_field = $group."_field_".$field;
 778                  $fields[] = $form->generate_check_box("permissions[{$field}]", 1, $lang->$lang_field, array('checked' => $permission_data[$field], 'id' => $field));
 779              }
 780              $form_container->output_row("", "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $fields)."</div>");
 781              $form_container->end();
 782              echo "</div>";
 783          }
 784  
 785          if($mybb->input['ajax'] == 1)
 786          {
 787              $buttons[] = $form->generate_submit_button($lang->cancel, array('onclick' => '$.modal.close(); return false;'));
 788              $buttons[] = $form->generate_submit_button($lang->save_permissions, array('id' => 'savePermissions'));
 789              $form->output_submit_wrapper($buttons);
 790              $form->end();
 791              echo "</div>";
 792              echo "</div>";
 793          }
 794          else
 795          {
 796              $buttons[] = $form->generate_submit_button($lang->save_permissions);
 797              $form->output_submit_wrapper($buttons);
 798  
 799              $form->end();
 800          }
 801      }
 802  
 803      if($mybb->input['ajax'] != 1)
 804      {
 805          $page->output_footer();
 806      }
 807  }
 808  
 809  if($mybb->input['action'] == "add")
 810  {
 811      $plugins->run_hooks("admin_forum_management_add");
 812  
 813      if($mybb->request_method == "post")
 814      {
 815          if(!trim($mybb->input['title']))
 816          {
 817              $errors[] = $lang->error_missing_title;
 818          }
 819  
 820          $pid = $mybb->get_input('pid', MyBB::INPUT_INT);
 821          $type = $mybb->input['type'];
 822  
 823          if($pid <= 0 && $type == "f")
 824          {
 825              $errors[] = $lang->error_no_parent;
 826          }
 827  
 828          if(!$errors)
 829          {
 830              if($pid < 0)
 831              {
 832                  $pid = 0;
 833              }
 834              $insert_array = array(
 835                  "name" => $db->escape_string($mybb->input['title']),
 836                  "description" => $db->escape_string($mybb->input['description']),
 837                  "linkto" => $db->escape_string($mybb->input['linkto']),
 838                  "type" => $db->escape_string($type),
 839                  "pid" => $pid,
 840                  "parentlist" => '',
 841                  "disporder" => $mybb->get_input('disporder', MyBB::INPUT_INT),
 842                  "active" => $mybb->get_input('active', MyBB::INPUT_INT),
 843                  "open" => $mybb->get_input('open', MyBB::INPUT_INT),
 844                  "allowhtml" => $mybb->get_input('allowhtml', MyBB::INPUT_INT),
 845                  "allowmycode" => $mybb->get_input('allowmycode', MyBB::INPUT_INT),
 846                  "allowsmilies" => $mybb->get_input('allowsmilies', MyBB::INPUT_INT),
 847                  "allowimgcode" => $mybb->get_input('allowimgcode', MyBB::INPUT_INT),
 848                  "allowvideocode" => $mybb->get_input('allowvideocode', MyBB::INPUT_INT),
 849                  "allowpicons" => $mybb->get_input('allowpicons', MyBB::INPUT_INT),
 850                  "allowtratings" => $mybb->get_input('allowtratings', MyBB::INPUT_INT),
 851                  "usepostcounts" => $mybb->get_input('usepostcounts', MyBB::INPUT_INT),
 852                  "usethreadcounts" => $mybb->get_input('usethreadcounts', MyBB::INPUT_INT),
 853                  "requireprefix" => $mybb->get_input('requireprefix', MyBB::INPUT_INT),
 854                  "password" => $db->escape_string($mybb->input['password']),
 855                  "showinjump" => $mybb->get_input('showinjump', MyBB::INPUT_INT),
 856                  "style" => $mybb->get_input('style', MyBB::INPUT_INT),
 857                  "overridestyle" => $mybb->get_input('overridestyle', MyBB::INPUT_INT),
 858                  "rulestype" => $mybb->get_input('rulestype', MyBB::INPUT_INT),
 859                  "rulestitle" => $db->escape_string($mybb->input['rulestitle']),
 860                  "rules" => $db->escape_string($mybb->input['rules']),
 861                  "defaultdatecut" => $mybb->get_input('defaultdatecut', MyBB::INPUT_INT),
 862                  "defaultsortby" => $db->escape_string($mybb->input['defaultsortby']),
 863                  "defaultsortorder" => $db->escape_string($mybb->input['defaultsortorder']),
 864              );
 865              $fid = $db->insert_query("forums", $insert_array);
 866  
 867              $parentlist = make_parent_list($fid);
 868              $db->update_query("forums", array("parentlist" => $parentlist), "fid='$fid'");
 869  
 870              $inherit = $mybb->input['default_permissions'];
 871  
 872              foreach($mybb->input as $id => $permission)
 873              {
 874                  if(strpos($id, 'fields_') === false)
 875                  {
 876                      continue;
 877                  }
 878  
 879                  list(, $gid) = explode('fields_', $id);
 880  
 881                  // If it isn't an array then it came from the javascript form
 882                  if(!is_array($permission))
 883                  {
 884                      $permission = explode(',', $permission);
 885                      $permission = array_flip($permission);
 886                      foreach($permission as $name => $value)
 887                      {
 888                          $permission[$name] = 1;
 889                      }
 890                  }
 891  
 892                  foreach(array('canview','canpostthreads','canpostreplys','canpostpolls') as $name)
 893                  {
 894                      if(in_array($name, $permission)  || $permission[$name])
 895                      {
 896                          $permissions[$name][$gid] = 1;
 897                      }
 898                      else
 899                      {
 900                          $permissions[$name][$gid] = 0;
 901                      }
 902                  }
 903              }
 904  
 905              $canview = $permissions['canview'];
 906              $canpostthreads = $permissions['canpostthreads'];
 907              $canpostpolls = $permissions['canpostpolls'];
 908              $canpostattachments = $permissions['canpostattachments'];
 909              $canpostreplies = $permissions['canpostreplys'];
 910              save_quick_perms($fid);
 911  
 912              $plugins->run_hooks("admin_forum_management_add_commit");
 913  
 914              $cache->update_forums();
 915  
 916              // Log admin action
 917              log_admin_action($fid, $insert_array['name']);
 918  
 919              flash_message($lang->success_forum_added, 'success');
 920              admin_redirect("index.php?module=forum-management");
 921          }
 922      }
 923  
 924      $page->extra_header .=  "<script src=\"jscripts/quick_perm_editor.js\" type=\"text/javascript\"></script>\n";
 925  
 926      $page->add_breadcrumb_item($lang->add_forum);
 927      $page->output_header($lang->add_forum);
 928      $page->output_nav_tabs($sub_tabs, 'add_forum');
 929  
 930      $form = new Form("index.php?module=forum-management&amp;action=add", "post");
 931  
 932      if($errors)
 933      {
 934          $page->output_inline_error($errors);
 935          $forum_data = $mybb->input;
 936      }
 937      else
 938      {
 939          $forum_data['type'] = "f";
 940          $forum_data['title'] = "";
 941          $forum_data['description'] = "";
 942  
 943          if(!$mybb->input['pid'])
 944          {
 945              $forum_data['pid'] = "-1";
 946          }
 947          else
 948          {
 949              $forum_data['pid'] = $mybb->get_input('pid', MyBB::INPUT_INT);
 950          }
 951          $forum_data['disporder'] = "1";
 952          $forum_data['linkto'] = "";
 953          $forum_data['password'] = "";
 954          $forum_data['active'] = 1;
 955          $forum_data['open'] = 1;
 956          $forum_data['overridestyle'] = "";
 957          $forum_data['style'] = "";
 958          $forum_data['rulestype'] = "";
 959          $forum_data['rulestitle'] = "";
 960          $forum_data['rules'] = "";
 961          $forum_data['defaultdatecut'] = "";
 962          $forum_data['defaultsortby'] = "";
 963          $forum_data['defaultsortorder'] = "";
 964          $forum_data['allowhtml'] = "";
 965          $forum_data['allowmycode'] = 1;
 966          $forum_data['allowsmilies'] = 1;
 967          $forum_data['allowimgcode'] = 1;
 968          $forum_data['allowvideocode'] = 1;
 969          $forum_data['allowpicons'] = 1;
 970          $forum_data['allowtratings'] = 1;
 971          $forum_data['showinjump'] = 1;
 972          $forum_data['usepostcounts'] = 1;
 973          $forum_data['usethreadcounts'] = 1;
 974          $forum_data['requireprefix'] = 0;
 975      }
 976  
 977      $types = array(
 978          'f' => $lang->forum,
 979          'c' => $lang->category
 980      );
 981  
 982      $create_a_options_f = array(
 983          'id' => 'forum'
 984      );
 985  
 986      $create_a_options_c = array(
 987          'id' => 'category'
 988      );
 989  
 990      if($forum_data['type'] == "f")
 991      {
 992          $create_a_options_f['checked'] = true;
 993      }
 994      else
 995      {
 996          $create_a_options_c['checked'] = true;
 997      }
 998  
 999      $form_container = new FormContainer($lang->add_forum);
1000      $form_container->output_row($lang->forum_type, $lang->forum_type_desc, $form->generate_radio_button('type', 'f', $lang->forum, $create_a_options_f)."<br />\n".$form->generate_radio_button('type', 'c', $lang->category, $create_a_options_c));
1001      $form_container->output_row($lang->title." <em>*</em>", "", $form->generate_text_box('title', $forum_data['title'], array('id' => 'title')), 'title');
1002      $form_container->output_row($lang->description, "", $form->generate_text_area('description', $forum_data['description'], array('id' => 'description')), 'description');
1003      $form_container->output_row($lang->parent_forum." <em>*</em>", $lang->parent_forum_desc, $form->generate_forum_select('pid', $forum_data['pid'], array('id' => 'pid', 'main_option' => $lang->none)), 'pid');
1004      $form_container->output_row($lang->display_order, "", $form->generate_numeric_field('disporder', $forum_data['disporder'], array('id' => 'disporder', 'min' => 0)), 'disporder');
1005      $form_container->end();
1006  
1007      echo "<div id=\"additional_options_link\"><strong><a href=\"#\" onclick=\"$('#additional_options_link').toggle(); $('#additional_options').fadeToggle('fast'); return false;\">{$lang->show_additional_options}</a></strong><br /><br /></div>";
1008      echo "<div id=\"additional_options\" style=\"display: none;\">";
1009      $form_container = new FormContainer("<div class=\"float_right\" style=\"font-weight: normal;\"><a href=\"#\" onclick=\"$('#additional_options_link').toggle(); $('#additional_options').fadeToggle('fast'); return false;\">{$lang->hide_additional_options}</a></div>".$lang->additional_forum_options);
1010      $form_container->output_row($lang->forum_link, $lang->forum_link_desc, $form->generate_text_box('linkto', $forum_data['linkto'], array('id' => 'linkto')), 'linkto');
1011      $form_container->output_row($lang->forum_password, $lang->forum_password_desc, $form->generate_text_box('password', $forum_data['password'], array('id' => 'password')), 'password');
1012  
1013      $access_options = array(
1014          $form->generate_check_box('active', 1, $lang->forum_is_active."<br />\n<small>{$lang->forum_is_active_desc}</small>", array('checked' => $forum_data['active'], 'id' => 'active')),
1015          $form->generate_check_box('open', 1, $lang->forum_is_open."<br />\n<small>{$lang->forum_is_open_desc}</small>", array('checked' => $forum_data['open'], 'id' => 'open'))
1016      );
1017  
1018      $form_container->output_row($lang->access_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $access_options)."</div>");
1019  
1020      $styles = array(
1021          '0' => $lang->use_default
1022      );
1023  
1024      $query = $db->simple_select("themes", "tid,name", "name!='((master))' AND name!='((master-backup))'", array('order_by' => 'name'));
1025      while($style = $db->fetch_array($query))
1026      {
1027          $styles[$style['tid']] = htmlspecialchars_uni($style['name']);
1028      }
1029  
1030      $style_options = array(
1031          $form->generate_check_box('overridestyle', 1, $lang->override_user_style, array('checked' => $forum_data['overridestyle'], 'id' => 'overridestyle')),
1032          $lang->forum_specific_style."<br />\n".$form->generate_select_box('style', $styles, $forum_data['style'], array('id' => 'style'))
1033      );
1034  
1035      $form_container->output_row($lang->style_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $style_options)."</div>");
1036  
1037      $display_methods = array(
1038          '0' => $lang->dont_display_rules,
1039          '1' => $lang->display_rules_inline,
1040          '3' => $lang->display_rules_inline_new,
1041          '2' => $lang->display_rules_link
1042      );
1043  
1044      $forum_rules = array(
1045          $lang->display_method."<br />\n".$form->generate_select_box('rulestype', $display_methods, $forum_data['rulestype'], array('checked' => $forum_data['rulestype'], 'id' => 'rulestype')),
1046          $lang->title."<br />\n".$form->generate_text_box('rulestitle', $forum_data['rulestitle'], array('checked' => $forum_data['rulestitle'], 'id' => 'rulestitle')),
1047          $lang->rules."<br />\n".$form->generate_text_area('rules', $forum_data['rules'], array('checked' => $forum_data['rules'], 'id' => 'rules'))
1048      );
1049  
1050      $form_container->output_row($lang->forum_rules, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $forum_rules)."</div>");
1051  
1052      $default_date_cut = array(
1053          0 => $lang->board_default,
1054          1 => $lang->datelimit_1day,
1055          5 => $lang->datelimit_5days,
1056          10 => $lang->datelimit_10days,
1057          20 => $lang->datelimit_20days,
1058          50 => $lang->datelimit_50days,
1059          75 => $lang->datelimit_75days,
1060          100 => $lang->datelimit_100days,
1061          365 => $lang->datelimit_lastyear,
1062          9999 => $lang->datelimit_beginning,
1063      );
1064  
1065      $default_sort_by = array(
1066          "" => $lang->board_default,
1067          "subject" => $lang->sort_by_subject,
1068          "lastpost" => $lang->sort_by_lastpost,
1069          "starter" => $lang->sort_by_starter,
1070          "started" => $lang->sort_by_started,
1071          "rating" => $lang->sort_by_rating,
1072          "replies" => $lang->sort_by_replies,
1073          "views" => $lang->sort_by_views,
1074      );
1075  
1076      $default_sort_order = array(
1077          "" => $lang->board_default,
1078          "asc" => $lang->sort_order_asc,
1079          "desc" => $lang->sort_order_desc,
1080      );
1081  
1082      $view_options = array(
1083          $lang->default_date_cut."<br />\n".$form->generate_select_box('defaultdatecut', $default_date_cut, $forum_data['defaultdatecut'], array('checked' => $forum_data['defaultdatecut'], 'id' => 'defaultdatecut')),
1084          $lang->default_sort_by."<br />\n".$form->generate_select_box('defaultsortby', $default_sort_by, $forum_data['defaultsortby'], array('checked' => $forum_data['defaultsortby'], 'id' => 'defaultsortby')),
1085          $lang->default_sort_order."<br />\n".$form->generate_select_box('defaultsortorder', $default_sort_order, $forum_data['defaultsortorder'], array('checked' => $forum_data['defaultsortorder'], 'id' => 'defaultsortorder')),
1086      );
1087  
1088      $form_container->output_row($lang->default_view_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $view_options)."</div>");
1089  
1090      $misc_options = array(
1091          $form->generate_check_box('allowhtml', 1, $lang->allow_html, array('checked' => $forum_data['allowhtml'], 'id' => 'allowhtml')),
1092          $form->generate_check_box('allowmycode', 1, $lang->allow_mycode, array('checked' => $forum_data['allowmycode'], 'id' => 'allowmycode')),
1093          $form->generate_check_box('allowsmilies', 1, $lang->allow_smilies, array('checked' => $forum_data['allowsmilies'], 'id' => 'allowsmilies')),
1094          $form->generate_check_box('allowimgcode', 1, $lang->allow_img_code, array('checked' => $forum_data['allowimgcode'], 'id' => 'allowimgcode')),
1095          $form->generate_check_box('allowvideocode', 1, $lang->allow_video_code, array('checked' => $forum_data['allowvideocode'], 'id' => 'allowvideocode')),
1096          $form->generate_check_box('allowpicons', 1, $lang->allow_post_icons, array('checked' => $forum_data['allowpicons'], 'id' => 'allowpicons')),
1097          $form->generate_check_box('allowtratings', 1, $lang->allow_thread_ratings, array('checked' => $forum_data['allowtratings'], 'id' => 'allowtratings')),
1098          $form->generate_check_box('showinjump', 1, $lang->show_forum_jump, array('checked' => $forum_data['showinjump'], 'id' => 'showinjump')),
1099          $form->generate_check_box('usepostcounts', 1, $lang->use_postcounts, array('checked' => $forum_data['usepostcounts'], 'id' => 'usepostcounts')),
1100          $form->generate_check_box('usethreadcounts', 1, $lang->use_threadcounts, array('checked' => $forum_data['usethreadcounts'], 'id' => 'usethreadcounts')),
1101          $form->generate_check_box('requireprefix', 1, $lang->require_thread_prefix, array('checked' => $forum_data['requireprefix'], 'id' => 'requireprefix'))
1102      );
1103  
1104      $form_container->output_row($lang->misc_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $misc_options)."</div>");
1105      $form_container->end();
1106      echo "</div>";
1107  
1108      $query = $db->simple_select("usergroups", "*", "", array("order" => "name"));
1109      while($usergroup = $db->fetch_array($query))
1110      {
1111          $usergroups[$usergroup['gid']] = $usergroup;
1112      }
1113  
1114      $cached_forum_perms = $cache->read("forumpermissions");
1115      $field_list = array(
1116          'canview' => $lang->permissions_canview,
1117          'canpostthreads' => $lang->permissions_canpostthreads,
1118          'canpostreplys' => $lang->permissions_canpostreplys,
1119          'canpostpolls' => $lang->permissions_canpostpolls,
1120      );
1121  
1122      $field_list2 = array(
1123          'canview' => $lang->perm_drag_canview,
1124          'canpostthreads' => $lang->perm_drag_canpostthreads,
1125          'canpostreplys' => $lang->perm_drag_canpostreplys,
1126          'canpostpolls' => $lang->perm_drag_canpostpolls,
1127      );
1128  
1129      $ids = array();
1130  
1131      $form_container = new FormContainer($lang->forum_permissions);
1132      $form_container->output_row_header($lang->permissions_group, array("class" => "align_center", 'style' => 'width: 40%'));
1133      $form_container->output_row_header($lang->overview_allowed_actions, array("class" => "align_center"));
1134      $form_container->output_row_header($lang->overview_disallowed_actions, array("class" => "align_center"));
1135  
1136      if($mybb->request_method == "post")
1137      {
1138          foreach($usergroups as $usergroup)
1139          {
1140              if(isset($mybb->input['fields_'.$usergroup['gid']]))
1141              {
1142                  $input_permissions = $mybb->input['fields_'.$usergroup['gid']];
1143                  if(!is_array($input_permissions))
1144                  {
1145                      // Convering the comma separated list from Javascript form into a variable
1146                      $input_permissions = explode(',' , $input_permissions);
1147                  }
1148                  foreach($input_permissions as $input_permission)
1149                  {
1150                      $mybb->input['permissions'][$usergroup['gid']][$input_permission] = 1;
1151                  }
1152              }
1153          }
1154      }
1155  
1156      foreach($usergroups as $usergroup)
1157      {
1158          $perms = array();
1159          if(isset($mybb->input['default_permissions']) && $mybb->input['default_permissions'][$usergroup['gid']])
1160          {
1161              if(is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
1162              {
1163                  $perms = $existing_permissions[$usergroup['gid']];
1164                  $default_checked = false;
1165              }
1166              elseif(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['fid']][$usergroup['gid']])
1167              {
1168                  $perms = $cached_forum_perms[$forum_data['fid']][$usergroup['gid']];
1169                  $default_checked = true;
1170              }
1171              else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['pid']][$usergroup['gid']])
1172              {
1173                  $perms = $cached_forum_perms[$forum_data['pid']][$usergroup['gid']];
1174                  $default_checked = true;
1175              }
1176          }
1177  
1178          if(!$perms)
1179          {
1180              $perms = $usergroup;
1181              $default_checked = true;
1182          }
1183  
1184          foreach($field_list as $forum_permission => $forum_perm_title)
1185          {
1186              if(isset($mybb->input['permissions']))
1187              {
1188                  if($mybb->input['default_permissions'][$usergroup['gid']])
1189                  {
1190                      $default_checked = true;
1191                  }
1192                  else
1193                  {
1194                      $default_checked = false;
1195                  }
1196  
1197                  if($mybb->input['permissions'][$usergroup['gid']][$forum_permission])
1198                  {
1199                      $perms_checked[$forum_permission] = 1;
1200                  }
1201                  else
1202                  {
1203                      $perms_checked[$forum_permission] = 0;
1204                  }
1205              }
1206              else
1207              {
1208                  if($perms[$forum_permission] == 1)
1209                  {
1210                      $perms_checked[$forum_permission] = 1;
1211                  }
1212                  else
1213                  {
1214                      $perms_checked[$forum_permission] = 0;
1215                  }
1216              }
1217          }
1218          $usergroup['title'] = htmlspecialchars_uni($usergroup['title']);
1219  
1220          if($default_checked)
1221          {
1222              $inherited_text = $lang->inherited_permission;
1223          }
1224          else
1225          {
1226              $inherited_text = $lang->custom_permission;
1227          }
1228  
1229          $form_container->output_cell("<strong>{$usergroup['title']}</strong><br />".$form->generate_check_box("default_permissions[{$usergroup['gid']}]", 1, "", array("id" => "default_permissions_{$usergroup['gid']}", "checked" => $default_checked, "onclick" => $default_click))." <small><label for=\"default_permissions_{$usergroup['gid']}\">{$lang->permissions_use_group_default}</label></small>");
1230  
1231          $field_select = "<div class=\"quick_perm_fields\">\n";
1232          $field_select .= "<div class=\"enabled\"><ul id=\"fields_enabled_{$usergroup['gid']}\">\n";
1233          foreach($perms_checked as $perm => $value)
1234          {
1235              if($value == 1)
1236              {
1237                  $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
1238              }
1239          }
1240          $field_select .= "</ul></div>\n";
1241          $field_select .= "<div class=\"disabled\"><ul id=\"fields_disabled_{$usergroup['gid']}\">\n";
1242          foreach($perms_checked as $perm => $value)
1243          {
1244              if($value == 0)
1245              {
1246                  $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
1247              }
1248          }
1249          $field_select .= "</ul></div></div>\n";
1250          $field_select .= $form->generate_hidden_field("fields_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, '1')), array('id' => 'fields_'.$usergroup['gid']));
1251          $field_select = str_replace("'", "\\'", $field_select);
1252          $field_select = str_replace("\n", "", $field_select);
1253  
1254          $field_select = "<script type=\"text/javascript\">
1255  //<![CDATA[
1256  document.write('".str_replace("/", "\/", $field_select)."');
1257  //]]>
1258  </script>\n";
1259  
1260          $field_selected = array();
1261          foreach($field_list as $forum_permission => $permission_title)
1262          {
1263              $field_options[$forum_permission] = $permission_title;
1264              if($perms_checked[$forum_permission])
1265              {
1266                  $field_selected[] = $forum_permission;
1267              }
1268          }
1269  
1270          $field_select .= "<noscript>".$form->generate_select_box('fields_'.$usergroup['gid'].'[]', $field_options, $field_selected, array('id' => 'fields_'.$usergroup['gid'].'[]', 'multiple' => true))."</noscript>\n";
1271          $form_container->output_cell($field_select, array('colspan' => 2));
1272  
1273          $form_container->construct_row();
1274  
1275          $ids[] = $usergroup['gid'];
1276      }
1277      $form_container->end();
1278  
1279      $buttons[] = $form->generate_submit_button($lang->save_forum);
1280      $form->output_submit_wrapper($buttons);
1281      $form->end();
1282  
1283      // Write in our JS based field selector
1284      echo "<script type=\"text/javascript\">\n<!--\n";
1285      foreach($ids as $id)
1286      {
1287          echo "$(function() { QuickPermEditor.init(".$id.") });\n";
1288      }
1289      echo "// -->\n</script>\n";
1290  
1291      $page->output_footer();
1292  }
1293  
1294  if($mybb->input['action'] == "edit")
1295  {
1296      if(!$mybb->input['fid'])
1297      {
1298          flash_message($lang->error_invalid_fid, 'error');
1299          admin_redirect("index.php?module=forum-management");
1300      }
1301  
1302      $query = $db->simple_select("forums", "*", "fid='{$mybb->input['fid']}'");
1303      $forum_data = $db->fetch_array($query);
1304      if(!$forum_data)
1305      {
1306          flash_message($lang->error_invalid_fid, 'error');
1307          admin_redirect("index.php?module=forum-management");
1308      }
1309  
1310      $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
1311  
1312      $plugins->run_hooks("admin_forum_management_edit");
1313  
1314      if($mybb->request_method == "post")
1315      {
1316          if(!trim($mybb->input['title']))
1317          {
1318              $errors[] = $lang->error_missing_title;
1319          }
1320  
1321          $pid = $mybb->get_input('pid', MyBB::INPUT_INT);
1322  
1323          if($pid == $mybb->input['fid'])
1324          {
1325              $errors[] = $lang->error_forum_parent_itself;
1326          }
1327          else
1328          {
1329              $query = $db->simple_select('forums', 'parentlist', "fid='{$pid}'");
1330              $parents = explode(',', $db->fetch_field($query, 'parentlist'));
1331              if(in_array($mybb->input['fid'], $parents))
1332              {
1333                  $errors[] = $lang->error_forum_parent_child;
1334              }
1335          }
1336  
1337          $type = $mybb->input['type'];
1338  
1339          if($pid <= 0 && $type == "f")
1340          {
1341              $errors[] = $lang->error_no_parent;
1342          }
1343  
1344          if($type == 'c' && $forum_data['type'] == 'f')
1345          {
1346              $query = $db->simple_select('threads', 'COUNT(tid) as num_threads', "fid = '{$fid}'");
1347              if($db->fetch_field($query, "num_threads") > 0)
1348              {
1349                  $errors[] = $lang->error_not_empty;
1350              }
1351          }
1352  
1353          if(!empty($mybb->input['linkto']) && empty($forum_data['linkto']))
1354          {
1355              $query = $db->simple_select('threads', 'COUNT(tid) as num_threads', "fid = '{$fid}'", array("limit" => 1));
1356              if($db->fetch_field($query, "num_threads") > 0)
1357              {
1358                  $errors[] = $lang->error_forum_link_not_empty;
1359              }
1360          }
1361  
1362          if(!$errors)
1363          {
1364              if($pid < 0)
1365              {
1366                  $pid = 0;
1367              }
1368              $update_array = array(
1369                  "name" => $db->escape_string($mybb->input['title']),
1370                  "description" => $db->escape_string($mybb->input['description']),
1371                  "linkto" => $db->escape_string($mybb->input['linkto']),
1372                  "type" => $db->escape_string($type),
1373                  "pid" => $pid,
1374                  "disporder" => $mybb->get_input('disporder', MyBB::INPUT_INT),
1375                  "active" => $mybb->get_input('active', MyBB::INPUT_INT),
1376                  "open" => $mybb->get_input('open', MyBB::INPUT_INT),
1377                  "allowhtml" => $mybb->get_input('allowhtml', MyBB::INPUT_INT),
1378                  "allowmycode" => $mybb->get_input('allowmycode', MyBB::INPUT_INT),
1379                  "allowsmilies" => $mybb->get_input('allowsmilies', MyBB::INPUT_INT),
1380                  "allowimgcode" => $mybb->get_input('allowimgcode', MyBB::INPUT_INT),
1381                  "allowvideocode" => $mybb->get_input('allowvideocode', MyBB::INPUT_INT),
1382                  "allowpicons" => $mybb->get_input('allowpicons', MyBB::INPUT_INT),
1383                  "allowtratings" => $mybb->get_input('allowtratings', MyBB::INPUT_INT),
1384                  "usepostcounts" => $mybb->get_input('usepostcounts', MyBB::INPUT_INT),
1385                  "usethreadcounts" => $mybb->get_input('usethreadcounts', MyBB::INPUT_INT),
1386                  "requireprefix" => $mybb->get_input('requireprefix', MyBB::INPUT_INT),
1387                  "password" => $db->escape_string($mybb->input['password']),
1388                  "showinjump" => $mybb->get_input('showinjump', MyBB::INPUT_INT),
1389                  "style" => $mybb->get_input('style', MyBB::INPUT_INT),
1390                  "overridestyle" => $mybb->get_input('overridestyle', MyBB::INPUT_INT),
1391                  "rulestype" => $mybb->get_input('rulestype', MyBB::INPUT_INT),
1392                  "rulestitle" => $db->escape_string($mybb->input['rulestitle']),
1393                  "rules" => $db->escape_string($mybb->input['rules']),
1394                  "defaultdatecut" => $mybb->get_input('defaultdatecut', MyBB::INPUT_INT),
1395                  "defaultsortby" => $db->escape_string($mybb->input['defaultsortby']),
1396                  "defaultsortorder" => $db->escape_string($mybb->input['defaultsortorder']),
1397              );
1398              $db->update_query("forums", $update_array, "fid='{$fid}'");
1399              if($pid != $forum_data['pid'])
1400              {
1401                  // Update the parentlist of this forum.
1402                  $db->update_query("forums", array("parentlist" => make_parent_list($fid)), "fid='{$fid}'");
1403  
1404                  // Rebuild the parentlist of all of the subforums of this forum
1405                  switch($db->type)
1406                  {
1407                      case "sqlite":
1408                      case "pgsql":
1409                          $query = $db->simple_select("forums", "fid", "','||parentlist||',' LIKE '%,$fid,%'");
1410                          break;
1411                      default:
1412                          $query = $db->simple_select("forums", "fid", "CONCAT(',',parentlist,',') LIKE '%,$fid,%'");
1413                  }
1414  
1415                  while($child = $db->fetch_array($query))
1416                  {
1417                      $db->update_query("forums", array("parentlist" => make_parent_list($child['fid'])), "fid='{$child['fid']}'");
1418                  }
1419              }
1420  
1421              $inherit = $mybb->input['default_permissions'];
1422  
1423              foreach($mybb->input as $id => $permission)
1424              {
1425                  // Make sure we're only skipping inputs that don't start with "fields_" and aren't fields_default_ or fields_inherit_
1426                  if(strpos($id, 'fields_') === false || (strpos($id, 'fields_default_') !== false || strpos($id, 'fields_inherit_') !== false))
1427                  {
1428                      continue;
1429                  }
1430  
1431                  list(, $gid) = explode('fields_', $id);
1432  
1433                  if($mybb->input['fields_default_'.$gid] == $permission && $mybb->input['fields_inherit_'.$gid] == 1)
1434                  {
1435                      $inherit[$gid] = 1;
1436                      continue;
1437                  }
1438                  $inherit[$gid] = 0;
1439  
1440                  // If it isn't an array then it came from the javascript form
1441                  if(!is_array($permission))
1442                  {
1443                      $permission = explode(',', $permission);
1444                      $permission = array_flip($permission);
1445                      foreach($permission as $name => $value)
1446                      {
1447                          $permission[$name] = 1;
1448                      }
1449                  }
1450  
1451                  foreach(array('canview','canpostthreads','canpostreplys','canpostpolls') as $name)
1452                  {
1453                      if(in_array($name, $permission) || $permission[$name])
1454                      {
1455                          $permissions[$name][$gid] = 1;
1456                      }
1457                      else
1458                      {
1459                          $permissions[$name][$gid] = 0;
1460                      }
1461                  }
1462              }
1463  
1464              $cache->update_forums();
1465  
1466              $canview = $permissions['canview'];
1467              $canpostthreads = $permissions['canpostthreads'];
1468              $canpostpolls = $permissions['canpostpolls'];
1469              $canpostattachments = $permissions['canpostattachments'];
1470              $canpostreplies = $permissions['canpostreplys'];
1471  
1472              save_quick_perms($fid);
1473  
1474              $plugins->run_hooks("admin_forum_management_edit_commit");
1475  
1476              // Log admin action
1477              log_admin_action($fid, $mybb->input['title']);
1478  
1479              flash_message($lang->success_forum_updated, 'success');
1480              admin_redirect("index.php?module=forum-management&fid={$fid}");
1481          }
1482      }
1483  
1484      $page->extra_header .=  "<script src=\"jscripts/quick_perm_editor.js\" type=\"text/javascript\"></script>\n";
1485  
1486      $page->add_breadcrumb_item($lang->edit_forum);
1487      $page->output_header($lang->edit_forum);
1488  
1489      $page->output_nav_tabs($sub_tabs, 'edit_forum_settings');
1490  
1491      $form = new Form("index.php?module=forum-management&amp;action=edit", "post");
1492      echo $form->generate_hidden_field("fid", $fid);
1493  
1494      if($errors)
1495      {
1496          $page->output_inline_error($errors);
1497          $forum_data = $mybb->input;
1498      }
1499      else
1500      {
1501          $forum_data['title'] = $forum_data['name'];
1502      }
1503  
1504      $query = $db->simple_select("usergroups", "*", "", array("order_dir" => "name"));
1505      while($usergroup = $db->fetch_array($query))
1506      {
1507          $usergroups[$usergroup['gid']] = $usergroup;
1508      }
1509  
1510      $query = $db->simple_select("forumpermissions", "*", "fid='{$fid}'");
1511      while($existing = $db->fetch_array($query))
1512      {
1513          $existing_permissions[$existing['gid']] = $existing;
1514      }
1515  
1516      $types = array(
1517          'f' => $lang->forum,
1518          'c' => $lang->category
1519      );
1520  
1521      $create_a_options_f = array(
1522          'id' => 'forum'
1523      );
1524  
1525      $create_a_options_c = array(
1526          'id' => 'category'
1527      );
1528  
1529      if($forum_data['type'] == "f")
1530      {
1531          $create_a_options_f['checked'] = true;
1532      }
1533      else
1534      {
1535          $create_a_options_c['checked'] = true;
1536      }
1537  
1538      $form_container = new FormContainer($lang->edit_forum);
1539      $form_container->output_row($lang->forum_type, $lang->forum_type_desc, $form->generate_radio_button('type', 'f', $lang->forum, $create_a_options_f)."<br />\n".$form->generate_radio_button('type', 'c', $lang->category, $create_a_options_c));
1540      $form_container->output_row($lang->title." <em>*</em>", "", $form->generate_text_box('title', $forum_data['title'], array('id' => 'title')), 'title');
1541      $form_container->output_row($lang->description, "", $form->generate_text_area('description', $forum_data['description'], array('id' => 'description')), 'description');
1542      $form_container->output_row($lang->parent_forum." <em>*</em>", $lang->parent_forum_desc, $form->generate_forum_select('pid', $forum_data['pid'], array('id' => 'pid', 'main_option' => $lang->none)), 'pid');
1543      $form_container->output_row($lang->display_order, "", $form->generate_numeric_field('disporder', $forum_data['disporder'], array('id' => 'disporder', 'min' => 0)), 'disporder');
1544      $form_container->end();
1545  
1546      $form_container = new FormContainer($lang->additional_forum_options);
1547      $form_container->output_row($lang->forum_link, $lang->forum_link_desc, $form->generate_text_box('linkto', $forum_data['linkto'], array('id' => 'linkto')), 'linkto');
1548      $form_container->output_row($lang->forum_password, $lang->forum_password_desc, $form->generate_text_box('password', $forum_data['password'], array('id' => 'password')), 'password');
1549  
1550      $access_options = array(
1551          $form->generate_check_box('active', 1, $lang->forum_is_active."<br />\n<small>{$lang->forum_is_active_desc}</small>", array('checked' => $forum_data['active'], 'id' => 'active')),
1552          $form->generate_check_box('open', 1, $lang->forum_is_open."<br />\n<small>{$lang->forum_is_open_desc}</small>", array('checked' => $forum_data['open'], 'id' => 'open'))
1553      );
1554  
1555      $form_container->output_row($lang->access_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $access_options)."</div>");
1556  
1557      $styles = array(
1558          '0' => $lang->use_default
1559      );
1560  
1561      $query = $db->simple_select("themes", "tid,name", "name!='((master))' AND name!='((master-backup))'", array('order_by' => 'name'));
1562      while($style = $db->fetch_array($query))
1563      {
1564          $styles[$style['tid']] = $style['name'];
1565      }
1566  
1567      $style_options = array(
1568          $form->generate_check_box('overridestyle', 1, $lang->override_user_style, array('checked' => $forum_data['overridestyle'], 'id' => 'overridestyle')),
1569          $lang->forum_specific_style."<br />\n".$form->generate_select_box('style', $styles, $forum_data['style'], array('id' => 'style'))
1570      );
1571  
1572      $form_container->output_row($lang->style_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $style_options)."</div>");
1573  
1574      $display_methods = array(
1575          '0' => $lang->dont_display_rules,
1576          '1' => $lang->display_rules_inline,
1577          '3' => $lang->display_rules_inline_new,
1578          '2' => $lang->display_rules_link
1579      );
1580  
1581      $forum_rules = array(
1582          $lang->display_method."<br />\n".$form->generate_select_box('rulestype', $display_methods, $forum_data['rulestype'], array('checked' => $forum_data['rulestype'], 'id' => 'rulestype')),
1583          $lang->title."<br />\n".$form->generate_text_box('rulestitle', $forum_data['rulestitle'], array('checked' => $forum_data['rulestitle'], 'id' => 'rulestitle')),
1584          $lang->rules."<br />\n".$form->generate_text_area('rules', $forum_data['rules'], array('checked' => $forum_data['rules'], 'id' => 'rules'))
1585      );
1586  
1587      $form_container->output_row($lang->forum_rules, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $forum_rules)."</div>");
1588  
1589      $default_date_cut = array(
1590          0 => $lang->board_default,
1591          1 => $lang->datelimit_1day,
1592          5 => $lang->datelimit_5days,
1593          10 => $lang->datelimit_10days,
1594          20 => $lang->datelimit_20days,
1595          50 => $lang->datelimit_50days,
1596          75 => $lang->datelimit_75days,
1597          100 => $lang->datelimit_100days,
1598          365 => $lang->datelimit_lastyear,
1599          9999 => $lang->datelimit_beginning,
1600      );
1601  
1602      $default_sort_by = array(
1603          "" => $lang->board_default,
1604          "subject" => $lang->sort_by_subject,
1605          "lastpost" => $lang->sort_by_lastpost,
1606          "starter" => $lang->sort_by_starter,
1607          "started" => $lang->sort_by_started,
1608          "rating" => $lang->sort_by_rating,
1609          "replies" => $lang->sort_by_replies,
1610          "views" => $lang->sort_by_views,
1611      );
1612  
1613      $default_sort_order = array(
1614          "" => $lang->board_default,
1615          "asc" => $lang->sort_order_asc,
1616          "desc" => $lang->sort_order_desc,
1617      );
1618  
1619      $view_options = array(
1620          $lang->default_date_cut."<br />\n".$form->generate_select_box('defaultdatecut', $default_date_cut, $forum_data['defaultdatecut'], array('checked' => $forum_data['defaultdatecut'], 'id' => 'defaultdatecut')),
1621          $lang->default_sort_by."<br />\n".$form->generate_select_box('defaultsortby', $default_sort_by, $forum_data['defaultsortby'], array('checked' => $forum_data['defaultsortby'], 'id' => 'defaultsortby')),
1622          $lang->default_sort_order."<br />\n".$form->generate_select_box('defaultsortorder', $default_sort_order, $forum_data['defaultsortorder'], array('checked' => $forum_data['defaultsortorder'], 'id' => 'defaultsortorder')),
1623      );
1624  
1625      $form_container->output_row($lang->default_view_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $view_options)."</div>");
1626  
1627      $misc_options = array(
1628          $form->generate_check_box('allowhtml', 1, $lang->allow_html, array('checked' => $forum_data['allowhtml'], 'id' => 'allowhtml')),
1629          $form->generate_check_box('allowmycode', 1, $lang->allow_mycode, array('checked' => $forum_data['allowmycode'], 'id' => 'allowmycode')),
1630          $form->generate_check_box('allowsmilies', 1, $lang->allow_smilies, array('checked' => $forum_data['allowsmilies'], 'id' => 'allowsmilies')),
1631          $form->generate_check_box('allowimgcode', 1, $lang->allow_img_code, array('checked' => $forum_data['allowimgcode'], 'id' => 'allowimgcode')),
1632          $form->generate_check_box('allowvideocode', 1, $lang->allow_video_code, array('checked' => $forum_data['allowvideocode'], 'id' => 'allowvideocode')),
1633          $form->generate_check_box('allowpicons', 1, $lang->allow_post_icons, array('checked' => $forum_data['allowpicons'], 'id' => 'allowpicons')),
1634          $form->generate_check_box('allowtratings', 1, $lang->allow_thread_ratings, array('checked' => $forum_data['allowtratings'], 'id' => 'allowtratings')),
1635          $form->generate_check_box('showinjump', 1, $lang->show_forum_jump, array('checked' => $forum_data['showinjump'], 'id' => 'showinjump')),
1636          $form->generate_check_box('usepostcounts', 1, $lang->use_postcounts, array('checked' => $forum_data['usepostcounts'], 'id' => 'usepostcounts')),
1637          $form->generate_check_box('usethreadcounts', 1, $lang->use_threadcounts, array('checked' => $forum_data['usethreadcounts'], 'id' => 'usethreadcounts')),
1638          $form->generate_check_box('requireprefix', 1, $lang->require_thread_prefix, array('checked' => $forum_data['requireprefix'], 'id' => 'requireprefix'))
1639      );
1640  
1641      $form_container->output_row($lang->misc_options, "", "<div class=\"forum_settings_bit\">".implode("</div><div class=\"forum_settings_bit\">", $misc_options)."</div>");
1642      $form_container->end();
1643  
1644      $cached_forum_perms = $cache->read("forumpermissions");
1645      $field_list = array(
1646          'canview' => $lang->permissions_canview,
1647          'canpostthreads' => $lang->permissions_canpostthreads,
1648          'canpostreplys' => $lang->permissions_canpostreplys,
1649          'canpostpolls' => $lang->permissions_canpostpolls,
1650      );
1651  
1652      $field_list2 = array(
1653          'canview' => $lang->perm_drag_canview,
1654          'canpostthreads' => $lang->perm_drag_canpostthreads,
1655          'canpostreplys' => $lang->perm_drag_canpostreplys,
1656          'canpostpolls' => $lang->perm_drag_canpostpolls,
1657      );
1658  
1659      $ids = array();
1660  
1661      $form_container = new FormContainer($lang->sprintf($lang->forum_permissions_in, $forum_data['name']));
1662      $form_container->output_row_header($lang->permissions_group, array("class" => "align_center", 'style' => 'width: 30%'));
1663      $form_container->output_row_header($lang->overview_allowed_actions, array("class" => "align_center"));
1664      $form_container->output_row_header($lang->overview_disallowed_actions, array("class" => "align_center"));
1665      $form_container->output_row_header($lang->controls, array("class" => "align_center", 'style' => 'width: 120px', 'colspan' => 2));
1666  
1667      if($mybb->request_method == "post")
1668      {
1669          foreach($usergroups as $usergroup)
1670          {
1671              if(isset($mybb->input['fields_'.$usergroup['gid']]))
1672              {
1673                  $input_permissions = $mybb->input['fields_'.$usergroup['gid']];
1674                  if(!is_array($input_permissions))
1675                  {
1676                      // Convering the comma separated list from Javascript form into a variable
1677                      $input_permissions = explode(',' , $input_permissions);
1678                  }
1679                  foreach($input_permissions as $input_permission)
1680                  {
1681                      $mybb->input['permissions'][$usergroup['gid']][$input_permission] = 1;
1682                  }
1683              }
1684          }
1685      }
1686  
1687      foreach($usergroups as $usergroup)
1688      {
1689          $perms = array();
1690          if(isset($mybb->input['default_permissions']))
1691          {
1692              if($mybb->input['default_permissions'][$usergroup['gid']])
1693              {
1694                  if(is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
1695                  {
1696                      $perms = $existing_permissions[$usergroup['gid']];
1697                      $default_checked = false;
1698                  }
1699                  elseif(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['fid']][$usergroup['gid']])
1700                  {
1701                      $perms = $cached_forum_perms[$forum_data['fid']][$usergroup['gid']];
1702                      $default_checked = true;
1703                  }
1704                  else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['pid']][$usergroup['gid']])
1705                  {
1706                      $perms = $cached_forum_perms[$forum_data['pid']][$usergroup['gid']];
1707                      $default_checked = true;
1708                  }
1709              }
1710  
1711              if(!$perms)
1712              {
1713                  $perms = $usergroup;
1714                  $default_checked = true;
1715              }
1716          }
1717          else
1718          {
1719              if(is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
1720              {
1721                  $perms = $existing_permissions[$usergroup['gid']];
1722                  $default_checked = false;
1723              }
1724              elseif(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['fid']][$usergroup['gid']])
1725              {
1726                  $perms = $cached_forum_perms[$forum_data['fid']][$usergroup['gid']];
1727                  $default_checked = true;
1728              }
1729              else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['pid']][$usergroup['gid']])
1730              {
1731                  $perms = $cached_forum_perms[$forum_data['pid']][$usergroup['gid']];
1732                  $default_checked = true;
1733              }
1734  
1735              if(!$perms)
1736              {
1737                  $perms = $usergroup;
1738                  $default_checked = true;
1739              }
1740          }
1741  
1742          foreach($field_list as $forum_permission => $forum_perm_title)
1743          {
1744              if(isset($mybb->input['permissions']))
1745              {
1746                  if($mybb->input['permissions'][$usergroup['gid']][$forum_permission])
1747                  {
1748                      $perms_checked[$forum_permission] = 1;
1749                  }
1750                  else
1751                  {
1752                      $perms_checked[$forum_permission] = 0;
1753                  }
1754              }
1755              else
1756              {
1757                  if($perms[$forum_permission] == 1)
1758                  {
1759                      $perms_checked[$forum_permission] = 1;
1760                  }
1761                  else
1762                  {
1763                      $perms_checked[$forum_permission] = 0;
1764                  }
1765              }
1766          }
1767          $usergroup['title'] = htmlspecialchars_uni($usergroup['title']);
1768  
1769          if($default_checked)
1770          {
1771              $inherited_text = $lang->inherited_permission;
1772          }
1773          else
1774          {
1775              $inherited_text = $lang->custom_permission;
1776          }
1777  
1778          $form_container->output_cell("<strong>{$usergroup['title']}</strong> <small style=\"vertical-align: middle;\">({$inherited_text})</small>");
1779  
1780          $field_select = "<div class=\"quick_perm_fields\">\n";
1781          $field_select .= "<div class=\"enabled\"><div class=\"fields_title\">{$lang->enabled}</div><ul id=\"fields_enabled_{$usergroup['gid']}\">\n";
1782          foreach($perms_checked as $perm => $value)
1783          {
1784              if($value == 1)
1785              {
1786                  $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
1787              }
1788          }
1789          $field_select .= "</ul></div>\n";
1790          $field_select .= "<div class=\"disabled\"><div class=\"fields_title\">{$lang->disabled}</div><ul id=\"fields_disabled_{$usergroup['gid']}\">\n";
1791          foreach($perms_checked as $perm => $value)
1792          {
1793              if($value == 0)
1794              {
1795                  $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
1796              }
1797          }
1798          $field_select .= "</ul></div></div>\n";
1799          $field_select .= $form->generate_hidden_field("fields_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, '1')), array('id' => 'fields_'.$usergroup['gid']));
1800          $field_select .= $form->generate_hidden_field("fields_inherit_".$usergroup['gid'], (int)$default_checked, array('id' => 'fields_inherit_'.$usergroup['gid']));
1801          $field_select .= $form->generate_hidden_field("fields_default_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, '1')), array('id' => 'fields_default_'.$usergroup['gid']));
1802          $field_select = str_replace("'", "\\'", $field_select);
1803          $field_select = str_replace("\n", "", $field_select);
1804  
1805          $field_select = "<script type=\"text/javascript\">
1806  //<![CDATA[
1807  document.write('".str_replace("/", "\/", $field_select)."');
1808  //]]>
1809  </script>\n";
1810  
1811          $field_selected = array();
1812          foreach($field_list as $forum_permission => $permission_title)
1813          {
1814              $field_options[$forum_permission] = $permission_title;
1815              if($perms_checked[$forum_permission])
1816              {
1817                  $field_selected[] = $forum_permission;
1818              }
1819          }
1820  
1821          $field_select .= "<noscript>".$form->generate_select_box('fields_'.$usergroup['gid'].'[]', $field_options, $field_selected, array('id' => 'fields_'.$usergroup['gid'].'[]', 'multiple' => true))."</noscript>\n";
1822          $form_container->output_cell($field_select, array('colspan' => 2));
1823          
1824          if(!$default_checked)
1825          {
1826              $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=permissions&amp;pid={$perms['pid']}\" onclick=\"MyBB.popupWindow('index.php?module=forum-management&action=permissions&pid={$perms['pid']}&ajax=1', null, true); return false;\">{$lang->edit_permissions}</a>", array("class" => "align_center"));
1827              $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=clear_permission&amp;pid={$perms['pid']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->confirm_clear_custom_permission}')\">{$lang->clear_custom_perms}</a>", array("class" => "align_center"));
1828          }
1829          else
1830          {
1831              $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=permissions&amp;gid={$usergroup['gid']}&amp;fid={$fid}\" onclick=\"MyBB.popupWindow('index.php?module=forum-management&action=permissions&gid={$usergroup['gid']}&fid={$fid}&ajax=1', null, true); return false;\">{$lang->set_custom_perms}</a>", array("class" => "align_center", "colspan" => 2));
1832          }
1833          
1834          $form_container->construct_row(array('id' => 'row_'.$usergroup['gid']));
1835  
1836          $ids[] = $usergroup['gid'];
1837      }
1838      $form_container->end();
1839  
1840      $buttons[] = $form->generate_submit_button($lang->save_forum);
1841      $form->output_submit_wrapper($buttons);
1842      $form->end();
1843  
1844      // Write in our JS based field selector
1845      echo "<script type=\"text/javascript\">\n<!--\n";
1846      foreach($ids as $id)
1847      {
1848          echo "$(function() { QuickPermEditor.init(".$id."); });\n";
1849      }
1850      echo "// -->\n</script>\n";
1851  
1852      $page->output_footer();
1853  }
1854  
1855  if($mybb->input['action'] == "deletemod")
1856  {
1857      $modid = $mybb->get_input('id', MyBB::INPUT_INT);
1858      $isgroup = $mybb->get_input('isgroup', MyBB::INPUT_INT);
1859      $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
1860  
1861      $query = $db->simple_select("moderators", "*", "id='{$modid}' AND isgroup = '{$isgroup}' AND fid='{$fid}'");
1862      $mod = $db->fetch_array($query);
1863  
1864      // Does the forum not exist?
1865      if(!$mod['mid'])
1866      {
1867          flash_message($lang->error_invalid_moderator, 'error');
1868          admin_redirect("index.php?module=forum-management&fid={$fid}");
1869      }
1870  
1871      // User clicked no
1872      if($mybb->input['no'])
1873      {
1874          admin_redirect("index.php?module=forum-management&fid={$fid}");
1875      }
1876  
1877      $plugins->run_hooks("admin_forum_management_deletemod");
1878  
1879      if($mybb->request_method == "post")
1880      {
1881          $mid = $mod['mid'];
1882          if($mybb->input['isgroup'])
1883          {
1884              $query = $db->query("
1885                  SELECT m.*, g.title
1886                  FROM ".TABLE_PREFIX."moderators m
1887                  LEFT JOIN ".TABLE_PREFIX."usergroups g ON (g.gid=m.id)
1888                  WHERE m.mid='{$mid}'
1889              ");
1890          }
1891          else
1892          {
1893              $query = $db->query("
1894                  SELECT m.*, u.username, u.usergroup
1895                  FROM ".TABLE_PREFIX."moderators m
1896                  LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=m.id)
1897                  WHERE m.mid='{$mid}'
1898              ");
1899          }
1900          $mod = $db->fetch_array($query);
1901  
1902          $db->delete_query("moderators", "mid='{$mid}'");
1903  
1904          $plugins->run_hooks("admin_forum_management_deletemod_commit");
1905  
1906          $cache->update_moderators();
1907  
1908          $forum = get_forum($fid);
1909  
1910          // Log admin action
1911          if($isgroup)
1912          {
1913              log_admin_action($mid, $mod['title'], $forum['fid'], $forum['name']);
1914          }
1915          else
1916          {
1917              log_admin_action($mid, $mod['username'], $forum['fid'], $forum['name']);
1918          }
1919  
1920          flash_message($lang->success_moderator_deleted, 'success');
1921          admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
1922      }
1923      else
1924      {
1925          $page->output_confirm_action("index.php?module=forum-management&amp;action=deletemod&amp;fid={$mod['fid']}&amp;uid={$mod['uid']}", $lang->confirm_moderator_deletion);
1926      }
1927  }
1928  
1929  if($mybb->input['action'] == "delete")
1930  {
1931      $query = $db->simple_select("forums", "*", "fid='{$mybb->input['fid']}'");
1932      $forum = $db->fetch_array($query);
1933  
1934      // Does the forum not exist?
1935      if(!$forum['fid'])
1936      {
1937          flash_message($lang->error_invalid_forum, 'error');
1938          admin_redirect("index.php?module=forum-management");
1939      }
1940  
1941      // User clicked no
1942      if($mybb->input['no'])
1943      {
1944          admin_redirect("index.php?module=forum-management");
1945      }
1946  
1947      $plugins->run_hooks("admin_forum_management_delete");
1948  
1949      if($mybb->request_method == "post")
1950      {
1951          $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
1952          $forum_info = get_forum($fid);
1953  
1954          $delquery = "";
1955          switch($db->type)
1956          {
1957              case "pgsql":
1958              case "sqlite":
1959                  $query = $db->simple_select("forums", "*", "','|| parentlist|| ',' LIKE '%,$fid,%'");
1960                  break;
1961              default:
1962                  $query = $db->simple_select("forums", "*", "CONCAT(',', parentlist, ',') LIKE '%,$fid,%'");
1963          }
1964          while($forum = $db->fetch_array($query))
1965          {
1966              $fids[$forum['fid']] = $fid;
1967              $delquery .= " OR fid='{$forum['fid']}'";
1968          }
1969  
1970          require_once  MYBB_ROOT.'inc/class_moderation.php';
1971          $moderation = new Moderation();
1972  
1973          // Start pagination. Limit results to 50
1974          $query = $db->simple_select("threads", "tid", "fid='{$fid}' {$delquery}", array("limit" => 50));
1975  
1976          while($tid = $db->fetch_field($query, 'tid'))
1977          {
1978              $moderation->delete_thread($tid);
1979          }
1980  
1981          // Check whether all threads have been deleted
1982          $query = $db->simple_select("threads", "tid", "fid='{$fid}' {$delquery}");
1983  
1984          if($db->num_rows($query) > 0)
1985          {
1986              $page->output_header();
1987  
1988              $form = new Form("index.php?module=forum-management", 'post');
1989  
1990              echo $form->generate_hidden_field("fid", $fid);
1991              echo $form->generate_hidden_field("action", "delete");
1992              output_auto_redirect($form, $lang->confirm_proceed_deletion);
1993  
1994              $form->end();
1995  
1996              $page->output_footer();
1997              exit;
1998          }
1999  
2000          // End pagination
2001  
2002          // Delete the forum
2003          $db->delete_query("forums", "fid='$fid'");
2004  
2005          // Delete subforums
2006          switch($db->type)
2007          {
2008              case "pgsql":
2009              case "sqlite":
2010                  $db->delete_query("forums", "','||parentlist||',' LIKE '%,$fid,%'");
2011                  break;
2012              default:
2013                  $db->delete_query("forums", "CONCAT(',',parentlist,',') LIKE '%,$fid,%'");
2014          }
2015  
2016          $db->delete_query('moderators', "fid='{$fid}' {$delquery}");
2017          $db->delete_query('forumsubscriptions', "fid='{$fid}' {$delquery}");
2018          $db->delete_query('forumpermissions', "fid='{$fid}' {$delquery}");
2019          $db->delete_query('announcements', "fid='{$fid}' {$delquery}");
2020          $db->delete_query('forumsread', "fid='{$fid}' {$delquery}");
2021  
2022          $plugins->run_hooks("admin_forum_management_delete_commit");
2023  
2024          $cache->update_forums();
2025          $cache->update_moderators();
2026          $cache->update_forumpermissions();
2027          $cache->update_forumsdisplay();
2028  
2029          // Log admin action
2030          log_admin_action($forum_info['fid'], $forum_info['name']);
2031  
2032          flash_message($lang->success_forum_deleted, 'success');
2033          admin_redirect("index.php?module=forum-management");
2034      }
2035      else
2036      {
2037          $page->output_confirm_action("index.php?module=forum-management&amp;action=delete&amp;fid={$forum['fid']}", $lang->confirm_forum_deletion);
2038      }
2039  }
2040  
2041  if(!$mybb->input['action'])
2042  {
2043      if(!isset($mybb->input['fid']))
2044      {
2045          $mybb->input['fid'] = 0;
2046      }
2047  
2048      $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
2049      if($fid)
2050      {
2051          $forum = get_forum($fid);
2052      }
2053  
2054      $plugins->run_hooks("admin_forum_management_start");
2055  
2056      if($mybb->request_method == "post")
2057      {
2058          if($mybb->input['update'] == "permissions")
2059          {
2060              $inherit = array();
2061              foreach($mybb->input as $id => $permission)
2062              {
2063                  // Make sure we're only skipping inputs that don't start with "fields_" and aren't fields_default_ or fields_inherit_
2064                  if(strpos($id, 'fields_') === false || (strpos($id, 'fields_default_') !== false || strpos($id, 'fields_inherit_') !== false))
2065                  {
2066                      continue;
2067                  }
2068  
2069                  list(, $gid) = explode('fields_', $id);
2070  
2071                  if($mybb->input['fields_default_'.$gid] == $permission && $mybb->input['fields_inherit_'.$gid] == 1)
2072                  {
2073                      $inherit[$gid] = 1;
2074                      continue;
2075                  }
2076                  $inherit[$gid] = 0;
2077  
2078                  // If it isn't an array then it came from the javascript form
2079                  if(!is_array($permission))
2080                  {
2081                      $permission = explode(',', $permission);
2082                      $permission = array_flip($permission);
2083                      foreach($permission as $name => $value)
2084                      {
2085                          $permission[$name] = 1;
2086                      }
2087                  }
2088                  foreach(array('canview','canpostthreads','canpostreplys','canpostpolls') as $name)
2089                  {
2090                      if($permission[$name])
2091                      {
2092                          $permissions[$name][$gid] = 1;
2093                      }
2094                      else
2095                      {
2096                          $permissions[$name][$gid] = 0;
2097                      }
2098                  }
2099              }
2100  
2101              $canview = $permissions['canview'];
2102              $canpostthreads = $permissions['canpostthreads'];
2103              $canpostpolls = $permissions['canpostpolls'];
2104              $canpostattachments = $permissions['canpostattachments'];
2105              $canpostreplies = $permissions['canpostreplys'];
2106  
2107              save_quick_perms($fid);
2108  
2109              $plugins->run_hooks("admin_forum_management_start_permissions_commit");
2110  
2111              $cache->update_forums();
2112  
2113              // Log admin action
2114              log_admin_action('quickpermissions', $fid, $forum['name']);
2115  
2116              flash_message($lang->success_forum_permissions_updated, 'success');
2117              admin_redirect("index.php?module=forum-management&fid={$fid}#tab_permissions");
2118          }
2119          elseif($mybb->input['add'] == "moderators")
2120          {
2121              $forum = get_forum($fid);
2122              if(!$forum)
2123              {
2124                  flash_message($lang->error_invalid_forum, 'error');
2125                  admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2126              }
2127              if(!empty($mybb->input['usergroup']))
2128              {
2129                  $isgroup = 1;
2130                  $gid = $mybb->get_input('usergroup', MyBB::INPUT_INT);
2131  
2132                  if(!$groupscache[$gid])
2133                   {
2134                       // Didn't select a valid moderator
2135                       flash_message($lang->error_moderator_not_found, 'error');
2136                       admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2137                   }
2138  
2139                  $newmod = array(
2140                      "id" => $gid,
2141                      "name" => $groupscache[$gid]['title']
2142                  );
2143              }
2144              else
2145              {
2146                  $options = array(
2147                      'fields' => array('uid AS id', 'username AS name')
2148                  );
2149                  $newmod = get_user_by_username($mybb->input['username'], $options);
2150  
2151                  if(empty($newmod['id']))
2152                  {
2153                      flash_message($lang->error_moderator_not_found, 'error');
2154                      admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2155                  }
2156  
2157                  $isgroup = 0;
2158              }
2159  
2160              if($newmod['id'])
2161              {
2162                  $query = $db->simple_select("moderators", "id", "id='".$newmod['id']."' AND fid='".$fid."' AND isgroup='{$isgroup}'", array('limit' => 1));
2163  
2164                  if(!$db->num_rows($query))
2165                  {
2166                      $new_mod = array(
2167                          "fid" => $fid,
2168                          "id" => $newmod['id'],
2169                          "isgroup" => $isgroup,
2170                          "caneditposts" => 1,
2171                          "cansoftdeleteposts" => 1,
2172                          "canrestoreposts" => 1,
2173                          "candeleteposts" => 1,
2174                          "cansoftdeletethreads" => 1,
2175                          "canrestorethreads" => 1,
2176                          "candeletethreads" => 1,
2177                          "canviewips" => 1,
2178                          "canviewunapprove" => 1,
2179                          "canviewdeleted" => 1,
2180                          "canopenclosethreads" => 1,
2181                          "canstickunstickthreads" => 1,
2182                          "canapproveunapprovethreads" => 1,
2183                          "canapproveunapproveposts" => 1,
2184                          "canapproveunapproveattachs" => 1,
2185                          "canmanagethreads" => 1,
2186                          "canmanagepolls" => 1,
2187                          "canpostclosedthreads" => 1,
2188                          "canmovetononmodforum" => 1,
2189                          "canusecustomtools" => 1,
2190                          "canmanageannouncements" => 1,
2191                          "canmanagereportedposts" => 1,
2192                          "canviewmodlog" => 1
2193                      );
2194  
2195                      $mid = $db->insert_query("moderators", $new_mod);
2196  
2197                      if(!$isgroup)
2198                      {
2199                          $db->update_query("users", array('usergroup' => 6), "uid='{$newmod['id']}' AND usergroup='2'");
2200                      }
2201  
2202                      $plugins->run_hooks("admin_forum_management_start_moderators_commit");
2203  
2204                      $cache->update_moderators();
2205  
2206                      // Log admin action
2207                      log_admin_action('addmod', $mid, $newmod['name'], $fid, $forum['name']);
2208  
2209                      flash_message($lang->success_moderator_added, 'success');
2210                      admin_redirect("index.php?module=forum-management&action=editmod&mid={$mid}");
2211                  }
2212                  else
2213                  {
2214                      flash_message($lang->error_moderator_already_added, 'error');
2215                      admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2216                  }
2217              }
2218              else
2219              {
2220                  flash_message($lang->error_moderator_not_found, 'error');
2221                  admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2222              }
2223          }
2224          else
2225          {
2226              if(!empty($mybb->input['disporder']) && is_array($mybb->input['disporder']))
2227              {
2228                  foreach($mybb->input['disporder'] as $update_fid => $order)
2229                  {
2230                      $db->update_query("forums", array('disporder' => (int)$order), "fid='".(int)$update_fid."'");
2231                  }
2232  
2233                  $plugins->run_hooks("admin_forum_management_start_disporder_commit");
2234  
2235                  $cache->update_forums();
2236  
2237                  // Log admin action
2238                  log_admin_action('orders', $forum['fid'], $forum['name']);
2239  
2240                  flash_message($lang->success_forum_disporder_updated, 'success');
2241                  admin_redirect("index.php?module=forum-management&fid=".$mybb->input['fid']);
2242              }
2243          }
2244      }
2245  
2246      $page->extra_header .=  "<script src=\"jscripts/quick_perm_editor.js\" type=\"text/javascript\"></script>\n";
2247  
2248      if($fid)
2249      {
2250          $page->add_breadcrumb_item($lang->view_forum, "index.php?module=forum-management");
2251      }
2252  
2253      $page->output_header($lang->forum_management);
2254  
2255      if($fid)
2256      {
2257          $page->output_nav_tabs($sub_tabs, 'view_forum');
2258      }
2259      else
2260      {
2261          $page->output_nav_tabs($sub_tabs, 'forum_management');
2262      }
2263  
2264      $form = new Form("index.php?module=forum-management", "post", "management");
2265      echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2266  
2267      if($fid)
2268      {
2269          $tabs = array(
2270              'subforums' => $lang->subforums,
2271              'permissions' => $lang->forum_permissions,
2272              'moderators' => $lang->moderators,
2273          );
2274          $tabs = $plugins->run_hooks("admin_forum_management_start_graph_tabs", $tabs);
2275          $page->output_tab_control($tabs);
2276  
2277          echo "<div id=\"tab_subforums\">\n";
2278          if(!isset($forum_cache) || !is_array($forum_cache))
2279          {
2280              cache_forums();
2281          }
2282          $form_container = new FormContainer($lang->sprintf($lang->in_forums, $forum_cache[$fid]['name']));
2283      }
2284      else
2285      {
2286          $form_container = new FormContainer($lang->manage_forums);
2287      }
2288      $form_container->output_row_header($lang->forum);
2289      $form_container->output_row_header($lang->order, array("class" => "align_center", 'width' => '5%'));
2290      $form_container->output_row_header($lang->controls, array("class" => "align_center", 'style' => 'width: 200px'));
2291  
2292      build_admincp_forums_list($form_container, $form, $fid);
2293  
2294      $submit_options = array();
2295  
2296      if($form_container->num_rows() == 0)
2297      {
2298          $form_container->output_cell($lang->no_forums, array('colspan' => 3));
2299          $form_container->construct_row();
2300          $submit_options = array('disabled' => true);
2301      }
2302  
2303      $form_container->end();
2304  
2305      $buttons[] = $form->generate_submit_button($lang->update_forum_orders, $submit_options);
2306      $buttons[] = $form->generate_reset_button($lang->reset);
2307  
2308      $form->output_submit_wrapper($buttons);
2309  
2310      if(!$fid)
2311      {
2312          $form->end();
2313      }
2314  
2315      if($fid)
2316      {
2317          echo "</div>\n";
2318          $form->end();
2319  
2320          $query = $db->simple_select("usergroups", "*", "", array("order" => "name"));
2321          while($usergroup = $db->fetch_array($query))
2322          {
2323              $usergroups[$usergroup['gid']] = $usergroup;
2324          }
2325  
2326          $query = $db->simple_select("forumpermissions", "*", "fid='{$fid}'");
2327          while($existing = $db->fetch_array($query))
2328          {
2329              $existing_permissions[$existing['gid']] = $existing;
2330          }
2331  
2332          $cached_forum_perms = $cache->read("forumpermissions");
2333          $field_list = array(
2334              'canview' => $lang->permissions_canview,
2335              'canpostthreads' => $lang->permissions_canpostthreads,
2336              'canpostreplys' => $lang->permissions_canpostreplys,
2337              'canpostpolls' => $lang->permissions_canpostpolls,
2338          );
2339  
2340          $field_list2 = array(
2341              'canview' => $lang->perm_drag_canview,
2342              'canpostthreads' => $lang->perm_drag_canpostthreads,
2343              'canpostreplys' => $lang->perm_drag_canpostreplys,
2344              'canpostpolls' => $lang->perm_drag_canpostpolls,
2345          );
2346  
2347          $ids = array();
2348  
2349          $form = new Form("index.php?module=forum-management", "post", "management");
2350          echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2351          echo $form->generate_hidden_field("update", "permissions");
2352  
2353          echo "<div id=\"tab_permissions\">\n";
2354  
2355          $form_container = new FormContainer($lang->sprintf($lang->forum_permissions_in, $forum_cache[$fid]['name']));
2356          $form_container->output_row_header($lang->permissions_group, array("class" => "align_center", 'style' => 'width: 30%'));
2357          $form_container->output_row_header($lang->overview_allowed_actions, array("class" => "align_center"));
2358          $form_container->output_row_header($lang->overview_disallowed_actions, array("class" => "align_center"));
2359          $form_container->output_row_header($lang->controls, array("class" => "align_center", 'style' => 'width: 120px', 'colspan' => 2));
2360          foreach($usergroups as $usergroup)
2361          {
2362              $perms = array();
2363              if(isset($mybb->input['default_permissions']))
2364              {
2365                  if($mybb->input['default_permissions'][$usergroup['gid']])
2366                  {
2367                      if(is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
2368                      {
2369                          $perms = $existing_permissions[$usergroup['gid']];
2370                          $default_checked = false;
2371                      }
2372                      elseif(is_array($cached_forum_perms) && $cached_forum_perms[$forum['fid']][$usergroup['gid']])
2373                      {
2374                          $perms = $cached_forum_perms[$forum['fid']][$usergroup['gid']];
2375                          $default_checked = true;
2376                      }
2377                      else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum['pid']][$usergroup['gid']])
2378                      {
2379                          $perms = $cached_forum_perms[$forum['pid']][$usergroup['gid']];
2380                          $default_checked = true;
2381                      }
2382                  }
2383  
2384                  if(!$perms)
2385                  {
2386                      $perms = $usergroup;
2387                      $default_checked = true;
2388                  }
2389              }
2390              else
2391              {
2392                  if(isset($existing_permissions) && is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
2393                  {
2394                      $perms = $existing_permissions[$usergroup['gid']];
2395                      $default_checked = false;
2396                  }
2397                  elseif(is_array($cached_forum_perms) && isset($cached_forum_perms[$forum['fid']]) && $cached_forum_perms[$forum['fid']][$usergroup['gid']])
2398                  {
2399                      $perms = $cached_forum_perms[$forum['fid']][$usergroup['gid']];
2400                      $default_checked = true;
2401                  }
2402                  else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum['pid']][$usergroup['gid']])
2403                  {
2404                      $perms = $cached_forum_perms[$forum['pid']][$usergroup['gid']];
2405                      $default_checked = true;
2406                  }
2407  
2408                  if(!$perms)
2409                  {
2410                      $perms = $usergroup;
2411                      $default_checked = true;
2412                  }
2413              }
2414              foreach($field_list as $forum_permission => $forum_perm_title)
2415              {
2416                  if(isset($mybb->input['permissions']))
2417                  {
2418                      if($mybb->input['permissions'][$usergroup['gid']][$forum_permission])
2419                      {
2420                          $perms_checked[$forum_permission] = 1;
2421                      }
2422                      else
2423                      {
2424                          $perms_checked[$forum_permission] = 0;
2425                      }
2426                  }
2427                  else
2428                  {
2429                      if($perms[$forum_permission] == 1)
2430                      {
2431                          $perms_checked[$forum_permission] = 1;
2432                      }
2433                      else
2434                      {
2435                          $perms_checked[$forum_permission] = 0;
2436                      }
2437                  }
2438              }
2439              $usergroup['title'] = htmlspecialchars_uni($usergroup['title']);
2440  
2441              if($default_checked == 1)
2442              {
2443                  $inherited_text = $lang->inherited_permission;
2444              }
2445              else
2446              {
2447                  $inherited_text = $lang->custom_permission;
2448              }
2449  
2450              $form_container->output_cell("<strong>{$usergroup['title']}</strong> <small style=\"vertical-align: middle;\">({$inherited_text})</small>");
2451  
2452              $field_select = "<div class=\"quick_perm_fields\">\n";
2453              $field_select .= "<div class=\"enabled\"><ul id=\"fields_enabled_{$usergroup['gid']}\">\n";
2454              foreach($perms_checked as $perm => $value)
2455              {
2456                  if($value == 1)
2457                  {
2458                      $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2459                  }
2460              }
2461              $field_select .= "</ul></div>\n";
2462              $field_select .= "<div class=\"disabled\"><ul id=\"fields_disabled_{$usergroup['gid']}\">\n";
2463              foreach($perms_checked as $perm => $value)
2464              {
2465                  if($value == 0)
2466                  {
2467                      $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2468                  }
2469              }
2470              $field_select .= "</ul></div></div>\n";
2471              $field_select .= $form->generate_hidden_field("fields_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, '1')), array('id' => 'fields_'.$usergroup['gid']));
2472              $field_select .= $form->generate_hidden_field("fields_inherit_".$usergroup['gid'], (int)$default_checked, array('id' => 'fields_inherit_'.$usergroup['gid']));
2473              $field_select .= $form->generate_hidden_field("fields_default_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, '1')), array('id' => 'fields_default_'.$usergroup['gid']));
2474              $field_select = str_replace("'", "\\'", $field_select);
2475              $field_select = str_replace("\n", "", $field_select);
2476  
2477              $field_select = "<script type=\"text/javascript\">
2478  //<![CDATA[
2479  document.write('".str_replace("/", "\/", $field_select)."');
2480  //]]>
2481  </script>\n";
2482  
2483              $field_selected = array();
2484              foreach($field_list as $forum_permission => $permission_title)
2485              {
2486                  $field_options[$forum_permission] = $permission_title;
2487                  if($perms_checked[$forum_permission])
2488                  {
2489                      $field_selected[] = $forum_permission;
2490                  }
2491              }
2492  
2493              $field_select .= "<noscript>".$form->generate_select_box('fields_'.$usergroup['gid'].'[]', $field_options, $field_selected, array('id' => 'fields_'.$usergroup['gid'].'[]', 'multiple' => true))."</noscript>\n";
2494              $form_container->output_cell($field_select, array('colspan' => 2));
2495  
2496              if(!$default_checked)
2497              {
2498                  $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=permissions&amp;pid={$perms['pid']}\" onclick=\"MyBB.popupWindow('index.php?module=forum-management&action=permissions&pid={$perms['pid']}&ajax=1', null, true); return false;\">{$lang->edit_permissions}</a>", array("class" => "align_center"));
2499                  $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=clear_permission&amp;pid={$perms['pid']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->confirm_clear_custom_permission}')\">{$lang->clear_custom_perms}</a>", array("class" => "align_center"));
2500              }
2501              else
2502              {
2503                  $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=permissions&amp;gid={$usergroup['gid']}&amp;fid={$fid}\"  onclick=\"MyBB.popupWindow('index.php?module=forum-management&action=permissions&gid={$usergroup['gid']}&fid={$fid}&ajax=1', null, true); return false;\">{$lang->set_custom_perms}</a>", array("class" => "align_center", "colspan" => 2));
2504              }
2505              $form_container->construct_row(array('id' => 'row_'.$usergroup['gid']));
2506  
2507              $ids[] = $usergroup['gid'];
2508          }
2509          $form_container->end();
2510  
2511          $buttons = array();
2512          $buttons[] = $form->generate_submit_button($lang->update_forum_permissions);
2513          $buttons[] = $form->generate_reset_button($lang->reset);
2514  
2515          $form->output_submit_wrapper($buttons);
2516  
2517          // Write in our JS based field selector
2518          echo "<script type=\"text/javascript\">\n<!--\n";
2519          foreach($ids as $id)
2520          {
2521              echo "$(function() { QuickPermEditor.init(".$id.") });\n";
2522          }
2523          echo "// -->\n</script>\n";
2524  
2525          echo "</div>\n";
2526          $form->end();
2527          echo "<div id=\"tab_moderators\">\n";
2528          $form_container = new FormContainer($lang->sprintf($lang->moderators_assigned_to, $forum_cache[$fid]['name']));
2529          $form_container->output_row_header($lang->name, array('width' => '75%'));
2530          $form_container->output_row_header($lang->controls, array("class" => "align_center", 'style' => 'width: 200px', 'colspan' => 2));
2531          $query = $db->query("
2532              SELECT m.mid, m.id, m.isgroup, u.username, g.title
2533              FROM ".TABLE_PREFIX."moderators m
2534              LEFT JOIN ".TABLE_PREFIX."users u ON (m.isgroup='0' AND m.id=u.uid)
2535              LEFT JOIN ".TABLE_PREFIX."usergroups g ON (m.isgroup='1' AND m.id=g.gid)
2536              WHERE fid='{$fid}'
2537              ORDER BY m.isgroup DESC, u.username, g.title
2538          ");
2539          while($moderator = $db->fetch_array($query))
2540          {
2541              if($moderator['isgroup'])
2542              {
2543                  $moderator['img'] = "<img src=\"styles/{$page->style}/images/icons/group.png\" alt=\"{$lang->group}\" title=\"{$lang->group}\" />";
2544                  $form_container->output_cell("{$moderator['img']} <a href=\"index.php?module=user-groups&amp;action=edit&amp;gid={$moderator['id']}\">".htmlspecialchars_uni($moderator['title'])." ({$lang->usergroup} {$moderator['id']})</a>");
2545                  $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=editmod&amp;mid={$moderator['mid']}\">{$lang->edit}</a>", array("class" => "align_center"));
2546                  $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=deletemod&amp;id={$moderator['id']}&amp;isgroup=1&amp;fid={$fid}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->confirm_moderator_deletion}')\">{$lang->delete}</a>", array("class" => "align_center"));
2547              }
2548              else
2549              {
2550                  $moderator['img'] = "<img src=\"styles/{$page->style}/images/icons/user.png\" alt=\"{$lang->user}\" title=\"{$lang->user}\" />";
2551                  $form_container->output_cell("{$moderator['img']} <a href=\"index.php?module=user-users&amp;action=edit&amp;uid={$moderator['id']}\">".htmlspecialchars_uni($moderator['username'])."</a>");
2552                  $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=editmod&amp;mid={$moderator['mid']}\">{$lang->edit}</a>", array("class" => "align_center"));
2553                  $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=deletemod&amp;id={$moderator['id']}&amp;isgroup=0&amp;fid={$fid}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->confirm_moderator_deletion}')\">{$lang->delete}</a>", array("class" => "align_center"));
2554              }
2555              $form_container->construct_row();
2556          }
2557  
2558          if($form_container->num_rows() == 0)
2559          {
2560              $form_container->output_cell($lang->no_moderators, array('colspan' => 3));
2561              $form_container->construct_row();
2562          }
2563          $form_container->end();
2564  
2565          // Users
2566          $buttons = array();
2567          $form = new Form("index.php?module=forum-management", "post", "management");
2568          echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2569          echo $form->generate_hidden_field("add", "moderators");
2570  
2571          // Usergroup Moderator
2572          if(!is_array($usergroups))
2573          {
2574              $usergroups = $groupscache;
2575          }
2576  
2577          foreach($usergroups as $group)
2578          {
2579              $modgroups[$group['gid']] = $lang->usergroup." ".$group['gid'].": ".htmlspecialchars_uni($group['title']);
2580          }
2581  
2582          if(!isset($mybb->input['usergroup']))
2583          {
2584              $mybb->input['usergroup'] = '';
2585          }
2586  
2587          if(!isset($mybb->input['username']))
2588          {
2589              $mybb->input['username'] = '';
2590          }
2591  
2592          $form_container = new FormContainer($lang->add_usergroup_as_moderator);
2593          $form_container->output_row($lang->usergroup." <em>*</em>", $lang->moderator_usergroup_desc, $form->generate_select_box('usergroup', $modgroups, $mybb->input['usergroup'], array('id' => 'usergroup')), 'usergroup');
2594          $form_container->end();
2595  
2596          $buttons[] = $form->generate_submit_button($lang->add_usergroup_moderator);
2597          $form->output_submit_wrapper($buttons);
2598          $form->end();
2599          echo "<br />";
2600  
2601          $form = new Form("index.php?module=forum-management", "post", "management");
2602          echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2603          echo $form->generate_hidden_field("add", "moderators");
2604          $form_container = new FormContainer($lang->add_user_as_moderator);
2605          $form_container->output_row($lang->username." <em>*</em>", $lang->moderator_username_desc, $form->generate_text_box('username', htmlspecialchars_uni($mybb->get_input('username')), array('id' => 'username')), 'username');
2606          $form_container->end();
2607  
2608          // Autocompletion for usernames
2609          echo '
2610          <link rel="stylesheet" href="../jscripts/select2/select2.css">
2611          <script type="text/javascript" src="../jscripts/select2/select2.min.js?ver=1804"></script>
2612          <script type="text/javascript">
2613          <!--
2614          $("#username").select2({
2615              placeholder: "'.$lang->search_for_a_user.'",
2616              minimumInputLength: 2,
2617              multiple: false,
2618              ajax: { // instead of writing the function to execute the request we use Select2\'s convenient helper
2619                  url: "../xmlhttp.php?action=get_users",
2620                  dataType: \'json\',
2621                  data: function (term, page) {
2622                      return {
2623                          query: term, // search term
2624                      };
2625                  },
2626                  results: function (data, page) { // parse the results into the format expected by Select2.
2627                      // since we are using custom formatting functions we do not need to alter remote JSON data
2628                      return {results: data};
2629                  }
2630              },
2631              initSelection: function(element, callback) {
2632                  var query = $(element).val();
2633                  if (query !== "") {
2634                      $.ajax("../xmlhttp.php?action=get_users&getone=1", {
2635                          data: {
2636                              query: query
2637                          },
2638                          dataType: "json"
2639                      }).done(function(data) { callback(data); });
2640                  }
2641              },
2642          });
2643  
2644          $(\'[for=username]\').on(\'click\', function(){
2645              $("#username").select2(\'open\');
2646              return false;
2647          });
2648          // -->
2649          </script>';
2650  
2651          $buttons = array($form->generate_submit_button($lang->add_user_moderator));
2652          $form->output_submit_wrapper($buttons);
2653          $form->end();
2654  
2655          echo "</div>\n";
2656  
2657          $plugins->run_hooks("admin_forum_management_start_graph");
2658      }
2659  
2660      $page->output_footer();
2661  }
2662  
2663  /**
2664   * @param DefaultFormContainer $form_container
2665   * @param DefaultForm $form
2666   * @param int $pid
2667   * @param int $depth
2668   */
2669  function build_admincp_forums_list(&$form_container, &$form, $pid=0, $depth=1)
2670  {
2671      global $mybb, $lang, $db, $sub_forums;
2672      static $forums_by_parent;
2673  
2674      if(!is_array($forums_by_parent))
2675      {
2676          $forum_cache = cache_forums();
2677  
2678          foreach($forum_cache as $forum)
2679          {
2680              $forums_by_parent[$forum['pid']][$forum['disporder']][$forum['fid']] = $forum;
2681          }
2682      }
2683  
2684      if(!is_array($forums_by_parent[$pid]))
2685      {
2686          return;
2687      }
2688  
2689      foreach($forums_by_parent[$pid] as $children)
2690      {
2691          foreach($children as $forum)
2692          {
2693              $forum['name'] = preg_replace("#&(?!\#[0-9]+;)#si", "&amp;", $forum['name']); // Fix & but allow unicode
2694  
2695              if($forum['active'] == 0)
2696              {
2697                  $forum['name'] = "<em>".$forum['name']."</em>";
2698              }
2699  
2700              if($forum['type'] == "c" && ($depth == 1 || $depth == 2))
2701              {
2702                  $sub_forums = '';
2703                  if(isset($forums_by_parent[$forum['fid']]) && $depth == 2)
2704                  {
2705                      build_admincp_forums_list($form_container, $form, $forum['fid'], $depth+1);
2706                  }
2707                  if($sub_forums)
2708                  {
2709                      $sub_forums = "<br /><small>{$lang->sub_forums}: {$sub_forums}</small>";
2710                  }
2711  
2712                  $form_container->output_cell("<div style=\"padding-left: ".(40*($depth-1))."px;\"><a href=\"index.php?module=forum-management&amp;fid={$forum['fid']}\"><strong>{$forum['name']}</strong></a>{$sub_forums}</div>");
2713  
2714                  $form_container->output_cell($form->generate_numeric_field("disporder[{$forum['fid']}]", "{$forum['disporder']}", array('min' => 0, 'class' => 'align_center', 'style' => 'width:80%; font-weight:bold')), array("class" => "align_center"));
2715  
2716                  $popup = new PopupMenu("forum_{$forum['fid']}", $lang->options);
2717                  $popup->add_item($lang->edit_forum, "index.php?module=forum-management&amp;action=edit&amp;fid={$forum['fid']}");
2718                  $popup->add_item($lang->subforums, "index.php?module=forum-management&amp;fid={$forum['fid']}");
2719                  $popup->add_item($lang->moderators, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_moderators");
2720                  $popup->add_item($lang->permissions, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_permissions");
2721                  $popup->add_item($lang->add_child_forum, "index.php?module=forum-management&amp;action=add&amp;pid={$forum['fid']}");
2722                  $popup->add_item($lang->copy_forum, "index.php?module=forum-management&amp;action=copy&amp;fid={$forum['fid']}");
2723                  $popup->add_item($lang->delete_forum, "index.php?module=forum-management&amp;action=delete&amp;fid={$forum['fid']}&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_forum_deletion}')");
2724  
2725                  $form_container->output_cell($popup->fetch(), array("class" => "align_center"));
2726  
2727                  $form_container->construct_row();
2728  
2729                  // Does this category have any sub forums?
2730                  if($forums_by_parent[$forum['fid']])
2731                  {
2732                      build_admincp_forums_list($form_container, $form, $forum['fid'], $depth+1);
2733                  }
2734              }
2735              elseif($forum['type'] == "f" && ($depth == 1 || $depth == 2))
2736              {
2737                  if($forum['description'])
2738                  {
2739                      $forum['description'] = preg_replace("#&(?!\#[0-9]+;)#si", "&amp;", $forum['description']);
2740                         $forum['description'] = "<br /><small>".$forum['description']."</small>";
2741                     }
2742  
2743                  $sub_forums = '';
2744                  if(isset($forums_by_parent[$forum['fid']]) && $depth == 2)
2745                  {
2746                      build_admincp_forums_list($form_container, $form, $forum['fid'], $depth+1);
2747                  }
2748                  if($sub_forums)
2749                  {
2750                      $sub_forums = "<br /><small>{$lang->sub_forums}: {$sub_forums}</small>";
2751                  }
2752  
2753                  $form_container->output_cell("<div style=\"padding-left: ".(40*($depth-1))."px;\"><a href=\"index.php?module=forum-management&amp;fid={$forum['fid']}\">{$forum['name']}</a>{$forum['description']}{$sub_forums}</div>");
2754  
2755                  $form_container->output_cell($form->generate_numeric_field("disporder[{$forum['fid']}]", "{$forum['disporder']}", array('min' => 0, 'class' => 'align_center', 'style' => 'width:80%')), array("class" => "align_center"));
2756                  $popup = new PopupMenu("forum_{$forum['fid']}", $lang->options);
2757                  $popup->add_item($lang->edit_forum, "index.php?module=forum-management&amp;action=edit&amp;fid={$forum['fid']}");
2758                  $popup->add_item($lang->subforums, "index.php?module=forum-management&amp;fid={$forum['fid']}");
2759                  $popup->add_item($lang->moderators, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_moderators");
2760                  $popup->add_item($lang->permissions, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_permissions");
2761                  $popup->add_item($lang->add_child_forum, "index.php?module=forum-management&amp;action=add&amp;pid={$forum['fid']}");
2762                  $popup->add_item($lang->copy_forum, "index.php?module=forum-management&amp;action=copy&amp;fid={$forum['fid']}");
2763                  $popup->add_item($lang->delete_forum, "index.php?module=forum-management&amp;action=delete&amp;fid={$forum['fid']}&amp;my_post_key={$mybb->post_code}", "return AdminCP.deleteConfirmation(this, '{$lang->confirm_forum_deletion}')");
2764  
2765                  $form_container->output_cell($popup->fetch(), array("class" => "align_center"));
2766  
2767                  $form_container->construct_row();
2768  
2769                  if(isset($forums_by_parent[$forum['fid']]) && $depth == 1)
2770                  {
2771                      build_admincp_forums_list($form_container, $form, $forum['fid'], $depth+1);
2772                  }
2773              }
2774              else if($depth == 3)
2775              {
2776                  if($donecount < $mybb->settings['subforumsindex'])
2777                  {
2778                      $sub_forums .= "{$comma} <a href=\"index.php?module=forum-management&amp;fid={$forum['fid']}\">{$forum['name']}</a>";
2779                      $comma = $lang->comma;
2780                  }
2781  
2782                  // Have we reached our max visible subforums? put a nice message and break out of the loop
2783                  ++$donecount;
2784                  if($donecount == $mybb->settings['subforumsindex'])
2785                  {
2786                      if(subforums_count($forums_by_parent[$pid]) > $donecount)
2787                      {
2788                          $sub_forums .= $comma.$lang->sprintf($lang->more_subforums, (subforums_count($forums_by_parent[$pid]) - $donecount));
2789                          return;
2790                      }
2791                  }
2792              }
2793          }
2794      }
2795  }
2796  
2797  /**
2798   * @param int $gid
2799   * @param int $fid
2800   *
2801   * @return string
2802   */
2803  function retrieve_single_permissions_row($gid, $fid)
2804  {
2805      global $mybb, $lang, $cache, $db;
2806  
2807      $query = $db->simple_select("usergroups", "*", "gid='{$gid}'");
2808      $usergroup = $db->fetch_array($query);
2809  
2810      $query = $db->simple_select("forums", "*", "fid='{$fid}'");
2811      $forum_data = $db->fetch_array($query);
2812  
2813      $query = $db->simple_select("forumpermissions", "*", "fid='{$fid}'");
2814      while($existing = $db->fetch_array($query))
2815      {
2816          $existing_permissions[$existing['gid']] = $existing;
2817      }
2818  
2819      $cached_forum_perms = $cache->read("forumpermissions");
2820      $field_list = array(
2821          'canview' => $lang->permissions_canview,
2822          'canpostthreads' => $lang->permissions_canpostthreads,
2823          'canpostreplys' => $lang->permissions_canpostreplys,
2824          'canpostpolls' => $lang->permissions_canpostpolls,
2825      );
2826  
2827      $field_list2 = array(
2828          'canview' => $lang->perm_drag_canview,
2829          'canpostthreads' => $lang->perm_drag_canpostthreads,
2830          'canpostreplys' => $lang->perm_drag_canpostreplys,
2831          'canpostpolls' => $lang->perm_drag_canpostpolls,
2832      );
2833  
2834      $form = new Form('', '', "", 0, "", true);
2835      $form_container = new FormContainer();
2836  
2837      $perms = array();
2838  
2839      if(is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
2840      {
2841          $perms = $existing_permissions[$usergroup['gid']];
2842          $default_checked = false;
2843      }
2844      elseif(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['fid']][$usergroup['gid']])
2845      {
2846          $perms = $cached_forum_perms[$forum_data['fid']][$usergroup['gid']];
2847          $default_checked = true;
2848      }
2849      else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['pid']][$usergroup['gid']])
2850      {
2851          $perms = $cached_forum_perms[$forum_data['pid']][$usergroup['gid']];
2852          $default_checked = true;
2853      }
2854  
2855      if(!$perms)
2856      {
2857          $perms = $usergroup;
2858          $default_checked = true;
2859      }
2860  
2861      foreach($field_list as $forum_permission => $forum_perm_title)
2862      {
2863          if($perms[$forum_permission] == 1)
2864          {
2865              $perms_checked[$forum_permission] = 1;
2866          }
2867          else
2868          {
2869              $perms_checked[$forum_permission] = 0;
2870          }
2871      }
2872  
2873      $usergroup['title'] = htmlspecialchars_uni($usergroup['title']);
2874  
2875      if($default_checked == 1)
2876      {
2877          $inherited_text = $lang->inherited_permission;
2878      }
2879      else
2880      {
2881          $inherited_text = $lang->custom_permission;
2882      }
2883  
2884      $form_container->output_cell("<strong>{$usergroup['title']}</strong> <small style=\"vertical-align: middle;\">({$inherited_text})</small>");
2885  
2886      $field_select = "<div class=\"quick_perm_fields\">\n";
2887      $field_select .= "<div class=\"enabled\"><div class=\"fields_title\">{$lang->enabled}</div><ul id=\"fields_enabled_{$usergroup['gid']}\">\n";
2888      foreach($perms_checked as $perm => $value)
2889      {
2890          if($value == 1)
2891          {
2892              $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2893          }
2894      }
2895      $field_select .= "</ul></div>\n";
2896      $field_select .= "<div class=\"disabled\"><div class=\"fields_title\">{$lang->disabled}</div><ul id=\"fields_disabled_{$usergroup['gid']}\">\n";
2897      foreach($perms_checked as $perm => $value)
2898      {
2899          if($value == 0)
2900          {
2901              $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2902          }
2903      }
2904      $field_select .= "</ul></div></div>\n";
2905      $field_select .= $form->generate_hidden_field("fields_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, 1)), array('id' => 'fields_'.$usergroup['gid']));
2906      $field_select = str_replace("\n", "", $field_select);
2907  
2908      foreach($field_list as $forum_permission => $permission_title)
2909      {
2910          $field_options[$forum_permission] = $permission_title;
2911      }
2912      $form_container->output_cell($field_select, array('colspan' => 2));
2913  
2914      if(!$default_checked)
2915      {
2916          $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=permissions&amp;pid={$perms['pid']}\" onclick=\"MyBB.popupWindow('index.php?module=forum-management&action=permissions&pid={$perms['pid']}&ajax=1', null, true); return false;\">{$lang->edit_permissions}</a>", array("class" => "align_center"));
2917          $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=clear_permission&amp;pid={$perms['pid']}&amp;my_post_key={$mybb->post_code}\" onclick=\"return AdminCP.deleteConfirmation(this, '{$lang->confirm_clear_custom_permission}')\">{$lang->clear_custom_perms}</a>", array("class" => "align_center"));
2918      }
2919      else
2920      {
2921          $form_container->output_cell("<a href=\"index.php?module=forum-management&amp;action=permissions&amp;gid={$usergroup['gid']}&amp;fid={$fid}\"  onclick=\"MyBB.popupWindow('index.php?module=forum-management&action=permissions&gid={$usergroup['gid']}&fid={$fid}&ajax=1', null, true); return false;\">{$lang->set_custom_perms}</a>", array("class" => "align_center", "colspan" => 2));
2922      }
2923      $form_container->construct_row();
2924      return $form_container->output_row_cells(0, true);
2925  }
2926  


2005 - 2019 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref 0.7.1