5
* @version $Id: auth.php,v 1.88 2007/10/05 14:30:07 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
* Permission/Auth class
27
var $acl_options = array();
28
var $acl_forum_ids = false;
33
function acl(&$userdata)
37
$this->acl = $this->cache = $this->acl_options = array();
38
$this->acl_forum_ids = false;
40
if (($this->acl_options = $cache->get('_acl_options')) === false)
42
$sql = 'SELECT auth_option, is_global, is_local
43
FROM ' . ACL_OPTIONS_TABLE . '
44
ORDER BY auth_option_id';
45
$result = $db->sql_query($sql);
48
$this->acl_options = array();
49
while ($row = $db->sql_fetchrow($result))
51
if ($row['is_global'])
53
$this->acl_options['global'][$row['auth_option']] = $global++;
58
$this->acl_options['local'][$row['auth_option']] = $local++;
61
$db->sql_freeresult($result);
63
$cache->put('_acl_options', $this->acl_options);
64
$this->acl_cache($userdata);
66
else if (!trim($userdata['user_permissions']))
68
$this->acl_cache($userdata);
71
$user_permissions = explode("\n", $userdata['user_permissions']);
73
foreach ($user_permissions as $f => $seq)
79
if (!isset($this->acl[$f]))
84
while ($subseq = substr($seq, $i, 6))
86
// We put the original bitstring into the acl array
87
$this->acl[$f] .= str_pad(base_convert($subseq, 36, 2), 31, 0, STR_PAD_LEFT);
98
* if the option is prefixed with !, then the result becomes negated
100
* If a forum id is specified the local option will be combined with a global option if one exist.
101
* If a forum id is not specified, only the global option will be checked.
103
function acl_get($opt, $f = 0)
107
if (strpos($opt, '!') === 0)
110
$opt = substr($opt, 1);
113
if (!isset($this->cache[$f][$opt]))
115
// We combine the global/local option with an OR because some options are global and local.
116
// If the user has the global permission the local one is true too and vice versa
117
$this->cache[$f][$opt] = false;
119
// Is this option a global permission setting?
120
if (isset($this->acl_options['global'][$opt]))
122
if (isset($this->acl[0]))
124
$this->cache[$f][$opt] = $this->acl[0][$this->acl_options['global'][$opt]];
128
// Is this option a local permission setting?
129
// But if we check for a global option only, we won't combine the options...
130
if ($f != 0 && isset($this->acl_options['local'][$opt]))
132
if (isset($this->acl[$f]) && isset($this->acl[$f][$this->acl_options['local'][$opt]]))
134
$this->cache[$f][$opt] |= $this->acl[$f][$this->acl_options['local'][$opt]];
139
// Founder always has all global options set to true...
140
return ($negate) ? !$this->cache[$f][$opt] : $this->cache[$f][$opt];
144
* Get forums with the specified permission setting
145
* if the option is prefixed with !, then the result becomes nagated
147
* @param bool $clean set to true if only values needs to be returned which are set/unset
149
function acl_getf($opt, $clean = false)
154
if (strpos($opt, '!') === 0)
157
$opt = substr($opt, 1);
160
// If we retrieve a list of forums not having permissions in, we need to get every forum_id
163
if ($this->acl_forum_ids === false)
167
$sql = 'SELECT forum_id
168
FROM ' . FORUMS_TABLE;
170
if (sizeof($this->acl))
172
$sql .= ' WHERE ' . $db->sql_in_set('forum_id', array_keys($this->acl), true);
174
$result = $db->sql_query($sql);
176
$this->acl_forum_ids = array();
177
while ($row = $db->sql_fetchrow($result))
179
$this->acl_forum_ids[] = $row['forum_id'];
181
$db->sql_freeresult($result);
185
if (isset($this->acl_options['local'][$opt]))
187
foreach ($this->acl as $f => $bitstring)
189
// Skip global settings
195
$allowed = (!isset($this->cache[$f][$opt])) ? $this->acl_get($opt, $f) : $this->cache[$f][$opt];
199
$acl_f[$f][$opt] = ($negate) ? !$allowed : $allowed;
203
if (($negate && !$allowed) || (!$negate && $allowed))
205
$acl_f[$f][$opt] = 1;
211
// If we get forum_ids not having this permission, we need to fill the remaining parts
212
if ($negate && sizeof($this->acl_forum_ids))
214
foreach ($this->acl_forum_ids as $f)
216
$acl_f[$f][$opt] = 1;
224
* Get local permission state for any forum.
226
* Returns true if user has the permission in one or more forums, false if in no forum.
227
* If global option is checked it returns the global state (same as acl_get($opt))
228
* Local option has precedence...
230
function acl_getf_global($opt)
234
// evaluates to true as soon as acl_getf_global is true for one option
235
foreach ($opt as $check_option)
237
if ($this->acl_getf_global($check_option))
246
if (isset($this->acl_options['local'][$opt]))
248
foreach ($this->acl as $f => $bitstring)
250
// Skip global settings
256
// as soon as the user has any permission we're done so return true
257
if ((!isset($this->cache[$f][$opt])) ? $this->acl_get($opt, $f) : $this->cache[$f][$opt])
263
else if (isset($this->acl_options['global'][$opt]))
265
return $this->acl_get($opt);
272
* Get permission settings (more than one)
276
$args = func_get_args();
277
$f = array_pop($args);
285
// alternate syntax: acl_gets(array('m_', 'a_'), $forum_id)
286
if (is_array($args[0]))
292
foreach ($args as $opt)
294
$acl |= $this->acl_get($opt, $f);
301
* Get permission listing based on user_id/options/forum_ids
303
function acl_get_list($user_id = false, $opts = false, $forum_id = false)
305
$hold_ary = $this->acl_raw_data($user_id, $opts, $forum_id);
308
foreach ($hold_ary as $user_id => $forum_ary)
310
foreach ($forum_ary as $forum_id => $auth_option_ary)
312
foreach ($auth_option_ary as $auth_option => $auth_setting)
316
$auth_ary[$forum_id][$auth_option][] = $user_id;
326
* Cache data to user_permissions row
328
function acl_cache(&$userdata)
332
// Empty user_permissions
333
$userdata['user_permissions'] = '';
335
$hold_ary = $this->acl_raw_data($userdata['user_id'], false, false);
337
if (isset($hold_ary[$userdata['user_id']]))
339
$hold_ary = $hold_ary[$userdata['user_id']];
342
// Key 0 in $hold_ary are global options, all others are forum_ids
344
// If this user is founder we're going to force fill the admin options ...
345
if ($userdata['user_type'] == USER_FOUNDER)
347
foreach ($this->acl_options['global'] as $opt => $id)
349
if (strpos($opt, 'a_') === 0)
351
$hold_ary[0][$opt] = ACL_YES;
356
// Sometimes, it can happen $hold_ary holding forums which do not exist.
357
// Since this function is not called that often (we are caching the data) we check for this inconsistency.
358
$sql = 'SELECT forum_id
359
FROM ' . FORUMS_TABLE . '
360
WHERE ' . $db->sql_in_set('forum_id', array_keys($hold_ary), false, true);
361
$result = $db->sql_query($sql);
363
$forum_ids = (isset($hold_ary[0])) ? array(0) : array();
364
while ($row = $db->sql_fetchrow($result))
366
$forum_ids[] = $row['forum_id'];
368
$db->sql_freeresult($result);
370
// Now determine forums which do not exist and remove the unneeded information (for modding purposes it is clearly the wrong place. ;))
371
$missing_forums = array_diff(array_keys($hold_ary), $forum_ids);
373
if (sizeof($missing_forums))
375
foreach ($missing_forums as $forum_id)
377
unset($hold_ary[$forum_id]);
380
$sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . ' WHERE ' . $db->sql_in_set('forum_id', $missing_forums);
381
$db->sql_query($sql);
383
$sql = 'DELETE FROM ' . ACL_USERS_TABLE . ' WHERE ' . $db->sql_in_set('forum_id', $missing_forums);
384
$db->sql_query($sql);
387
$hold_str = $this->build_bitstring($hold_ary);
391
$userdata['user_permissions'] = $hold_str;
393
$sql = 'UPDATE ' . USERS_TABLE . "
394
SET user_permissions = '" . $db->sql_escape($userdata['user_permissions']) . "',
396
WHERE user_id = " . $userdata['user_id'];
397
$db->sql_query($sql);
404
* Build bitstring from permission set
406
function build_bitstring(&$hold_ary)
410
if (sizeof($hold_ary))
416
foreach ($hold_ary as $f => $auth_ary)
418
$ary_key = (!$f) ? 'global' : 'local';
420
$bitstring = array();
421
foreach ($this->acl_options[$ary_key] as $opt => $id)
423
if (isset($auth_ary[$opt]))
425
$bitstring[$id] = $auth_ary[$opt];
427
$option_key = substr($opt, 0, strpos($opt, '_') + 1);
429
// If one option is allowed, the global permission for this option has to be allowed too
430
// example: if the user has the a_ permission this means he has one or more a_* permissions
431
if ($auth_ary[$opt] == ACL_YES && (!isset($bitstring[$this->acl_options[$ary_key][$option_key]]) || $bitstring[$this->acl_options[$ary_key][$option_key]] == ACL_NEVER))
433
$bitstring[$this->acl_options[$ary_key][$option_key]] = ACL_YES;
438
$bitstring[$id] = ACL_NEVER;
442
// Now this bitstring defines the permission setting for the current forum $f (or global setting)
443
$bitstring = implode('', $bitstring);
445
// The line number indicates the id, therefore we have to add empty lines for those ids not present
446
$hold_str .= str_repeat("\n", $f - $last_f);
448
// Convert bitstring for storage - we do not use binary/bytes because PHP's string functions are not fully binary safe
449
for ($i = 0, $bit_length = strlen($bitstring); $i < $bit_length; $i += 31)
451
$hold_str .= str_pad(base_convert(str_pad(substr($bitstring, $i, 31), 31, 0, STR_PAD_RIGHT), 2, 36), 6, 0, STR_PAD_LEFT);
458
$hold_str = rtrim($hold_str);
465
* Clear one or all users cached permission settings
467
function acl_clear_prefetch($user_id = false)
473
if ($user_id !== false)
475
$user_id = (!is_array($user_id)) ? $user_id = array((int) $user_id) : array_map('intval', $user_id);
476
$where_sql = ' WHERE ' . $db->sql_in_set('user_id', $user_id);
479
$sql = 'UPDATE ' . USERS_TABLE . "
480
SET user_permissions = '',
483
$db->sql_query($sql);
491
function acl_role_data($user_type, $role_type, $ug_id = false, $forum_id = false)
497
$sql_id = ($user_type == 'user') ? 'user_id' : 'group_id';
499
$sql_ug = ($ug_id !== false) ? ((!is_array($ug_id)) ? "AND a.$sql_id = $ug_id" : 'AND ' . $db->sql_in_set("a.$sql_id", $ug_id)) : '';
500
$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? "AND a.forum_id = $forum_id" : 'AND ' . $db->sql_in_set('a.forum_id', $forum_id)) : '';
502
// Grab assigned roles...
503
$sql = 'SELECT a.auth_role_id, a.' . $sql_id . ', a.forum_id
504
FROM ' . (($user_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE) . ' a, ' . ACL_ROLES_TABLE . " r
505
WHERE a.auth_role_id = r.role_id
506
AND r.role_type = '" . $db->sql_escape($role_type) . "'
509
ORDER BY r.role_order ASC";
510
$result = $db->sql_query($sql);
512
while ($row = $db->sql_fetchrow($result))
514
$roles[$row[$sql_id]][$row['forum_id']] = $row['auth_role_id'];
516
$db->sql_freeresult($result);
522
* Get raw acl data based on user/option/forum
524
function acl_raw_data($user_id = false, $opts = false, $forum_id = false)
528
$sql_user = ($user_id !== false) ? ((!is_array($user_id)) ? 'user_id = ' . (int) $user_id : $db->sql_in_set('user_id', array_map('intval', $user_id))) : '';
529
$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : '';
535
$this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
540
// First grab user settings ... each user has only one setting for each
541
// option ... so we shouldn't need any ACL_NEVER checks ... he says ...
542
// Grab assigned roles...
543
$sql = $db->sql_build_query('SELECT', array(
544
'SELECT' => 'ao.auth_option, a.auth_role_id, r.auth_setting as role_auth_setting, a.user_id, a.forum_id, a.auth_setting',
547
ACL_OPTIONS_TABLE => 'ao',
548
ACL_USERS_TABLE => 'a'
551
'LEFT_JOIN' => array(
553
'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
554
'ON' => 'a.auth_role_id = r.role_id'
558
'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
559
' . (($sql_user) ? 'AND a.' . $sql_user : '') . "
563
$result = $db->sql_query($sql);
565
while ($row = $db->sql_fetchrow($result))
567
$setting = ($row['auth_role_id']) ? $row['role_auth_setting'] : $row['auth_setting'];
568
$hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']] = $setting;
570
$db->sql_freeresult($result);
572
// Now grab group settings ... ACL_NEVER overrides ACL_YES so act appropriatley
573
$sql_ary[] = $db->sql_build_query('SELECT', array(
574
'SELECT' => 'ug.user_id, ao.auth_option, a.forum_id, a.auth_setting, a.auth_role_id, r.auth_setting as role_auth_setting',
577
USER_GROUP_TABLE => 'ug',
578
ACL_OPTIONS_TABLE => 'ao',
579
ACL_GROUPS_TABLE => 'a'
582
'LEFT_JOIN' => array(
584
'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
585
'ON' => 'a.auth_role_id = r.role_id'
589
'WHERE' => 'ao.auth_option_id = a.auth_option_id
590
AND a.group_id = ug.group_id
591
AND ug.user_pending = 0
592
' . (($sql_user) ? 'AND ug.' . $sql_user : '') . "
597
$sql_ary[] = $db->sql_build_query('SELECT', array(
598
'SELECT' => 'ug.user_id, a.forum_id, a.auth_setting, a.auth_role_id, r.auth_setting as role_auth_setting, ao.auth_option' ,
601
ACL_OPTIONS_TABLE => 'ao'
605
'LEFT_JOIN' => array(
608
'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
609
'ON' => 'r.auth_option_id = ao.auth_option_id'
612
'FROM' => array(ACL_GROUPS_TABLE => 'a'),
613
'ON' => 'a.auth_role_id = r.role_id'
616
'FROM' => array(USER_GROUP_TABLE => 'ug'),
617
'ON' => 'ug.group_id = a.group_id'
622
'WHERE' => 'ug.user_pending = 0
623
' . (($sql_user) ? 'AND ug.' . $sql_user : '') . "
629
foreach ($sql_ary as $sql)
631
$result = $db->sql_query($sql);
633
while ($row = $db->sql_fetchrow($result))
635
if (!isset($hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']]) || (isset($hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']]) && $hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']] != ACL_NEVER))
637
$setting = ($row['auth_role_id']) ? $row['role_auth_setting'] : $row['auth_setting'];
638
$hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']] = $setting;
640
// Check for existence of ACL_YES if an option got set to ACL_NEVER
641
if ($setting == ACL_NEVER)
643
$flag = substr($row['auth_option'], 0, strpos($row['auth_option'], '_') + 1);
645
if (isset($hold_ary[$row['user_id']][$row['forum_id']][$flag]) && $hold_ary[$row['user_id']][$row['forum_id']][$flag] == ACL_YES)
647
unset($hold_ary[$row['user_id']][$row['forum_id']][$flag]);
649
if (in_array(ACL_YES, $hold_ary[$row['user_id']][$row['forum_id']]))
651
$hold_ary[$row['user_id']][$row['forum_id']][$flag] = ACL_YES;
657
$db->sql_freeresult($result);
664
* Get raw user based permission settings
666
function acl_user_raw_data($user_id = false, $opts = false, $forum_id = false)
670
$sql_user = ($user_id !== false) ? ((!is_array($user_id)) ? 'user_id = ' . (int) $user_id : $db->sql_in_set('user_id', array_map('intval', $user_id))) : '';
671
$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : '';
677
$this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
682
// Grab user settings...
683
$sql = $db->sql_build_query('SELECT', array(
684
'SELECT' => 'ao.auth_option, a.auth_role_id, r.auth_setting as role_auth_setting, a.user_id, a.forum_id, a.auth_setting',
687
ACL_OPTIONS_TABLE => 'ao',
688
ACL_USERS_TABLE => 'a'
691
'LEFT_JOIN' => array(
693
'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
694
'ON' => 'a.auth_role_id = r.role_id'
698
'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
699
' . (($sql_user) ? 'AND a.' . $sql_user : '') . "
703
'ORDER_BY' => 'a.forum_id, ao.auth_option'
705
$result = $db->sql_query($sql);
707
while ($row = $db->sql_fetchrow($result))
709
$setting = ($row['auth_role_id']) ? $row['role_auth_setting'] : $row['auth_setting'];
710
$hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']] = $setting;
712
$db->sql_freeresult($result);
718
* Get raw group based permission settings
720
function acl_group_raw_data($group_id = false, $opts = false, $forum_id = false)
724
$sql_group = ($group_id !== false) ? ((!is_array($group_id)) ? 'group_id = ' . (int) $group_id : $db->sql_in_set('group_id', array_map('intval', $group_id))) : '';
725
$sql_forum = ($forum_id !== false) ? ((!is_array($forum_id)) ? 'AND a.forum_id = ' . (int) $forum_id : 'AND ' . $db->sql_in_set('a.forum_id', array_map('intval', $forum_id))) : '';
731
$this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
736
// Grab group settings...
737
$sql = $db->sql_build_query('SELECT', array(
738
'SELECT' => 'a.group_id, ao.auth_option, a.forum_id, a.auth_setting, a.auth_role_id, r.auth_setting as role_auth_setting',
741
ACL_OPTIONS_TABLE => 'ao',
742
ACL_GROUPS_TABLE => 'a'
745
'LEFT_JOIN' => array(
747
'FROM' => array(ACL_ROLES_DATA_TABLE => 'r'),
748
'ON' => 'a.auth_role_id = r.role_id'
752
'WHERE' => '(ao.auth_option_id = a.auth_option_id OR ao.auth_option_id = r.auth_option_id)
753
' . (($sql_group) ? 'AND a.' . $sql_group : '') . "
757
'ORDER_BY' => 'a.forum_id, ao.auth_option'
759
$result = $db->sql_query($sql);
761
while ($row = $db->sql_fetchrow($result))
763
$setting = ($row['auth_role_id']) ? $row['role_auth_setting'] : $row['auth_setting'];
764
$hold_ary[$row['group_id']][$row['forum_id']][$row['auth_option']] = $setting;
766
$db->sql_freeresult($result);
772
* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
774
function login($username, $password, $autologin = false, $viewonline = 1, $admin = 0)
776
global $config, $db, $user, $phpbb_root_path, $phpEx;
778
$method = trim(basename($config['auth_method']));
779
include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
781
$method = 'login_' . $method;
782
if (function_exists($method))
784
$login = $method($username, $password);
786
// If the auth module wants us to create an empty profile do so and then treat the status as LOGIN_SUCCESS
787
if ($login['status'] == LOGIN_SUCCESS_CREATE_PROFILE)
789
// we are going to use the user_add function so include functions_user.php if it wasn't defined yet
790
if (!function_exists('user_add'))
792
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
795
user_add($login['user_row'], (isset($login['cp_data'])) ? $login['cp_data'] : false);
797
$sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type
798
FROM ' . USERS_TABLE . "
799
WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
800
$result = $db->sql_query($sql);
801
$row = $db->sql_fetchrow($result);
802
$db->sql_freeresult($result);
807
'status' => LOGIN_ERROR_EXTERNAL_AUTH,
808
'error_msg' => 'AUTH_NO_PROFILE_CREATED',
809
'user_row' => array('user_id' => ANONYMOUS),
814
'status' => LOGIN_SUCCESS,
815
'error_msg' => false,
820
// If login succeeded, we will log the user in... else we pass the login array through...
821
if ($login['status'] == LOGIN_SUCCESS)
823
$old_session_id = $user->session_id;
829
$cookie_expire = time() - 31536000;
830
$user->set_cookie('u', '', $cookie_expire);
831
$user->set_cookie('sid', '', $cookie_expire);
832
unset($cookie_expire);
835
$user->session_id = $_SID = '';
838
$result = $user->session_create($login['user_row']['user_id'], $admin, $autologin, $viewonline);
840
// Successful session creation
841
if ($result === true)
843
// If admin re-authentication we remove the old session entry because a new one has been created...
846
// the login array is used because the user ids do not differ for re-authentication
847
$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
848
WHERE session_id = '" . $db->sql_escape($old_session_id) . "'
849
AND session_user_id = {$login['user_row']['user_id']}";
850
$db->sql_query($sql);
854
'status' => LOGIN_SUCCESS,
855
'error_msg' => false,
856
'user_row' => $login['user_row'],
861
'status' => LOGIN_BREAK,
862
'error_msg' => $result,
863
'user_row' => $login['user_row'],
870
trigger_error('Authentication method not found', E_USER_ERROR);
874
* Fill auth_option statement for later querying based on the supplied options
876
function build_auth_option_statement($key, $auth_options, &$sql_opts)
880
if (!is_array($auth_options))
882
if (strpos($auth_options, '%') !== false)
884
$sql_opts = "AND $key " . $db->sql_like_expression(str_replace('%', $db->any_char, $auth_options));
888
$sql_opts = "AND $key = '" . $db->sql_escape($auth_options) . "'";
893
$is_like_expression = false;
895
foreach ($auth_options as $option)
897
if (strpos($option, '%') !== false)
899
$is_like_expression = true;
903
if (!$is_like_expression)
905
$sql_opts = 'AND ' . $db->sql_in_set($key, $auth_options);
911
foreach ($auth_options as $option)
913
if (strpos($option, '%') !== false)
915
$sql[] = $key . ' ' . $db->sql_like_expression(str_replace('%', $db->any_char, $option));
919
$sql[] = $key . " = '" . $db->sql_escape($option) . "'";
923
$sql_opts = 'AND (' . implode(' OR ', $sql) . ')';
b'\\ No newline at end of file'