6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * ACP Permission/Auth class
23 class auth_admin extends auth
32 if (($this->acl_options = $cache->get('_acl_options')) === false)
34 $sql = 'SELECT auth_option_id, auth_option, is_global, is_local
35 FROM ' . ACL_OPTIONS_TABLE . '
36 ORDER BY auth_option_id';
37 $result = $db->sql_query($sql);
40 $this->acl_options = array();
41 while ($row = $db->sql_fetchrow($result))
43 if ($row['is_global'])
45 $this->acl_options['global'][$row['auth_option']] = $global++;
50 $this->acl_options['local'][$row['auth_option']] = $local++;
53 $this->acl_options['id'][$row['auth_option']] = (int) $row['auth_option_id'];
54 $this->acl_options['option'][(int) $row['auth_option_id']] = $row['auth_option'];
56 $db->sql_freeresult($result);
58 $cache->put('_acl_options', $this->acl_options);
64 * This function only supports getting permissions of one type (for example a_)
66 * @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
67 * @param mixed $user_id user ids to search for (a user_id or a group_id has to be specified at least)
68 * @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)
69 * @param mixed $forum_id forum_ids to search for. Defining a forum id also means getting local settings
70 * @param string $auth_option the auth_option defines the permission setting to look for (a_ for example)
71 * @param local|global $scope the scope defines the permission scope. If local, a forum_id is additionally required
72 * @param ACL_NEVER|ACL_NO|ACL_YES $acl_fill defines the mode those permissions not set are getting filled with
74 function get_mask($mode, $user_id = false, $group_id = false, $forum_id = false, $auth_option = false, $scope = false, $acl_fill = ACL_NEVER)
79 $view_user_mask = ($mode == 'view' && $group_id === false) ? true : false;
81 if ($auth_option === false || $scope === false)
86 $acl_user_function = ($mode == 'set') ? 'acl_user_raw_data' : 'acl_raw_data';
90 if ($forum_id !== false)
92 $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);
96 $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);
100 // Make sure hold_ary is filled with every setting (prevents missing forums/users/groups)
101 $ug_id = ($group_id !== false) ? ((!is_array($group_id)) ? array($group_id) : $group_id) : ((!is_array($user_id)) ? array($user_id) : $user_id);
102 $forum_ids = ($forum_id !== false) ? ((!is_array($forum_id)) ? array($forum_id) : $forum_id) : (($scope == 'global') ? array(0) : array());
104 // Only those options we need
105 $compare_options = array_diff(preg_replace('/^((?!' . $auth_option . ').+)|(' . $auth_option . ')$/', '', array_keys($this->acl_options[$scope])), array(''));
107 // If forum_ids is false and the scope is local we actually want to have all forums within the array
108 if ($scope == 'local' && !sizeof($forum_ids))
110 $sql = 'SELECT forum_id
111 FROM ' . FORUMS_TABLE;
112 $result = $db->sql_query($sql, 120);
114 while ($row = $db->sql_fetchrow($result))
116 $forum_ids[] = (int) $row['forum_id'];
118 $db->sql_freeresult($result);
125 $sql = 'SELECT user_id, user_permissions, user_type
126 FROM ' . USERS_TABLE . '
127 WHERE ' . $db->sql_in_set('user_id', $ug_id);
128 $result = $db->sql_query($sql);
130 while ($userdata = $db->sql_fetchrow($result))
132 if ($user->data['user_id'] != $userdata['user_id'])
135 $auth2->acl($userdata);
144 $hold_ary[$userdata['user_id']] = array();
145 foreach ($forum_ids as $f_id)
147 $hold_ary[$userdata['user_id']][$f_id] = array();
148 foreach ($compare_options as $option)
150 $hold_ary[$userdata['user_id']][$f_id][$option] = $auth2->acl_get($option, $f_id);
154 $db->sql_freeresult($result);
160 foreach ($ug_id as $_id)
162 if (!isset($hold_ary[$_id]))
164 $hold_ary[$_id] = array();
167 foreach ($forum_ids as $f_id)
169 if (!isset($hold_ary[$_id][$f_id]))
171 $hold_ary[$_id][$f_id] = array();
176 // Now, we need to fill the gaps with $acl_fill. ;)
178 // Now switch back to keys
179 if (sizeof($compare_options))
181 $compare_options = array_combine($compare_options, array_fill(1, sizeof($compare_options), $acl_fill));
184 // Defining the user-function here to save some memory
185 $return_acl_fill = create_function('$value', 'return ' . $acl_fill . ';');
187 // Actually fill the gaps
188 if (sizeof($hold_ary))
190 foreach ($hold_ary as $ug_id => $row)
192 foreach ($row as $id => $options)
194 // Do not include the global auth_option
195 unset($options[$auth_option]);
197 // Not a "fine" solution, but at all it's a 1-dimensional
198 // array_diff_key function filling the resulting array values with zeros
199 // The differences get merged into $hold_ary (all permissions having $acl_fill set)
200 $hold_ary[$ug_id][$id] = array_merge($options,
202 array_map($return_acl_fill,
205 array_keys($compare_options), array_keys($options)
215 $hold_ary[($group_id !== false) ? $group_id : $user_id][(int) $forum_id] = $compare_options;
222 * Get permission mask for roles
223 * This function only supports getting masks for one role
225 function get_role_mask($role_id)
231 // Get users having this role set...
232 $sql = 'SELECT user_id, forum_id
233 FROM ' . ACL_USERS_TABLE . '
234 WHERE auth_role_id = ' . $role_id . '
236 $result = $db->sql_query($sql);
238 while ($row = $db->sql_fetchrow($result))
240 $hold_ary[$row['forum_id']]['users'][] = $row['user_id'];
242 $db->sql_freeresult($result);
244 // Now grab groups...
245 $sql = 'SELECT group_id, forum_id
246 FROM ' . ACL_GROUPS_TABLE . '
247 WHERE auth_role_id = ' . $role_id . '
249 $result = $db->sql_query($sql);
251 while ($row = $db->sql_fetchrow($result))
253 $hold_ary[$row['forum_id']]['groups'][] = $row['group_id'];
255 $db->sql_freeresult($result);
261 * Display permission mask (assign to template)
263 function display_mask($mode, $permission_type, &$hold_ary, $user_mode = 'user', $local = false, $group_display = true)
265 global $template, $user, $db, $phpbb_root_path, $phpEx;
267 // Define names for template loops, might be able to be set
268 $tpl_pmask = 'p_mask';
269 $tpl_fmask = 'f_mask';
270 $tpl_category = 'category';
273 $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);
275 // Allow trace for viewing permissions and in user mode
276 $show_trace = ($mode == 'view' && $user_mode == 'user') ? true : false;
279 if ($user_mode == 'user')
281 $sql = 'SELECT user_id as ug_id, username as ug_name
282 FROM ' . USERS_TABLE . '
283 WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary)) . '
284 ORDER BY username_clean ASC';
288 $sql = 'SELECT group_id as ug_id, group_name as ug_name, group_type
289 FROM ' . GROUPS_TABLE . '
290 WHERE ' . $db->sql_in_set('group_id', array_keys($hold_ary)) . '
291 ORDER BY group_type DESC, group_name ASC';
293 $result = $db->sql_query($sql);
295 $ug_names_ary = array();
296 while ($row = $db->sql_fetchrow($result))
298 $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']);
300 $db->sql_freeresult($result);
303 $forum_ids = array();
304 foreach ($hold_ary as $ug_id => $row)
306 $forum_ids = array_merge($forum_ids, array_keys($row));
308 $forum_ids = array_unique($forum_ids);
310 $forum_names_ary = array();
313 $forum_names_ary = make_forum_select(false, false, true, false, false, false, true);
315 // Remove the disabled ones, since we do not create an option field here...
316 foreach ($forum_names_ary as $key => $value)
318 if (!$value['disabled'])
322 unset($forum_names_ary[$key]);
327 $forum_names_ary[0] = $l_acl_type;
330 // Get available roles
332 FROM ' . ACL_ROLES_TABLE . "
333 WHERE role_type = '" . $db->sql_escape($permission_type) . "'
334 ORDER BY role_order ASC";
335 $result = $db->sql_query($sql);
338 while ($row = $db->sql_fetchrow($result))
340 $roles[$row['role_id']] = $row;
342 $db->sql_freeresult($result);
344 $cur_roles = $this->acl_role_data($user_mode, $permission_type, array_keys($hold_ary));
346 // Build js roles array (role data assignments)
347 $s_role_js_array = '';
351 $s_role_js_array = array();
353 // Make sure every role (even if empty) has its array defined
354 foreach ($roles as $_role_id => $null)
356 $s_role_js_array[$_role_id] = "\n" . 'role_options[' . $_role_id . '] = new Array();' . "\n";
359 $sql = 'SELECT r.role_id, o.auth_option, r.auth_setting
360 FROM ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' o
361 WHERE o.auth_option_id = r.auth_option_id
362 AND ' . $db->sql_in_set('r.role_id', array_keys($roles));
363 $result = $db->sql_query($sql);
365 while ($row = $db->sql_fetchrow($result))
367 $flag = substr($row['auth_option'], 0, strpos($row['auth_option'], '_') + 1);
368 if ($flag == $row['auth_option'])
373 $s_role_js_array[$row['role_id']] .= 'role_options[' . $row['role_id'] . '][\'' . addslashes($row['auth_option']) . '\'] = ' . $row['auth_setting'] . '; ';
375 $db->sql_freeresult($result);
377 $s_role_js_array = implode('', $s_role_js_array);
380 $template->assign_var('S_ROLE_JS_ARRAY', $s_role_js_array);
381 unset($s_role_js_array);
383 // Now obtain memberships
384 $user_groups_default = $user_groups_custom = array();
385 if ($user_mode == 'user' && $group_display)
387 $sql = 'SELECT group_id, group_name, group_type
388 FROM ' . GROUPS_TABLE . '
389 ORDER BY group_type DESC, group_name ASC';
390 $result = $db->sql_query($sql);
393 while ($row = $db->sql_fetchrow($result))
395 $groups[$row['group_id']] = $row;
397 $db->sql_freeresult($result);
399 $memberships = group_memberships(false, array_keys($hold_ary), false);
401 // User is not a member of any group? Bad admin, bad bad admin...
404 foreach ($memberships as $row)
406 if ($groups[$row['group_id']]['group_type'] == GROUP_SPECIAL)
408 $user_groups_default[$row['user_id']][] = $user->lang['G_' . $groups[$row['group_id']]['group_name']];
412 $user_groups_custom[$row['user_id']][] = $groups[$row['group_id']]['group_name'];
416 unset($memberships, $groups);
419 // If we only have one forum id to display or being in local mode and more than one user/group to display,
420 // we switch the complete interface to group by user/usergroup instead of grouping by forum
421 // To achieve this, we need to switch the array a bit
422 if (sizeof($forum_ids) == 1 || ($local && sizeof($ug_names_ary) > 1))
424 $hold_ary_temp = $hold_ary;
426 foreach ($hold_ary_temp as $ug_id => $row)
428 foreach ($forum_names_ary as $forum_id => $forum_row)
430 if (isset($row[$forum_id]))
432 $hold_ary[$forum_id][$ug_id] = $row[$forum_id];
436 unset($hold_ary_temp);
438 foreach ($hold_ary as $forum_id => $forum_array)
440 $content_array = $categories = array();
441 $this->build_permission_array($hold_ary[$forum_id], $content_array, $categories, array_keys($ug_names_ary));
443 $template->assign_block_vars($tpl_pmask, array(
444 'NAME' => ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
445 'PADDING' => ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
447 'CATEGORIES' => implode('</th><th>', $categories),
449 'L_ACL_TYPE' => $l_acl_type,
451 'S_LOCAL' => ($local) ? true : false,
452 'S_GLOBAL' => (!$local) ? true : false,
453 'S_NUM_CATS' => sizeof($categories),
454 'S_VIEW' => ($mode == 'view') ? true : false,
455 'S_NUM_OBJECTS' => sizeof($content_array),
456 'S_USER_MODE' => ($user_mode == 'user') ? true : false,
457 'S_GROUP_MODE' => ($user_mode == 'group') ? true : false)
460 @reset($content_array);
461 while (list($ug_id, $ug_array) = each($content_array))
463 // Build role dropdown options
464 $current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
466 $s_role_options = '';
469 while (list($role_id, $role_row) = each($roles))
471 $role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
472 $role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
474 $title = ($role_description) ? ' title="' . $role_description . '"' : '';
475 $s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
480 $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;
483 if (!$current_role_id && $mode != 'view')
485 $s_custom_permissions = false;
487 foreach ($ug_array as $key => $value)
489 if ($value['S_NEVER'] || $value['S_YES'])
491 $s_custom_permissions = true;
498 $s_custom_permissions = false;
501 $template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
502 'NAME' => $ug_names_ary[$ug_id],
503 'S_ROLE_OPTIONS' => $s_role_options,
505 'S_CUSTOM' => $s_custom_permissions,
506 'FORUM_ID' => $forum_id)
509 $this->assign_cat_array($ug_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
511 unset($content_array[$ug_id]);
514 unset($hold_ary[$forum_id]);
519 foreach ($ug_names_ary as $ug_id => $ug_name)
521 if (!isset($hold_ary[$ug_id]))
526 $content_array = $categories = array();
527 $this->build_permission_array($hold_ary[$ug_id], $content_array, $categories, array_keys($forum_names_ary));
529 $template->assign_block_vars($tpl_pmask, array(
531 'CATEGORIES' => implode('</th><th>', $categories),
533 'USER_GROUPS_DEFAULT' => ($user_mode == 'user' && isset($user_groups_default[$ug_id]) && sizeof($user_groups_default[$ug_id])) ? implode(', ', $user_groups_default[$ug_id]) : '',
534 'USER_GROUPS_CUSTOM' => ($user_mode == 'user' && isset($user_groups_custom[$ug_id]) && sizeof($user_groups_custom[$ug_id])) ? implode(', ', $user_groups_custom[$ug_id]) : '',
535 'L_ACL_TYPE' => $l_acl_type,
537 'S_LOCAL' => ($local) ? true : false,
538 'S_GLOBAL' => (!$local) ? true : false,
539 'S_NUM_CATS' => sizeof($categories),
540 'S_VIEW' => ($mode == 'view') ? true : false,
541 'S_NUM_OBJECTS' => sizeof($content_array),
542 'S_USER_MODE' => ($user_mode == 'user') ? true : false,
543 'S_GROUP_MODE' => ($user_mode == 'group') ? true : false)
546 @reset($content_array);
547 while (list($forum_id, $forum_array) = each($content_array))
549 // Build role dropdown options
550 $current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
552 $s_role_options = '';
555 while (list($role_id, $role_row) = each($roles))
557 $role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
558 $role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
560 $title = ($role_description) ? ' title="' . $role_description . '"' : '';
561 $s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
566 $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;
569 if (!$current_role_id && $mode != 'view')
571 $s_custom_permissions = false;
573 foreach ($forum_array as $key => $value)
575 if ($value['S_NEVER'] || $value['S_YES'])
577 $s_custom_permissions = true;
584 $s_custom_permissions = false;
587 $template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
588 'NAME' => ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
589 'PADDING' => ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
590 'S_ROLE_OPTIONS' => $s_role_options,
591 'S_CUSTOM' => $s_custom_permissions,
593 'FORUM_ID' => $forum_id)
596 $this->assign_cat_array($forum_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
599 unset($hold_ary[$ug_id], $ug_names_ary[$ug_id]);
605 * Display permission mask for roles
607 function display_role_mask(&$hold_ary)
609 global $db, $template, $user, $phpbb_root_path, $phpbb_admin_path, $phpEx;
611 if (!sizeof($hold_ary))
617 $sql = 'SELECT forum_id, forum_name
618 FROM ' . FORUMS_TABLE . '
619 WHERE ' . $db->sql_in_set('forum_id', array_keys($hold_ary)) . '
621 $result = $db->sql_query($sql);
623 // If the role is used globally, then reflect that
624 $forum_names = (isset($hold_ary[0])) ? array(0 => '') : array();
625 while ($row = $db->sql_fetchrow($result))
627 $forum_names[$row['forum_id']] = $row['forum_name'];
629 $db->sql_freeresult($result);
631 foreach ($forum_names as $forum_id => $forum_name)
633 $auth_ary = $hold_ary[$forum_id];
635 $template->assign_block_vars('role_mask', array(
636 'NAME' => ($forum_id == 0) ? $user->lang['GLOBAL_MASK'] : $forum_name,
637 'FORUM_ID' => $forum_id)
640 if (isset($auth_ary['users']) && sizeof($auth_ary['users']))
642 $sql = 'SELECT user_id, username
643 FROM ' . USERS_TABLE . '
644 WHERE ' . $db->sql_in_set('user_id', $auth_ary['users']) . '
645 ORDER BY username_clean ASC';
646 $result = $db->sql_query($sql);
648 while ($row = $db->sql_fetchrow($result))
650 $template->assign_block_vars('role_mask.users', array(
651 'USER_ID' => $row['user_id'],
652 'USERNAME' => $row['username'],
653 'U_PROFILE' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&u={$row['user_id']}"))
656 $db->sql_freeresult($result);
659 if (isset($auth_ary['groups']) && sizeof($auth_ary['groups']))
661 $sql = 'SELECT group_id, group_name, group_type
662 FROM ' . GROUPS_TABLE . '
663 WHERE ' . $db->sql_in_set('group_id', $auth_ary['groups']) . '
664 ORDER BY group_type ASC, group_name';
665 $result = $db->sql_query($sql);
667 while ($row = $db->sql_fetchrow($result))
669 $template->assign_block_vars('role_mask.groups', array(
670 'GROUP_ID' => $row['group_id'],
671 'GROUP_NAME' => ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'],
672 'U_PROFILE' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=group&g={$row['group_id']}"))
675 $db->sql_freeresult($result);
681 * NOTE: this function is not in use atm
682 * Add a new option to the list ... $options is a hash of form ->
684 * 'local' => array('option1', 'option2', ...),
685 * 'global' => array('optionA', 'optionB', ...)
688 function acl_add_option($options)
692 if (!is_array($options))
697 $cur_options = array();
699 // Determine current options
700 $sql = 'SELECT auth_option, is_global, is_local
701 FROM ' . ACL_OPTIONS_TABLE . '
702 ORDER BY auth_option_id';
703 $result = $db->sql_query($sql);
705 while ($row = $db->sql_fetchrow($result))
707 $cur_options[$row['auth_option']] = ($row['is_global'] && $row['is_local']) ? 'both' : (($row['is_global']) ? 'global' : 'local');
709 $db->sql_freeresult($result);
711 // Here we need to insert new options ... this requires discovering whether
712 // an options is global, local or both and whether we need to add an permission
714 $new_options = array('local' => array(), 'global' => array());
716 foreach ($options as $type => $option_ary)
718 $option_ary = array_unique($option_ary);
720 foreach ($option_ary as $option_value)
722 $new_options[$type][] = $option_value;
724 $flag = substr($option_value, 0, strpos($option_value, '_') + 1);
726 if (!in_array($flag, $new_options[$type]))
728 $new_options[$type][] = $flag;
735 $options['local'] = array_diff($new_options['local'], $new_options['global']);
736 $options['global'] = array_diff($new_options['global'], $new_options['local']);
737 $options['both'] = array_intersect($new_options['local'], $new_options['global']);
739 // Now check which options to add/update
740 $add_options = $update_options = array();
742 // First local ones...
743 foreach ($options as $type => $option_ary)
745 foreach ($option_ary as $option)
747 if (!isset($cur_options[$option]))
749 $add_options[] = array(
750 'auth_option' => (string) $option,
751 'is_global' => ($type == 'global' || $type == 'both') ? 1 : 0,
752 'is_local' => ($type == 'local' || $type == 'both') ? 1 : 0
758 // Else, update existing entry if it is changed...
759 if ($type === $cur_options[$option])
764 // New type is always both:
765 // If is now both, we set both.
766 // If it was global the new one is local and we need to set it to both
767 // If it was local the new one is global and we need to set it to both
768 $update_options[] = $option;
772 if (!empty($add_options))
774 $db->sql_multi_insert(ACL_OPTIONS_TABLE, $add_options);
777 if (!empty($update_options))
779 $sql = 'UPDATE ' . ACL_OPTIONS_TABLE . '
780 SET is_global = 1, is_local = 1
781 WHERE ' . $db->sql_in_set('auth_option', $update_options);
782 $db->sql_query($sql);
785 $cache->destroy('_acl_options');
786 $this->acl_clear_prefetch();
788 // Because we just changed the options and also purged the options cache, we instantly update/regenerate it for later calls to succeed.
789 $this->acl_options = array();
796 * Set a user or group ACL record
798 function acl_set($ug_type, $forum_id, $ug_id, $auth, $role_id = 0, $clear_prefetch = true)
802 // One or more forums
803 if (!is_array($forum_id))
805 $forum_id = array($forum_id);
809 if (!is_array($ug_id))
811 $ug_id = array($ug_id);
814 $ug_id_sql = $db->sql_in_set($ug_type . '_id', array_map('intval', $ug_id));
815 $forum_sql = $db->sql_in_set('forum_id', array_map('intval', $forum_id));
817 // Instead of updating, inserting, removing we just remove all current settings and re-set everything...
818 $table = ($ug_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
819 $id_field = $ug_type . '_id';
821 // Get any flags as required
824 $flag = substr($flag, 0, strpos($flag, '_') + 1);
826 // This ID (the any-flag) is set if one or more permissions are true...
827 $any_option_id = (int) $this->acl_options['id'][$flag];
829 // Remove any-flag from auth ary
830 if (isset($auth[$flag]))
835 // Remove current auth options...
836 $auth_option_ids = array((int)$any_option_id);
837 foreach ($auth as $auth_option => $auth_setting)
839 $auth_option_ids[] = (int) $this->acl_options['id'][$auth_option];
842 $sql = "DELETE FROM $table
845 AND " . $db->sql_in_set('auth_option_id', $auth_option_ids);
846 $db->sql_query($sql);
848 // Remove those having a role assigned... the correct type of course...
849 $sql = 'SELECT role_id
850 FROM ' . ACL_ROLES_TABLE . "
851 WHERE role_type = '" . $db->sql_escape($flag) . "'";
852 $result = $db->sql_query($sql);
855 while ($row = $db->sql_fetchrow($result))
857 $role_ids[] = $row['role_id'];
859 $db->sql_freeresult($result);
861 if (sizeof($role_ids))
863 $sql = "DELETE FROM $table
866 AND auth_option_id = 0
867 AND " . $db->sql_in_set('auth_role_id', $role_ids);
868 $db->sql_query($sql);
871 // Ok, include the any-flag if one or more auth options are set to yes...
872 foreach ($auth as $auth_option => $setting)
874 if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
876 $auth[$flag] = ACL_YES;
881 foreach ($forum_id as $forum)
883 $forum = (int) $forum;
887 foreach ($ug_id as $id)
890 $id_field => (int) $id,
891 'forum_id' => (int) $forum,
892 'auth_option_id' => 0,
894 'auth_role_id' => (int) $role_id,
900 foreach ($auth as $auth_option => $setting)
902 $auth_option_id = (int) $this->acl_options['id'][$auth_option];
904 if ($setting != ACL_NO)
906 foreach ($ug_id as $id)
909 $id_field => (int) $id,
910 'forum_id' => (int) $forum,
911 'auth_option_id' => (int) $auth_option_id,
912 'auth_setting' => (int) $setting
920 $db->sql_multi_insert($table, $sql_ary);
924 $this->acl_clear_prefetch();
929 * Set a role-specific ACL record
931 function acl_set_role($role_id, $auth)
935 // Get any-flag as required
938 $flag = substr($flag, 0, strpos($flag, '_') + 1);
940 // Remove any-flag from auth ary
941 if (isset($auth[$flag]))
946 // Re-set any flag...
947 foreach ($auth as $auth_option => $setting)
949 if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
951 $auth[$flag] = ACL_YES;
956 foreach ($auth as $auth_option => $setting)
958 $auth_option_id = (int) $this->acl_options['id'][$auth_option];
960 if ($setting != ACL_NO)
963 'role_id' => (int) $role_id,
964 'auth_option_id' => (int) $auth_option_id,
965 'auth_setting' => (int) $setting
970 // If no data is there, we set the any-flag to ACL_NEVER...
971 if (!sizeof($sql_ary))
974 'role_id' => (int) $role_id,
975 'auth_option_id' => (int) $this->acl_options['id'][$flag],
976 'auth_setting' => ACL_NEVER
980 // Remove current auth options...
981 $sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . '
982 WHERE role_id = ' . $role_id;
983 $db->sql_query($sql);
985 // Now insert the new values
986 $db->sql_multi_insert(ACL_ROLES_DATA_TABLE, $sql_ary);
988 $this->acl_clear_prefetch();
992 * Remove local permission
994 function acl_delete($mode, $ug_id = false, $forum_id = false, $permission_type = false)
998 if ($ug_id === false && $forum_id === false)
1003 $option_id_ary = array();
1004 $table = ($mode == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
1005 $id_field = $mode . '_id';
1007 $where_sql = array();
1009 if ($forum_id !== false)
1011 $where_sql[] = (!is_array($forum_id)) ? 'forum_id = ' . (int) $forum_id : $db->sql_in_set('forum_id', array_map('intval', $forum_id));
1014 if ($ug_id !== false)
1016 $where_sql[] = (!is_array($ug_id)) ? $id_field . ' = ' . (int) $ug_id : $db->sql_in_set($id_field, array_map('intval', $ug_id));
1019 // There seem to be auth options involved, therefore we need to go through the list and make sure we capture roles correctly
1020 if ($permission_type !== false)
1022 // Get permission type
1023 $sql = 'SELECT auth_option, auth_option_id
1024 FROM ' . ACL_OPTIONS_TABLE . "
1025 WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char);
1026 $result = $db->sql_query($sql);
1028 $auth_id_ary = array();
1029 while ($row = $db->sql_fetchrow($result))
1031 $option_id_ary[] = $row['auth_option_id'];
1032 $auth_id_ary[$row['auth_option']] = ACL_NO;
1034 $db->sql_freeresult($result);
1036 // First of all, lets grab the items having roles with the specified auth options assigned
1037 $sql = "SELECT auth_role_id, $id_field, forum_id
1038 FROM $table, " . ACL_ROLES_TABLE . " r
1039 WHERE auth_role_id <> 0
1040 AND auth_role_id = r.role_id
1041 AND r.role_type = '{$permission_type}'
1042 AND " . implode(' AND ', $where_sql) . '
1043 ORDER BY auth_role_id';
1044 $result = $db->sql_query($sql);
1046 $cur_role_auth = array();
1047 while ($row = $db->sql_fetchrow($result))
1049 $cur_role_auth[$row['auth_role_id']][$row['forum_id']][] = $row[$id_field];
1051 $db->sql_freeresult($result);
1053 // Get role data for resetting data
1054 if (sizeof($cur_role_auth))
1056 $sql = 'SELECT ao.auth_option, rd.role_id, rd.auth_setting
1057 FROM ' . ACL_OPTIONS_TABLE . ' ao, ' . ACL_ROLES_DATA_TABLE . ' rd
1058 WHERE ao.auth_option_id = rd.auth_option_id
1059 AND ' . $db->sql_in_set('rd.role_id', array_keys($cur_role_auth));
1060 $result = $db->sql_query($sql);
1062 $auth_settings = array();
1063 while ($row = $db->sql_fetchrow($result))
1065 // We need to fill all auth_options, else setting it will fail...
1066 if (!isset($auth_settings[$row['role_id']]))
1068 $auth_settings[$row['role_id']] = $auth_id_ary;
1070 $auth_settings[$row['role_id']][$row['auth_option']] = $row['auth_setting'];
1072 $db->sql_freeresult($result);
1075 foreach ($cur_role_auth as $role_id => $auth_row)
1077 foreach ($auth_row as $f_id => $ug_row)
1079 $this->acl_set($mode, $f_id, $ug_row, $auth_settings[$role_id], 0, false);
1085 // Now, normally remove permissions...
1086 if ($permission_type !== false)
1088 $where_sql[] = $db->sql_in_set('auth_option_id', array_map('intval', $option_id_ary));
1091 $sql = "DELETE FROM $table
1092 WHERE " . implode(' AND ', $where_sql);
1093 $db->sql_query($sql);
1095 $this->acl_clear_prefetch();
1099 * Assign category to template
1100 * used by display_mask()
1102 function assign_cat_array(&$category_array, $tpl_cat, $tpl_mask, $ug_id, $forum_id, $show_trace = false, $s_view)
1104 global $template, $user, $phpbb_admin_path, $phpEx;
1106 @reset($category_array);
1107 while (list($cat, $cat_array) = each($category_array))
1109 $template->assign_block_vars($tpl_cat, array(
1110 'S_YES' => ($cat_array['S_YES'] && !$cat_array['S_NEVER'] && !$cat_array['S_NO']) ? true : false,
1111 'S_NEVER' => ($cat_array['S_NEVER'] && !$cat_array['S_YES'] && !$cat_array['S_NO']) ? true : false,
1112 'S_NO' => ($cat_array['S_NO'] && !$cat_array['S_NEVER'] && !$cat_array['S_YES']) ? true : false,
1114 'CAT_NAME' => $user->lang['permission_cat'][$cat])
1117 /* Sort permissions by name (more naturaly and user friendly than sorting by a primary key)
1118 * Commented out due to it's memory consumption and time needed
1120 $key_array = array_intersect(array_keys($user->lang), array_map(create_function('$a', 'return "acl_" . $a;'), array_keys($cat_array['permissions'])));
1121 $values_array = $cat_array['permissions'];
1123 $cat_array['permissions'] = array();
1125 foreach ($key_array as $key)
1127 $key = str_replace('acl_', '', $key);
1128 $cat_array['permissions'][$key] = $values_array[$key];
1130 unset($key_array, $values_array);
1132 @reset($cat_array['permissions']);
1133 while (list($permission, $allowed) = each($cat_array['permissions']))
1137 $template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
1138 'S_YES' => ($allowed == ACL_YES) ? true : false,
1139 'S_NEVER' => ($allowed == ACL_NEVER) ? true : false,
1142 'FORUM_ID' => $forum_id,
1143 'FIELD_NAME' => $permission,
1144 'S_FIELD_NAME' => 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
1146 'U_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission") : '',
1147 'UA_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
1149 'PERMISSION' => $user->lang['acl_' . $permission]['lang'])
1154 $template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
1155 'S_YES' => ($allowed == ACL_YES) ? true : false,
1156 'S_NEVER' => ($allowed == ACL_NEVER) ? true : false,
1157 'S_NO' => ($allowed == ACL_NO) ? true : false,
1160 'FORUM_ID' => $forum_id,
1161 'FIELD_NAME' => $permission,
1162 'S_FIELD_NAME' => 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
1164 'U_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission") : '',
1165 'UA_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
1167 'PERMISSION' => $user->lang['acl_' . $permission]['lang'])
1175 * Building content array from permission rows with explicit key ordering
1176 * used by display_mask()
1178 function build_permission_array(&$permission_row, &$content_array, &$categories, $key_sort_array)
1182 foreach ($key_sort_array as $forum_id)
1184 if (!isset($permission_row[$forum_id]))
1189 $permissions = $permission_row[$forum_id];
1190 ksort($permissions);
1192 @reset($permissions);
1193 while (list($permission, $auth_setting) = each($permissions))
1195 if (!isset($user->lang['acl_' . $permission]))
1197 $user->lang['acl_' . $permission] = array(
1199 'lang' => '{ acl_' . $permission . ' }'
1203 $cat = $user->lang['acl_' . $permission]['cat'];
1205 // Build our categories array
1206 if (!isset($categories[$cat]))
1208 $categories[$cat] = $user->lang['permission_cat'][$cat];
1211 // Build our content array
1212 if (!isset($content_array[$forum_id]))
1214 $content_array[$forum_id] = array();
1217 if (!isset($content_array[$forum_id][$cat]))
1219 $content_array[$forum_id][$cat] = array(
1223 'permissions' => array(),
1227 $content_array[$forum_id][$cat]['S_YES'] |= ($auth_setting == ACL_YES) ? true : false;
1228 $content_array[$forum_id][$cat]['S_NEVER'] |= ($auth_setting == ACL_NEVER) ? true : false;
1229 $content_array[$forum_id][$cat]['S_NO'] |= ($auth_setting == ACL_NO) ? true : false;
1231 $content_array[$forum_id][$cat]['permissions'][$permission] = $auth_setting;
1237 * Use permissions from another user. This transferes a permission set from one user to another.
1238 * The other user is always able to revert back to his permission set.
1239 * This function does not check for lower/higher permissions, it is possible for the user to gain
1240 * "more" permissions by this.
1241 * Admin permissions will not be copied.
1243 function ghost_permissions($from_user_id, $to_user_id)
1247 if ($to_user_id == ANONYMOUS)
1252 $hold_ary = $this->acl_raw_data_single_user($from_user_id);
1254 // Key 0 in $hold_ary are global options, all others are forum_ids
1256 // We disallow copying admin permissions
1257 foreach ($this->acl_options['global'] as $opt => $id)
1259 if (strpos($opt, 'a_') === 0)
1261 $hold_ary[0][$this->acl_options['id'][$opt]] = ACL_NEVER;
1265 // Force a_switchperm to be allowed
1266 $hold_ary[0][$this->acl_options['id']['a_switchperm']] = ACL_YES;
1268 $user_permissions = $this->build_bitstring($hold_ary);
1270 if (!$user_permissions)
1275 $sql = 'UPDATE ' . USERS_TABLE . "
1276 SET user_permissions = '" . $db->sql_escape($user_permissions) . "',
1277 user_perm_from = $from_user_id
1278 WHERE user_id = " . $to_user_id;
1279 $db->sql_query($sql);