[ Index ]

PHP Cross Reference of MyBB 1.8.27

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