[ Index ]

PHP Cross Reference of MyBB 1.8.21

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              echo "<div class=\"confirm_action\">\n";
1993              echo "<p>{$lang->confirm_proceed_deletion}</p>\n";
1994              echo "<br />\n";
1995              echo "<script type=\"text/javascript\">$(function() { var button = $(\"#proceed_button\"); if(button.length > 0) { button.val(\"{$lang->automatically_redirecting}\"); button.attr(\"disabled\", true); button.css(\"color\", \"#aaa\"); button.css(\"borderColor\", \"#aaa\"); document.forms[0].trigger('submit'); }})</script>";
1996              echo "<p class=\"buttons\">\n";
1997              echo $form->generate_submit_button($lang->proceed, array('class' => 'button_yes', 'id' => 'proceed_button'));
1998              echo "</p>\n";
1999              echo "</div>\n";
2000  
2001              $form->end();
2002  
2003              $page->output_footer();
2004              exit;
2005          }
2006  
2007          // End pagination
2008  
2009          // Delete the forum
2010          $db->delete_query("forums", "fid='$fid'");
2011  
2012          // Delete subforums
2013          switch($db->type)
2014          {
2015              case "pgsql":
2016              case "sqlite":
2017                  $db->delete_query("forums", "','||parentlist||',' LIKE '%,$fid,%'");
2018                  break;
2019              default:
2020                  $db->delete_query("forums", "CONCAT(',',parentlist,',') LIKE '%,$fid,%'");
2021          }
2022  
2023          $db->delete_query('moderators', "fid='{$fid}' {$delquery}");
2024          $db->delete_query('forumsubscriptions', "fid='{$fid}' {$delquery}");
2025          $db->delete_query('forumpermissions', "fid='{$fid}' {$delquery}");
2026          $db->delete_query('announcements', "fid='{$fid}' {$delquery}");
2027          $db->delete_query('forumsread', "fid='{$fid}' {$delquery}");
2028  
2029          $plugins->run_hooks("admin_forum_management_delete_commit");
2030  
2031          $cache->update_forums();
2032          $cache->update_moderators();
2033          $cache->update_forumpermissions();
2034          $cache->update_forumsdisplay();
2035  
2036          // Log admin action
2037          log_admin_action($forum_info['fid'], $forum_info['name']);
2038  
2039          flash_message($lang->success_forum_deleted, 'success');
2040          admin_redirect("index.php?module=forum-management");
2041      }
2042      else
2043      {
2044          $page->output_confirm_action("index.php?module=forum-management&amp;action=delete&amp;fid={$forum['fid']}", $lang->confirm_forum_deletion);
2045      }
2046  }
2047  
2048  if(!$mybb->input['action'])
2049  {
2050      if(!isset($mybb->input['fid']))
2051      {
2052          $mybb->input['fid'] = 0;
2053      }
2054  
2055      $fid = $mybb->get_input('fid', MyBB::INPUT_INT);
2056      if($fid)
2057      {
2058          $forum = get_forum($fid);
2059      }
2060  
2061      $plugins->run_hooks("admin_forum_management_start");
2062  
2063      if($mybb->request_method == "post")
2064      {
2065          if($mybb->input['update'] == "permissions")
2066          {
2067              $inherit = array();
2068              foreach($mybb->input as $id => $permission)
2069              {
2070                  // Make sure we're only skipping inputs that don't start with "fields_" and aren't fields_default_ or fields_inherit_
2071                  if(strpos($id, 'fields_') === false || (strpos($id, 'fields_default_') !== false || strpos($id, 'fields_inherit_') !== false))
2072                  {
2073                      continue;
2074                  }
2075  
2076                  list(, $gid) = explode('fields_', $id);
2077  
2078                  if($mybb->input['fields_default_'.$gid] == $permission && $mybb->input['fields_inherit_'.$gid] == 1)
2079                  {
2080                      $inherit[$gid] = 1;
2081                      continue;
2082                  }
2083                  $inherit[$gid] = 0;
2084  
2085                  // If it isn't an array then it came from the javascript form
2086                  if(!is_array($permission))
2087                  {
2088                      $permission = explode(',', $permission);
2089                      $permission = array_flip($permission);
2090                      foreach($permission as $name => $value)
2091                      {
2092                          $permission[$name] = 1;
2093                      }
2094                  }
2095                  foreach(array('canview','canpostthreads','canpostreplys','canpostpolls') as $name)
2096                  {
2097                      if($permission[$name])
2098                      {
2099                          $permissions[$name][$gid] = 1;
2100                      }
2101                      else
2102                      {
2103                          $permissions[$name][$gid] = 0;
2104                      }
2105                  }
2106              }
2107  
2108              $canview = $permissions['canview'];
2109              $canpostthreads = $permissions['canpostthreads'];
2110              $canpostpolls = $permissions['canpostpolls'];
2111              $canpostattachments = $permissions['canpostattachments'];
2112              $canpostreplies = $permissions['canpostreplys'];
2113  
2114              save_quick_perms($fid);
2115  
2116              $plugins->run_hooks("admin_forum_management_start_permissions_commit");
2117  
2118              $cache->update_forums();
2119  
2120              // Log admin action
2121              log_admin_action('quickpermissions', $fid, $forum['name']);
2122  
2123              flash_message($lang->success_forum_permissions_updated, 'success');
2124              admin_redirect("index.php?module=forum-management&fid={$fid}#tab_permissions");
2125          }
2126          elseif($mybb->input['add'] == "moderators")
2127          {
2128              $forum = get_forum($fid);
2129              if(!$forum)
2130              {
2131                  flash_message($lang->error_invalid_forum, 'error');
2132                  admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2133              }
2134              if(!empty($mybb->input['usergroup']))
2135              {
2136                  $isgroup = 1;
2137                  $gid = $mybb->get_input('usergroup', MyBB::INPUT_INT);
2138  
2139                  if(!$groupscache[$gid])
2140                   {
2141                       // Didn't select a valid moderator
2142                       flash_message($lang->error_moderator_not_found, 'error');
2143                       admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2144                   }
2145  
2146                  $newmod = array(
2147                      "id" => $gid,
2148                      "name" => $groupscache[$gid]['title']
2149                  );
2150              }
2151              else
2152              {
2153                  $options = array(
2154                      'fields' => array('uid AS id', 'username AS name')
2155                  );
2156                  $newmod = get_user_by_username($mybb->input['username'], $options);
2157  
2158                  if(empty($newmod['id']))
2159                  {
2160                      flash_message($lang->error_moderator_not_found, 'error');
2161                      admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2162                  }
2163  
2164                  $isgroup = 0;
2165              }
2166  
2167              if($newmod['id'])
2168              {
2169                  $query = $db->simple_select("moderators", "id", "id='".$newmod['id']."' AND fid='".$fid."' AND isgroup='{$isgroup}'", array('limit' => 1));
2170  
2171                  if(!$db->num_rows($query))
2172                  {
2173                      $new_mod = array(
2174                          "fid" => $fid,
2175                          "id" => $newmod['id'],
2176                          "isgroup" => $isgroup,
2177                          "caneditposts" => 1,
2178                          "cansoftdeleteposts" => 1,
2179                          "canrestoreposts" => 1,
2180                          "candeleteposts" => 1,
2181                          "cansoftdeletethreads" => 1,
2182                          "canrestorethreads" => 1,
2183                          "candeletethreads" => 1,
2184                          "canviewips" => 1,
2185                          "canviewunapprove" => 1,
2186                          "canviewdeleted" => 1,
2187                          "canopenclosethreads" => 1,
2188                          "canstickunstickthreads" => 1,
2189                          "canapproveunapprovethreads" => 1,
2190                          "canapproveunapproveposts" => 1,
2191                          "canapproveunapproveattachs" => 1,
2192                          "canmanagethreads" => 1,
2193                          "canmanagepolls" => 1,
2194                          "canpostclosedthreads" => 1,
2195                          "canmovetononmodforum" => 1,
2196                          "canusecustomtools" => 1,
2197                          "canmanageannouncements" => 1,
2198                          "canmanagereportedposts" => 1,
2199                          "canviewmodlog" => 1
2200                      );
2201  
2202                      $mid = $db->insert_query("moderators", $new_mod);
2203  
2204                      if(!$isgroup)
2205                      {
2206                          $db->update_query("users", array('usergroup' => 6), "uid='{$newmod['id']}' AND usergroup='2'");
2207                      }
2208  
2209                      $plugins->run_hooks("admin_forum_management_start_moderators_commit");
2210  
2211                      $cache->update_moderators();
2212  
2213                      // Log admin action
2214                      log_admin_action('addmod', $mid, $newmod['name'], $fid, $forum['name']);
2215  
2216                      flash_message($lang->success_moderator_added, 'success');
2217                      admin_redirect("index.php?module=forum-management&action=editmod&mid={$mid}");
2218                  }
2219                  else
2220                  {
2221                      flash_message($lang->error_moderator_already_added, 'error');
2222                      admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2223                  }
2224              }
2225              else
2226              {
2227                  flash_message($lang->error_moderator_not_found, 'error');
2228                  admin_redirect("index.php?module=forum-management&fid={$fid}#tab_moderators");
2229              }
2230          }
2231          else
2232          {
2233              if(!empty($mybb->input['disporder']) && is_array($mybb->input['disporder']))
2234              {
2235                  foreach($mybb->input['disporder'] as $update_fid => $order)
2236                  {
2237                      $db->update_query("forums", array('disporder' => (int)$order), "fid='".(int)$update_fid."'");
2238                  }
2239  
2240                  $plugins->run_hooks("admin_forum_management_start_disporder_commit");
2241  
2242                  $cache->update_forums();
2243  
2244                  // Log admin action
2245                  log_admin_action('orders', $forum['fid'], $forum['name']);
2246  
2247                  flash_message($lang->success_forum_disporder_updated, 'success');
2248                  admin_redirect("index.php?module=forum-management&fid=".$mybb->input['fid']);
2249              }
2250          }
2251      }
2252  
2253      $page->extra_header .=  "<script src=\"jscripts/quick_perm_editor.js\" type=\"text/javascript\"></script>\n";
2254  
2255      if($fid)
2256      {
2257          $page->add_breadcrumb_item($lang->view_forum, "index.php?module=forum-management");
2258      }
2259  
2260      $page->output_header($lang->forum_management);
2261  
2262      if($fid)
2263      {
2264          $page->output_nav_tabs($sub_tabs, 'view_forum');
2265      }
2266      else
2267      {
2268          $page->output_nav_tabs($sub_tabs, 'forum_management');
2269      }
2270  
2271      $form = new Form("index.php?module=forum-management", "post", "management");
2272      echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2273  
2274      if($fid)
2275      {
2276          $tabs = array(
2277              'subforums' => $lang->subforums,
2278              'permissions' => $lang->forum_permissions,
2279              'moderators' => $lang->moderators,
2280          );
2281          $tabs = $plugins->run_hooks("admin_forum_management_start_graph_tabs", $tabs);
2282          $page->output_tab_control($tabs);
2283  
2284          echo "<div id=\"tab_subforums\">\n";
2285          if(!isset($forum_cache) || !is_array($forum_cache))
2286          {
2287              cache_forums();
2288          }
2289          $form_container = new FormContainer($lang->sprintf($lang->in_forums, $forum_cache[$fid]['name']));
2290      }
2291      else
2292      {
2293          $form_container = new FormContainer($lang->manage_forums);
2294      }
2295      $form_container->output_row_header($lang->forum);
2296      $form_container->output_row_header($lang->order, array("class" => "align_center", 'width' => '5%'));
2297      $form_container->output_row_header($lang->controls, array("class" => "align_center", 'style' => 'width: 200px'));
2298  
2299      build_admincp_forums_list($form_container, $fid);
2300  
2301      $submit_options = array();
2302  
2303      if($form_container->num_rows() == 0)
2304      {
2305          $form_container->output_cell($lang->no_forums, array('colspan' => 3));
2306          $form_container->construct_row();
2307          $submit_options = array('disabled' => true);
2308      }
2309  
2310      $form_container->end();
2311  
2312      $buttons[] = $form->generate_submit_button($lang->update_forum_orders, $submit_options);
2313      $buttons[] = $form->generate_reset_button($lang->reset);
2314  
2315      $form->output_submit_wrapper($buttons);
2316  
2317      if(!$fid)
2318      {
2319          $form->end();
2320      }
2321  
2322      if($fid)
2323      {
2324          echo "</div>\n";
2325          $form->end();
2326  
2327          $query = $db->simple_select("usergroups", "*", "", array("order" => "name"));
2328          while($usergroup = $db->fetch_array($query))
2329          {
2330              $usergroups[$usergroup['gid']] = $usergroup;
2331          }
2332  
2333          $query = $db->simple_select("forumpermissions", "*", "fid='{$fid}'");
2334          while($existing = $db->fetch_array($query))
2335          {
2336              $existing_permissions[$existing['gid']] = $existing;
2337          }
2338  
2339          $cached_forum_perms = $cache->read("forumpermissions");
2340          $field_list = array(
2341              'canview' => $lang->permissions_canview,
2342              'canpostthreads' => $lang->permissions_canpostthreads,
2343              'canpostreplys' => $lang->permissions_canpostreplys,
2344              'canpostpolls' => $lang->permissions_canpostpolls,
2345          );
2346  
2347          $field_list2 = array(
2348              'canview' => $lang->perm_drag_canview,
2349              'canpostthreads' => $lang->perm_drag_canpostthreads,
2350              'canpostreplys' => $lang->perm_drag_canpostreplys,
2351              'canpostpolls' => $lang->perm_drag_canpostpolls,
2352          );
2353  
2354          $ids = array();
2355  
2356          $form = new Form("index.php?module=forum-management", "post", "management");
2357          echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2358          echo $form->generate_hidden_field("update", "permissions");
2359  
2360          echo "<div id=\"tab_permissions\">\n";
2361  
2362          $form_container = new FormContainer($lang->sprintf($lang->forum_permissions_in, $forum_cache[$fid]['name']));
2363          $form_container->output_row_header($lang->permissions_group, array("class" => "align_center", 'style' => 'width: 30%'));
2364          $form_container->output_row_header($lang->overview_allowed_actions, array("class" => "align_center"));
2365          $form_container->output_row_header($lang->overview_disallowed_actions, array("class" => "align_center"));
2366          $form_container->output_row_header($lang->controls, array("class" => "align_center", 'style' => 'width: 120px', 'colspan' => 2));
2367          foreach($usergroups as $usergroup)
2368          {
2369              $perms = array();
2370              if(isset($mybb->input['default_permissions']))
2371              {
2372                  if($mybb->input['default_permissions'][$usergroup['gid']])
2373                  {
2374                      if(is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
2375                      {
2376                          $perms = $existing_permissions[$usergroup['gid']];
2377                          $default_checked = false;
2378                      }
2379                      elseif(is_array($cached_forum_perms) && $cached_forum_perms[$forum['fid']][$usergroup['gid']])
2380                      {
2381                          $perms = $cached_forum_perms[$forum['fid']][$usergroup['gid']];
2382                          $default_checked = true;
2383                      }
2384                      else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum['pid']][$usergroup['gid']])
2385                      {
2386                          $perms = $cached_forum_perms[$forum['pid']][$usergroup['gid']];
2387                          $default_checked = true;
2388                      }
2389                  }
2390  
2391                  if(!$perms)
2392                  {
2393                      $perms = $usergroup;
2394                      $default_checked = true;
2395                  }
2396              }
2397              else
2398              {
2399                  if(isset($existing_permissions) && is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
2400                  {
2401                      $perms = $existing_permissions[$usergroup['gid']];
2402                      $default_checked = false;
2403                  }
2404                  elseif(is_array($cached_forum_perms) && isset($cached_forum_perms[$forum['fid']]) && $cached_forum_perms[$forum['fid']][$usergroup['gid']])
2405                  {
2406                      $perms = $cached_forum_perms[$forum['fid']][$usergroup['gid']];
2407                      $default_checked = true;
2408                  }
2409                  else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum['pid']][$usergroup['gid']])
2410                  {
2411                      $perms = $cached_forum_perms[$forum['pid']][$usergroup['gid']];
2412                      $default_checked = true;
2413                  }
2414  
2415                  if(!$perms)
2416                  {
2417                      $perms = $usergroup;
2418                      $default_checked = true;
2419                  }
2420              }
2421              foreach($field_list as $forum_permission => $forum_perm_title)
2422              {
2423                  if(isset($mybb->input['permissions']))
2424                  {
2425                      if($mybb->input['permissions'][$usergroup['gid']][$forum_permission])
2426                      {
2427                          $perms_checked[$forum_permission] = 1;
2428                      }
2429                      else
2430                      {
2431                          $perms_checked[$forum_permission] = 0;
2432                      }
2433                  }
2434                  else
2435                  {
2436                      if($perms[$forum_permission] == 1)
2437                      {
2438                          $perms_checked[$forum_permission] = 1;
2439                      }
2440                      else
2441                      {
2442                          $perms_checked[$forum_permission] = 0;
2443                      }
2444                  }
2445              }
2446              $usergroup['title'] = htmlspecialchars_uni($usergroup['title']);
2447  
2448              if($default_checked == 1)
2449              {
2450                  $inherited_text = $lang->inherited_permission;
2451              }
2452              else
2453              {
2454                  $inherited_text = $lang->custom_permission;
2455              }
2456  
2457              $form_container->output_cell("<strong>{$usergroup['title']}</strong> <small style=\"vertical-align: middle;\">({$inherited_text})</small>");
2458  
2459              $field_select = "<div class=\"quick_perm_fields\">\n";
2460              $field_select .= "<div class=\"enabled\"><ul id=\"fields_enabled_{$usergroup['gid']}\">\n";
2461              foreach($perms_checked as $perm => $value)
2462              {
2463                  if($value == 1)
2464                  {
2465                      $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2466                  }
2467              }
2468              $field_select .= "</ul></div>\n";
2469              $field_select .= "<div class=\"disabled\"><ul id=\"fields_disabled_{$usergroup['gid']}\">\n";
2470              foreach($perms_checked as $perm => $value)
2471              {
2472                  if($value == 0)
2473                  {
2474                      $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2475                  }
2476              }
2477              $field_select .= "</ul></div></div>\n";
2478              $field_select .= $form->generate_hidden_field("fields_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, '1')), array('id' => 'fields_'.$usergroup['gid']));
2479              $field_select .= $form->generate_hidden_field("fields_inherit_".$usergroup['gid'], (int)$default_checked, array('id' => 'fields_inherit_'.$usergroup['gid']));
2480              $field_select .= $form->generate_hidden_field("fields_default_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, '1')), array('id' => 'fields_default_'.$usergroup['gid']));
2481              $field_select = str_replace("'", "\\'", $field_select);
2482              $field_select = str_replace("\n", "", $field_select);
2483  
2484              $field_select = "<script type=\"text/javascript\">
2485  //<![CDATA[
2486  document.write('".str_replace("/", "\/", $field_select)."');
2487  //]]>
2488  </script>\n";
2489  
2490              $field_selected = array();
2491              foreach($field_list as $forum_permission => $permission_title)
2492              {
2493                  $field_options[$forum_permission] = $permission_title;
2494                  if($perms_checked[$forum_permission])
2495                  {
2496                      $field_selected[] = $forum_permission;
2497                  }
2498              }
2499  
2500              $field_select .= "<noscript>".$form->generate_select_box('fields_'.$usergroup['gid'].'[]', $field_options, $field_selected, array('id' => 'fields_'.$usergroup['gid'].'[]', 'multiple' => true))."</noscript>\n";
2501              $form_container->output_cell($field_select, array('colspan' => 2));
2502  
2503              if(!$default_checked)
2504              {
2505                  $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"));
2506                  $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"));
2507              }
2508              else
2509              {
2510                  $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));
2511              }
2512              $form_container->construct_row(array('id' => 'row_'.$usergroup['gid']));
2513  
2514              $ids[] = $usergroup['gid'];
2515          }
2516          $form_container->end();
2517  
2518          $buttons = array();
2519          $buttons[] = $form->generate_submit_button($lang->update_forum_permissions);
2520          $buttons[] = $form->generate_reset_button($lang->reset);
2521  
2522          $form->output_submit_wrapper($buttons);
2523  
2524          // Write in our JS based field selector
2525          echo "<script type=\"text/javascript\">\n<!--\n";
2526          foreach($ids as $id)
2527          {
2528              echo "$(function() { QuickPermEditor.init(".$id.") });\n";
2529          }
2530          echo "// -->\n</script>\n";
2531  
2532          echo "</div>\n";
2533          $form->end();
2534          echo "<div id=\"tab_moderators\">\n";
2535          $form_container = new FormContainer($lang->sprintf($lang->moderators_assigned_to, $forum_cache[$fid]['name']));
2536          $form_container->output_row_header($lang->name, array('width' => '75%'));
2537          $form_container->output_row_header($lang->controls, array("class" => "align_center", 'style' => 'width: 200px', 'colspan' => 2));
2538          $query = $db->query("
2539              SELECT m.mid, m.id, m.isgroup, u.username, g.title
2540              FROM ".TABLE_PREFIX."moderators m
2541              LEFT JOIN ".TABLE_PREFIX."users u ON (m.isgroup='0' AND m.id=u.uid)
2542              LEFT JOIN ".TABLE_PREFIX."usergroups g ON (m.isgroup='1' AND m.id=g.gid)
2543              WHERE fid='{$fid}'
2544              ORDER BY m.isgroup DESC, u.username, g.title
2545          ");
2546          while($moderator = $db->fetch_array($query))
2547          {
2548              if($moderator['isgroup'])
2549              {
2550                  $moderator['img'] = "<img src=\"styles/{$page->style}/images/icons/group.png\" alt=\"{$lang->group}\" title=\"{$lang->group}\" />";
2551                  $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>");
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=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"));
2554              }
2555              else
2556              {
2557                  $moderator['img'] = "<img src=\"styles/{$page->style}/images/icons/user.png\" alt=\"{$lang->user}\" title=\"{$lang->user}\" />";
2558                  $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>");
2559                  $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"));
2560                  $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"));
2561              }
2562              $form_container->construct_row();
2563          }
2564  
2565          if($form_container->num_rows() == 0)
2566          {
2567              $form_container->output_cell($lang->no_moderators, array('colspan' => 3));
2568              $form_container->construct_row();
2569          }
2570          $form_container->end();
2571  
2572          // Users
2573          $buttons = array();
2574          $form = new Form("index.php?module=forum-management", "post", "management");
2575          echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2576          echo $form->generate_hidden_field("add", "moderators");
2577  
2578          // Usergroup Moderator
2579          if(!is_array($usergroups))
2580          {
2581              $usergroups = $groupscache;
2582          }
2583  
2584          foreach($usergroups as $group)
2585          {
2586              $modgroups[$group['gid']] = $lang->usergroup." ".$group['gid'].": ".htmlspecialchars_uni($group['title']);
2587          }
2588  
2589          if(!isset($mybb->input['usergroup']))
2590          {
2591              $mybb->input['usergroup'] = '';
2592          }
2593  
2594          if(!isset($mybb->input['username']))
2595          {
2596              $mybb->input['username'] = '';
2597          }
2598  
2599          $form_container = new FormContainer($lang->add_usergroup_as_moderator);
2600          $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');
2601          $form_container->end();
2602  
2603          $buttons[] = $form->generate_submit_button($lang->add_usergroup_moderator);
2604          $form->output_submit_wrapper($buttons);
2605          $form->end();
2606          echo "<br />";
2607  
2608          $form = new Form("index.php?module=forum-management", "post", "management");
2609          echo $form->generate_hidden_field("fid", $mybb->input['fid']);
2610          echo $form->generate_hidden_field("add", "moderators");
2611          $form_container = new FormContainer($lang->add_user_as_moderator);
2612          $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');
2613          $form_container->end();
2614  
2615          // Autocompletion for usernames
2616          echo '
2617          <link rel="stylesheet" href="../jscripts/select2/select2.css">
2618          <script type="text/javascript" src="../jscripts/select2/select2.min.js?ver=1804"></script>
2619          <script type="text/javascript">
2620          <!--
2621          $("#username").select2({
2622              placeholder: "'.$lang->search_for_a_user.'",
2623              minimumInputLength: 2,
2624              multiple: false,
2625              ajax: { // instead of writing the function to execute the request we use Select2\'s convenient helper
2626                  url: "../xmlhttp.php?action=get_users",
2627                  dataType: \'json\',
2628                  data: function (term, page) {
2629                      return {
2630                          query: term, // search term
2631                      };
2632                  },
2633                  results: function (data, page) { // parse the results into the format expected by Select2.
2634                      // since we are using custom formatting functions we do not need to alter remote JSON data
2635                      return {results: data};
2636                  }
2637              },
2638              initSelection: function(element, callback) {
2639                  var query = $(element).val();
2640                  if (query !== "") {
2641                      $.ajax("../xmlhttp.php?action=get_users&getone=1", {
2642                          data: {
2643                              query: query
2644                          },
2645                          dataType: "json"
2646                      }).done(function(data) { callback(data); });
2647                  }
2648              },
2649          });
2650  
2651          $(\'[for=username]\').on(\'click\', function(){
2652              $("#username").select2(\'open\');
2653              return false;
2654          });
2655          // -->
2656          </script>';
2657  
2658          $buttons = array($form->generate_submit_button($lang->add_user_moderator));
2659          $form->output_submit_wrapper($buttons);
2660          $form->end();
2661  
2662          echo "</div>\n";
2663  
2664          $plugins->run_hooks("admin_forum_management_start_graph");
2665      }
2666  
2667      $page->output_footer();
2668  }
2669  
2670  /**
2671   * @param DefaultFormContainer $form_container
2672   * @param int $pid
2673   * @param int $depth
2674   */
2675  function build_admincp_forums_list(&$form_container, $pid=0, $depth=1)
2676  {
2677      global $mybb, $lang, $db, $sub_forums;
2678      static $forums_by_parent;
2679  
2680      if(!is_array($forums_by_parent))
2681      {
2682          $forum_cache = cache_forums();
2683  
2684          foreach($forum_cache as $forum)
2685          {
2686              $forums_by_parent[$forum['pid']][$forum['disporder']][$forum['fid']] = $forum;
2687          }
2688      }
2689  
2690      if(!is_array($forums_by_parent[$pid]))
2691      {
2692          return;
2693      }
2694  
2695      foreach($forums_by_parent[$pid] as $children)
2696      {
2697          foreach($children as $forum)
2698          {
2699              $forum['name'] = preg_replace("#&(?!\#[0-9]+;)#si", "&amp;", $forum['name']); // Fix & but allow unicode
2700  
2701              if($forum['active'] == 0)
2702              {
2703                  $forum['name'] = "<em>".$forum['name']."</em>";
2704              }
2705  
2706              if($forum['type'] == "c" && ($depth == 1 || $depth == 2))
2707              {
2708                  $sub_forums = '';
2709                  if(isset($forums_by_parent[$forum['fid']]) && $depth == 2)
2710                  {
2711                      build_admincp_forums_list($form_container, $forum['fid'], $depth+1);
2712                  }
2713                  if($sub_forums)
2714                  {
2715                      $sub_forums = "<br /><small>{$lang->sub_forums}: {$sub_forums}</small>";
2716                  }
2717  
2718                  $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>");
2719  
2720                  $form_container->output_cell("<input type=\"text\" name=\"disporder[".$forum['fid']."]\" value=\"".$forum['disporder']."\" class=\"text_input align_center\" style=\"width: 80%; font-weight: bold;\" />", array("class" => "align_center"));
2721  
2722                  $popup = new PopupMenu("forum_{$forum['fid']}", $lang->options);
2723                  $popup->add_item($lang->edit_forum, "index.php?module=forum-management&amp;action=edit&amp;fid={$forum['fid']}");
2724                  $popup->add_item($lang->subforums, "index.php?module=forum-management&amp;fid={$forum['fid']}");
2725                  $popup->add_item($lang->moderators, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_moderators");
2726                  $popup->add_item($lang->permissions, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_permissions");
2727                  $popup->add_item($lang->add_child_forum, "index.php?module=forum-management&amp;action=add&amp;pid={$forum['fid']}");
2728                  $popup->add_item($lang->copy_forum, "index.php?module=forum-management&amp;action=copy&amp;fid={$forum['fid']}");
2729                  $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}')");
2730  
2731                  $form_container->output_cell($popup->fetch(), array("class" => "align_center"));
2732  
2733                  $form_container->construct_row();
2734  
2735                  // Does this category have any sub forums?
2736                  if($forums_by_parent[$forum['fid']])
2737                  {
2738                      build_admincp_forums_list($form_container, $forum['fid'], $depth+1);
2739                  }
2740              }
2741              elseif($forum['type'] == "f" && ($depth == 1 || $depth == 2))
2742              {
2743                  if($forum['description'])
2744                  {
2745                      $forum['description'] = preg_replace("#&(?!\#[0-9]+;)#si", "&amp;", $forum['description']);
2746                         $forum['description'] = "<br /><small>".$forum['description']."</small>";
2747                     }
2748  
2749                  $sub_forums = '';
2750                  if(isset($forums_by_parent[$forum['fid']]) && $depth == 2)
2751                  {
2752                      build_admincp_forums_list($form_container, $forum['fid'], $depth+1);
2753                  }
2754                  if($sub_forums)
2755                  {
2756                      $sub_forums = "<br /><small>{$lang->sub_forums}: {$sub_forums}</small>";
2757                  }
2758  
2759                  $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>");
2760  
2761                  $form_container->output_cell("<input type=\"text\" name=\"disporder[".$forum['fid']."]\" value=\"".$forum['disporder']."\" class=\"text_input align_center\" style=\"width: 80%;\" />", array("class" => "align_center"));
2762  
2763                  $popup = new PopupMenu("forum_{$forum['fid']}", $lang->options);
2764                  $popup->add_item($lang->edit_forum, "index.php?module=forum-management&amp;action=edit&amp;fid={$forum['fid']}");
2765                  $popup->add_item($lang->subforums, "index.php?module=forum-management&amp;fid={$forum['fid']}");
2766                  $popup->add_item($lang->moderators, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_moderators");
2767                  $popup->add_item($lang->permissions, "index.php?module=forum-management&amp;fid={$forum['fid']}#tab_permissions");
2768                  $popup->add_item($lang->add_child_forum, "index.php?module=forum-management&amp;action=add&amp;pid={$forum['fid']}");
2769                  $popup->add_item($lang->copy_forum, "index.php?module=forum-management&amp;action=copy&amp;fid={$forum['fid']}");
2770                  $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}')");
2771  
2772                  $form_container->output_cell($popup->fetch(), array("class" => "align_center"));
2773  
2774                  $form_container->construct_row();
2775  
2776                  if(isset($forums_by_parent[$forum['fid']]) && $depth == 1)
2777                  {
2778                      build_admincp_forums_list($form_container, $forum['fid'], $depth+1);
2779                  }
2780              }
2781              else if($depth == 3)
2782              {
2783                  if($donecount < $mybb->settings['subforumsindex'])
2784                  {
2785                      $sub_forums .= "{$comma} <a href=\"index.php?module=forum-management&amp;fid={$forum['fid']}\">{$forum['name']}</a>";
2786                      $comma = $lang->comma;
2787                  }
2788  
2789                  // Have we reached our max visible subforums? put a nice message and break out of the loop
2790                  ++$donecount;
2791                  if($donecount == $mybb->settings['subforumsindex'])
2792                  {
2793                      if(subforums_count($forums_by_parent[$pid]) > $donecount)
2794                      {
2795                          $sub_forums .= $comma.$lang->sprintf($lang->more_subforums, (subforums_count($forums_by_parent[$pid]) - $donecount));
2796                          return;
2797                      }
2798                  }
2799              }
2800          }
2801      }
2802  }
2803  
2804  /**
2805   * @param int $gid
2806   * @param int $fid
2807   *
2808   * @return string
2809   */
2810  function retrieve_single_permissions_row($gid, $fid)
2811  {
2812      global $mybb, $lang, $cache, $db;
2813  
2814      $query = $db->simple_select("usergroups", "*", "gid='{$gid}'");
2815      $usergroup = $db->fetch_array($query);
2816  
2817      $query = $db->simple_select("forums", "*", "fid='{$fid}'");
2818      $forum_data = $db->fetch_array($query);
2819  
2820      $query = $db->simple_select("forumpermissions", "*", "fid='{$fid}'");
2821      while($existing = $db->fetch_array($query))
2822      {
2823          $existing_permissions[$existing['gid']] = $existing;
2824      }
2825  
2826      $cached_forum_perms = $cache->read("forumpermissions");
2827      $field_list = array(
2828          'canview' => $lang->permissions_canview,
2829          'canpostthreads' => $lang->permissions_canpostthreads,
2830          'canpostreplys' => $lang->permissions_canpostreplys,
2831          'canpostpolls' => $lang->permissions_canpostpolls,
2832      );
2833  
2834      $field_list2 = array(
2835          'canview' => $lang->perm_drag_canview,
2836          'canpostthreads' => $lang->perm_drag_canpostthreads,
2837          'canpostreplys' => $lang->perm_drag_canpostreplys,
2838          'canpostpolls' => $lang->perm_drag_canpostpolls,
2839      );
2840  
2841      $form = new Form('', '', "", 0, "", true);
2842      $form_container = new FormContainer();
2843  
2844      $perms = array();
2845  
2846      if(is_array($existing_permissions) && $existing_permissions[$usergroup['gid']])
2847      {
2848          $perms = $existing_permissions[$usergroup['gid']];
2849          $default_checked = false;
2850      }
2851      elseif(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['fid']][$usergroup['gid']])
2852      {
2853          $perms = $cached_forum_perms[$forum_data['fid']][$usergroup['gid']];
2854          $default_checked = true;
2855      }
2856      else if(is_array($cached_forum_perms) && $cached_forum_perms[$forum_data['pid']][$usergroup['gid']])
2857      {
2858          $perms = $cached_forum_perms[$forum_data['pid']][$usergroup['gid']];
2859          $default_checked = true;
2860      }
2861  
2862      if(!$perms)
2863      {
2864          $perms = $usergroup;
2865          $default_checked = true;
2866      }
2867  
2868      foreach($field_list as $forum_permission => $forum_perm_title)
2869      {
2870          if($perms[$forum_permission] == 1)
2871          {
2872              $perms_checked[$forum_permission] = 1;
2873          }
2874          else
2875          {
2876              $perms_checked[$forum_permission] = 0;
2877          }
2878      }
2879  
2880      $usergroup['title'] = htmlspecialchars_uni($usergroup['title']);
2881  
2882      if($default_checked == 1)
2883      {
2884          $inherited_text = $lang->inherited_permission;
2885      }
2886      else
2887      {
2888          $inherited_text = $lang->custom_permission;
2889      }
2890  
2891      $form_container->output_cell("<strong>{$usergroup['title']}</strong> <small style=\"vertical-align: middle;\">({$inherited_text})</small>");
2892  
2893      $field_select = "<div class=\"quick_perm_fields\">\n";
2894      $field_select .= "<div class=\"enabled\"><div class=\"fields_title\">{$lang->enabled}</div><ul id=\"fields_enabled_{$usergroup['gid']}\">\n";
2895      foreach($perms_checked as $perm => $value)
2896      {
2897          if($value == 1)
2898          {
2899              $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2900          }
2901      }
2902      $field_select .= "</ul></div>\n";
2903      $field_select .= "<div class=\"disabled\"><div class=\"fields_title\">{$lang->disabled}</div><ul id=\"fields_disabled_{$usergroup['gid']}\">\n";
2904      foreach($perms_checked as $perm => $value)
2905      {
2906          if($value == 0)
2907          {
2908              $field_select .= "<li id=\"field-{$perm}\">{$field_list2[$perm]}</li>";
2909          }
2910      }
2911      $field_select .= "</ul></div></div>\n";
2912      $field_select .= $form->generate_hidden_field("fields_".$usergroup['gid'], @implode(",", @array_keys($perms_checked, 1)), array('id' => 'fields_'.$usergroup['gid']));
2913      $field_select = str_replace("\n", "", $field_select);
2914  
2915      foreach($field_list as $forum_permission => $permission_title)
2916      {
2917          $field_options[$forum_permission] = $permission_title;
2918      }
2919      $form_container->output_cell($field_select, array('colspan' => 2));
2920  
2921      if(!$default_checked)
2922      {
2923          $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"));
2924          $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"));
2925      }
2926      else
2927      {
2928          $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));
2929      }
2930      $form_container->construct_row();
2931      return $form_container->output_row_cells(0, true);
2932  }
2933  


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