1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/auth.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,1064 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package phpBB3
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* Permission/Auth class
1.24 +* @package phpBB3
1.25 +*/
1.26 +class auth
1.27 +{
1.28 + var $acl = array();
1.29 + var $cache = array();
1.30 + var $acl_options = array();
1.31 + var $acl_forum_ids = false;
1.32 +
1.33 + /**
1.34 + * Init permissions
1.35 + */
1.36 + function acl(&$userdata)
1.37 + {
1.38 + global $db, $cache;
1.39 +
1.40 + $this->acl = $this->cache = $this->acl_options = array();
1.41 + $this->acl_forum_ids = false;
1.42 +
1.43 + if (($this->acl_options = $cache->get('_acl_options')) === false)
1.44 + {
1.45 + $sql = 'SELECT auth_option_id, auth_option, is_global, is_local
1.46 + FROM ' . ACL_OPTIONS_TABLE . '
1.47 + ORDER BY auth_option_id';
1.48 + $result = $db->sql_query($sql);
1.49 +
1.50 + $global = $local = 0;
1.51 + $this->acl_options = array();
1.52 + while ($row = $db->sql_fetchrow($result))
1.53 + {
1.54 + if ($row['is_global'])
1.55 + {
1.56 + $this->acl_options['global'][$row['auth_option']] = $global++;
1.57 + }
1.58 +
1.59 + if ($row['is_local'])
1.60 + {
1.61 + $this->acl_options['local'][$row['auth_option']] = $local++;
1.62 + }
1.63 +
1.64 + $this->acl_options['id'][$row['auth_option']] = (int) $row['auth_option_id'];
1.65 + $this->acl_options['option'][(int) $row['auth_option_id']] = $row['auth_option'];
1.66 + }
1.67 + $db->sql_freeresult($result);
1.68 +
1.69 + $cache->put('_acl_options', $this->acl_options);
1.70 + }
1.71 +
1.72 + if (!trim($userdata['user_permissions']))
1.73 + {
1.74 + $this->acl_cache($userdata);
1.75 + }
1.76 +
1.77 + // Fill ACL array
1.78 + $this->_fill_acl($userdata['user_permissions']);
1.79 +
1.80 + // Verify bitstring length with options provided...
1.81 + $renew = false;
1.82 + $global_length = sizeof($this->acl_options['global']);
1.83 + $local_length = sizeof($this->acl_options['local']);
1.84 +
1.85 + // Specify comparing length (bitstring is padded to 31 bits)
1.86 + $global_length = ($global_length % 31) ? ($global_length - ($global_length % 31) + 31) : $global_length;
1.87 + $local_length = ($local_length % 31) ? ($local_length - ($local_length % 31) + 31) : $local_length;
1.88 +
1.89 + // You thought we are finished now? Noooo... now compare them.
1.90 + foreach ($this->acl as $forum_id => $bitstring)
1.91 + {
1.92 + if (($forum_id && strlen($bitstring) != $local_length) || (!$forum_id && strlen($bitstring) != $global_length))
1.93 + {
1.94 + $renew = true;
1.95 + break;
1.96 + }
1.97 + }
1.98 +
1.99 + // If a bitstring within the list does not match the options, we have a user with incorrect permissions set and need to renew them
1.100 + if ($renew)
1.101 + {
1.102 + $this->acl_cache($userdata);
1.103 + $this->_fill_acl($userdata['user_permissions']);
1.104 + }
1.105 +
1.106 + return;
1.107 + }
1.108 +
1.109 + /**
1.110 + * Fill ACL array with relevant bitstrings from user_permissions column
1.111 + * @access private
1.112 + */
1.113 + function _fill_acl($user_permissions)
1.114 + {
1.115 + $seq_cache = array();
1.116 + $this->acl = array();
1.117 + $user_permissions = explode("\n", $user_permissions);
1.118 +
1.119 + foreach ($user_permissions as $f => $seq)
1.120 + {
1.121 + if ($seq)
1.122 + {
1.123 + $i = 0;
1.124 +
1.125 + if (!isset($this->acl[$f]))
1.126 + {
1.127 + $this->acl[$f] = '';
1.128 + }
1.129 +
1.130 + while ($subseq = substr($seq, $i, 6))
1.131 + {
1.132 + if (isset($seq_cache[$subseq]))
1.133 + {
1.134 + $converted = $seq_cache[$subseq];
1.135 + }
1.136 + else
1.137 + {
1.138 + $converted = $seq_cache[$subseq] = str_pad(base_convert($subseq, 36, 2), 31, 0, STR_PAD_LEFT);
1.139 + }
1.140 +
1.141 + // We put the original bitstring into the acl array
1.142 + $this->acl[$f] .= $converted;
1.143 + $i += 6;
1.144 + }
1.145 + }
1.146 + }
1.147 + }
1.148 +
1.149 + /**
1.150 + * Look up an option
1.151 + * if the option is prefixed with !, then the result becomes negated
1.152 + *
1.153 + * If a forum id is specified the local option will be combined with a global option if one exist.
1.154 + * If a forum id is not specified, only the global option will be checked.
1.155 + */
1.156 + function acl_get($opt, $f = 0)
1.157 + {
1.158 + $negate = false;
1.159 +
1.160 + if (strpos($opt, '!') === 0)
1.161 + {
1.162 + $negate = true;
1.163 + $opt = substr($opt, 1);
1.164 + }
1.165 +
1.166 + if (!isset($this->cache[$f][$opt]))
1.167 + {
1.168 + // We combine the global/local option with an OR because some options are global and local.
1.169 + // If the user has the global permission the local one is true too and vice versa
1.170 + $this->cache[$f][$opt] = false;
1.171 +
1.172 + // Is this option a global permission setting?
1.173 + if (isset($this->acl_options['global'][$opt]))
1.174 + {
1.175 + if (isset($this->acl[0]))
1.176 + {
1.177 + $this->cache[$f][$opt] = $this->acl[0][$this->acl_options['global'][$opt]];
1.178 + }
1.179 + }
1.180 +
1.181 + // Is this option a local permission setting?
1.182 + // But if we check for a global option only, we won't combine the options...
1.183 + if ($f != 0 && isset($this->acl_options['local'][$opt]))
1.184 + {
1.185 + if (isset($this->acl[$f]) && isset($this->acl[$f][$this->acl_options['local'][$opt]]))
1.186 + {
1.187 + $this->cache[$f][$opt] |= $this->acl[$f][$this->acl_options['local'][$opt]];
1.188 + }
1.189 + }
1.190 + }
1.191 +
1.192 + // Founder always has all global options set to true...
1.193 + return ($negate) ? !$this->cache[$f][$opt] : $this->cache[$f][$opt];
1.194 + }
1.195 +
1.196 + /**
1.197 + * Get forums with the specified permission setting
1.198 + * if the option is prefixed with !, then the result becomes nagated
1.199 + *
1.200 + * @param bool $clean set to true if only values needs to be returned which are set/unset
1.201 + */
1.202 + function acl_getf($opt, $clean = false)
1.203 + {
1.204 + $acl_f = array();
1.205 + $negate = false;
1.206 +
1.207 + if (strpos($opt, '!') === 0)
1.208 + {
1.209 + $negate = true;
1.210 + $opt = substr($opt, 1);
1.211 + }
1.212 +
1.213 + // If we retrieve a list of forums not having permissions in, we need to get every forum_id
1.214 + if ($negate)
1.215 + {
1.216 + if ($this->acl_forum_ids === false)
1.217 + {
1.218 + global $db;
1.219 +
1.220 + $sql = 'SELECT forum_id
1.221 + FROM ' . FORUMS_TABLE;
1.222 +
1.223 + if (sizeof($this->acl))
1.224 + {
1.225 + $sql .= ' WHERE ' . $db->sql_in_set('forum_id', array_keys($this->acl), true);
1.226 + }
1.227 + $result = $db->sql_query($sql);
1.228 +
1.229 + $this->acl_forum_ids = array();
1.230 + while ($row = $db->sql_fetchrow($result))
1.231 + {
1.232 + $this->acl_forum_ids[] = $row['forum_id'];
1.233 + }
1.234 + $db->sql_freeresult($result);
1.235 + }
1.236 + }
1.237 +
1.238 + if (isset($this->acl_options['local'][$opt]))
1.239 + {
1.240 + foreach ($this->acl as $f => $bitstring)
1.241 + {
1.242 + // Skip global settings
1.243 + if (!$f)
1.244 + {
1.245 + continue;
1.246 + }
1.247 +
1.248 + $allowed = (!isset($this->cache[$f][$opt])) ? $this->acl_get($opt, $f) : $this->cache[$f][$opt];
1.249 +
1.250 + if (!$clean)
1.251 + {
1.252 + $acl_f[$f][$opt] = ($negate) ? !$allowed : $allowed;
1.253 + }
1.254 + else
1.255 + {
1.256 + if (($negate && !$allowed) || (!$negate && $allowed))
1.257 + {
1.258 + $acl_f[$f][$opt] = 1;
1.259 + }
1.260 + }
1.261 + }
1.262 + }
1.263 +
1.264 + // If we get forum_ids not having this permission, we need to fill the remaining parts
1.265 + if ($negate && sizeof($this->acl_forum_ids))
1.266 + {
1.267 + foreach ($this->acl_forum_ids as $f)
1.268 + {
1.269 + $acl_f[$f][$opt] = 1;
1.270 + }
1.271 + }
1.272 +
1.273 + return $acl_f;
1.274 + }
1.275 +
1.276 + /**
1.277 + * Get local permission state for any forum.
1.278 + *
1.279 + * Returns true if user has the permission in one or more forums, false if in no forum.
1.280 + * If global option is checked it returns the global state (same as acl_get($opt))
1.281 + * Local option has precedence...
1.282 + */
1.283 + function acl_getf_global($opt)
1.284 + {
1.285 + if (is_array($opt))
1.286 + {
1.287 + // evaluates to true as soon as acl_getf_global is true for one option
1.288 + foreach ($opt as $check_option)
1.289 + {
1.290 + if ($this->acl_getf_global($check_option))
1.291 + {
1.292 + return true;
1.293 + }
1.294 + }
1.295 +
1.296 + return false;
1.297 + }
1.298 +
1.299 + if (isset($this->acl_options['local'][$opt]))
1.300 + {
1.301 + foreach ($this->acl as $f => $bitstring)
1.302 + {
1.303 + // Skip global settings
1.304 + if (!$f)
1.305 + {
1.306 + continue;
1.307 + }
1.308 +
1.309 + // as soon as the user has any permission we're done so return true
1.310 + if ((!isset($this->cache[$f][$opt])) ? $this->acl_get($opt, $f) : $this->cache[$f][$opt])
1.311 + {
1.312 + return true;
1.313 + }
1.314 + }
1.315 + }
1.316 + else if (isset($this->acl_options['global'][$opt]))
1.317 + {
1.318 + return $this->acl_get($opt);
1.319 + }
1.320 +
1.321 + return false;
1.322 + }
1.323 +
1.324 + /**
1.325 + * Get permission settings (more than one)
1.326 + */
1.327 + function acl_gets()
1.328 + {
1.329 + $args = func_get_args();
1.330 + $f = array_pop($args);
1.331 +
1.332 + if (!is_numeric($f))
1.333 + {
1.334 + $args[] = $f;
1.335 + $f = 0;
1.336 + }
1.337 +
1.338 + // alternate syntax: acl_gets(array('m_', 'a_'), $forum_id)
1.339 + if (is_array($args[0]))
1.340 + {
1.341 + $args = $args[0];
1.342 + }
1.343 +
1.344 + $acl = 0;
1.345 + foreach ($args as $opt)
1.346 + {
1.347 + $acl |= $this->acl_get($opt, $f);
1.348 + }
1.349 +
1.350 + return $acl;
1.351 + }
1.352 +
1.353 + /**
1.354 + * Get permission listing based on user_id/options/forum_ids
1.355 + *
1.356 + * Be careful when using this function with permissions a_, m_, u_ and f_ !
1.357 + * It may not work correctly. When a user group grants an a_* permission,
1.358 + * e.g. a_foo, but the user's a_foo permission is set to "Never", then
1.359 + * the user does not in fact have the a_ permission.
1.360 + * But the user will still be listed as having the a_ permission.
1.361 + *
1.362 + * For more information see: http://tracker.phpbb.com/browse/PHPBB3-10252
1.363 + */
1.364 + function acl_get_list($user_id = false, $opts = false, $forum_id = false)
1.365 + {
1.366 + if ($user_id !== false && !is_array($user_id) && $opts === false && $forum_id === false)
1.367 + {
1.368 + $hold_ary = array($user_id => $this->acl_raw_data_single_user($user_id));
1.369 + }
1.370 + else
1.371 + {
1.372 + $hold_ary = $this->acl_raw_data($user_id, $opts, $forum_id);
1.373 + }
1.374 +
1.375 + $auth_ary = array();
1.376 + foreach ($hold_ary as $user_id => $forum_ary)
1.377 + {
1.378 + foreach ($forum_ary as $forum_id => $auth_option_ary)
1.379 + {
1.380 + foreach ($auth_option_ary as $auth_option => $auth_setting)
1.381 + {
1.382 + if ($auth_setting)
1.383 + {
1.384 + $auth_ary[$forum_id][$auth_option][] = $user_id;
1.385 + }
1.386 + }
1.387 + }
1.388 + }
1.389 +
1.390 + return $auth_ary;
1.391 + }
1.392 +
1.393 + /**
1.394 + * Cache data to user_permissions row
1.395 + */
1.396 + function acl_cache(&$userdata)
1.397 + {
1.398 + global $db;
1.399 +
1.400 + // Empty user_permissions
1.401 + $userdata['user_permissions'] = '';
1.402 +
1.403 + $hold_ary = $this->acl_raw_data_single_user($userdata['user_id']);
1.404 +
1.405 + // Key 0 in $hold_ary are global options, all others are forum_ids
1.406 +
1.407 + // If this user is founder we're going to force fill the admin options ...
1.408 + if ($userdata['user_type'] == USER_FOUNDER)
1.409 + {
1.410 + foreach ($this->acl_options['global'] as $opt => $id)
1.411 + {
1.412 + if (strpos($opt, 'a_') === 0)
1.413 + {
1.414 + $hold_ary[0][$this->acl_options['id'][$opt]] = ACL_YES;
1.415 + }
1.416 + }
1.417 + }
1.418 +
1.419 + $hold_str = $this->build_bitstring($hold_ary);
1.420 +
1.421 + if ($hold_str)
1.422 + {
1.423 + $userdata['user_permissions'] = $hold_str;
1.424 +
1.425 + $sql = 'UPDATE ' . USERS_TABLE . "
1.426 + SET user_permissions = '" . $db->sql_escape($userdata['user_permissions']) . "',
1.427 + user_perm_from = 0
1.428 + WHERE user_id = " . $userdata['user_id'];
1.429 + $db->sql_query($sql);
1.430 + }
1.431 +
1.432 + return;
1.433 + }
1.434 +
1.435 + /**
1.436 + * Build bitstring from permission set
1.437 + */
1.438 + function build_bitstring(&$hold_ary)
1.439 + {
1.440 + $hold_str = '';
1.441 +
1.442 + if (sizeof($hold_ary))
1.443 + {
1.444 + ksort($hold_ary);
1.445 +
1.446 + $last_f = 0;
1.447 +
1.448 + foreach ($hold_ary as $f => $auth_ary)
1.449 + {
1.450 + $ary_key = (!$f) ? 'global' : 'local';
1.451 +
1.452 + $bitstring = array();
1.453 + foreach ($this->acl_options[$ary_key] as $opt => $id)
1.454 + {
1.455 + if (isset($auth_ary[$this->acl_options['id'][$opt]]))
1.456 + {
1.457 + $bitstring[$id] = $auth_ary[$this->acl_options['id'][$opt]];
1.458 +
1.459 + $option_key = substr($opt, 0, strpos($opt, '_') + 1);
1.460 +
1.461 + // If one option is allowed, the global permission for this option has to be allowed too
1.462 + // example: if the user has the a_ permission this means he has one or more a_* permissions
1.463 + if ($auth_ary[$this->acl_options['id'][$opt]] == ACL_YES && (!isset($bitstring[$this->acl_options[$ary_key][$option_key]]) || $bitstring[$this->acl_options[$ary_key][$option_key]] == ACL_NEVER))
1.464 + {
1.465 + $bitstring[$this->acl_options[$ary_key][$option_key]] = ACL_YES;
1.466 + }
1.467 + }
1.468 + else
1.469 + {
1.470 + $bitstring[$id] = ACL_NEVER;
1.471 + }
1.472 + }
1.473 +
1.474 + // Now this bitstring defines the permission setting for the current forum $f (or global setting)
1.475 + $bitstring = implode('', $bitstring);
1.476 +
1.477 + // The line number indicates the id, therefore we have to add empty lines for those ids not present
1.478 + $hold_str .= str_repeat("\n", $f - $last_f);
1.479 +
1.480 + // Convert bitstring for storage - we do not use binary/bytes because PHP's string functions are not fully binary safe
1.481 + for ($i = 0, $bit_length = strlen($bitstring); $i < $bit_length; $i += 31)
1.482 + {
1.483 + $hold_str .= str_pad(base_convert(str_pad(substr($bitstring, $i, 31), 31, 0, STR_PAD_RIGHT), 2, 36), 6, 0, STR_PAD_LEFT);
1.484 + }
1.485 +
1.486 + $last_f = $f;
1.487 + }
1.488 + unset($bitstring);
1.489 +
1.490 + $hold_str = rtrim($hold_str);
1.491 + }
1.492 +
1.493 + return $hold_str;
1.494 + }
1.495 +
1.496 + /**
1.497 + * Clear one or all users cached permission settings
1.498 + */
1.499 + function acl_clear_prefetch($user_id = false)
1.500 + {
1.501 + global $db, $cache;
1.502 +
1.503 + // Rebuild options cache
1.504 + $cache->destroy('_role_cache');
1.505 +
1.506 + $sql = 'SELECT *
1.507 + FROM ' . ACL_ROLES_DATA_TABLE . '
1.508 + ORDER BY role_id ASC';
1.509 + $result = $db->sql_query($sql);
1.510 +
1.511 + $this->role_cache = array();
1.512 + while ($row = $db->sql_fetchrow($result))
1.513 + {
1.514 + $this->role_cache[$row['role_id']][$row['auth_option_id']] = (int) $row['auth_setting'];
1.515 + }
1.516 + $db->sql_freeresult($result);
1.517 +
1.518 + foreach ($this->role_cache as $role_id => $role_options)
1.519 + {
1.520 + $this->role_cache[$role_id] = serialize($role_options);
1.521 + }
1.522 +
1.523 + $cache->put('_role_cache', $this->role_cache);
1.524 +
1.525 + // Now empty user permissions
1.526 + $where_sql = '';
1.527 +
1.528 + if ($user_id !== false)
1.529 + {
1.530 + $user_id = (!is_array($user_id)) ? $user_id = array((int) $user_id) : array_map('intval', $user_id);
1.531 + $where_sql = ' WHERE ' . $db->sql_in_set('user_id', $user_id);
1.532 + }
1.533 +
1.534 + $sql = 'UPDATE ' . USERS_TABLE . "
1.535 + SET user_permissions = '',
1.536 + user_perm_from = 0
1.537 + $where_sql";
1.538 + $db->sql_query($sql);
1.539 +
1.540 + return;
1.541 + }
1.542 +
1.543 + /**
1.544 + * Get assigned roles
1.545 + */
1.546 + function acl_role_data($user_type, $role_type, $ug_id = false, $forum_id = false)
1.547 + {
1.548 + global $db;
1.549 +
1.550 + $roles = array();
1.551 +
1.552 + $sql_id = ($user_type == 'user') ? 'user_id' : 'group_id';
1.553 +
1.554 + $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)) : '';
1.555 + $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)) : '';
1.556 +
1.557 + // Grab assigned roles...
1.558 + $sql = 'SELECT a.auth_role_id, a.' . $sql_id . ', a.forum_id
1.559 + FROM ' . (($user_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE) . ' a, ' . ACL_ROLES_TABLE . " r
1.560 + WHERE a.auth_role_id = r.role_id
1.561 + AND r.role_type = '" . $db->sql_escape($role_type) . "'
1.562 + $sql_ug
1.563 + $sql_forum
1.564 + ORDER BY r.role_order ASC";
1.565 + $result = $db->sql_query($sql);
1.566 +
1.567 + while ($row = $db->sql_fetchrow($result))
1.568 + {
1.569 + $roles[$row[$sql_id]][$row['forum_id']] = $row['auth_role_id'];
1.570 + }
1.571 + $db->sql_freeresult($result);
1.572 +
1.573 + return $roles;
1.574 + }
1.575 +
1.576 + /**
1.577 + * Get raw acl data based on user/option/forum
1.578 + */
1.579 + function acl_raw_data($user_id = false, $opts = false, $forum_id = false)
1.580 + {
1.581 + global $db;
1.582 +
1.583 + $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))) : '';
1.584 + $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))) : '';
1.585 +
1.586 + $sql_opts = $sql_opts_select = $sql_opts_from = '';
1.587 + $hold_ary = array();
1.588 +
1.589 + if ($opts !== false)
1.590 + {
1.591 + $sql_opts_select = ', ao.auth_option';
1.592 + $sql_opts_from = ', ' . ACL_OPTIONS_TABLE . ' ao';
1.593 + $this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
1.594 + }
1.595 +
1.596 + $sql_ary = array();
1.597 +
1.598 + // Grab non-role settings - user-specific
1.599 + $sql_ary[] = 'SELECT a.user_id, a.forum_id, a.auth_setting, a.auth_option_id' . $sql_opts_select . '
1.600 + FROM ' . ACL_USERS_TABLE . ' a' . $sql_opts_from . '
1.601 + WHERE a.auth_role_id = 0 ' .
1.602 + (($sql_opts_from) ? 'AND a.auth_option_id = ao.auth_option_id ' : '') .
1.603 + (($sql_user) ? 'AND a.' . $sql_user : '') . "
1.604 + $sql_forum
1.605 + $sql_opts";
1.606 +
1.607 + // Now the role settings - user-specific
1.608 + $sql_ary[] = 'SELECT a.user_id, a.forum_id, r.auth_option_id, r.auth_setting, r.auth_option_id' . $sql_opts_select . '
1.609 + FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r' . $sql_opts_from . '
1.610 + WHERE a.auth_role_id = r.role_id ' .
1.611 + (($sql_opts_from) ? 'AND r.auth_option_id = ao.auth_option_id ' : '') .
1.612 + (($sql_user) ? 'AND a.' . $sql_user : '') . "
1.613 + $sql_forum
1.614 + $sql_opts";
1.615 +
1.616 + foreach ($sql_ary as $sql)
1.617 + {
1.618 + $result = $db->sql_query($sql);
1.619 +
1.620 + while ($row = $db->sql_fetchrow($result))
1.621 + {
1.622 + $option = ($sql_opts_select) ? $row['auth_option'] : $this->acl_options['option'][$row['auth_option_id']];
1.623 + $hold_ary[$row['user_id']][$row['forum_id']][$option] = $row['auth_setting'];
1.624 + }
1.625 + $db->sql_freeresult($result);
1.626 + }
1.627 +
1.628 + $sql_ary = array();
1.629 +
1.630 + // Now grab group settings - non-role specific...
1.631 + $sql_ary[] = 'SELECT ug.user_id, a.forum_id, a.auth_setting, a.auth_option_id' . $sql_opts_select . '
1.632 + FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g' . $sql_opts_from . '
1.633 + WHERE a.auth_role_id = 0 ' .
1.634 + (($sql_opts_from) ? 'AND a.auth_option_id = ao.auth_option_id ' : '') . '
1.635 + AND a.group_id = ug.group_id
1.636 + AND g.group_id = ug.group_id
1.637 + AND ug.user_pending = 0
1.638 + AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
1.639 + ' . (($sql_user) ? 'AND ug.' . $sql_user : '') . "
1.640 + $sql_forum
1.641 + $sql_opts";
1.642 +
1.643 + // Now grab group settings - role specific...
1.644 + $sql_ary[] = 'SELECT ug.user_id, a.forum_id, r.auth_setting, r.auth_option_id' . $sql_opts_select . '
1.645 + FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g, ' . ACL_ROLES_DATA_TABLE . ' r' . $sql_opts_from . '
1.646 + WHERE a.auth_role_id = r.role_id ' .
1.647 + (($sql_opts_from) ? 'AND r.auth_option_id = ao.auth_option_id ' : '') . '
1.648 + AND a.group_id = ug.group_id
1.649 + AND g.group_id = ug.group_id
1.650 + AND ug.user_pending = 0
1.651 + AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
1.652 + ' . (($sql_user) ? 'AND ug.' . $sql_user : '') . "
1.653 + $sql_forum
1.654 + $sql_opts";
1.655 +
1.656 + foreach ($sql_ary as $sql)
1.657 + {
1.658 + $result = $db->sql_query($sql);
1.659 +
1.660 + while ($row = $db->sql_fetchrow($result))
1.661 + {
1.662 + $option = ($sql_opts_select) ? $row['auth_option'] : $this->acl_options['option'][$row['auth_option_id']];
1.663 +
1.664 + if (!isset($hold_ary[$row['user_id']][$row['forum_id']][$option]) || (isset($hold_ary[$row['user_id']][$row['forum_id']][$option]) && $hold_ary[$row['user_id']][$row['forum_id']][$option] != ACL_NEVER))
1.665 + {
1.666 + $hold_ary[$row['user_id']][$row['forum_id']][$option] = $row['auth_setting'];
1.667 +
1.668 + // If we detect ACL_NEVER, we will unset the flag option (within building the bitstring it is correctly set again)
1.669 + if ($row['auth_setting'] == ACL_NEVER)
1.670 + {
1.671 + $flag = substr($option, 0, strpos($option, '_') + 1);
1.672 +
1.673 + if (isset($hold_ary[$row['user_id']][$row['forum_id']][$flag]) && $hold_ary[$row['user_id']][$row['forum_id']][$flag] == ACL_YES)
1.674 + {
1.675 + unset($hold_ary[$row['user_id']][$row['forum_id']][$flag]);
1.676 +
1.677 +/* if (in_array(ACL_YES, $hold_ary[$row['user_id']][$row['forum_id']]))
1.678 + {
1.679 + $hold_ary[$row['user_id']][$row['forum_id']][$flag] = ACL_YES;
1.680 + }
1.681 +*/
1.682 + }
1.683 + }
1.684 + }
1.685 + }
1.686 + $db->sql_freeresult($result);
1.687 + }
1.688 +
1.689 + return $hold_ary;
1.690 + }
1.691 +
1.692 + /**
1.693 + * Get raw user based permission settings
1.694 + */
1.695 + function acl_user_raw_data($user_id = false, $opts = false, $forum_id = false)
1.696 + {
1.697 + global $db;
1.698 +
1.699 + $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))) : '';
1.700 + $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))) : '';
1.701 +
1.702 + $sql_opts = '';
1.703 + $hold_ary = $sql_ary = array();
1.704 +
1.705 + if ($opts !== false)
1.706 + {
1.707 + $this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
1.708 + }
1.709 +
1.710 + // Grab user settings - non-role specific...
1.711 + $sql_ary[] = 'SELECT a.user_id, a.forum_id, a.auth_setting, a.auth_option_id, ao.auth_option
1.712 + FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . ' ao
1.713 + WHERE a.auth_role_id = 0
1.714 + AND a.auth_option_id = ao.auth_option_id ' .
1.715 + (($sql_user) ? 'AND a.' . $sql_user : '') . "
1.716 + $sql_forum
1.717 + $sql_opts
1.718 + ORDER BY a.forum_id, ao.auth_option";
1.719 +
1.720 + // Now the role settings - user-specific
1.721 + $sql_ary[] = 'SELECT a.user_id, a.forum_id, r.auth_option_id, r.auth_setting, r.auth_option_id, ao.auth_option
1.722 + FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' ao
1.723 + WHERE a.auth_role_id = r.role_id
1.724 + AND r.auth_option_id = ao.auth_option_id ' .
1.725 + (($sql_user) ? 'AND a.' . $sql_user : '') . "
1.726 + $sql_forum
1.727 + $sql_opts
1.728 + ORDER BY a.forum_id, ao.auth_option";
1.729 +
1.730 + foreach ($sql_ary as $sql)
1.731 + {
1.732 + $result = $db->sql_query($sql);
1.733 +
1.734 + while ($row = $db->sql_fetchrow($result))
1.735 + {
1.736 + $hold_ary[$row['user_id']][$row['forum_id']][$row['auth_option']] = $row['auth_setting'];
1.737 + }
1.738 + $db->sql_freeresult($result);
1.739 + }
1.740 +
1.741 + return $hold_ary;
1.742 + }
1.743 +
1.744 + /**
1.745 + * Get raw group based permission settings
1.746 + */
1.747 + function acl_group_raw_data($group_id = false, $opts = false, $forum_id = false)
1.748 + {
1.749 + global $db;
1.750 +
1.751 + $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))) : '';
1.752 + $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))) : '';
1.753 +
1.754 + $sql_opts = '';
1.755 + $hold_ary = $sql_ary = array();
1.756 +
1.757 + if ($opts !== false)
1.758 + {
1.759 + $this->build_auth_option_statement('ao.auth_option', $opts, $sql_opts);
1.760 + }
1.761 +
1.762 + // Grab group settings - non-role specific...
1.763 + $sql_ary[] = 'SELECT a.group_id, a.forum_id, a.auth_setting, a.auth_option_id, ao.auth_option
1.764 + FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . ' ao
1.765 + WHERE a.auth_role_id = 0
1.766 + AND a.auth_option_id = ao.auth_option_id ' .
1.767 + (($sql_group) ? 'AND a.' . $sql_group : '') . "
1.768 + $sql_forum
1.769 + $sql_opts
1.770 + ORDER BY a.forum_id, ao.auth_option";
1.771 +
1.772 + // Now grab group settings - role specific...
1.773 + $sql_ary[] = 'SELECT a.group_id, a.forum_id, r.auth_setting, r.auth_option_id, ao.auth_option
1.774 + FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' ao
1.775 + WHERE a.auth_role_id = r.role_id
1.776 + AND r.auth_option_id = ao.auth_option_id ' .
1.777 + (($sql_group) ? 'AND a.' . $sql_group : '') . "
1.778 + $sql_forum
1.779 + $sql_opts
1.780 + ORDER BY a.forum_id, ao.auth_option";
1.781 +
1.782 + foreach ($sql_ary as $sql)
1.783 + {
1.784 + $result = $db->sql_query($sql);
1.785 +
1.786 + while ($row = $db->sql_fetchrow($result))
1.787 + {
1.788 + $hold_ary[$row['group_id']][$row['forum_id']][$row['auth_option']] = $row['auth_setting'];
1.789 + }
1.790 + $db->sql_freeresult($result);
1.791 + }
1.792 +
1.793 + return $hold_ary;
1.794 + }
1.795 +
1.796 + /**
1.797 + * Get raw acl data based on user for caching user_permissions
1.798 + * This function returns the same data as acl_raw_data(), but without the user id as the first key within the array.
1.799 + */
1.800 + function acl_raw_data_single_user($user_id)
1.801 + {
1.802 + global $db, $cache;
1.803 +
1.804 + // Check if the role-cache is there
1.805 + if (($this->role_cache = $cache->get('_role_cache')) === false)
1.806 + {
1.807 + $this->role_cache = array();
1.808 +
1.809 + // We pre-fetch roles
1.810 + $sql = 'SELECT *
1.811 + FROM ' . ACL_ROLES_DATA_TABLE . '
1.812 + ORDER BY role_id ASC';
1.813 + $result = $db->sql_query($sql);
1.814 +
1.815 + while ($row = $db->sql_fetchrow($result))
1.816 + {
1.817 + $this->role_cache[$row['role_id']][$row['auth_option_id']] = (int) $row['auth_setting'];
1.818 + }
1.819 + $db->sql_freeresult($result);
1.820 +
1.821 + foreach ($this->role_cache as $role_id => $role_options)
1.822 + {
1.823 + $this->role_cache[$role_id] = serialize($role_options);
1.824 + }
1.825 +
1.826 + $cache->put('_role_cache', $this->role_cache);
1.827 + }
1.828 +
1.829 + $hold_ary = array();
1.830 +
1.831 + // Grab user-specific permission settings
1.832 + $sql = 'SELECT forum_id, auth_option_id, auth_role_id, auth_setting
1.833 + FROM ' . ACL_USERS_TABLE . '
1.834 + WHERE user_id = ' . $user_id;
1.835 + $result = $db->sql_query($sql);
1.836 +
1.837 + while ($row = $db->sql_fetchrow($result))
1.838 + {
1.839 + // If a role is assigned, assign all options included within this role. Else, only set this one option.
1.840 + if ($row['auth_role_id'])
1.841 + {
1.842 + $hold_ary[$row['forum_id']] = (empty($hold_ary[$row['forum_id']])) ? unserialize($this->role_cache[$row['auth_role_id']]) : $hold_ary[$row['forum_id']] + unserialize($this->role_cache[$row['auth_role_id']]);
1.843 + }
1.844 + else
1.845 + {
1.846 + $hold_ary[$row['forum_id']][$row['auth_option_id']] = $row['auth_setting'];
1.847 + }
1.848 + }
1.849 + $db->sql_freeresult($result);
1.850 +
1.851 + // Now grab group-specific permission settings
1.852 + $sql = 'SELECT a.forum_id, a.auth_option_id, a.auth_role_id, a.auth_setting
1.853 + FROM ' . ACL_GROUPS_TABLE . ' a, ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g
1.854 + WHERE a.group_id = ug.group_id
1.855 + AND g.group_id = ug.group_id
1.856 + AND ug.user_pending = 0
1.857 + AND NOT (ug.group_leader = 1 AND g.group_skip_auth = 1)
1.858 + AND ug.user_id = ' . $user_id;
1.859 + $result = $db->sql_query($sql);
1.860 +
1.861 + while ($row = $db->sql_fetchrow($result))
1.862 + {
1.863 + if (!$row['auth_role_id'])
1.864 + {
1.865 + $this->_set_group_hold_ary($hold_ary[$row['forum_id']], $row['auth_option_id'], $row['auth_setting']);
1.866 + }
1.867 + else if (!empty($this->role_cache[$row['auth_role_id']]))
1.868 + {
1.869 + foreach (unserialize($this->role_cache[$row['auth_role_id']]) as $option_id => $setting)
1.870 + {
1.871 + $this->_set_group_hold_ary($hold_ary[$row['forum_id']], $option_id, $setting);
1.872 + }
1.873 + }
1.874 + }
1.875 + $db->sql_freeresult($result);
1.876 +
1.877 + return $hold_ary;
1.878 + }
1.879 +
1.880 + /**
1.881 + * Private function snippet for setting a specific piece of the hold_ary
1.882 + */
1.883 + function _set_group_hold_ary(&$hold_ary, $option_id, $setting)
1.884 + {
1.885 + if (!isset($hold_ary[$option_id]) || (isset($hold_ary[$option_id]) && $hold_ary[$option_id] != ACL_NEVER))
1.886 + {
1.887 + $hold_ary[$option_id] = $setting;
1.888 +
1.889 + // If we detect ACL_NEVER, we will unset the flag option (within building the bitstring it is correctly set again)
1.890 + if ($setting == ACL_NEVER)
1.891 + {
1.892 + $flag = substr($this->acl_options['option'][$option_id], 0, strpos($this->acl_options['option'][$option_id], '_') + 1);
1.893 + $flag = (int) $this->acl_options['id'][$flag];
1.894 +
1.895 + if (isset($hold_ary[$flag]) && $hold_ary[$flag] == ACL_YES)
1.896 + {
1.897 + unset($hold_ary[$flag]);
1.898 +
1.899 +/* This is uncommented, because i suspect this being slightly wrong due to mixed permission classes being possible
1.900 + if (in_array(ACL_YES, $hold_ary))
1.901 + {
1.902 + $hold_ary[$flag] = ACL_YES;
1.903 + }*/
1.904 + }
1.905 + }
1.906 + }
1.907 + }
1.908 +
1.909 + /**
1.910 + * Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
1.911 + */
1.912 + function login($username, $password, $autologin = false, $viewonline = 1, $admin = 0)
1.913 + {
1.914 + global $config, $db, $user, $phpbb_root_path, $phpEx;
1.915 +
1.916 + $method = trim(basename($config['auth_method']));
1.917 + include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
1.918 +
1.919 + $method = 'login_' . $method;
1.920 + if (function_exists($method))
1.921 + {
1.922 + $login = $method($username, $password, $user->ip, $user->browser, $user->forwarded_for);
1.923 +
1.924 + // If the auth module wants us to create an empty profile do so and then treat the status as LOGIN_SUCCESS
1.925 + if ($login['status'] == LOGIN_SUCCESS_CREATE_PROFILE)
1.926 + {
1.927 + // we are going to use the user_add function so include functions_user.php if it wasn't defined yet
1.928 + if (!function_exists('user_add'))
1.929 + {
1.930 + include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
1.931 + }
1.932 +
1.933 + user_add($login['user_row'], (isset($login['cp_data'])) ? $login['cp_data'] : false);
1.934 +
1.935 + $sql = 'SELECT user_id, username, user_password, user_passchg, user_email, user_type
1.936 + FROM ' . USERS_TABLE . "
1.937 + WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
1.938 + $result = $db->sql_query($sql);
1.939 + $row = $db->sql_fetchrow($result);
1.940 + $db->sql_freeresult($result);
1.941 +
1.942 + if (!$row)
1.943 + {
1.944 + return array(
1.945 + 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
1.946 + 'error_msg' => 'AUTH_NO_PROFILE_CREATED',
1.947 + 'user_row' => array('user_id' => ANONYMOUS),
1.948 + );
1.949 + }
1.950 +
1.951 + $login = array(
1.952 + 'status' => LOGIN_SUCCESS,
1.953 + 'error_msg' => false,
1.954 + 'user_row' => $row,
1.955 + );
1.956 + }
1.957 +
1.958 + // If login succeeded, we will log the user in... else we pass the login array through...
1.959 + if ($login['status'] == LOGIN_SUCCESS)
1.960 + {
1.961 + $old_session_id = $user->session_id;
1.962 +
1.963 + if ($admin)
1.964 + {
1.965 + global $SID, $_SID;
1.966 +
1.967 + $cookie_expire = time() - 31536000;
1.968 + $user->set_cookie('u', '', $cookie_expire);
1.969 + $user->set_cookie('sid', '', $cookie_expire);
1.970 + unset($cookie_expire);
1.971 +
1.972 + $SID = '?sid=';
1.973 + $user->session_id = $_SID = '';
1.974 + }
1.975 +
1.976 + $result = $user->session_create($login['user_row']['user_id'], $admin, $autologin, $viewonline);
1.977 +
1.978 + // Successful session creation
1.979 + if ($result === true)
1.980 + {
1.981 + // If admin re-authentication we remove the old session entry because a new one has been created...
1.982 + if ($admin)
1.983 + {
1.984 + // the login array is used because the user ids do not differ for re-authentication
1.985 + $sql = 'DELETE FROM ' . SESSIONS_TABLE . "
1.986 + WHERE session_id = '" . $db->sql_escape($old_session_id) . "'
1.987 + AND session_user_id = {$login['user_row']['user_id']}";
1.988 + $db->sql_query($sql);
1.989 + }
1.990 +
1.991 + return array(
1.992 + 'status' => LOGIN_SUCCESS,
1.993 + 'error_msg' => false,
1.994 + 'user_row' => $login['user_row'],
1.995 + );
1.996 + }
1.997 +
1.998 + return array(
1.999 + 'status' => LOGIN_BREAK,
1.1000 + 'error_msg' => $result,
1.1001 + 'user_row' => $login['user_row'],
1.1002 + );
1.1003 + }
1.1004 +
1.1005 + return $login;
1.1006 + }
1.1007 +
1.1008 + trigger_error('Authentication method not found', E_USER_ERROR);
1.1009 + }
1.1010 +
1.1011 + /**
1.1012 + * Fill auth_option statement for later querying based on the supplied options
1.1013 + */
1.1014 + function build_auth_option_statement($key, $auth_options, &$sql_opts)
1.1015 + {
1.1016 + global $db;
1.1017 +
1.1018 + if (!is_array($auth_options))
1.1019 + {
1.1020 + if (strpos($auth_options, '%') !== false)
1.1021 + {
1.1022 + $sql_opts = "AND $key " . $db->sql_like_expression(str_replace('%', $db->any_char, $auth_options));
1.1023 + }
1.1024 + else
1.1025 + {
1.1026 + $sql_opts = "AND $key = '" . $db->sql_escape($auth_options) . "'";
1.1027 + }
1.1028 + }
1.1029 + else
1.1030 + {
1.1031 + $is_like_expression = false;
1.1032 +
1.1033 + foreach ($auth_options as $option)
1.1034 + {
1.1035 + if (strpos($option, '%') !== false)
1.1036 + {
1.1037 + $is_like_expression = true;
1.1038 + }
1.1039 + }
1.1040 +
1.1041 + if (!$is_like_expression)
1.1042 + {
1.1043 + $sql_opts = 'AND ' . $db->sql_in_set($key, $auth_options);
1.1044 + }
1.1045 + else
1.1046 + {
1.1047 + $sql = array();
1.1048 +
1.1049 + foreach ($auth_options as $option)
1.1050 + {
1.1051 + if (strpos($option, '%') !== false)
1.1052 + {
1.1053 + $sql[] = $key . ' ' . $db->sql_like_expression(str_replace('%', $db->any_char, $option));
1.1054 + }
1.1055 + else
1.1056 + {
1.1057 + $sql[] = $key . " = '" . $db->sql_escape($option) . "'";
1.1058 + }
1.1059 + }
1.1060 +
1.1061 + $sql_opts = 'AND (' . implode(' OR ', $sql) . ')';
1.1062 + }
1.1063 + }
1.1064 + }
1.1065 +}
1.1066 +
1.1067 +?>
1.1068 \ No newline at end of file