Blame Extras/phpBB/3.0.4/includes/acp/auth.php

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