Merge changes made in revisions #r9405 to #r9467
[phpbb.git] / phpBB / modules / acp / acp_permissions.php
blob074a468fefdad26df1cb2106af4b76957ffb3599
1 <?php
2 /**
4 * @package acp
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * @package acp
22 class acp_permissions
24 var $u_action;
25 var $permission_dropdown;
27 function main($id, $mode)
29 include_once(PHPBB_ROOT_PATH . 'includes/functions_user.' . PHP_EXT);
30 include_once(PHPBB_ROOT_PATH . 'includes/acp/auth.' . PHP_EXT);
32 $auth_admin = new auth_admin();
34 phpbb::$user->add_lang('acp/permissions');
35 add_permission_language();
37 $this->tpl_name = 'acp_permissions';
39 // Trace has other vars
40 if ($mode == 'trace')
42 $user_id = request_var('u', 0);
43 $forum_id = request_var('f', 0);
44 $permission = request_var('auth', '');
46 $this->tpl_name = 'permission_trace';
48 if ($user_id && isset($auth_admin->acl_options['id'][$permission]) && phpbb::$acl->acl_get('a_viewauth'))
50 $this->page_title = sprintf(phpbb::$user->lang['TRACE_PERMISSION'], phpbb::$user->lang['acl_' . $permission]['lang']);
51 $this->permission_trace($user_id, $forum_id, $permission);
52 return;
54 trigger_error('NO_MODE', E_USER_ERROR);
57 // Set some vars
58 $action = request_var('action', array('' => 0));
59 $action = key($action);
60 $action = (phpbb_request::is_set_post('psubmit')) ? 'apply_permissions' : $action;
62 $all_forums = request_var('all_forums', 0);
63 $subforum_id = request_var('subforum_id', 0);
64 $forum_id = request_var('forum_id', array(0));
66 $username = request_var('username', array(''), true);
67 $usernames = request_var('usernames', '', true);
68 $user_id = request_var('user_id', array(0));
70 $group_id = request_var('group_id', array(0));
71 $select_all_groups = request_var('select_all_groups', 0);
73 $form_name = 'acp_permissions';
74 add_form_key($form_name);
76 // 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)
77 if ($select_all_groups)
79 // Add default groups to selection
80 $sql_and = (!phpbb::$config['coppa_enable']) ? " AND group_name <> 'REGISTERED_COPPA'" : '';
82 $sql = 'SELECT group_id
83 FROM ' . GROUPS_TABLE . '
84 WHERE group_type = ' . GROUP_SPECIAL . "
85 $sql_and";
86 $result = phpbb::$db->sql_query($sql);
88 while ($row = phpbb::$db->sql_fetchrow($result))
90 $group_id[] = $row['group_id'];
92 phpbb::$db->sql_freeresult($result);
95 // Map usernames to ids and vice versa
96 if ($usernames)
98 $username = explode("\n", $usernames);
100 unset($usernames);
102 if (sizeof($username) && !sizeof($user_id))
104 user_get_id_name($user_id, $username);
106 if (!sizeof($user_id))
108 trigger_error(phpbb::$user->lang['SELECTED_USER_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
111 unset($username);
113 // Build forum ids (of all forums are checked or subforum listing used)
114 if ($all_forums)
116 $sql = 'SELECT forum_id
117 FROM ' . FORUMS_TABLE . '
118 ORDER BY left_id';
119 $result = phpbb::$db->sql_query($sql);
121 $forum_id = array();
122 while ($row = phpbb::$db->sql_fetchrow($result))
124 $forum_id[] = (int) $row['forum_id'];
126 phpbb::$db->sql_freeresult($result);
128 else if ($subforum_id)
130 $forum_id = array();
131 foreach (get_forum_branch($subforum_id, 'children') as $row)
133 $forum_id[] = (int) $row['forum_id'];
137 // Define some common variables for every mode
138 $error = array();
140 $permission_scope = (strpos($mode, '_global') !== false) ? 'global' : 'local';
142 // Showing introductionary page?
143 if ($mode == 'intro')
145 $this->page_title = 'ACP_PERMISSIONS';
147 phpbb::$template->assign_vars(array(
148 'S_INTRO' => true,
151 return;
154 switch ($mode)
156 case 'setting_user_global':
157 case 'setting_group_global':
158 $this->permission_dropdown = array('u_', 'm_', 'a_');
159 $permission_victim = ($mode == 'setting_user_global') ? array('user') : array('group');
160 $this->page_title = ($mode == 'setting_user_global') ? 'ACP_USERS_PERMISSIONS' : 'ACP_GROUPS_PERMISSIONS';
161 break;
163 case 'setting_user_local':
164 case 'setting_group_local':
165 $this->permission_dropdown = array('f_', 'm_');
166 $permission_victim = ($mode == 'setting_user_local') ? array('user', 'forums') : array('group', 'forums');
167 $this->page_title = ($mode == 'setting_user_local') ? 'ACP_USERS_FORUM_PERMISSIONS' : 'ACP_GROUPS_FORUM_PERMISSIONS';
168 break;
170 case 'setting_admin_global':
171 case 'setting_mod_global':
172 $this->permission_dropdown = (strpos($mode, '_admin_') !== false) ? array('a_') : array('m_');
173 $permission_victim = array('usergroup');
174 $this->page_title = ($mode == 'setting_admin_global') ? 'ACP_ADMINISTRATORS' : 'ACP_GLOBAL_MODERATORS';
175 break;
177 case 'setting_mod_local':
178 case 'setting_forum_local':
179 $this->permission_dropdown = ($mode == 'setting_mod_local') ? array('m_') : array('f_');
180 $permission_victim = array('forums', 'usergroup');
181 $this->page_title = ($mode == 'setting_mod_local') ? 'ACP_FORUM_MODERATORS' : 'ACP_FORUM_PERMISSIONS';
182 break;
184 case 'view_admin_global':
185 case 'view_user_global':
186 case 'view_mod_global':
187 $this->permission_dropdown = ($mode == 'view_admin_global') ? array('a_') : (($mode == 'view_user_global') ? array('u_') : array('m_'));
188 $permission_victim = array('usergroup_view');
189 $this->page_title = ($mode == 'view_admin_global') ? 'ACP_VIEW_ADMIN_PERMISSIONS' : (($mode == 'view_user_global') ? 'ACP_VIEW_USER_PERMISSIONS' : 'ACP_VIEW_GLOBAL_MOD_PERMISSIONS');
190 break;
192 case 'view_mod_local':
193 case 'view_forum_local':
194 $this->permission_dropdown = ($mode == 'view_mod_local') ? array('m_') : array('f_');
195 $permission_victim = array('forums', 'usergroup_view');
196 $this->page_title = ($mode == 'view_mod_local') ? 'ACP_VIEW_FORUM_MOD_PERMISSIONS' : 'ACP_VIEW_FORUM_PERMISSIONS';
197 break;
199 default:
200 trigger_error('NO_MODE', E_USER_ERROR);
201 break;
204 phpbb::$template->assign_vars(array(
205 'L_TITLE' => phpbb::$user->lang[$this->page_title],
206 'L_EXPLAIN' => phpbb::$user->lang[$this->page_title . '_EXPLAIN'],
209 // Get permission type
210 $permission_type = request_var('type', $this->permission_dropdown[0]);
212 if (!in_array($permission_type, $this->permission_dropdown))
214 trigger_error(phpbb::$user->lang['WRONG_PERMISSION_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
218 // Handle actions
219 if (strpos($mode, 'setting_') === 0 && $action)
221 switch ($action)
223 case 'delete':
225 if (!check_form_key($form_name))
227 trigger_error(phpbb::$user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
229 // All users/groups selected?
230 $all_users = phpbb_request::is_set_post('all_users');
231 $all_groups = phpbb_request::is_set_post('all_groups');
233 if ($all_users || $all_groups)
235 $items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
237 if ($all_users && sizeof($items['user_ids']))
239 $user_id = $items['user_ids'];
241 else if ($all_groups && sizeof($items['group_ids']))
243 $group_id = $items['group_ids'];
247 if (sizeof($user_id) || sizeof($group_id))
249 $this->remove_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id, $forum_id);
251 else
253 trigger_error(phpbb::$user->lang['NO_USER_GROUP_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
255 break;
257 case 'apply_permissions':
258 if (!phpbb_request::is_set_post('setting'))
260 trigger_error(phpbb::$user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
262 if (!check_form_key($form_name))
264 trigger_error(phpbb::$user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
267 $this->set_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
268 break;
270 case 'apply_all_permissions':
271 if (!phpbb_request::is_set_post('setting'))
273 trigger_error(phpbb::$user->lang['NO_AUTH_SETTING_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
275 if (!check_form_key($form_name))
277 trigger_error(phpbb::$user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
280 $this->set_all_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
281 break;
286 // Setting permissions screen
287 $s_hidden_fields = build_hidden_fields(array(
288 'user_id' => $user_id,
289 'group_id' => $group_id,
290 'forum_id' => $forum_id,
291 'type' => $permission_type)
294 // Go through the screens/options needed and present them in correct order
295 foreach ($permission_victim as $victim)
297 switch ($victim)
299 case 'forum_dropdown':
301 if (sizeof($forum_id))
303 $this->check_existence('forum', $forum_id);
304 continue 2;
307 phpbb::$template->assign_vars(array(
308 'S_SELECT_FORUM' => true,
309 'S_FORUM_OPTIONS' => make_forum_select(false, false, true, false, false),
312 break;
314 case 'forums':
316 if (sizeof($forum_id))
318 $this->check_existence('forum', $forum_id);
319 continue 2;
322 $forum_list = make_forum_select(false, false, true, false, false, false, true);
324 // Build forum options
325 $s_forum_options = '';
326 foreach ($forum_list as $f_id => $f_row)
328 $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>';
331 // Build subforum options
332 $s_subforum_options = $this->build_subforum_options($forum_list);
334 phpbb::$template->assign_vars(array(
335 'S_SELECT_FORUM' => true,
336 'S_FORUM_OPTIONS' => $s_forum_options,
337 'S_SUBFORUM_OPTIONS' => $s_subforum_options,
338 'S_FORUM_ALL' => true,
339 'S_FORUM_MULTIPLE' => true,
342 break;
344 case 'user':
346 if (sizeof($user_id))
348 $this->check_existence('user', $user_id);
349 continue 2;
352 phpbb::$template->assign_vars(array(
353 'S_SELECT_USER' => true,
354 'U_FIND_USERNAME' => append_sid('memberlist', 'mode=searchuser&amp;form=select_victim&amp;field=username&amp;select_single=true'),
357 break;
359 case 'group':
361 if (sizeof($group_id))
363 $this->check_existence('group', $group_id);
364 continue 2;
367 phpbb::$template->assign_vars(array(
368 'S_SELECT_GROUP' => true,
369 'S_GROUP_OPTIONS' => group_select_options(false, false, ((phpbb::$user->is_founder) ? false : 0)),
372 break;
374 case 'usergroup':
375 case 'usergroup_view':
377 $all_users = phpbb_request::is_set_post('all_users');
378 $all_groups = phpbb_request::is_set_post('all_groups');
380 if ((sizeof($user_id) && !$all_users) || (sizeof($group_id) && !$all_groups))
382 if (sizeof($user_id))
384 $this->check_existence('user', $user_id);
387 if (sizeof($group_id))
389 $this->check_existence('group', $group_id);
392 continue 2;
395 // Now we check the users... because the "all"-selection is different here (all defined users/groups)
396 $items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
398 if ($all_users && sizeof($items['user_ids']))
400 $user_id = $items['user_ids'];
401 continue 2;
404 if ($all_groups && sizeof($items['group_ids']))
406 $group_id = $items['group_ids'];
407 continue 2;
410 phpbb::$template->assign_vars(array(
411 'S_SELECT_USERGROUP' => ($victim == 'usergroup') ? true : false,
412 'S_SELECT_USERGROUP_VIEW' => ($victim == 'usergroup_view') ? true : false,
413 'S_DEFINED_USER_OPTIONS' => $items['user_ids_options'],
414 'S_DEFINED_GROUP_OPTIONS' => $items['group_ids_options'],
415 'S_ADD_GROUP_OPTIONS' => group_select_options(false, $items['group_ids'], ((phpbb::$user->is_founder) ? false : 0)),
416 'U_FIND_USERNAME' => append_sid('memberlist', 'mode=searchuser&amp;form=add_user&amp;field=username&amp;select_single=true'),
419 break;
422 // The S_ALLOW_SELECT parameter below is a measure to lower memory usage.
423 // If there are more than 5 forums selected the admin is not able to select all users/groups too.
424 // We need to see if the number of forums can be increased or need to be decreased.
426 phpbb::$template->assign_vars(array(
427 'U_ACTION' => $this->u_action,
428 'ANONYMOUS_USER_ID' => ANONYMOUS,
430 'S_SELECT_VICTIM' => true,
431 'S_ALLOW_ALL_SELECT' => (sizeof($forum_id) > 5) ? false : true,
432 'S_CAN_SELECT_USER' => (phpbb::$acl->acl_get('a_authusers')) ? true : false,
433 'S_CAN_SELECT_GROUP' => (phpbb::$acl->acl_get('a_authgroups')) ? true : false,
434 'S_HIDDEN_FIELDS' => $s_hidden_fields,
437 // Let the forum names being displayed
438 if (sizeof($forum_id))
440 $sql = 'SELECT forum_name
441 FROM ' . FORUMS_TABLE . '
442 WHERE ' . phpbb::$db->sql_in_set('forum_id', $forum_id) . '
443 ORDER BY left_id ASC';
444 $result = phpbb::$db->sql_query($sql);
446 $forum_names = array();
447 while ($row = phpbb::$db->sql_fetchrow($result))
449 $forum_names[] = $row['forum_name'];
451 phpbb::$db->sql_freeresult($result);
453 phpbb::$template->assign_vars(array(
454 'S_FORUM_NAMES' => (sizeof($forum_names)) ? true : false,
455 'FORUM_NAMES' => implode(', ', $forum_names),
459 return;
462 // Do not allow forum_ids being set and no other setting defined (will bog down the server too much)
463 if (sizeof($forum_id) && !sizeof($user_id) && !sizeof($group_id))
465 trigger_error(phpbb::$user->lang['ONLY_FORUM_DEFINED'] . adm_back_link($this->u_action), E_USER_WARNING);
468 phpbb::$template->assign_vars(array(
469 'S_PERMISSION_DROPDOWN' => (sizeof($this->permission_dropdown) > 1) ? $this->build_permission_dropdown($this->permission_dropdown, $permission_type, $permission_scope) : false,
470 'L_PERMISSION_TYPE' => phpbb::$user->lang['ACL_TYPE_' . strtoupper($permission_type)],
472 'U_ACTION' => $this->u_action,
473 'S_HIDDEN_FIELDS' => $s_hidden_fields,
476 if (strpos($mode, 'setting_') === 0)
478 phpbb::$template->assign_vars(array(
479 'S_SETTING_PERMISSIONS' => true,
482 $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, phpbb::ACL_NO);
483 $auth_admin->display_mask('set', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
485 else
487 phpbb::$template->assign_vars(array(
488 'S_VIEWING_PERMISSIONS' => true,
491 $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, phpbb::ACL_NEVER);
492 $auth_admin->display_mask('view', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
497 * Build +subforum options
499 function build_subforum_options($forum_list)
501 $s_options = '';
503 $forum_list = array_merge($forum_list);
505 foreach ($forum_list as $key => $row)
507 if ($row['disabled'])
509 continue;
512 $s_options .= '<option value="' . $row['forum_id'] . '"' . (($row['selected']) ? ' selected="selected"' : '') . '>' . $row['padding'] . $row['forum_name'];
514 // We check if a branch is there...
515 $branch_there = false;
517 foreach (array_slice($forum_list, $key + 1) as $temp_row)
519 if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
521 $branch_there = true;
522 break;
524 continue;
527 if ($branch_there)
529 $s_options .= ' [' . phpbb::$user->lang['PLUS_SUBFORUMS'] . ']';
532 $s_options .= '</option>';
535 return $s_options;
539 * Build dropdown field for changing permission types
541 function build_permission_dropdown($options, $default_option, $permission_scope)
543 $s_dropdown_options = '';
544 foreach ($options as $setting)
546 if (!phpbb::$acl->acl_get('a_' . str_replace('_', '', $setting) . 'auth'))
548 continue;
551 $selected = ($setting == $default_option) ? ' selected="selected"' : '';
552 $l_setting = (isset(phpbb::$user->lang['permission_type'][$permission_scope][$setting])) ? phpbb::$user->lang['permission_type'][$permission_scope][$setting] : phpbb::$user->lang['permission_type'][$setting];
553 $s_dropdown_options .= '<option value="' . $setting . '"' . $selected . '>' . $l_setting . '</option>';
556 return $s_dropdown_options;
560 * Check if selected items exist. Remove not found ids and if empty return error.
562 function check_existence($mode, &$ids)
564 switch ($mode)
566 case 'user':
567 $table = USERS_TABLE;
568 $sql_id = 'user_id';
569 break;
571 case 'group':
572 $table = GROUPS_TABLE;
573 $sql_id = 'group_id';
574 break;
576 case 'forum':
577 $table = FORUMS_TABLE;
578 $sql_id = 'forum_id';
579 break;
582 if (sizeof($ids))
584 $sql = "SELECT $sql_id
585 FROM $table
586 WHERE " . phpbb::$db->sql_in_set($sql_id, $ids);
587 $result = phpbb::$db->sql_query($sql);
589 $ids = array();
590 while ($row = phpbb::$db->sql_fetchrow($result))
592 $ids[] = (int) $row[$sql_id];
594 phpbb::$db->sql_freeresult($result);
597 if (!sizeof($ids))
599 trigger_error(phpbb::$user->lang['SELECTED_' . strtoupper($mode) . '_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
604 * Apply permissions
606 function set_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
608 $psubmit = request_var('psubmit', array(0 => array(0 => 0)));
610 // User or group to be set?
611 $ug_type = (sizeof($user_id)) ? 'user' : 'group';
613 // Check the permission setting again
614 if (!phpbb::$acl->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !phpbb::$acl->acl_get('a_auth' . $ug_type . 's'))
616 trigger_error(phpbb::$user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
619 $ug_id = $forum_id = 0;
621 // We loop through the auth settings defined in our submit
622 list($ug_id, ) = each($psubmit);
623 list($forum_id, ) = each($psubmit[$ug_id]);
625 $auth_settings = phpbb_request::variable('setting', array(0 => array(0 => array('' => 0))), false, phpbb_request::POST);
626 if (!isset($auth_settings[$ug_id][$forum_id]) || !sizeof($auth_settings[$ug_id][$forum_id])))
628 trigger_error('WRONG_PERMISSION_SETTING_FORMAT', E_USER_WARNING);
631 // Do we have a role we want to set?
632 $assigned_role = phpbb_request::variable(array('role', $ug_id, $forum_id), 0, false, phpbb_request::POST));
634 // Do the admin want to set these permissions to other items too?
635 $inherit = request_var('inherit', array(0 => array(0)));
637 $ug_id = array($ug_id);
638 $forum_id = array($forum_id);
640 if (sizeof($inherit))
642 foreach ($inherit as $_ug_id => $forum_id_ary)
644 // Inherit users/groups?
645 if (!in_array($_ug_id, $ug_id))
647 $ug_id[] = $_ug_id;
650 // Inherit forums?
651 $forum_id = array_merge($forum_id, array_keys($forum_id_ary));
655 $forum_id = array_unique($forum_id);
657 // If the auth settings differ from the assigned role, then do not set a role...
658 if ($assigned_role)
660 if (!$this->check_assigned_role($assigned_role, $auth_settings))
662 $assigned_role = 0;
666 // Update the permission set...
667 $auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_settings, $assigned_role);
669 // Do we need to recache the moderator lists?
670 if ($permission_type == 'm_')
672 cache_moderators();
675 // Remove users who are now moderators or admins from everyones foes list
676 if ($permission_type == 'm_' || $permission_type == 'a_')
678 update_foes($group_id, $user_id);
681 $this->log_action($mode, 'add', $permission_type, $ug_type, $ug_id, $forum_id);
683 trigger_error(phpbb::$user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action));
687 * Apply all permissions
689 function set_all_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
691 // User or group to be set?
692 $ug_type = (sizeof($user_id)) ? 'user' : 'group';
694 // Check the permission setting again
695 if (!phpbb::$acl->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !phpbb::$acl->acl_get('a_auth' . $ug_type . 's'))
697 trigger_error(phpbb::$user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
700 $auth_settings = phpbb_request::variable('setting', array(0 => array(0 => array('' => 0))), false, phpbb_request::POST);
701 $auth_roles = phpbb_request::variable('role', array(0 => array(0 => 0)), false, phpbb_request::POST);
702 $ug_ids = $forum_ids = array();
704 // We need to go through the auth settings
705 foreach ($auth_settings as $ug_id => $forum_auth_row)
707 $ug_ids[] = $ug_id;
709 foreach ($forum_auth_row as $forum_id => $auth_options)
711 $forum_ids[] = $forum_id;
713 // Check role...
714 $assigned_role = (isset($auth_roles[$ug_id][$forum_id])) ? $auth_roles[$ug_id][$forum_id] : 0;
716 // If the auth settings differ from the assigned role, then do not set a role...
717 if ($assigned_role)
719 if (!$this->check_assigned_role($assigned_role, $auth_options))
721 $assigned_role = 0;
725 // Update the permission set...
726 $auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_options, $assigned_role, false);
730 $auth_admin->acl_clear_prefetch();
732 // Do we need to recache the moderator lists?
733 if ($permission_type == 'm_')
735 cache_moderators();
738 // Remove users who are now moderators or admins from everyones foes list
739 if ($permission_type == 'm_' || $permission_type == 'a_')
741 update_foes($group_id, $user_id);
744 $this->log_action($mode, 'add', $permission_type, $ug_type, $ug_ids, $forum_ids);
746 trigger_error(phpbb::$user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action . '&amp;forum_id[]=' . implode('&amp;forum_id[]=', $forum_ids)));
750 * Compare auth settings with auth settings from role
751 * returns false if they differ, true if they are equal
753 function check_assigned_role($role_id, &$auth_settings)
755 $sql = 'SELECT o.auth_option, r.auth_setting
756 FROM ' . ACL_OPTIONS_TABLE . ' o, ' . ACL_ROLES_DATA_TABLE . ' r
757 WHERE o.auth_option_id = r.auth_option_id
758 AND r.role_id = ' . $role_id;
759 $result = phpbb::$db->sql_query($sql);
761 $test_auth_settings = array();
762 while ($row = phpbb::$db->sql_fetchrow($result))
764 $test_auth_settings[$row['auth_option']] = $row['auth_setting'];
766 phpbb::$db->sql_freeresult($result);
768 // We need to add any ACL_NO setting from auth_settings to compare correctly
769 foreach ($auth_settings as $option => $setting)
771 if ($setting == phpbb::ACL_NO)
773 $test_auth_settings[$option] = $setting;
777 if (sizeof(array_diff_assoc($auth_settings, $test_auth_settings)))
779 return false;
782 return true;
786 * Remove permissions
788 function remove_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id, &$forum_id)
790 // User or group to be set?
791 $ug_type = (sizeof($user_id)) ? 'user' : 'group';
793 // Check the permission setting again
794 if (!phpbb::$acl->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !phpbb::$acl->acl_get('a_auth' . $ug_type . 's'))
796 trigger_error(phpbb::$user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
799 $auth_admin->acl_delete($ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : false), $permission_type);
801 // Do we need to recache the moderator lists?
802 if ($permission_type == 'm_')
804 cache_moderators();
807 $this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : array(0 => 0)));
809 trigger_error(phpbb::$user->lang['AUTH_UPDATED'] . adm_back_link($this->u_action . '&amp;forum_id[]=' . implode('&amp;forum_id[]=', $forum_id)));
813 * Log permission changes
815 function log_action($mode, $action, $permission_type, $ug_type, $ug_id, $forum_id)
817 if (!is_array($ug_id))
819 $ug_id = array($ug_id);
822 if (!is_array($forum_id))
824 $forum_id = array($forum_id);
827 // Logging ... first grab user or groupnames ...
828 $sql = ($ug_type == 'group') ? 'SELECT group_name as name, group_type FROM ' . GROUPS_TABLE . ' WHERE ' : 'SELECT username as name FROM ' . USERS_TABLE . ' WHERE ';
829 $sql .= phpbb::$db->sql_in_set(($ug_type == 'group') ? 'group_id' : 'user_id', array_map('intval', $ug_id));
830 $result = phpbb::$db->sql_query($sql);
832 $l_ug_list = '';
833 while ($row = phpbb::$db->sql_fetchrow($result))
835 $l_ug_list .= (($l_ug_list != '') ? ', ' : '') . ((isset($row['group_type']) && $row['group_type'] == GROUP_SPECIAL) ? '<span class="sep">' . phpbb::$user->lang['G_' . $row['name']] . '</span>' : $row['name']);
837 phpbb::$db->sql_freeresult($result);
839 $mode = str_replace('setting_', '', $mode);
841 if ($forum_id[0] == 0)
843 add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_ug_list);
845 else
847 // Grab the forum details if non-zero forum_id
848 $sql = 'SELECT forum_name
849 FROM ' . FORUMS_TABLE . '
850 WHERE ' . phpbb::$db->sql_in_set('forum_id', $forum_id);
851 $result = phpbb::$db->sql_query($sql);
853 $l_forum_list = '';
854 while ($row = phpbb::$db->sql_fetchrow($result))
856 $l_forum_list .= (($l_forum_list != '') ? ', ' : '') . $row['forum_name'];
858 phpbb::$db->sql_freeresult($result);
860 add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_forum_list, $l_ug_list);
865 * Display a complete trace tree for the selected permission to determine where settings are set/unset
867 function permission_trace($user_id, $forum_id, $permission)
869 if ($user_id != phpbb::$user->data['user_id'])
871 $sql = 'SELECT user_id, username, user_permissions, user_type
872 FROM ' . USERS_TABLE . '
873 WHERE user_id = ' . $user_id;
874 $result = phpbb::$db->sql_query($sql);
875 $userdata = phpbb::$db->sql_fetchrow($result);
876 phpbb::$db->sql_freeresult($result);
878 else
880 $userdata = phpbb::$user->data;
883 if (!$userdata)
885 trigger_error('NO_USERS', E_USER_ERROR);
888 $forum_name = false;
890 if ($forum_id)
892 $sql = 'SELECT forum_name
893 FROM ' . FORUMS_TABLE . "
894 WHERE forum_id = $forum_id";
895 $result = phpbb::$db->sql_query($sql, 3600);
896 $forum_name = phpbb::$db->sql_fetchfield('forum_name');
897 phpbb::$db->sql_freeresult($result);
900 $back = request_var('back', 0);
902 phpbb::$template->assign_vars(array(
903 'PERMISSION' => phpbb::$user->lang['acl_' . $permission]['lang'],
904 'PERMISSION_USERNAME' => $userdata['username'],
905 'FORUM_NAME' => $forum_name,
907 'S_GLOBAL_TRACE' => ($forum_id) ? false : true,
909 'U_BACK' => ($back) ? build_url(array('f', 'back')) . "&amp;f=$back" : '',
912 phpbb::$template->assign_block_vars('trace', array(
913 'WHO' => phpbb::$user->lang['DEFAULT'],
914 'INFORMATION' => phpbb::$user->lang['TRACE_DEFAULT'],
916 'S_SETTING_NO' => true,
917 'S_TOTAL_NO' => true,
920 $sql = 'SELECT DISTINCT g.group_name, g.group_id, g.group_type
921 FROM ' . GROUPS_TABLE . ' g
922 LEFT JOIN ' . USER_GROUP_TABLE . ' ug ON (ug.group_id = g.group_id)
923 WHERE ug.user_id = ' . $user_id . '
924 AND ug.user_pending = 0
925 ORDER BY g.group_type DESC, g.group_id DESC';
926 $result = phpbb::$db->sql_query($sql);
928 $groups = array();
929 while ($row = phpbb::$db->sql_fetchrow($result))
931 $groups[$row['group_id']] = array(
932 'auth_setting' => phpbb::ACL_NO,
933 'group_name' => ($row['group_type'] == GROUP_SPECIAL) ? phpbb::$user->lang['G_' . $row['group_name']] : $row['group_name']
936 phpbb::$db->sql_freeresult($result);
938 $total = phpbb::ACL_NO;
939 $add_key = (($forum_id) ? '_LOCAL' : '');
941 if (sizeof($groups))
943 // Get group auth settings
944 $hold_ary = phpbb::$acl->acl_group_raw_data(array_keys($groups), $permission, $forum_id);
946 foreach ($hold_ary as $group_id => $forum_ary)
948 $groups[$group_id]['auth_setting'] = $hold_ary[$group_id][$forum_id][$permission];
950 unset($hold_ary);
952 foreach ($groups as $id => $row)
954 switch ($row['auth_setting'])
956 case phpbb::ACL_NO:
957 $information = phpbb::$user->lang['TRACE_GROUP_NO' . $add_key];
958 break;
960 case phpbb::ACL_YES:
961 $information = ($total == phpbb::ACL_YES) ? phpbb::$user->lang['TRACE_GROUP_YES_TOTAL_YES' . $add_key] : (($total == phpbb::ACL_NEVER) ? phpbb::$user->lang['TRACE_GROUP_YES_TOTAL_NEVER' . $add_key] : phpbb::$user->lang['TRACE_GROUP_YES_TOTAL_NO' . $add_key]);
962 $total = ($total == phpbb::ACL_NO) ? phpbb::ACL_YES : $total;
963 break;
965 case phpbb::ACL_NEVER:
966 $information = ($total == phpbb::ACL_YES) ? phpbb::$user->lang['TRACE_GROUP_NEVER_TOTAL_YES' . $add_key] : (($total == phpbb::ACL_NEVER) ? phpbb::$user->lang['TRACE_GROUP_NEVER_TOTAL_NEVER' . $add_key] : phpbb::$user->lang['TRACE_GROUP_NEVER_TOTAL_NO' . $add_key]);
967 $total = phpbb::ACL_NEVER;
968 break;
971 phpbb::$template->assign_block_vars('trace', array(
972 'WHO' => $row['group_name'],
973 'INFORMATION' => $information,
975 'S_SETTING_NO' => ($row['auth_setting'] == phpbb::ACL_NO) ? true : false,
976 'S_SETTING_YES' => ($row['auth_setting'] == phpbb::ACL_YES) ? true : false,
977 'S_SETTING_NEVER' => ($row['auth_setting'] == phpbb::ACL_NEVER) ? true : false,
978 'S_TOTAL_NO' => ($total == phpbb::ACL_NO) ? true : false,
979 'S_TOTAL_YES' => ($total == phpbb::ACL_YES) ? true : false,
980 'S_TOTAL_NEVER' => ($total == phpbb::ACL_NEVER) ? true : false,
985 // Get user specific permission... globally or for this forum
986 $hold_ary = phpbb::$acl->acl_user_raw_data($user_id, $permission, $forum_id);
987 $auth_setting = (!sizeof($hold_ary)) ? phpbb::ACL_NO : $hold_ary[$user_id][$forum_id][$permission];
989 switch ($auth_setting)
991 case phpbb::ACL_NO:
992 $information = ($total == phpbb::ACL_NO) ? phpbb::$user->lang['TRACE_USER_NO_TOTAL_NO' . $add_key] : phpbb::$user->lang['TRACE_USER_KEPT' . $add_key];
993 $total = ($total == phpbb::ACL_NO) ? phpbb::ACL_NEVER : $total;
994 break;
996 case phpbb::ACL_YES:
997 $information = ($total == phpbb::ACL_YES) ? phpbb::$user->lang['TRACE_USER_YES_TOTAL_YES' . $add_key] : (($total == phpbb::ACL_NEVER) ? phpbb::$user->lang['TRACE_USER_YES_TOTAL_NEVER' . $add_key] : phpbb::$user->lang['TRACE_USER_YES_TOTAL_NO' . $add_key]);
998 $total = ($total == phpbb::ACL_NO) ? phpbb::ACL_YES : $total;
999 break;
1001 case phpbb::ACL_NEVER:
1002 $information = ($total == phpbb::ACL_YES) ? phpbb::$user->lang['TRACE_USER_NEVER_TOTAL_YES' . $add_key] : (($total == phpbb::ACL_NEVER) ? phpbb::$user->lang['TRACE_USER_NEVER_TOTAL_NEVER' . $add_key] : phpbb::$user->lang['TRACE_USER_NEVER_TOTAL_NO' . $add_key]);
1003 $total = phpbb::ACL_NEVER;
1004 break;
1007 phpbb::$template->assign_block_vars('trace', array(
1008 'WHO' => $userdata['username'],
1009 'INFORMATION' => $information,
1011 'S_SETTING_NO' => ($auth_setting == phpbb::ACL_NO) ? true : false,
1012 'S_SETTING_YES' => ($auth_setting == phpbb::ACL_YES) ? true : false,
1013 'S_SETTING_NEVER' => ($auth_setting == phpbb::ACL_NEVER) ? true : false,
1014 'S_TOTAL_NO' => false,
1015 'S_TOTAL_YES' => ($total == phpbb::ACL_YES) ? true : false,
1016 'S_TOTAL_NEVER' => ($total == phpbb::ACL_NEVER) ? true : false,
1019 if ($forum_id != 0 && isset(phpbb::$acl->acl_options['global'][$permission]))
1021 if ($user_id != phpbb::$user->data['user_id'])
1023 $auth2 = new auth();
1024 $auth2->acl($userdata);
1025 $auth_setting = $auth2->acl_get($permission);
1027 else
1029 $auth_setting = phpbb::$acl->acl_get($permission);
1032 if ($auth_setting)
1034 $information = ($total == phpbb::ACL_YES) ? phpbb::$user->lang['TRACE_USER_GLOBAL_YES_TOTAL_YES'] : phpbb::$user->lang['TRACE_USER_GLOBAL_YES_TOTAL_NEVER'];
1035 $total = phpbb::ACL_YES;
1037 else
1039 $information = phpbb::$user->lang['TRACE_USER_GLOBAL_NEVER_TOTAL_KEPT'];
1042 // If there is no auth information we do not need to worry the user by showing non-relevant data.
1043 if ($auth_setting)
1045 phpbb::$template->assign_block_vars('trace', array(
1046 'WHO' => sprintf(phpbb::$user->lang['TRACE_GLOBAL_SETTING'], $userdata['username']),
1047 'INFORMATION' => sprintf($information, '<a href="' . $this->u_action . "&amp;u=$user_id&amp;f=0&amp;auth=$permission&amp;back=$forum_id\">", '</a>'),
1049 'S_SETTING_NO' => false,
1050 'S_SETTING_YES' => $auth_setting,
1051 'S_SETTING_NEVER' => !$auth_setting,
1052 'S_TOTAL_NO' => false,
1053 'S_TOTAL_YES' => ($total == phpbb::ACL_YES) ? true : false,
1054 'S_TOTAL_NEVER' => ($total == phpbb::ACL_NEVER) ? true : false,
1059 // Take founder status into account, overwriting the default values
1060 if ($userdata['user_type'] == phpbb::USER_FOUNDER && strpos($permission, 'a_') === 0)
1062 phpbb::$template->assign_block_vars('trace', array(
1063 'WHO' => $userdata['username'],
1064 'INFORMATION' => phpbb::$user->lang['TRACE_USER_FOUNDER'],
1066 'S_SETTING_NO' => ($auth_setting == phpbb::ACL_NO) ? true : false,
1067 'S_SETTING_YES' => ($auth_setting == phpbb::ACL_YES) ? true : false,
1068 'S_SETTING_NEVER' => ($auth_setting == phpbb::ACL_NEVER) ? true : false,
1069 'S_TOTAL_NO' => false,
1070 'S_TOTAL_YES' => true,
1071 'S_TOTAL_NEVER' => false,
1074 $total = phpbb::ACL_YES;
1077 // Total value...
1078 phpbb::$template->assign_vars(array(
1079 'S_RESULT_NO' => ($total == phpbb::ACL_NO) ? true : false,
1080 'S_RESULT_YES' => ($total == phpbb::ACL_YES) ? true : false,
1081 'S_RESULT_NEVER' => ($total == phpbb::ACL_NEVER) ? true : false,
1086 * Get already assigned users/groups
1088 function retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type)
1090 $sql_forum_id = ($permission_scope == 'global') ? 'AND a.forum_id = 0' : ((sizeof($forum_id)) ? 'AND ' . phpbb::$db->sql_in_set('a.forum_id', $forum_id) : 'AND a.forum_id <> 0');
1092 // Permission options are only able to be a permission set... therefore we will pre-fetch the possible options and also the possible roles
1093 $option_ids = $role_ids = array();
1095 $sql = 'SELECT auth_option_id
1096 FROM ' . ACL_OPTIONS_TABLE . '
1097 WHERE auth_option ' . phpbb::$db->sql_like_expression($permission_type . phpbb::$db->any_char);
1098 $result = phpbb::$db->sql_query($sql);
1100 while ($row = phpbb::$db->sql_fetchrow($result))
1102 $option_ids[] = (int) $row['auth_option_id'];
1104 phpbb::$db->sql_freeresult($result);
1106 if (sizeof($option_ids))
1108 $sql = 'SELECT DISTINCT role_id
1109 FROM ' . ACL_ROLES_DATA_TABLE . '
1110 WHERE ' . phpbb::$db->sql_in_set('auth_option_id', $option_ids);
1111 $result = phpbb::$db->sql_query($sql);
1113 while ($row = phpbb::$db->sql_fetchrow($result))
1115 $role_ids[] = (int) $row['role_id'];
1117 phpbb::$db->sql_freeresult($result);
1120 if (sizeof($option_ids) && sizeof($role_ids))
1122 $sql_where = 'AND (' . phpbb::$db->sql_in_set('a.auth_option_id', $option_ids) . ' OR ' . phpbb::$db->sql_in_set('a.auth_role_id', $role_ids) . ')';
1124 else if (sizeof($role_ids))
1126 $sql_where = 'AND ' . phpbb::$db->sql_in_set('a.auth_role_id', $role_ids);
1128 else if (sizeof($option_ids))
1130 $sql_where = 'AND ' . phpbb::$db->sql_in_set('a.auth_option_id', $option_ids);
1133 // Not ideal, due to the filesort, non-use of indexes, etc.
1134 $sql = 'SELECT DISTINCT u.user_id, u.username, u.username_clean, u.user_regdate
1135 FROM ' . USERS_TABLE . ' u, ' . ACL_USERS_TABLE . " a
1136 WHERE u.user_id = a.user_id
1137 $sql_forum_id
1138 $sql_where
1139 ORDER BY u.username_clean, u.user_regdate ASC";
1140 $result = phpbb::$db->sql_query($sql);
1142 $s_defined_user_options = '';
1143 $defined_user_ids = array();
1144 while ($row = phpbb::$db->sql_fetchrow($result))
1146 $s_defined_user_options .= '<option value="' . $row['user_id'] . '">' . $row['username'] . '</option>';
1147 $defined_user_ids[] = $row['user_id'];
1149 phpbb::$db->sql_freeresult($result);
1151 $sql = 'SELECT DISTINCT g.group_type, g.group_name, g.group_id
1152 FROM ' . GROUPS_TABLE . ' g, ' . ACL_GROUPS_TABLE . " a
1153 WHERE g.group_id = a.group_id
1154 $sql_forum_id
1155 $sql_where
1156 ORDER BY g.group_type DESC, g.group_name ASC";
1157 $result = phpbb::$db->sql_query($sql);
1159 $s_defined_group_options = '';
1160 $defined_group_ids = array();
1161 while ($row = phpbb::$db->sql_fetchrow($result))
1163 $s_defined_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '">' . (($row['group_type'] == GROUP_SPECIAL) ? phpbb::$user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
1164 $defined_group_ids[] = $row['group_id'];
1166 phpbb::$db->sql_freeresult($result);
1168 return array(
1169 'group_ids' => $defined_group_ids,
1170 'group_ids_options' => $s_defined_group_options,
1171 'user_ids' => $defined_user_ids,
1172 'user_ids_options' => $s_defined_user_options