replace constants with class constants.
[phpbb.git] / phpBB / modules / acp / auth.php
blobee4e45a08d55542374012bf505178b327b28ed51
1 <?php
2 /**
4 * @package phpBB3
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 * ACP Permission/Auth class
21 * @package phpBB3
23 class auth_admin extends auth
25 /**
26 * Init auth settings
28 function __construct()
30 global $db, $cache;
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);
39 $global = $local = 0;
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++;
48 if ($row['is_local'])
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);
62 /**
63 * Get permission mask
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 phpbb::ACL_NEVER|phpbb::ACL_NO|phpbb::ACL_YES $acl_fill defines the mode those permissions not set are getting filled with
74 public function get_mask($mode, $user_id = false, $group_id = false, $forum_id = false, $auth_option = false, $scope = false, $acl_fill = phpbb::ACL_NEVER)
76 global $db, $user;
78 $hold_ary = array();
79 $view_user_mask = ($mode == 'view' && $group_id === false) ? true : false;
81 if ($auth_option === false || $scope === false)
83 return array();
86 $acl_user_function = ($mode == 'set') ? 'acl_user_raw_data' : 'acl_raw_data';
88 if (!$view_user_mask)
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);
94 else
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);
121 if ($view_user_mask)
123 $auth2 = null;
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'])
134 $auth2 = new auth();
135 $auth2->acl($userdata);
137 else
139 global $auth;
140 $auth2 = &$auth;
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);
156 unset($userdata);
157 unset($auth2);
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,
203 array_flip(
204 array_diff(
205 array_keys($compare_options), array_keys($options)
213 else
215 $hold_ary[($group_id !== false) ? $group_id : $user_id][(int) $forum_id] = $compare_options;
218 return $hold_ary;
222 * Get permission mask for roles
223 * This function only supports getting masks for one role
225 public function get_role_mask($role_id)
227 global $db;
229 $hold_ary = array();
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 . '
235 ORDER BY forum_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 . '
248 ORDER BY forum_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);
257 return $hold_ary;
261 * Display permission mask (assign to template)
263 public function display_mask($mode, $permission_type, array $hold_ary, $user_mode = 'user', $local = false, $group_display = true)
265 global $template, $user, $db;
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';
271 $tpl_mask = 'mask';
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;
278 // Get names
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';
286 else
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);
302 // Get used forums
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();
311 if ($local)
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'])
320 continue;
322 unset($forum_names_ary[$key]);
325 else
327 $forum_names_ary[0] = $l_acl_type;
330 // Get available roles
331 $sql = 'SELECT *
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);
337 $roles = array();
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 = '';
349 if (sizeof($roles))
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'])
370 continue;
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);
392 $groups = array();
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...
402 if ($memberships)
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']];
410 else
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;
425 $hold_ary = array();
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 self::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 = '';
468 @reset($roles);
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>';
478 if ($s_role_options)
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;
492 break;
496 else
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,
504 'UG_ID' => $ug_id,
505 'S_CUSTOM' => $s_custom_permissions,
506 'FORUM_ID' => $forum_id)
509 self::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]);
517 else
519 foreach ($ug_names_ary as $ug_id => $ug_name)
521 if (!isset($hold_ary[$ug_id]))
523 continue;
526 $content_array = $categories = array();
527 self::build_permission_array($hold_ary[$ug_id], $content_array, $categories, array_keys($forum_names_ary));
529 $template->assign_block_vars($tpl_pmask, array(
530 'NAME' => $ug_name,
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 = '';
554 @reset($roles);
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>';
564 if ($s_role_options)
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;
578 break;
582 else
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,
592 'UG_ID' => $ug_id,
593 'FORUM_ID' => $forum_id)
596 self::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 public function display_role_mask(array $hold_ary)
609 global $db, $template, $user;
611 if (!sizeof($hold_ary))
613 return;
616 // Get forum names
617 $sql = 'SELECT forum_id, forum_name
618 FROM ' . FORUMS_TABLE . '
619 WHERE ' . $db->sql_in_set('forum_id', array_keys($hold_ary)) . '
620 ORDER BY left_id';
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('memberlist', "mode=viewprofile&amp;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('memberlist', "mode=group&amp;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 ->
683 * $options = array(
684 * 'local' => array('option1', 'option2', ...),
685 * 'global' => array('optionA', 'optionB', ...)
686 * );
688 public function acl_add_option(array $options)
690 global $db, $cache;
692 $cur_options = array();
694 $sql = 'SELECT auth_option, is_global, is_local
695 FROM ' . ACL_OPTIONS_TABLE . '
696 ORDER BY auth_option_id';
697 $result = $db->sql_query($sql);
699 while ($row = $db->sql_fetchrow($result))
701 if ($row['is_global'])
703 $cur_options['global'][] = $row['auth_option'];
706 if ($row['is_local'])
708 $cur_options['local'][] = $row['auth_option'];
711 $db->sql_freeresult($result);
713 // Here we need to insert new options ... this requires discovering whether
714 // an options is global, local or both and whether we need to add an permission
715 // set flag (x_)
716 $new_options = array('local' => array(), 'global' => array());
718 foreach ($options as $type => $option_ary)
720 $option_ary = array_unique($option_ary);
722 foreach ($option_ary as $option_value)
724 if (!in_array($option_value, $cur_options[$type]))
726 $new_options[$type][] = $option_value;
729 $flag = substr($option_value, 0, strpos($option_value, '_') + 1);
731 if (!in_array($flag, $cur_options[$type]) && !in_array($flag, $new_options[$type]))
733 $new_options[$type][] = $flag;
737 unset($options);
739 $options = array();
740 $options['local'] = array_diff($new_options['local'], $new_options['global']);
741 $options['global'] = array_diff($new_options['global'], $new_options['local']);
742 $options['local_global'] = array_intersect($new_options['local'], $new_options['global']);
744 $sql_ary = array();
746 foreach ($options as $type => $option_ary)
748 foreach ($option_ary as $option)
750 $sql_ary[] = array(
751 'auth_option' => (string) $option,
752 'is_global' => ($type == 'global' || $type == 'local_global') ? 1 : 0,
753 'is_local' => ($type == 'local' || $type == 'local_global') ? 1 : 0
758 $db->sql_multi_insert(ACL_OPTIONS_TABLE, $sql_ary);
760 $cache->destroy('_acl_options');
761 $this->acl_clear_prefetch();
763 // Because we just changed the options and also purged the options cache, we instantly update/regenerate it for later calls to succeed.
764 $this->acl_options = array();
765 $this->__construct();
767 return true;
771 * Set a user or group ACL record
773 public function acl_set($ug_type, $forum_id, $ug_id, $auth, $role_id = 0, $clear_prefetch = true)
775 global $db;
777 // One or more forums
778 if (!is_array($forum_id))
780 $forum_id = array($forum_id);
783 // One or more users
784 if (!is_array($ug_id))
786 $ug_id = array($ug_id);
789 $ug_id_sql = $db->sql_in_set($ug_type . '_id', array_map('intval', $ug_id));
790 $forum_sql = $db->sql_in_set('forum_id', array_map('intval', $forum_id));
792 // Instead of updating, inserting, removing we just remove all current settings and re-set everything...
793 $table = ($ug_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
794 $id_field = $ug_type . '_id';
796 // Get any flags as required
797 reset($auth);
798 $flag = key($auth);
799 $flag = substr($flag, 0, strpos($flag, '_') + 1);
801 // This ID (the any-flag) is set if one or more permissions are true...
802 $any_option_id = (int) $this->acl_options['id'][$flag];
804 // Remove any-flag from auth ary
805 if (isset($auth[$flag]))
807 unset($auth[$flag]);
810 // Remove current auth options...
811 $auth_option_ids = array((int)$any_option_id);
812 foreach ($auth as $auth_option => $auth_setting)
814 $auth_option_ids[] = (int) $this->acl_options['id'][$auth_option];
817 $sql = "DELETE FROM $table
818 WHERE $forum_sql
819 AND $ug_id_sql
820 AND " . $db->sql_in_set('auth_option_id', $auth_option_ids);
821 $db->sql_query($sql);
823 // Remove those having a role assigned... the correct type of course...
824 $sql = 'SELECT role_id
825 FROM ' . ACL_ROLES_TABLE . "
826 WHERE role_type = '" . $db->sql_escape($flag) . "'";
827 $result = $db->sql_query($sql);
829 $role_ids = array();
830 while ($row = $db->sql_fetchrow($result))
832 $role_ids[] = $row['role_id'];
834 $db->sql_freeresult($result);
836 if (sizeof($role_ids))
838 $sql = "DELETE FROM $table
839 WHERE $forum_sql
840 AND $ug_id_sql
841 AND auth_option_id = 0
842 AND " . $db->sql_in_set('auth_role_id', $role_ids);
843 $db->sql_query($sql);
846 // Ok, include the any-flag if one or more auth options are set to yes...
847 foreach ($auth as $auth_option => $setting)
849 if ($setting == phpbb::ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == phpbb::ACL_NEVER))
851 $auth[$flag] = phpbb::ACL_YES;
855 $sql_ary = array();
856 foreach ($forum_id as $forum)
858 $forum = (int) $forum;
860 if ($role_id)
862 foreach ($ug_id as $id)
864 $sql_ary[] = array(
865 $id_field => (int) $id,
866 'forum_id' => (int) $forum,
867 'auth_option_id' => 0,
868 'auth_setting' => 0,
869 'auth_role_id' => (int) $role_id,
873 else
875 foreach ($auth as $auth_option => $setting)
877 $auth_option_id = (int) $this->acl_options['id'][$auth_option];
879 if ($setting != phpbb::ACL_NO)
881 foreach ($ug_id as $id)
883 $sql_ary[] = array(
884 $id_field => (int) $id,
885 'forum_id' => (int) $forum,
886 'auth_option_id' => (int) $auth_option_id,
887 'auth_setting' => (int) $setting
895 $db->sql_multi_insert($table, $sql_ary);
897 if ($clear_prefetch)
899 $this->acl_clear_prefetch();
904 * Set a role-specific ACL record
906 public function acl_set_role($role_id, $auth)
908 global $db;
910 // Get any-flag as required
911 reset($auth);
912 $flag = key($auth);
913 $flag = substr($flag, 0, strpos($flag, '_') + 1);
915 // Remove any-flag from auth ary
916 if (isset($auth[$flag]))
918 unset($auth[$flag]);
921 // Re-set any flag...
922 foreach ($auth as $auth_option => $setting)
924 if ($setting == phpbb::ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == phpbb::ACL_NEVER))
926 $auth[$flag] = phpbb::ACL_YES;
930 $sql_ary = array();
931 foreach ($auth as $auth_option => $setting)
933 $auth_option_id = (int) $this->acl_options['id'][$auth_option];
935 if ($setting != phpbb::ACL_NO)
937 $sql_ary[] = array(
938 'role_id' => (int) $role_id,
939 'auth_option_id' => (int) $auth_option_id,
940 'auth_setting' => (int) $setting
945 // If no data is there, we set the any-flag to ACL_NEVER...
946 if (!sizeof($sql_ary))
948 $sql_ary[] = array(
949 'role_id' => (int) $role_id,
950 'auth_option_id' => (int) $this->acl_options['id'][$flag],
951 'auth_setting' => phpbb::ACL_NEVER
955 // Remove current auth options...
956 $sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . '
957 WHERE role_id = ' . $role_id;
958 $db->sql_query($sql);
960 // Now insert the new values
961 $db->sql_multi_insert(ACL_ROLES_DATA_TABLE, $sql_ary);
963 $this->acl_clear_prefetch();
967 * Remove local permission
969 public function acl_delete($mode, $ug_id = false, $forum_id = false, $permission_type = false)
971 global $db;
973 if ($ug_id === false && $forum_id === false)
975 return;
978 $option_id_ary = array();
979 $table = ($mode == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
980 $id_field = $mode . '_id';
982 $where_sql = array();
984 if ($forum_id !== false)
986 $where_sql[] = (!is_array($forum_id)) ? 'forum_id = ' . (int) $forum_id : $db->sql_in_set('forum_id', array_map('intval', $forum_id));
989 if ($ug_id !== false)
991 $where_sql[] = (!is_array($ug_id)) ? $id_field . ' = ' . (int) $ug_id : $db->sql_in_set($id_field, array_map('intval', $ug_id));
994 // There seem to be auth options involved, therefore we need to go through the list and make sure we capture roles correctly
995 if ($permission_type !== false)
997 // Get permission type
998 $sql = 'SELECT auth_option, auth_option_id
999 FROM ' . ACL_OPTIONS_TABLE . "
1000 WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char);
1001 $result = $db->sql_query($sql);
1003 $auth_id_ary = array();
1004 while ($row = $db->sql_fetchrow($result))
1006 $option_id_ary[] = $row['auth_option_id'];
1007 $auth_id_ary[$row['auth_option']] = phpbb::ACL_NO;
1009 $db->sql_freeresult($result);
1011 // First of all, lets grab the items having roles with the specified auth options assigned
1012 $sql = "SELECT auth_role_id, $id_field, forum_id
1013 FROM $table, " . ACL_ROLES_TABLE . " r
1014 WHERE auth_role_id <> 0
1015 AND auth_role_id = r.role_id
1016 AND r.role_type = '{$permission_type}'
1017 AND " . implode(' AND ', $where_sql) . '
1018 ORDER BY auth_role_id';
1019 $result = $db->sql_query($sql);
1021 $cur_role_auth = array();
1022 while ($row = $db->sql_fetchrow($result))
1024 $cur_role_auth[$row['auth_role_id']][$row['forum_id']][] = $row[$id_field];
1026 $db->sql_freeresult($result);
1028 // Get role data for resetting data
1029 if (sizeof($cur_role_auth))
1031 $sql = 'SELECT ao.auth_option, rd.role_id, rd.auth_setting
1032 FROM ' . ACL_OPTIONS_TABLE . ' ao, ' . ACL_ROLES_DATA_TABLE . ' rd
1033 WHERE ao.auth_option_id = rd.auth_option_id
1034 AND ' . $db->sql_in_set('rd.role_id', array_keys($cur_role_auth));
1035 $result = $db->sql_query($sql);
1037 $auth_settings = array();
1038 while ($row = $db->sql_fetchrow($result))
1040 // We need to fill all auth_options, else setting it will fail...
1041 if (!isset($auth_settings[$row['role_id']]))
1043 $auth_settings[$row['role_id']] = $auth_id_ary;
1045 $auth_settings[$row['role_id']][$row['auth_option']] = $row['auth_setting'];
1047 $db->sql_freeresult($result);
1049 // Set the options
1050 foreach ($cur_role_auth as $role_id => $auth_row)
1052 foreach ($auth_row as $f_id => $ug_row)
1054 $this->acl_set($mode, $f_id, $ug_row, $auth_settings[$role_id], 0, false);
1060 // Now, normally remove permissions...
1061 if ($permission_type !== false)
1063 $where_sql[] = $db->sql_in_set('auth_option_id', array_map('intval', $option_id_ary));
1066 $sql = "DELETE FROM $table
1067 WHERE " . implode(' AND ', $where_sql);
1068 $db->sql_query($sql);
1070 $this->acl_clear_prefetch();
1074 * Assign category to template
1075 * used by display_mask()
1077 private static function assign_cat_array(array $category_array, $tpl_cat, $tpl_mask, $ug_id, $forum_id, $show_trace = false, $s_view)
1079 global $template, $user;
1081 @reset($category_array);
1082 while (list($cat, $cat_array) = each($category_array))
1084 $template->assign_block_vars($tpl_cat, array(
1085 'S_YES' => ($cat_array['S_YES'] && !$cat_array['S_NEVER'] && !$cat_array['S_NO']) ? true : false,
1086 'S_NEVER' => ($cat_array['S_NEVER'] && !$cat_array['S_YES'] && !$cat_array['S_NO']) ? true : false,
1087 'S_NO' => ($cat_array['S_NO'] && !$cat_array['S_NEVER'] && !$cat_array['S_YES']) ? true : false,
1089 'CAT_NAME' => $user->lang['permission_cat'][$cat])
1092 /* Sort permissions by name (more naturaly and user friendly than sorting by a primary key)
1093 * Commented out due to it's memory consumption and time needed
1095 $key_array = array_intersect(array_keys($user->lang), array_map(create_function('$a', 'return "acl_" . $a;'), array_keys($cat_array['permissions'])));
1096 $values_array = $cat_array['permissions'];
1098 $cat_array['permissions'] = array();
1100 foreach ($key_array as $key)
1102 $key = str_replace('acl_', '', $key);
1103 $cat_array['permissions'][$key] = $values_array[$key];
1105 unset($key_array, $values_array);
1107 @reset($cat_array['permissions']);
1108 while (list($permission, $allowed) = each($cat_array['permissions']))
1110 if ($s_view)
1112 $template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
1113 'S_YES' => ($allowed == phpbb::ACL_YES) ? true : false,
1114 'S_NEVER' => ($allowed == phpbb::ACL_NEVER) ? true : false,
1116 'UG_ID' => $ug_id,
1117 'FORUM_ID' => $forum_id,
1118 'FIELD_NAME' => $permission,
1119 'S_FIELD_NAME' => 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
1121 'U_TRACE' => ($show_trace) ? append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, "i=permissions&amp;mode=trace&amp;u=$ug_id&amp;f=$forum_id&amp;auth=$permission") : '',
1122 'UA_TRACE' => ($show_trace) ? append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
1124 'PERMISSION' => $user->lang['acl_' . $permission]['lang'])
1127 else
1129 $template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
1130 'S_YES' => ($allowed == phpbb::ACL_YES) ? true : false,
1131 'S_NEVER' => ($allowed == phpbb::ACL_NEVER) ? true : false,
1132 'S_NO' => ($allowed == phpbb::ACL_NO) ? true : false,
1134 'UG_ID' => $ug_id,
1135 'FORUM_ID' => $forum_id,
1136 'FIELD_NAME' => $permission,
1137 'S_FIELD_NAME' => 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
1139 'U_TRACE' => ($show_trace) ? append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, "i=permissions&amp;mode=trace&amp;u=$ug_id&amp;f=$forum_id&amp;auth=$permission") : '',
1140 'UA_TRACE' => ($show_trace) ? append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
1142 'PERMISSION' => $user->lang['acl_' . $permission]['lang'])
1150 * Building content array from permission rows with explicit key ordering
1151 * used by display_mask()
1153 public static function build_permission_array(array $permission_row, array &$content_array, array &$categories, array $key_sort_array)
1155 global $user;
1157 foreach ($key_sort_array as $forum_id)
1159 if (!isset($permission_row[$forum_id]))
1161 continue;
1164 $permissions = $permission_row[$forum_id];
1165 ksort($permissions);
1167 @reset($permissions);
1168 while (list($permission, $auth_setting) = each($permissions))
1170 if (!isset($user->lang['acl_' . $permission]))
1172 $user->lang['acl_' . $permission] = array(
1173 'cat' => 'misc',
1174 'lang' => '{ acl_' . $permission . ' }'
1178 $cat = $user->lang['acl_' . $permission]['cat'];
1180 // Build our categories array
1181 if (!isset($categories[$cat]))
1183 $categories[$cat] = $user->lang['permission_cat'][$cat];
1186 // Build our content array
1187 if (!isset($content_array[$forum_id]))
1189 $content_array[$forum_id] = array();
1192 if (!isset($content_array[$forum_id][$cat]))
1194 $content_array[$forum_id][$cat] = array(
1195 'S_YES' => false,
1196 'S_NEVER' => false,
1197 'S_NO' => false,
1198 'permissions' => array(),
1202 $content_array[$forum_id][$cat]['S_YES'] |= ($auth_setting == phpbb::ACL_YES) ? true : false;
1203 $content_array[$forum_id][$cat]['S_NEVER'] |= ($auth_setting == phpbb::ACL_NEVER) ? true : false;
1204 $content_array[$forum_id][$cat]['S_NO'] |= ($auth_setting == phpbb::ACL_NO) ? true : false;
1206 $content_array[$forum_id][$cat]['permissions'][$permission] = $auth_setting;
1212 * Use permissions from another user. This transferes a permission set from one user to another.
1213 * The other user is always able to revert back to his permission set.
1214 * This function does not check for lower/higher permissions, it is possible for the user to gain
1215 * "more" permissions by this.
1216 * Admin permissions will not be copied.
1218 public function ghost_permissions($from_user_id, $to_user_id)
1220 global $db;
1222 if ($to_user_id == ANONYMOUS)
1224 return false;
1227 $hold_ary = $this->acl_raw_data_single_user($from_user_id);
1229 // Key 0 in $hold_ary are global options, all others are forum_ids
1231 // We disallow copying admin permissions
1232 foreach ($this->acl_options['global'] as $opt => $id)
1234 if (strpos($opt, 'a_') === 0)
1236 $hold_ary[0][$this->acl_options['id'][$opt]] = phpbb::ACL_NEVER;
1240 // Force a_switchperm to be allowed
1241 $hold_ary[0][$this->acl_options['id']['a_switchperm']] = phpbb::ACL_YES;
1243 $user_permissions = $this->build_bitstring($hold_ary);
1245 if (!$user_permissions)
1247 return false;
1250 $sql = 'UPDATE ' . USERS_TABLE . "
1251 SET user_permissions = '" . $db->sql_escape($user_permissions) . "',
1252 user_perm_from = $from_user_id
1253 WHERE user_id = " . $to_user_id;
1254 $db->sql_query($sql);
1256 return true;