Blame Identity/Webenv/phpBB/3.0.4/includes/acp/auth.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package phpBB3
ef5584
* @version $Id: auth.php 8479 2008-03-29 00:22:48Z naderman $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* ACP Permission/Auth class
ef5584
* @package phpBB3
ef5584
*/
ef5584
class auth_admin extends auth
ef5584
{
ef5584
	/**
ef5584
	* Init auth settings
ef5584
	*/
ef5584
	function auth_admin()
ef5584
	{
ef5584
		global $db, $cache;
ef5584
ef5584
		if (($this->acl_options = $cache->get('_acl_options')) === false)
ef5584
		{
ef5584
			$sql = 'SELECT auth_option_id, auth_option, is_global, is_local
ef5584
				FROM ' . ACL_OPTIONS_TABLE . '
ef5584
				ORDER BY auth_option_id';
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$global = $local = 0;
ef5584
			$this->acl_options = array();
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				if ($row['is_global'])
ef5584
				{
ef5584
					$this->acl_options['global'][$row['auth_option']] = $global++;
ef5584
				}
ef5584
ef5584
				if ($row['is_local'])
ef5584
				{
ef5584
					$this->acl_options['local'][$row['auth_option']] = $local++;
ef5584
				}
ef5584
ef5584
				$this->acl_options['id'][$row['auth_option']] = (int) $row['auth_option_id'];
ef5584
				$this->acl_options['option'][(int) $row['auth_option_id']] = $row['auth_option'];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$cache->put('_acl_options', $this->acl_options);
ef5584
		}
ef5584
	}
ef5584
	
ef5584
	/**
ef5584
	* Get permission mask
ef5584
	* This function only supports getting permissions of one type (for example a_)
ef5584
	*
ef5584
	* @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
ef5584
	* @param mixed $user_id user ids to search for (a user_id or a group_id has to be specified at least)
ef5584
	* @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)
ef5584
	* @param mixed $forum_id forum_ids to search for. Defining a forum id also means getting local settings
ef5584
	* @param string $auth_option the auth_option defines the permission setting to look for (a_ for example)
ef5584
	* @param local|global $scope the scope defines the permission scope. If local, a forum_id is additionally required
ef5584
	* @param ACL_NEVER|ACL_NO|ACL_YES $acl_fill defines the mode those permissions not set are getting filled with
ef5584
	*/
ef5584
	function get_mask($mode, $user_id = false, $group_id = false, $forum_id = false, $auth_option = false, $scope = false, $acl_fill = ACL_NEVER)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$hold_ary = array();
ef5584
		$view_user_mask = ($mode == 'view' && $group_id === false) ? true : false;
ef5584
ef5584
		if ($auth_option === false || $scope === false)
ef5584
		{
ef5584
			return array();
ef5584
		}
ef5584
ef5584
		$acl_user_function = ($mode == 'set') ? 'acl_user_raw_data' : 'acl_raw_data';
ef5584
ef5584
		if (!$view_user_mask)
ef5584
		{
ef5584
			if ($forum_id !== false)
ef5584
			{
ef5584
				$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);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$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);
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Make sure hold_ary is filled with every setting (prevents missing forums/users/groups)
ef5584
		$ug_id = ($group_id !== false) ? ((!is_array($group_id)) ? array($group_id) : $group_id) : ((!is_array($user_id)) ? array($user_id) : $user_id);
ef5584
		$forum_ids = ($forum_id !== false) ? ((!is_array($forum_id)) ? array($forum_id) : $forum_id) : (($scope == 'global') ? array(0) : array());
ef5584
ef5584
		// Only those options we need
ef5584
		$compare_options = array_diff(preg_replace('/^((?!' . $auth_option . ').+)|(' . $auth_option . ')$/', '', array_keys($this->acl_options[$scope])), array(''));
ef5584
ef5584
		// If forum_ids is false and the scope is local we actually want to have all forums within the array
ef5584
		if ($scope == 'local' && !sizeof($forum_ids))
ef5584
		{
ef5584
			$sql = 'SELECT forum_id
ef5584
				FROM ' . FORUMS_TABLE;
ef5584
			$result = $db->sql_query($sql, 120);
ef5584
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$forum_ids[] = (int) $row['forum_id'];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
ef5584
		if ($view_user_mask)
ef5584
		{
ef5584
			$auth2 = null;
ef5584
ef5584
			$sql = 'SELECT user_id, user_permissions, user_type
ef5584
				FROM ' . USERS_TABLE . '
ef5584
				WHERE ' . $db->sql_in_set('user_id', $ug_id);
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			while ($userdata = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				if ($user->data['user_id'] != $userdata['user_id'])
ef5584
				{
ef5584
					$auth2 = new auth();
ef5584
					$auth2->acl($userdata);
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					global $auth;
ef5584
					$auth2 = &$auth;
ef5584
				}
ef5584
ef5584
				
ef5584
				$hold_ary[$userdata['user_id']] = array();
ef5584
				foreach ($forum_ids as $f_id)
ef5584
				{
ef5584
					$hold_ary[$userdata['user_id']][$f_id] = array();
ef5584
					foreach ($compare_options as $option)
ef5584
					{
ef5584
						$hold_ary[$userdata['user_id']][$f_id][$option] = $auth2->acl_get($option, $f_id);
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			unset($userdata);
ef5584
			unset($auth2);
ef5584
		}
ef5584
ef5584
		foreach ($ug_id as $_id)
ef5584
		{
ef5584
			if (!isset($hold_ary[$_id]))
ef5584
			{
ef5584
				$hold_ary[$_id] = array();
ef5584
			}
ef5584
ef5584
			foreach ($forum_ids as $f_id)
ef5584
			{
ef5584
				if (!isset($hold_ary[$_id][$f_id]))
ef5584
				{
ef5584
					$hold_ary[$_id][$f_id] = array();
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Now, we need to fill the gaps with $acl_fill. ;)
ef5584
ef5584
		// Now switch back to keys
ef5584
		if (sizeof($compare_options))
ef5584
		{
ef5584
			$compare_options = array_combine($compare_options, array_fill(1, sizeof($compare_options), $acl_fill));
ef5584
		}
ef5584
ef5584
		// Defining the user-function here to save some memory
ef5584
		$return_acl_fill = create_function('$value', 'return ' . $acl_fill . ';');
ef5584
ef5584
		// Actually fill the gaps
ef5584
		if (sizeof($hold_ary))
ef5584
		{
ef5584
			foreach ($hold_ary as $ug_id => $row)
ef5584
			{
ef5584
				foreach ($row as $id => $options)
ef5584
				{
ef5584
					// Do not include the global auth_option
ef5584
					unset($options[$auth_option]);
ef5584
ef5584
					// Not a "fine" solution, but at all it's a 1-dimensional
ef5584
					// array_diff_key function filling the resulting array values with zeros
ef5584
					// The differences get merged into $hold_ary (all permissions having $acl_fill set)
ef5584
					$hold_ary[$ug_id][$id] = array_merge($options,
ef5584
ef5584
						array_map($return_acl_fill,
ef5584
							array_flip(
ef5584
								array_diff(
ef5584
									array_keys($compare_options), array_keys($options)
ef5584
								)
ef5584
							)
ef5584
						)
ef5584
					);
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$hold_ary[($group_id !== false) ? $group_id : $user_id][(int) $forum_id] = $compare_options;
ef5584
		}
ef5584
ef5584
		return $hold_ary;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get permission mask for roles
ef5584
	* This function only supports getting masks for one role
ef5584
	*/
ef5584
	function get_role_mask($role_id)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		$hold_ary = array();
ef5584
ef5584
		// Get users having this role set...
ef5584
		$sql = 'SELECT user_id, forum_id
ef5584
			FROM ' . ACL_USERS_TABLE . '
ef5584
			WHERE auth_role_id = ' . $role_id . '
ef5584
			ORDER BY forum_id';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$hold_ary[$row['forum_id']]['users'][] = $row['user_id'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// Now grab groups...
ef5584
		$sql = 'SELECT group_id, forum_id
ef5584
			FROM ' . ACL_GROUPS_TABLE . '
ef5584
			WHERE auth_role_id = ' . $role_id . '
ef5584
			ORDER BY forum_id';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$hold_ary[$row['forum_id']]['groups'][] = $row['group_id'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		return $hold_ary;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Display permission mask (assign to template)
ef5584
	*/
ef5584
	function display_mask($mode, $permission_type, &$hold_ary, $user_mode = 'user', $local = false, $group_display = true)
ef5584
	{
ef5584
		global $template, $user, $db, $phpbb_root_path, $phpEx;
ef5584
ef5584
		// Define names for template loops, might be able to be set
ef5584
		$tpl_pmask = 'p_mask';
ef5584
		$tpl_fmask = 'f_mask';
ef5584
		$tpl_category = 'category';
ef5584
		$tpl_mask = 'mask';
ef5584
ef5584
		$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);
ef5584
ef5584
		// Allow trace for viewing permissions and in user mode
ef5584
		$show_trace = ($mode == 'view' && $user_mode == 'user') ? true : false;
ef5584
ef5584
		// Get names
ef5584
		if ($user_mode == 'user')
ef5584
		{
ef5584
			$sql = 'SELECT user_id as ug_id, username as ug_name
ef5584
				FROM ' . USERS_TABLE . '
ef5584
				WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary)) . '
ef5584
				ORDER BY username_clean ASC';
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$sql = 'SELECT group_id as ug_id, group_name as ug_name, group_type
ef5584
				FROM ' . GROUPS_TABLE . '
ef5584
				WHERE ' . $db->sql_in_set('group_id', array_keys($hold_ary)) . '
ef5584
				ORDER BY group_type DESC, group_name ASC';
ef5584
		}
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$ug_names_ary = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$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']);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// Get used forums
ef5584
		$forum_ids = array();
ef5584
		foreach ($hold_ary as $ug_id => $row)
ef5584
		{
ef5584
			$forum_ids = array_merge($forum_ids, array_keys($row));
ef5584
		}
ef5584
		$forum_ids = array_unique($forum_ids);
ef5584
ef5584
		$forum_names_ary = array();
ef5584
		if ($local)
ef5584
		{
ef5584
			$forum_names_ary = make_forum_select(false, false, true, false, false, false, true);
ef5584
ef5584
			// Remove the disabled ones, since we do not create an option field here...
ef5584
			foreach ($forum_names_ary as $key => $value)
ef5584
			{
ef5584
				if (!$value['disabled'])
ef5584
				{
ef5584
					continue;
ef5584
				}
ef5584
				unset($forum_names_ary[$key]);
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$forum_names_ary[0] = $l_acl_type;
ef5584
		}
ef5584
ef5584
		// Get available roles
ef5584
		$sql = 'SELECT *
ef5584
			FROM ' . ACL_ROLES_TABLE . "
ef5584
			WHERE role_type = '" . $db->sql_escape($permission_type) . "'
ef5584
			ORDER BY role_order ASC";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$roles = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$roles[$row['role_id']] = $row;
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		$cur_roles = $this->acl_role_data($user_mode, $permission_type, array_keys($hold_ary));
ef5584
ef5584
		// Build js roles array (role data assignments)
ef5584
		$s_role_js_array = '';
ef5584
		
ef5584
		if (sizeof($roles))
ef5584
		{
ef5584
			$s_role_js_array = array();
ef5584
ef5584
			// Make sure every role (even if empty) has its array defined
ef5584
			foreach ($roles as $_role_id => $null)
ef5584
			{
ef5584
				$s_role_js_array[$_role_id] = "\n" . 'role_options[' . $_role_id . '] = new Array();' . "\n";
ef5584
			}
ef5584
ef5584
			$sql = 'SELECT r.role_id, o.auth_option, r.auth_setting
ef5584
				FROM ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' o
ef5584
				WHERE o.auth_option_id = r.auth_option_id
ef5584
					AND ' . $db->sql_in_set('r.role_id', array_keys($roles));
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$flag = substr($row['auth_option'], 0, strpos($row['auth_option'], '_') + 1);
ef5584
				if ($flag == $row['auth_option'])
ef5584
				{
ef5584
					continue;
ef5584
				}
ef5584
ef5584
				$s_role_js_array[$row['role_id']] .= 'role_options[' . $row['role_id'] . '][\'' . addslashes($row['auth_option']) . '\'] = ' . $row['auth_setting'] . '; ';
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$s_role_js_array = implode('', $s_role_js_array);
ef5584
		}
ef5584
ef5584
		$template->assign_var('S_ROLE_JS_ARRAY', $s_role_js_array);
ef5584
		unset($s_role_js_array);
ef5584
ef5584
		// Now obtain memberships
ef5584
		$user_groups_default = $user_groups_custom = array();
ef5584
		if ($user_mode == 'user' && $group_display)
ef5584
		{
ef5584
			$sql = 'SELECT group_id, group_name, group_type
ef5584
				FROM ' . GROUPS_TABLE . '
ef5584
				ORDER BY group_type DESC, group_name ASC';
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$groups = array();
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$groups[$row['group_id']] = $row;
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$memberships = group_memberships(false, array_keys($hold_ary), false);
ef5584
ef5584
			// User is not a member of any group? Bad admin, bad bad admin...
ef5584
			if ($memberships)
ef5584
			{
ef5584
				foreach ($memberships as $row)
ef5584
				{
ef5584
					if ($groups[$row['group_id']]['group_type'] == GROUP_SPECIAL)
ef5584
					{
ef5584
						$user_groups_default[$row['user_id']][] = $user->lang['G_' . $groups[$row['group_id']]['group_name']];
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$user_groups_custom[$row['user_id']][] = $groups[$row['group_id']]['group_name'];
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			unset($memberships, $groups);
ef5584
		}
ef5584
ef5584
		// If we only have one forum id to display or being in local mode and more than one user/group to display,
ef5584
		// we switch the complete interface to group by user/usergroup instead of grouping by forum
ef5584
		// To achieve this, we need to switch the array a bit
ef5584
		if (sizeof($forum_ids) == 1 || ($local && sizeof($ug_names_ary) > 1))
ef5584
		{
ef5584
			$hold_ary_temp = $hold_ary;
ef5584
			$hold_ary = array();
ef5584
			foreach ($hold_ary_temp as $ug_id => $row)
ef5584
			{
ef5584
				foreach ($forum_names_ary as $forum_id => $forum_row)
ef5584
				{
ef5584
					if (isset($row[$forum_id]))
ef5584
					{
ef5584
						$hold_ary[$forum_id][$ug_id] = $row[$forum_id];
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			unset($hold_ary_temp);
ef5584
ef5584
			foreach ($hold_ary as $forum_id => $forum_array)
ef5584
			{
ef5584
				$content_array = $categories = array();
ef5584
				$this->build_permission_array($hold_ary[$forum_id], $content_array, $categories, array_keys($ug_names_ary));
ef5584
ef5584
				$template->assign_block_vars($tpl_pmask, array(
ef5584
					'NAME'			=> ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
ef5584
					'PADDING'		=> ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
ef5584
ef5584
					'CATEGORIES'	=> implode('', $categories),
ef5584
ef5584
					'L_ACL_TYPE'	=> $l_acl_type,
ef5584
ef5584
					'S_LOCAL'		=> ($local) ? true : false,
ef5584
					'S_GLOBAL'		=> (!$local) ? true : false,
ef5584
					'S_NUM_CATS'	=> sizeof($categories),
ef5584
					'S_VIEW'		=> ($mode == 'view') ? true : false,
ef5584
					'S_NUM_OBJECTS'	=> sizeof($content_array),
ef5584
					'S_USER_MODE'	=> ($user_mode == 'user') ? true : false,
ef5584
					'S_GROUP_MODE'	=> ($user_mode == 'group') ? true : false)
ef5584
				);
ef5584
ef5584
				@reset($content_array);
ef5584
				while (list($ug_id, $ug_array) = each($content_array))
ef5584
				{
ef5584
					// Build role dropdown options
ef5584
					$current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
ef5584
ef5584
					$s_role_options = '';
ef5584
ef5584
					@reset($roles);
ef5584
					while (list($role_id, $role_row) = each($roles))
ef5584
					{
ef5584
						$role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
ef5584
						$role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
ef5584
ef5584
						$title = ($role_description) ? ' title="' . $role_description . '"' : '';
ef5584
						$s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
ef5584
					}
ef5584
ef5584
					if ($s_role_options)
ef5584
					{
ef5584
						$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;
ef5584
					}
ef5584
ef5584
					if (!$current_role_id && $mode != 'view')
ef5584
					{
ef5584
						$s_custom_permissions = false;
ef5584
ef5584
						foreach ($ug_array as $key => $value)
ef5584
						{
ef5584
							if ($value['S_NEVER'] || $value['S_YES'])
ef5584
							{
ef5584
								$s_custom_permissions = true;
ef5584
								break;
ef5584
							}
ef5584
						}
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$s_custom_permissions = false;
ef5584
					}
ef5584
ef5584
					$template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
ef5584
						'NAME'				=> $ug_names_ary[$ug_id],
ef5584
						'S_ROLE_OPTIONS'	=> $s_role_options,
ef5584
						'UG_ID'				=> $ug_id,
ef5584
						'S_CUSTOM'			=> $s_custom_permissions,
ef5584
						'FORUM_ID'			=> $forum_id)
ef5584
					);
ef5584
ef5584
					$this->assign_cat_array($ug_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
ef5584
ef5584
					unset($content_array[$ug_id]);
ef5584
				}
ef5584
ef5584
				unset($hold_ary[$forum_id]);
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			foreach ($ug_names_ary as $ug_id => $ug_name)
ef5584
			{
ef5584
				if (!isset($hold_ary[$ug_id]))
ef5584
				{
ef5584
					continue;
ef5584
				}
ef5584
ef5584
				$content_array = $categories = array();
ef5584
				$this->build_permission_array($hold_ary[$ug_id], $content_array, $categories, array_keys($forum_names_ary));
ef5584
ef5584
				$template->assign_block_vars($tpl_pmask, array(
ef5584
					'NAME'			=> $ug_name,
ef5584
					'CATEGORIES'	=> implode('', $categories),
ef5584
ef5584
					'USER_GROUPS_DEFAULT'	=> ($user_mode == 'user' && isset($user_groups_default[$ug_id]) && sizeof($user_groups_default[$ug_id])) ? implode(', ', $user_groups_default[$ug_id]) : '',
ef5584
					'USER_GROUPS_CUSTOM'	=> ($user_mode == 'user' && isset($user_groups_custom[$ug_id]) && sizeof($user_groups_custom[$ug_id])) ? implode(', ', $user_groups_custom[$ug_id]) : '',
ef5584
					'L_ACL_TYPE'			=> $l_acl_type,
ef5584
ef5584
					'S_LOCAL'		=> ($local) ? true : false,
ef5584
					'S_GLOBAL'		=> (!$local) ? true : false,
ef5584
					'S_NUM_CATS'	=> sizeof($categories),
ef5584
					'S_VIEW'		=> ($mode == 'view') ? true : false,
ef5584
					'S_NUM_OBJECTS'	=> sizeof($content_array),
ef5584
					'S_USER_MODE'	=> ($user_mode == 'user') ? true : false,
ef5584
					'S_GROUP_MODE'	=> ($user_mode == 'group') ? true : false)
ef5584
				);
ef5584
ef5584
				@reset($content_array);
ef5584
				while (list($forum_id, $forum_array) = each($content_array))
ef5584
				{
ef5584
					// Build role dropdown options
ef5584
					$current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
ef5584
ef5584
					$s_role_options = '';
ef5584
ef5584
					@reset($roles);
ef5584
					while (list($role_id, $role_row) = each($roles))
ef5584
					{
ef5584
						$role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
ef5584
						$role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
ef5584
ef5584
						$title = ($role_description) ? ' title="' . $role_description . '"' : '';
ef5584
						$s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
ef5584
					}
ef5584
ef5584
					if ($s_role_options)
ef5584
					{
ef5584
						$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;
ef5584
					}
ef5584
ef5584
					if (!$current_role_id && $mode != 'view')
ef5584
					{
ef5584
						$s_custom_permissions = false;
ef5584
ef5584
						foreach ($forum_array as $key => $value)
ef5584
						{
ef5584
							if ($value['S_NEVER'] || $value['S_YES'])
ef5584
							{
ef5584
								$s_custom_permissions = true;
ef5584
								break;
ef5584
							}
ef5584
						}
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$s_custom_permissions = false;
ef5584
					}
ef5584
ef5584
					$template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
ef5584
						'NAME'				=> ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
ef5584
						'PADDING'			=> ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
ef5584
						'S_ROLE_OPTIONS'	=> $s_role_options,
ef5584
						'S_CUSTOM'			=> $s_custom_permissions,
ef5584
						'UG_ID'				=> $ug_id,
ef5584
						'FORUM_ID'			=> $forum_id)
ef5584
					);
ef5584
ef5584
					$this->assign_cat_array($forum_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
ef5584
				}
ef5584
ef5584
				unset($hold_ary[$ug_id], $ug_names_ary[$ug_id]);
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Display permission mask for roles
ef5584
	*/
ef5584
	function display_role_mask(&$hold_ary)
ef5584
	{
ef5584
		global $db, $template, $user, $phpbb_root_path, $phpbb_admin_path, $phpEx;
ef5584
ef5584
		if (!sizeof($hold_ary))
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		// Get forum names
ef5584
		$sql = 'SELECT forum_id, forum_name
ef5584
			FROM ' . FORUMS_TABLE . '
ef5584
			WHERE ' . $db->sql_in_set('forum_id', array_keys($hold_ary)) . '
ef5584
			ORDER BY left_id';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		// If the role is used globally, then reflect that
ef5584
		$forum_names = (isset($hold_ary[0])) ? array(0 => '') : array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$forum_names[$row['forum_id']] = $row['forum_name'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		foreach ($forum_names as $forum_id => $forum_name)
ef5584
		{
ef5584
			$auth_ary = $hold_ary[$forum_id];
ef5584
ef5584
			$template->assign_block_vars('role_mask', array(
ef5584
				'NAME'				=> ($forum_id == 0) ? $user->lang['GLOBAL_MASK'] : $forum_name,
ef5584
				'FORUM_ID'			=> $forum_id)
ef5584
			);
ef5584
ef5584
			if (isset($auth_ary['users']) && sizeof($auth_ary['users']))
ef5584
			{
ef5584
				$sql = 'SELECT user_id, username
ef5584
					FROM ' . USERS_TABLE . '
ef5584
					WHERE ' . $db->sql_in_set('user_id', $auth_ary['users']) . '
ef5584
					ORDER BY username_clean ASC';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$template->assign_block_vars('role_mask.users', array(
ef5584
						'USER_ID'		=> $row['user_id'],
ef5584
						'USERNAME'		=> $row['username'],
ef5584
						'U_PROFILE'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&u={$row['user_id']}"))
ef5584
					);
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
			}
ef5584
ef5584
			if (isset($auth_ary['groups']) && sizeof($auth_ary['groups']))
ef5584
			{
ef5584
				$sql = 'SELECT group_id, group_name, group_type
ef5584
					FROM ' . GROUPS_TABLE . '
ef5584
					WHERE ' . $db->sql_in_set('group_id', $auth_ary['groups']) . '
ef5584
					ORDER BY group_type ASC, group_name';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$template->assign_block_vars('role_mask.groups', array(
ef5584
						'GROUP_ID'		=> $row['group_id'],
ef5584
						'GROUP_NAME'	=> ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'],
ef5584
						'U_PROFILE'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=group&g={$row['group_id']}"))
ef5584
					);
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* NOTE: this function is not in use atm
ef5584
	* Add a new option to the list ... $options is a hash of form ->
ef5584
	* $options = array(
ef5584
	*	'local'		=> array('option1', 'option2', ...),
ef5584
	*	'global'	=> array('optionA', 'optionB', ...)
ef5584
	* );
ef5584
	*/
ef5584
	function acl_add_option($options)
ef5584
	{
ef5584
		global $db, $cache;
ef5584
ef5584
		if (!is_array($options))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$cur_options = array();
ef5584
ef5584
		$sql = 'SELECT auth_option, is_global, is_local
ef5584
			FROM ' . ACL_OPTIONS_TABLE . '
ef5584
			ORDER BY auth_option_id';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			if ($row['is_global'])
ef5584
			{
ef5584
				$cur_options['global'][] = $row['auth_option'];
ef5584
			}
ef5584
ef5584
			if ($row['is_local'])
ef5584
			{
ef5584
				$cur_options['local'][] = $row['auth_option'];
ef5584
			}
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// Here we need to insert new options ... this requires discovering whether
ef5584
		// an options is global, local or both and whether we need to add an permission
ef5584
		// set flag (x_)
ef5584
		$new_options = array('local' => array(), 'global' => array());
ef5584
ef5584
		foreach ($options as $type => $option_ary)
ef5584
		{
ef5584
			$option_ary = array_unique($option_ary);
ef5584
ef5584
			foreach ($option_ary as $option_value)
ef5584
			{
ef5584
				if (!in_array($option_value, $cur_options[$type]))
ef5584
				{
ef5584
					$new_options[$type][] = $option_value;
ef5584
				}
ef5584
ef5584
				$flag = substr($option_value, 0, strpos($option_value, '_') + 1);
ef5584
ef5584
				if (!in_array($flag, $cur_options[$type]) && !in_array($flag, $new_options[$type]))
ef5584
				{
ef5584
					$new_options[$type][] = $flag;
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
		unset($options);
ef5584
ef5584
		$options = array();
ef5584
		$options['local'] = array_diff($new_options['local'], $new_options['global']);
ef5584
		$options['global'] = array_diff($new_options['global'], $new_options['local']);
ef5584
		$options['local_global'] = array_intersect($new_options['local'], $new_options['global']);
ef5584
ef5584
		$sql_ary = array();
ef5584
ef5584
		foreach ($options as $type => $option_ary)
ef5584
		{
ef5584
			foreach ($option_ary as $option)
ef5584
			{
ef5584
				$sql_ary[] = array(
ef5584
					'auth_option'	=> (string) $option,
ef5584
					'is_global'		=> ($type == 'global' || $type == 'local_global') ? 1 : 0,
ef5584
					'is_local'		=> ($type == 'local' || $type == 'local_global') ? 1 : 0
ef5584
				);
ef5584
			}
ef5584
		}
ef5584
ef5584
		$db->sql_multi_insert(ACL_OPTIONS_TABLE, $sql_ary);
ef5584
ef5584
		$cache->destroy('_acl_options');
ef5584
		$this->acl_clear_prefetch();
ef5584
ef5584
		// Because we just changed the options and also purged the options cache, we instantly update/regenerate it for later calls to succeed.
ef5584
		$this->acl_options = array();
ef5584
		$this->auth_admin();
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Set a user or group ACL record
ef5584
	*/
ef5584
	function acl_set($ug_type, $forum_id, $ug_id, $auth, $role_id = 0, $clear_prefetch = true)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		// One or more forums
ef5584
		if (!is_array($forum_id))
ef5584
		{
ef5584
			$forum_id = array($forum_id);
ef5584
		}
ef5584
ef5584
		// One or more users
ef5584
		if (!is_array($ug_id))
ef5584
		{
ef5584
			$ug_id = array($ug_id);
ef5584
		}
ef5584
ef5584
		$ug_id_sql = $db->sql_in_set($ug_type . '_id', array_map('intval', $ug_id));
ef5584
		$forum_sql = $db->sql_in_set('forum_id', array_map('intval', $forum_id));
ef5584
ef5584
		// Instead of updating, inserting, removing we just remove all current settings and re-set everything...
ef5584
		$table = ($ug_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
ef5584
		$id_field = $ug_type . '_id';
ef5584
ef5584
		// Get any flags as required
ef5584
		reset($auth);
ef5584
		$flag = key($auth);
ef5584
		$flag = substr($flag, 0, strpos($flag, '_') + 1);
ef5584
		
ef5584
		// This ID (the any-flag) is set if one or more permissions are true...
ef5584
		$any_option_id = (int) $this->acl_options['id'][$flag];
ef5584
ef5584
		// Remove any-flag from auth ary
ef5584
		if (isset($auth[$flag]))
ef5584
		{
ef5584
			unset($auth[$flag]);
ef5584
		}
ef5584
ef5584
		// Remove current auth options...
ef5584
		$auth_option_ids = array((int)$any_option_id);
ef5584
		foreach ($auth as $auth_option => $auth_setting)
ef5584
		{
ef5584
			$auth_option_ids[] = (int) $this->acl_options['id'][$auth_option];
ef5584
		}
ef5584
ef5584
		$sql = "DELETE FROM $table
ef5584
			WHERE $forum_sql
ef5584
				AND $ug_id_sql
ef5584
				AND " . $db->sql_in_set('auth_option_id', $auth_option_ids);
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		// Remove those having a role assigned... the correct type of course...
ef5584
		$sql = 'SELECT role_id
ef5584
			FROM ' . ACL_ROLES_TABLE . "
ef5584
			WHERE role_type = '" . $db->sql_escape($flag) . "'";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$role_ids = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$role_ids[] = $row['role_id'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (sizeof($role_ids))
ef5584
		{
ef5584
			$sql = "DELETE FROM $table
ef5584
				WHERE $forum_sql
ef5584
					AND $ug_id_sql
ef5584
					AND auth_option_id = 0
ef5584
					AND " . $db->sql_in_set('auth_role_id', $role_ids);
ef5584
			$db->sql_query($sql);
ef5584
		}
ef5584
ef5584
		// Ok, include the any-flag if one or more auth options are set to yes...
ef5584
		foreach ($auth as $auth_option => $setting)
ef5584
		{
ef5584
			if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
ef5584
			{
ef5584
				$auth[$flag] = ACL_YES;
ef5584
			}
ef5584
		}
ef5584
ef5584
		$sql_ary = array();
ef5584
		foreach ($forum_id as $forum)
ef5584
		{
ef5584
			$forum = (int) $forum;
ef5584
ef5584
			if ($role_id)
ef5584
			{
ef5584
				foreach ($ug_id as $id)
ef5584
				{
ef5584
					$sql_ary[] = array(
ef5584
						$id_field			=> (int) $id,
ef5584
						'forum_id'			=> (int) $forum,
ef5584
						'auth_option_id'	=> 0,
ef5584
						'auth_setting'		=> 0,
ef5584
						'auth_role_id'		=> (int) $role_id,
ef5584
					);
ef5584
				}
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				foreach ($auth as $auth_option => $setting)
ef5584
				{
ef5584
					$auth_option_id = (int) $this->acl_options['id'][$auth_option];
ef5584
ef5584
					if ($setting != ACL_NO)
ef5584
					{
ef5584
						foreach ($ug_id as $id)
ef5584
						{
ef5584
							$sql_ary[] = array(
ef5584
								$id_field			=> (int) $id,
ef5584
								'forum_id'			=> (int) $forum,
ef5584
								'auth_option_id'	=> (int) $auth_option_id,
ef5584
								'auth_setting'		=> (int) $setting
ef5584
							);
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		$db->sql_multi_insert($table, $sql_ary);
ef5584
ef5584
		if ($clear_prefetch)
ef5584
		{
ef5584
			$this->acl_clear_prefetch();
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Set a role-specific ACL record
ef5584
	*/
ef5584
	function acl_set_role($role_id, $auth)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		// Get any-flag as required
ef5584
		reset($auth);
ef5584
		$flag = key($auth);
ef5584
		$flag = substr($flag, 0, strpos($flag, '_') + 1);
ef5584
		
ef5584
		// Remove any-flag from auth ary
ef5584
		if (isset($auth[$flag]))
ef5584
		{
ef5584
			unset($auth[$flag]);
ef5584
		}
ef5584
ef5584
		// Re-set any flag...
ef5584
		foreach ($auth as $auth_option => $setting)
ef5584
		{
ef5584
			if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
ef5584
			{
ef5584
				$auth[$flag] = ACL_YES;
ef5584
			}
ef5584
		}
ef5584
ef5584
		$sql_ary = array();
ef5584
		foreach ($auth as $auth_option => $setting)
ef5584
		{
ef5584
			$auth_option_id = (int) $this->acl_options['id'][$auth_option];
ef5584
ef5584
			if ($setting != ACL_NO)
ef5584
			{
ef5584
				$sql_ary[] = array(
ef5584
					'role_id'			=> (int) $role_id,
ef5584
					'auth_option_id'	=> (int) $auth_option_id,
ef5584
					'auth_setting'		=> (int) $setting
ef5584
				);
ef5584
			}
ef5584
		}
ef5584
ef5584
		// If no data is there, we set the any-flag to ACL_NEVER...
ef5584
		if (!sizeof($sql_ary))
ef5584
		{
ef5584
			$sql_ary[] = array(
ef5584
				'role_id'			=> (int) $role_id,
ef5584
				'auth_option_id'	=> (int) $this->acl_options['id'][$flag],
ef5584
				'auth_setting'		=> ACL_NEVER
ef5584
			);
ef5584
		}
ef5584
ef5584
		// Remove current auth options...
ef5584
		$sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . '
ef5584
			WHERE role_id = ' . $role_id;
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		// Now insert the new values
ef5584
		$db->sql_multi_insert(ACL_ROLES_DATA_TABLE, $sql_ary);
ef5584
ef5584
		$this->acl_clear_prefetch();
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Remove local permission
ef5584
	*/
ef5584
	function acl_delete($mode, $ug_id = false, $forum_id = false, $permission_type = false)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		if ($ug_id === false && $forum_id === false)
ef5584
		{
ef5584
			return;
ef5584
		}
ef5584
ef5584
		$option_id_ary = array();
ef5584
		$table = ($mode == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
ef5584
		$id_field = $mode . '_id';
ef5584
ef5584
		$where_sql = array();
ef5584
ef5584
		if ($forum_id !== false)
ef5584
		{
ef5584
			$where_sql[] = (!is_array($forum_id)) ? 'forum_id = ' . (int) $forum_id : $db->sql_in_set('forum_id', array_map('intval', $forum_id));
ef5584
		}
ef5584
ef5584
		if ($ug_id !== false)
ef5584
		{
ef5584
			$where_sql[] = (!is_array($ug_id)) ? $id_field . ' = ' . (int) $ug_id : $db->sql_in_set($id_field, array_map('intval', $ug_id));
ef5584
		}
ef5584
ef5584
		// There seem to be auth options involved, therefore we need to go through the list and make sure we capture roles correctly
ef5584
		if ($permission_type !== false)
ef5584
		{
ef5584
			// Get permission type
ef5584
			$sql = 'SELECT auth_option, auth_option_id
ef5584
				FROM ' . ACL_OPTIONS_TABLE . "
ef5584
				WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char);
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$auth_id_ary = array();
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$option_id_ary[] = $row['auth_option_id'];
ef5584
				$auth_id_ary[$row['auth_option']] = ACL_NO;
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			// First of all, lets grab the items having roles with the specified auth options assigned
ef5584
			$sql = "SELECT auth_role_id, $id_field, forum_id
ef5584
				FROM $table, " . ACL_ROLES_TABLE . " r
ef5584
				WHERE auth_role_id <> 0
ef5584
					AND auth_role_id = r.role_id
ef5584
					AND r.role_type = '{$permission_type}'
ef5584
					AND " . implode(' AND ', $where_sql) . '
ef5584
				ORDER BY auth_role_id';
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$cur_role_auth = array();
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$cur_role_auth[$row['auth_role_id']][$row['forum_id']][] = $row[$id_field];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			// Get role data for resetting data
ef5584
			if (sizeof($cur_role_auth))
ef5584
			{
ef5584
				$sql = 'SELECT ao.auth_option, rd.role_id, rd.auth_setting
ef5584
					FROM ' . ACL_OPTIONS_TABLE . ' ao, ' . ACL_ROLES_DATA_TABLE . ' rd
ef5584
					WHERE ao.auth_option_id = rd.auth_option_id
ef5584
						AND ' . $db->sql_in_set('rd.role_id', array_keys($cur_role_auth));
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				$auth_settings = array();
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					// We need to fill all auth_options, else setting it will fail...
ef5584
					if (!isset($auth_settings[$row['role_id']]))
ef5584
					{
ef5584
						$auth_settings[$row['role_id']] = $auth_id_ary;
ef5584
					}
ef5584
					$auth_settings[$row['role_id']][$row['auth_option']] = $row['auth_setting'];
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				// Set the options
ef5584
				foreach ($cur_role_auth as $role_id => $auth_row)
ef5584
				{
ef5584
					foreach ($auth_row as $f_id => $ug_row)
ef5584
					{
ef5584
						$this->acl_set($mode, $f_id, $ug_row, $auth_settings[$role_id], 0, false);
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Now, normally remove permissions...
ef5584
		if ($permission_type !== false)
ef5584
		{
ef5584
			$where_sql[] = $db->sql_in_set('auth_option_id', array_map('intval', $option_id_ary));
ef5584
		}
ef5584
		
ef5584
		$sql = "DELETE FROM $table
ef5584
			WHERE " . implode(' AND ', $where_sql);
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		$this->acl_clear_prefetch();
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Assign category to template
ef5584
	* used by display_mask()
ef5584
	*/
ef5584
	function assign_cat_array(&$category_array, $tpl_cat, $tpl_mask, $ug_id, $forum_id, $show_trace = false, $s_view)
ef5584
	{
ef5584
		global $template, $user, $phpbb_admin_path, $phpEx;
ef5584
ef5584
		@reset($category_array);
ef5584
		while (list($cat, $cat_array) = each($category_array))
ef5584
		{
ef5584
			$template->assign_block_vars($tpl_cat, array(
ef5584
				'S_YES'		=> ($cat_array['S_YES'] && !$cat_array['S_NEVER'] && !$cat_array['S_NO']) ? true : false,
ef5584
				'S_NEVER'	=> ($cat_array['S_NEVER'] && !$cat_array['S_YES'] && !$cat_array['S_NO']) ? true : false,
ef5584
				'S_NO'		=> ($cat_array['S_NO'] && !$cat_array['S_NEVER'] && !$cat_array['S_YES']) ? true : false,
ef5584
							
ef5584
				'CAT_NAME'	=> $user->lang['permission_cat'][$cat])
ef5584
			);
ef5584
ef5584
			/*	Sort permissions by name (more naturaly and user friendly than sorting by a primary key)
ef5584
			*	Commented out due to it's memory consumption and time needed
ef5584
			*
ef5584
			$key_array = array_intersect(array_keys($user->lang), array_map(create_function('$a', 'return "acl_" . $a;'), array_keys($cat_array['permissions'])));
ef5584
			$values_array = $cat_array['permissions'];
ef5584
ef5584
			$cat_array['permissions'] = array();
ef5584
ef5584
			foreach ($key_array as $key)
ef5584
			{
ef5584
				$key = str_replace('acl_', '', $key);
ef5584
				$cat_array['permissions'][$key] = $values_array[$key];
ef5584
			}
ef5584
			unset($key_array, $values_array);
ef5584
*/
ef5584
			@reset($cat_array['permissions']);
ef5584
			while (list($permission, $allowed) = each($cat_array['permissions']))
ef5584
			{
ef5584
				if ($s_view)
ef5584
				{
ef5584
					$template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
ef5584
						'S_YES'		=> ($allowed == ACL_YES) ? true : false,
ef5584
						'S_NEVER'	=> ($allowed == ACL_NEVER) ? true : false,
ef5584
ef5584
						'UG_ID'			=> $ug_id,
ef5584
						'FORUM_ID'		=> $forum_id,
ef5584
						'FIELD_NAME'	=> $permission,
ef5584
						'S_FIELD_NAME'	=> 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
ef5584
ef5584
						'U_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission") : '',
ef5584
						'UA_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
ef5584
ef5584
						'PERMISSION'	=> $user->lang['acl_' . $permission]['lang'])
ef5584
					);
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
ef5584
						'S_YES'		=> ($allowed == ACL_YES) ? true : false,
ef5584
						'S_NEVER'	=> ($allowed == ACL_NEVER) ? true : false,
ef5584
						'S_NO'		=> ($allowed == ACL_NO) ? true : false,
ef5584
ef5584
						'UG_ID'			=> $ug_id,
ef5584
						'FORUM_ID'		=> $forum_id,
ef5584
						'FIELD_NAME'	=> $permission,
ef5584
						'S_FIELD_NAME'	=> 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
ef5584
ef5584
						'U_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission") : '',
ef5584
						'UA_TRACE'		=> ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
ef5584
ef5584
						'PERMISSION'	=> $user->lang['acl_' . $permission]['lang'])
ef5584
					);
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Building content array from permission rows with explicit key ordering
ef5584
	* used by display_mask()
ef5584
	*/
ef5584
	function build_permission_array(&$permission_row, &$content_array, &$categories, $key_sort_array)
ef5584
	{
ef5584
		global $user;
ef5584
ef5584
		foreach ($key_sort_array as $forum_id)
ef5584
		{
ef5584
			if (!isset($permission_row[$forum_id]))
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$permissions = $permission_row[$forum_id];
ef5584
			ksort($permissions);
ef5584
ef5584
			@reset($permissions);
ef5584
			while (list($permission, $auth_setting) = each($permissions))
ef5584
			{
ef5584
				if (!isset($user->lang['acl_' . $permission]))
ef5584
				{
ef5584
					$user->lang['acl_' . $permission] = array(
ef5584
						'cat'	=> 'misc',
ef5584
						'lang'	=> '{ acl_' . $permission . ' }'
ef5584
					);
ef5584
				}
ef5584
			
ef5584
				$cat = $user->lang['acl_' . $permission]['cat'];
ef5584
			
ef5584
				// Build our categories array
ef5584
				if (!isset($categories[$cat]))
ef5584
				{
ef5584
					$categories[$cat] = $user->lang['permission_cat'][$cat];
ef5584
				}
ef5584
ef5584
				// Build our content array
ef5584
				if (!isset($content_array[$forum_id]))
ef5584
				{
ef5584
					$content_array[$forum_id] = array();
ef5584
				}
ef5584
ef5584
				if (!isset($content_array[$forum_id][$cat]))
ef5584
				{
ef5584
					$content_array[$forum_id][$cat] = array(
ef5584
						'S_YES'			=> false,
ef5584
						'S_NEVER'		=> false,
ef5584
						'S_NO'			=> false,
ef5584
						'permissions'	=> array(),
ef5584
					);
ef5584
				}
ef5584
ef5584
				$content_array[$forum_id][$cat]['S_YES'] |= ($auth_setting == ACL_YES) ? true : false;
ef5584
				$content_array[$forum_id][$cat]['S_NEVER'] |= ($auth_setting == ACL_NEVER) ? true : false;
ef5584
				$content_array[$forum_id][$cat]['S_NO'] |= ($auth_setting == ACL_NO) ? true : false;
ef5584
ef5584
				$content_array[$forum_id][$cat]['permissions'][$permission] = $auth_setting;
ef5584
			}
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Use permissions from another user. This transferes a permission set from one user to another.
ef5584
	* The other user is always able to revert back to his permission set.
ef5584
	* This function does not check for lower/higher permissions, it is possible for the user to gain
ef5584
	* "more" permissions by this.
ef5584
	* Admin permissions will not be copied.
ef5584
	*/
ef5584
	function ghost_permissions($from_user_id, $to_user_id)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		if ($to_user_id == ANONYMOUS)
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$hold_ary = $this->acl_raw_data_single_user($from_user_id);
ef5584
ef5584
		// Key 0 in $hold_ary are global options, all others are forum_ids
ef5584
ef5584
		// We disallow copying admin permissions
ef5584
		foreach ($this->acl_options['global'] as $opt => $id)
ef5584
		{
ef5584
			if (strpos($opt, 'a_') === 0)
ef5584
			{
ef5584
				$hold_ary[0][$this->acl_options['id'][$opt]] = ACL_NEVER;
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Force a_switchperm to be allowed
ef5584
		$hold_ary[0][$this->acl_options['id']['a_switchperm']] = ACL_YES;
ef5584
ef5584
		$user_permissions = $this->build_bitstring($hold_ary);
ef5584
ef5584
		if (!$user_permissions)
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		$sql = 'UPDATE ' . USERS_TABLE . "
ef5584
			SET user_permissions = '" . $db->sql_escape($user_permissions) . "',
ef5584
				user_perm_from = $from_user_id
ef5584
			WHERE user_id = " . $to_user_id;
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		return true;
ef5584
	}
ef5584
}
ef5584
ef5584
?>