includes/acp/acp_permissions.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/acp_permissions.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1315 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* @package acp
    1.24 +*/
    1.25 +class acp_permissions
    1.26 +{
    1.27 +	var $u_action;
    1.28 +	var $permission_dropdown;
    1.29 +
    1.30 +	function main($id, $mode)
    1.31 +	{
    1.32 +		global $db, $user, $auth, $template, $cache;
    1.33 +		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
    1.34 +
    1.35 +		include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
    1.36 +		include_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
    1.37 +
    1.38 +		$auth_admin = new auth_admin();
    1.39 +
    1.40 +		$user->add_lang('acp/permissions');
    1.41 +		add_permission_language();
    1.42 +
    1.43 +		$this->tpl_name = 'acp_permissions';
    1.44 +
    1.45 +		// Trace has other vars
    1.46 +		if ($mode == 'trace')
    1.47 +		{
    1.48 +			$user_id = request_var('u', 0);
    1.49 +			$forum_id = request_var('f', 0);
    1.50 +			$permission = request_var('auth', '');
    1.51 +
    1.52 +			$this->tpl_name = 'permission_trace';
    1.53 +
    1.54 +			if ($user_id && isset($auth_admin->acl_options['id'][$permission]) && $auth->acl_get('a_viewauth'))
    1.55 +			{
    1.56 +				$this->page_title = sprintf($user->lang['TRACE_PERMISSION'], $user->lang['acl_' . $permission]['lang']);
    1.57 +				$this->permission_trace($user_id, $forum_id, $permission);
    1.58 +				return;
    1.59 +			}
    1.60 +			trigger_error('NO_MODE', E_USER_ERROR);
    1.61 +		}
    1.62 +
    1.63 +		// Copy forum permissions
    1.64 +		if ($mode == 'setting_forum_copy')
    1.65 +		{
    1.66 +			$this->tpl_name = 'permission_forum_copy';
    1.67 +
    1.68 +			if ($auth->acl_get('a_fauth') && $auth->acl_get('a_authusers') && $auth->acl_get('a_authgroups') && $auth->acl_get('a_mauth'))
    1.69 +			{
    1.70 +				$this->page_title = 'ACP_FORUM_PERMISSIONS_COPY';
    1.71 +				$this->copy_forum_permissions();
    1.72 +				return;
    1.73 +			}
    1.74 +
    1.75 +			trigger_error('NO_MODE', E_USER_ERROR);
    1.76 +		}
    1.77 +
    1.78 +		// Set some vars
    1.79 +		$action = request_var('action', array('' => 0));
    1.80 +		$action = key($action);
    1.81 +		$action = (isset($_POST['psubmit'])) ? 'apply_permissions' : $action;
    1.82 +
    1.83 +		$all_forums = request_var('all_forums', 0);
    1.84 +		$subforum_id = request_var('subforum_id', 0);
    1.85 +		$forum_id = request_var('forum_id', array(0));
    1.86 +
    1.87 +		$username = request_var('username', array(''), true);
    1.88 +		$usernames = request_var('usernames', '', true);
    1.89 +		$user_id = request_var('user_id', array(0));
    1.90 +
    1.91 +		$group_id = request_var('group_id', array(0));
    1.92 +		$select_all_groups = request_var('select_all_groups', 0);
    1.93 +
    1.94 +		$form_name = 'acp_permissions';
    1.95 +		add_form_key($form_name);
    1.96 +
    1.97 +		// 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)
    1.98 +		if ($select_all_groups)
    1.99 +		{
   1.100 +			// Add default groups to selection
   1.101 +			$sql_and = (!$config['coppa_enable']) ? " AND group_name <> 'REGISTERED_COPPA'" : '';
   1.102 +
   1.103 +			$sql = 'SELECT group_id
   1.104 +				FROM ' . GROUPS_TABLE . '
   1.105 +				WHERE group_type = ' . GROUP_SPECIAL . "
   1.106 +				$sql_and";
   1.107 +			$result = $db->sql_query($sql);
   1.108 +
   1.109 +			while ($row = $db->sql_fetchrow($result))
   1.110 +			{
   1.111 +				$group_id[] = $row['group_id'];
   1.112 +			}
   1.113 +			$db->sql_freeresult($result);
   1.114 +		}
   1.115 +
   1.116 +		// Map usernames to ids and vice versa
   1.117 +		if ($usernames)
   1.118 +		{
   1.119 +			$username = explode("\n", $usernames);
   1.120 +		}
   1.121 +		unset($usernames);
   1.122 +
   1.123 +		if (sizeof($username) && !sizeof($user_id))
   1.124 +		{
   1.125 +			user_get_id_name($user_id, $username);
   1.126 +
   1.127 +			if (!sizeof($user_id))
   1.128 +			{
   1.129 +				trigger_error($user->lang['SELECTED_USER_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.130 +			}
   1.131 +		}
   1.132 +		unset($username);
   1.133 +
   1.134 +		// Build forum ids (of all forums are checked or subforum listing used)
   1.135 +		if ($all_forums)
   1.136 +		{
   1.137 +			$sql = 'SELECT forum_id
   1.138 +				FROM ' . FORUMS_TABLE . '
   1.139 +				ORDER BY left_id';
   1.140 +			$result = $db->sql_query($sql);
   1.141 +
   1.142 +			$forum_id = array();
   1.143 +			while ($row = $db->sql_fetchrow($result))
   1.144 +			{
   1.145 +				$forum_id[] = (int) $row['forum_id'];
   1.146 +			}
   1.147 +			$db->sql_freeresult($result);
   1.148 +		}
   1.149 +		else if ($subforum_id)
   1.150 +		{
   1.151 +			$forum_id = array();
   1.152 +			foreach (get_forum_branch($subforum_id, 'children') as $row)
   1.153 +			{
   1.154 +				$forum_id[] = (int) $row['forum_id'];
   1.155 +			}
   1.156 +		}
   1.157 +
   1.158 +		// Define some common variables for every mode
   1.159 +		$error = array();
   1.160 +
   1.161 +		$permission_scope = (strpos($mode, '_global') !== false) ? 'global' : 'local';
   1.162 +
   1.163 +		// Showing introductionary page?
   1.164 +		if ($mode == 'intro')
   1.165 +		{
   1.166 +			$this->page_title = 'ACP_PERMISSIONS';
   1.167 +
   1.168 +			$template->assign_vars(array(
   1.169 +				'S_INTRO'		=> true)
   1.170 +			);
   1.171 +
   1.172 +			return;
   1.173 +		}
   1.174 +
   1.175 +		switch ($mode)
   1.176 +		{
   1.177 +			case 'setting_user_global':
   1.178 +			case 'setting_group_global':
   1.179 +				$this->permission_dropdown = array('u_', 'm_', 'a_');
   1.180 +				$permission_victim = ($mode == 'setting_user_global') ? array('user') : array('group');
   1.181 +				$this->page_title = ($mode == 'setting_user_global') ? 'ACP_USERS_PERMISSIONS' : 'ACP_GROUPS_PERMISSIONS';
   1.182 +			break;
   1.183 +
   1.184 +			case 'setting_user_local':
   1.185 +			case 'setting_group_local':
   1.186 +				$this->permission_dropdown = array('f_', 'm_');
   1.187 +				$permission_victim = ($mode == 'setting_user_local') ? array('user', 'forums') : array('group', 'forums');
   1.188 +				$this->page_title = ($mode == 'setting_user_local') ? 'ACP_USERS_FORUM_PERMISSIONS' : 'ACP_GROUPS_FORUM_PERMISSIONS';
   1.189 +			break;
   1.190 +
   1.191 +			case 'setting_admin_global':
   1.192 +			case 'setting_mod_global':
   1.193 +				$this->permission_dropdown = (strpos($mode, '_admin_') !== false) ? array('a_') : array('m_');
   1.194 +				$permission_victim = array('usergroup');
   1.195 +				$this->page_title = ($mode == 'setting_admin_global') ? 'ACP_ADMINISTRATORS' : 'ACP_GLOBAL_MODERATORS';
   1.196 +			break;
   1.197 +
   1.198 +			case 'setting_mod_local':
   1.199 +			case 'setting_forum_local':
   1.200 +				$this->permission_dropdown = ($mode == 'setting_mod_local') ? array('m_') : array('f_');
   1.201 +				$permission_victim = array('forums', 'usergroup');
   1.202 +				$this->page_title = ($mode == 'setting_mod_local') ? 'ACP_FORUM_MODERATORS' : 'ACP_FORUM_PERMISSIONS';
   1.203 +			break;
   1.204 +
   1.205 +			case 'view_admin_global':
   1.206 +			case 'view_user_global':
   1.207 +			case 'view_mod_global':
   1.208 +				$this->permission_dropdown = ($mode == 'view_admin_global') ? array('a_') : (($mode == 'view_user_global') ? array('u_') : array('m_'));
   1.209 +				$permission_victim = array('usergroup_view');
   1.210 +				$this->page_title = ($mode == 'view_admin_global') ? 'ACP_VIEW_ADMIN_PERMISSIONS' : (($mode == 'view_user_global') ? 'ACP_VIEW_USER_PERMISSIONS' : 'ACP_VIEW_GLOBAL_MOD_PERMISSIONS');
   1.211 +			break;
   1.212 +
   1.213 +			case 'view_mod_local':
   1.214 +			case 'view_forum_local':
   1.215 +				$this->permission_dropdown = ($mode == 'view_mod_local') ? array('m_') : array('f_');
   1.216 +				$permission_victim = array('forums', 'usergroup_view');
   1.217 +				$this->page_title = ($mode == 'view_mod_local') ? 'ACP_VIEW_FORUM_MOD_PERMISSIONS' : 'ACP_VIEW_FORUM_PERMISSIONS';
   1.218 +			break;
   1.219 +
   1.220 +			default:
   1.221 +				trigger_error('NO_MODE', E_USER_ERROR);
   1.222 +			break;
   1.223 +		}
   1.224 +
   1.225 +		$template->assign_vars(array(
   1.226 +			'L_TITLE'		=> $user->lang[$this->page_title],
   1.227 +			'L_EXPLAIN'		=> $user->lang[$this->page_title . '_EXPLAIN'])
   1.228 +		);
   1.229 +
   1.230 +		// Get permission type
   1.231 +		$permission_type = request_var('type', $this->permission_dropdown[0]);
   1.232 +
   1.233 +		if (!in_array($permission_type, $this->permission_dropdown))
   1.234 +		{
   1.235 +			trigger_error($user->lang['WRONG_PERMISSION_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.236 +		}
   1.237 +
   1.238 +		// Handle actions
   1.239 +		if (strpos($mode, 'setting_') === 0 && $action)
   1.240 +		{
   1.241 +			switch ($action)
   1.242 +			{
   1.243 +				case 'delete':
   1.244 +					if (confirm_box(true))
   1.245 +					{
   1.246 +						// All users/groups selected?
   1.247 +						$all_users = (isset($_POST['all_users'])) ? true : false;
   1.248 +						$all_groups = (isset($_POST['all_groups'])) ? true : false;
   1.249 +
   1.250 +						if ($all_users || $all_groups)
   1.251 +						{
   1.252 +							$items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
   1.253 +
   1.254 +							if ($all_users && sizeof($items['user_ids']))
   1.255 +							{
   1.256 +								$user_id = $items['user_ids'];
   1.257 +							}
   1.258 +							else if ($all_groups && sizeof($items['group_ids']))
   1.259 +							{
   1.260 +								$group_id = $items['group_ids'];
   1.261 +							}
   1.262 +						}
   1.263 +
   1.264 +						if (sizeof($user_id) || sizeof($group_id))
   1.265 +						{
   1.266 +							$this->remove_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id, $forum_id);
   1.267 +						}
   1.268 +						else
   1.269 +						{
   1.270 +							trigger_error($user->lang['NO_USER_GROUP_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.271 +						}
   1.272 +					}
   1.273 +					else
   1.274 +					{
   1.275 +						if (isset($_POST['cancel']))
   1.276 +						{
   1.277 +							$u_redirect = $this->u_action . '&amp;type=' . $permission_type;
   1.278 +							foreach ($forum_id as $fid)
   1.279 +							{
   1.280 +								$u_redirect .= '&amp;forum_id[]=' . $fid;
   1.281 +							}
   1.282 +							redirect($u_redirect);
   1.283 +						}
   1.284 +
   1.285 +						$s_hidden_fields = array(
   1.286 +							'i'				=> $id,
   1.287 +							'mode'			=> $mode,
   1.288 +							'action'		=> array($action => 1),
   1.289 +							'user_id'		=> $user_id,
   1.290 +							'group_id'		=> $group_id,
   1.291 +							'forum_id'		=> $forum_id,
   1.292 +							'type'			=> $permission_type,
   1.293 +						);
   1.294 +						if (isset($_POST['all_users']))
   1.295 +						{
   1.296 +							$s_hidden_fields['all_users'] = 1;
   1.297 +						}
   1.298 +						if (isset($_POST['all_groups']))
   1.299 +						{
   1.300 +							$s_hidden_fields['all_groups'] = 1;
   1.301 +						}
   1.302 +						confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($s_hidden_fields));
   1.303 +					}
   1.304 +				break;
   1.305 +
   1.306 +				case 'apply_permissions':
   1.307 +					if (!isset($_POST['setting']))
   1.308 +					{
   1.309 +						trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.310 +					}
   1.311 +					if (!check_form_key($form_name))
   1.312 +					{
   1.313 +						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
   1.314 +					}
   1.315 +
   1.316 +					$this->set_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
   1.317 +				break;
   1.318 +
   1.319 +				case 'apply_all_permissions':
   1.320 +					if (!isset($_POST['setting']))
   1.321 +					{
   1.322 +						trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.323 +					}
   1.324 +					if (!check_form_key($form_name))
   1.325 +					{
   1.326 +						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
   1.327 +					}
   1.328 +
   1.329 +					$this->set_all_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
   1.330 +				break;
   1.331 +			}
   1.332 +		}
   1.333 +
   1.334 +
   1.335 +		// Setting permissions screen
   1.336 +		$s_hidden_fields = build_hidden_fields(array(
   1.337 +			'user_id'		=> $user_id,
   1.338 +			'group_id'		=> $group_id,
   1.339 +			'forum_id'		=> $forum_id,
   1.340 +			'type'			=> $permission_type)
   1.341 +		);
   1.342 +
   1.343 +		// Go through the screens/options needed and present them in correct order
   1.344 +		foreach ($permission_victim as $victim)
   1.345 +		{
   1.346 +			switch ($victim)
   1.347 +			{
   1.348 +				case 'forum_dropdown':
   1.349 +
   1.350 +					if (sizeof($forum_id))
   1.351 +					{
   1.352 +						$this->check_existence('forum', $forum_id);
   1.353 +						continue 2;
   1.354 +					}
   1.355 +
   1.356 +					$template->assign_vars(array(
   1.357 +						'S_SELECT_FORUM'		=> true,
   1.358 +						'S_FORUM_OPTIONS'		=> make_forum_select(false, false, true, false, false))
   1.359 +					);
   1.360 +
   1.361 +				break;
   1.362 +
   1.363 +				case 'forums':
   1.364 +
   1.365 +					if (sizeof($forum_id))
   1.366 +					{
   1.367 +						$this->check_existence('forum', $forum_id);
   1.368 +						continue 2;
   1.369 +					}
   1.370 +
   1.371 +					$forum_list = make_forum_select(false, false, true, false, false, false, true);
   1.372 +
   1.373 +					// Build forum options
   1.374 +					$s_forum_options = '';
   1.375 +					foreach ($forum_list as $f_id => $f_row)
   1.376 +					{
   1.377 +						$s_forum_options .= '<option value="' . $f_id . '"' . (($f_row['selected']) ? ' selected="selected"' : '') . (($f_row['disabled']) ? ' disabled="disabled" class="disabled-option"' : '') . '>' . $f_row['padding'] . $f_row['forum_name'] . '</option>';
   1.378 +					}
   1.379 +
   1.380 +					// Build subforum options
   1.381 +					$s_subforum_options = $this->build_subforum_options($forum_list);
   1.382 +
   1.383 +					$template->assign_vars(array(
   1.384 +						'S_SELECT_FORUM'		=> true,
   1.385 +						'S_FORUM_OPTIONS'		=> $s_forum_options,
   1.386 +						'S_SUBFORUM_OPTIONS'	=> $s_subforum_options,
   1.387 +						'S_FORUM_ALL'			=> true,
   1.388 +						'S_FORUM_MULTIPLE'		=> true)
   1.389 +					);
   1.390 +
   1.391 +				break;
   1.392 +
   1.393 +				case 'user':
   1.394 +
   1.395 +					if (sizeof($user_id))
   1.396 +					{
   1.397 +						$this->check_existence('user', $user_id);
   1.398 +						continue 2;
   1.399 +					}
   1.400 +
   1.401 +					$template->assign_vars(array(
   1.402 +						'S_SELECT_USER'			=> true,
   1.403 +						'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=select_victim&amp;field=username&amp;select_single=true'),
   1.404 +					));
   1.405 +
   1.406 +				break;
   1.407 +
   1.408 +				case 'group':
   1.409 +
   1.410 +					if (sizeof($group_id))
   1.411 +					{
   1.412 +						$this->check_existence('group', $group_id);
   1.413 +						continue 2;
   1.414 +					}
   1.415 +
   1.416 +					$template->assign_vars(array(
   1.417 +						'S_SELECT_GROUP'		=> true,
   1.418 +						'S_GROUP_OPTIONS'		=> group_select_options(false, false, false), // Show all groups
   1.419 +					));
   1.420 +
   1.421 +				break;
   1.422 +
   1.423 +				case 'usergroup':
   1.424 +				case 'usergroup_view':
   1.425 +
   1.426 +					$all_users = (isset($_POST['all_users'])) ? true : false;
   1.427 +					$all_groups = (isset($_POST['all_groups'])) ? true : false;
   1.428 +
   1.429 +					if ((sizeof($user_id) && !$all_users) || (sizeof($group_id) && !$all_groups))
   1.430 +					{
   1.431 +						if (sizeof($user_id))
   1.432 +						{
   1.433 +							$this->check_existence('user', $user_id);
   1.434 +						}
   1.435 +
   1.436 +						if (sizeof($group_id))
   1.437 +						{
   1.438 +							$this->check_existence('group', $group_id);
   1.439 +						}
   1.440 +
   1.441 +						continue 2;
   1.442 +					}
   1.443 +
   1.444 +					// Now we check the users... because the "all"-selection is different here (all defined users/groups)
   1.445 +					$items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
   1.446 +
   1.447 +					if ($all_users && sizeof($items['user_ids']))
   1.448 +					{
   1.449 +						$user_id = $items['user_ids'];
   1.450 +						continue 2;
   1.451 +					}
   1.452 +
   1.453 +					if ($all_groups && sizeof($items['group_ids']))
   1.454 +					{
   1.455 +						$group_id = $items['group_ids'];
   1.456 +						continue 2;
   1.457 +					}
   1.458 +
   1.459 +					$template->assign_vars(array(
   1.460 +						'S_SELECT_USERGROUP'		=> ($victim == 'usergroup') ? true : false,
   1.461 +						'S_SELECT_USERGROUP_VIEW'	=> ($victim == 'usergroup_view') ? true : false,
   1.462 +						'S_DEFINED_USER_OPTIONS'	=> $items['user_ids_options'],
   1.463 +						'S_DEFINED_GROUP_OPTIONS'	=> $items['group_ids_options'],
   1.464 +						'S_ADD_GROUP_OPTIONS'		=> group_select_options(false, $items['group_ids'], false),	// Show all groups
   1.465 +						'U_FIND_USERNAME'			=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=add_user&amp;field=username&amp;select_single=true'),
   1.466 +					));
   1.467 +
   1.468 +				break;
   1.469 +			}
   1.470 +
   1.471 +			// The S_ALLOW_SELECT parameter below is a measure to lower memory usage.
   1.472 +			// If there are more than 5 forums selected the admin is not able to select all users/groups too.
   1.473 +			// We need to see if the number of forums can be increased or need to be decreased.
   1.474 +
   1.475 +			$template->assign_vars(array(
   1.476 +				'U_ACTION'				=> $this->u_action,
   1.477 +				'ANONYMOUS_USER_ID'		=> ANONYMOUS,
   1.478 +
   1.479 +				'S_SELECT_VICTIM'		=> true,
   1.480 +				'S_ALLOW_ALL_SELECT'	=> (sizeof($forum_id) > 5) ? false : true,
   1.481 +				'S_CAN_SELECT_USER'		=> ($auth->acl_get('a_authusers')) ? true : false,
   1.482 +				'S_CAN_SELECT_GROUP'	=> ($auth->acl_get('a_authgroups')) ? true : false,
   1.483 +				'S_HIDDEN_FIELDS'		=> $s_hidden_fields)
   1.484 +			);
   1.485 +
   1.486 +			// Let the forum names being displayed
   1.487 +			if (sizeof($forum_id))
   1.488 +			{
   1.489 +				$sql = 'SELECT forum_name
   1.490 +					FROM ' . FORUMS_TABLE . '
   1.491 +					WHERE ' . $db->sql_in_set('forum_id', $forum_id) . '
   1.492 +					ORDER BY left_id ASC';
   1.493 +				$result = $db->sql_query($sql);
   1.494 +
   1.495 +				$forum_names = array();
   1.496 +				while ($row = $db->sql_fetchrow($result))
   1.497 +				{
   1.498 +					$forum_names[] = $row['forum_name'];
   1.499 +				}
   1.500 +				$db->sql_freeresult($result);
   1.501 +
   1.502 +				$template->assign_vars(array(
   1.503 +					'S_FORUM_NAMES'		=> (sizeof($forum_names)) ? true : false,
   1.504 +					'FORUM_NAMES'		=> implode(', ', $forum_names))
   1.505 +				);
   1.506 +			}
   1.507 +
   1.508 +			return;
   1.509 +		}
   1.510 +
   1.511 +		// Do not allow forum_ids being set and no other setting defined (will bog down the server too much)
   1.512 +		if (sizeof($forum_id) && !sizeof($user_id) && !sizeof($group_id))
   1.513 +		{
   1.514 +			trigger_error($user->lang['ONLY_FORUM_DEFINED'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.515 +		}
   1.516 +
   1.517 +		$template->assign_vars(array(
   1.518 +			'S_PERMISSION_DROPDOWN'		=> (sizeof($this->permission_dropdown) > 1) ? $this->build_permission_dropdown($this->permission_dropdown, $permission_type, $permission_scope) : false,
   1.519 +			'L_PERMISSION_TYPE'			=> $user->lang['ACL_TYPE_' . strtoupper($permission_type)],
   1.520 +
   1.521 +			'U_ACTION'					=> $this->u_action,
   1.522 +			'S_HIDDEN_FIELDS'			=> $s_hidden_fields)
   1.523 +		);
   1.524 +
   1.525 +		if (strpos($mode, 'setting_') === 0)
   1.526 +		{
   1.527 +			$template->assign_vars(array(
   1.528 +				'S_SETTING_PERMISSIONS'		=> true)
   1.529 +			);
   1.530 +
   1.531 +			$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);
   1.532 +			$auth_admin->display_mask('set', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
   1.533 +		}
   1.534 +		else
   1.535 +		{
   1.536 +			$template->assign_vars(array(
   1.537 +				'S_VIEWING_PERMISSIONS'		=> true)
   1.538 +			);
   1.539 +
   1.540 +			$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);
   1.541 +			$auth_admin->display_mask('view', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
   1.542 +		}
   1.543 +	}
   1.544 +
   1.545 +	/**
   1.546 +	* Build +subforum options
   1.547 +	*/
   1.548 +	function build_subforum_options($forum_list)
   1.549 +	{
   1.550 +		global $user;
   1.551 +
   1.552 +		$s_options = '';
   1.553 +
   1.554 +		$forum_list = array_merge($forum_list);
   1.555 +
   1.556 +		foreach ($forum_list as $key => $row)
   1.557 +		{
   1.558 +			if ($row['disabled'])
   1.559 +			{
   1.560 +				continue;
   1.561 +			}
   1.562 +
   1.563 +			$s_options .= '<option value="' . $row['forum_id'] . '"' . (($row['selected']) ? ' selected="selected"' : '') . '>' . $row['padding'] . $row['forum_name'];
   1.564 +
   1.565 +			// We check if a branch is there...
   1.566 +			$branch_there = false;
   1.567 +
   1.568 +			foreach (array_slice($forum_list, $key + 1) as $temp_row)
   1.569 +			{
   1.570 +				if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
   1.571 +				{
   1.572 +					$branch_there = true;
   1.573 +					break;
   1.574 +				}
   1.575 +				continue;
   1.576 +			}
   1.577 +
   1.578 +			if ($branch_there)
   1.579 +			{
   1.580 +				$s_options .= ' [' . $user->lang['PLUS_SUBFORUMS'] . ']';
   1.581 +			}
   1.582 +
   1.583 +			$s_options .= '</option>';
   1.584 +		}
   1.585 +
   1.586 +		return $s_options;
   1.587 +	}
   1.588 +
   1.589 +	/**
   1.590 +	* Build dropdown field for changing permission types
   1.591 +	*/
   1.592 +	function build_permission_dropdown($options, $default_option, $permission_scope)
   1.593 +	{
   1.594 +		global $user, $auth;
   1.595 +
   1.596 +		$s_dropdown_options = '';
   1.597 +		foreach ($options as $setting)
   1.598 +		{
   1.599 +			if (!$auth->acl_get('a_' . str_replace('_', '', $setting) . 'auth'))
   1.600 +			{
   1.601 +				continue;
   1.602 +			}
   1.603 +
   1.604 +			$selected = ($setting == $default_option) ? ' selected="selected"' : '';
   1.605 +			$l_setting = (isset($user->lang['permission_type'][$permission_scope][$setting])) ? $user->lang['permission_type'][$permission_scope][$setting] : $user->lang['permission_type'][$setting];
   1.606 +			$s_dropdown_options .= '<option value="' . $setting . '"' . $selected . '>' . $l_setting . '</option>';
   1.607 +		}
   1.608 +
   1.609 +		return $s_dropdown_options;
   1.610 +	}
   1.611 +
   1.612 +	/**
   1.613 +	* Check if selected items exist. Remove not found ids and if empty return error.
   1.614 +	*/
   1.615 +	function check_existence($mode, &$ids)
   1.616 +	{
   1.617 +		global $db, $user;
   1.618 +
   1.619 +		switch ($mode)
   1.620 +		{
   1.621 +			case 'user':
   1.622 +				$table = USERS_TABLE;
   1.623 +				$sql_id = 'user_id';
   1.624 +			break;
   1.625 +
   1.626 +			case 'group':
   1.627 +				$table = GROUPS_TABLE;
   1.628 +				$sql_id = 'group_id';
   1.629 +			break;
   1.630 +
   1.631 +			case 'forum':
   1.632 +				$table = FORUMS_TABLE;
   1.633 +				$sql_id = 'forum_id';
   1.634 +			break;
   1.635 +		}
   1.636 +
   1.637 +		if (sizeof($ids))
   1.638 +		{
   1.639 +			$sql = "SELECT $sql_id
   1.640 +				FROM $table
   1.641 +				WHERE " . $db->sql_in_set($sql_id, $ids);
   1.642 +			$result = $db->sql_query($sql);
   1.643 +
   1.644 +			$ids = array();
   1.645 +			while ($row = $db->sql_fetchrow($result))
   1.646 +			{
   1.647 +				$ids[] = (int) $row[$sql_id];
   1.648 +			}
   1.649 +			$db->sql_freeresult($result);
   1.650 +		}
   1.651 +
   1.652 +		if (!sizeof($ids))
   1.653 +		{
   1.654 +			trigger_error($user->lang['SELECTED_' . strtoupper($mode) . '_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.655 +		}
   1.656 +	}
   1.657 +
   1.658 +	/**
   1.659 +	* Apply permissions
   1.660 +	*/
   1.661 +	function set_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
   1.662 +	{
   1.663 +		global $user, $auth;
   1.664 +
   1.665 +		$psubmit = request_var('psubmit', array(0 => array(0 => 0)));
   1.666 +
   1.667 +		// User or group to be set?
   1.668 +		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
   1.669 +
   1.670 +		// Check the permission setting again
   1.671 +		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
   1.672 +		{
   1.673 +			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.674 +		}
   1.675 +
   1.676 +		$ug_id = $forum_id = 0;
   1.677 +
   1.678 +		// We loop through the auth settings defined in our submit
   1.679 +		list($ug_id, ) = each($psubmit);
   1.680 +		list($forum_id, ) = each($psubmit[$ug_id]);
   1.681 +
   1.682 +		if (empty($_POST['setting']) || empty($_POST['setting'][$ug_id]) || empty($_POST['setting'][$ug_id][$forum_id]) || !is_array($_POST['setting'][$ug_id][$forum_id]))
   1.683 +		{
   1.684 +			trigger_error('WRONG_PERMISSION_SETTING_FORMAT', E_USER_WARNING);
   1.685 +		}
   1.686 +
   1.687 +		// We obtain and check $_POST['setting'][$ug_id][$forum_id] directly and not using request_var() because request_var()
   1.688 +		// currently does not support the amount of dimensions required. ;)
   1.689 +		//		$auth_settings = request_var('setting', array(0 => array(0 => array('' => 0))));
   1.690 +		$auth_settings = array_map('intval', $_POST['setting'][$ug_id][$forum_id]);
   1.691 +
   1.692 +		// Do we have a role we want to set?
   1.693 +		$assigned_role = (isset($_POST['role'][$ug_id][$forum_id])) ? (int) $_POST['role'][$ug_id][$forum_id] : 0;
   1.694 +
   1.695 +		// Do the admin want to set these permissions to other items too?
   1.696 +		$inherit = request_var('inherit', array(0 => array(0)));
   1.697 +
   1.698 +		$ug_id = array($ug_id);
   1.699 +		$forum_id = array($forum_id);
   1.700 +
   1.701 +		if (sizeof($inherit))
   1.702 +		{
   1.703 +			foreach ($inherit as $_ug_id => $forum_id_ary)
   1.704 +			{
   1.705 +				// Inherit users/groups?
   1.706 +				if (!in_array($_ug_id, $ug_id))
   1.707 +				{
   1.708 +					$ug_id[] = $_ug_id;
   1.709 +				}
   1.710 +
   1.711 +				// Inherit forums?
   1.712 +				$forum_id = array_merge($forum_id, array_keys($forum_id_ary));
   1.713 +			}
   1.714 +		}
   1.715 +
   1.716 +		$forum_id = array_unique($forum_id);
   1.717 +
   1.718 +		// If the auth settings differ from the assigned role, then do not set a role...
   1.719 +		if ($assigned_role)
   1.720 +		{
   1.721 +			if (!$this->check_assigned_role($assigned_role, $auth_settings))
   1.722 +			{
   1.723 +				$assigned_role = 0;
   1.724 +			}
   1.725 +		}
   1.726 +
   1.727 +		// Update the permission set...
   1.728 +		$auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_settings, $assigned_role);
   1.729 +
   1.730 +		// Do we need to recache the moderator lists?
   1.731 +		if ($permission_type == 'm_')
   1.732 +		{
   1.733 +			cache_moderators();
   1.734 +		}
   1.735 +
   1.736 +		// Remove users who are now moderators or admins from everyones foes list
   1.737 +		if ($permission_type == 'm_' || $permission_type == 'a_')
   1.738 +		{
   1.739 +			update_foes($group_id, $user_id);
   1.740 +		}
   1.741 +
   1.742 +		$this->log_action($mode, 'add', $permission_type, $ug_type, $ug_id, $forum_id);
   1.743 +
   1.744 +		trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
   1.745 +	}
   1.746 +
   1.747 +	/**
   1.748 +	* Apply all permissions
   1.749 +	*/
   1.750 +	function set_all_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
   1.751 +	{
   1.752 +		global $user, $auth;
   1.753 +
   1.754 +		// User or group to be set?
   1.755 +		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
   1.756 +
   1.757 +		// Check the permission setting again
   1.758 +		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
   1.759 +		{
   1.760 +			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.761 +		}
   1.762 +
   1.763 +		$auth_settings = (isset($_POST['setting'])) ? $_POST['setting'] : array();
   1.764 +		$auth_roles = (isset($_POST['role'])) ? $_POST['role'] : array();
   1.765 +		$ug_ids = $forum_ids = array();
   1.766 +
   1.767 +		// We need to go through the auth settings
   1.768 +		foreach ($auth_settings as $ug_id => $forum_auth_row)
   1.769 +		{
   1.770 +			$ug_id = (int) $ug_id;
   1.771 +			$ug_ids[] = $ug_id;
   1.772 +
   1.773 +			foreach ($forum_auth_row as $forum_id => $auth_options)
   1.774 +			{
   1.775 +				$forum_id = (int) $forum_id;
   1.776 +				$forum_ids[] = $forum_id;
   1.777 +
   1.778 +				// Check role...
   1.779 +				$assigned_role = (isset($auth_roles[$ug_id][$forum_id])) ? (int) $auth_roles[$ug_id][$forum_id] : 0;
   1.780 +
   1.781 +				// If the auth settings differ from the assigned role, then do not set a role...
   1.782 +				if ($assigned_role)
   1.783 +				{
   1.784 +					if (!$this->check_assigned_role($assigned_role, $auth_options))
   1.785 +					{
   1.786 +						$assigned_role = 0;
   1.787 +					}
   1.788 +				}
   1.789 +
   1.790 +				// Update the permission set...
   1.791 +				$auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_options, $assigned_role, false);
   1.792 +			}
   1.793 +		}
   1.794 +
   1.795 +		$auth_admin->acl_clear_prefetch();
   1.796 +
   1.797 +		// Do we need to recache the moderator lists?
   1.798 +		if ($permission_type == 'm_')
   1.799 +		{
   1.800 +			cache_moderators();
   1.801 +		}
   1.802 +
   1.803 +		// Remove users who are now moderators or admins from everyones foes list
   1.804 +		if ($permission_type == 'm_' || $permission_type == 'a_')
   1.805 +		{
   1.806 +			update_foes($group_id, $user_id);
   1.807 +		}
   1.808 +
   1.809 +		$this->log_action($mode, 'add', $permission_type, $ug_type, $ug_ids, $forum_ids);
   1.810 +
   1.811 +		if ($mode == 'setting_forum_local' || $mode == 'setting_mod_local')
   1.812 +		{
   1.813 +			trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action . '&amp;forum_id[]=' . implode('&amp;forum_id[]=', $forum_ids)));
   1.814 +		}
   1.815 +		else
   1.816 +		{
   1.817 +			trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
   1.818 +		}
   1.819 +	}
   1.820 +
   1.821 +	/**
   1.822 +	* Compare auth settings with auth settings from role
   1.823 +	* returns false if they differ, true if they are equal
   1.824 +	*/
   1.825 +	function check_assigned_role($role_id, &$auth_settings)
   1.826 +	{
   1.827 +		global $db;
   1.828 +
   1.829 +		$sql = 'SELECT o.auth_option, r.auth_setting
   1.830 +			FROM ' . ACL_OPTIONS_TABLE . ' o, ' . ACL_ROLES_DATA_TABLE . ' r
   1.831 +			WHERE o.auth_option_id = r.auth_option_id
   1.832 +				AND r.role_id = ' . $role_id;
   1.833 +		$result = $db->sql_query($sql);
   1.834 +
   1.835 +		$test_auth_settings = array();
   1.836 +		while ($row = $db->sql_fetchrow($result))
   1.837 +		{
   1.838 +			$test_auth_settings[$row['auth_option']] = $row['auth_setting'];
   1.839 +		}
   1.840 +		$db->sql_freeresult($result);
   1.841 +
   1.842 +		// We need to add any ACL_NO setting from auth_settings to compare correctly
   1.843 +		foreach ($auth_settings as $option => $setting)
   1.844 +		{
   1.845 +			if ($setting == ACL_NO)
   1.846 +			{
   1.847 +				$test_auth_settings[$option] = $setting;
   1.848 +			}
   1.849 +		}
   1.850 +
   1.851 +		if (sizeof(array_diff_assoc($auth_settings, $test_auth_settings)))
   1.852 +		{
   1.853 +			return false;
   1.854 +		}
   1.855 +
   1.856 +		return true;
   1.857 +	}
   1.858 +
   1.859 +	/**
   1.860 +	* Remove permissions
   1.861 +	*/
   1.862 +	function remove_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id, &$forum_id)
   1.863 +	{
   1.864 +		global $user, $db, $auth;
   1.865 +
   1.866 +		// User or group to be set?
   1.867 +		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
   1.868 +
   1.869 +		// Check the permission setting again
   1.870 +		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
   1.871 +		{
   1.872 +			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.873 +		}
   1.874 +
   1.875 +		$auth_admin->acl_delete($ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : false), $permission_type);
   1.876 +
   1.877 +		// Do we need to recache the moderator lists?
   1.878 +		if ($permission_type == 'm_')
   1.879 +		{
   1.880 +			cache_moderators();
   1.881 +		}
   1.882 +
   1.883 +		$this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : array(0 => 0)));
   1.884 +
   1.885 +		if ($mode == 'setting_forum_local' || $mode == 'setting_mod_local')
   1.886 +		{
   1.887 +			trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action . '&amp;forum_id[]=' . implode('&amp;forum_id[]=', $forum_id)));
   1.888 +		}
   1.889 +		else
   1.890 +		{
   1.891 +			trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
   1.892 +		}
   1.893 +	}
   1.894 +
   1.895 +	/**
   1.896 +	* Log permission changes
   1.897 +	*/
   1.898 +	function log_action($mode, $action, $permission_type, $ug_type, $ug_id, $forum_id)
   1.899 +	{
   1.900 +		global $db, $user;
   1.901 +
   1.902 +		if (!is_array($ug_id))
   1.903 +		{
   1.904 +			$ug_id = array($ug_id);
   1.905 +		}
   1.906 +
   1.907 +		if (!is_array($forum_id))
   1.908 +		{
   1.909 +			$forum_id = array($forum_id);
   1.910 +		}
   1.911 +
   1.912 +		// Logging ... first grab user or groupnames ...
   1.913 +		$sql = ($ug_type == 'group') ? 'SELECT group_name as name, group_type FROM ' . GROUPS_TABLE . ' WHERE ' : 'SELECT username as name FROM ' . USERS_TABLE . ' WHERE ';
   1.914 +		$sql .= $db->sql_in_set(($ug_type == 'group') ? 'group_id' : 'user_id', array_map('intval', $ug_id));
   1.915 +		$result = $db->sql_query($sql);
   1.916 +
   1.917 +		$l_ug_list = '';
   1.918 +		while ($row = $db->sql_fetchrow($result))
   1.919 +		{
   1.920 +			$l_ug_list .= (($l_ug_list != '') ? ', ' : '') . ((isset($row['group_type']) && $row['group_type'] == GROUP_SPECIAL) ? '<span class="sep">' . $user->lang['G_' . $row['name']] . '</span>' : $row['name']);
   1.921 +		}
   1.922 +		$db->sql_freeresult($result);
   1.923 +
   1.924 +		$mode = str_replace('setting_', '', $mode);
   1.925 +
   1.926 +		if ($forum_id[0] == 0)
   1.927 +		{
   1.928 +			add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_ug_list);
   1.929 +		}
   1.930 +		else
   1.931 +		{
   1.932 +			// Grab the forum details if non-zero forum_id
   1.933 +			$sql = 'SELECT forum_name
   1.934 +				FROM ' . FORUMS_TABLE . '
   1.935 +				WHERE ' . $db->sql_in_set('forum_id', $forum_id);
   1.936 +			$result = $db->sql_query($sql);
   1.937 +
   1.938 +			$l_forum_list = '';
   1.939 +			while ($row = $db->sql_fetchrow($result))
   1.940 +			{
   1.941 +				$l_forum_list .= (($l_forum_list != '') ? ', ' : '') . $row['forum_name'];
   1.942 +			}
   1.943 +			$db->sql_freeresult($result);
   1.944 +
   1.945 +			add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_forum_list, $l_ug_list);
   1.946 +		}
   1.947 +	}
   1.948 +
   1.949 +	/**
   1.950 +	* Display a complete trace tree for the selected permission to determine where settings are set/unset
   1.951 +	*/
   1.952 +	function permission_trace($user_id, $forum_id, $permission)
   1.953 +	{
   1.954 +		global $db, $template, $user, $auth;
   1.955 +
   1.956 +		if ($user_id != $user->data['user_id'])
   1.957 +		{
   1.958 +			$sql = 'SELECT user_id, username, user_permissions, user_type
   1.959 +				FROM ' . USERS_TABLE . '
   1.960 +				WHERE user_id = ' . $user_id;
   1.961 +			$result = $db->sql_query($sql);
   1.962 +			$userdata = $db->sql_fetchrow($result);
   1.963 +			$db->sql_freeresult($result);
   1.964 +		}
   1.965 +		else
   1.966 +		{
   1.967 +			$userdata = $user->data;
   1.968 +		}
   1.969 +
   1.970 +		if (!$userdata)
   1.971 +		{
   1.972 +			trigger_error('NO_USERS', E_USER_ERROR);
   1.973 +		}
   1.974 +
   1.975 +		$forum_name = false;
   1.976 +
   1.977 +		if ($forum_id)
   1.978 +		{
   1.979 +			$sql = 'SELECT forum_name
   1.980 +				FROM ' . FORUMS_TABLE . "
   1.981 +				WHERE forum_id = $forum_id";
   1.982 +			$result = $db->sql_query($sql, 3600);
   1.983 +			$forum_name = $db->sql_fetchfield('forum_name');
   1.984 +			$db->sql_freeresult($result);
   1.985 +		}
   1.986 +
   1.987 +		$back = request_var('back', 0);
   1.988 +
   1.989 +		$template->assign_vars(array(
   1.990 +			'PERMISSION'			=> $user->lang['acl_' . $permission]['lang'],
   1.991 +			'PERMISSION_USERNAME'	=> $userdata['username'],
   1.992 +			'FORUM_NAME'			=> $forum_name,
   1.993 +
   1.994 +			'S_GLOBAL_TRACE'		=> ($forum_id) ? false : true,
   1.995 +
   1.996 +			'U_BACK'				=> ($back) ? build_url(array('f', 'back')) . "&amp;f=$back" : '')
   1.997 +		);
   1.998 +
   1.999 +		$template->assign_block_vars('trace', array(
  1.1000 +			'WHO'			=> $user->lang['DEFAULT'],
  1.1001 +			'INFORMATION'	=> $user->lang['TRACE_DEFAULT'],
  1.1002 +
  1.1003 +			'S_SETTING_NO'		=> true,
  1.1004 +			'S_TOTAL_NO'		=> true)
  1.1005 +		);
  1.1006 +
  1.1007 +		$sql = 'SELECT DISTINCT g.group_name, g.group_id, g.group_type
  1.1008 +			FROM ' . GROUPS_TABLE . ' g
  1.1009 +				LEFT JOIN ' . USER_GROUP_TABLE . ' ug ON (ug.group_id = g.group_id)
  1.1010 +			WHERE ug.user_id = ' . $user_id . '
  1.1011 +				AND ug.user_pending = 0
  1.1012 +				AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
  1.1013 +			ORDER BY g.group_type DESC, g.group_id DESC';
  1.1014 +		$result = $db->sql_query($sql);
  1.1015 +
  1.1016 +		$groups = array();
  1.1017 +		while ($row = $db->sql_fetchrow($result))
  1.1018 +		{
  1.1019 +			$groups[$row['group_id']] = array(
  1.1020 +				'auth_setting'		=> ACL_NO,
  1.1021 +				'group_name'		=> ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']
  1.1022 +			);
  1.1023 +		}
  1.1024 +		$db->sql_freeresult($result);
  1.1025 +
  1.1026 +		$total = ACL_NO;
  1.1027 +		$add_key = (($forum_id) ? '_LOCAL' : '');
  1.1028 +
  1.1029 +		if (sizeof($groups))
  1.1030 +		{
  1.1031 +			// Get group auth settings
  1.1032 +			$hold_ary = $auth->acl_group_raw_data(array_keys($groups), $permission, $forum_id);
  1.1033 +
  1.1034 +			foreach ($hold_ary as $group_id => $forum_ary)
  1.1035 +			{
  1.1036 +				$groups[$group_id]['auth_setting'] = $hold_ary[$group_id][$forum_id][$permission];
  1.1037 +			}
  1.1038 +			unset($hold_ary);
  1.1039 +
  1.1040 +			foreach ($groups as $id => $row)
  1.1041 +			{
  1.1042 +				switch ($row['auth_setting'])
  1.1043 +				{
  1.1044 +					case ACL_NO:
  1.1045 +						$information = $user->lang['TRACE_GROUP_NO' . $add_key];
  1.1046 +					break;
  1.1047 +
  1.1048 +					case ACL_YES:
  1.1049 +						$information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_YES_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_YES_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_GROUP_YES_TOTAL_NO' . $add_key]);
  1.1050 +						$total = ($total == ACL_NO) ? ACL_YES : $total;
  1.1051 +					break;
  1.1052 +
  1.1053 +					case ACL_NEVER:
  1.1054 +						$information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_GROUP_NEVER_TOTAL_NO' . $add_key]);
  1.1055 +						$total = ACL_NEVER;
  1.1056 +					break;
  1.1057 +				}
  1.1058 +
  1.1059 +				$template->assign_block_vars('trace', array(
  1.1060 +					'WHO'			=> $row['group_name'],
  1.1061 +					'INFORMATION'	=> $information,
  1.1062 +
  1.1063 +					'S_SETTING_NO'		=> ($row['auth_setting'] == ACL_NO) ? true : false,
  1.1064 +					'S_SETTING_YES'		=> ($row['auth_setting'] == ACL_YES) ? true : false,
  1.1065 +					'S_SETTING_NEVER'	=> ($row['auth_setting'] == ACL_NEVER) ? true : false,
  1.1066 +					'S_TOTAL_NO'		=> ($total == ACL_NO) ? true : false,
  1.1067 +					'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
  1.1068 +					'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
  1.1069 +				);
  1.1070 +			}
  1.1071 +		}
  1.1072 +
  1.1073 +		// Get user specific permission... globally or for this forum
  1.1074 +		$hold_ary = $auth->acl_user_raw_data($user_id, $permission, $forum_id);
  1.1075 +		$auth_setting = (!sizeof($hold_ary)) ? ACL_NO : $hold_ary[$user_id][$forum_id][$permission];
  1.1076 +
  1.1077 +		switch ($auth_setting)
  1.1078 +		{
  1.1079 +			case ACL_NO:
  1.1080 +				$information = ($total == ACL_NO) ? $user->lang['TRACE_USER_NO_TOTAL_NO' . $add_key] : $user->lang['TRACE_USER_KEPT' . $add_key];
  1.1081 +				$total = ($total == ACL_NO) ? ACL_NEVER : $total;
  1.1082 +			break;
  1.1083 +
  1.1084 +			case ACL_YES:
  1.1085 +				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_YES_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_YES_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_USER_YES_TOTAL_NO' . $add_key]);
  1.1086 +				$total = ($total == ACL_NO) ? ACL_YES : $total;
  1.1087 +			break;
  1.1088 +
  1.1089 +			case ACL_NEVER:
  1.1090 +				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_NEVER_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_NEVER_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_USER_NEVER_TOTAL_NO' . $add_key]);
  1.1091 +				$total = ACL_NEVER;
  1.1092 +			break;
  1.1093 +		}
  1.1094 +
  1.1095 +		$template->assign_block_vars('trace', array(
  1.1096 +			'WHO'			=> $userdata['username'],
  1.1097 +			'INFORMATION'	=> $information,
  1.1098 +
  1.1099 +			'S_SETTING_NO'		=> ($auth_setting == ACL_NO) ? true : false,
  1.1100 +			'S_SETTING_YES'		=> ($auth_setting == ACL_YES) ? true : false,
  1.1101 +			'S_SETTING_NEVER'	=> ($auth_setting == ACL_NEVER) ? true : false,
  1.1102 +			'S_TOTAL_NO'		=> false,
  1.1103 +			'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
  1.1104 +			'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
  1.1105 +		);
  1.1106 +
  1.1107 +		if ($forum_id != 0 && isset($auth->acl_options['global'][$permission]))
  1.1108 +		{
  1.1109 +			if ($user_id != $user->data['user_id'])
  1.1110 +			{
  1.1111 +				$auth2 = new auth();
  1.1112 +				$auth2->acl($userdata);
  1.1113 +				$auth_setting = $auth2->acl_get($permission);
  1.1114 +			}
  1.1115 +			else
  1.1116 +			{
  1.1117 +				$auth_setting = $auth->acl_get($permission);
  1.1118 +			}
  1.1119 +
  1.1120 +			if ($auth_setting)
  1.1121 +			{
  1.1122 +				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_YES'] : $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_NEVER'];
  1.1123 +				$total = ACL_YES;
  1.1124 +			}
  1.1125 +			else
  1.1126 +			{
  1.1127 +				$information = $user->lang['TRACE_USER_GLOBAL_NEVER_TOTAL_KEPT'];
  1.1128 +			}
  1.1129 +
  1.1130 +			// If there is no auth information we do not need to worry the user by showing non-relevant data.
  1.1131 +			if ($auth_setting)
  1.1132 +			{
  1.1133 +				$template->assign_block_vars('trace', array(
  1.1134 +					'WHO'			=> sprintf($user->lang['TRACE_GLOBAL_SETTING'], $userdata['username']),
  1.1135 +					'INFORMATION'	=> sprintf($information, '<a href="' . $this->u_action . "&amp;u=$user_id&amp;f=0&amp;auth=$permission&amp;back=$forum_id\">", '</a>'),
  1.1136 +
  1.1137 +					'S_SETTING_NO'		=> false,
  1.1138 +					'S_SETTING_YES'		=> $auth_setting,
  1.1139 +					'S_SETTING_NEVER'	=> !$auth_setting,
  1.1140 +					'S_TOTAL_NO'		=> false,
  1.1141 +					'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
  1.1142 +					'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
  1.1143 +				);
  1.1144 +			}
  1.1145 +		}
  1.1146 +
  1.1147 +		// Take founder status into account, overwriting the default values
  1.1148 +		if ($userdata['user_type'] == USER_FOUNDER && strpos($permission, 'a_') === 0)
  1.1149 +		{
  1.1150 +			$template->assign_block_vars('trace', array(
  1.1151 +				'WHO'			=> $userdata['username'],
  1.1152 +				'INFORMATION'	=> $user->lang['TRACE_USER_FOUNDER'],
  1.1153 +
  1.1154 +				'S_SETTING_NO'		=> ($auth_setting == ACL_NO) ? true : false,
  1.1155 +				'S_SETTING_YES'		=> ($auth_setting == ACL_YES) ? true : false,
  1.1156 +				'S_SETTING_NEVER'	=> ($auth_setting == ACL_NEVER) ? true : false,
  1.1157 +				'S_TOTAL_NO'		=> false,
  1.1158 +				'S_TOTAL_YES'		=> true,
  1.1159 +				'S_TOTAL_NEVER'		=> false)
  1.1160 +			);
  1.1161 +
  1.1162 +			$total = ACL_YES;
  1.1163 +		}
  1.1164 +
  1.1165 +		// Total value...
  1.1166 +		$template->assign_vars(array(
  1.1167 +			'S_RESULT_NO'		=> ($total == ACL_NO) ? true : false,
  1.1168 +			'S_RESULT_YES'		=> ($total == ACL_YES) ? true : false,
  1.1169 +			'S_RESULT_NEVER'	=> ($total == ACL_NEVER) ? true : false,
  1.1170 +		));
  1.1171 +	}
  1.1172 +
  1.1173 +	/**
  1.1174 +	* Handles copying permissions from one forum to others
  1.1175 +	*/
  1.1176 +	function copy_forum_permissions()
  1.1177 +	{
  1.1178 +		global $auth, $cache, $template, $user;
  1.1179 +
  1.1180 +		$user->add_lang('acp/forums');
  1.1181 +
  1.1182 +		$submit = isset($_POST['submit']) ? true : false;
  1.1183 +
  1.1184 +		if ($submit)
  1.1185 +		{
  1.1186 +			$src = request_var('src_forum_id', 0);
  1.1187 +			$dest = request_var('dest_forum_ids', array(0));
  1.1188 +
  1.1189 +			if (confirm_box(true))
  1.1190 +			{
  1.1191 +				if (copy_forum_permissions($src, $dest))
  1.1192 +				{
  1.1193 +					cache_moderators();
  1.1194 +
  1.1195 +					$auth->acl_clear_prefetch();
  1.1196 +					$cache->destroy('sql', FORUMS_TABLE);
  1.1197 +
  1.1198 +					trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
  1.1199 +				}
  1.1200 +				else
  1.1201 +				{
  1.1202 +					trigger_error($user->lang['SELECTED_FORUM_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
  1.1203 +				}
  1.1204 +			}
  1.1205 +			else
  1.1206 +			{
  1.1207 +				$s_hidden_fields = array(
  1.1208 +					'submit'			=> $submit,
  1.1209 +					'src_forum_id'		=> $src,
  1.1210 +					'dest_forum_ids'	=> $dest,
  1.1211 +				);
  1.1212 +
  1.1213 +				$s_hidden_fields = build_hidden_fields($s_hidden_fields);
  1.1214 +
  1.1215 +				confirm_box(false, $user->lang['COPY_PERMISSIONS_CONFIRM'], $s_hidden_fields);
  1.1216 +			}
  1.1217 +		}
  1.1218 +
  1.1219 +		$template->assign_vars(array(
  1.1220 +			'S_FORUM_OPTIONS' => make_forum_select(false, false, false, false, false),
  1.1221 +		));
  1.1222 +	}
  1.1223 +
  1.1224 +	/**
  1.1225 +	* Get already assigned users/groups
  1.1226 +	*/
  1.1227 +	function retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type)
  1.1228 +	{
  1.1229 +		global $db, $user;
  1.1230 +
  1.1231 +		$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');
  1.1232 +
  1.1233 +		// Permission options are only able to be a permission set... therefore we will pre-fetch the possible options and also the possible roles
  1.1234 +		$option_ids = $role_ids = array();
  1.1235 +
  1.1236 +		$sql = 'SELECT auth_option_id
  1.1237 +			FROM ' . ACL_OPTIONS_TABLE . '
  1.1238 +			WHERE auth_option ' . $db->sql_like_expression($permission_type . $db->any_char);
  1.1239 +		$result = $db->sql_query($sql);
  1.1240 +
  1.1241 +		while ($row = $db->sql_fetchrow($result))
  1.1242 +		{
  1.1243 +			$option_ids[] = (int) $row['auth_option_id'];
  1.1244 +		}
  1.1245 +		$db->sql_freeresult($result);
  1.1246 +
  1.1247 +		if (sizeof($option_ids))
  1.1248 +		{
  1.1249 +			$sql = 'SELECT DISTINCT role_id
  1.1250 +				FROM ' . ACL_ROLES_DATA_TABLE . '
  1.1251 +				WHERE ' . $db->sql_in_set('auth_option_id', $option_ids);
  1.1252 +			$result = $db->sql_query($sql);
  1.1253 +
  1.1254 +			while ($row = $db->sql_fetchrow($result))
  1.1255 +			{
  1.1256 +				$role_ids[] = (int) $row['role_id'];
  1.1257 +			}
  1.1258 +			$db->sql_freeresult($result);
  1.1259 +		}
  1.1260 +
  1.1261 +		if (sizeof($option_ids) && sizeof($role_ids))
  1.1262 +		{
  1.1263 +			$sql_where = 'AND (' . $db->sql_in_set('a.auth_option_id', $option_ids) . ' OR ' . $db->sql_in_set('a.auth_role_id', $role_ids) . ')';
  1.1264 +		}
  1.1265 +		else if (sizeof($role_ids))
  1.1266 +		{
  1.1267 +			$sql_where = 'AND ' . $db->sql_in_set('a.auth_role_id', $role_ids);
  1.1268 +		}
  1.1269 +		else if (sizeof($option_ids))
  1.1270 +		{
  1.1271 +			$sql_where = 'AND ' . $db->sql_in_set('a.auth_option_id', $option_ids);
  1.1272 +		}
  1.1273 +
  1.1274 +		// Not ideal, due to the filesort, non-use of indexes, etc.
  1.1275 +		$sql = 'SELECT DISTINCT u.user_id, u.username, u.username_clean, u.user_regdate
  1.1276 +			FROM ' . USERS_TABLE . ' u, ' . ACL_USERS_TABLE . " a
  1.1277 +			WHERE u.user_id = a.user_id
  1.1278 +				$sql_forum_id
  1.1279 +				$sql_where
  1.1280 +			ORDER BY u.username_clean, u.user_regdate ASC";
  1.1281 +		$result = $db->sql_query($sql);
  1.1282 +
  1.1283 +		$s_defined_user_options = '';
  1.1284 +		$defined_user_ids = array();
  1.1285 +		while ($row = $db->sql_fetchrow($result))
  1.1286 +		{
  1.1287 +			$s_defined_user_options .= '<option value="' . $row['user_id'] . '">' . $row['username'] . '</option>';
  1.1288 +			$defined_user_ids[] = $row['user_id'];
  1.1289 +		}
  1.1290 +		$db->sql_freeresult($result);
  1.1291 +
  1.1292 +		$sql = 'SELECT DISTINCT g.group_type, g.group_name, g.group_id
  1.1293 +			FROM ' . GROUPS_TABLE . ' g, ' . ACL_GROUPS_TABLE . " a
  1.1294 +			WHERE g.group_id = a.group_id
  1.1295 +				$sql_forum_id
  1.1296 +				$sql_where
  1.1297 +			ORDER BY g.group_type DESC, g.group_name ASC";
  1.1298 +		$result = $db->sql_query($sql);
  1.1299 +
  1.1300 +		$s_defined_group_options = '';
  1.1301 +		$defined_group_ids = array();
  1.1302 +		while ($row = $db->sql_fetchrow($result))
  1.1303 +		{
  1.1304 +			$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>';
  1.1305 +			$defined_group_ids[] = $row['group_id'];
  1.1306 +		}
  1.1307 +		$db->sql_freeresult($result);
  1.1308 +
  1.1309 +		return array(
  1.1310 +			'group_ids'			=> $defined_group_ids,
  1.1311 +			'group_ids_options'	=> $s_defined_group_options,
  1.1312 +			'user_ids'			=> $defined_user_ids,
  1.1313 +			'user_ids_options'	=> $s_defined_user_options
  1.1314 +		);
  1.1315 +	}
  1.1316 +}
  1.1317 +
  1.1318 +?>
  1.1319 \ No newline at end of file