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

ef5584
ef5584
/**
ef5584
*
ef5584
* @package acp
ef5584
* @version $Id: acp_permissions.php 8710 2008-07-29 13:35:49Z acydburn $
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
* @package acp
ef5584
*/
ef5584
class acp_permissions
ef5584
{
ef5584
	var $u_action;
ef5584
	var $permission_dropdown;
ef5584
	
ef5584
	function main($id, $mode)
ef5584
	{
ef5584
		global $db, $user, $auth, $template, $cache;
ef5584
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
ef5584
ef5584
		include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
ef5584
		include_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
ef5584
ef5584
		$auth_admin = new auth_admin();
ef5584
ef5584
		$user->add_lang('acp/permissions');
ef5584
		add_permission_language();
ef5584
ef5584
		$this->tpl_name = 'acp_permissions';
ef5584
ef5584
		// Trace has other vars
ef5584
		if ($mode == 'trace')
ef5584
		{
ef5584
			$user_id = request_var('u', 0);
ef5584
			$forum_id = request_var('f', 0);
ef5584
			$permission = request_var('auth', '');
ef5584
ef5584
			$this->tpl_name = 'permission_trace';
ef5584
ef5584
			if ($user_id && isset($auth_admin->acl_options['id'][$permission]) && $auth->acl_get('a_viewauth'))
ef5584
			{
ef5584
				$this->page_title = sprintf($user->lang['TRACE_PERMISSION'], $user->lang['acl_' . $permission]['lang']);
ef5584
				$this->permission_trace($user_id, $forum_id, $permission);
ef5584
				return;
ef5584
			}
ef5584
			trigger_error('NO_MODE', E_USER_ERROR);
ef5584
		}
ef5584
ef5584
		// Set some vars
ef5584
		$action = request_var('action', array('' => 0));
ef5584
		$action = key($action);
ef5584
		$action = (isset($_POST['psubmit'])) ? 'apply_permissions' : $action;
ef5584
ef5584
		$all_forums = request_var('all_forums', 0);
ef5584
		$subforum_id = request_var('subforum_id', 0);
ef5584
		$forum_id = request_var('forum_id', array(0));
ef5584
ef5584
		$username = request_var('username', array(''), true);
ef5584
		$usernames = request_var('usernames', '', true);
ef5584
		$user_id = request_var('user_id', array(0));
ef5584
ef5584
		$group_id = request_var('group_id', array(0));
ef5584
		$select_all_groups = request_var('select_all_groups', 0);
ef5584
ef5584
		$form_name = 'acp_permissions';
ef5584
		add_form_key($form_name);
ef5584
ef5584
		// If select all groups is set, we pre-build the group id array (this option is used for other screens to link to the permission settings screen)
ef5584
		if ($select_all_groups)
ef5584
		{
ef5584
			// Add default groups to selection
ef5584
			$sql_and = (!$config['coppa_enable']) ? " AND group_name <> 'REGISTERED_COPPA'" : '';
ef5584
ef5584
			$sql = 'SELECT group_id
ef5584
				FROM ' . GROUPS_TABLE . '
ef5584
				WHERE group_type = ' . GROUP_SPECIAL . "
ef5584
				$sql_and";
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$group_id[] = $row['group_id'];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
		
ef5584
		// Map usernames to ids and vice versa
ef5584
		if ($usernames)
ef5584
		{
ef5584
			$username = explode("\n", $usernames);
ef5584
		}
ef5584
		unset($usernames);
ef5584
ef5584
		if (sizeof($username) && !sizeof($user_id))
ef5584
		{
ef5584
			user_get_id_name($user_id, $username);
ef5584
ef5584
			if (!sizeof($user_id))
ef5584
			{
ef5584
				trigger_error($user->lang['SELECTED_USER_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
			}
ef5584
		}
ef5584
		unset($username);
ef5584
		
ef5584
		// Build forum ids (of all forums are checked or subforum listing used)
ef5584
		if ($all_forums)
ef5584
		{
ef5584
			$sql = 'SELECT forum_id
ef5584
				FROM ' . FORUMS_TABLE . '
ef5584
				ORDER BY left_id';
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$forum_id = array();
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$forum_id[] = (int) $row['forum_id'];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
		else if ($subforum_id)
ef5584
		{
ef5584
			$forum_id = array();
ef5584
			foreach (get_forum_branch($subforum_id, 'children') as $row)
ef5584
			{
ef5584
				$forum_id[] = (int) $row['forum_id'];
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Define some common variables for every mode
ef5584
		$error = array();
ef5584
ef5584
		$permission_scope = (strpos($mode, '_global') !== false) ? 'global' : 'local';
ef5584
ef5584
		// Showing introductionary page?
ef5584
		if ($mode == 'intro')
ef5584
		{
ef5584
			$this->page_title = 'ACP_PERMISSIONS';
ef5584
ef5584
			$template->assign_vars(array(
ef5584
				'S_INTRO'		=> true)
ef5584
			);
ef5584
ef5584
			return;
ef5584
		}
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'setting_user_global':
ef5584
			case 'setting_group_global':
ef5584
				$this->permission_dropdown = array('u_', 'm_', 'a_');
ef5584
				$permission_victim = ($mode == 'setting_user_global') ? array('user') : array('group');
ef5584
				$this->page_title = ($mode == 'setting_user_global') ? 'ACP_USERS_PERMISSIONS' : 'ACP_GROUPS_PERMISSIONS';
ef5584
			break;
ef5584
ef5584
			case 'setting_user_local':
ef5584
			case 'setting_group_local':
ef5584
				$this->permission_dropdown = array('f_', 'm_');
ef5584
				$permission_victim = ($mode == 'setting_user_local') ? array('user', 'forums') : array('group', 'forums');
ef5584
				$this->page_title = ($mode == 'setting_user_local') ? 'ACP_USERS_FORUM_PERMISSIONS' : 'ACP_GROUPS_FORUM_PERMISSIONS';
ef5584
			break;
ef5584
ef5584
			case 'setting_admin_global':
ef5584
			case 'setting_mod_global':
ef5584
				$this->permission_dropdown = (strpos($mode, '_admin_') !== false) ? array('a_') : array('m_');
ef5584
				$permission_victim = array('usergroup');
ef5584
				$this->page_title = ($mode == 'setting_admin_global') ? 'ACP_ADMINISTRATORS' : 'ACP_GLOBAL_MODERATORS';
ef5584
			break;
ef5584
ef5584
			case 'setting_mod_local':
ef5584
			case 'setting_forum_local':
ef5584
				$this->permission_dropdown = ($mode == 'setting_mod_local') ? array('m_') : array('f_');
ef5584
				$permission_victim = array('forums', 'usergroup');
ef5584
				$this->page_title = ($mode == 'setting_mod_local') ? 'ACP_FORUM_MODERATORS' : 'ACP_FORUM_PERMISSIONS';
ef5584
			break;
ef5584
ef5584
			case 'view_admin_global':
ef5584
			case 'view_user_global':
ef5584
			case 'view_mod_global':
ef5584
				$this->permission_dropdown = ($mode == 'view_admin_global') ? array('a_') : (($mode == 'view_user_global') ? array('u_') : array('m_'));
ef5584
				$permission_victim = array('usergroup_view');
ef5584
				$this->page_title = ($mode == 'view_admin_global') ? 'ACP_VIEW_ADMIN_PERMISSIONS' : (($mode == 'view_user_global') ? 'ACP_VIEW_USER_PERMISSIONS' : 'ACP_VIEW_GLOBAL_MOD_PERMISSIONS');
ef5584
			break;
ef5584
ef5584
			case 'view_mod_local':
ef5584
			case 'view_forum_local':
ef5584
				$this->permission_dropdown = ($mode == 'view_mod_local') ? array('m_') : array('f_');
ef5584
				$permission_victim = array('forums', 'usergroup_view');
ef5584
				$this->page_title = ($mode == 'view_mod_local') ? 'ACP_VIEW_FORUM_MOD_PERMISSIONS' : 'ACP_VIEW_FORUM_PERMISSIONS';
ef5584
			break;
ef5584
ef5584
			default:
ef5584
				trigger_error('NO_MODE', E_USER_ERROR);
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'L_TITLE'		=> $user->lang[$this->page_title],
ef5584
			'L_EXPLAIN'		=> $user->lang[$this->page_title . '_EXPLAIN'])
ef5584
		);
ef5584
ef5584
		// Get permission type
ef5584
		$permission_type = request_var('type', $this->permission_dropdown[0]);
ef5584
ef5584
		if (!in_array($permission_type, $this->permission_dropdown))
ef5584
		{
ef5584
			trigger_error($user->lang['WRONG_PERMISSION_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
ef5584
		// Handle actions
ef5584
		if (strpos($mode, 'setting_') === 0 && $action)
ef5584
		{
ef5584
			switch ($action)
ef5584
			{
ef5584
				case 'delete':
ef5584
ef5584
					if (!check_form_key($form_name))
ef5584
					{
ef5584
						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
					// All users/groups selected?
ef5584
					$all_users = (isset($_POST['all_users'])) ? true : false;
ef5584
					$all_groups = (isset($_POST['all_groups'])) ? true : false;
ef5584
ef5584
					if ($all_users || $all_groups)
ef5584
					{
ef5584
						$items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
ef5584
ef5584
						if ($all_users && sizeof($items['user_ids']))
ef5584
						{
ef5584
							$user_id = $items['user_ids'];
ef5584
						}
ef5584
						else if ($all_groups && sizeof($items['group_ids']))
ef5584
						{
ef5584
							$group_id = $items['group_ids'];
ef5584
						}
ef5584
					}
ef5584
ef5584
					if (sizeof($user_id) || sizeof($group_id))
ef5584
					{
ef5584
						$this->remove_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id, $forum_id);
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						trigger_error($user->lang['NO_USER_GROUP_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
				break;
ef5584
ef5584
				case 'apply_permissions':
ef5584
					if (!isset($_POST['setting']))
ef5584
					{
ef5584
						trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
					if (!check_form_key($form_name))
ef5584
					{
ef5584
						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
ef5584
					$this->set_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
ef5584
				break;
ef5584
ef5584
				case 'apply_all_permissions':
ef5584
					if (!isset($_POST['setting']))
ef5584
					{
ef5584
						trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
					if (!check_form_key($form_name))
ef5584
					{
ef5584
						trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
ef5584
					$this->set_all_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
ef5584
				break;
ef5584
			}
ef5584
		}
ef5584
ef5584
ef5584
		// Setting permissions screen
ef5584
		$s_hidden_fields = build_hidden_fields(array(
ef5584
			'user_id'		=> $user_id,
ef5584
			'group_id'		=> $group_id,
ef5584
			'forum_id'		=> $forum_id,
ef5584
			'type'			=> $permission_type)
ef5584
		);
ef5584
ef5584
		// Go through the screens/options needed and present them in correct order
ef5584
		foreach ($permission_victim as $victim)
ef5584
		{
ef5584
			switch ($victim)
ef5584
			{
ef5584
				case 'forum_dropdown':
ef5584
ef5584
					if (sizeof($forum_id))
ef5584
					{
ef5584
						$this->check_existence('forum', $forum_id);
ef5584
						continue 2;
ef5584
					}
ef5584
ef5584
					$template->assign_vars(array(
ef5584
						'S_SELECT_FORUM'		=> true,
ef5584
						'S_FORUM_OPTIONS'		=> make_forum_select(false, false, true, false, false))
ef5584
					);
ef5584
ef5584
				break;
ef5584
ef5584
				case 'forums':
ef5584
ef5584
					if (sizeof($forum_id))
ef5584
					{
ef5584
						$this->check_existence('forum', $forum_id);
ef5584
						continue 2;
ef5584
					}
ef5584
ef5584
					$forum_list = make_forum_select(false, false, true, false, false, false, true);
ef5584
ef5584
					// Build forum options
ef5584
					$s_forum_options = '';
ef5584
					foreach ($forum_list as $f_id => $f_row)
ef5584
					{
ef5584
						$s_forum_options .= '<option value="' . $f_id . '"' . (($f_row['selected']) ? ' selected="selected"' : '') . (($f_row['disabled']) ? ' disabled="disabled" class="disabled-option"' : '') . '>' . $f_row['padding'] . $f_row['forum_name'] . '</option>';
ef5584
					}
ef5584
ef5584
					// Build subforum options
ef5584
					$s_subforum_options = $this->build_subforum_options($forum_list);
ef5584
ef5584
					$template->assign_vars(array(
ef5584
						'S_SELECT_FORUM'		=> true,
ef5584
						'S_FORUM_OPTIONS'		=> $s_forum_options,
ef5584
						'S_SUBFORUM_OPTIONS'	=> $s_subforum_options,
ef5584
						'S_FORUM_ALL'			=> true,
ef5584
						'S_FORUM_MULTIPLE'		=> true)
ef5584
					);
ef5584
ef5584
				break;
ef5584
ef5584
				case 'user':
ef5584
ef5584
					if (sizeof($user_id))
ef5584
					{
ef5584
						$this->check_existence('user', $user_id);
ef5584
						continue 2;
ef5584
					}
ef5584
ef5584
					$template->assign_vars(array(
ef5584
						'S_SELECT_USER'			=> true,
ef5584
						'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=select_victim&field=username&select_single=true'),
ef5584
					));
ef5584
ef5584
				break;
ef5584
ef5584
				case 'group':
ef5584
ef5584
					if (sizeof($group_id))
ef5584
					{
ef5584
						$this->check_existence('group', $group_id);
ef5584
						continue 2;
ef5584
					}
ef5584
ef5584
					$template->assign_vars(array(
ef5584
						'S_SELECT_GROUP'		=> true,
ef5584
						'S_GROUP_OPTIONS'		=> group_select_options(false, false, false), // Show all groups
ef5584
					));
ef5584
ef5584
				break;
ef5584
ef5584
				case 'usergroup':
ef5584
				case 'usergroup_view':
ef5584
ef5584
					$all_users = (isset($_POST['all_users'])) ? true : false;
ef5584
					$all_groups = (isset($_POST['all_groups'])) ? true : false;
ef5584
ef5584
					if ((sizeof($user_id) && !$all_users) || (sizeof($group_id) && !$all_groups))
ef5584
					{
ef5584
						if (sizeof($user_id))
ef5584
						{
ef5584
							$this->check_existence('user', $user_id);
ef5584
						}
ef5584
ef5584
						if (sizeof($group_id))
ef5584
						{
ef5584
							$this->check_existence('group', $group_id);
ef5584
						}
ef5584
ef5584
						continue 2;
ef5584
					}
ef5584
ef5584
					// Now we check the users... because the "all"-selection is different here (all defined users/groups)
ef5584
					$items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
ef5584
ef5584
					if ($all_users && sizeof($items['user_ids']))
ef5584
					{
ef5584
						$user_id = $items['user_ids'];
ef5584
						continue 2;
ef5584
					}
ef5584
ef5584
					if ($all_groups && sizeof($items['group_ids']))
ef5584
					{
ef5584
						$group_id = $items['group_ids'];
ef5584
						continue 2;
ef5584
					}
ef5584
ef5584
					$template->assign_vars(array(
ef5584
						'S_SELECT_USERGROUP'		=> ($victim == 'usergroup') ? true : false,
ef5584
						'S_SELECT_USERGROUP_VIEW'	=> ($victim == 'usergroup_view') ? true : false,
ef5584
						'S_DEFINED_USER_OPTIONS'	=> $items['user_ids_options'],
ef5584
						'S_DEFINED_GROUP_OPTIONS'	=> $items['group_ids_options'],
ef5584
						'S_ADD_GROUP_OPTIONS'		=> group_select_options(false, $items['group_ids'], false),	// Show all groups
ef5584
						'U_FIND_USERNAME'			=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=add_user&field=username&select_single=true'),
ef5584
					));
ef5584
ef5584
				break;
ef5584
			}
ef5584
ef5584
			// The S_ALLOW_SELECT parameter below is a measure to lower memory usage.
ef5584
			// If there are more than 5 forums selected the admin is not able to select all users/groups too.
ef5584
			// We need to see if the number of forums can be increased or need to be decreased.
ef5584
ef5584
			$template->assign_vars(array(
ef5584
				'U_ACTION'				=> $this->u_action,
ef5584
				'ANONYMOUS_USER_ID'		=> ANONYMOUS,
ef5584
ef5584
				'S_SELECT_VICTIM'		=> true,
ef5584
				'S_ALLOW_ALL_SELECT'	=> (sizeof($forum_id) > 5) ? false : true,
ef5584
				'S_CAN_SELECT_USER'		=> ($auth->acl_get('a_authusers')) ? true : false,
ef5584
				'S_CAN_SELECT_GROUP'	=> ($auth->acl_get('a_authgroups')) ? true : false,
ef5584
				'S_HIDDEN_FIELDS'		=> $s_hidden_fields)
ef5584
			);
ef5584
ef5584
			// Let the forum names being displayed
ef5584
			if (sizeof($forum_id))
ef5584
			{
ef5584
				$sql = 'SELECT forum_name
ef5584
					FROM ' . FORUMS_TABLE . '
ef5584
					WHERE ' . $db->sql_in_set('forum_id', $forum_id) . '
ef5584
					ORDER BY left_id ASC';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				$forum_names = array();
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$forum_names[] = $row['forum_name'];
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'S_FORUM_NAMES'		=> (sizeof($forum_names)) ? true : false,
ef5584
					'FORUM_NAMES'		=> implode(', ', $forum_names))
ef5584
				);
ef5584
			}
ef5584
ef5584
			return;
ef5584
		}
ef5584
ef5584
		// Do not allow forum_ids being set and no other setting defined (will bog down the server too much)
ef5584
		if (sizeof($forum_id) && !sizeof($user_id) && !sizeof($group_id))
ef5584
		{
ef5584
			trigger_error($user->lang['ONLY_FORUM_DEFINED'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'S_PERMISSION_DROPDOWN'		=> (sizeof($this->permission_dropdown) > 1) ? $this->build_permission_dropdown($this->permission_dropdown, $permission_type, $permission_scope) : false,
ef5584
			'L_PERMISSION_TYPE'			=> $user->lang['ACL_TYPE_' . strtoupper($permission_type)],
ef5584
ef5584
			'U_ACTION'					=> $this->u_action,
ef5584
			'S_HIDDEN_FIELDS'			=> $s_hidden_fields)
ef5584
		);
ef5584
ef5584
		if (strpos($mode, 'setting_') === 0)
ef5584
		{
ef5584
			$template->assign_vars(array(
ef5584
				'S_SETTING_PERMISSIONS'		=> true)
ef5584
			);
ef5584
ef5584
			$hold_ary = $auth_admin->get_mask('set', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NO);
ef5584
			$auth_admin->display_mask('set', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$template->assign_vars(array(
ef5584
				'S_VIEWING_PERMISSIONS'		=> true)
ef5584
			);
ef5584
ef5584
			$hold_ary = $auth_admin->get_mask('view', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NEVER);
ef5584
			$auth_admin->display_mask('view', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build +subforum options
ef5584
	*/
ef5584
	function build_subforum_options($forum_list)
ef5584
	{
ef5584
		global $user;
ef5584
ef5584
		$s_options = '';
ef5584
ef5584
		$forum_list = array_merge($forum_list);
ef5584
ef5584
		foreach ($forum_list as $key => $row)
ef5584
		{
ef5584
			if ($row['disabled'])
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$s_options .= '<option value="' . $row['forum_id'] . '"' . (($row['selected']) ? ' selected="selected"' : '') . '>' . $row['padding'] . $row['forum_name'];
ef5584
ef5584
			// We check if a branch is there...
ef5584
			$branch_there = false;
ef5584
ef5584
			foreach (array_slice($forum_list, $key + 1) as $temp_row)
ef5584
			{
ef5584
				if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
ef5584
				{
ef5584
					$branch_there = true;
ef5584
					break;
ef5584
				}
ef5584
				continue;
ef5584
			}
ef5584
			
ef5584
			if ($branch_there)
ef5584
			{
ef5584
				$s_options .= ' [' . $user->lang['PLUS_SUBFORUMS'] . ']';
ef5584
			}
ef5584
ef5584
			$s_options .= '</option>';
ef5584
		}
ef5584
ef5584
		return $s_options;
ef5584
	}
ef5584
	
ef5584
	/**
ef5584
	* Build dropdown field for changing permission types
ef5584
	*/
ef5584
	function build_permission_dropdown($options, $default_option, $permission_scope)
ef5584
	{
ef5584
		global $user, $auth;
ef5584
		
ef5584
		$s_dropdown_options = '';
ef5584
		foreach ($options as $setting)
ef5584
		{
ef5584
			if (!$auth->acl_get('a_' . str_replace('_', '', $setting) . 'auth'))
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$selected = ($setting == $default_option) ? ' selected="selected"' : '';
ef5584
			$l_setting = (isset($user->lang['permission_type'][$permission_scope][$setting])) ? $user->lang['permission_type'][$permission_scope][$setting] : $user->lang['permission_type'][$setting];
ef5584
			$s_dropdown_options .= '<option value="' . $setting . '"' . $selected . '>' . $l_setting . '</option>';
ef5584
		}
ef5584
ef5584
		return $s_dropdown_options;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Check if selected items exist. Remove not found ids and if empty return error.
ef5584
	*/
ef5584
	function check_existence($mode, &$ids)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'user':
ef5584
				$table = USERS_TABLE;
ef5584
				$sql_id = 'user_id';
ef5584
			break;
ef5584
ef5584
			case 'group':
ef5584
				$table = GROUPS_TABLE;
ef5584
				$sql_id = 'group_id';
ef5584
			break;
ef5584
ef5584
			case 'forum':
ef5584
				$table = FORUMS_TABLE;
ef5584
				$sql_id = 'forum_id';
ef5584
			break;
ef5584
		}
ef5584
ef5584
		if (sizeof($ids))
ef5584
		{
ef5584
			$sql = "SELECT $sql_id
ef5584
				FROM $table
ef5584
				WHERE " . $db->sql_in_set($sql_id, $ids);
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$ids = array();
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$ids[] = (int) $row[$sql_id];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
ef5584
		if (!sizeof($ids))
ef5584
		{
ef5584
			trigger_error($user->lang['SELECTED_' . strtoupper($mode) . '_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Apply permissions
ef5584
	*/
ef5584
	function set_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
ef5584
	{
ef5584
		global $user, $auth;
ef5584
ef5584
		$psubmit = request_var('psubmit', array(0 => array(0 => 0)));
ef5584
ef5584
		// User or group to be set?
ef5584
		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
ef5584
ef5584
		// Check the permission setting again
ef5584
		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
ef5584
		{
ef5584
			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
		
ef5584
		$ug_id = $forum_id = 0;
ef5584
ef5584
		// We loop through the auth settings defined in our submit
ef5584
		list($ug_id, ) = each($psubmit);
ef5584
		list($forum_id, ) = each($psubmit[$ug_id]);
ef5584
ef5584
		if (empty($_POST['setting']) || empty($_POST['setting'][$ug_id]) || empty($_POST['setting'][$ug_id][$forum_id]) || !is_array($_POST['setting'][$ug_id][$forum_id]))
ef5584
		{
ef5584
			trigger_error('WRONG_PERMISSION_SETTING_FORMAT', E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		// We obtain and check $_POST['setting'][$ug_id][$forum_id] directly and not using request_var() because request_var()
ef5584
		// currently does not support the amount of dimensions required. ;)
ef5584
		//		$auth_settings = request_var('setting', array(0 => array(0 => array('' => 0))));
ef5584
		$auth_settings = array_map('intval', $_POST['setting'][$ug_id][$forum_id]);
ef5584
ef5584
		// Do we have a role we want to set?
ef5584
		$assigned_role = (isset($_POST['role'][$ug_id][$forum_id])) ? (int) $_POST['role'][$ug_id][$forum_id] : 0;
ef5584
ef5584
		// Do the admin want to set these permissions to other items too?
ef5584
		$inherit = request_var('inherit', array(0 => array(0)));
ef5584
ef5584
		$ug_id = array($ug_id);
ef5584
		$forum_id = array($forum_id);
ef5584
ef5584
		if (sizeof($inherit))
ef5584
		{
ef5584
			foreach ($inherit as $_ug_id => $forum_id_ary)
ef5584
			{
ef5584
				// Inherit users/groups?
ef5584
				if (!in_array($_ug_id, $ug_id))
ef5584
				{
ef5584
					$ug_id[] = $_ug_id;
ef5584
				}
ef5584
ef5584
				// Inherit forums?
ef5584
				$forum_id = array_merge($forum_id, array_keys($forum_id_ary));
ef5584
			}
ef5584
		}
ef5584
ef5584
		$forum_id = array_unique($forum_id);
ef5584
ef5584
		// If the auth settings differ from the assigned role, then do not set a role...
ef5584
		if ($assigned_role)
ef5584
		{
ef5584
			if (!$this->check_assigned_role($assigned_role, $auth_settings))
ef5584
			{
ef5584
				$assigned_role = 0;
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Update the permission set...
ef5584
		$auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_settings, $assigned_role);
ef5584
ef5584
		// Do we need to recache the moderator lists?
ef5584
		if ($permission_type == 'm_')
ef5584
		{
ef5584
			cache_moderators();
ef5584
		}
ef5584
ef5584
		// Remove users who are now moderators or admins from everyones foes list
ef5584
		if ($permission_type == 'm_' || $permission_type == 'a_')
ef5584
		{
ef5584
			update_foes($group_id, $user_id);
ef5584
		}
ef5584
ef5584
		$this->log_action($mode, 'add', $permission_type, $ug_type, $ug_id, $forum_id);
ef5584
ef5584
		trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Apply all permissions
ef5584
	*/
ef5584
	function set_all_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
ef5584
	{
ef5584
		global $user, $auth;
ef5584
ef5584
		// User or group to be set?
ef5584
		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
ef5584
ef5584
		// Check the permission setting again
ef5584
		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
ef5584
		{
ef5584
			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$auth_settings = (isset($_POST['setting'])) ? $_POST['setting'] : array();
ef5584
		$auth_roles = (isset($_POST['role'])) ? $_POST['role'] : array();
ef5584
		$ug_ids = $forum_ids = array();
ef5584
ef5584
		// We need to go through the auth settings
ef5584
		foreach ($auth_settings as $ug_id => $forum_auth_row)
ef5584
		{
ef5584
			$ug_id = (int) $ug_id;
ef5584
			$ug_ids[] = $ug_id;
ef5584
ef5584
			foreach ($forum_auth_row as $forum_id => $auth_options)
ef5584
			{
ef5584
				$forum_id = (int) $forum_id;
ef5584
				$forum_ids[] = $forum_id;
ef5584
ef5584
				// Check role...
ef5584
				$assigned_role = (isset($auth_roles[$ug_id][$forum_id])) ? (int) $auth_roles[$ug_id][$forum_id] : 0;
ef5584
ef5584
				// If the auth settings differ from the assigned role, then do not set a role...
ef5584
				if ($assigned_role)
ef5584
				{
ef5584
					if (!$this->check_assigned_role($assigned_role, $auth_options))
ef5584
					{
ef5584
						$assigned_role = 0;
ef5584
					}
ef5584
				}
ef5584
ef5584
				// Update the permission set...
ef5584
				$auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_options, $assigned_role, false);
ef5584
			}
ef5584
		}
ef5584
ef5584
		$auth_admin->acl_clear_prefetch();
ef5584
ef5584
		// Do we need to recache the moderator lists?
ef5584
		if ($permission_type == 'm_')
ef5584
		{
ef5584
			cache_moderators();
ef5584
		}
ef5584
ef5584
		// Remove users who are now moderators or admins from everyones foes list
ef5584
		if ($permission_type == 'm_' || $permission_type == 'a_')
ef5584
		{
ef5584
			update_foes($group_id, $user_id);
ef5584
		}
ef5584
ef5584
		$this->log_action($mode, 'add', $permission_type, $ug_type, $ug_ids, $forum_ids);
ef5584
ef5584
		trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Compare auth settings with auth settings from role
ef5584
	* returns false if they differ, true if they are equal
ef5584
	*/
ef5584
	function check_assigned_role($role_id, &$auth_settings)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		$sql = 'SELECT o.auth_option, r.auth_setting
ef5584
			FROM ' . ACL_OPTIONS_TABLE . ' o, ' . ACL_ROLES_DATA_TABLE . ' r
ef5584
			WHERE o.auth_option_id = r.auth_option_id
ef5584
				AND r.role_id = ' . $role_id;
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$test_auth_settings = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$test_auth_settings[$row['auth_option']] = $row['auth_setting'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// We need to add any ACL_NO setting from auth_settings to compare correctly
ef5584
		foreach ($auth_settings as $option => $setting)
ef5584
		{
ef5584
			if ($setting == ACL_NO)
ef5584
			{
ef5584
				$test_auth_settings[$option] = $setting;
ef5584
			}
ef5584
		}
ef5584
ef5584
		if (sizeof(array_diff_assoc($auth_settings, $test_auth_settings)))
ef5584
		{
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		return true;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Remove permissions
ef5584
	*/
ef5584
	function remove_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id, &$forum_id)
ef5584
	{
ef5584
		global $user, $db, $auth;
ef5584
			
ef5584
		// User or group to be set?
ef5584
		$ug_type = (sizeof($user_id)) ? 'user' : 'group';
ef5584
ef5584
		// Check the permission setting again
ef5584
		if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
ef5584
		{
ef5584
			trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		$auth_admin->acl_delete($ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : false), $permission_type);
ef5584
ef5584
		// Do we need to recache the moderator lists?
ef5584
		if ($permission_type == 'm_')
ef5584
		{
ef5584
			cache_moderators();
ef5584
		}
ef5584
ef5584
		$this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : array(0 => 0)));
ef5584
ef5584
		trigger_error($user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Log permission changes
ef5584
	*/
ef5584
	function log_action($mode, $action, $permission_type, $ug_type, $ug_id, $forum_id)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		if (!is_array($ug_id))
ef5584
		{
ef5584
			$ug_id = array($ug_id);
ef5584
		}
ef5584
ef5584
		if (!is_array($forum_id))
ef5584
		{
ef5584
			$forum_id = array($forum_id);
ef5584
		}
ef5584
ef5584
		// Logging ... first grab user or groupnames ...
ef5584
		$sql = ($ug_type == 'group') ? 'SELECT group_name as name, group_type FROM ' . GROUPS_TABLE . ' WHERE ' : 'SELECT username as name FROM ' . USERS_TABLE . ' WHERE ';
ef5584
		$sql .= $db->sql_in_set(($ug_type == 'group') ? 'group_id' : 'user_id', array_map('intval', $ug_id));
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$l_ug_list = '';
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$l_ug_list .= (($l_ug_list != '') ? ', ' : '') . ((isset($row['group_type']) && $row['group_type'] == GROUP_SPECIAL) ? '' . $user->lang['G_' . $row['name']] . '' : $row['name']);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		$mode = str_replace('setting_', '', $mode);
ef5584
ef5584
		if ($forum_id[0] == 0)
ef5584
		{
ef5584
			add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_ug_list);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			// Grab the forum details if non-zero forum_id
ef5584
			$sql = 'SELECT forum_name
ef5584
				FROM ' . FORUMS_TABLE . '
ef5584
				WHERE ' . $db->sql_in_set('forum_id', $forum_id);
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$l_forum_list = '';
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$l_forum_list .= (($l_forum_list != '') ? ', ' : '') . $row['forum_name'];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_forum_list, $l_ug_list);
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Display a complete trace tree for the selected permission to determine where settings are set/unset
ef5584
	*/
ef5584
	function permission_trace($user_id, $forum_id, $permission)
ef5584
	{
ef5584
		global $db, $template, $user, $auth;
ef5584
ef5584
		if ($user_id != $user->data['user_id'])
ef5584
		{
ef5584
			$sql = 'SELECT user_id, username, user_permissions, user_type
ef5584
				FROM ' . USERS_TABLE . '
ef5584
				WHERE user_id = ' . $user_id;
ef5584
			$result = $db->sql_query($sql);
ef5584
			$userdata = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$userdata = $user->data;
ef5584
		}
ef5584
ef5584
		if (!$userdata)
ef5584
		{
ef5584
			trigger_error('NO_USERS', E_USER_ERROR);
ef5584
		}
ef5584
ef5584
		$forum_name = false;
ef5584
ef5584
		if ($forum_id)
ef5584
		{
ef5584
			$sql = 'SELECT forum_name
ef5584
				FROM ' . FORUMS_TABLE . "
ef5584
				WHERE forum_id = $forum_id";
ef5584
			$result = $db->sql_query($sql, 3600);
ef5584
			$forum_name = $db->sql_fetchfield('forum_name');
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
ef5584
		$back = request_var('back', 0);
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'PERMISSION'			=> $user->lang['acl_' . $permission]['lang'],
ef5584
			'PERMISSION_USERNAME'	=> $userdata['username'],
ef5584
			'FORUM_NAME'			=> $forum_name,
ef5584
ef5584
			'S_GLOBAL_TRACE'		=> ($forum_id) ? false : true,
ef5584
ef5584
			'U_BACK'				=> ($back) ? build_url(array('f', 'back')) . "&f=$back" : '')
ef5584
		);
ef5584
ef5584
		$template->assign_block_vars('trace', array(
ef5584
			'WHO'			=> $user->lang['DEFAULT'],
ef5584
			'INFORMATION'	=> $user->lang['TRACE_DEFAULT'],
ef5584
ef5584
			'S_SETTING_NO'		=> true,
ef5584
			'S_TOTAL_NO'		=> true)
ef5584
		);
ef5584
ef5584
		$sql = 'SELECT DISTINCT g.group_name, g.group_id, g.group_type
ef5584
			FROM ' . GROUPS_TABLE . ' g
ef5584
				LEFT JOIN ' . USER_GROUP_TABLE . ' ug ON (ug.group_id = g.group_id)
ef5584
			WHERE ug.user_id = ' . $user_id . '
ef5584
				AND ug.user_pending = 0
ef5584
			ORDER BY g.group_type DESC, g.group_id DESC';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$groups = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$groups[$row['group_id']] = array(
ef5584
				'auth_setting'		=> ACL_NO,
ef5584
				'group_name'		=> ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']
ef5584
			);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		$total = ACL_NO;
ef5584
		$add_key = (($forum_id) ? '_LOCAL' : '');
ef5584
ef5584
		if (sizeof($groups))
ef5584
		{
ef5584
			// Get group auth settings
ef5584
			$hold_ary = $auth->acl_group_raw_data(array_keys($groups), $permission, $forum_id);
ef5584
ef5584
			foreach ($hold_ary as $group_id => $forum_ary)
ef5584
			{
ef5584
				$groups[$group_id]['auth_setting'] = $hold_ary[$group_id][$forum_id][$permission];
ef5584
			}
ef5584
			unset($hold_ary);
ef5584
ef5584
			foreach ($groups as $id => $row)
ef5584
			{
ef5584
				switch ($row['auth_setting'])
ef5584
				{
ef5584
					case ACL_NO:
ef5584
						$information = $user->lang['TRACE_GROUP_NO' . $add_key];
ef5584
					break;
ef5584
ef5584
					case ACL_YES:
ef5584
						$information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_YES_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_YES_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_GROUP_YES_TOTAL_NO' . $add_key]);
ef5584
						$total = ($total == ACL_NO) ? ACL_YES : $total;
ef5584
					break;
ef5584
ef5584
					case ACL_NEVER:
ef5584
						$information = ($total == ACL_YES) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_GROUP_NEVER_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_GROUP_NEVER_TOTAL_NO' . $add_key]);
ef5584
						$total = ACL_NEVER;
ef5584
					break;
ef5584
				}
ef5584
ef5584
				$template->assign_block_vars('trace', array(
ef5584
					'WHO'			=> $row['group_name'],
ef5584
					'INFORMATION'	=> $information,
ef5584
ef5584
					'S_SETTING_NO'		=> ($row['auth_setting'] == ACL_NO) ? true : false,
ef5584
					'S_SETTING_YES'		=> ($row['auth_setting'] == ACL_YES) ? true : false,
ef5584
					'S_SETTING_NEVER'	=> ($row['auth_setting'] == ACL_NEVER) ? true : false,
ef5584
					'S_TOTAL_NO'		=> ($total == ACL_NO) ? true : false,
ef5584
					'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
ef5584
					'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
ef5584
				);
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Get user specific permission... globally or for this forum
ef5584
		$hold_ary = $auth->acl_user_raw_data($user_id, $permission, $forum_id);
ef5584
		$auth_setting = (!sizeof($hold_ary)) ? ACL_NO : $hold_ary[$user_id][$forum_id][$permission];
ef5584
ef5584
		switch ($auth_setting)
ef5584
		{
ef5584
			case ACL_NO:
ef5584
				$information = ($total == ACL_NO) ? $user->lang['TRACE_USER_NO_TOTAL_NO' . $add_key] : $user->lang['TRACE_USER_KEPT' . $add_key];
ef5584
				$total = ($total == ACL_NO) ? ACL_NEVER : $total;
ef5584
			break;
ef5584
ef5584
			case ACL_YES:
ef5584
				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_YES_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_YES_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_USER_YES_TOTAL_NO' . $add_key]);
ef5584
				$total = ($total == ACL_NO) ? ACL_YES : $total;
ef5584
			break;
ef5584
ef5584
			case ACL_NEVER:
ef5584
				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_NEVER_TOTAL_YES' . $add_key] : (($total == ACL_NEVER) ? $user->lang['TRACE_USER_NEVER_TOTAL_NEVER' . $add_key] : $user->lang['TRACE_USER_NEVER_TOTAL_NO' . $add_key]);
ef5584
				$total = ACL_NEVER;
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$template->assign_block_vars('trace', array(
ef5584
			'WHO'			=> $userdata['username'],
ef5584
			'INFORMATION'	=> $information,
ef5584
ef5584
			'S_SETTING_NO'		=> ($auth_setting == ACL_NO) ? true : false,
ef5584
			'S_SETTING_YES'		=> ($auth_setting == ACL_YES) ? true : false,
ef5584
			'S_SETTING_NEVER'	=> ($auth_setting == ACL_NEVER) ? true : false,
ef5584
			'S_TOTAL_NO'		=> false,
ef5584
			'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
ef5584
			'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
ef5584
		);
ef5584
ef5584
		if ($forum_id != 0 && isset($auth->acl_options['global'][$permission]))
ef5584
		{
ef5584
			if ($user_id != $user->data['user_id'])
ef5584
			{
ef5584
				$auth2 = new auth();
ef5584
				$auth2->acl($userdata);
ef5584
				$auth_setting = $auth2->acl_get($permission);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$auth_setting = $auth->acl_get($permission);
ef5584
			}
ef5584
ef5584
			if ($auth_setting)
ef5584
			{
ef5584
				$information = ($total == ACL_YES) ? $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_YES'] : $user->lang['TRACE_USER_GLOBAL_YES_TOTAL_NEVER'];
ef5584
				$total = ACL_YES;
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$information = $user->lang['TRACE_USER_GLOBAL_NEVER_TOTAL_KEPT'];
ef5584
			}
ef5584
ef5584
			// If there is no auth information we do not need to worry the user by showing non-relevant data.
ef5584
			if ($auth_setting)
ef5584
			{
ef5584
				$template->assign_block_vars('trace', array(
ef5584
					'WHO'			=> sprintf($user->lang['TRACE_GLOBAL_SETTING'], $userdata['username']),
ef5584
					'INFORMATION'	=> sprintf($information, '", ''),
ef5584
ef5584
					'S_SETTING_NO'		=> false,
ef5584
					'S_SETTING_YES'		=> $auth_setting,
ef5584
					'S_SETTING_NEVER'	=> !$auth_setting,
ef5584
					'S_TOTAL_NO'		=> false,
ef5584
					'S_TOTAL_YES'		=> ($total == ACL_YES) ? true : false,
ef5584
					'S_TOTAL_NEVER'		=> ($total == ACL_NEVER) ? true : false)
ef5584
				);
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Take founder status into account, overwriting the default values
ef5584
		if ($userdata['user_type'] == USER_FOUNDER && strpos($permission, 'a_') === 0)
ef5584
		{
ef5584
			$template->assign_block_vars('trace', array(
ef5584
				'WHO'			=> $userdata['username'],
ef5584
				'INFORMATION'	=> $user->lang['TRACE_USER_FOUNDER'],
ef5584
ef5584
				'S_SETTING_NO'		=> ($auth_setting == ACL_NO) ? true : false,
ef5584
				'S_SETTING_YES'		=> ($auth_setting == ACL_YES) ? true : false,
ef5584
				'S_SETTING_NEVER'	=> ($auth_setting == ACL_NEVER) ? true : false,
ef5584
				'S_TOTAL_NO'		=> false,
ef5584
				'S_TOTAL_YES'		=> true,
ef5584
				'S_TOTAL_NEVER'		=> false)
ef5584
			);
ef5584
ef5584
			$total = ACL_YES;
ef5584
		}
ef5584
ef5584
		// Total value...
ef5584
		$template->assign_vars(array(
ef5584
			'S_RESULT_NO'		=> ($total == ACL_NO) ? true : false,
ef5584
			'S_RESULT_YES'		=> ($total == ACL_YES) ? true : false,
ef5584
			'S_RESULT_NEVER'	=> ($total == ACL_NEVER) ? true : false,
ef5584
		));
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get already assigned users/groups
ef5584
	*/
ef5584
	function retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$sql_forum_id = ($permission_scope == 'global') ? 'AND a.forum_id = 0' : ((sizeof($forum_id)) ? 'AND ' . $db->sql_in_set('a.forum_id', $forum_id) : 'AND a.forum_id <> 0');
ef5584
ef5584
		// Permission options are only able to be a permission set... therefore we will pre-fetch the possible options and also the possible roles
ef5584
		$option_ids = $role_ids = array();
ef5584
ef5584
		$sql = 'SELECT 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
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$option_ids[] = (int) $row['auth_option_id'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (sizeof($option_ids))
ef5584
		{
ef5584
			$sql = 'SELECT DISTINCT role_id
ef5584
				FROM ' . ACL_ROLES_DATA_TABLE . '
ef5584
				WHERE ' . $db->sql_in_set('auth_option_id', $option_ids);
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			while ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$role_ids[] = (int) $row['role_id'];
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
ef5584
		if (sizeof($option_ids) && sizeof($role_ids))
ef5584
		{
ef5584
			$sql_where = 'AND (' . $db->sql_in_set('a.auth_option_id', $option_ids) . ' OR ' . $db->sql_in_set('a.auth_role_id', $role_ids) . ')';
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$sql_where = 'AND ' . $db->sql_in_set('a.auth_option_id', $option_ids);
ef5584
		}
ef5584
ef5584
		// Not ideal, due to the filesort, non-use of indexes, etc.
ef5584
		$sql = 'SELECT DISTINCT u.user_id, u.username, u.username_clean, u.user_regdate
ef5584
			FROM ' . USERS_TABLE . ' u, ' . ACL_USERS_TABLE . " a
ef5584
			WHERE u.user_id = a.user_id
ef5584
				$sql_forum_id
ef5584
				$sql_where
ef5584
			ORDER BY u.username_clean, u.user_regdate ASC";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$s_defined_user_options = '';
ef5584
		$defined_user_ids = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$s_defined_user_options .= '<option value="' . $row['user_id'] . '">' . $row['username'] . '</option>';
ef5584
			$defined_user_ids[] = $row['user_id'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		$sql = 'SELECT DISTINCT g.group_type, g.group_name, g.group_id
ef5584
			FROM ' . GROUPS_TABLE . ' g, ' . ACL_GROUPS_TABLE . " a
ef5584
			WHERE g.group_id = a.group_id
ef5584
				$sql_forum_id
ef5584
				$sql_where
ef5584
			ORDER BY g.group_type DESC, g.group_name ASC";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$s_defined_group_options = '';
ef5584
		$defined_group_ids = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$s_defined_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '">' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
ef5584
			$defined_group_ids[] = $row['group_id'];
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		return array(
ef5584
			'group_ids'			=> $defined_group_ids,
ef5584
			'group_ids_options'	=> $s_defined_group_options,
ef5584
			'user_ids'			=> $defined_user_ids,
ef5584
			'user_ids_options'	=> $s_defined_user_options
ef5584
		);
ef5584
	}
ef5584
}
ef5584
ef5584
?>