5
* @version $Id: auth.php,v 1.54 2007/10/05 14:36:32 acydburn Exp $
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
25
var $option_ids = array();
34
if (($this->acl_options = $cache->get('_acl_options')) === false)
36
$sql = 'SELECT auth_option, is_global, is_local
37
FROM ' . ACL_OPTIONS_TABLE . '
38
ORDER BY auth_option_id';
39
$result = $db->sql_query($sql);
42
$this->acl_options = array();
43
while ($row = $db->sql_fetchrow($result))
45
if ($row['is_global'])
47
$this->acl_options['global'][$row['auth_option']] = $global++;
52
$this->acl_options['local'][$row['auth_option']] = $local++;
55
$db->sql_freeresult($result);
57
$cache->put('_acl_options', $this->acl_options);
60
if (!sizeof($this->option_ids))
62
$sql = 'SELECT auth_option_id, auth_option
63
FROM ' . ACL_OPTIONS_TABLE;
64
$result = $db->sql_query($sql);
66
$this->option_ids = array();
67
while ($row = $db->sql_fetchrow($result))
69
$this->option_ids[$row['auth_option']] = $row['auth_option_id'];
71
$db->sql_freeresult($result);
77
* This function only supports getting permissions of one type (for example a_)
79
* @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
80
* @param mixed $user_id user ids to search for (a user_id or a group_id has to be specified at least)
81
* @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)
82
* @param mixed $forum_id forum_ids to search for. Defining a forum id also means getting local settings
83
* @param string $auth_option the auth_option defines the permission setting to look for (a_ for example)
84
* @param local|global $scope the scope defines the permission scope. If local, a forum_id is additionally required
85
* @param ACL_NEVER|ACL_NO|ACL_YES $acl_fill defines the mode those permissions not set are getting filled with
87
function get_mask($mode, $user_id = false, $group_id = false, $forum_id = false, $auth_option = false, $scope = false, $acl_fill = ACL_NEVER)
92
$view_user_mask = ($mode == 'view' && $group_id === false) ? true : false;
94
if ($auth_option === false || $scope === false)
99
$acl_user_function = ($mode == 'set') ? 'acl_user_raw_data' : 'acl_raw_data';
101
if (!$view_user_mask)
103
if ($forum_id !== false)
105
$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);
109
$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);
113
// Make sure hold_ary is filled with every setting (prevents missing forums/users/groups)
114
$ug_id = ($group_id !== false) ? ((!is_array($group_id)) ? array($group_id) : $group_id) : ((!is_array($user_id)) ? array($user_id) : $user_id);
115
$forum_ids = ($forum_id !== false) ? ((!is_array($forum_id)) ? array($forum_id) : $forum_id) : (($scope == 'global') ? array(0) : array());
117
// Only those options we need
118
$compare_options = array_diff(preg_replace('/^((?!' . $auth_option . ').+)|(' . $auth_option . ')$/', '', array_keys($this->acl_options[$scope])), array(''));
120
// If forum_ids is false and the scope is local we actually want to have all forums within the array
121
if ($scope == 'local' && !sizeof($forum_ids))
123
$sql = 'SELECT forum_id
124
FROM ' . FORUMS_TABLE;
125
$result = $db->sql_query($sql, 120);
127
while ($row = $db->sql_fetchrow($result))
129
$forum_ids[] = $row['forum_id'];
131
$db->sql_freeresult($result);
138
$sql = 'SELECT user_id, user_permissions, user_type
139
FROM ' . USERS_TABLE . '
140
WHERE ' . $db->sql_in_set('user_id', $ug_id);
141
$result = $db->sql_query($sql);
143
while ($userdata = $db->sql_fetchrow($result))
145
if ($user->data['user_id'] != $userdata['user_id'])
148
$auth2->acl($userdata);
157
$hold_ary[$userdata['user_id']] = array();
158
foreach ($forum_ids as $f_id)
160
$hold_ary[$userdata['user_id']][$f_id] = array();
161
foreach ($compare_options as $option)
163
$hold_ary[$userdata['user_id']][$f_id][$option] = $auth2->acl_get($option, $f_id);
167
$db->sql_freeresult($result);
173
foreach ($ug_id as $_id)
175
if (!isset($hold_ary[$_id]))
177
$hold_ary[$_id] = array();
180
foreach ($forum_ids as $f_id)
182
if (!isset($hold_ary[$_id][$f_id]))
184
$hold_ary[$_id][$f_id] = array();
189
// Now, we need to fill the gaps with $acl_fill. ;)
191
// Now switch back to keys
192
if (sizeof($compare_options))
194
$compare_options = array_combine($compare_options, array_fill(1, sizeof($compare_options), $acl_fill));
197
// Defining the user-function here to save some memory
198
$return_acl_fill = create_function('$value', 'return ' . $acl_fill . ';');
200
// Actually fill the gaps
201
if (sizeof($hold_ary))
203
foreach ($hold_ary as $ug_id => $row)
205
foreach ($row as $id => $options)
207
// Do not include the global auth_option
208
unset($options[$auth_option]);
210
// Not a "fine" solution, but at all it's a 1-dimensional
211
// array_diff_key function filling the resulting array values with zeros
212
// The differences get merged into $hold_ary (all permissions having $acl_fill set)
213
$hold_ary[$ug_id][$id] = array_merge($options,
215
array_map($return_acl_fill,
218
array_keys($compare_options), array_keys($options)
228
$hold_ary[($group_id !== false) ? $group_id : $user_id][(int) $forum_id] = $compare_options;
235
* Get permission mask for roles
236
* This function only supports getting masks for one role
238
function get_role_mask($role_id)
244
// Get users having this role set...
245
$sql = 'SELECT user_id, forum_id
246
FROM ' . ACL_USERS_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']]['users'][] = $row['user_id'];
255
$db->sql_freeresult($result);
257
// Now grab groups...
258
$sql = 'SELECT group_id, forum_id
259
FROM ' . ACL_GROUPS_TABLE . '
260
WHERE auth_role_id = ' . $role_id . '
262
$result = $db->sql_query($sql);
264
while ($row = $db->sql_fetchrow($result))
266
$hold_ary[$row['forum_id']]['groups'][] = $row['group_id'];
268
$db->sql_freeresult($result);
274
* Display permission mask (assign to template)
276
function display_mask($mode, $permission_type, &$hold_ary, $user_mode = 'user', $local = false, $group_display = true)
278
global $template, $user, $db, $phpbb_root_path, $phpEx;
280
// Define names for template loops, might be able to be set
281
$tpl_pmask = 'p_mask';
282
$tpl_fmask = 'f_mask';
283
$tpl_category = 'category';
286
$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);
288
// Allow trace for viewing permissions and in user mode
289
$show_trace = ($mode == 'view' && $user_mode == 'user') ? true : false;
292
if ($user_mode == 'user')
294
$sql = 'SELECT user_id as ug_id, username as ug_name
295
FROM ' . USERS_TABLE . '
296
WHERE ' . $db->sql_in_set('user_id', array_keys($hold_ary)) . '
297
ORDER BY username_clean ASC';
301
$sql = 'SELECT group_id as ug_id, group_name as ug_name, group_type
302
FROM ' . GROUPS_TABLE . '
303
WHERE ' . $db->sql_in_set('group_id', array_keys($hold_ary)) . '
304
ORDER BY group_type DESC, group_name ASC';
306
$result = $db->sql_query($sql);
308
$ug_names_ary = array();
309
while ($row = $db->sql_fetchrow($result))
311
$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']);
313
$db->sql_freeresult($result);
316
$forum_ids = array();
317
foreach ($hold_ary as $ug_id => $row)
319
$forum_ids = array_merge($forum_ids, array_keys($row));
321
$forum_ids = array_unique($forum_ids);
323
$forum_names_ary = array();
326
$forum_names_ary = make_forum_select(false, false, true, false, false, false, true);
328
// Remove the disabled ones, since we do not create an option field here...
329
foreach ($forum_names_ary as $key => $value)
331
if (!$value['disabled'])
335
unset($forum_names_ary[$key]);
340
$forum_names_ary[0] = $l_acl_type;
343
// Get available roles
345
FROM ' . ACL_ROLES_TABLE . "
346
WHERE role_type = '" . $db->sql_escape($permission_type) . "'
347
ORDER BY role_order ASC";
348
$result = $db->sql_query($sql);
351
while ($row = $db->sql_fetchrow($result))
353
$roles[$row['role_id']] = $row;
355
$db->sql_freeresult($result);
357
$cur_roles = $this->acl_role_data($user_mode, $permission_type, array_keys($hold_ary));
359
// Build js roles array (role data assignments)
360
$s_role_js_array = '';
364
$s_role_js_array = array();
366
// Make sure every role (even if empty) has its array defined
367
foreach ($roles as $_role_id => $null)
369
$s_role_js_array[$_role_id] = "\n" . 'role_options[' . $_role_id . '] = new Array();' . "\n";
372
$sql = 'SELECT r.role_id, o.auth_option, r.auth_setting
373
FROM ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' o
374
WHERE o.auth_option_id = r.auth_option_id
375
AND ' . $db->sql_in_set('r.role_id', array_keys($roles));
376
$result = $db->sql_query($sql);
378
while ($row = $db->sql_fetchrow($result))
380
$flag = substr($row['auth_option'], 0, strpos($row['auth_option'], '_') + 1);
381
if ($flag == $row['auth_option'])
386
$s_role_js_array[$row['role_id']] .= 'role_options[' . $row['role_id'] . '][\'' . addslashes($row['auth_option']) . '\'] = ' . $row['auth_setting'] . '; ';
388
$db->sql_freeresult($result);
390
$s_role_js_array = implode('', $s_role_js_array);
393
$template->assign_var('S_ROLE_JS_ARRAY', $s_role_js_array);
394
unset($s_role_js_array);
396
// Now obtain memberships
397
$user_groups_default = $user_groups_custom = array();
398
if ($user_mode == 'user' && $group_display)
400
$sql = 'SELECT group_id, group_name, group_type
401
FROM ' . GROUPS_TABLE . '
402
ORDER BY group_type DESC, group_name ASC';
403
$result = $db->sql_query($sql);
406
while ($row = $db->sql_fetchrow($result))
408
$groups[$row['group_id']] = $row;
410
$db->sql_freeresult($result);
412
$memberships = group_memberships(false, array_keys($hold_ary), false);
414
// User is not a member of any group? Bad admin, bad bad admin...
417
foreach ($memberships as $row)
419
if ($groups[$row['group_id']]['group_type'] == GROUP_SPECIAL)
421
$user_groups_default[$row['user_id']][] = $user->lang['G_' . $groups[$row['group_id']]['group_name']];
425
$user_groups_custom[$row['user_id']][] = $groups[$row['group_id']]['group_name'];
429
unset($memberships, $groups);
432
// If we only have one forum id to display or being in local mode and more than one user/group to display,
433
// we switch the complete interface to group by user/usergroup instead of grouping by forum
434
// To achieve this, we need to switch the array a bit
435
if (sizeof($forum_ids) == 1 || ($local && sizeof($ug_names_ary) > 1))
437
$hold_ary_temp = $hold_ary;
439
foreach ($hold_ary_temp as $ug_id => $row)
441
foreach ($forum_names_ary as $forum_id => $forum_row)
443
if (isset($row[$forum_id]))
445
$hold_ary[$forum_id][$ug_id] = $row[$forum_id];
449
unset($hold_ary_temp);
451
foreach ($hold_ary as $forum_id => $forum_array)
453
$content_array = $categories = array();
454
$this->build_permission_array($hold_ary[$forum_id], $content_array, $categories, array_keys($ug_names_ary));
456
$template->assign_block_vars($tpl_pmask, array(
457
'NAME' => ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
458
'PADDING' => ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
460
'CATEGORIES' => implode('</th><th>', $categories),
462
'L_ACL_TYPE' => $l_acl_type,
464
'S_LOCAL' => ($local) ? true : false,
465
'S_GLOBAL' => (!$local) ? true : false,
466
'S_NUM_CATS' => sizeof($categories),
467
'S_VIEW' => ($mode == 'view') ? true : false,
468
'S_NUM_OBJECTS' => sizeof($content_array),
469
'S_USER_MODE' => ($user_mode == 'user') ? true : false,
470
'S_GROUP_MODE' => ($user_mode == 'group') ? true : false)
473
@reset($content_array);
474
while (list($ug_id, $ug_array) = each($content_array))
476
// Build role dropdown options
477
$current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
479
$s_role_options = '';
482
while (list($role_id, $role_row) = each($roles))
484
$role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
485
$role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
487
$title = ($role_description) ? ' title="' . $role_description . '"' : '';
488
$s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
493
$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;
496
if (!$current_role_id && $mode != 'view')
498
$s_custom_permissions = false;
500
foreach ($ug_array as $key => $value)
502
if ($value['S_NEVER'] || $value['S_YES'])
504
$s_custom_permissions = true;
511
$s_custom_permissions = false;
514
$template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
515
'NAME' => $ug_names_ary[$ug_id],
516
'S_ROLE_OPTIONS' => $s_role_options,
518
'S_CUSTOM' => $s_custom_permissions,
519
'FORUM_ID' => $forum_id)
522
$this->assign_cat_array($ug_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
524
unset($content_array[$ug_id]);
527
unset($hold_ary[$forum_id]);
532
foreach ($ug_names_ary as $ug_id => $ug_name)
534
if (!isset($hold_ary[$ug_id]))
539
$content_array = $categories = array();
540
$this->build_permission_array($hold_ary[$ug_id], $content_array, $categories, array_keys($forum_names_ary));
542
$template->assign_block_vars($tpl_pmask, array(
544
'CATEGORIES' => implode('</th><th>', $categories),
546
'USER_GROUPS_DEFAULT' => ($user_mode == 'user' && isset($user_groups_default[$ug_id]) && sizeof($user_groups_default[$ug_id])) ? implode(', ', $user_groups_default[$ug_id]) : '',
547
'USER_GROUPS_CUSTOM' => ($user_mode == 'user' && isset($user_groups_custom[$ug_id]) && sizeof($user_groups_custom[$ug_id])) ? implode(', ', $user_groups_custom[$ug_id]) : '',
548
'L_ACL_TYPE' => $l_acl_type,
550
'S_LOCAL' => ($local) ? true : false,
551
'S_GLOBAL' => (!$local) ? true : false,
552
'S_NUM_CATS' => sizeof($categories),
553
'S_VIEW' => ($mode == 'view') ? true : false,
554
'S_NUM_OBJECTS' => sizeof($content_array),
555
'S_USER_MODE' => ($user_mode == 'user') ? true : false,
556
'S_GROUP_MODE' => ($user_mode == 'group') ? true : false)
559
@reset($content_array);
560
while (list($forum_id, $forum_array) = each($content_array))
562
// Build role dropdown options
563
$current_role_id = (isset($cur_roles[$ug_id][$forum_id])) ? $cur_roles[$ug_id][$forum_id] : 0;
565
$s_role_options = '';
568
while (list($role_id, $role_row) = each($roles))
570
$role_description = (!empty($user->lang[$role_row['role_description']])) ? $user->lang[$role_row['role_description']] : nl2br($role_row['role_description']);
571
$role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
573
$title = ($role_description) ? ' title="' . $role_description . '"' : '';
574
$s_role_options .= '<option value="' . $role_id . '"' . (($role_id == $current_role_id) ? ' selected="selected"' : '') . $title . '>' . $role_name . '</option>';
579
$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;
582
if (!$current_role_id && $mode != 'view')
584
$s_custom_permissions = false;
586
foreach ($forum_array as $key => $value)
588
if ($value['S_NEVER'] || $value['S_YES'])
590
$s_custom_permissions = true;
597
$s_custom_permissions = false;
600
$template->assign_block_vars($tpl_pmask . '.' . $tpl_fmask, array(
601
'NAME' => ($forum_id == 0) ? $forum_names_ary[0] : $forum_names_ary[$forum_id]['forum_name'],
602
'PADDING' => ($forum_id == 0) ? '' : $forum_names_ary[$forum_id]['padding'],
603
'S_ROLE_OPTIONS' => $s_role_options,
604
'S_CUSTOM' => $s_custom_permissions,
606
'FORUM_ID' => $forum_id)
609
$this->assign_cat_array($forum_array, $tpl_pmask . '.' . $tpl_fmask . '.' . $tpl_category, $tpl_mask, $ug_id, $forum_id, $show_trace, ($mode == 'view'));
612
unset($hold_ary[$ug_id], $ug_names_ary[$ug_id]);
618
* Display permission mask for roles
620
function display_role_mask(&$hold_ary)
622
global $db, $template, $user, $phpbb_root_path, $phpbb_admin_path, $phpEx;
624
if (!sizeof($hold_ary))
630
$sql = 'SELECT forum_id, forum_name
631
FROM ' . FORUMS_TABLE . '
632
WHERE ' . $db->sql_in_set('forum_id', array_keys($hold_ary)) . '
634
$result = $db->sql_query($sql);
636
// If the role is used globally, then reflect that
637
$forum_names = (isset($hold_ary[0])) ? array(0 => '') : array();
638
while ($row = $db->sql_fetchrow($result))
640
$forum_names[$row['forum_id']] = $row['forum_name'];
642
$db->sql_freeresult($result);
644
foreach ($forum_names as $forum_id => $forum_name)
646
$auth_ary = $hold_ary[$forum_id];
648
$template->assign_block_vars('role_mask', array(
649
'NAME' => ($forum_id == 0) ? $user->lang['GLOBAL_MASK'] : $forum_name,
650
'FORUM_ID' => $forum_id)
653
if (isset($auth_ary['users']) && sizeof($auth_ary['users']))
655
$sql = 'SELECT user_id, username
656
FROM ' . USERS_TABLE . '
657
WHERE ' . $db->sql_in_set('user_id', $auth_ary['users']) . '
658
ORDER BY username_clean ASC';
659
$result = $db->sql_query($sql);
661
while ($row = $db->sql_fetchrow($result))
663
$template->assign_block_vars('role_mask.users', array(
664
'USER_ID' => $row['user_id'],
665
'USERNAME' => $row['username'],
666
'U_PROFILE' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&u={$row['user_id']}"))
669
$db->sql_freeresult($result);
672
if (isset($auth_ary['groups']) && sizeof($auth_ary['groups']))
674
$sql = 'SELECT group_id, group_name, group_type
675
FROM ' . GROUPS_TABLE . '
676
WHERE ' . $db->sql_in_set('group_id', $auth_ary['groups']) . '
677
ORDER BY group_type ASC, group_name';
678
$result = $db->sql_query($sql);
680
while ($row = $db->sql_fetchrow($result))
682
$template->assign_block_vars('role_mask.groups', array(
683
'GROUP_ID' => $row['group_id'],
684
'GROUP_NAME' => ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'],
685
'U_PROFILE' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=group&g={$row['group_id']}"))
688
$db->sql_freeresult($result);
694
* NOTE: this function is not in use atm
695
* Add a new option to the list ... $options is a hash of form ->
697
* 'local' => array('option1', 'option2', ...),
698
* 'global' => array('optionA', 'optionB', ...)
701
function acl_add_option($options)
705
if (!is_array($options))
710
$cur_options = array();
712
$sql = 'SELECT auth_option, is_global, is_local
713
FROM ' . ACL_OPTIONS_TABLE . '
714
ORDER BY auth_option_id';
715
$result = $db->sql_query($sql);
717
while ($row = $db->sql_fetchrow($result))
719
if ($row['is_global'])
721
$cur_options['global'][] = $row['auth_option'];
724
if ($row['is_local'])
726
$cur_options['local'][] = $row['auth_option'];
729
$db->sql_freeresult($result);
731
// Here we need to insert new options ... this requires discovering whether
732
// an options is global, local or both and whether we need to add an permission
734
$new_options = array('local' => array(), 'global' => array());
736
foreach ($options as $type => $option_ary)
738
$option_ary = array_unique($option_ary);
740
foreach ($option_ary as $option_value)
742
if (!in_array($option_value, $cur_options[$type]))
744
$new_options[$type][] = $option_value;
747
$flag = substr($option_value, 0, strpos($option_value, '_') + 1);
749
if (!in_array($flag, $cur_options[$type]) && !in_array($flag, $new_options[$type]))
751
$new_options[$type][] = $flag;
758
$options['local'] = array_diff($new_options['local'], $new_options['global']);
759
$options['global'] = array_diff($new_options['global'], $new_options['local']);
760
$options['local_global'] = array_intersect($new_options['local'], $new_options['global']);
764
foreach ($options as $type => $option_ary)
766
foreach ($option_ary as $option)
769
'auth_option' => (string) $option,
770
'is_global' => ($type == 'global' || $type == 'local_global') ? 1 : 0,
771
'is_local' => ($type == 'local' || $type == 'local_global') ? 1 : 0
776
$db->sql_multi_insert(ACL_OPTIONS_TABLE, $sql_ary);
778
$cache->destroy('_acl_options');
779
$this->acl_clear_prefetch();
785
* Set a user or group ACL record
787
function acl_set($ug_type, $forum_id, $ug_id, $auth, $role_id = 0, $clear_prefetch = true)
791
// One or more forums
792
if (!is_array($forum_id))
794
$forum_id = array($forum_id);
798
if (!is_array($ug_id))
800
$ug_id = array($ug_id);
803
$ug_id_sql = $db->sql_in_set($ug_type . '_id', array_map('intval', $ug_id));
804
$forum_sql = $db->sql_in_set('forum_id', array_map('intval', $forum_id));
806
// Instead of updating, inserting, removing we just remove all current settings and re-set everything...
807
$table = ($ug_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
808
$id_field = $ug_type . '_id';
810
// Get any flags as required
813
$flag = substr($flag, 0, strpos($flag, '_') + 1);
815
// This ID (the any-flag) is set if one or more permissions are true...
816
$any_option_id = (int) $this->option_ids[$flag];
818
// Remove any-flag from auth ary
819
if (isset($auth[$flag]))
824
// Remove current auth options...
825
$auth_option_ids = array((int)$any_option_id);
826
foreach ($auth as $auth_option => $auth_setting)
828
$auth_option_ids[] = (int) $this->option_ids[$auth_option];
831
$sql = "DELETE FROM $table
834
AND " . $db->sql_in_set('auth_option_id', $auth_option_ids);
835
$db->sql_query($sql);
837
// Remove those having a role assigned... the correct type of course...
838
$sql = 'SELECT role_id
839
FROM ' . ACL_ROLES_TABLE . "
840
WHERE role_type = '" . $db->sql_escape($flag) . "'";
841
$result = $db->sql_query($sql);
844
while ($row = $db->sql_fetchrow($result))
846
$role_ids[] = $row['role_id'];
848
$db->sql_freeresult($result);
850
if (sizeof($role_ids))
852
$sql = "DELETE FROM $table
855
AND auth_option_id = 0
856
AND " . $db->sql_in_set('auth_role_id', $role_ids);
857
$db->sql_query($sql);
860
// Ok, include the any-flag if one or more auth options are set to yes...
861
foreach ($auth as $auth_option => $setting)
863
if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
865
$auth[$flag] = ACL_YES;
870
foreach ($forum_id as $forum)
872
$forum = (int) $forum;
876
foreach ($ug_id as $id)
879
$id_field => (int) $id,
880
'forum_id' => (int) $forum,
881
'auth_option_id' => 0,
883
'auth_role_id' => (int) $role_id,
889
foreach ($auth as $auth_option => $setting)
891
$auth_option_id = (int) $this->option_ids[$auth_option];
893
if ($setting != ACL_NO)
895
foreach ($ug_id as $id)
898
$id_field => (int) $id,
899
'forum_id' => (int) $forum,
900
'auth_option_id' => (int) $auth_option_id,
901
'auth_setting' => (int) $setting
909
$db->sql_multi_insert($table, $sql_ary);
913
$this->acl_clear_prefetch();
918
* Set a role-specific ACL record
920
function acl_set_role($role_id, $auth)
924
// Get any-flag as required
927
$flag = substr($flag, 0, strpos($flag, '_') + 1);
929
// Remove any-flag from auth ary
930
if (isset($auth[$flag]))
935
// Re-set any flag...
936
foreach ($auth as $auth_option => $setting)
938
if ($setting == ACL_YES && (!isset($auth[$flag]) || $auth[$flag] == ACL_NEVER))
940
$auth[$flag] = ACL_YES;
945
foreach ($auth as $auth_option => $setting)
947
$auth_option_id = (int) $this->option_ids[$auth_option];
949
if ($setting != ACL_NO)
952
'role_id' => (int) $role_id,
953
'auth_option_id' => (int) $auth_option_id,
954
'auth_setting' => (int) $setting
959
// If no data is there, we set the any-flag to ACL_NEVER...
960
if (!sizeof($sql_ary))
963
'role_id' => (int) $role_id,
964
'auth_option_id' => (int) $this->option_ids[$flag],
965
'auth_setting' => ACL_NEVER
969
// Remove current auth options...
970
$sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . '
971
WHERE role_id = ' . $role_id;
972
$db->sql_query($sql);
974
// Now insert the new values
975
$db->sql_multi_insert(ACL_ROLES_DATA_TABLE, $sql_ary);
977
$this->acl_clear_prefetch();
981
* Remove local permission
983
function acl_delete($mode, $ug_id = false, $forum_id = false, $permission_type = false)
987
if ($ug_id === false && $forum_id === false)
992
$option_id_ary = array();
993
$table = ($mode == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
994
$id_field = $mode . '_id';
996
$where_sql = array();
998
if ($forum_id !== false)
1000
$where_sql[] = (!is_array($forum_id)) ? 'forum_id = ' . (int) $forum_id : $db->sql_in_set('forum_id', array_map('intval', $forum_id));
1003
if ($ug_id !== false)
1005
$where_sql[] = (!is_array($ug_id)) ? $id_field . ' = ' . (int) $ug_id : $db->sql_in_set($id_field, array_map('intval', $ug_id));
1008
// There seem to be auth options involved, therefore we need to go through the list and make sure we capture roles correctly
1009
if ($permission_type !== false)
1011
// Get permission type
1012
$sql = 'SELECT auth_option, auth_option_id
1013
FROM ' . ACL_OPTIONS_TABLE . "
1014
WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char);
1015
$result = $db->sql_query($sql);
1017
$auth_id_ary = array();
1018
while ($row = $db->sql_fetchrow($result))
1020
$option_id_ary[] = $row['auth_option_id'];
1021
$auth_id_ary[$row['auth_option']] = ACL_NO;
1023
$db->sql_freeresult($result);
1025
// First of all, lets grab the items having roles with the specified auth options assigned
1026
$sql = "SELECT auth_role_id, $id_field, forum_id
1027
FROM $table, " . ACL_ROLES_TABLE . " r
1028
WHERE auth_role_id <> 0
1029
AND auth_role_id = r.role_id
1030
AND r.role_type = '{$permission_type}'
1031
AND " . implode(' AND ', $where_sql) . '
1032
ORDER BY auth_role_id';
1033
$result = $db->sql_query($sql);
1035
$cur_role_auth = array();
1036
while ($row = $db->sql_fetchrow($result))
1038
$cur_role_auth[$row['auth_role_id']][$row['forum_id']][] = $row[$id_field];
1040
$db->sql_freeresult($result);
1042
// Get role data for resetting data
1043
if (sizeof($cur_role_auth))
1045
$sql = 'SELECT ao.auth_option, rd.role_id, rd.auth_setting
1046
FROM ' . ACL_OPTIONS_TABLE . ' ao, ' . ACL_ROLES_DATA_TABLE . ' rd
1047
WHERE ao.auth_option_id = rd.auth_option_id
1048
AND ' . $db->sql_in_set('rd.role_id', array_keys($cur_role_auth));
1049
$result = $db->sql_query($sql);
1051
$auth_settings = array();
1052
while ($row = $db->sql_fetchrow($result))
1054
// We need to fill all auth_options, else setting it will fail...
1055
if (!isset($auth_settings[$row['role_id']]))
1057
$auth_settings[$row['role_id']] = $auth_id_ary;
1059
$auth_settings[$row['role_id']][$row['auth_option']] = $row['auth_setting'];
1061
$db->sql_freeresult($result);
1064
foreach ($cur_role_auth as $role_id => $auth_row)
1066
foreach ($auth_row as $f_id => $ug_row)
1068
$this->acl_set($mode, $f_id, $ug_row, $auth_settings[$role_id], 0, false);
1074
// Now, normally remove permissions...
1075
if ($permission_type !== false)
1077
$where_sql[] = $db->sql_in_set('auth_option_id', array_map('intval', $option_id_ary));
1080
$sql = "DELETE FROM $table
1081
WHERE " . implode(' AND ', $where_sql);
1082
$db->sql_query($sql);
1084
$this->acl_clear_prefetch();
1088
* Assign category to template
1089
* used by display_mask()
1091
function assign_cat_array(&$category_array, $tpl_cat, $tpl_mask, $ug_id, $forum_id, $show_trace = false, $s_view)
1093
global $template, $user, $phpbb_admin_path, $phpEx;
1095
@reset($category_array);
1096
while (list($cat, $cat_array) = each($category_array))
1098
$template->assign_block_vars($tpl_cat, array(
1099
'S_YES' => ($cat_array['S_YES'] && !$cat_array['S_NEVER'] && !$cat_array['S_NO']) ? true : false,
1100
'S_NEVER' => ($cat_array['S_NEVER'] && !$cat_array['S_YES'] && !$cat_array['S_NO']) ? true : false,
1101
'S_NO' => ($cat_array['S_NO'] && !$cat_array['S_NEVER'] && !$cat_array['S_YES']) ? true : false,
1103
'CAT_NAME' => $user->lang['permission_cat'][$cat])
1106
/* Sort permissions by name (more naturaly and user friendly than sorting by a primary key)
1107
* Commented out due to it's memory consumption and time needed
1109
$key_array = array_intersect(array_keys($user->lang), array_map(create_function('$a', 'return "acl_" . $a;'), array_keys($cat_array['permissions'])));
1110
$values_array = $cat_array['permissions'];
1112
$cat_array['permissions'] = array();
1114
foreach ($key_array as $key)
1116
$key = str_replace('acl_', '', $key);
1117
$cat_array['permissions'][$key] = $values_array[$key];
1119
unset($key_array, $values_array);
1121
@reset($cat_array['permissions']);
1122
while (list($permission, $allowed) = each($cat_array['permissions']))
1126
$template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
1127
'S_YES' => ($allowed == ACL_YES) ? true : false,
1128
'S_NEVER' => ($allowed == ACL_NEVER) ? true : false,
1131
'FORUM_ID' => $forum_id,
1132
'FIELD_NAME' => $permission,
1133
'S_FIELD_NAME' => 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
1135
'U_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission") : '',
1136
'UA_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
1138
'PERMISSION' => $user->lang['acl_' . $permission]['lang'])
1143
$template->assign_block_vars($tpl_cat . '.' . $tpl_mask, array(
1144
'S_YES' => ($allowed == ACL_YES) ? true : false,
1145
'S_NEVER' => ($allowed == ACL_NEVER) ? true : false,
1146
'S_NO' => ($allowed == ACL_NO) ? true : false,
1149
'FORUM_ID' => $forum_id,
1150
'FIELD_NAME' => $permission,
1151
'S_FIELD_NAME' => 'setting[' . $ug_id . '][' . $forum_id . '][' . $permission . ']',
1153
'U_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission") : '',
1154
'UA_TRACE' => ($show_trace) ? append_sid("{$phpbb_admin_path}index.$phpEx", "i=permissions&mode=trace&u=$ug_id&f=$forum_id&auth=$permission", false) : '',
1156
'PERMISSION' => $user->lang['acl_' . $permission]['lang'])
1164
* Building content array from permission rows with explicit key ordering
1165
* used by display_mask()
1167
function build_permission_array(&$permission_row, &$content_array, &$categories, $key_sort_array)
1171
foreach ($key_sort_array as $forum_id)
1173
if (!isset($permission_row[$forum_id]))
1178
$permissions = $permission_row[$forum_id];
1179
ksort($permissions);
1181
@reset($permissions);
1182
while (list($permission, $auth_setting) = each($permissions))
1184
if (!isset($user->lang['acl_' . $permission]))
1186
$user->lang['acl_' . $permission] = array(
1188
'lang' => '{ acl_' . $permission . ' }'
1192
$cat = $user->lang['acl_' . $permission]['cat'];
1194
// Build our categories array
1195
if (!isset($categories[$cat]))
1197
$categories[$cat] = $user->lang['permission_cat'][$cat];
1200
// Build our content array
1201
if (!isset($content_array[$forum_id]))
1203
$content_array[$forum_id] = array();
1206
if (!isset($content_array[$forum_id][$cat]))
1208
$content_array[$forum_id][$cat] = array(
1212
'permissions' => array(),
1216
$content_array[$forum_id][$cat]['S_YES'] |= ($auth_setting == ACL_YES) ? true : false;
1217
$content_array[$forum_id][$cat]['S_NEVER'] |= ($auth_setting == ACL_NEVER) ? true : false;
1218
$content_array[$forum_id][$cat]['S_NO'] |= ($auth_setting == ACL_NO) ? true : false;
1220
$content_array[$forum_id][$cat]['permissions'][$permission] = $auth_setting;
1226
* Use permissions from another user. This transferes a permission set from one user to another.
1227
* The other user is always able to revert back to his permission set.
1228
* This function does not check for lower/higher permissions, it is possible for the user to gain
1229
* "more" permissions by this.
1230
* Admin permissions will not be copied.
1232
function ghost_permissions($from_user_id, $to_user_id)
1236
if ($to_user_id == ANONYMOUS)
1241
$hold_ary = $this->acl_raw_data($from_user_id, false, false);
1243
if (isset($hold_ary[$from_user_id]))
1245
$hold_ary = $hold_ary[$from_user_id];
1248
// Key 0 in $hold_ary are global options, all others are forum_ids
1250
// We disallow copying admin permissions
1251
foreach ($this->acl_options['global'] as $opt => $id)
1253
if (strpos($opt, 'a_') === 0)
1255
$hold_ary[0][$opt] = ACL_NEVER;
1259
// Force a_switchperm to be allowed
1260
$hold_ary[0]['a_switchperm'] = ACL_YES;
1262
$user_permissions = $this->build_bitstring($hold_ary);
1264
if (!$user_permissions)
1269
$sql = 'UPDATE ' . USERS_TABLE . "
1270
SET user_permissions = '" . $db->sql_escape($user_permissions) . "',
1271
user_perm_from = $from_user_id
1272
WHERE user_id = " . $to_user_id;
1273
$db->sql_query($sql);
b'\\ No newline at end of file'