includes/acp/auth.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/auth.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1285 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     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 +* ACP Permission/Auth class
    1.24 +* @package phpBB3
    1.25 +*/
    1.26 +class auth_admin extends auth
    1.27 +{
    1.28 +	/**
    1.29 +	* Init auth settings
    1.30 +	*/
    1.31 +	function auth_admin()
    1.32 +	{
    1.33 +		global $db, $cache;
    1.34 +
    1.35 +		if (($this->acl_options = $cache->get('_acl_options')) === false)
    1.36 +		{
    1.37 +			$sql = 'SELECT auth_option_id, auth_option, is_global, is_local
    1.38 +				FROM ' . ACL_OPTIONS_TABLE . '
    1.39 +				ORDER BY auth_option_id';
    1.40 +			$result = $db->sql_query($sql);
    1.41 +
    1.42 +			$global = $local = 0;
    1.43 +			$this->acl_options = array();
    1.44 +			while ($row = $db->sql_fetchrow($result))
    1.45 +			{
    1.46 +				if ($row['is_global'])
    1.47 +				{
    1.48 +					$this->acl_options['global'][$row['auth_option']] = $global++;
    1.49 +				}
    1.50 +
    1.51 +				if ($row['is_local'])
    1.52 +				{
    1.53 +					$this->acl_options['local'][$row['auth_option']] = $local++;
    1.54 +				}
    1.55 +
    1.56 +				$this->acl_options['id'][$row['auth_option']] = (int) $row['auth_option_id'];
    1.57 +				$this->acl_options['option'][(int) $row['auth_option_id']] = $row['auth_option'];
    1.58 +			}
    1.59 +			$db->sql_freeresult($result);
    1.60 +
    1.61 +			$cache->put('_acl_options', $this->acl_options);
    1.62 +		}
    1.63 +	}
    1.64 +
    1.65 +	/**
    1.66 +	* Get permission mask
    1.67 +	* This function only supports getting permissions of one type (for example a_)
    1.68 +	*
    1.69 +	* @param set|view $mode defines the permissions we get, view gets effective permissions (checking user AND group permissions), set only gets the user or group permission set alone
    1.70 +	* @param mixed $user_id user ids to search for (a user_id or a group_id has to be specified at least)
    1.71 +	* @param mixed $group_id group ids to search for, return group related settings (a user_id or a group_id has to be specified at least)
    1.72 +	* @param mixed $forum_id forum_ids to search for. Defining a forum id also means getting local settings
    1.73 +	* @param string $auth_option the auth_option defines the permission setting to look for (a_ for example)
    1.74 +	* @param local|global $scope the scope defines the permission scope. If local, a forum_id is additionally required
    1.75 +	* @param ACL_NEVER|ACL_NO|ACL_YES $acl_fill defines the mode those permissions not set are getting filled with
    1.76 +	*/
    1.77 +	function get_mask($mode, $user_id = false, $group_id = false, $forum_id = false, $auth_option = false, $scope = false, $acl_fill = ACL_NEVER)
    1.78 +	{
    1.79 +		global $db, $user;
    1.80 +
    1.81 +		$hold_ary = array();
    1.82 +		$view_user_mask = ($mode == 'view' && $group_id === false) ? true : false;
    1.83 +
    1.84 +		if ($auth_option === false || $scope === false)
    1.85 +		{
    1.86 +			return array();
    1.87 +		}
    1.88 +
    1.89 +		$acl_user_function = ($mode == 'set') ? 'acl_user_raw_data' : 'acl_raw_data';
    1.90 +
    1.91 +		if (!$view_user_mask)
    1.92 +		{
    1.93 +			if ($forum_id !== false)
    1.94 +			{
    1.95 +				$hold_ary = ($group_id !== false) ? $this->acl_group_raw_data($group_id, $auth_option . '%', $forum_id) : $this->$acl_user_function($user_id, $auth_option . '%', $forum_id);
    1.96 +			}
    1.97 +			else
    1.98 +			{
    1.99 +				$hold_ary = ($group_id !== false) ? $this->acl_group_raw_data($group_id, $auth_option . '%', ($scope == 'global') ? 0 : false) : $this->$acl_user_function($user_id, $auth_option . '%', ($scope == 'global') ? 0 : false);
   1.100 +			}
   1.101 +		}
   1.102 +
   1.103 +		// Make sure hold_ary is filled with every setting (prevents missing forums/users/groups)
   1.104 +		$ug_id = ($group_id !== false) ? ((!is_array($group_id)) ? array($group_id) : $group_id) : ((!is_array($user_id)) ? array($user_id) : $user_id);
   1.105 +		$forum_ids = ($forum_id !== false) ? ((!is_array($forum_id)) ? array($forum_id) : $forum_id) : (($scope == 'global') ? array(0) : array());
   1.106 +
   1.107 +		// Only those options we need
   1.108 +		$compare_options = array_diff(preg_replace('/^((?!' . $auth_option . ').+)|(' . $auth_option . ')$/', '', array_keys($this->acl_options[$scope])), array(''));
   1.109 +
   1.110 +		// If forum_ids is false and the scope is local we actually want to have all forums within the array
   1.111 +		if ($scope == 'local' && !sizeof($forum_ids))
   1.112 +		{
   1.113 +			$sql = 'SELECT forum_id
   1.114 +				FROM ' . FORUMS_TABLE;
   1.115 +			$result = $db->sql_query($sql, 120);
   1.116 +
   1.117 +			while ($row = $db->sql_fetchrow($result))
   1.118 +			{
   1.119 +				$forum_ids[] = (int) $row['forum_id'];
   1.120 +			}
   1.121 +			$db->sql_freeresult($result);
   1.122 +		}
   1.123 +
   1.124 +		if ($view_user_mask)
   1.125 +		{
   1.126 +			$auth2 = null;
   1.127 +
   1.128 +			$sql = 'SELECT user_id, user_permissions, user_type
   1.129 +				FROM ' . USERS_TABLE . '
   1.130 +				WHERE ' . $db->sql_in_set('user_id', $ug_id);
   1.131 +			$result = $db->sql_query($sql);
   1.132 +
   1.133 +			while ($userdata = $db->sql_fetchrow($result))
   1.134 +			{
   1.135 +				if ($user->data['user_id'] != $userdata['user_id'])
   1.136 +				{
   1.137 +					$auth2 = new auth();
   1.138 +					$auth2->acl($userdata);
   1.139 +				}
   1.140 +				else
   1.141 +				{
   1.142 +					global $auth;
   1.143 +					$auth2 = &$auth;
   1.144 +				}
   1.145 +
   1.146 +
   1.147 +				$hold_ary[$userdata['user_id']] = array();
   1.148 +				foreach ($forum_ids as $f_id)
   1.149 +				{
   1.150 +					$hold_ary[$userdata['user_id']][$f_id] = array();
   1.151 +					foreach ($compare_options as $option)
   1.152 +					{
   1.153 +						$hold_ary[$userdata['user_id']][$f_id][$option] = $auth2->acl_get($option, $f_id);
   1.154 +					}
   1.155 +				}
   1.156 +			}
   1.157 +			$db->sql_freeresult($result);
   1.158 +
   1.159 +			unset($userdata);
   1.160 +			unset($auth2);
   1.161 +		}
   1.162 +
   1.163 +		foreach ($ug_id as $_id)
   1.164 +		{
   1.165 +			if (!isset($hold_ary[$_id]))
   1.166 +			{
   1.167 +				$hold_ary[$_id] = array();
   1.168 +			}
   1.169 +
   1.170 +			foreach ($forum_ids as $f_id)
   1.171 +			{
   1.172 +				if (!isset($hold_ary[$_id][$f_id]))
   1.173 +				{
   1.174 +					$hold_ary[$_id][$f_id] = array();
   1.175 +				}
   1.176 +			}
   1.177 +		}
   1.178 +
   1.179 +		// Now, we need to fill the gaps with $acl_fill. ;)
   1.180 +
   1.181 +		// Now switch back to keys
   1.182 +		if (sizeof($compare_options))
   1.183 +		{
   1.184 +			$compare_options = array_combine($compare_options, array_fill(1, sizeof($compare_options), $acl_fill));
   1.185 +		}
   1.186 +
   1.187 +		// Defining the user-function here to save some memory
   1.188 +		$return_acl_fill = create_function('$value', 'return ' . $acl_fill . ';');
   1.189 +
   1.190 +		// Actually fill the gaps
   1.191 +		if (sizeof($hold_ary))
   1.192 +		{
   1.193 +			foreach ($hold_ary as $ug_id => $row)
   1.194 +			{
   1.195 +				foreach ($row as $id => $options)
   1.196 +				{
   1.197 +					// Do not include the global auth_option
   1.198 +					unset($options[$auth_option]);
   1.199 +
   1.200 +					// Not a "fine" solution, but at all it's a 1-dimensional
   1.201 +					// array_diff_key function filling the resulting array values with zeros
   1.202 +					// The differences get merged into $hold_ary (all permissions having $acl_fill set)
   1.203 +					$hold_ary[$ug_id][$id] = array_merge($options,
   1.204 +
   1.205 +						array_map($return_acl_fill,
   1.206 +							array_flip(
   1.207 +								array_diff(
   1.208 +									array_keys($compare_options), array_keys($options)
   1.209 +								)
   1.210 +							)
   1.211 +						)
   1.212 +					);
   1.213 +				}
   1.214 +			}
   1.215 +		}
   1.216 +		else
   1.217 +		{
   1.218 +			$hold_ary[($group_id !== false) ? $group_id : $user_id][(int) $forum_id] = $compare_options;
   1.219 +		}
   1.220 +
   1.221 +		return $hold_ary;
   1.222 +	}
   1.223 +
   1.224 +	/**
   1.225 +	* Get permission mask for roles
   1.226 +	* This function only supports getting masks for one role
   1.227 +	*/
   1.228 +	function get_role_mask($role_id)
   1.229 +	{
   1.230 +		global $db;
   1.231 +
   1.232 +		$hold_ary = array();
   1.233 +
   1.234 +		// Get users having this role set...
   1.235 +		$sql = 'SELECT user_id, forum_id
   1.236 +			FROM ' . ACL_USERS_TABLE . '
   1.237 +			WHERE auth_role_id = ' . $role_id . '
   1.238 +			ORDER BY forum_id';
   1.239 +		$result = $db->sql_query($sql);
   1.240 +
   1.241 +		while ($row = $db->sql_fetchrow($result))
   1.242 +		{
   1.243 +			$hold_ary[$row['forum_id']]['users'][] = $row['user_id'];
   1.244 +		}
   1.245 +		$db->sql_freeresult($result);
   1.246 +
   1.247 +		// Now grab groups...
   1.248 +		$sql = 'SELECT group_id, forum_id
   1.249 +			FROM ' . ACL_GROUPS_TABLE . '
   1.250 +			WHERE auth_role_id = ' . $role_id . '
   1.251 +			ORDER BY forum_id';
   1.252 +		$result = $db->sql_query($sql);
   1.253 +
   1.254 +		while ($row = $db->sql_fetchrow($result))
   1.255 +		{
   1.256 +			$hold_ary[$row['forum_id']]['groups'][] = $row['group_id'];
   1.257 +		}
   1.258 +		$db->sql_freeresult($result);
   1.259 +
   1.260 +		return $hold_ary;
   1.261 +	}
   1.262 +
   1.263 +	/**
   1.264 +	* Display permission mask (assign to template)
   1.265 +	*/
   1.266 +	function display_mask($mode, $permission_type, &$hold_ary, $user_mode = 'user', $local = false, $group_display = true)
   1.267 +	{
   1.268 +		global $template, $user, $db, $phpbb_root_path, $phpEx;
   1.269 +
   1.270 +		// Define names for template loops, might be able to be set
   1.271 +		$tpl_pmask = 'p_mask';
   1.272 +		$tpl_fmask = 'f_mask';
   1.273 +		$tpl_category = 'category';
   1.274 +		$tpl_mask = 'mask';
   1.275 +
   1.276 +		$l_acl_type = (isset($user->lang['ACL_TYPE_' . (($local) ? 'LOCAL' : 'GLOBAL') . '_' . strtoupper($permission_type)])) ? $user->lang['ACL_TYPE_' . (($local) ? 'LOCAL' : 'GLOBAL') . '_' . strtoupper($permission_type)] : 'ACL_TYPE_' . (($local) ? 'LOCAL' : 'GLOBAL') . '_' . strtoupper($permission_type);
   1.277 +
   1.278 +		// Allow trace for viewing permissions and in user mode
   1.279 +		$show_trace = ($mode == 'view' && $user_mode == 'user') ? true : false;
   1.280 +
   1.281 +		// Get names
   1.282 +		if ($user_mode == 'user')
   1.283 +		{
   1.284 +			$sql = 'SELECT user_id as ug_id, username as ug_name
   1.285 +				FROM ' . USERS_TABLE . '
   1.286 +				WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary)) . '
   1.287 +				ORDER BY username_clean ASC';
   1.288 +		}
   1.289 +		else
   1.290 +		{
   1.291 +			$sql = 'SELECT group_id as ug_id, group_name as ug_name, group_type
   1.292 +				FROM ' . GROUPS_TABLE . '
   1.293 +				WHERE ' . $db->sql_in_set('group_id', array_keys($hold_ary)) . '
   1.294 +				ORDER BY group_type DESC, group_name ASC';
   1.295 +		}
   1.296 +		$result = $db->sql_query($sql);
   1.297 +
   1.298 +		$ug_names_ary = array();
   1.299 +		while ($row = $db->sql_fetchrow($result))
   1.300 +		{
   1.301 +			$ug_names_ary[$row['ug_id']] = ($user_mode == 'user') ? $row['ug_name'] : (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['ug_name']] : $row['ug_name']);
   1.302 +		}
   1.303 +		$db->sql_freeresult($result);
   1.304 +
   1.305 +		// Get used forums
   1.306 +		$forum_ids = array();
   1.307 +		foreach ($hold_ary as $ug_id => $row)
   1.308 +		{
   1.309 +			$forum_ids = array_merge($forum_ids, array_keys($row));
   1.310 +		}
   1.311 +		$forum_ids = array_unique($forum_ids);
   1.312 +
   1.313 +		$forum_names_ary = array();
   1.314 +		if ($local)
   1.315 +		{
   1.316 +			$forum_names_ary = make_forum_select(false, false, true, false, false, false, true);
   1.317 +
   1.318 +			// Remove the disabled ones, since we do not create an option field here...
   1.319 +			foreach ($forum_names_ary as $key => $value)
   1.320 +			{
   1.321 +				if (!$value['disabled'])
   1.322 +				{
   1.323 +					continue;
   1.324 +				}
   1.325 +				unset($forum_names_ary[$key]);
   1.326 +			}
   1.327 +		}
   1.328 +		else
   1.329 +		{
   1.330 +			$forum_names_ary[0] = $l_acl_type;
   1.331 +		}
   1.332 +
   1.333 +		// Get available roles
   1.334 +		$sql = 'SELECT *
   1.335 +			FROM ' . ACL_ROLES_TABLE . "
   1.336 +			WHERE role_type = '" . $db->sql_escape($permission_type) . "'
   1.337 +			ORDER BY role_order ASC";
   1.338 +		$result = $db->sql_query($sql);
   1.339 +
   1.340 +		$roles = array();
   1.341 +		while ($row = $db->sql_fetchrow($result))
   1.342 +		{
   1.343 +			$roles[$row['role_id']] = $row;
   1.344 +		}
   1.345 +		$db->sql_freeresult($result);
   1.346 +
   1.347 +		$cur_roles = $this->acl_role_data($user_mode, $permission_type, array_keys($hold_ary));
   1.348 +
   1.349 +		// Build js roles array (role data assignments)
   1.350 +		$s_role_js_array = '';
   1.351 +
   1.352 +		if (sizeof($roles))
   1.353 +		{
   1.354 +			$s_role_js_array = array();
   1.355 +
   1.356 +			// Make sure every role (even if empty) has its array defined
   1.357 +			foreach ($roles as $_role_id => $null)
   1.358 +			{
   1.359 +				$s_role_js_array[$_role_id] = "\n" . 'role_options[' . $_role_id . '] = new Array();' . "\n";
   1.360 +			}
   1.361 +
   1.362 +			$sql = 'SELECT r.role_id, o.auth_option, r.auth_setting
   1.363 +				FROM ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' o
   1.364 +				WHERE o.auth_option_id = r.auth_option_id
   1.365 +					AND ' . $db->sql_in_set('r.role_id', array_keys($roles));
   1.366 +			$result = $db->sql_query($sql);
   1.367 +
   1.368 +			while ($row = $db->sql_fetchrow($result))
   1.369 +			{
   1.370 +				$flag = substr($row['auth_option'], 0, strpos($row['auth_option'], '_') + 1);
   1.371 +				if ($flag == $row['auth_option'])
   1.372 +				{
   1.373 +					continue;
   1.374 +				}
   1.375 +
   1.376 +				$s_role_js_array[$row['role_id']] .= 'role_options[' . $row['role_id'] . '][\'' . addslashes($row['auth_option']) . '\'] = ' . $row['auth_setting'] . '; ';
   1.377 +			}
   1.378 +			$db->sql_freeresult($result);
   1.379 +
   1.380 +			$s_role_js_array = implode('', $s_role_js_array);
   1.381 +		}
   1.382 +
   1.383 +		$template->assign_var('S_ROLE_JS_ARRAY', $s_role_js_array);
   1.384 +		unset($s_role_js_array);
   1.385 +
   1.386 +		// Now obtain memberships
   1.387 +		$user_groups_default = $user_groups_custom = array();
   1.388 +		if ($user_mode == 'user' && $group_display)
   1.389 +		{
   1.390 +			$sql = 'SELECT group_id, group_name, group_type
   1.391 +				FROM ' . GROUPS_TABLE . '
   1.392 +				ORDER BY group_type DESC, group_name ASC';
   1.393 +			$result = $db->sql_query($sql);
   1.394 +
   1.395 +			$groups = array();
   1.396 +			while ($row = $db->sql_fetchrow($result))
   1.397 +			{
   1.398 +				$groups[$row['group_id']] = $row;
   1.399 +			}
   1.400 +			$db->sql_freeresult($result);
   1.401 +
   1.402 +			$memberships = group_memberships(false, array_keys($hold_ary), false);
   1.403 +
   1.404 +			// User is not a member of any group? Bad admin, bad bad admin...
   1.405 +			if ($memberships)
   1.406 +			{
   1.407 +				foreach ($memberships as $row)
   1.408 +				{
   1.409 +					if ($groups[$row['group_id']]['group_type'] == GROUP_SPECIAL)
   1.410 +					{
   1.411 +						$user_groups_default[$row['user_id']][] = $user->lang['G_' . $groups[$row['group_id']]['group_name']];
   1.412 +					}
   1.413 +					else
   1.414 +					{
   1.415 +						$user_groups_custom[$row['user_id']][] = $groups[$row['group_id']]['group_name'];
   1.416 +					}
   1.417 +				}
   1.418 +			}
   1.419 +			unset($memberships, $groups);
   1.420 +		}
   1.421 +
   1.422 +		// If we only have one forum id to display or being in local mode and more than one user/group to display,
   1.423 +		// we switch the complete interface to group by user/usergroup instead of grouping by forum
   1.424 +		// To achieve this, we need to switch the array a bit
   1.425 +		if (sizeof($forum_ids) == 1 || ($local && sizeof($ug_names_ary) > 1))
   1.426 +		{
   1.427 +			$hold_ary_temp = $hold_ary;
   1.428 +			$hold_ary = array();
   1.429 +			foreach ($hold_ary_temp as $ug_id => $row)
   1.430 +			{
   1.431 +				foreach ($forum_names_ary as $forum_id => $forum_row)
   1.432 +				{
   1.433 +					if (isset($row[$forum_id]))
   1.434 +					{
   1.435 +						$hold_ary[$forum_id][$ug_id] = $row[$forum_id];
   1.436 +					}
   1.437 +				}
   1.438 +			}
   1.439 +			unset($hold_ary_temp);
   1.440 +
   1.441 +			foreach ($hold_ary as $forum_id => $forum_array)
   1.442 +			{
   1.443 +				$content_array = $categories = array();
   1.444 +				$this->build_permission_array($hold_ary[$forum_id], $content_array, $categories, array_keys($ug_names_ary));
   1.445 +
   1.446 +				$template->assign_block_vars($tpl_pmask, array(
   1.447 +					'NAME'			=> ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
   1.448 +					'PADDING'		=> ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
   1.449 +
   1.450 +					'CATEGORIES'	=> implode('</th><th>', $categories),
   1.451 +
   1.452 +					'L_ACL_TYPE'	=> $l_acl_type,
   1.453 +
   1.454 +					'S_LOCAL'		=> ($local) ? true : false,
   1.455 +					'S_GLOBAL'		=> (!$local) ? true : false,
   1.456 +					'S_NUM_CATS'	=> sizeof($categories),
   1.457 +					'S_VIEW'		=> ($mode == 'view') ? true : false,
   1.458 +					'S_NUM_OBJECTS'	=> sizeof($content_array),
   1.459 +					'S_USER_MODE'	=> ($user_mode == 'user') ? true : false,
   1.460 +					'S_GROUP_MODE'	=> ($user_mode == 'group') ? true : false)
   1.461 +				);
   1.462 +
   1.463 +				@reset($content_array);
   1.464 +				while (list($ug_id, $ug_array) = each($content_array))
   1.465 +				{
   1.466 +					// Build role dropdown options
   1.467 +					$current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
   1.468 +
   1.469 +					$s_role_options = '';
   1.470 +
   1.471 +					@reset($roles);
   1.472 +					while (list($role_id, $role_row) = each($roles))
   1.473 +					{
   1.474 +						$role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
   1.475 +						$role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
   1.476 +
   1.477 +						$title = ($role_description) ? ' title="' . $role_description . '"' : '';
   1.478 +						$s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
   1.479 +					}
   1.480 +
   1.481 +					if ($s_role_options)
   1.482 +					{
   1.483 +						$s_role_options = '<option value="0"' . ((!$current_role_id) ? ' selected="selected"' : '') . ' title="' . htmlspecialchars($user->lang['NO_ROLE_ASSIGNED_EXPLAIN']) . '">' . $user->lang['NO_ROLE_ASSIGNED'] . '</option>' . $s_role_options;
   1.484 +					}
   1.485 +
   1.486 +					if (!$current_role_id && $mode != 'view')
   1.487 +					{
   1.488 +						$s_custom_permissions = false;
   1.489 +
   1.490 +						foreach ($ug_array as $key => $value)
   1.491 +						{
   1.492 +							if ($value['S_NEVER'] || $value['S_YES'])
   1.493 +							{
   1.494 +								$s_custom_permissions = true;
   1.495 +								break;
   1.496 +							}
   1.497 +						}
   1.498 +					}
   1.499 +					else
   1.500 +					{
   1.501 +						$s_custom_permissions = false;
   1.502 +					}
   1.503 +
   1.504 +					$template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
   1.505 +						'NAME'				=> $ug_names_ary[$ug_id],
   1.506 +						'S_ROLE_OPTIONS'	=> $s_role_options,
   1.507 +						'UG_ID'				=> $ug_id,
   1.508 +						'S_CUSTOM'			=> $s_custom_permissions,
   1.509 +						'FORUM_ID'			=> $forum_id)
   1.510 +					);
   1.511 +
   1.512 +					$this->assign_cat_array($ug_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
   1.513 +
   1.514 +					unset($content_array[$ug_id]);
   1.515 +				}
   1.516 +
   1.517 +				unset($hold_ary[$forum_id]);
   1.518 +			}
   1.519 +		}
   1.520 +		else
   1.521 +		{
   1.522 +			foreach ($ug_names_ary as $ug_id => $ug_name)
   1.523 +			{
   1.524 +				if (!isset($hold_ary[$ug_id]))
   1.525 +				{
   1.526 +					continue;
   1.527 +				}
   1.528 +
   1.529 +				$content_array = $categories = array();
   1.530 +				$this->build_permission_array($hold_ary[$ug_id], $content_array, $categories, array_keys($forum_names_ary));
   1.531 +
   1.532 +				$template->assign_block_vars($tpl_pmask, array(
   1.533 +					'NAME'			=> $ug_name,
   1.534 +					'CATEGORIES'	=> implode('</th><th>', $categories),
   1.535 +
   1.536 +					'USER_GROUPS_DEFAULT'	=> ($user_mode == 'user' && isset($user_groups_default[$ug_id]) && sizeof($user_groups_default[$ug_id])) ? implode(', ', $user_groups_default[$ug_id]) : '',
   1.537 +					'USER_GROUPS_CUSTOM'	=> ($user_mode == 'user' && isset($user_groups_custom[$ug_id]) && sizeof($user_groups_custom[$ug_id])) ? implode(', ', $user_groups_custom[$ug_id]) : '',
   1.538 +					'L_ACL_TYPE'			=> $l_acl_type,
   1.539 +
   1.540 +					'S_LOCAL'		=> ($local) ? true : false,
   1.541 +					'S_GLOBAL'		=> (!$local) ? true : false,
   1.542 +					'S_NUM_CATS'	=> sizeof($categories),
   1.543 +					'S_VIEW'		=> ($mode == 'view') ? true : false,
   1.544 +					'S_NUM_OBJECTS'	=> sizeof($content_array),
   1.545 +					'S_USER_MODE'	=> ($user_mode == 'user') ? true : false,
   1.546 +					'S_GROUP_MODE'	=> ($user_mode == 'group') ? true : false)
   1.547 +				);
   1.548 +
   1.549 +				@reset($content_array);
   1.550 +				while (list($forum_id, $forum_array) = each($content_array))
   1.551 +				{
   1.552 +					// Build role dropdown options
   1.553 +					$current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
   1.554 +
   1.555 +					$s_role_options = '';
   1.556 +
   1.557 +					@reset($roles);
   1.558 +					while (list($role_id, $role_row) = each($roles))
   1.559 +					{
   1.560 +						$role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
   1.561 +						$role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
   1.562 +
   1.563 +						$title = ($role_description) ? ' title="' . $role_description . '"' : '';
   1.564 +						$s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
   1.565 +					}
   1.566 +
   1.567 +					if ($s_role_options)
   1.568 +					{
   1.569 +						$s_role_options = '<option value="0"' . ((!$current_role_id) ? ' selected="selected"' : '') . ' title="' . htmlspecialchars($user->lang['NO_ROLE_ASSIGNED_EXPLAIN']) . '">' . $user->lang['NO_ROLE_ASSIGNED'] . '</option>' . $s_role_options;
   1.570 +					}
   1.571 +
   1.572 +					if (!$current_role_id && $mode != 'view')
   1.573 +					{
   1.574 +						$s_custom_permissions = false;
   1.575 +
   1.576 +						foreach ($forum_array as $key => $value)
   1.577 +						{
   1.578 +							if ($value['S_NEVER'] || $value['S_YES'])
   1.579 +							{
   1.580 +								$s_custom_permissions = true;
   1.581 +								break;
   1.582 +							}
   1.583 +						}
   1.584 +					}
   1.585 +					else
   1.586 +					{
   1.587 +						$s_custom_permissions = false;
   1.588 +					}
   1.589 +
   1.590 +					$template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
   1.591 +						'NAME'				=> ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
   1.592 +						'PADDING'			=> ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
   1.593 +						'S_ROLE_OPTIONS'	=> $s_role_options,
   1.594 +						'S_CUSTOM'			=> $s_custom_permissions,
   1.595 +						'UG_ID'				=> $ug_id,
   1.596 +						'FORUM_ID'			=> $forum_id)
   1.597 +					);
   1.598 +
   1.599 +					$this->assign_cat_array($forum_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
   1.600 +				}
   1.601 +
   1.602 +				unset($hold_ary[$ug_id], $ug_names_ary[$ug_id]);
   1.603 +			}
   1.604 +		}
   1.605 +	}
   1.606 +
   1.607 +	/**
   1.608 +	* Display permission mask for roles
   1.609 +	*/
   1.610 +	function display_role_mask(&$hold_ary)
   1.611 +	{
   1.612 +		global $db, $template, $user, $phpbb_root_path, $phpbb_admin_path, $phpEx;
   1.613 +
   1.614 +		if (!sizeof($hold_ary))
   1.615 +		{
   1.616 +			return;
   1.617 +		}
   1.618 +
   1.619 +		// Get forum names
   1.620 +		$sql = 'SELECT forum_id, forum_name
   1.621 +			FROM ' . FORUMS_TABLE . '
   1.622 +			WHERE ' . $db->sql_in_set('forum_id', array_keys($hold_ary)) . '
   1.623 +			ORDER BY left_id';
   1.624 +		$result = $db->sql_query($sql);
   1.625 +
   1.626 +		// If the role is used globally, then reflect that
   1.627 +		$forum_names = (isset($hold_ary[0])) ? array(0 => '') : array();
   1.628 +		while ($row = $db->sql_fetchrow($result))
   1.629 +		{
   1.630 +			$forum_names[$row['forum_id']] = $row['forum_name'];
   1.631 +		}
   1.632 +		$db->sql_freeresult($result);
   1.633 +
   1.634 +		foreach ($forum_names as $forum_id => $forum_name)
   1.635 +		{
   1.636 +			$auth_ary = $hold_ary[$forum_id];
   1.637 +
   1.638 +			$template->assign_block_vars('role_mask', array(
   1.639 +				'NAME'				=> ($forum_id == 0) ? $user->lang['GLOBAL_MASK'] : $forum_name,
   1.640 +				'FORUM_ID'			=> $forum_id)
   1.641 +			);
   1.642 +
   1.643 +			if (isset($auth_ary['users']) && sizeof($auth_ary['users']))
   1.644 +			{
   1.645 +				$sql = 'SELECT user_id, username
   1.646 +					FROM ' . USERS_TABLE . '
   1.647 +					WHERE ' . $db->sql_in_set('user_id', $auth_ary['users']) . '
   1.648 +					ORDER BY username_clean ASC';
   1.649 +				$result = $db->sql_query($sql);
   1.650 +
   1.651 +				while ($row = $db->sql_fetchrow($result))
   1.652 +				{
   1.653 +					$template->assign_block_vars('role_mask.users', array(
   1.654 +						'USER_ID'		=> $row['user_id'],
   1.655 +						'USERNAME'		=> $row['username'],
   1.656 +						'U_PROFILE'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u={$row['user_id']}"))
   1.657 +					);
   1.658 +				}
   1.659 +				$db->sql_freeresult($result);
   1.660 +			}
   1.661 +
   1.662 +			if (isset($auth_ary['groups']) && sizeof($auth_ary['groups']))
   1.663 +			{
   1.664 +				$sql = 'SELECT group_id, group_name, group_type
   1.665 +					FROM ' . GROUPS_TABLE . '
   1.666 +					WHERE ' . $db->sql_in_set('group_id', $auth_ary['groups']) . '
   1.667 +					ORDER BY group_type ASC, group_name';
   1.668 +				$result = $db->sql_query($sql);
   1.669 +
   1.670 +				while ($row = $db->sql_fetchrow($result))
   1.671 +				{
   1.672 +					$template->assign_block_vars('role_mask.groups', array(
   1.673 +						'GROUP_ID'		=> $row['group_id'],
   1.674 +						'GROUP_NAME'	=> ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'],
   1.675 +						'U_PROFILE'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=group&amp;g={$row['group_id']}"))
   1.676 +					);
   1.677 +				}
   1.678 +				$db->sql_freeresult($result);
   1.679 +			}
   1.680 +		}
   1.681 +	}
   1.682 +
   1.683 +	/**
   1.684 +	* NOTE: this function is not in use atm
   1.685 +	* Add a new option to the list ... $options is a hash of form ->
   1.686 +	* $options = array(
   1.687 +	*	'local'		=> array('option1', 'option2', ...),
   1.688 +	*	'global'	=> array('optionA', 'optionB', ...)
   1.689 +	* );
   1.690 +	*/
   1.691 +	function acl_add_option($options)
   1.692 +	{
   1.693 +		global $db, $cache;
   1.694 +
   1.695 +		if (!is_array($options))
   1.696 +		{
   1.697 +			return false;
   1.698 +		}
   1.699 +
   1.700 +		$cur_options = array();
   1.701 +
   1.702 +		// Determine current options
   1.703 +		$sql = 'SELECT auth_option, is_global, is_local
   1.704 +			FROM ' . ACL_OPTIONS_TABLE . '
   1.705 +			ORDER BY auth_option_id';
   1.706 +		$result = $db->sql_query($sql);
   1.707 +
   1.708 +		while ($row = $db->sql_fetchrow($result))
   1.709 +		{
   1.710 +			$cur_options[$row['auth_option']] = ($row['is_global'] && $row['is_local']) ? 'both' : (($row['is_global']) ? 'global' : 'local');
   1.711 +		}
   1.712 +		$db->sql_freeresult($result);
   1.713 +
   1.714 +		// Here we need to insert new options ... this requires discovering whether
   1.715 +		// an options is global, local or both and whether we need to add an permission
   1.716 +		// set flag (x_)
   1.717 +		$new_options = array('local' => array(), 'global' => array());
   1.718 +
   1.719 +		foreach ($options as $type => $option_ary)
   1.720 +		{
   1.721 +			$option_ary = array_unique($option_ary);
   1.722 +
   1.723 +			foreach ($option_ary as $option_value)
   1.724 +			{
   1.725 +				$new_options[$type][] = $option_value;
   1.726 +
   1.727 +				$flag = substr($option_value, 0, strpos($option_value, '_') + 1);
   1.728 +
   1.729 +				if (!in_array($flag, $new_options[$type]))
   1.730 +				{
   1.731 +					$new_options[$type][] = $flag;
   1.732 +				}
   1.733 +			}
   1.734 +		}
   1.735 +		unset($options);
   1.736 +
   1.737 +		$options = array();
   1.738 +		$options['local'] = array_diff($new_options['local'], $new_options['global']);
   1.739 +		$options['global'] = array_diff($new_options['global'], $new_options['local']);
   1.740 +		$options['both'] = array_intersect($new_options['local'], $new_options['global']);
   1.741 +
   1.742 +		// Now check which options to add/update
   1.743 +		$add_options = $update_options = array();
   1.744 +
   1.745 +		// First local ones...
   1.746 +		foreach ($options as $type => $option_ary)
   1.747 +		{
   1.748 +			foreach ($option_ary as $option)
   1.749 +			{
   1.750 +				if (!isset($cur_options[$option]))
   1.751 +				{
   1.752 +					$add_options[] = array(
   1.753 +						'auth_option'	=> (string) $option,
   1.754 +						'is_global'		=> ($type == 'global' || $type == 'both') ? 1 : 0,
   1.755 +						'is_local'		=> ($type == 'local' || $type == 'both') ? 1 : 0
   1.756 +					);
   1.757 +
   1.758 +					continue;
   1.759 +				}
   1.760 +
   1.761 +				// Else, update existing entry if it is changed...
   1.762 +				if ($type === $cur_options[$option])
   1.763 +				{
   1.764 +					continue;
   1.765 +				}
   1.766 +
   1.767 +				// New type is always both:
   1.768 +				// If is now both, we set both.
   1.769 +				// If it was global the new one is local and we need to set it to both
   1.770 +				// If it was local the new one is global and we need to set it to both
   1.771 +				$update_options[] = $option;
   1.772 +			}
   1.773 +		}
   1.774 +
   1.775 +		if (!empty($add_options))
   1.776 +		{
   1.777 +			$db->sql_multi_insert(ACL_OPTIONS_TABLE, $add_options);
   1.778 +		}
   1.779 +
   1.780 +		if (!empty($update_options))
   1.781 +		{
   1.782 +			$sql = 'UPDATE ' . ACL_OPTIONS_TABLE . '
   1.783 +				SET is_global = 1, is_local = 1
   1.784 +				WHERE ' . $db->sql_in_set('auth_option', $update_options);
   1.785 +			$db->sql_query($sql);
   1.786 +		}
   1.787 +
   1.788 +		$cache->destroy('_acl_options');
   1.789 +		$this->acl_clear_prefetch();
   1.790 +
   1.791 +		// Because we just changed the options and also purged the options cache, we instantly update/regenerate it for later calls to succeed.
   1.792 +		$this->acl_options = array();
   1.793 +		$this->auth_admin();
   1.794 +
   1.795 +		return true;
   1.796 +	}
   1.797 +
   1.798 +	/**
   1.799 +	* Set a user or group ACL record
   1.800 +	*/
   1.801 +	function acl_set($ug_type, $forum_id, $ug_id, $auth, $role_id = 0, $clear_prefetch = true)
   1.802 +	{
   1.803 +		global $db;
   1.804 +
   1.805 +		// One or more forums
   1.806 +		if (!is_array($forum_id))
   1.807 +		{
   1.808 +			$forum_id = array($forum_id);
   1.809 +		}
   1.810 +
   1.811 +		// One or more users
   1.812 +		if (!is_array($ug_id))
   1.813 +		{
   1.814 +			$ug_id = array($ug_id);
   1.815 +		}
   1.816 +
   1.817 +		$ug_id_sql = $db->sql_in_set($ug_type . '_id', array_map('intval', $ug_id));
   1.818 +		$forum_sql = $db->sql_in_set('forum_id', array_map('intval', $forum_id));
   1.819 +
   1.820 +		// Instead of updating, inserting, removing we just remove all current settings and re-set everything...
   1.821 +		$table = ($ug_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
   1.822 +		$id_field = $ug_type . '_id';
   1.823 +
   1.824 +		// Get any flags as required
   1.825 +		reset($auth);
   1.826 +		$flag = key($auth);
   1.827 +		$flag = substr($flag, 0, strpos($flag, '_') + 1);
   1.828 +
   1.829 +		// This ID (the any-flag) is set if one or more permissions are true...
   1.830 +		$any_option_id = (int) $this->acl_options['id'][$flag];
   1.831 +
   1.832 +		// Remove any-flag from auth ary
   1.833 +		if (isset($auth[$flag]))
   1.834 +		{
   1.835 +			unset($auth[$flag]);
   1.836 +		}
   1.837 +
   1.838 +		// Remove current auth options...
   1.839 +		$auth_option_ids = array((int)$any_option_id);
   1.840 +		foreach ($auth as $auth_option => $auth_setting)
   1.841 +		{
   1.842 +			$auth_option_ids[] = (int) $this->acl_options['id'][$auth_option];
   1.843 +		}
   1.844 +
   1.845 +		$sql = "DELETE FROM $table
   1.846 +			WHERE $forum_sql
   1.847 +				AND $ug_id_sql
   1.848 +				AND " . $db->sql_in_set('auth_option_id', $auth_option_ids);
   1.849 +		$db->sql_query($sql);
   1.850 +
   1.851 +		// Remove those having a role assigned... the correct type of course...
   1.852 +		$sql = 'SELECT role_id
   1.853 +			FROM ' . ACL_ROLES_TABLE . "
   1.854 +			WHERE role_type = '" . $db->sql_escape($flag) . "'";
   1.855 +		$result = $db->sql_query($sql);
   1.856 +
   1.857 +		$role_ids = array();
   1.858 +		while ($row = $db->sql_fetchrow($result))
   1.859 +		{
   1.860 +			$role_ids[] = $row['role_id'];
   1.861 +		}
   1.862 +		$db->sql_freeresult($result);
   1.863 +
   1.864 +		if (sizeof($role_ids))
   1.865 +		{
   1.866 +			$sql = "DELETE FROM $table
   1.867 +				WHERE $forum_sql
   1.868 +					AND $ug_id_sql
   1.869 +					AND auth_option_id = 0
   1.870 +					AND " . $db->sql_in_set('auth_role_id', $role_ids);
   1.871 +			$db->sql_query($sql);
   1.872 +		}
   1.873 +
   1.874 +		// Ok, include the any-flag if one or more auth options are set to yes...
   1.875 +		foreach ($auth as $auth_option => $setting)
   1.876 +		{
   1.877 +			if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
   1.878 +			{
   1.879 +				$auth[$flag] = ACL_YES;
   1.880 +			}
   1.881 +		}
   1.882 +
   1.883 +		$sql_ary = array();
   1.884 +		foreach ($forum_id as $forum)
   1.885 +		{
   1.886 +			$forum = (int) $forum;
   1.887 +
   1.888 +			if ($role_id)
   1.889 +			{
   1.890 +				foreach ($ug_id as $id)
   1.891 +				{
   1.892 +					$sql_ary[] = array(
   1.893 +						$id_field			=> (int) $id,
   1.894 +						'forum_id'			=> (int) $forum,
   1.895 +						'auth_option_id'	=> 0,
   1.896 +						'auth_setting'		=> 0,
   1.897 +						'auth_role_id'		=> (int) $role_id,
   1.898 +					);
   1.899 +				}
   1.900 +			}
   1.901 +			else
   1.902 +			{
   1.903 +				foreach ($auth as $auth_option => $setting)
   1.904 +				{
   1.905 +					$auth_option_id = (int) $this->acl_options['id'][$auth_option];
   1.906 +
   1.907 +					if ($setting != ACL_NO)
   1.908 +					{
   1.909 +						foreach ($ug_id as $id)
   1.910 +						{
   1.911 +							$sql_ary[] = array(
   1.912 +								$id_field			=> (int) $id,
   1.913 +								'forum_id'			=> (int) $forum,
   1.914 +								'auth_option_id'	=> (int) $auth_option_id,
   1.915 +								'auth_setting'		=> (int) $setting
   1.916 +							);
   1.917 +						}
   1.918 +					}
   1.919 +				}
   1.920 +			}
   1.921 +		}
   1.922 +
   1.923 +		$db->sql_multi_insert($table, $sql_ary);
   1.924 +
   1.925 +		if ($clear_prefetch)
   1.926 +		{
   1.927 +			$this->acl_clear_prefetch();
   1.928 +		}
   1.929 +	}
   1.930 +
   1.931 +	/**
   1.932 +	* Set a role-specific ACL record
   1.933 +	*/
   1.934 +	function acl_set_role($role_id, $auth)
   1.935 +	{
   1.936 +		global $db;
   1.937 +
   1.938 +		// Get any-flag as required
   1.939 +		reset($auth);
   1.940 +		$flag = key($auth);
   1.941 +		$flag = substr($flag, 0, strpos($flag, '_') + 1);
   1.942 +
   1.943 +		// Remove any-flag from auth ary
   1.944 +		if (isset($auth[$flag]))
   1.945 +		{
   1.946 +			unset($auth[$flag]);
   1.947 +		}
   1.948 +
   1.949 +		// Re-set any flag...
   1.950 +		foreach ($auth as $auth_option => $setting)
   1.951 +		{
   1.952 +			if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
   1.953 +			{
   1.954 +				$auth[$flag] = ACL_YES;
   1.955 +			}
   1.956 +		}
   1.957 +
   1.958 +		$sql_ary = array();
   1.959 +		foreach ($auth as $auth_option => $setting)
   1.960 +		{
   1.961 +			$auth_option_id = (int) $this->acl_options['id'][$auth_option];
   1.962 +
   1.963 +			if ($setting != ACL_NO)
   1.964 +			{
   1.965 +				$sql_ary[] = array(
   1.966 +					'role_id'			=> (int) $role_id,
   1.967 +					'auth_option_id'	=> (int) $auth_option_id,
   1.968 +					'auth_setting'		=> (int) $setting
   1.969 +				);
   1.970 +			}
   1.971 +		}
   1.972 +
   1.973 +		// If no data is there, we set the any-flag to ACL_NEVER...
   1.974 +		if (!sizeof($sql_ary))
   1.975 +		{
   1.976 +			$sql_ary[] = array(
   1.977 +				'role_id'			=> (int) $role_id,
   1.978 +				'auth_option_id'	=> (int) $this->acl_options['id'][$flag],
   1.979 +				'auth_setting'		=> ACL_NEVER
   1.980 +			);
   1.981 +		}
   1.982 +
   1.983 +		// Remove current auth options...
   1.984 +		$sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . '
   1.985 +			WHERE role_id = ' . $role_id;
   1.986 +		$db->sql_query($sql);
   1.987 +
   1.988 +		// Now insert the new values
   1.989 +		$db->sql_multi_insert(ACL_ROLES_DATA_TABLE, $sql_ary);
   1.990 +
   1.991 +		$this->acl_clear_prefetch();
   1.992 +	}
   1.993 +
   1.994 +	/**
   1.995 +	* Remove local permission
   1.996 +	*/
   1.997 +	function acl_delete($mode, $ug_id = false, $forum_id = false, $permission_type = false)
   1.998 +	{
   1.999 +		global $db;
  1.1000 +
  1.1001 +		if ($ug_id === false && $forum_id === false)
  1.1002 +		{
  1.1003 +			return;
  1.1004 +		}
  1.1005 +
  1.1006 +		$option_id_ary = array();
  1.1007 +		$table = ($mode == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
  1.1008 +		$id_field = $mode . '_id';
  1.1009 +
  1.1010 +		$where_sql = array();
  1.1011 +
  1.1012 +		if ($forum_id !== false)
  1.1013 +		{
  1.1014 +			$where_sql[] = (!is_array($forum_id)) ? 'forum_id = ' . (int) $forum_id : $db->sql_in_set('forum_id', array_map('intval', $forum_id));
  1.1015 +		}
  1.1016 +
  1.1017 +		if ($ug_id !== false)
  1.1018 +		{
  1.1019 +			$where_sql[] = (!is_array($ug_id)) ? $id_field . ' = ' . (int) $ug_id : $db->sql_in_set($id_field, array_map('intval', $ug_id));
  1.1020 +		}
  1.1021 +
  1.1022 +		// There seem to be auth options involved, therefore we need to go through the list and make sure we capture roles correctly
  1.1023 +		if ($permission_type !== false)
  1.1024 +		{
  1.1025 +			// Get permission type
  1.1026 +			$sql = 'SELECT auth_option, auth_option_id
  1.1027 +				FROM ' . ACL_OPTIONS_TABLE . "
  1.1028 +				WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char);
  1.1029 +			$result = $db->sql_query($sql);
  1.1030 +
  1.1031 +			$auth_id_ary = array();
  1.1032 +			while ($row = $db->sql_fetchrow($result))
  1.1033 +			{
  1.1034 +				$option_id_ary[] = $row['auth_option_id'];
  1.1035 +				$auth_id_ary[$row['auth_option']] = ACL_NO;
  1.1036 +			}
  1.1037 +			$db->sql_freeresult($result);
  1.1038 +
  1.1039 +			// First of all, lets grab the items having roles with the specified auth options assigned
  1.1040 +			$sql = "SELECT auth_role_id, $id_field, forum_id
  1.1041 +				FROM $table, " . ACL_ROLES_TABLE . " r
  1.1042 +				WHERE auth_role_id <> 0
  1.1043 +					AND auth_role_id = r.role_id
  1.1044 +					AND r.role_type = '{$permission_type}'
  1.1045 +					AND " . implode(' AND ', $where_sql) . '
  1.1046 +				ORDER BY auth_role_id';
  1.1047 +			$result = $db->sql_query($sql);
  1.1048 +
  1.1049 +			$cur_role_auth = array();
  1.1050 +			while ($row = $db->sql_fetchrow($result))
  1.1051 +			{
  1.1052 +				$cur_role_auth[$row['auth_role_id']][$row['forum_id']][] = $row[$id_field];
  1.1053 +			}
  1.1054 +			$db->sql_freeresult($result);
  1.1055 +
  1.1056 +			// Get role data for resetting data
  1.1057 +			if (sizeof($cur_role_auth))
  1.1058 +			{
  1.1059 +				$sql = 'SELECT ao.auth_option, rd.role_id, rd.auth_setting
  1.1060 +					FROM ' . ACL_OPTIONS_TABLE . ' ao, ' . ACL_ROLES_DATA_TABLE . ' rd
  1.1061 +					WHERE ao.auth_option_id = rd.auth_option_id
  1.1062 +						AND ' . $db->sql_in_set('rd.role_id', array_keys($cur_role_auth));
  1.1063 +				$result = $db->sql_query($sql);
  1.1064 +
  1.1065 +				$auth_settings = array();
  1.1066 +				while ($row = $db->sql_fetchrow($result))
  1.1067 +				{
  1.1068 +					// We need to fill all auth_options, else setting it will fail...
  1.1069 +					if (!isset($auth_settings[$row['role_id']]))
  1.1070 +					{
  1.1071 +						$auth_settings[$row['role_id']] = $auth_id_ary;
  1.1072 +					}
  1.1073 +					$auth_settings[$row['role_id']][$row['auth_option']] = $row['auth_setting'];
  1.1074 +				}
  1.1075 +				$db->sql_freeresult($result);
  1.1076 +
  1.1077 +				// Set the options
  1.1078 +				foreach ($cur_role_auth as $role_id => $auth_row)
  1.1079 +				{
  1.1080 +					foreach ($auth_row as $f_id => $ug_row)
  1.1081 +					{
  1.1082 +						$this->acl_set($mode, $f_id, $ug_row, $auth_settings[$role_id], 0, false);
  1.1083 +					}
  1.1084 +				}
  1.1085 +			}
  1.1086 +		}
  1.1087 +
  1.1088 +		// Now, normally remove permissions...
  1.1089 +		if ($permission_type !== false)
  1.1090 +		{
  1.1091 +			$where_sql[] = $db->sql_in_set('auth_option_id', array_map('intval', $option_id_ary));
  1.1092 +		}
  1.1093 +
  1.1094 +		$sql = "DELETE FROM $table
  1.1095 +			WHERE " . implode(' AND ', $where_sql);
  1.1096 +		$db->sql_query($sql);
  1.1097 +
  1.1098 +		$this->acl_clear_prefetch();
  1.1099 +	}
  1.1100 +
  1.1101 +	/**
  1.1102 +	* Assign category to template
  1.1103 +	* used by display_mask()
  1.1104 +	*/
  1.1105 +	function assign_cat_array(&$category_array, $tpl_cat, $tpl_mask, $ug_id, $forum_id, $show_trace = false, $s_view)
  1.1106 +	{
  1.1107 +		global $template, $user, $phpbb_admin_path, $phpEx;
  1.1108 +
  1.1109 +		@reset($category_array);
  1.1110 +		while (list($cat, $cat_array) = each($category_array))
  1.1111 +		{
  1.1112 +			$template->assign_block_vars($tpl_cat, array(
  1.1113 +				'S_YES'		=> ($cat_array['S_YES'] && !$cat_array['S_NEVER'] && !$cat_array['S_NO']) ? true : false,
  1.1114 +				'S_NEVER'	=> ($cat_array['S_NEVER'] && !$cat_array['S_YES'] && !$cat_array['S_NO']) ? true : false,
  1.1115 +				'S_NO'		=> ($cat_array['S_NO'] && !$cat_array['S_NEVER'] && !$cat_array['S_YES']) ? true : false,
  1.1116 +
  1.1117 +				'CAT_NAME'	=> $user->lang['permission_cat'][$cat])
  1.1118 +			);
  1.1119 +
  1.1120 +			/*	Sort permissions by name (more naturaly and user friendly than sorting by a primary key)
  1.1121 +			*	Commented out due to it's memory consumption and time needed
  1.1122 +			*
  1.1123 +			$key_array = array_intersect(array_keys($user->lang), array_map(create_function('$a', 'return "acl_" . $a;'), array_keys($cat_array['permissions'])));
  1.1124 +			$values_array = $cat_array['permissions'];
  1.1125 +
  1.1126 +			$cat_array['permissions'] = array();
  1.1127 +
  1.1128 +			foreach ($key_array as $key)
  1.1129 +			{
  1.1130 +				$key = str_replace('acl_', '', $key);
  1.1131 +				$cat_array['permissions'][$key] = $values_array[$key];
  1.1132 +			}
  1.1133 +			unset($key_array, $values_array);
  1.1134 +*/
  1.1135 +			@reset($cat_array['permissions']);
  1.1136 +			while (list($permission, $allowed) = each($cat_array['permissions']))
  1.1137 +			{
  1.1138 +				if ($s_view)
  1.1139 +				{
  1.1140 +					$template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
  1.1141 +						'S_YES'		=> ($allowed == ACL_YES) ? true : false,
  1.1142 +						'S_NEVER'	=> ($allowed == ACL_NEVER) ? true : false,
  1.1143 +
  1.1144 +						'UG_ID'			=> $ug_id,
  1.1145 +						'FORUM_ID'		=> $forum_id,
  1.1146 +						'FIELD_NAME'	=> $permission,
  1.1147 +						'S_FIELD_NAME'	=> 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
  1.1148 +
  1.1149 +						'U_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&amp;mode=trace&amp;u=$ug_id&amp;f=$forum_id&amp;auth=$permission") : '',
  1.1150 +						'UA_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
  1.1151 +
  1.1152 +						'PERMISSION'	=> $user->lang['acl_' . $permission]['lang'])
  1.1153 +					);
  1.1154 +				}
  1.1155 +				else
  1.1156 +				{
  1.1157 +					$template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
  1.1158 +						'S_YES'		=> ($allowed == ACL_YES) ? true : false,
  1.1159 +						'S_NEVER'	=> ($allowed == ACL_NEVER) ? true : false,
  1.1160 +						'S_NO'		=> ($allowed == ACL_NO) ? true : false,
  1.1161 +
  1.1162 +						'UG_ID'			=> $ug_id,
  1.1163 +						'FORUM_ID'		=> $forum_id,
  1.1164 +						'FIELD_NAME'	=> $permission,
  1.1165 +						'S_FIELD_NAME'	=> 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
  1.1166 +
  1.1167 +						'U_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&amp;mode=trace&amp;u=$ug_id&amp;f=$forum_id&amp;auth=$permission") : '',
  1.1168 +						'UA_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
  1.1169 +
  1.1170 +						'PERMISSION'	=> $user->lang['acl_' . $permission]['lang'])
  1.1171 +					);
  1.1172 +				}
  1.1173 +			}
  1.1174 +		}
  1.1175 +	}
  1.1176 +
  1.1177 +	/**
  1.1178 +	* Building content array from permission rows with explicit key ordering
  1.1179 +	* used by display_mask()
  1.1180 +	*/
  1.1181 +	function build_permission_array(&$permission_row, &$content_array, &$categories, $key_sort_array)
  1.1182 +	{
  1.1183 +		global $user;
  1.1184 +
  1.1185 +		foreach ($key_sort_array as $forum_id)
  1.1186 +		{
  1.1187 +			if (!isset($permission_row[$forum_id]))
  1.1188 +			{
  1.1189 +				continue;
  1.1190 +			}
  1.1191 +
  1.1192 +			$permissions = $permission_row[$forum_id];
  1.1193 +			ksort($permissions);
  1.1194 +
  1.1195 +			@reset($permissions);
  1.1196 +			while (list($permission, $auth_setting) = each($permissions))
  1.1197 +			{
  1.1198 +				if (!isset($user->lang['acl_' . $permission]))
  1.1199 +				{
  1.1200 +					$user->lang['acl_' . $permission] = array(
  1.1201 +						'cat'	=> 'misc',
  1.1202 +						'lang'	=> '{ acl_' . $permission . ' }'
  1.1203 +					);
  1.1204 +				}
  1.1205 +
  1.1206 +				$cat = $user->lang['acl_' . $permission]['cat'];
  1.1207 +
  1.1208 +				// Build our categories array
  1.1209 +				if (!isset($categories[$cat]))
  1.1210 +				{
  1.1211 +					$categories[$cat] = $user->lang['permission_cat'][$cat];
  1.1212 +				}
  1.1213 +
  1.1214 +				// Build our content array
  1.1215 +				if (!isset($content_array[$forum_id]))
  1.1216 +				{
  1.1217 +					$content_array[$forum_id] = array();
  1.1218 +				}
  1.1219 +
  1.1220 +				if (!isset($content_array[$forum_id][$cat]))
  1.1221 +				{
  1.1222 +					$content_array[$forum_id][$cat] = array(
  1.1223 +						'S_YES'			=> false,
  1.1224 +						'S_NEVER'		=> false,
  1.1225 +						'S_NO'			=> false,
  1.1226 +						'permissions'	=> array(),
  1.1227 +					);
  1.1228 +				}
  1.1229 +
  1.1230 +				$content_array[$forum_id][$cat]['S_YES'] |= ($auth_setting == ACL_YES) ? true : false;
  1.1231 +				$content_array[$forum_id][$cat]['S_NEVER'] |= ($auth_setting == ACL_NEVER) ? true : false;
  1.1232 +				$content_array[$forum_id][$cat]['S_NO'] |= ($auth_setting == ACL_NO) ? true : false;
  1.1233 +
  1.1234 +				$content_array[$forum_id][$cat]['permissions'][$permission] = $auth_setting;
  1.1235 +			}
  1.1236 +		}
  1.1237 +	}
  1.1238 +
  1.1239 +	/**
  1.1240 +	* Use permissions from another user. This transferes a permission set from one user to another.
  1.1241 +	* The other user is always able to revert back to his permission set.
  1.1242 +	* This function does not check for lower/higher permissions, it is possible for the user to gain
  1.1243 +	* "more" permissions by this.
  1.1244 +	* Admin permissions will not be copied.
  1.1245 +	*/
  1.1246 +	function ghost_permissions($from_user_id, $to_user_id)
  1.1247 +	{
  1.1248 +		global $db;
  1.1249 +
  1.1250 +		if ($to_user_id == ANONYMOUS)
  1.1251 +		{
  1.1252 +			return false;
  1.1253 +		}
  1.1254 +
  1.1255 +		$hold_ary = $this->acl_raw_data_single_user($from_user_id);
  1.1256 +
  1.1257 +		// Key 0 in $hold_ary are global options, all others are forum_ids
  1.1258 +
  1.1259 +		// We disallow copying admin permissions
  1.1260 +		foreach ($this->acl_options['global'] as $opt => $id)
  1.1261 +		{
  1.1262 +			if (strpos($opt, 'a_') === 0)
  1.1263 +			{
  1.1264 +				$hold_ary[0][$this->acl_options['id'][$opt]] = ACL_NEVER;
  1.1265 +			}
  1.1266 +		}
  1.1267 +
  1.1268 +		// Force a_switchperm to be allowed
  1.1269 +		$hold_ary[0][$this->acl_options['id']['a_switchperm']] = ACL_YES;
  1.1270 +
  1.1271 +		$user_permissions = $this->build_bitstring($hold_ary);
  1.1272 +
  1.1273 +		if (!$user_permissions)
  1.1274 +		{
  1.1275 +			return false;
  1.1276 +		}
  1.1277 +
  1.1278 +		$sql = 'UPDATE ' . USERS_TABLE . "
  1.1279 +			SET user_permissions = '" . $db->sql_escape($user_permissions) . "',
  1.1280 +				user_perm_from = $from_user_id
  1.1281 +			WHERE user_id = " . $to_user_id;
  1.1282 +		$db->sql_query($sql);
  1.1283 +
  1.1284 +		return true;
  1.1285 +	}
  1.1286 +}
  1.1287 +
  1.1288 +?>
  1.1289 \ No newline at end of file