1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/session.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,2448 @@
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 +* Session class
1.24 +* @package phpBB3
1.25 +*/
1.26 +class session
1.27 +{
1.28 + var $cookie_data = array();
1.29 + var $page = array();
1.30 + var $data = array();
1.31 + var $browser = '';
1.32 + var $forwarded_for = '';
1.33 + var $host = '';
1.34 + var $session_id = '';
1.35 + var $ip = '';
1.36 + var $load = 0;
1.37 + var $time_now = 0;
1.38 + var $update_session_page = true;
1.39 +
1.40 + /**
1.41 + * Extract current session page
1.42 + *
1.43 + * @param string $root_path current root path (phpbb_root_path)
1.44 + */
1.45 + function extract_current_page($root_path)
1.46 + {
1.47 + $page_array = array();
1.48 +
1.49 + // First of all, get the request uri...
1.50 + $script_name = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : getenv('PHP_SELF');
1.51 + $args = (!empty($_SERVER['QUERY_STRING'])) ? explode('&', $_SERVER['QUERY_STRING']) : explode('&', getenv('QUERY_STRING'));
1.52 +
1.53 + // If we are unable to get the script name we use REQUEST_URI as a failover and note it within the page array for easier support...
1.54 + if (!$script_name)
1.55 + {
1.56 + $script_name = (!empty($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : getenv('REQUEST_URI');
1.57 + $script_name = (($pos = strpos($script_name, '?')) !== false) ? substr($script_name, 0, $pos) : $script_name;
1.58 + $page_array['failover'] = 1;
1.59 + }
1.60 +
1.61 + // Replace backslashes and doubled slashes (could happen on some proxy setups)
1.62 + $script_name = str_replace(array('\\', '//'), '/', $script_name);
1.63 +
1.64 + // Now, remove the sid and let us get a clean query string...
1.65 + $use_args = array();
1.66 +
1.67 + // Since some browser do not encode correctly we need to do this with some "special" characters...
1.68 + // " -> %22, ' => %27, < -> %3C, > -> %3E
1.69 + $find = array('"', "'", '<', '>');
1.70 + $replace = array('%22', '%27', '%3C', '%3E');
1.71 +
1.72 + foreach ($args as $key => $argument)
1.73 + {
1.74 + if (strpos($argument, 'sid=') === 0)
1.75 + {
1.76 + continue;
1.77 + }
1.78 +
1.79 + $use_args[] = str_replace($find, $replace, $argument);
1.80 + }
1.81 + unset($args);
1.82 +
1.83 + // The following examples given are for an request uri of {path to the phpbb directory}/adm/index.php?i=10&b=2
1.84 +
1.85 + // The current query string
1.86 + $query_string = trim(implode('&', $use_args));
1.87 +
1.88 + // basenamed page name (for example: index.php)
1.89 + $page_name = (substr($script_name, -1, 1) == '/') ? '' : basename($script_name);
1.90 + $page_name = urlencode(htmlspecialchars($page_name));
1.91 +
1.92 + // current directory within the phpBB root (for example: adm)
1.93 + $root_dirs = explode('/', str_replace('\\', '/', phpbb_realpath($root_path)));
1.94 + $page_dirs = explode('/', str_replace('\\', '/', phpbb_realpath('./')));
1.95 + $intersection = array_intersect_assoc($root_dirs, $page_dirs);
1.96 +
1.97 + $root_dirs = array_diff_assoc($root_dirs, $intersection);
1.98 + $page_dirs = array_diff_assoc($page_dirs, $intersection);
1.99 +
1.100 + $page_dir = str_repeat('../', sizeof($root_dirs)) . implode('/', $page_dirs);
1.101 +
1.102 + if ($page_dir && substr($page_dir, -1, 1) == '/')
1.103 + {
1.104 + $page_dir = substr($page_dir, 0, -1);
1.105 + }
1.106 +
1.107 + // Current page from phpBB root (for example: adm/index.php?i=10&b=2)
1.108 + $page = (($page_dir) ? $page_dir . '/' : '') . $page_name . (($query_string) ? "?$query_string" : '');
1.109 +
1.110 + // The script path from the webroot to the current directory (for example: /phpBB3/adm/) : always prefixed with / and ends in /
1.111 + $script_path = trim(str_replace('\\', '/', dirname($script_name)));
1.112 +
1.113 + // The script path from the webroot to the phpBB root (for example: /phpBB3/)
1.114 + $script_dirs = explode('/', $script_path);
1.115 + array_splice($script_dirs, -sizeof($page_dirs));
1.116 + $root_script_path = implode('/', $script_dirs) . (sizeof($root_dirs) ? '/' . implode('/', $root_dirs) : '');
1.117 +
1.118 + // We are on the base level (phpBB root == webroot), lets adjust the variables a bit...
1.119 + if (!$root_script_path)
1.120 + {
1.121 + $root_script_path = ($page_dir) ? str_replace($page_dir, '', $script_path) : $script_path;
1.122 + }
1.123 +
1.124 + $script_path .= (substr($script_path, -1, 1) == '/') ? '' : '/';
1.125 + $root_script_path .= (substr($root_script_path, -1, 1) == '/') ? '' : '/';
1.126 +
1.127 + $page_array += array(
1.128 + 'page_name' => $page_name,
1.129 + 'page_dir' => $page_dir,
1.130 +
1.131 + 'query_string' => $query_string,
1.132 + 'script_path' => str_replace(' ', '%20', htmlspecialchars($script_path)),
1.133 + 'root_script_path' => str_replace(' ', '%20', htmlspecialchars($root_script_path)),
1.134 +
1.135 + 'page' => $page,
1.136 + 'forum' => (isset($_REQUEST['f']) && $_REQUEST['f'] > 0) ? (int) $_REQUEST['f'] : 0,
1.137 + );
1.138 +
1.139 + return $page_array;
1.140 + }
1.141 +
1.142 + /**
1.143 + * Get valid hostname/port. HTTP_HOST is used, SERVER_NAME if HTTP_HOST not present.
1.144 + */
1.145 + function extract_current_hostname()
1.146 + {
1.147 + global $config;
1.148 +
1.149 + // Get hostname
1.150 + $host = (!empty($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME'));
1.151 +
1.152 + // Should be a string and lowered
1.153 + $host = (string) strtolower($host);
1.154 +
1.155 + // If host is equal the cookie domain or the server name (if config is set), then we assume it is valid
1.156 + if ((isset($config['cookie_domain']) && $host === $config['cookie_domain']) || (isset($config['server_name']) && $host === $config['server_name']))
1.157 + {
1.158 + return $host;
1.159 + }
1.160 +
1.161 + // Is the host actually a IP? If so, we use the IP... (IPv4)
1.162 + if (long2ip(ip2long($host)) === $host)
1.163 + {
1.164 + return $host;
1.165 + }
1.166 +
1.167 + // Now return the hostname (this also removes any port definition). The http:// is prepended to construct a valid URL, hosts never have a scheme assigned
1.168 + $host = @parse_url('http://' . $host);
1.169 + $host = (!empty($host['host'])) ? $host['host'] : '';
1.170 +
1.171 + // Remove any portions not removed by parse_url (#)
1.172 + $host = str_replace('#', '', $host);
1.173 +
1.174 + // If, by any means, the host is now empty, we will use a "best approach" way to guess one
1.175 + if (empty($host))
1.176 + {
1.177 + if (!empty($config['server_name']))
1.178 + {
1.179 + $host = $config['server_name'];
1.180 + }
1.181 + else if (!empty($config['cookie_domain']))
1.182 + {
1.183 + $host = (strpos($config['cookie_domain'], '.') === 0) ? substr($config['cookie_domain'], 1) : $config['cookie_domain'];
1.184 + }
1.185 + else
1.186 + {
1.187 + // Set to OS hostname or localhost
1.188 + $host = (function_exists('php_uname')) ? php_uname('n') : 'localhost';
1.189 + }
1.190 + }
1.191 +
1.192 + // It may be still no valid host, but for sure only a hostname (we may further expand on the cookie domain... if set)
1.193 + return $host;
1.194 + }
1.195 +
1.196 + /**
1.197 + * Start session management
1.198 + *
1.199 + * This is where all session activity begins. We gather various pieces of
1.200 + * information from the client and server. We test to see if a session already
1.201 + * exists. If it does, fine and dandy. If it doesn't we'll go on to create a
1.202 + * new one ... pretty logical heh? We also examine the system load (if we're
1.203 + * running on a system which makes such information readily available) and
1.204 + * halt if it's above an admin definable limit.
1.205 + *
1.206 + * @param bool $update_session_page if true the session page gets updated.
1.207 + * This can be set to circumvent certain scripts to update the users last visited page.
1.208 + */
1.209 + function session_begin($update_session_page = true)
1.210 + {
1.211 + global $phpEx, $SID, $_SID, $_EXTRA_URL, $db, $config, $phpbb_root_path;
1.212 +
1.213 + // Give us some basic information
1.214 + $this->time_now = time();
1.215 + $this->cookie_data = array('u' => 0, 'k' => '');
1.216 + $this->update_session_page = $update_session_page;
1.217 + $this->browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
1.218 + $this->referer = (!empty($_SERVER['HTTP_REFERER'])) ? htmlspecialchars((string) $_SERVER['HTTP_REFERER']) : '';
1.219 + $this->forwarded_for = (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? htmlspecialchars((string) $_SERVER['HTTP_X_FORWARDED_FOR']) : '';
1.220 +
1.221 + $this->host = $this->extract_current_hostname();
1.222 + $this->page = $this->extract_current_page($phpbb_root_path);
1.223 +
1.224 + // if the forwarded for header shall be checked we have to validate its contents
1.225 + if ($config['forwarded_for_check'])
1.226 + {
1.227 + $this->forwarded_for = preg_replace('# {2,}#', ' ', str_replace(',', ' ', $this->forwarded_for));
1.228 +
1.229 + // split the list of IPs
1.230 + $ips = explode(' ', $this->forwarded_for);
1.231 + foreach ($ips as $ip)
1.232 + {
1.233 + // check IPv4 first, the IPv6 is hopefully only going to be used very seldomly
1.234 + if (!empty($ip) && !preg_match(get_preg_expression('ipv4'), $ip) && !preg_match(get_preg_expression('ipv6'), $ip))
1.235 + {
1.236 + // contains invalid data, don't use the forwarded for header
1.237 + $this->forwarded_for = '';
1.238 + break;
1.239 + }
1.240 + }
1.241 + }
1.242 + else
1.243 + {
1.244 + $this->forwarded_for = '';
1.245 + }
1.246 +
1.247 + if (isset($_COOKIE[$config['cookie_name'] . '_sid']) || isset($_COOKIE[$config['cookie_name'] . '_u']))
1.248 + {
1.249 + $this->cookie_data['u'] = request_var($config['cookie_name'] . '_u', 0, false, true);
1.250 + $this->cookie_data['k'] = request_var($config['cookie_name'] . '_k', '', false, true);
1.251 + $this->session_id = request_var($config['cookie_name'] . '_sid', '', false, true);
1.252 +
1.253 + $SID = (defined('NEED_SID')) ? '?sid=' . $this->session_id : '?sid=';
1.254 + $_SID = (defined('NEED_SID')) ? $this->session_id : '';
1.255 +
1.256 + if (empty($this->session_id))
1.257 + {
1.258 + $this->session_id = $_SID = request_var('sid', '');
1.259 + $SID = '?sid=' . $this->session_id;
1.260 + $this->cookie_data = array('u' => 0, 'k' => '');
1.261 + }
1.262 + }
1.263 + else
1.264 + {
1.265 + $this->session_id = $_SID = request_var('sid', '');
1.266 + $SID = '?sid=' . $this->session_id;
1.267 + }
1.268 +
1.269 + $_EXTRA_URL = array();
1.270 +
1.271 + // Why no forwarded_for et al? Well, too easily spoofed. With the results of my recent requests
1.272 + // it's pretty clear that in the majority of cases you'll at least be left with a proxy/cache ip.
1.273 + $this->ip = (!empty($_SERVER['REMOTE_ADDR'])) ? (string) $_SERVER['REMOTE_ADDR'] : '';
1.274 + $this->ip = preg_replace('# {2,}#', ' ', str_replace(',', ' ', $this->ip));
1.275 +
1.276 + // split the list of IPs
1.277 + $ips = explode(' ', trim($this->ip));
1.278 +
1.279 + // Default IP if REMOTE_ADDR is invalid
1.280 + $this->ip = '127.0.0.1';
1.281 +
1.282 + foreach ($ips as $ip)
1.283 + {
1.284 + if (preg_match(get_preg_expression('ipv4'), $ip))
1.285 + {
1.286 + $this->ip = $ip;
1.287 + }
1.288 + else if (preg_match(get_preg_expression('ipv6'), $ip))
1.289 + {
1.290 + // Quick check for IPv4-mapped address in IPv6
1.291 + if (stripos($ip, '::ffff:') === 0)
1.292 + {
1.293 + $ipv4 = substr($ip, 7);
1.294 +
1.295 + if (preg_match(get_preg_expression('ipv4'), $ipv4))
1.296 + {
1.297 + $ip = $ipv4;
1.298 + }
1.299 + }
1.300 +
1.301 + $this->ip = $ip;
1.302 + }
1.303 + else
1.304 + {
1.305 + // We want to use the last valid address in the chain
1.306 + // Leave foreach loop when address is invalid
1.307 + break;
1.308 + }
1.309 + }
1.310 +
1.311 + $this->load = false;
1.312 +
1.313 + // Load limit check (if applicable)
1.314 + if ($config['limit_load'] || $config['limit_search_load'])
1.315 + {
1.316 + if ((function_exists('sys_getloadavg') && $load = sys_getloadavg()) || ($load = explode(' ', @file_get_contents('/proc/loadavg'))))
1.317 + {
1.318 + $this->load = array_slice($load, 0, 1);
1.319 + $this->load = floatval($this->load[0]);
1.320 + }
1.321 + else
1.322 + {
1.323 + set_config('limit_load', '0');
1.324 + set_config('limit_search_load', '0');
1.325 + }
1.326 + }
1.327 +
1.328 + // Is session_id is set or session_id is set and matches the url param if required
1.329 + if (!empty($this->session_id) && (!defined('NEED_SID') || (isset($_GET['sid']) && $this->session_id === $_GET['sid'])))
1.330 + {
1.331 + $sql = 'SELECT u.*, s.*
1.332 + FROM ' . SESSIONS_TABLE . ' s, ' . USERS_TABLE . " u
1.333 + WHERE s.session_id = '" . $db->sql_escape($this->session_id) . "'
1.334 + AND u.user_id = s.session_user_id";
1.335 + $result = $db->sql_query($sql);
1.336 + $this->data = $db->sql_fetchrow($result);
1.337 + $db->sql_freeresult($result);
1.338 +
1.339 + // Did the session exist in the DB?
1.340 + if (isset($this->data['user_id']))
1.341 + {
1.342 + // Validate IP length according to admin ... enforces an IP
1.343 + // check on bots if admin requires this
1.344 +// $quadcheck = ($config['ip_check_bot'] && $this->data['user_type'] & USER_BOT) ? 4 : $config['ip_check'];
1.345 +
1.346 + if (strpos($this->ip, ':') !== false && strpos($this->data['session_ip'], ':') !== false)
1.347 + {
1.348 + $s_ip = short_ipv6($this->data['session_ip'], $config['ip_check']);
1.349 + $u_ip = short_ipv6($this->ip, $config['ip_check']);
1.350 + }
1.351 + else
1.352 + {
1.353 + $s_ip = implode('.', array_slice(explode('.', $this->data['session_ip']), 0, $config['ip_check']));
1.354 + $u_ip = implode('.', array_slice(explode('.', $this->ip), 0, $config['ip_check']));
1.355 + }
1.356 +
1.357 + $s_browser = ($config['browser_check']) ? trim(strtolower(substr($this->data['session_browser'], 0, 149))) : '';
1.358 + $u_browser = ($config['browser_check']) ? trim(strtolower(substr($this->browser, 0, 149))) : '';
1.359 +
1.360 + $s_forwarded_for = ($config['forwarded_for_check']) ? substr($this->data['session_forwarded_for'], 0, 254) : '';
1.361 + $u_forwarded_for = ($config['forwarded_for_check']) ? substr($this->forwarded_for, 0, 254) : '';
1.362 +
1.363 + // referer checks
1.364 + // The @ before $config['referer_validation'] suppresses notices present while running the updater
1.365 + $check_referer_path = (@$config['referer_validation'] == REFERER_VALIDATE_PATH);
1.366 + $referer_valid = true;
1.367 +
1.368 + // we assume HEAD and TRACE to be foul play and thus only whitelist GET
1.369 + if (@$config['referer_validation'] && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) !== 'get')
1.370 + {
1.371 + $referer_valid = $this->validate_referer($check_referer_path);
1.372 + }
1.373 +
1.374 + if ($u_ip === $s_ip && $s_browser === $u_browser && $s_forwarded_for === $u_forwarded_for && $referer_valid)
1.375 + {
1.376 + $session_expired = false;
1.377 +
1.378 + // Check whether the session is still valid if we have one
1.379 + $method = basename(trim($config['auth_method']));
1.380 + include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
1.381 +
1.382 + $method = 'validate_session_' . $method;
1.383 + if (function_exists($method))
1.384 + {
1.385 + if (!$method($this->data))
1.386 + {
1.387 + $session_expired = true;
1.388 + }
1.389 + }
1.390 +
1.391 + if (!$session_expired)
1.392 + {
1.393 + // Check the session length timeframe if autologin is not enabled.
1.394 + // Else check the autologin length... and also removing those having autologin enabled but no longer allowed board-wide.
1.395 + if (!$this->data['session_autologin'])
1.396 + {
1.397 + if ($this->data['session_time'] < $this->time_now - ($config['session_length'] + 60))
1.398 + {
1.399 + $session_expired = true;
1.400 + }
1.401 + }
1.402 + else if (!$config['allow_autologin'] || ($config['max_autologin_time'] && $this->data['session_time'] < $this->time_now - (86400 * (int) $config['max_autologin_time']) + 60))
1.403 + {
1.404 + $session_expired = true;
1.405 + }
1.406 + }
1.407 +
1.408 + if (!$session_expired)
1.409 + {
1.410 + // Only update session DB a minute or so after last update or if page changes
1.411 + if ($this->time_now - $this->data['session_time'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page']))
1.412 + {
1.413 + $sql_ary = array('session_time' => $this->time_now);
1.414 +
1.415 + if ($this->update_session_page)
1.416 + {
1.417 + $sql_ary['session_page'] = substr($this->page['page'], 0, 199);
1.418 + $sql_ary['session_forum_id'] = $this->page['forum'];
1.419 + }
1.420 +
1.421 + $db->sql_return_on_error(true);
1.422 +
1.423 + $sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
1.424 + WHERE session_id = '" . $db->sql_escape($this->session_id) . "'";
1.425 + $result = $db->sql_query($sql);
1.426 +
1.427 + $db->sql_return_on_error(false);
1.428 +
1.429 + // If the database is not yet updated, there will be an error due to the session_forum_id
1.430 + // @todo REMOVE for 3.0.2
1.431 + if ($result === false)
1.432 + {
1.433 + unset($sql_ary['session_forum_id']);
1.434 +
1.435 + $sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
1.436 + WHERE session_id = '" . $db->sql_escape($this->session_id) . "'";
1.437 + $db->sql_query($sql);
1.438 + }
1.439 +
1.440 + if ($this->data['user_id'] != ANONYMOUS && !empty($config['new_member_post_limit']) && $this->data['user_new'] && $config['new_member_post_limit'] <= $this->data['user_posts'])
1.441 + {
1.442 + $this->leave_newly_registered();
1.443 + }
1.444 + }
1.445 +
1.446 + $this->data['is_registered'] = ($this->data['user_id'] != ANONYMOUS && ($this->data['user_type'] == USER_NORMAL || $this->data['user_type'] == USER_FOUNDER)) ? true : false;
1.447 + $this->data['is_bot'] = (!$this->data['is_registered'] && $this->data['user_id'] != ANONYMOUS) ? true : false;
1.448 + $this->data['user_lang'] = basename($this->data['user_lang']);
1.449 +
1.450 + return true;
1.451 + }
1.452 + }
1.453 + else
1.454 + {
1.455 + // Added logging temporarly to help debug bugs...
1.456 + if (defined('DEBUG_EXTRA') && $this->data['user_id'] != ANONYMOUS)
1.457 + {
1.458 + if ($referer_valid)
1.459 + {
1.460 + add_log('critical', 'LOG_IP_BROWSER_FORWARDED_CHECK', $u_ip, $s_ip, $u_browser, $s_browser, htmlspecialchars($u_forwarded_for), htmlspecialchars($s_forwarded_for));
1.461 + }
1.462 + else
1.463 + {
1.464 + add_log('critical', 'LOG_REFERER_INVALID', $this->referer);
1.465 + }
1.466 + }
1.467 + }
1.468 + }
1.469 + }
1.470 +
1.471 + // If we reach here then no (valid) session exists. So we'll create a new one
1.472 + return $this->session_create();
1.473 + }
1.474 +
1.475 + /**
1.476 + * Create a new session
1.477 + *
1.478 + * If upon trying to start a session we discover there is nothing existing we
1.479 + * jump here. Additionally this method is called directly during login to regenerate
1.480 + * the session for the specific user. In this method we carry out a number of tasks;
1.481 + * garbage collection, (search)bot checking, banned user comparison. Basically
1.482 + * though this method will result in a new session for a specific user.
1.483 + */
1.484 + function session_create($user_id = false, $set_admin = false, $persist_login = false, $viewonline = true)
1.485 + {
1.486 + global $SID, $_SID, $db, $config, $cache, $phpbb_root_path, $phpEx;
1.487 +
1.488 + $this->data = array();
1.489 +
1.490 + /* Garbage collection ... remove old sessions updating user information
1.491 + // if necessary. It means (potentially) 11 queries but only infrequently
1.492 + if ($this->time_now > $config['session_last_gc'] + $config['session_gc'])
1.493 + {
1.494 + $this->session_gc();
1.495 + }*/
1.496 +
1.497 + // Do we allow autologin on this board? No? Then override anything
1.498 + // that may be requested here
1.499 + if (!$config['allow_autologin'])
1.500 + {
1.501 + $this->cookie_data['k'] = $persist_login = false;
1.502 + }
1.503 +
1.504 + /**
1.505 + * Here we do a bot check, oh er saucy! No, not that kind of bot
1.506 + * check. We loop through the list of bots defined by the admin and
1.507 + * see if we have any useragent and/or IP matches. If we do, this is a
1.508 + * bot, act accordingly
1.509 + */
1.510 + $bot = false;
1.511 + $active_bots = $cache->obtain_bots();
1.512 +
1.513 + foreach ($active_bots as $row)
1.514 + {
1.515 + if ($row['bot_agent'] && preg_match('#' . str_replace('\*', '.*?', preg_quote($row['bot_agent'], '#')) . '#i', $this->browser))
1.516 + {
1.517 + $bot = $row['user_id'];
1.518 + }
1.519 +
1.520 + // If ip is supplied, we will make sure the ip is matching too...
1.521 + if ($row['bot_ip'] && ($bot || !$row['bot_agent']))
1.522 + {
1.523 + // Set bot to false, then we only have to set it to true if it is matching
1.524 + $bot = false;
1.525 +
1.526 + foreach (explode(',', $row['bot_ip']) as $bot_ip)
1.527 + {
1.528 + $bot_ip = trim($bot_ip);
1.529 +
1.530 + if (!$bot_ip)
1.531 + {
1.532 + continue;
1.533 + }
1.534 +
1.535 + if (strpos($this->ip, $bot_ip) === 0)
1.536 + {
1.537 + $bot = (int) $row['user_id'];
1.538 + break;
1.539 + }
1.540 + }
1.541 + }
1.542 +
1.543 + if ($bot)
1.544 + {
1.545 + break;
1.546 + }
1.547 + }
1.548 +
1.549 + $method = basename(trim($config['auth_method']));
1.550 + include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
1.551 +
1.552 + $method = 'autologin_' . $method;
1.553 + if (function_exists($method))
1.554 + {
1.555 + $this->data = $method();
1.556 +
1.557 + if (sizeof($this->data))
1.558 + {
1.559 + $this->cookie_data['k'] = '';
1.560 + $this->cookie_data['u'] = $this->data['user_id'];
1.561 + }
1.562 + }
1.563 +
1.564 + // If we're presented with an autologin key we'll join against it.
1.565 + // Else if we've been passed a user_id we'll grab data based on that
1.566 + if (isset($this->cookie_data['k']) && $this->cookie_data['k'] && $this->cookie_data['u'] && !sizeof($this->data))
1.567 + {
1.568 + $sql = 'SELECT u.*
1.569 + FROM ' . USERS_TABLE . ' u, ' . SESSIONS_KEYS_TABLE . ' k
1.570 + WHERE u.user_id = ' . (int) $this->cookie_data['u'] . '
1.571 + AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ")
1.572 + AND k.user_id = u.user_id
1.573 + AND k.key_id = '" . $db->sql_escape(md5($this->cookie_data['k'])) . "'";
1.574 + $result = $db->sql_query($sql);
1.575 + $this->data = $db->sql_fetchrow($result);
1.576 + $db->sql_freeresult($result);
1.577 + $bot = false;
1.578 + }
1.579 + else if ($user_id !== false && !sizeof($this->data))
1.580 + {
1.581 + $this->cookie_data['k'] = '';
1.582 + $this->cookie_data['u'] = $user_id;
1.583 +
1.584 + $sql = 'SELECT *
1.585 + FROM ' . USERS_TABLE . '
1.586 + WHERE user_id = ' . (int) $this->cookie_data['u'] . '
1.587 + AND user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')';
1.588 + $result = $db->sql_query($sql);
1.589 + $this->data = $db->sql_fetchrow($result);
1.590 + $db->sql_freeresult($result);
1.591 + $bot = false;
1.592 + }
1.593 +
1.594 + // Bot user, if they have a SID in the Request URI we need to get rid of it
1.595 + // otherwise they'll index this page with the SID, duplicate content oh my!
1.596 + if ($bot && isset($_GET['sid']))
1.597 + {
1.598 + send_status_line(301, 'Moved Permanently');
1.599 + redirect(build_url(array('sid')));
1.600 + }
1.601 +
1.602 + // If no data was returned one or more of the following occurred:
1.603 + // Key didn't match one in the DB
1.604 + // User does not exist
1.605 + // User is inactive
1.606 + // User is bot
1.607 + if (!sizeof($this->data) || !is_array($this->data))
1.608 + {
1.609 + $this->cookie_data['k'] = '';
1.610 + $this->cookie_data['u'] = ($bot) ? $bot : ANONYMOUS;
1.611 +
1.612 + if (!$bot)
1.613 + {
1.614 + $sql = 'SELECT *
1.615 + FROM ' . USERS_TABLE . '
1.616 + WHERE user_id = ' . (int) $this->cookie_data['u'];
1.617 + }
1.618 + else
1.619 + {
1.620 + // We give bots always the same session if it is not yet expired.
1.621 + $sql = 'SELECT u.*, s.*
1.622 + FROM ' . USERS_TABLE . ' u
1.623 + LEFT JOIN ' . SESSIONS_TABLE . ' s ON (s.session_user_id = u.user_id)
1.624 + WHERE u.user_id = ' . (int) $bot;
1.625 + }
1.626 +
1.627 + $result = $db->sql_query($sql);
1.628 + $this->data = $db->sql_fetchrow($result);
1.629 + $db->sql_freeresult($result);
1.630 + }
1.631 +
1.632 + if ($this->data['user_id'] != ANONYMOUS && !$bot)
1.633 + {
1.634 + $this->data['session_last_visit'] = (isset($this->data['session_time']) && $this->data['session_time']) ? $this->data['session_time'] : (($this->data['user_lastvisit']) ? $this->data['user_lastvisit'] : time());
1.635 + }
1.636 + else
1.637 + {
1.638 + $this->data['session_last_visit'] = $this->time_now;
1.639 + }
1.640 +
1.641 + // Force user id to be integer...
1.642 + $this->data['user_id'] = (int) $this->data['user_id'];
1.643 +
1.644 + // At this stage we should have a filled data array, defined cookie u and k data.
1.645 + // data array should contain recent session info if we're a real user and a recent
1.646 + // session exists in which case session_id will also be set
1.647 +
1.648 + // Is user banned? Are they excluded? Won't return on ban, exists within method
1.649 + if ($this->data['user_type'] != USER_FOUNDER)
1.650 + {
1.651 + if (!$config['forwarded_for_check'])
1.652 + {
1.653 + $this->check_ban($this->data['user_id'], $this->ip);
1.654 + }
1.655 + else
1.656 + {
1.657 + $ips = explode(' ', $this->forwarded_for);
1.658 + $ips[] = $this->ip;
1.659 + $this->check_ban($this->data['user_id'], $ips);
1.660 + }
1.661 + }
1.662 +
1.663 + $this->data['is_registered'] = (!$bot && $this->data['user_id'] != ANONYMOUS && ($this->data['user_type'] == USER_NORMAL || $this->data['user_type'] == USER_FOUNDER)) ? true : false;
1.664 + $this->data['is_bot'] = ($bot) ? true : false;
1.665 +
1.666 + // If our friend is a bot, we re-assign a previously assigned session
1.667 + if ($this->data['is_bot'] && $bot == $this->data['user_id'] && $this->data['session_id'])
1.668 + {
1.669 + // Only assign the current session if the ip, browser and forwarded_for match...
1.670 + if (strpos($this->ip, ':') !== false && strpos($this->data['session_ip'], ':') !== false)
1.671 + {
1.672 + $s_ip = short_ipv6($this->data['session_ip'], $config['ip_check']);
1.673 + $u_ip = short_ipv6($this->ip, $config['ip_check']);
1.674 + }
1.675 + else
1.676 + {
1.677 + $s_ip = implode('.', array_slice(explode('.', $this->data['session_ip']), 0, $config['ip_check']));
1.678 + $u_ip = implode('.', array_slice(explode('.', $this->ip), 0, $config['ip_check']));
1.679 + }
1.680 +
1.681 + $s_browser = ($config['browser_check']) ? trim(strtolower(substr($this->data['session_browser'], 0, 149))) : '';
1.682 + $u_browser = ($config['browser_check']) ? trim(strtolower(substr($this->browser, 0, 149))) : '';
1.683 +
1.684 + $s_forwarded_for = ($config['forwarded_for_check']) ? substr($this->data['session_forwarded_for'], 0, 254) : '';
1.685 + $u_forwarded_for = ($config['forwarded_for_check']) ? substr($this->forwarded_for, 0, 254) : '';
1.686 +
1.687 + if ($u_ip === $s_ip && $s_browser === $u_browser && $s_forwarded_for === $u_forwarded_for)
1.688 + {
1.689 + $this->session_id = $this->data['session_id'];
1.690 +
1.691 + // Only update session DB a minute or so after last update or if page changes
1.692 + if ($this->time_now - $this->data['session_time'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page']))
1.693 + {
1.694 + $this->data['session_time'] = $this->data['session_last_visit'] = $this->time_now;
1.695 +
1.696 + $sql_ary = array('session_time' => $this->time_now, 'session_last_visit' => $this->time_now, 'session_admin' => 0);
1.697 +
1.698 + if ($this->update_session_page)
1.699 + {
1.700 + $sql_ary['session_page'] = substr($this->page['page'], 0, 199);
1.701 + $sql_ary['session_forum_id'] = $this->page['forum'];
1.702 + }
1.703 +
1.704 + $sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
1.705 + WHERE session_id = '" . $db->sql_escape($this->session_id) . "'";
1.706 + $db->sql_query($sql);
1.707 +
1.708 + // Update the last visit time
1.709 + $sql = 'UPDATE ' . USERS_TABLE . '
1.710 + SET user_lastvisit = ' . (int) $this->data['session_time'] . '
1.711 + WHERE user_id = ' . (int) $this->data['user_id'];
1.712 + $db->sql_query($sql);
1.713 + }
1.714 +
1.715 + $SID = '?sid=';
1.716 + $_SID = '';
1.717 + return true;
1.718 + }
1.719 + else
1.720 + {
1.721 + // If the ip and browser does not match make sure we only have one bot assigned to one session
1.722 + $db->sql_query('DELETE FROM ' . SESSIONS_TABLE . ' WHERE session_user_id = ' . $this->data['user_id']);
1.723 + }
1.724 + }
1.725 +
1.726 + $session_autologin = (($this->cookie_data['k'] || $persist_login) && $this->data['is_registered']) ? true : false;
1.727 + $set_admin = ($set_admin && $this->data['is_registered']) ? true : false;
1.728 +
1.729 + // Create or update the session
1.730 + $sql_ary = array(
1.731 + 'session_user_id' => (int) $this->data['user_id'],
1.732 + 'session_start' => (int) $this->time_now,
1.733 + 'session_last_visit' => (int) $this->data['session_last_visit'],
1.734 + 'session_time' => (int) $this->time_now,
1.735 + 'session_browser' => (string) trim(substr($this->browser, 0, 149)),
1.736 + 'session_forwarded_for' => (string) $this->forwarded_for,
1.737 + 'session_ip' => (string) $this->ip,
1.738 + 'session_autologin' => ($session_autologin) ? 1 : 0,
1.739 + 'session_admin' => ($set_admin) ? 1 : 0,
1.740 + 'session_viewonline' => ($viewonline) ? 1 : 0,
1.741 + );
1.742 +
1.743 + if ($this->update_session_page)
1.744 + {
1.745 + $sql_ary['session_page'] = (string) substr($this->page['page'], 0, 199);
1.746 + $sql_ary['session_forum_id'] = $this->page['forum'];
1.747 + }
1.748 +
1.749 + $db->sql_return_on_error(true);
1.750 +
1.751 + $sql = 'DELETE
1.752 + FROM ' . SESSIONS_TABLE . '
1.753 + WHERE session_id = \'' . $db->sql_escape($this->session_id) . '\'
1.754 + AND session_user_id = ' . ANONYMOUS;
1.755 +
1.756 + if (!defined('IN_ERROR_HANDLER') && (!$this->session_id || !$db->sql_query($sql) || !$db->sql_affectedrows()))
1.757 + {
1.758 + // Limit new sessions in 1 minute period (if required)
1.759 + if (empty($this->data['session_time']) && $config['active_sessions'])
1.760 + {
1.761 +// $db->sql_return_on_error(false);
1.762 +
1.763 + $sql = 'SELECT COUNT(session_id) AS sessions
1.764 + FROM ' . SESSIONS_TABLE . '
1.765 + WHERE session_time >= ' . ($this->time_now - 60);
1.766 + $result = $db->sql_query($sql);
1.767 + $row = $db->sql_fetchrow($result);
1.768 + $db->sql_freeresult($result);
1.769 +
1.770 + if ((int) $row['sessions'] > (int) $config['active_sessions'])
1.771 + {
1.772 + send_status_line(503, 'Service Unavailable');
1.773 + trigger_error('BOARD_UNAVAILABLE');
1.774 + }
1.775 + }
1.776 + }
1.777 +
1.778 + // Since we re-create the session id here, the inserted row must be unique. Therefore, we display potential errors.
1.779 + // Commented out because it will not allow forums to update correctly
1.780 +// $db->sql_return_on_error(false);
1.781 +
1.782 + // Something quite important: session_page always holds the *last* page visited, except for the *first* visit.
1.783 + // We are not able to simply have an empty session_page btw, therefore we need to tell phpBB how to detect this special case.
1.784 + // If the session id is empty, we have a completely new one and will set an "identifier" here. This identifier is able to be checked later.
1.785 + if (empty($this->data['session_id']))
1.786 + {
1.787 + // This is a temporary variable, only set for the very first visit
1.788 + $this->data['session_created'] = true;
1.789 + }
1.790 +
1.791 + $this->session_id = $this->data['session_id'] = md5(unique_id());
1.792 +
1.793 + $sql_ary['session_id'] = (string) $this->session_id;
1.794 + $sql_ary['session_page'] = (string) substr($this->page['page'], 0, 199);
1.795 + $sql_ary['session_forum_id'] = $this->page['forum'];
1.796 +
1.797 + $sql = 'INSERT INTO ' . SESSIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1.798 + $db->sql_query($sql);
1.799 +
1.800 + $db->sql_return_on_error(false);
1.801 +
1.802 + // Regenerate autologin/persistent login key
1.803 + if ($session_autologin)
1.804 + {
1.805 + $this->set_login_key();
1.806 + }
1.807 +
1.808 + // refresh data
1.809 + $SID = '?sid=' . $this->session_id;
1.810 + $_SID = $this->session_id;
1.811 + $this->data = array_merge($this->data, $sql_ary);
1.812 +
1.813 + if (!$bot)
1.814 + {
1.815 + $cookie_expire = $this->time_now + (($config['max_autologin_time']) ? 86400 * (int) $config['max_autologin_time'] : 31536000);
1.816 +
1.817 + $this->set_cookie('u', $this->cookie_data['u'], $cookie_expire);
1.818 + $this->set_cookie('k', $this->cookie_data['k'], $cookie_expire);
1.819 + $this->set_cookie('sid', $this->session_id, $cookie_expire);
1.820 +
1.821 + unset($cookie_expire);
1.822 +
1.823 + $sql = 'SELECT COUNT(session_id) AS sessions
1.824 + FROM ' . SESSIONS_TABLE . '
1.825 + WHERE session_user_id = ' . (int) $this->data['user_id'] . '
1.826 + AND session_time >= ' . (int) ($this->time_now - (max($config['session_length'], $config['form_token_lifetime'])));
1.827 + $result = $db->sql_query($sql);
1.828 + $row = $db->sql_fetchrow($result);
1.829 + $db->sql_freeresult($result);
1.830 +
1.831 + if ((int) $row['sessions'] <= 1 || empty($this->data['user_form_salt']))
1.832 + {
1.833 + $this->data['user_form_salt'] = unique_id();
1.834 + // Update the form key
1.835 + $sql = 'UPDATE ' . USERS_TABLE . '
1.836 + SET user_form_salt = \'' . $db->sql_escape($this->data['user_form_salt']) . '\'
1.837 + WHERE user_id = ' . (int) $this->data['user_id'];
1.838 + $db->sql_query($sql);
1.839 + }
1.840 + }
1.841 + else
1.842 + {
1.843 + $this->data['session_time'] = $this->data['session_last_visit'] = $this->time_now;
1.844 +
1.845 + // Update the last visit time
1.846 + $sql = 'UPDATE ' . USERS_TABLE . '
1.847 + SET user_lastvisit = ' . (int) $this->data['session_time'] . '
1.848 + WHERE user_id = ' . (int) $this->data['user_id'];
1.849 + $db->sql_query($sql);
1.850 +
1.851 + $SID = '?sid=';
1.852 + $_SID = '';
1.853 + }
1.854 +
1.855 + return true;
1.856 + }
1.857 +
1.858 + /**
1.859 + * Kills a session
1.860 + *
1.861 + * This method does what it says on the tin. It will delete a pre-existing session.
1.862 + * It resets cookie information (destroying any autologin key within that cookie data)
1.863 + * and update the users information from the relevant session data. It will then
1.864 + * grab guest user information.
1.865 + */
1.866 + function session_kill($new_session = true)
1.867 + {
1.868 + global $SID, $_SID, $db, $config, $phpbb_root_path, $phpEx;
1.869 +
1.870 + $sql = 'DELETE FROM ' . SESSIONS_TABLE . "
1.871 + WHERE session_id = '" . $db->sql_escape($this->session_id) . "'
1.872 + AND session_user_id = " . (int) $this->data['user_id'];
1.873 + $db->sql_query($sql);
1.874 +
1.875 + // Allow connecting logout with external auth method logout
1.876 + $method = basename(trim($config['auth_method']));
1.877 + include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
1.878 +
1.879 + $method = 'logout_' . $method;
1.880 + if (function_exists($method))
1.881 + {
1.882 + $method($this->data, $new_session);
1.883 + }
1.884 +
1.885 + if ($this->data['user_id'] != ANONYMOUS)
1.886 + {
1.887 + // Delete existing session, update last visit info first!
1.888 + if (!isset($this->data['session_time']))
1.889 + {
1.890 + $this->data['session_time'] = time();
1.891 + }
1.892 +
1.893 + $sql = 'UPDATE ' . USERS_TABLE . '
1.894 + SET user_lastvisit = ' . (int) $this->data['session_time'] . '
1.895 + WHERE user_id = ' . (int) $this->data['user_id'];
1.896 + $db->sql_query($sql);
1.897 +
1.898 + if ($this->cookie_data['k'])
1.899 + {
1.900 + $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
1.901 + WHERE user_id = ' . (int) $this->data['user_id'] . "
1.902 + AND key_id = '" . $db->sql_escape(md5($this->cookie_data['k'])) . "'";
1.903 + $db->sql_query($sql);
1.904 + }
1.905 +
1.906 + // Reset the data array
1.907 + $this->data = array();
1.908 +
1.909 + $sql = 'SELECT *
1.910 + FROM ' . USERS_TABLE . '
1.911 + WHERE user_id = ' . ANONYMOUS;
1.912 + $result = $db->sql_query($sql);
1.913 + $this->data = $db->sql_fetchrow($result);
1.914 + $db->sql_freeresult($result);
1.915 + }
1.916 +
1.917 + $cookie_expire = $this->time_now - 31536000;
1.918 + $this->set_cookie('u', '', $cookie_expire);
1.919 + $this->set_cookie('k', '', $cookie_expire);
1.920 + $this->set_cookie('sid', '', $cookie_expire);
1.921 + unset($cookie_expire);
1.922 +
1.923 + $SID = '?sid=';
1.924 + $this->session_id = $_SID = '';
1.925 +
1.926 + // To make sure a valid session is created we create one for the anonymous user
1.927 + if ($new_session)
1.928 + {
1.929 + $this->session_create(ANONYMOUS);
1.930 + }
1.931 +
1.932 + return true;
1.933 + }
1.934 +
1.935 + /**
1.936 + * Session garbage collection
1.937 + *
1.938 + * This looks a lot more complex than it really is. Effectively we are
1.939 + * deleting any sessions older than an admin definable limit. Due to the
1.940 + * way in which we maintain session data we have to ensure we update user
1.941 + * data before those sessions are destroyed. In addition this method
1.942 + * removes autologin key information that is older than an admin defined
1.943 + * limit.
1.944 + */
1.945 + function session_gc()
1.946 + {
1.947 + global $db, $config, $phpbb_root_path, $phpEx;
1.948 +
1.949 + $batch_size = 10;
1.950 +
1.951 + if (!$this->time_now)
1.952 + {
1.953 + $this->time_now = time();
1.954 + }
1.955 +
1.956 + // Firstly, delete guest sessions
1.957 + $sql = 'DELETE FROM ' . SESSIONS_TABLE . '
1.958 + WHERE session_user_id = ' . ANONYMOUS . '
1.959 + AND session_time < ' . (int) ($this->time_now - $config['session_length']);
1.960 + $db->sql_query($sql);
1.961 +
1.962 + // Get expired sessions, only most recent for each user
1.963 + $sql = 'SELECT session_user_id, session_page, MAX(session_time) AS recent_time
1.964 + FROM ' . SESSIONS_TABLE . '
1.965 + WHERE session_time < ' . ($this->time_now - $config['session_length']) . '
1.966 + GROUP BY session_user_id, session_page';
1.967 + $result = $db->sql_query_limit($sql, $batch_size);
1.968 +
1.969 + $del_user_id = array();
1.970 + $del_sessions = 0;
1.971 +
1.972 + while ($row = $db->sql_fetchrow($result))
1.973 + {
1.974 + $sql = 'UPDATE ' . USERS_TABLE . '
1.975 + SET user_lastvisit = ' . (int) $row['recent_time'] . ", user_lastpage = '" . $db->sql_escape($row['session_page']) . "'
1.976 + WHERE user_id = " . (int) $row['session_user_id'];
1.977 + $db->sql_query($sql);
1.978 +
1.979 + $del_user_id[] = (int) $row['session_user_id'];
1.980 + $del_sessions++;
1.981 + }
1.982 + $db->sql_freeresult($result);
1.983 +
1.984 + if (sizeof($del_user_id))
1.985 + {
1.986 + // Delete expired sessions
1.987 + $sql = 'DELETE FROM ' . SESSIONS_TABLE . '
1.988 + WHERE ' . $db->sql_in_set('session_user_id', $del_user_id) . '
1.989 + AND session_time < ' . ($this->time_now - $config['session_length']);
1.990 + $db->sql_query($sql);
1.991 + }
1.992 +
1.993 + if ($del_sessions < $batch_size)
1.994 + {
1.995 + // Less than 10 users, update gc timer ... else we want gc
1.996 + // called again to delete other sessions
1.997 + set_config('session_last_gc', $this->time_now, true);
1.998 +
1.999 + if ($config['max_autologin_time'])
1.1000 + {
1.1001 + $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
1.1002 + WHERE last_login < ' . (time() - (86400 * (int) $config['max_autologin_time']));
1.1003 + $db->sql_query($sql);
1.1004 + }
1.1005 +
1.1006 + // only called from CRON; should be a safe workaround until the infrastructure gets going
1.1007 + if (!class_exists('phpbb_captcha_factory'))
1.1008 + {
1.1009 + include($phpbb_root_path . "includes/captcha/captcha_factory." . $phpEx);
1.1010 + }
1.1011 + phpbb_captcha_factory::garbage_collect($config['captcha_plugin']);
1.1012 +
1.1013 + $sql = 'DELETE FROM ' . LOGIN_ATTEMPT_TABLE . '
1.1014 + WHERE attempt_time < ' . (time() - (int) $config['ip_login_limit_time']);
1.1015 + $db->sql_query($sql);
1.1016 + }
1.1017 +
1.1018 + return;
1.1019 + }
1.1020 +
1.1021 + /**
1.1022 + * Sets a cookie
1.1023 + *
1.1024 + * Sets a cookie of the given name with the specified data for the given length of time. If no time is specified, a session cookie will be set.
1.1025 + *
1.1026 + * @param string $name Name of the cookie, will be automatically prefixed with the phpBB cookie name. track becomes [cookie_name]_track then.
1.1027 + * @param string $cookiedata The data to hold within the cookie
1.1028 + * @param int $cookietime The expiration time as UNIX timestamp. If 0 is provided, a session cookie is set.
1.1029 + */
1.1030 + function set_cookie($name, $cookiedata, $cookietime)
1.1031 + {
1.1032 + global $config;
1.1033 +
1.1034 + $name_data = rawurlencode($config['cookie_name'] . '_' . $name) . '=' . rawurlencode($cookiedata);
1.1035 + $expire = gmdate('D, d-M-Y H:i:s \\G\\M\\T', $cookietime);
1.1036 + $domain = (!$config['cookie_domain'] || $config['cookie_domain'] == 'localhost' || $config['cookie_domain'] == '127.0.0.1') ? '' : '; domain=' . $config['cookie_domain'];
1.1037 +
1.1038 + header('Set-Cookie: ' . $name_data . (($cookietime) ? '; expires=' . $expire : '') . '; path=' . $config['cookie_path'] . $domain . ((!$config['cookie_secure']) ? '' : '; secure') . '; HttpOnly', false);
1.1039 + }
1.1040 +
1.1041 + /**
1.1042 + * Check for banned user
1.1043 + *
1.1044 + * Checks whether the supplied user is banned by id, ip or email. If no parameters
1.1045 + * are passed to the method pre-existing session data is used. If $return is false
1.1046 + * this routine does not return on finding a banned user, it outputs a relevant
1.1047 + * message and stops execution.
1.1048 + *
1.1049 + * @param string|array $user_ips Can contain a string with one IP or an array of multiple IPs
1.1050 + */
1.1051 + function check_ban($user_id = false, $user_ips = false, $user_email = false, $return = false)
1.1052 + {
1.1053 + global $config, $db;
1.1054 +
1.1055 + if (defined('IN_CHECK_BAN'))
1.1056 + {
1.1057 + return;
1.1058 + }
1.1059 +
1.1060 + $banned = false;
1.1061 + $cache_ttl = 3600;
1.1062 + $where_sql = array();
1.1063 +
1.1064 + $sql = 'SELECT ban_ip, ban_userid, ban_email, ban_exclude, ban_give_reason, ban_end
1.1065 + FROM ' . BANLIST_TABLE . '
1.1066 + WHERE ';
1.1067 +
1.1068 + // Determine which entries to check, only return those
1.1069 + if ($user_email === false)
1.1070 + {
1.1071 + $where_sql[] = "ban_email = ''";
1.1072 + }
1.1073 +
1.1074 + if ($user_ips === false)
1.1075 + {
1.1076 + $where_sql[] = "(ban_ip = '' OR ban_exclude = 1)";
1.1077 + }
1.1078 +
1.1079 + if ($user_id === false)
1.1080 + {
1.1081 + $where_sql[] = '(ban_userid = 0 OR ban_exclude = 1)';
1.1082 + }
1.1083 + else
1.1084 + {
1.1085 + $cache_ttl = ($user_id == ANONYMOUS) ? 3600 : 0;
1.1086 + $_sql = '(ban_userid = ' . $user_id;
1.1087 +
1.1088 + if ($user_email !== false)
1.1089 + {
1.1090 + $_sql .= " OR ban_email <> ''";
1.1091 + }
1.1092 +
1.1093 + if ($user_ips !== false)
1.1094 + {
1.1095 + $_sql .= " OR ban_ip <> ''";
1.1096 + }
1.1097 +
1.1098 + $_sql .= ')';
1.1099 +
1.1100 + $where_sql[] = $_sql;
1.1101 + }
1.1102 +
1.1103 + $sql .= (sizeof($where_sql)) ? implode(' AND ', $where_sql) : '';
1.1104 + $result = $db->sql_query($sql, $cache_ttl);
1.1105 +
1.1106 + $ban_triggered_by = 'user';
1.1107 + while ($row = $db->sql_fetchrow($result))
1.1108 + {
1.1109 + if ($row['ban_end'] && $row['ban_end'] < time())
1.1110 + {
1.1111 + continue;
1.1112 + }
1.1113 +
1.1114 + $ip_banned = false;
1.1115 + if (!empty($row['ban_ip']))
1.1116 + {
1.1117 + if (!is_array($user_ips))
1.1118 + {
1.1119 + $ip_banned = preg_match('#^' . str_replace('\*', '.*?', preg_quote($row['ban_ip'], '#')) . '$#i', $user_ips);
1.1120 + }
1.1121 + else
1.1122 + {
1.1123 + foreach ($user_ips as $user_ip)
1.1124 + {
1.1125 + if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($row['ban_ip'], '#')) . '$#i', $user_ip))
1.1126 + {
1.1127 + $ip_banned = true;
1.1128 + break;
1.1129 + }
1.1130 + }
1.1131 + }
1.1132 + }
1.1133 +
1.1134 + if ((!empty($row['ban_userid']) && intval($row['ban_userid']) == $user_id) ||
1.1135 + $ip_banned ||
1.1136 + (!empty($row['ban_email']) && preg_match('#^' . str_replace('\*', '.*?', preg_quote($row['ban_email'], '#')) . '$#i', $user_email)))
1.1137 + {
1.1138 + if (!empty($row['ban_exclude']))
1.1139 + {
1.1140 + $banned = false;
1.1141 + break;
1.1142 + }
1.1143 + else
1.1144 + {
1.1145 + $banned = true;
1.1146 + $ban_row = $row;
1.1147 +
1.1148 + if (!empty($row['ban_userid']) && intval($row['ban_userid']) == $user_id)
1.1149 + {
1.1150 + $ban_triggered_by = 'user';
1.1151 + }
1.1152 + else if ($ip_banned)
1.1153 + {
1.1154 + $ban_triggered_by = 'ip';
1.1155 + }
1.1156 + else
1.1157 + {
1.1158 + $ban_triggered_by = 'email';
1.1159 + }
1.1160 +
1.1161 + // Don't break. Check if there is an exclude rule for this user
1.1162 + }
1.1163 + }
1.1164 + }
1.1165 + $db->sql_freeresult($result);
1.1166 +
1.1167 + if ($banned && !$return)
1.1168 + {
1.1169 + global $template;
1.1170 +
1.1171 + // If the session is empty we need to create a valid one...
1.1172 + if (empty($this->session_id))
1.1173 + {
1.1174 + // This seems to be no longer needed? - #14971
1.1175 +// $this->session_create(ANONYMOUS);
1.1176 + }
1.1177 +
1.1178 + // Initiate environment ... since it won't be set at this stage
1.1179 + $this->setup();
1.1180 +
1.1181 + // Logout the user, banned users are unable to use the normal 'logout' link
1.1182 + if ($this->data['user_id'] != ANONYMOUS)
1.1183 + {
1.1184 + $this->session_kill();
1.1185 + }
1.1186 +
1.1187 + // We show a login box here to allow founders accessing the board if banned by IP
1.1188 + if (defined('IN_LOGIN') && $this->data['user_id'] == ANONYMOUS)
1.1189 + {
1.1190 + global $phpEx;
1.1191 +
1.1192 + $this->setup('ucp');
1.1193 + $this->data['is_registered'] = $this->data['is_bot'] = false;
1.1194 +
1.1195 + // Set as a precaution to allow login_box() handling this case correctly as well as this function not being executed again.
1.1196 + define('IN_CHECK_BAN', 1);
1.1197 +
1.1198 + login_box("index.$phpEx");
1.1199 +
1.1200 + // The false here is needed, else the user is able to circumvent the ban.
1.1201 + $this->session_kill(false);
1.1202 + }
1.1203 +
1.1204 + // Ok, we catch the case of an empty session id for the anonymous user...
1.1205 + // This can happen if the user is logging in, banned by username and the login_box() being called "again".
1.1206 + if (empty($this->session_id) && defined('IN_CHECK_BAN'))
1.1207 + {
1.1208 + $this->session_create(ANONYMOUS);
1.1209 + }
1.1210 +
1.1211 +
1.1212 + // Determine which message to output
1.1213 + $till_date = ($ban_row['ban_end']) ? $this->format_date($ban_row['ban_end']) : '';
1.1214 + $message = ($ban_row['ban_end']) ? 'BOARD_BAN_TIME' : 'BOARD_BAN_PERM';
1.1215 +
1.1216 + $message = sprintf($this->lang[$message], $till_date, '<a href="mailto:' . $config['board_contact'] . '">', '</a>');
1.1217 + $message .= ($ban_row['ban_give_reason']) ? '<br /><br />' . sprintf($this->lang['BOARD_BAN_REASON'], $ban_row['ban_give_reason']) : '';
1.1218 + $message .= '<br /><br /><em>' . $this->lang['BAN_TRIGGERED_BY_' . strtoupper($ban_triggered_by)] . '</em>';
1.1219 +
1.1220 + // To circumvent session_begin returning a valid value and the check_ban() not called on second page view, we kill the session again
1.1221 + $this->session_kill(false);
1.1222 +
1.1223 + // A very special case... we are within the cron script which is not supposed to print out the ban message... show blank page
1.1224 + if (defined('IN_CRON'))
1.1225 + {
1.1226 + garbage_collection();
1.1227 + exit_handler();
1.1228 + exit;
1.1229 + }
1.1230 +
1.1231 + trigger_error($message);
1.1232 + }
1.1233 +
1.1234 + return ($banned && $ban_row['ban_give_reason']) ? $ban_row['ban_give_reason'] : $banned;
1.1235 + }
1.1236 +
1.1237 + /**
1.1238 + * Check if ip is blacklisted
1.1239 + * This should be called only where absolutly necessary
1.1240 + *
1.1241 + * Only IPv4 (rbldns does not support AAAA records/IPv6 lookups)
1.1242 + *
1.1243 + * @author satmd (from the php manual)
1.1244 + * @param string $mode register/post - spamcop for example is ommitted for posting
1.1245 + * @return false if ip is not blacklisted, else an array([checked server], [lookup])
1.1246 + */
1.1247 + function check_dnsbl($mode, $ip = false)
1.1248 + {
1.1249 + if ($ip === false)
1.1250 + {
1.1251 + $ip = $this->ip;
1.1252 + }
1.1253 +
1.1254 + // Neither Spamhaus nor Spamcop supports IPv6 addresses.
1.1255 + if (strpos($ip, ':') !== false)
1.1256 + {
1.1257 + return false;
1.1258 + }
1.1259 +
1.1260 + $dnsbl_check = array(
1.1261 + 'sbl.spamhaus.org' => 'http://www.spamhaus.org/query/bl?ip=',
1.1262 + );
1.1263 +
1.1264 + if ($mode == 'register')
1.1265 + {
1.1266 + $dnsbl_check['bl.spamcop.net'] = 'http://spamcop.net/bl.shtml?';
1.1267 + }
1.1268 +
1.1269 + if ($ip)
1.1270 + {
1.1271 + $quads = explode('.', $ip);
1.1272 + $reverse_ip = $quads[3] . '.' . $quads[2] . '.' . $quads[1] . '.' . $quads[0];
1.1273 +
1.1274 + // Need to be listed on all servers...
1.1275 + $listed = true;
1.1276 + $info = array();
1.1277 +
1.1278 + foreach ($dnsbl_check as $dnsbl => $lookup)
1.1279 + {
1.1280 + if (phpbb_checkdnsrr($reverse_ip . '.' . $dnsbl . '.', 'A') === true)
1.1281 + {
1.1282 + $info = array($dnsbl, $lookup . $ip);
1.1283 + }
1.1284 + else
1.1285 + {
1.1286 + $listed = false;
1.1287 + }
1.1288 + }
1.1289 +
1.1290 + if ($listed)
1.1291 + {
1.1292 + return $info;
1.1293 + }
1.1294 + }
1.1295 +
1.1296 + return false;
1.1297 + }
1.1298 +
1.1299 + /**
1.1300 + * Check if URI is blacklisted
1.1301 + * This should be called only where absolutly necessary, for example on the submitted website field
1.1302 + * This function is not in use at the moment and is only included for testing purposes, it may not work at all!
1.1303 + * This means it is untested at the moment and therefore commented out
1.1304 + *
1.1305 + * @param string $uri URI to check
1.1306 + * @return true if uri is on blacklist, else false. Only blacklist is checked (~zero FP), no grey lists
1.1307 + function check_uribl($uri)
1.1308 + {
1.1309 + // Normally parse_url() is not intended to parse uris
1.1310 + // We need to get the top-level domain name anyway... change.
1.1311 + $uri = parse_url($uri);
1.1312 +
1.1313 + if ($uri === false || empty($uri['host']))
1.1314 + {
1.1315 + return false;
1.1316 + }
1.1317 +
1.1318 + $uri = trim($uri['host']);
1.1319 +
1.1320 + if ($uri)
1.1321 + {
1.1322 + // One problem here... the return parameter for the "windows" method is different from what
1.1323 + // we expect... this may render this check useless...
1.1324 + if (phpbb_checkdnsrr($uri . '.multi.uribl.com.', 'A') === true)
1.1325 + {
1.1326 + return true;
1.1327 + }
1.1328 + }
1.1329 +
1.1330 + return false;
1.1331 + }
1.1332 + */
1.1333 +
1.1334 + /**
1.1335 + * Set/Update a persistent login key
1.1336 + *
1.1337 + * This method creates or updates a persistent session key. When a user makes
1.1338 + * use of persistent (formerly auto-) logins a key is generated and stored in the
1.1339 + * DB. When they revisit with the same key it's automatically updated in both the
1.1340 + * DB and cookie. Multiple keys may exist for each user representing different
1.1341 + * browsers or locations. As with _any_ non-secure-socket no passphrase login this
1.1342 + * remains vulnerable to exploit.
1.1343 + */
1.1344 + function set_login_key($user_id = false, $key = false, $user_ip = false)
1.1345 + {
1.1346 + global $config, $db;
1.1347 +
1.1348 + $user_id = ($user_id === false) ? $this->data['user_id'] : $user_id;
1.1349 + $user_ip = ($user_ip === false) ? $this->ip : $user_ip;
1.1350 + $key = ($key === false) ? (($this->cookie_data['k']) ? $this->cookie_data['k'] : false) : $key;
1.1351 +
1.1352 + $key_id = unique_id(hexdec(substr($this->session_id, 0, 8)));
1.1353 +
1.1354 + $sql_ary = array(
1.1355 + 'key_id' => (string) md5($key_id),
1.1356 + 'last_ip' => (string) $this->ip,
1.1357 + 'last_login' => (int) time()
1.1358 + );
1.1359 +
1.1360 + if (!$key)
1.1361 + {
1.1362 + $sql_ary += array(
1.1363 + 'user_id' => (int) $user_id
1.1364 + );
1.1365 + }
1.1366 +
1.1367 + if ($key)
1.1368 + {
1.1369 + $sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . '
1.1370 + SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1.1371 + WHERE user_id = ' . (int) $user_id . "
1.1372 + AND key_id = '" . $db->sql_escape(md5($key)) . "'";
1.1373 + }
1.1374 + else
1.1375 + {
1.1376 + $sql = 'INSERT INTO ' . SESSIONS_KEYS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1.1377 + }
1.1378 + $db->sql_query($sql);
1.1379 +
1.1380 + $this->cookie_data['k'] = $key_id;
1.1381 +
1.1382 + return false;
1.1383 + }
1.1384 +
1.1385 + /**
1.1386 + * Reset all login keys for the specified user
1.1387 + *
1.1388 + * This method removes all current login keys for a specified (or the current)
1.1389 + * user. It will be called on password change to render old keys unusable
1.1390 + */
1.1391 + function reset_login_keys($user_id = false)
1.1392 + {
1.1393 + global $config, $db;
1.1394 +
1.1395 + $user_id = ($user_id === false) ? (int) $this->data['user_id'] : (int) $user_id;
1.1396 +
1.1397 + $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
1.1398 + WHERE user_id = ' . (int) $user_id;
1.1399 + $db->sql_query($sql);
1.1400 +
1.1401 + // If the user is logged in, update last visit info first before deleting sessions
1.1402 + $sql = 'SELECT session_time, session_page
1.1403 + FROM ' . SESSIONS_TABLE . '
1.1404 + WHERE session_user_id = ' . (int) $user_id . '
1.1405 + ORDER BY session_time DESC';
1.1406 + $result = $db->sql_query_limit($sql, 1);
1.1407 + $row = $db->sql_fetchrow($result);
1.1408 + $db->sql_freeresult($result);
1.1409 +
1.1410 + if ($row)
1.1411 + {
1.1412 + $sql = 'UPDATE ' . USERS_TABLE . '
1.1413 + SET user_lastvisit = ' . (int) $row['session_time'] . ", user_lastpage = '" . $db->sql_escape($row['session_page']) . "'
1.1414 + WHERE user_id = " . (int) $user_id;
1.1415 + $db->sql_query($sql);
1.1416 + }
1.1417 +
1.1418 + // Let's also clear any current sessions for the specified user_id
1.1419 + // If it's the current user then we'll leave this session intact
1.1420 + $sql_where = 'session_user_id = ' . (int) $user_id;
1.1421 + $sql_where .= ($user_id === (int) $this->data['user_id']) ? " AND session_id <> '" . $db->sql_escape($this->session_id) . "'" : '';
1.1422 +
1.1423 + $sql = 'DELETE FROM ' . SESSIONS_TABLE . "
1.1424 + WHERE $sql_where";
1.1425 + $db->sql_query($sql);
1.1426 +
1.1427 + // We're changing the password of the current user and they have a key
1.1428 + // Lets regenerate it to be safe
1.1429 + if ($user_id === (int) $this->data['user_id'] && $this->cookie_data['k'])
1.1430 + {
1.1431 + $this->set_login_key($user_id);
1.1432 + }
1.1433 + }
1.1434 +
1.1435 +
1.1436 + /**
1.1437 + * Check if the request originated from the same page.
1.1438 + * @param bool $check_script_path If true, the path will be checked as well
1.1439 + */
1.1440 + function validate_referer($check_script_path = false)
1.1441 + {
1.1442 + global $config;
1.1443 +
1.1444 + // no referer - nothing to validate, user's fault for turning it off (we only check on POST; so meta can't be the reason)
1.1445 + if (empty($this->referer) || empty($this->host))
1.1446 + {
1.1447 + return true;
1.1448 + }
1.1449 +
1.1450 + $host = htmlspecialchars($this->host);
1.1451 + $ref = substr($this->referer, strpos($this->referer, '://') + 3);
1.1452 +
1.1453 + if (!(stripos($ref, $host) === 0) && (!$config['force_server_vars'] || !(stripos($ref, $config['server_name']) === 0)))
1.1454 + {
1.1455 + return false;
1.1456 + }
1.1457 + else if ($check_script_path && rtrim($this->page['root_script_path'], '/') !== '')
1.1458 + {
1.1459 + $ref = substr($ref, strlen($host));
1.1460 + $server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT');
1.1461 +
1.1462 + if ($server_port !== 80 && $server_port !== 443 && stripos($ref, ":$server_port") === 0)
1.1463 + {
1.1464 + $ref = substr($ref, strlen(":$server_port"));
1.1465 + }
1.1466 +
1.1467 + if (!(stripos(rtrim($ref, '/'), rtrim($this->page['root_script_path'], '/')) === 0))
1.1468 + {
1.1469 + return false;
1.1470 + }
1.1471 + }
1.1472 +
1.1473 + return true;
1.1474 + }
1.1475 +
1.1476 +
1.1477 + function unset_admin()
1.1478 + {
1.1479 + global $db;
1.1480 + $sql = 'UPDATE ' . SESSIONS_TABLE . '
1.1481 + SET session_admin = 0
1.1482 + WHERE session_id = \'' . $db->sql_escape($this->session_id) . '\'';
1.1483 + $db->sql_query($sql);
1.1484 + }
1.1485 +}
1.1486 +
1.1487 +
1.1488 +/**
1.1489 +* Base user class
1.1490 +*
1.1491 +* This is the overarching class which contains (through session extend)
1.1492 +* all methods utilised for user functionality during a session.
1.1493 +*
1.1494 +* @package phpBB3
1.1495 +*/
1.1496 +class user extends session
1.1497 +{
1.1498 + var $lang = array();
1.1499 + var $help = array();
1.1500 + var $theme = array();
1.1501 + var $date_format;
1.1502 + var $timezone;
1.1503 + var $dst;
1.1504 +
1.1505 + var $lang_name = false;
1.1506 + var $lang_id = false;
1.1507 + var $lang_path;
1.1508 + var $img_lang;
1.1509 + var $img_array = array();
1.1510 +
1.1511 + // Able to add new options (up to id 31)
1.1512 + var $keyoptions = array('viewimg' => 0, 'viewflash' => 1, 'viewsmilies' => 2, 'viewsigs' => 3, 'viewavatars' => 4, 'viewcensors' => 5, 'attachsig' => 6, 'bbcode' => 8, 'smilies' => 9, 'popuppm' => 10, 'sig_bbcode' => 15, 'sig_smilies' => 16, 'sig_links' => 17);
1.1513 + var $keyvalues = array();
1.1514 +
1.1515 + /**
1.1516 + * Constructor to set the lang path
1.1517 + */
1.1518 + function user()
1.1519 + {
1.1520 + global $phpbb_root_path;
1.1521 +
1.1522 + $this->lang_path = $phpbb_root_path . 'language/';
1.1523 + }
1.1524 +
1.1525 + /**
1.1526 + * Function to set custom language path (able to use directory outside of phpBB)
1.1527 + *
1.1528 + * @param string $lang_path New language path used.
1.1529 + * @access public
1.1530 + */
1.1531 + function set_custom_lang_path($lang_path)
1.1532 + {
1.1533 + $this->lang_path = $lang_path;
1.1534 +
1.1535 + if (substr($this->lang_path, -1) != '/')
1.1536 + {
1.1537 + $this->lang_path .= '/';
1.1538 + }
1.1539 + }
1.1540 +
1.1541 + /**
1.1542 + * Setup basic user-specific items (style, language, ...)
1.1543 + */
1.1544 + function setup($lang_set = false, $style = false)
1.1545 + {
1.1546 + global $db, $template, $config, $auth, $phpEx, $phpbb_root_path, $cache;
1.1547 +
1.1548 + if ($this->data['user_id'] != ANONYMOUS)
1.1549 + {
1.1550 + $this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']);
1.1551 +
1.1552 + $this->date_format = $this->data['user_dateformat'];
1.1553 + $this->timezone = $this->data['user_timezone'] * 3600;
1.1554 + $this->dst = $this->data['user_dst'] * 3600;
1.1555 + }
1.1556 + else
1.1557 + {
1.1558 + $this->lang_name = basename($config['default_lang']);
1.1559 + $this->date_format = $config['default_dateformat'];
1.1560 + $this->timezone = $config['board_timezone'] * 3600;
1.1561 + $this->dst = $config['board_dst'] * 3600;
1.1562 +
1.1563 + /**
1.1564 + * If a guest user is surfing, we try to guess his/her language first by obtaining the browser language
1.1565 + * If re-enabled we need to make sure only those languages installed are checked
1.1566 + * Commented out so we do not loose the code.
1.1567 +
1.1568 + if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
1.1569 + {
1.1570 + $accept_lang_ary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
1.1571 +
1.1572 + foreach ($accept_lang_ary as $accept_lang)
1.1573 + {
1.1574 + // Set correct format ... guess full xx_YY form
1.1575 + $accept_lang = substr($accept_lang, 0, 2) . '_' . strtoupper(substr($accept_lang, 3, 2));
1.1576 + $accept_lang = basename($accept_lang);
1.1577 +
1.1578 + if (file_exists($this->lang_path . $accept_lang . "/common.$phpEx"))
1.1579 + {
1.1580 + $this->lang_name = $config['default_lang'] = $accept_lang;
1.1581 + break;
1.1582 + }
1.1583 + else
1.1584 + {
1.1585 + // No match on xx_YY so try xx
1.1586 + $accept_lang = substr($accept_lang, 0, 2);
1.1587 + $accept_lang = basename($accept_lang);
1.1588 +
1.1589 + if (file_exists($this->lang_path . $accept_lang . "/common.$phpEx"))
1.1590 + {
1.1591 + $this->lang_name = $config['default_lang'] = $accept_lang;
1.1592 + break;
1.1593 + }
1.1594 + }
1.1595 + }
1.1596 + }
1.1597 + */
1.1598 + }
1.1599 +
1.1600 + // We include common language file here to not load it every time a custom language file is included
1.1601 + $lang = &$this->lang;
1.1602 +
1.1603 + // Do not suppress error if in DEBUG_EXTRA mode
1.1604 + $include_result = (defined('DEBUG_EXTRA')) ? (include $this->lang_path . $this->lang_name . "/common.$phpEx") : (@include $this->lang_path . $this->lang_name . "/common.$phpEx");
1.1605 +
1.1606 + if ($include_result === false)
1.1607 + {
1.1608 + die('Language file ' . $this->lang_path . $this->lang_name . "/common.$phpEx" . " couldn't be opened.");
1.1609 + }
1.1610 +
1.1611 + $this->add_lang($lang_set);
1.1612 + unset($lang_set);
1.1613 +
1.1614 + if (!empty($_GET['style']) && $auth->acl_get('a_styles') && !defined('ADMIN_START'))
1.1615 + {
1.1616 + global $SID, $_EXTRA_URL;
1.1617 +
1.1618 + $style = request_var('style', 0);
1.1619 + $SID .= '&style=' . $style;
1.1620 + $_EXTRA_URL = array('style=' . $style);
1.1621 + }
1.1622 + else
1.1623 + {
1.1624 + // Set up style
1.1625 + $style = ($style) ? $style : ((!$config['override_user_style']) ? $this->data['user_style'] : $config['default_style']);
1.1626 + }
1.1627 +
1.1628 + $sql = 'SELECT s.style_id, t.template_storedb, t.template_path, t.template_id, t.bbcode_bitfield, t.template_inherits_id, t.template_inherit_path, c.theme_path, c.theme_name, c.theme_storedb, c.theme_id, i.imageset_path, i.imageset_id, i.imageset_name
1.1629 + FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . " i
1.1630 + WHERE s.style_id = $style
1.1631 + AND t.template_id = s.template_id
1.1632 + AND c.theme_id = s.theme_id
1.1633 + AND i.imageset_id = s.imageset_id";
1.1634 + $result = $db->sql_query($sql, 3600);
1.1635 + $this->theme = $db->sql_fetchrow($result);
1.1636 + $db->sql_freeresult($result);
1.1637 +
1.1638 + // User has wrong style
1.1639 + if (!$this->theme && $style == $this->data['user_style'])
1.1640 + {
1.1641 + $style = $this->data['user_style'] = $config['default_style'];
1.1642 +
1.1643 + $sql = 'UPDATE ' . USERS_TABLE . "
1.1644 + SET user_style = $style
1.1645 + WHERE user_id = {$this->data['user_id']}";
1.1646 + $db->sql_query($sql);
1.1647 +
1.1648 + $sql = 'SELECT s.style_id, t.template_storedb, t.template_path, t.template_id, t.bbcode_bitfield, c.theme_path, c.theme_name, c.theme_storedb, c.theme_id, i.imageset_path, i.imageset_id, i.imageset_name
1.1649 + FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . " i
1.1650 + WHERE s.style_id = $style
1.1651 + AND t.template_id = s.template_id
1.1652 + AND c.theme_id = s.theme_id
1.1653 + AND i.imageset_id = s.imageset_id";
1.1654 + $result = $db->sql_query($sql, 3600);
1.1655 + $this->theme = $db->sql_fetchrow($result);
1.1656 + $db->sql_freeresult($result);
1.1657 + }
1.1658 +
1.1659 + if (!$this->theme)
1.1660 + {
1.1661 + trigger_error('Could not get style data', E_USER_ERROR);
1.1662 + }
1.1663 +
1.1664 + // Now parse the cfg file and cache it
1.1665 + $parsed_items = $cache->obtain_cfg_items($this->theme);
1.1666 +
1.1667 + // We are only interested in the theme configuration for now
1.1668 + $parsed_items = $parsed_items['theme'];
1.1669 +
1.1670 + $check_for = array(
1.1671 + 'parse_css_file' => (int) 0,
1.1672 + 'pagination_sep' => (string) ', '
1.1673 + );
1.1674 +
1.1675 + foreach ($check_for as $key => $default_value)
1.1676 + {
1.1677 + $this->theme[$key] = (isset($parsed_items[$key])) ? $parsed_items[$key] : $default_value;
1.1678 + settype($this->theme[$key], gettype($default_value));
1.1679 +
1.1680 + if (is_string($default_value))
1.1681 + {
1.1682 + $this->theme[$key] = htmlspecialchars($this->theme[$key]);
1.1683 + }
1.1684 + }
1.1685 +
1.1686 + // If the style author specified the theme needs to be cached
1.1687 + // (because of the used paths and variables) than make sure it is the case.
1.1688 + // For example, if the theme uses language-specific images it needs to be stored in db.
1.1689 + if (!$this->theme['theme_storedb'] && $this->theme['parse_css_file'])
1.1690 + {
1.1691 + $this->theme['theme_storedb'] = 1;
1.1692 +
1.1693 + $stylesheet = file_get_contents("{$phpbb_root_path}styles/{$this->theme['theme_path']}/theme/stylesheet.css");
1.1694 + // Match CSS imports
1.1695 + $matches = array();
1.1696 + preg_match_all('/@import url\(["\'](.*)["\']\);/i', $stylesheet, $matches);
1.1697 +
1.1698 + if (sizeof($matches))
1.1699 + {
1.1700 + $content = '';
1.1701 + foreach ($matches[0] as $idx => $match)
1.1702 + {
1.1703 + if ($content = @file_get_contents("{$phpbb_root_path}styles/{$this->theme['theme_path']}/theme/" . $matches[1][$idx]))
1.1704 + {
1.1705 + $content = trim($content);
1.1706 + }
1.1707 + else
1.1708 + {
1.1709 + $content = '';
1.1710 + }
1.1711 + $stylesheet = str_replace($match, $content, $stylesheet);
1.1712 + }
1.1713 + unset($content);
1.1714 + }
1.1715 +
1.1716 + $stylesheet = str_replace('./', 'styles/' . $this->theme['theme_path'] . '/theme/', $stylesheet);
1.1717 +
1.1718 + $sql_ary = array(
1.1719 + 'theme_data' => $stylesheet,
1.1720 + 'theme_mtime' => time(),
1.1721 + 'theme_storedb' => 1
1.1722 + );
1.1723 +
1.1724 + $sql = 'UPDATE ' . STYLES_THEME_TABLE . '
1.1725 + SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1.1726 + WHERE theme_id = ' . $this->theme['theme_id'];
1.1727 + $db->sql_query($sql);
1.1728 +
1.1729 + unset($sql_ary);
1.1730 + }
1.1731 +
1.1732 + $template->set_template();
1.1733 +
1.1734 + $this->img_lang = (file_exists($phpbb_root_path . 'styles/' . $this->theme['imageset_path'] . '/imageset/' . $this->lang_name)) ? $this->lang_name : $config['default_lang'];
1.1735 +
1.1736 + // Same query in style.php
1.1737 + $sql = 'SELECT *
1.1738 + FROM ' . STYLES_IMAGESET_DATA_TABLE . '
1.1739 + WHERE imageset_id = ' . $this->theme['imageset_id'] . "
1.1740 + AND image_filename <> ''
1.1741 + AND image_lang IN ('" . $db->sql_escape($this->img_lang) . "', '')";
1.1742 + $result = $db->sql_query($sql, 3600);
1.1743 +
1.1744 + $localised_images = false;
1.1745 + while ($row = $db->sql_fetchrow($result))
1.1746 + {
1.1747 + if ($row['image_lang'])
1.1748 + {
1.1749 + $localised_images = true;
1.1750 + }
1.1751 +
1.1752 + $row['image_filename'] = rawurlencode($row['image_filename']);
1.1753 + $this->img_array[$row['image_name']] = $row;
1.1754 + }
1.1755 + $db->sql_freeresult($result);
1.1756 +
1.1757 + // there were no localised images, try to refresh the localised imageset for the user's language
1.1758 + if (!$localised_images)
1.1759 + {
1.1760 + // Attention: this code ignores the image definition list from acp_styles and just takes everything
1.1761 + // that the config file contains
1.1762 + $sql_ary = array();
1.1763 +
1.1764 + $db->sql_transaction('begin');
1.1765 +
1.1766 + $sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . '
1.1767 + WHERE imageset_id = ' . $this->theme['imageset_id'] . '
1.1768 + AND image_lang = \'' . $db->sql_escape($this->img_lang) . '\'';
1.1769 + $result = $db->sql_query($sql);
1.1770 +
1.1771 + if (@file_exists("{$phpbb_root_path}styles/{$this->theme['imageset_path']}/imageset/{$this->img_lang}/imageset.cfg"))
1.1772 + {
1.1773 + $cfg_data_imageset_data = parse_cfg_file("{$phpbb_root_path}styles/{$this->theme['imageset_path']}/imageset/{$this->img_lang}/imageset.cfg");
1.1774 + foreach ($cfg_data_imageset_data as $image_name => $value)
1.1775 + {
1.1776 + if (strpos($value, '*') !== false)
1.1777 + {
1.1778 + if (substr($value, -1, 1) === '*')
1.1779 + {
1.1780 + list($image_filename, $image_height) = explode('*', $value);
1.1781 + $image_width = 0;
1.1782 + }
1.1783 + else
1.1784 + {
1.1785 + list($image_filename, $image_height, $image_width) = explode('*', $value);
1.1786 + }
1.1787 + }
1.1788 + else
1.1789 + {
1.1790 + $image_filename = $value;
1.1791 + $image_height = $image_width = 0;
1.1792 + }
1.1793 +
1.1794 + if (strpos($image_name, 'img_') === 0 && $image_filename)
1.1795 + {
1.1796 + $image_name = substr($image_name, 4);
1.1797 + $sql_ary[] = array(
1.1798 + 'image_name' => (string) $image_name,
1.1799 + 'image_filename' => (string) $image_filename,
1.1800 + 'image_height' => (int) $image_height,
1.1801 + 'image_width' => (int) $image_width,
1.1802 + 'imageset_id' => (int) $this->theme['imageset_id'],
1.1803 + 'image_lang' => (string) $this->img_lang,
1.1804 + );
1.1805 + }
1.1806 + }
1.1807 + }
1.1808 +
1.1809 + if (sizeof($sql_ary))
1.1810 + {
1.1811 + $db->sql_multi_insert(STYLES_IMAGESET_DATA_TABLE, $sql_ary);
1.1812 + $db->sql_transaction('commit');
1.1813 + $cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
1.1814 +
1.1815 + add_log('admin', 'LOG_IMAGESET_LANG_REFRESHED', $this->theme['imageset_name'], $this->img_lang);
1.1816 + }
1.1817 + else
1.1818 + {
1.1819 + $db->sql_transaction('commit');
1.1820 + add_log('admin', 'LOG_IMAGESET_LANG_MISSING', $this->theme['imageset_name'], $this->img_lang);
1.1821 + }
1.1822 + }
1.1823 +
1.1824 + // Call phpbb_user_session_handler() in case external application want to "bend" some variables or replace classes...
1.1825 + // After calling it we continue script execution...
1.1826 + phpbb_user_session_handler();
1.1827 +
1.1828 + // If this function got called from the error handler we are finished here.
1.1829 + if (defined('IN_ERROR_HANDLER'))
1.1830 + {
1.1831 + return;
1.1832 + }
1.1833 +
1.1834 + // Disable board if the install/ directory is still present
1.1835 + // For the brave development army we do not care about this, else we need to comment out this everytime we develop locally
1.1836 + if (!defined('DEBUG_EXTRA') && !defined('ADMIN_START') && !defined('IN_INSTALL') && !defined('IN_LOGIN') && file_exists($phpbb_root_path . 'install') && !is_file($phpbb_root_path . 'install'))
1.1837 + {
1.1838 + // Adjust the message slightly according to the permissions
1.1839 + if ($auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_'))
1.1840 + {
1.1841 + $message = 'REMOVE_INSTALL';
1.1842 + }
1.1843 + else
1.1844 + {
1.1845 + $message = (!empty($config['board_disable_msg'])) ? $config['board_disable_msg'] : 'BOARD_DISABLE';
1.1846 + }
1.1847 + trigger_error($message);
1.1848 + }
1.1849 +
1.1850 + // Is board disabled and user not an admin or moderator?
1.1851 + if ($config['board_disable'] && !defined('IN_LOGIN') && !$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
1.1852 + {
1.1853 + if ($this->data['is_bot'])
1.1854 + {
1.1855 + send_status_line(503, 'Service Unavailable');
1.1856 + }
1.1857 +
1.1858 + $message = (!empty($config['board_disable_msg'])) ? $config['board_disable_msg'] : 'BOARD_DISABLE';
1.1859 + trigger_error($message);
1.1860 + }
1.1861 +
1.1862 + // Is load exceeded?
1.1863 + if ($config['limit_load'] && $this->load !== false)
1.1864 + {
1.1865 + if ($this->load > floatval($config['limit_load']) && !defined('IN_LOGIN') && !defined('IN_ADMIN'))
1.1866 + {
1.1867 + // Set board disabled to true to let the admins/mods get the proper notification
1.1868 + $config['board_disable'] = '1';
1.1869 +
1.1870 + if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
1.1871 + {
1.1872 + if ($this->data['is_bot'])
1.1873 + {
1.1874 + send_status_line(503, 'Service Unavailable');
1.1875 + }
1.1876 + trigger_error('BOARD_UNAVAILABLE');
1.1877 + }
1.1878 + }
1.1879 + }
1.1880 +
1.1881 + if (isset($this->data['session_viewonline']))
1.1882 + {
1.1883 + // Make sure the user is able to hide his session
1.1884 + if (!$this->data['session_viewonline'])
1.1885 + {
1.1886 + // Reset online status if not allowed to hide the session...
1.1887 + if (!$auth->acl_get('u_hideonline'))
1.1888 + {
1.1889 + $sql = 'UPDATE ' . SESSIONS_TABLE . '
1.1890 + SET session_viewonline = 1
1.1891 + WHERE session_user_id = ' . $this->data['user_id'];
1.1892 + $db->sql_query($sql);
1.1893 + $this->data['session_viewonline'] = 1;
1.1894 + }
1.1895 + }
1.1896 + else if (!$this->data['user_allow_viewonline'])
1.1897 + {
1.1898 + // the user wants to hide and is allowed to -> cloaking device on.
1.1899 + if ($auth->acl_get('u_hideonline'))
1.1900 + {
1.1901 + $sql = 'UPDATE ' . SESSIONS_TABLE . '
1.1902 + SET session_viewonline = 0
1.1903 + WHERE session_user_id = ' . $this->data['user_id'];
1.1904 + $db->sql_query($sql);
1.1905 + $this->data['session_viewonline'] = 0;
1.1906 + }
1.1907 + }
1.1908 + }
1.1909 +
1.1910 +
1.1911 + // Does the user need to change their password? If so, redirect to the
1.1912 + // ucp profile reg_details page ... of course do not redirect if we're already in the ucp
1.1913 + if (!defined('IN_ADMIN') && !defined('ADMIN_START') && $config['chg_passforce'] && !empty($this->data['is_registered']) && $auth->acl_get('u_chgpasswd') && $this->data['user_passchg'] < time() - ($config['chg_passforce'] * 86400))
1.1914 + {
1.1915 + if (strpos($this->page['query_string'], 'mode=reg_details') === false && $this->page['page_name'] != "ucp.$phpEx")
1.1916 + {
1.1917 + redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=profile&mode=reg_details'));
1.1918 + }
1.1919 + }
1.1920 +
1.1921 + return;
1.1922 + }
1.1923 +
1.1924 + /**
1.1925 + * More advanced language substitution
1.1926 + * Function to mimic sprintf() with the possibility of using phpBB's language system to substitute nullar/singular/plural forms.
1.1927 + * Params are the language key and the parameters to be substituted.
1.1928 + * This function/functionality is inspired by SHS` and Ashe.
1.1929 + *
1.1930 + * Example call: <samp>$user->lang('NUM_POSTS_IN_QUEUE', 1);</samp>
1.1931 + */
1.1932 + function lang()
1.1933 + {
1.1934 + $args = func_get_args();
1.1935 + $key = $args[0];
1.1936 +
1.1937 + if (is_array($key))
1.1938 + {
1.1939 + $lang = &$this->lang[array_shift($key)];
1.1940 +
1.1941 + foreach ($key as $_key)
1.1942 + {
1.1943 + $lang = &$lang[$_key];
1.1944 + }
1.1945 + }
1.1946 + else
1.1947 + {
1.1948 + $lang = &$this->lang[$key];
1.1949 + }
1.1950 +
1.1951 + // Return if language string does not exist
1.1952 + if (!isset($lang) || (!is_string($lang) && !is_array($lang)))
1.1953 + {
1.1954 + return $key;
1.1955 + }
1.1956 +
1.1957 + // If the language entry is a string, we simply mimic sprintf() behaviour
1.1958 + if (is_string($lang))
1.1959 + {
1.1960 + if (sizeof($args) == 1)
1.1961 + {
1.1962 + return $lang;
1.1963 + }
1.1964 +
1.1965 + // Replace key with language entry and simply pass along...
1.1966 + $args[0] = $lang;
1.1967 + return call_user_func_array('sprintf', $args);
1.1968 + }
1.1969 +
1.1970 + // It is an array... now handle different nullar/singular/plural forms
1.1971 + $key_found = false;
1.1972 +
1.1973 + // We now get the first number passed and will select the key based upon this number
1.1974 + for ($i = 1, $num_args = sizeof($args); $i < $num_args; $i++)
1.1975 + {
1.1976 + if (is_int($args[$i]))
1.1977 + {
1.1978 + $numbers = array_keys($lang);
1.1979 +
1.1980 + foreach ($numbers as $num)
1.1981 + {
1.1982 + if ($num > $args[$i])
1.1983 + {
1.1984 + break;
1.1985 + }
1.1986 +
1.1987 + $key_found = $num;
1.1988 + }
1.1989 + break;
1.1990 + }
1.1991 + }
1.1992 +
1.1993 + // Ok, let's check if the key was found, else use the last entry (because it is mostly the plural form)
1.1994 + if ($key_found === false)
1.1995 + {
1.1996 + $numbers = array_keys($lang);
1.1997 + $key_found = end($numbers);
1.1998 + }
1.1999 +
1.2000 + // Use the language string we determined and pass it to sprintf()
1.2001 + $args[0] = $lang[$key_found];
1.2002 + return call_user_func_array('sprintf', $args);
1.2003 + }
1.2004 +
1.2005 + /**
1.2006 + * Add Language Items - use_db and use_help are assigned where needed (only use them to force inclusion)
1.2007 + *
1.2008 + * @param mixed $lang_set specifies the language entries to include
1.2009 + * @param bool $use_db internal variable for recursion, do not use
1.2010 + * @param bool $use_help internal variable for recursion, do not use
1.2011 + *
1.2012 + * Examples:
1.2013 + * <code>
1.2014 + * $lang_set = array('posting', 'help' => 'faq');
1.2015 + * $lang_set = array('posting', 'viewtopic', 'help' => array('bbcode', 'faq'))
1.2016 + * $lang_set = array(array('posting', 'viewtopic'), 'help' => array('bbcode', 'faq'))
1.2017 + * $lang_set = 'posting'
1.2018 + * $lang_set = array('help' => 'faq', 'db' => array('help:faq', 'posting'))
1.2019 + * </code>
1.2020 + */
1.2021 + function add_lang($lang_set, $use_db = false, $use_help = false)
1.2022 + {
1.2023 + global $phpEx;
1.2024 +
1.2025 + if (is_array($lang_set))
1.2026 + {
1.2027 + foreach ($lang_set as $key => $lang_file)
1.2028 + {
1.2029 + // Please do not delete this line.
1.2030 + // We have to force the type here, else [array] language inclusion will not work
1.2031 + $key = (string) $key;
1.2032 +
1.2033 + if ($key == 'db')
1.2034 + {
1.2035 + $this->add_lang($lang_file, true, $use_help);
1.2036 + }
1.2037 + else if ($key == 'help')
1.2038 + {
1.2039 + $this->add_lang($lang_file, $use_db, true);
1.2040 + }
1.2041 + else if (!is_array($lang_file))
1.2042 + {
1.2043 + $this->set_lang($this->lang, $this->help, $lang_file, $use_db, $use_help);
1.2044 + }
1.2045 + else
1.2046 + {
1.2047 + $this->add_lang($lang_file, $use_db, $use_help);
1.2048 + }
1.2049 + }
1.2050 + unset($lang_set);
1.2051 + }
1.2052 + else if ($lang_set)
1.2053 + {
1.2054 + $this->set_lang($this->lang, $this->help, $lang_set, $use_db, $use_help);
1.2055 + }
1.2056 + }
1.2057 +
1.2058 + /**
1.2059 + * Set language entry (called by add_lang)
1.2060 + * @access private
1.2061 + */
1.2062 + function set_lang(&$lang, &$help, $lang_file, $use_db = false, $use_help = false)
1.2063 + {
1.2064 + global $phpEx;
1.2065 +
1.2066 + // Make sure the language name is set (if the user setup did not happen it is not set)
1.2067 + if (!$this->lang_name)
1.2068 + {
1.2069 + global $config;
1.2070 + $this->lang_name = basename($config['default_lang']);
1.2071 + }
1.2072 +
1.2073 + // $lang == $this->lang
1.2074 + // $help == $this->help
1.2075 + // - add appropriate variables here, name them as they are used within the language file...
1.2076 + if (!$use_db)
1.2077 + {
1.2078 + if ($use_help && strpos($lang_file, '/') !== false)
1.2079 + {
1.2080 + $language_filename = $this->lang_path . $this->lang_name . '/' . substr($lang_file, 0, stripos($lang_file, '/') + 1) . 'help_' . substr($lang_file, stripos($lang_file, '/') + 1) . '.' . $phpEx;
1.2081 + }
1.2082 + else
1.2083 + {
1.2084 + $language_filename = $this->lang_path . $this->lang_name . '/' . (($use_help) ? 'help_' : '') . $lang_file . '.' . $phpEx;
1.2085 + }
1.2086 +
1.2087 + if (!file_exists($language_filename))
1.2088 + {
1.2089 + global $config;
1.2090 +
1.2091 + if ($this->lang_name == 'en')
1.2092 + {
1.2093 + // The user's selected language is missing the file, the board default's language is missing the file, and the file doesn't exist in /en.
1.2094 + $language_filename = str_replace($this->lang_path . 'en', $this->lang_path . $this->data['user_lang'], $language_filename);
1.2095 + trigger_error('Language file ' . $language_filename . ' couldn\'t be opened.', E_USER_ERROR);
1.2096 + }
1.2097 + else if ($this->lang_name == basename($config['default_lang']))
1.2098 + {
1.2099 + // Fall back to the English Language
1.2100 + $this->lang_name = 'en';
1.2101 + $this->set_lang($lang, $help, $lang_file, $use_db, $use_help);
1.2102 + }
1.2103 + else if ($this->lang_name == $this->data['user_lang'])
1.2104 + {
1.2105 + // Fall back to the board default language
1.2106 + $this->lang_name = basename($config['default_lang']);
1.2107 + $this->set_lang($lang, $help, $lang_file, $use_db, $use_help);
1.2108 + }
1.2109 +
1.2110 + // Reset the lang name
1.2111 + $this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']);
1.2112 + return;
1.2113 + }
1.2114 +
1.2115 + // Do not suppress error if in DEBUG_EXTRA mode
1.2116 + $include_result = (defined('DEBUG_EXTRA')) ? (include $language_filename) : (@include $language_filename);
1.2117 +
1.2118 + if ($include_result === false)
1.2119 + {
1.2120 + trigger_error('Language file ' . $language_filename . ' couldn\'t be opened.', E_USER_ERROR);
1.2121 + }
1.2122 + }
1.2123 + else if ($use_db)
1.2124 + {
1.2125 + // Get Database Language Strings
1.2126 + // Put them into $lang if nothing is prefixed, put them into $help if help: is prefixed
1.2127 + // For example: help:faq, posting
1.2128 + }
1.2129 + }
1.2130 +
1.2131 + /**
1.2132 + * Format user date
1.2133 + *
1.2134 + * @param int $gmepoch unix timestamp
1.2135 + * @param string $format date format in date() notation. | used to indicate relative dates, for example |d m Y|, h:i is translated to Today, h:i.
1.2136 + * @param bool $forcedate force non-relative date format.
1.2137 + *
1.2138 + * @return mixed translated date
1.2139 + */
1.2140 + function format_date($gmepoch, $format = false, $forcedate = false)
1.2141 + {
1.2142 + static $midnight;
1.2143 + static $date_cache;
1.2144 +
1.2145 + $format = (!$format) ? $this->date_format : $format;
1.2146 + $now = time();
1.2147 + $delta = $now - $gmepoch;
1.2148 +
1.2149 + if (!isset($date_cache[$format]))
1.2150 + {
1.2151 + // Is the user requesting a friendly date format (i.e. 'Today 12:42')?
1.2152 + $date_cache[$format] = array(
1.2153 + 'is_short' => strpos($format, '|'),
1.2154 + 'format_short' => substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1),
1.2155 + 'format_long' => str_replace('|', '', $format),
1.2156 + 'lang' => $this->lang['datetime'],
1.2157 + );
1.2158 +
1.2159 + // Short representation of month in format? Some languages use different terms for the long and short format of May
1.2160 + if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
1.2161 + {
1.2162 + $date_cache[$format]['lang']['May'] = $this->lang['datetime']['May_short'];
1.2163 + }
1.2164 + }
1.2165 +
1.2166 + // Zone offset
1.2167 + $zone_offset = $this->timezone + $this->dst;
1.2168 +
1.2169 + // Show date <= 1 hour ago as 'xx min ago' but not greater than 60 seconds in the future
1.2170 + // A small tolerence is given for times in the future but in the same minute are displayed as '< than a minute ago'
1.2171 + if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now / 60) % 60) == (($gmepoch / 60) % 60)) && $date_cache[$format]['is_short'] !== false && !$forcedate && isset($this->lang['datetime']['AGO']))
1.2172 + {
1.2173 + return $this->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
1.2174 + }
1.2175 +
1.2176 + if (!$midnight)
1.2177 + {
1.2178 + list($d, $m, $y) = explode(' ', gmdate('j n Y', time() + $zone_offset));
1.2179 + $midnight = gmmktime(0, 0, 0, $m, $d, $y) - $zone_offset;
1.2180 + }
1.2181 +
1.2182 + if ($date_cache[$format]['is_short'] !== false && !$forcedate && !($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800))
1.2183 + {
1.2184 + $day = false;
1.2185 +
1.2186 + if ($gmepoch > $midnight + 86400)
1.2187 + {
1.2188 + $day = 'TOMORROW';
1.2189 + }
1.2190 + else if ($gmepoch > $midnight)
1.2191 + {
1.2192 + $day = 'TODAY';
1.2193 + }
1.2194 + else if ($gmepoch > $midnight - 86400)
1.2195 + {
1.2196 + $day = 'YESTERDAY';
1.2197 + }
1.2198 +
1.2199 + if ($day !== false)
1.2200 + {
1.2201 + return str_replace('||', $this->lang['datetime'][$day], strtr(@gmdate($date_cache[$format]['format_short'], $gmepoch + $zone_offset), $date_cache[$format]['lang']));
1.2202 + }
1.2203 + }
1.2204 +
1.2205 + return strtr(@gmdate($date_cache[$format]['format_long'], $gmepoch + $zone_offset), $date_cache[$format]['lang']);
1.2206 + }
1.2207 +
1.2208 + /**
1.2209 + * Get language id currently used by the user
1.2210 + */
1.2211 + function get_iso_lang_id()
1.2212 + {
1.2213 + global $config, $db;
1.2214 +
1.2215 + if (!empty($this->lang_id))
1.2216 + {
1.2217 + return $this->lang_id;
1.2218 + }
1.2219 +
1.2220 + if (!$this->lang_name)
1.2221 + {
1.2222 + $this->lang_name = $config['default_lang'];
1.2223 + }
1.2224 +
1.2225 + $sql = 'SELECT lang_id
1.2226 + FROM ' . LANG_TABLE . "
1.2227 + WHERE lang_iso = '" . $db->sql_escape($this->lang_name) . "'";
1.2228 + $result = $db->sql_query($sql);
1.2229 + $this->lang_id = (int) $db->sql_fetchfield('lang_id');
1.2230 + $db->sql_freeresult($result);
1.2231 +
1.2232 + return $this->lang_id;
1.2233 + }
1.2234 +
1.2235 + /**
1.2236 + * Get users profile fields
1.2237 + */
1.2238 + function get_profile_fields($user_id)
1.2239 + {
1.2240 + global $db;
1.2241 +
1.2242 + if (isset($this->profile_fields))
1.2243 + {
1.2244 + return;
1.2245 + }
1.2246 +
1.2247 + $sql = 'SELECT *
1.2248 + FROM ' . PROFILE_FIELDS_DATA_TABLE . "
1.2249 + WHERE user_id = $user_id";
1.2250 + $result = $db->sql_query_limit($sql, 1);
1.2251 + $this->profile_fields = (!($row = $db->sql_fetchrow($result))) ? array() : $row;
1.2252 + $db->sql_freeresult($result);
1.2253 + }
1.2254 +
1.2255 + /**
1.2256 + * Specify/Get image
1.2257 + * $suffix is no longer used - we know it. ;) It is there for backward compatibility.
1.2258 + */
1.2259 + function img($img, $alt = '', $width = false, $suffix = '', $type = 'full_tag')
1.2260 + {
1.2261 + static $imgs;
1.2262 + global $phpbb_root_path;
1.2263 +
1.2264 + $img_data = &$imgs[$img];
1.2265 +
1.2266 + if (empty($img_data))
1.2267 + {
1.2268 + if (!isset($this->img_array[$img]))
1.2269 + {
1.2270 + // Do not fill the image to let designers decide what to do if the image is empty
1.2271 + $img_data = '';
1.2272 + return $img_data;
1.2273 + }
1.2274 +
1.2275 + // Use URL if told so
1.2276 + $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
1.2277 +
1.2278 + $path = 'styles/' . rawurlencode($this->theme['imageset_path']) . '/imageset/' . ($this->img_array[$img]['image_lang'] ? $this->img_array[$img]['image_lang'] .'/' : '') . $this->img_array[$img]['image_filename'];
1.2279 +
1.2280 + $img_data['src'] = $root_path . $path;
1.2281 + $img_data['width'] = $this->img_array[$img]['image_width'];
1.2282 + $img_data['height'] = $this->img_array[$img]['image_height'];
1.2283 +
1.2284 + // We overwrite the width and height to the phpbb logo's width
1.2285 + // and height here if the contents of the site_logo file are
1.2286 + // really equal to the phpbb_logo
1.2287 + // This allows us to change the dimensions of the phpbb_logo without
1.2288 + // modifying the imageset.cfg and causing a conflict for everyone
1.2289 + // who modified it for their custom logo on updating
1.2290 + if ($img == 'site_logo' && file_exists($phpbb_root_path . $path))
1.2291 + {
1.2292 + global $cache;
1.2293 +
1.2294 + $img_file_hashes = $cache->get('imageset_site_logo_md5');
1.2295 +
1.2296 + if ($img_file_hashes === false)
1.2297 + {
1.2298 + $img_file_hashes = array();
1.2299 + }
1.2300 +
1.2301 + $key = $this->theme['imageset_path'] . '::' . $this->img_array[$img]['image_lang'];
1.2302 + if (!isset($img_file_hashes[$key]))
1.2303 + {
1.2304 + $img_file_hashes[$key] = md5(file_get_contents($phpbb_root_path . $path));
1.2305 + $cache->put('imageset_site_logo_md5', $img_file_hashes);
1.2306 + }
1.2307 +
1.2308 + $phpbb_logo_hash = '0c461a32cd3621643105f0d02a772c10';
1.2309 +
1.2310 + if ($phpbb_logo_hash == $img_file_hashes[$key])
1.2311 + {
1.2312 + $img_data['width'] = '149';
1.2313 + $img_data['height'] = '52';
1.2314 + }
1.2315 + }
1.2316 + }
1.2317 +
1.2318 + $alt = (!empty($this->lang[$alt])) ? $this->lang[$alt] : $alt;
1.2319 +
1.2320 + switch ($type)
1.2321 + {
1.2322 + case 'src':
1.2323 + return $img_data['src'];
1.2324 + break;
1.2325 +
1.2326 + case 'width':
1.2327 + return ($width === false) ? $img_data['width'] : $width;
1.2328 + break;
1.2329 +
1.2330 + case 'height':
1.2331 + return $img_data['height'];
1.2332 + break;
1.2333 +
1.2334 + default:
1.2335 + $use_width = ($width === false) ? $img_data['width'] : $width;
1.2336 +
1.2337 + return '<img src="' . $img_data['src'] . '"' . (($use_width) ? ' width="' . $use_width . '"' : '') . (($img_data['height']) ? ' height="' . $img_data['height'] . '"' : '') . ' alt="' . $alt . '" title="' . $alt . '" />';
1.2338 + break;
1.2339 + }
1.2340 + }
1.2341 +
1.2342 + /**
1.2343 + * Get option bit field from user options
1.2344 + */
1.2345 + function optionget($key, $data = false)
1.2346 + {
1.2347 + if (!isset($this->keyvalues[$key]))
1.2348 + {
1.2349 + $var = ($data) ? $data : $this->data['user_options'];
1.2350 + $this->keyvalues[$key] = ($var & 1 << $this->keyoptions[$key]) ? true : false;
1.2351 + }
1.2352 +
1.2353 + return $this->keyvalues[$key];
1.2354 + }
1.2355 +
1.2356 + /**
1.2357 + * Set option bit field for user options
1.2358 + */
1.2359 + function optionset($key, $value, $data = false)
1.2360 + {
1.2361 + $var = ($data) ? $data : $this->data['user_options'];
1.2362 +
1.2363 + if ($value && !($var & 1 << $this->keyoptions[$key]))
1.2364 + {
1.2365 + $var += 1 << $this->keyoptions[$key];
1.2366 + }
1.2367 + else if (!$value && ($var & 1 << $this->keyoptions[$key]))
1.2368 + {
1.2369 + $var -= 1 << $this->keyoptions[$key];
1.2370 + }
1.2371 + else
1.2372 + {
1.2373 + return ($data) ? $var : false;
1.2374 + }
1.2375 +
1.2376 + if (!$data)
1.2377 + {
1.2378 + $this->data['user_options'] = $var;
1.2379 + return true;
1.2380 + }
1.2381 + else
1.2382 + {
1.2383 + return $var;
1.2384 + }
1.2385 + }
1.2386 +
1.2387 + /**
1.2388 + * Funtion to make the user leave the NEWLY_REGISTERED system group.
1.2389 + * @access public
1.2390 + */
1.2391 + function leave_newly_registered()
1.2392 + {
1.2393 + global $db;
1.2394 +
1.2395 + if (empty($this->data['user_new']))
1.2396 + {
1.2397 + return false;
1.2398 + }
1.2399 +
1.2400 + if (!function_exists('remove_newly_registered'))
1.2401 + {
1.2402 + global $phpbb_root_path, $phpEx;
1.2403 +
1.2404 + include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
1.2405 + }
1.2406 + if ($group = remove_newly_registered($this->data['user_id'], $this->data))
1.2407 + {
1.2408 + $this->data['group_id'] = $group;
1.2409 +
1.2410 + }
1.2411 + $this->data['user_permissions'] = '';
1.2412 + $this->data['user_new'] = 0;
1.2413 +
1.2414 + return true;
1.2415 + }
1.2416 +
1.2417 + /**
1.2418 + * Returns all password protected forum ids the user is currently NOT authenticated for.
1.2419 + *
1.2420 + * @return array Array of forum ids
1.2421 + * @access public
1.2422 + */
1.2423 + function get_passworded_forums()
1.2424 + {
1.2425 + global $db;
1.2426 +
1.2427 + $sql = 'SELECT f.forum_id, fa.user_id
1.2428 + FROM ' . FORUMS_TABLE . ' f
1.2429 + LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa
1.2430 + ON (fa.forum_id = f.forum_id
1.2431 + AND fa.session_id = '" . $db->sql_escape($this->session_id) . "')
1.2432 + WHERE f.forum_password <> ''";
1.2433 + $result = $db->sql_query($sql);
1.2434 +
1.2435 + $forum_ids = array();
1.2436 + while ($row = $db->sql_fetchrow($result))
1.2437 + {
1.2438 + $forum_id = (int) $row['forum_id'];
1.2439 +
1.2440 + if ($row['user_id'] != $this->data['user_id'])
1.2441 + {
1.2442 + $forum_ids[$forum_id] = $forum_id;
1.2443 + }
1.2444 + }
1.2445 + $db->sql_freeresult($result);
1.2446 +
1.2447 + return $forum_ids;
1.2448 + }
1.2449 +}
1.2450 +
1.2451 +?>
1.2452 \ No newline at end of file