[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

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

   1  <?php
   2  /** 
   3  *
   4  * @package acp
   5  * @version $Id: acp_permissions.php,v 1.41 2006/10/30 19:51:56 acydburn Exp $
   6  * @copyright (c) 2005 phpBB Group 
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
   8  *
   9  */
  10  
  11  /**
  12  * @package acp
  13  */
  14  class acp_permissions
  15  {
  16      var $u_action;
  17      var $permission_dropdown;
  18      
  19  	function main($id, $mode)
  20      {
  21          global $db, $user, $auth, $template, $cache;
  22          global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
  23  
  24          include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  25          include_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
  26  
  27          $auth_admin = new auth_admin();
  28  
  29          $user->add_lang('acp/permissions');
  30          add_permission_language();
  31  
  32          $this->tpl_name = 'acp_permissions';
  33  
  34          // Trace has other vars
  35          if ($mode == 'trace')
  36          {
  37              $user_id = request_var('u', 0);
  38              $forum_id = request_var('f', 0);
  39              $permission = request_var('auth', '');
  40  
  41              $this->tpl_name = 'permission_trace';
  42  
  43              if ($user_id && isset($auth_admin->option_ids[$permission]) && $auth->acl_get('a_viewauth'))
  44              {
  45                  $this->page_title = sprintf($user->lang['TRACE_PERMISSION'], $user->lang['acl_' . $permission]['lang']);
  46                  $this->permission_trace($user_id, $forum_id, $permission);
  47                  return;
  48              }
  49              
  50              trigger_error('NO_MODE', E_USER_ERROR);
  51          }
  52  
  53          // Set some vars
  54          $action = request_var('action', array('' => 0));
  55          $action = key($action);
  56          $action = (isset($_POST['psubmit'])) ? 'apply_permissions' : $action;
  57  
  58          $all_forums = request_var('all_forums', 0);
  59          $subforum_id = request_var('subforum_id', 0);
  60          $forum_id = request_var('forum_id', array(0));
  61  
  62          $username = request_var('username', array(''), true);
  63          $usernames = request_var('usernames', '', true);
  64          $user_id = request_var('user_id', array(0));
  65  
  66          $group_id = request_var('group_id', array(0));
  67          $select_all_groups = request_var('select_all_groups', 0);
  68  
  69          // If select all groups is set, we pre-build the group id array (this option is used for other screens to link to the permission settings screen)
  70          if ($select_all_groups)
  71          {
  72              // Add default groups to selection
  73              $sql_and = (!$config['coppa_enable']) ? " AND group_name <> 'REGISTERED_COPPA'" : '';
  74  
  75              $sql = 'SELECT group_id
  76                  FROM ' . GROUPS_TABLE . '
  77                  WHERE group_type = ' . GROUP_SPECIAL . "
  78                  $sql_and";
  79              $result = $db->sql_query($sql);
  80  
  81              while ($row = $db->sql_fetchrow($result))
  82              {
  83                  $group_id[] = $row['group_id'];
  84              }
  85              $db->sql_freeresult($result);
  86          }
  87          
  88          // Map usernames to ids and vice versa
  89          if ($usernames)
  90          {
  91              $username = explode("\n", $usernames);
  92          }
  93          unset($usernames);
  94  
  95          if (sizeof($username) && !sizeof($user_id))
  96          {
  97              user_get_id_name($user_id, $username);
  98  
  99              if (!sizeof($user_id))
 100              {
 101                  trigger_error($user->lang['SELECTED_USER_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
 102              }
 103          }
 104          unset($username);
 105          
 106          // Build forum ids (of all forums are checked or subforum listing used)
 107          if ($all_forums)
 108          {
 109              $sql = 'SELECT forum_id
 110                  FROM ' . FORUMS_TABLE . '
 111                  ORDER BY left_id';
 112              $result = $db->sql_query($sql);
 113  
 114              $forum_id = array();
 115              while ($row = $db->sql_fetchrow($result))
 116              {
 117                  $forum_id[] = $row['forum_id'];
 118              }
 119              $db->sql_freeresult($result);
 120          }
 121          else if ($subforum_id)
 122          {
 123              $forum_id = array();
 124              foreach (get_forum_branch($subforum_id, 'children') as $row)
 125              {
 126                  $forum_id[] = $row['forum_id'];
 127              }
 128          }
 129  
 130          // Define some common variables for every mode
 131          $error = array();
 132  
 133          $permission_scope = (strpos($mode, '_global') !== false) ? 'global' : 'local';
 134  
 135          // Showing introductionary page?
 136          if ($mode == 'intro')
 137          {
 138              $this->page_title = 'ACP_PERMISSIONS';
 139  
 140              $template->assign_vars(array(
 141                  'S_INTRO'        => true)
 142              );
 143  
 144              return;
 145          }
 146  
 147          switch ($mode)
 148          {
 149              case 'setting_user_global':
 150              case 'setting_group_global':
 151                  $this->permission_dropdown = array('u_', 'm_', 'a_');
 152                  $permission_victim = ($mode == 'setting_user_global') ? array('user') : array('group');
 153                  $this->page_title = ($mode == 'setting_user_global') ? 'ACP_USERS_PERMISSIONS' : 'ACP_GROUPS_PERMISSIONS';
 154              break;
 155  
 156              case 'setting_user_local':
 157              case 'setting_group_local':
 158                  $this->permission_dropdown = array('f_', 'm_');
 159                  $permission_victim = ($mode == 'setting_user_local') ? array('user', 'forums') : array('group', 'forums');
 160                  $this->page_title = ($mode == 'setting_user_local') ? 'ACP_USERS_FORUM_PERMISSIONS' : 'ACP_GROUPS_FORUM_PERMISSIONS';
 161              break;
 162  
 163              case 'setting_admin_global':
 164              case 'setting_mod_global':
 165                  $this->permission_dropdown = (strpos($mode, '_admin_') !== false) ? array('a_') : array('m_');
 166                  $permission_victim = array('usergroup');
 167                  $this->page_title = ($mode == 'setting_admin_global') ? 'ACP_ADMINISTRATORS' : 'ACP_GLOBAL_MODERATORS';
 168              break;
 169  
 170              case 'setting_mod_local':
 171              case 'setting_forum_local':
 172                  $this->permission_dropdown = ($mode == 'setting_mod_local') ? array('m_') : array('f_');
 173                  $permission_victim = array('forums', 'usergroup');
 174                  $this->page_title = ($mode == 'setting_mod_local') ? 'ACP_FORUM_MODERATORS' : 'ACP_FORUM_PERMISSIONS';
 175              break;
 176  
 177              case 'view_admin_global':
 178              case 'view_user_global':
 179              case 'view_mod_global':
 180                  $this->permission_dropdown = ($mode == 'view_admin_global') ? array('a_') : (($mode == 'view_user_global') ? array('u_') : array('m_'));
 181                  $permission_victim = array('usergroup_view');
 182                  $this->page_title = ($mode == 'view_admin_global') ? 'ACP_VIEW_ADMIN_PERMISSIONS' : (($mode == 'view_user_global') ? 'ACP_VIEW_USER_PERMISSIONS' : 'ACP_VIEW_GLOBAL_MOD_PERMISSIONS');
 183              break;
 184  
 185              case 'view_mod_local':
 186              case 'view_forum_local':
 187                  $this->permission_dropdown = ($mode == 'view_mod_local') ? array('m_') : array('f_');
 188                  $permission_victim = array('forums', 'usergroup_view');
 189                  $this->page_title = ($mode == 'view_mod_local') ? 'ACP_VIEW_FORUM_MOD_PERMISSIONS' : 'ACP_VIEW_FORUM_PERMISSIONS';
 190              break;
 191  
 192              default:
 193                  trigger_error('NO_MODE', E_USER_ERROR);
 194              break;
 195          }
 196  
 197          $template->assign_vars(array(
 198              'L_TITLE'        => $user->lang[$this->page_title],
 199              'L_EXPLAIN'        => $user->lang[$this->page_title . '_EXPLAIN'])
 200          );
 201  
 202          // Get permission type
 203          $permission_type = request_var('type', $this->permission_dropdown[0]);
 204  
 205          if (!in_array($permission_type, $this->permission_dropdown))
 206          {
 207              trigger_error($user->lang['WRONG_PERMISSION_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
 208          }
 209  
 210  
 211          // Handle actions
 212          if (strpos($mode, 'setting_') === 0 && $action)
 213          {
 214              switch ($action)
 215              {
 216                  case 'delete':
 217                      // All users/groups selected?
 218                      $all_users = (isset($_POST['all_users'])) ? true : false;
 219                      $all_groups = (isset($_POST['all_groups'])) ? true : false;
 220  
 221                      if ($all_users || $all_groups)
 222                      {
 223                          $items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
 224  
 225                          if ($all_users && sizeof($items['user_ids']))
 226                          {
 227                              $user_id = $items['user_ids'];
 228                          }
 229                          else if ($all_groups && sizeof($items['group_ids']))
 230                          {
 231                              $group_id = $items['group_ids'];
 232                          }
 233                      }
 234  
 235                      if (sizeof($user_id) || sizeof($group_id))
 236                      {
 237                          $this->remove_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id, $forum_id);
 238                      }
 239                      else
 240                      {
 241                          trigger_error($user->lang['NO_USER_GROUP_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
 242                      }
 243                  break;
 244  
 245                  case 'apply_permissions':
 246                      if (!isset($_POST['setting']))
 247                      {
 248                          trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
 249                      }
 250  
 251                      $this->set_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
 252                  break;
 253  
 254                  case 'apply_all_permissions':
 255                      if (!isset($_POST['setting']))
 256                      {
 257                          trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
 258                      }
 259  
 260                      $this->set_all_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
 261                  break;
 262              }
 263          }
 264  
 265  
 266          // Setting permissions screen
 267          $s_hidden_fields = build_hidden_fields(array(
 268              'user_id'        => $user_id,
 269              'group_id'        => $group_id,
 270              'forum_id'        => $forum_id,
 271              'type'            => $permission_type)
 272          );
 273  
 274          // Go through the screens/options needed and present them in correct order
 275          foreach ($permission_victim as $victim)
 276          {
 277              switch ($victim)
 278              {
 279                  case 'forum_dropdown':
 280  
 281                      if (sizeof($forum_id))
 282                      {
 283                          $this->check_existence('forum', $forum_id);
 284                          continue 2;
 285                      }
 286  
 287                      $template->assign_vars(array(
 288                          'S_SELECT_FORUM'        => true,
 289                          'S_FORUM_OPTIONS'        => make_forum_select(false, false, true, false, false))
 290                      );
 291  
 292                  break;
 293  
 294                  case 'forums':
 295  
 296                      if (sizeof($forum_id))
 297                      {
 298                          $this->check_existence('forum', $forum_id);
 299                          continue 2;
 300                      }
 301  
 302                      $forum_list = make_forum_select(false, false, true, false, false, false, true);
 303  
 304                      // Build forum options
 305                      $s_forum_options = '';
 306                      foreach ($forum_list as $f_id => $f_row)
 307                      {
 308                          $s_forum_options .= '<option value="' . $f_id . '"' . $f_row['selected'] . '>' . $f_row['padding'] . $f_row['forum_name'] . '</option>';
 309                      }
 310  
 311                      // Build subforum options
 312                      $s_subforum_options = $this->build_subforum_options($forum_list);
 313  
 314                      $template->assign_vars(array(
 315                          'S_SELECT_FORUM'        => true,
 316                          'S_FORUM_OPTIONS'        => $s_forum_options,
 317                          'S_SUBFORUM_OPTIONS'    => $s_subforum_options,
 318                          'S_FORUM_ALL'            => true,
 319                          'S_FORUM_MULTIPLE'        => true)
 320                      );
 321  
 322                  break;
 323  
 324                  case 'user':
 325  
 326                      if (sizeof($user_id))
 327                      {
 328                          $this->check_existence('user', $user_id);
 329                          continue 2;
 330                      }
 331  
 332                      $template->assign_vars(array(
 333                          'S_SELECT_USER'            => true,
 334                          'U_FIND_USERNAME'        => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=select_victim&amp;field=username'),
 335                          'UA_FIND_USERNAME'        => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=select_victim&field=username', false))
 336                      );
 337  
 338                  break;
 339  
 340                  case 'group':
 341  
 342                      if (sizeof($group_id))
 343                      {
 344                          $this->check_existence('group', $group_id);
 345                          continue 2;
 346                      }
 347  
 348                      $template->assign_vars(array(
 349                          'S_SELECT_GROUP'        => true,
 350                          'S_GROUP_OPTIONS'        => group_select_options(false))
 351                      );
 352  
 353                  break;
 354  
 355                  case 'usergroup':
 356                  case 'usergroup_view':
 357  
 358                      if (sizeof($user_id) || sizeof($group_id))
 359                      {
 360                          if (sizeof($user_id))
 361                          {
 362                              $this->check_existence('user', $user_id);
 363                          }
 364  
 365                          if (sizeof($group_id))
 366                          {
 367                              $this->check_existence('group', $group_id);
 368                          }
 369  
 370                          continue 2;
 371                      }
 372  
 373                      $items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
 374  
 375                      // Now we check the users... because the "all"-selection is different here (all defined users/groups)
 376                      $all_users = (isset($_POST['all_users'])) ? true : false;
 377                      $all_groups = (isset($_POST['all_groups'])) ? true : false;
 378  
 379                      if ($all_users && sizeof($items['user_ids']))
 380                      {
 381                          $user_id = $items['user_ids'];
 382                          continue 2;
 383                      }
 384  
 385                      if ($all_groups && sizeof($items['group_ids']))
 386                      {
 387                          $group_id = $items['group_ids'];
 388                          continue 2;
 389                      }
 390  
 391                      $template->assign_vars(array(
 392                          'S_SELECT_USERGROUP'        => ($victim == 'usergroup') ? true : false,
 393                          'S_SELECT_USERGROUP_VIEW'    => ($victim == 'usergroup_view') ? true : false,
 394                          'S_DEFINED_USER_OPTIONS'    => $items['user_ids_options'],
 395                          'S_DEFINED_GROUP_OPTIONS'    => $items['group_ids_options'],
 396                          'S_ADD_GROUP_OPTIONS'        => group_select_options(false, $items['group_ids']),
 397                          'U_FIND_USERNAME'            => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=add_user&amp;field=username'),
 398                          'UA_FIND_USERNAME'            => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=add_user&field=username', false))
 399                      );
 400  
 401                  break;
 402              }
 403  
 404              // The S_ALLOW_SELECT parameter below is a measure to lower memory usage.
 405              // If there are more than 5 forums selected the admin is not able to select all users/groups too.
 406              // We need to see if the number of forums can be increased or need to be decreased.
 407  
 408              $template->assign_vars(array(
 409                  'U_ACTION'                => $this->u_action,
 410                  'ANONYMOUS_USER_ID'        => ANONYMOUS,
 411  
 412                  'S_SELECT_VICTIM'        => true,
 413                  'S_ALLOW_ALL_SELECT'    => (sizeof($forum_id) > 5) ? false : true,
 414                  'S_CAN_SELECT_USER'        => ($auth->acl_get('a_authusers')) ? true : false,
 415                  'S_CAN_SELECT_GROUP'    => ($auth->acl_get('a_authgroups')) ? true : false,
 416                  'S_HIDDEN_FIELDS'        => $s_hidden_fields)
 417              );
 418  
 419              // Let the forum names being displayed
 420              if (sizeof($forum_id))
 421              {
 422                  $sql = 'SELECT forum_name
 423                      FROM ' . FORUMS_TABLE . '
 424                      WHERE ' . $db->sql_in_set('forum_id', $forum_id) . '
 425                      ORDER BY forum_name ASC';
 426                  $result = $db->sql_query($sql);
 427  
 428                  $forum_names = array();
 429                  while ($row = $db->sql_fetchrow($result))
 430                  {
 431                      $forum_names[] = $row['forum_name'];
 432                  }
 433                  $db->sql_freeresult($result);
 434  
 435                  $template->assign_vars(array(
 436                      'S_FORUM_NAMES'        => (sizeof($forum_names)) ? true : false,
 437                      'FORUM_NAMES'        => implode(', ', $forum_names))
 438                  );
 439              }
 440  
 441              return;
 442          }
 443  
 444          // Do not allow forum_ids being set and no other setting defined (will bog down the server too much)
 445          if (sizeof($forum_id) && !sizeof($user_id) && !sizeof($group_id))
 446          {
 447              trigger_error($user->lang['ONLY_FORUM_DEFINED'] . adm_back_link($this->u_action), E_USER_WARNING);
 448          }
 449  
 450          $template->assign_vars(array(
 451              'S_PERMISSION_DROPDOWN'        => (sizeof($this->permission_dropdown) > 1) ? $this->build_permission_dropdown($this->permission_dropdown, $permission_type) : false,
 452              'L_PERMISSION_TYPE'            => $user->lang['ACL_TYPE_' . strtoupper($permission_type)],
 453  
 454              'U_ACTION'                    => $this->u_action,
 455              'S_HIDDEN_FIELDS'            => $s_hidden_fields)
 456          );
 457  
 458          if (strpos($mode, 'setting_') === 0)
 459          {
 460              $template->assign_vars(array(
 461                  'S_SETTING_PERMISSIONS'        => true)
 462              );
 463  
 464              $hold_ary = $auth_admin->get_mask('set', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NO);
 465              $auth_admin->display_mask('set', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
 466          }
 467          else
 468          {
 469              $template->assign_vars(array(
 470                  'S_VIEWING_PERMISSIONS'        => true)
 471              );
 472  
 473              $hold_ary = $auth_admin->get_mask('view', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NEVER);
 474              $auth_admin->display_mask('view', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
 475          }
 476      }
 477  
 478      /**
 479      * Build +subforum options
 480      */
 481  	function build_subforum_options($forum_list)
 482      {
 483          global $user;
 484  
 485          $s_options = '';
 486  
 487          $forum_list = array_merge($forum_list);
 488  
 489          foreach ($forum_list as $key => $row)
 490          {
 491              $s_options .= '<option value="' . $row['forum_id'] . '"' . $row['selected'] . '>' . $row['padding'] . $row['forum_name'];
 492  
 493              // We check if a branch is there...
 494              $branch_there = false;
 495  
 496              foreach (array_slice($forum_list, $key + 1) as $temp_row)
 497              {
 498                  if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
 499                  {
 500                      $branch_there = true;
 501                      break;
 502                  }
 503                  continue;
 504              }
 505              
 506              if ($branch_there)
 507              {
 508                  $s_options .= ' [' . $user->lang['PLUS_SUBFORUMS'] . ']';
 509              }
 510  
 511              $s_options .= '</option>';
 512          }
 513  
 514          return $s_options;
 515      }
 516      
 517      /**
 518      * Build dropdown field for changing permission types
 519      */
 520  	function build_permission_dropdown($options, $default_option)
 521      {
 522          global $user, $auth;
 523          
 524          $s_dropdown_options = '';
 525          foreach ($options as $setting)
 526          {
 527              if (!$auth->acl_get('a_' . str_replace('_', '', $setting) . 'auth'))
 528              {
 529                  continue;
 530              }
 531              $selected = ($setting == $default_option) ? ' selected="selected"' : '';
 532              $s_dropdown_options .= '<option value="' . $setting . '"' . $selected . '>' . $user->lang['permission_type'][$setting] . '</option>';
 533          }
 534  
 535          return $s_dropdown_options;
 536      }
 537  
 538      /**
 539      * Check if selected items exist. Remove not found ids and if empty return error.
 540      */
 541  	function check_existence($mode, &$ids)
 542      {
 543          global $db, $user;
 544  
 545          switch ($mode)
 546          {
 547              case 'user':
 548                  $table = USERS_TABLE;
 549                  $sql_id = 'user_id';
 550              break;
 551  
 552              case 'group':
 553                  $table = GROUPS_TABLE;
 554                  $sql_id = 'group_id';
 555              break;
 556  
 557              case 'forum':
 558                  $table = FORUMS_TABLE;
 559                  $sql_id = 'forum_id';
 560              break;
 561          }
 562  
 563          $sql = "SELECT $sql_id
 564              FROM $table
 565              WHERE " . $db->sql_in_set($sql_id, $ids);
 566          $result = $db->sql_query($sql);
 567  
 568          $ids = array();
 569          while ($row = $db->sql_fetchrow($result))
 570          {
 571              $ids[] = $row[$sql_id];
 572          }
 573          $db->sql_freeresult($result);
 574  
 575          if (!sizeof($ids))
 576          {
 577              trigger_error($user->lang['SELECTED_' . strtoupper($mode) . '_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
 578          }
 579      }
 580  
 581      /** 
 582      * Apply permissions
 583      */
 584  	function set_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
 585      {
 586          global $user, $auth;
 587  
 588          $psubmit = request_var('psubmit', array(0));
 589  
 590          // User or group to be set?
 591          $ug_type = (sizeof($user_id)) ? 'user' : 'group';
 592  
 593          // Check the permission setting again
 594          if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
 595          {
 596              trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
 597          }
 598          
 599          $ug_id = $forum_id = 0;
 600  
 601          // We loop through the auth settings defined in our submit
 602          list($ug_id, ) = each($psubmit);
 603          list($forum_id, ) = each($psubmit[$ug_id]);
 604  
 605          $auth_settings = array_map('intval', $_POST['setting'][$ug_id][$forum_id]);
 606  
 607          // Do we have a role we want to set?
 608          $assigned_role = (isset($_POST['role'][$ug_id][$forum_id])) ? (int) $_POST['role'][$ug_id][$forum_id] : 0;
 609  
 610          // Do the admin want to set these permissions to other items too?
 611          $inherit = request_var('inherit', array(0));
 612  
 613          $ug_id = array($ug_id);
 614          $forum_id = array($forum_id);
 615  
 616          if (sizeof($inherit))
 617          {
 618              foreach ($inherit as $_ug_id => $forum_id_ary)
 619              {
 620                  // Inherit users/groups?
 621                  if (!in_array($_ug_id, $ug_id))
 622                  {
 623                      $ug_id[] = $_ug_id;
 624                  }
 625  
 626                  // Inherit forums?
 627                  $forum_id = array_merge($forum_id, array_keys($forum_id_ary));
 628              }
 629          }
 630  
 631          $forum_id = array_unique($forum_id);
 632  
 633          // If the auth settings differ from the assigned role, then do not set a role...
 634          if ($assigned_role)
 635          {
 636              if (!$this->check_assigned_role($assigned_role, $auth_settings))
 637              {
 638                  $assigned_role = 0;
 639              }
 640          }
 641  
 642          // Update the permission set...
 643          $auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_settings, $assigned_role);
 644  
 645          // Do we need to recache the moderator lists?
 646          if ($permission_type == 'm_')
 647          {
 648              cache_moderators();
 649          }
 650  
 651          // Remove users who are now moderators or admins from everyones foes list
 652          if ($permission_type == 'm_' || $permission_type == 'a_')
 653          {
 654              update_foes();
 655          }
 656  
 657          $this->log_action($mode, 'add', $permission_type, $ug_type, $ug_id, $forum_id);
 658  
 659          trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
 660      }
 661  
 662      /** 
 663      * Apply all permissions
 664      */
 665  	function set_all_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
 666      {
 667          global $user, $auth;
 668  
 669          // User or group to be set?
 670          $ug_type = (sizeof($user_id)) ? 'user' : 'group';
 671  
 672          // Check the permission setting again
 673          if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
 674          {
 675              trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
 676          }
 677  
 678          $auth_settings = (isset($_POST['setting'])) ? $_POST['setting'] : array();
 679          $auth_roles = (isset($_POST['role'])) ? $_POST['role'] : array();
 680          $ug_ids = $forum_ids = array();
 681  
 682          // We need to go through the auth settings
 683          foreach ($auth_settings as $ug_id => $forum_auth_row)
 684          {
 685              $ug_id = (int) $ug_id;
 686              $ug_ids[] = $ug_id;
 687  
 688              foreach ($forum_auth_row as $forum_id => $auth_options)
 689              {
 690                  $forum_id = (int) $forum_id;
 691                  $forum_ids[] = $forum_id;
 692  
 693                  // Check role...
 694                  $assigned_role = (isset($auth_roles[$ug_id][$forum_id])) ? (int) $auth_roles[$ug_id][$forum_id] : 0;
 695  
 696                  // If the auth settings differ from the assigned role, then do not set a role...
 697                  if ($assigned_role)
 698                  {
 699                      if (!$this->check_assigned_role($assigned_role, $auth_options))
 700                      {
 701                          $assigned_role = 0;
 702                      }
 703                  }
 704  
 705                  // Update the permission set...
 706                  $auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_options, $assigned_role, false);
 707              }
 708          }
 709  
 710          $auth_admin->acl_clear_prefetch();
 711  
 712          // Do we need to recache the moderator lists?
 713          if ($permission_type == 'm_')
 714          {
 715              cache_moderators();
 716          }
 717  
 718          // Remove users who are now moderators or admins from everyones foes list
 719          if ($permission_type == 'm_' || $permission_type == 'a_')
 720          {
 721              update_foes();
 722          }
 723  
 724          $this->log_action($mode, 'add', $permission_type, $ug_type, $ug_ids, $forum_ids);
 725  
 726          trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
 727      }
 728  
 729      /**
 730      * Compare auth settings with auth settings from role
 731      * returns false if they differ, true if they are equal
 732      */
 733  	function check_assigned_role($role_id, &$auth_settings)
 734      {
 735          global $db;
 736  
 737          $sql = 'SELECT o.auth_option, r.auth_setting
 738              FROM ' . ACL_OPTIONS_TABLE . ' o, ' . ACL_ROLES_DATA_TABLE . ' r
 739              WHERE o.auth_option_id = r.auth_option_id
 740                  AND r.role_id = ' . $role_id;
 741          $result = $db->sql_query($sql);
 742  
 743          $test_auth_settings = array();
 744          while ($row = $db->sql_fetchrow($result))
 745          {
 746              $test_auth_settings[$row['auth_option']] = $row['auth_setting'];
 747          }
 748          $db->sql_freeresult($result);
 749  
 750          // We need to add any ACL_NO setting from auth_settings to compare correctly
 751          foreach ($auth_settings as $option => $setting)
 752          {
 753              if ($setting == ACL_NO)
 754              {
 755                  $test_auth_settings[$option] = $setting;
 756              }
 757          }
 758  
 759          if (sizeof(array_diff_assoc($auth_settings, $test_auth_settings)))
 760          {
 761              return false;
 762          }
 763  
 764          return true;
 765      }
 766  
 767      /**
 768      * Remove permissions
 769      */
 770  	function remove_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id, &$forum_id)
 771      {
 772          global $user, $db, $auth;
 773              
 774          // User or group to be set?
 775          $ug_type = (sizeof($user_id)) ? 'user' : 'group';
 776  
 777          // Check the permission setting again
 778          if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
 779          {
 780              trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
 781          }
 782  
 783          $auth_admin->acl_delete($ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : false), $permission_type);
 784  
 785          // Do we need to recache the moderator lists?
 786          if ($permission_type == 'm_')
 787          {
 788              cache_moderators();
 789          }
 790  
 791          $this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : array(0 => 0)));
 792  
 793          trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
 794      }
 795  
 796      /**
 797      * Log permission changes
 798      */
 799  	function log_action($mode, $action, $permission_type, $ug_type, $ug_id, $forum_id)
 800      {
 801          global $db, $user;
 802  
 803          if (!is_array($ug_id))
 804          {
 805              $ug_id = array($ug_id);
 806          }
 807  
 808          if (!is_array($forum_id))
 809          {
 810              $forum_id = array($forum_id);
 811          }
 812  
 813          // Logging ... first grab user or groupnames ...
 814          $sql = ($ug_type == 'group') ? 'SELECT group_name as name, group_type FROM ' . GROUPS_TABLE . ' WHERE ' : 'SELECT username as name FROM ' . USERS_TABLE . ' WHERE ';
 815          $sql .=  $db->sql_in_set(($ug_type == 'group') ? 'group_id' : 'user_id', array_map('intval', $ug_id));
 816          $result = $db->sql_query($sql);
 817  
 818          $l_ug_list = '';
 819          while ($row = $db->sql_fetchrow($result))
 820          {
 821              $l_ug_list .= (($l_ug_list != '') ? ', ' : '') . ((isset($row['group_type']) && $row['group_type'] == GROUP_SPECIAL) ? '<span class="blue">' . $user->lang['G_' . $row['name']] . '</span>' : $row['name']);
 822          }
 823          $db->sql_freeresult($result);
 824  
 825          $mode = str_replace('setting_', '', $mode);
 826  
 827          if ($forum_id[0] == 0)
 828          {
 829              add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_ug_list);
 830          }
 831          else
 832          {
 833              // Grab the forum details if non-zero forum_id
 834              $sql = 'SELECT forum_name  
 835                  FROM ' . FORUMS_TABLE . '
 836                  WHERE ' . $db->sql_in_set('forum_id', $forum_id);
 837              $result = $db->sql_query($sql);
 838  
 839              $l_forum_list = '';
 840              while ($row = $db->sql_fetchrow($result))
 841              {
 842                  $l_forum_list .= (($l_forum_list != '') ? ', ' : '') . $row['forum_name'];
 843              }
 844              $db->sql_freeresult($result);
 845  
 846              add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_forum_list, $l_ug_list);
 847          }
 848      }
 849  
 850      /**
 851      * Display a complete trace tree for the selected permission to determine where settings are set/unset
 852      */
 853  	function permission_trace($user_id, $forum_id, $permission)
 854      {
 855          global $db, $template, $user, $auth;
 856  
 857          if ($user_id != $user->data['user_id'])
 858          {
 859              $sql = 'SELECT user_id, username, user_permissions, user_type
 860                  FROM ' . USERS_TABLE . '
 861                  WHERE user_id = ' . $user_id;
 862              $result = $db->sql_query($sql);
 863              $userdata = $db->sql_fetchrow($result);
 864              $db->sql_freeresult($result);
 865          }
 866          else
 867          {
 868              $userdata = $user->data;
 869          }
 870  
 871          if (!$userdata)
 872          {
 873              trigger_error('NO_USERS', E_USER_ERROR);
 874          }
 875  
 876          $forum_name = false;
 877  
 878          if ($forum_id)
 879          {
 880              $sql = 'SELECT forum_name
 881                  FROM ' . FORUMS_TABLE . "
 882                  WHERE forum_id = $forum_id";
 883              $result = $db->sql_query($sql, 3600);
 884              $forum_name = $db->sql_fetchfield('forum_name');
 885              $db->sql_freeresult($result);
 886          }
 887  
 888          $back = request_var('back', 0);
 889  
 890          $template->assign_vars(array(
 891              'PERMISSION'            => $user->lang['acl_' . $permission]['lang'],
 892              'PERMISSION_USERNAME'    => $userdata['username'],
 893              'FORUM_NAME'            => $forum_name,
 894              'U_BACK'                    => ($back) ? build_url(array('f', 'back')) . "&amp;f=$back" : '')
 895          );
 896  
 897          $template->assign_block_vars('trace', array(
 898              'WHO'            => $user->lang['DEFAULT'],
 899              'INFORMATION'    => $user->lang['TRACE_DEFAULT'],
 900  
 901              'S_SETTING_NO'        => true,
 902              'S_TOTAL_NO'        => true)
 903          );
 904  
 905          $sql = 'SELECT DISTINCT g.group_name, g.group_id, g.group_type
 906              FROM ' . GROUPS_TABLE . ' g
 907                  LEFT JOIN ' . USER_GROUP_TABLE . ' ug ON (ug.group_id = g.group_id)
 908              WHERE ug.user_id = ' . $user_id . '
 909                  AND ug.user_pending = 0
 910              ORDER BY g.group_type DESC, g.group_id DESC';
 911          $result = $db->sql_query($sql);
 912  
 913          $groups = array();
 914          while ($row = $db->sql_fetchrow($result))
 915          {
 916              $groups[$row['group_id']] = array(
 917                  'auth_setting'        => ACL_NO,
 918                  'group_name'        => ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']
 919              );
 920          }
 921          $db->sql_freeresult($result);
 922  
 923          $total = ACL_NO;
 924          if (sizeof($groups))
 925          {
 926              // Get group auth settings
 927              $hold_ary = $auth->acl_group_raw_data(array_keys($groups), $permission, $forum_id);
 928  
 929              foreach ($hold_ary as $group_id => $forum_ary)
 930              {
 931                  $groups[$group_id]['auth_setting'] = $hold_ary[$group_id][$forum_id][$permission];
 932              }
 933              unset($hold_ary);
 934  
 935              foreach ($groups as $id => $row)
 936              {
 937                  switch ($row['auth_setting'])
 938                  {
 939                      case ACL_NO:
 940                          $information = $user->lang['TRACE_GROUP_NO'];
 941                      break;
 942  
 943                      case ACL_YES:
 944                          $information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_YES_TOTAL_YES'] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_YES_TOTAL_NEVER'] : $user->lang['TRACE_GROUP_YES_TOTAL_NO']);
 945                          $total = ($total == ACL_NO) ? ACL_YES : $total;
 946                      break;
 947  
 948                      case ACL_NEVER:
 949                          $information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_YES'] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_NEVER'] : $user->lang['TRACE_GROUP_NEVER_TOTAL_NO']);
 950                          $total = ACL_NEVER;
 951                      break;
 952                  }
 953  
 954                  $template->assign_block_vars('trace', array(
 955                      'WHO'            => $row['group_name'],
 956                      'INFORMATION'    => $information,
 957  
 958                      'S_SETTING_NO'        => ($row['auth_setting'] == ACL_NO) ? true : false,
 959                      'S_SETTING_YES'        => ($row['auth_setting'] == ACL_YES) ? true : false,
 960                      'S_SETTING_NEVER'    => ($row['auth_setting'] == ACL_NEVER) ? true : false,
 961                      'S_TOTAL_NO'        => ($total == ACL_NO) ? true : false,
 962                      'S_TOTAL_YES'        => ($total == ACL_YES) ? true : false,
 963                      'S_TOTAL_NEVER'        => ($total == ACL_NEVER) ? true : false)
 964                  );
 965              }
 966          }
 967  
 968          // Get user specific permission...
 969          $hold_ary = $auth->acl_user_raw_data($user_id, $permission, $forum_id);
 970          $auth_setting = (!sizeof($hold_ary)) ? ACL_NO : $hold_ary[$user_id][$forum_id][$permission];
 971  
 972          switch ($auth_setting)
 973          {
 974              case ACL_NO:
 975                  $information = ($total == ACL_NO) ? $user->lang['TRACE_USER_NO_TOTAL_NO'] : $user->lang['TRACE_USER_KEPT'];
 976                  $total = ($total == ACL_NO) ? ACL_NEVER : $total;
 977              break;
 978  
 979              case ACL_YES:
 980                  $information = ($total == ACL_YES) ? $user->lang['TRACE_USER_YES_TOTAL_YES'] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_YES_TOTAL_NEVER'] : $user->lang['TRACE_USER_YES_TOTAL_NO']);
 981                  $total = ($total == ACL_NO) ? ACL_YES : $total;
 982              break;
 983  
 984              case ACL_NEVER:
 985                  $information = ($total == ACL_YES) ? $user->lang['TRACE_USER_NEVER_TOTAL_YES'] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_NEVER_TOTAL_NEVER'] : $user->lang['TRACE_USER_NEVER_TOTAL_NO']);
 986                  $total = ACL_NEVER;
 987              break;
 988          }
 989  
 990          $template->assign_block_vars('trace', array(
 991              'WHO'            => $userdata['username'],
 992              'INFORMATION'    => $information,
 993  
 994              'S_SETTING_NO'        => ($auth_setting == ACL_NO) ? true : false,
 995              'S_SETTING_YES'        => ($auth_setting == ACL_YES) ? true : false,
 996              'S_SETTING_NEVER'    => ($auth_setting == ACL_NEVER) ? true : false,
 997              'S_TOTAL_NO'        => false,
 998              'S_TOTAL_YES'        => ($total == ACL_YES) ? true : false,
 999              'S_TOTAL_NEVER'        => ($total == ACL_NEVER) ? true : false)
1000          );
1001  
1002          // global permission might overwrite local permission
1003          if (($forum_id != 0) && isset($auth->acl_options['global'][$permission]))
1004          {
1005              if ($user_id != $user->data['user_id'])
1006              {
1007                  $auth2 = new auth();
1008                  $auth2->acl($userdata);
1009                  $auth_setting = $auth2->acl_get($permission);
1010              }
1011              else
1012              {
1013                  $auth_setting = $auth->acl_get($permission);
1014              }
1015  
1016              if ($auth_setting)
1017              {
1018                  $information = ($total == ACL_YES) ? $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_YES'] : $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_NEVER'];
1019                  $total = ACL_YES;
1020              }
1021              else
1022              {
1023                  $information = $user->lang['TRACE_USER_GLOBAL_NEVER_TOTAL_KEPT'];
1024              }
1025  
1026              $template->assign_block_vars('trace', array(
1027                  'WHO'            => sprintf($user->lang['TRACE_GLOBAL_SETTING'], $userdata['username']),
1028                  'INFORMATION'    => sprintf($information, '<a href="' . $this->u_action . "&amp;u=$user_id&amp;f=0&amp;auth=$permission&amp;back=$forum_id\">", '</a>'),
1029  
1030                  'S_SETTING_NO'        => false,
1031                  'S_SETTING_YES'        => $auth_setting,
1032                  'S_SETTING_NEVER'    => !$auth_setting,
1033                  'S_TOTAL_NO'        => false,
1034                  'S_TOTAL_YES'        => ($total == ACL_YES) ? true : false,
1035                  'S_TOTAL_NEVER'        => ($total == ACL_NEVER) ? true : false)
1036              );
1037          }
1038  
1039          // Take founder status into account, overwriting the default values
1040          if ($userdata['user_type'] == USER_FOUNDER && strpos($permission, 'a_') === 0)
1041          {
1042              $template->assign_block_vars('trace', array(
1043                  'WHO'            => $userdata['username'],
1044                  'INFORMATION'    => $user->lang['TRACE_USER_FOUNDER'],
1045  
1046                  'S_SETTING_NO'        => ($auth_setting == ACL_NO) ? true : false,
1047                  'S_SETTING_YES'        => ($auth_setting == ACL_YES) ? true : false,
1048                  'S_SETTING_NEVER'    => ($auth_setting == ACL_NEVER) ? true : false,
1049                  'S_TOTAL_NO'        => false,
1050                  'S_TOTAL_YES'        => true,
1051                  'S_TOTAL_NEVER'        => false)
1052              );
1053          }
1054      }
1055  
1056      /**
1057      * Get already assigned users/groups
1058      */
1059  	function retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type)
1060      {
1061          global $db, $user;
1062  
1063          $sql_forum_id = ($permission_scope == 'global') ? 'AND a.forum_id = 0' : ((sizeof($forum_id)) ? 'AND ' . $db->sql_in_set('a.forum_id', $forum_id) : 'AND a.forum_id <> 0');
1064          $sql_permission_option = "AND o.auth_option LIKE '" . $db->sql_escape($permission_type) . "%'";
1065  
1066          $sql = $db->sql_build_query('SELECT_DISTINCT', array(
1067              'SELECT'    => 'u.username, u.user_regdate, u.user_id',
1068  
1069              'FROM'        => array(
1070                  USERS_TABLE            => 'u',
1071                  ACL_OPTIONS_TABLE    => 'o',
1072                  ACL_USERS_TABLE        => 'a'
1073              ),
1074  
1075              'LEFT_JOIN'    => array(
1076                  array(
1077                      'FROM'    => array(ACL_ROLES_DATA_TABLE => 'r'),
1078                      'ON'    => 'a.auth_role_id = r.role_id'
1079                  )
1080              ),
1081  
1082              'WHERE'        => "(a.auth_option_id = o.auth_option_id OR r.auth_option_id = o.auth_option_id)
1083                  $sql_permission_option
1084                  $sql_forum_id
1085                  AND u.user_id = a.user_id",
1086  
1087              'ORDER_BY'    => 'u.username, u.user_regdate ASC'
1088          ));
1089          $result = $db->sql_query($sql);
1090  
1091          $s_defined_user_options = '';
1092          $defined_user_ids = array();
1093          while ($row = $db->sql_fetchrow($result))
1094          {
1095              $s_defined_user_options .= '<option value="' . $row['user_id'] . '">' . $row['username'] . '</option>';
1096              $defined_user_ids[] = $row['user_id'];
1097          }
1098          $db->sql_freeresult($result);
1099  
1100          $sql = $db->sql_build_query('SELECT_DISTINCT', array(
1101              'SELECT'    => 'g.group_type, g.group_name, g.group_id',
1102  
1103              'FROM'        => array(
1104                  GROUPS_TABLE        => 'g',
1105                  ACL_OPTIONS_TABLE    => 'o',
1106                  ACL_GROUPS_TABLE    => 'a'
1107              ),
1108  
1109              'LEFT_JOIN'    => array(
1110                  array(
1111                      'FROM'    => array(ACL_ROLES_DATA_TABLE => 'r'),
1112                      'ON'    => 'a.auth_role_id = r.role_id'
1113                  )
1114              ),
1115  
1116              'WHERE'        => "(a.auth_option_id = o.auth_option_id OR r.auth_option_id = o.auth_option_id)
1117                  $sql_permission_option
1118                  $sql_forum_id
1119                  AND g.group_id = a.group_id",
1120  
1121              'ORDER_BY'    => 'g.group_type DESC, g.group_name ASC'
1122          ));
1123          $result = $db->sql_query($sql);
1124  
1125          $s_defined_group_options = '';
1126          $defined_group_ids = array();
1127          while ($row = $db->sql_fetchrow($result))
1128          {
1129              $s_defined_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '">' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
1130              $defined_group_ids[] = $row['group_id'];
1131          }
1132          $db->sql_freeresult($result);
1133  
1134          return array(
1135              'group_ids'            => $defined_group_ids,
1136              'group_ids_options'    => $s_defined_group_options,
1137              'user_ids'            => $defined_user_ids,
1138              'user_ids_options'    => $s_defined_user_options
1139          );
1140      }
1141  }
1142  
1143  ?>


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6