includes/session.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:53:57 +0400
changeset 17 049ab39a9b9c
parent 0 ceef6d64c21d
permissions -rw-r--r--
Show gamers online everywhere
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Session class
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class session
indvdum@0
    24
{
indvdum@0
    25
	var $cookie_data = array();
indvdum@0
    26
	var $page = array();
indvdum@0
    27
	var $data = array();
indvdum@0
    28
	var $browser = '';
indvdum@0
    29
	var $forwarded_for = '';
indvdum@0
    30
	var $host = '';
indvdum@0
    31
	var $session_id = '';
indvdum@0
    32
	var $ip = '';
indvdum@0
    33
	var $load = 0;
indvdum@0
    34
	var $time_now = 0;
indvdum@0
    35
	var $update_session_page = true;
indvdum@0
    36
indvdum@0
    37
	/**
indvdum@0
    38
	* Extract current session page
indvdum@0
    39
	*
indvdum@0
    40
	* @param string $root_path current root path (phpbb_root_path)
indvdum@0
    41
	*/
indvdum@0
    42
	function extract_current_page($root_path)
indvdum@0
    43
	{
indvdum@0
    44
		$page_array = array();
indvdum@0
    45
indvdum@0
    46
		// First of all, get the request uri...
indvdum@0
    47
		$script_name = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : getenv('PHP_SELF');
indvdum@0
    48
		$args = (!empty($_SERVER['QUERY_STRING'])) ? explode('&', $_SERVER['QUERY_STRING']) : explode('&', getenv('QUERY_STRING'));
indvdum@0
    49
indvdum@0
    50
		// 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...
indvdum@0
    51
		if (!$script_name)
indvdum@0
    52
		{
indvdum@0
    53
			$script_name = (!empty($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : getenv('REQUEST_URI');
indvdum@0
    54
			$script_name = (($pos = strpos($script_name, '?')) !== false) ? substr($script_name, 0, $pos) : $script_name;
indvdum@0
    55
			$page_array['failover'] = 1;
indvdum@0
    56
		}
indvdum@0
    57
indvdum@0
    58
		// Replace backslashes and doubled slashes (could happen on some proxy setups)
indvdum@0
    59
		$script_name = str_replace(array('\\', '//'), '/', $script_name);
indvdum@0
    60
indvdum@0
    61
		// Now, remove the sid and let us get a clean query string...
indvdum@0
    62
		$use_args = array();
indvdum@0
    63
indvdum@0
    64
		// Since some browser do not encode correctly we need to do this with some "special" characters...
indvdum@0
    65
		// " -> %22, ' => %27, < -> %3C, > -> %3E
indvdum@0
    66
		$find = array('"', "'", '<', '>');
indvdum@0
    67
		$replace = array('%22', '%27', '%3C', '%3E');
indvdum@0
    68
indvdum@0
    69
		foreach ($args as $key => $argument)
indvdum@0
    70
		{
indvdum@0
    71
			if (strpos($argument, 'sid=') === 0)
indvdum@0
    72
			{
indvdum@0
    73
				continue;
indvdum@0
    74
			}
indvdum@0
    75
indvdum@0
    76
			$use_args[] = str_replace($find, $replace, $argument);
indvdum@0
    77
		}
indvdum@0
    78
		unset($args);
indvdum@0
    79
indvdum@0
    80
		// The following examples given are for an request uri of {path to the phpbb directory}/adm/index.php?i=10&b=2
indvdum@0
    81
indvdum@0
    82
		// The current query string
indvdum@0
    83
		$query_string = trim(implode('&', $use_args));
indvdum@0
    84
indvdum@0
    85
		// basenamed page name (for example: index.php)
indvdum@0
    86
		$page_name = (substr($script_name, -1, 1) == '/') ? '' : basename($script_name);
indvdum@0
    87
		$page_name = urlencode(htmlspecialchars($page_name));
indvdum@0
    88
indvdum@0
    89
		// current directory within the phpBB root (for example: adm)
indvdum@0
    90
		$root_dirs = explode('/', str_replace('\\', '/', phpbb_realpath($root_path)));
indvdum@0
    91
		$page_dirs = explode('/', str_replace('\\', '/', phpbb_realpath('./')));
indvdum@0
    92
		$intersection = array_intersect_assoc($root_dirs, $page_dirs);
indvdum@0
    93
indvdum@0
    94
		$root_dirs = array_diff_assoc($root_dirs, $intersection);
indvdum@0
    95
		$page_dirs = array_diff_assoc($page_dirs, $intersection);
indvdum@0
    96
indvdum@0
    97
		$page_dir = str_repeat('../', sizeof($root_dirs)) . implode('/', $page_dirs);
indvdum@0
    98
indvdum@0
    99
		if ($page_dir && substr($page_dir, -1, 1) == '/')
indvdum@0
   100
		{
indvdum@0
   101
			$page_dir = substr($page_dir, 0, -1);
indvdum@0
   102
		}
indvdum@0
   103
indvdum@0
   104
		// Current page from phpBB root (for example: adm/index.php?i=10&b=2)
indvdum@0
   105
		$page = (($page_dir) ? $page_dir . '/' : '') . $page_name . (($query_string) ? "?$query_string" : '');
indvdum@0
   106
indvdum@0
   107
		// The script path from the webroot to the current directory (for example: /phpBB3/adm/) : always prefixed with / and ends in /
indvdum@0
   108
		$script_path = trim(str_replace('\\', '/', dirname($script_name)));
indvdum@0
   109
indvdum@0
   110
		// The script path from the webroot to the phpBB root (for example: /phpBB3/)
indvdum@0
   111
		$script_dirs = explode('/', $script_path);
indvdum@0
   112
		array_splice($script_dirs, -sizeof($page_dirs));
indvdum@0
   113
		$root_script_path = implode('/', $script_dirs) . (sizeof($root_dirs) ? '/' . implode('/', $root_dirs) : '');
indvdum@0
   114
indvdum@0
   115
		// We are on the base level (phpBB root == webroot), lets adjust the variables a bit...
indvdum@0
   116
		if (!$root_script_path)
indvdum@0
   117
		{
indvdum@0
   118
			$root_script_path = ($page_dir) ? str_replace($page_dir, '', $script_path) : $script_path;
indvdum@0
   119
		}
indvdum@0
   120
indvdum@0
   121
		$script_path .= (substr($script_path, -1, 1) == '/') ? '' : '/';
indvdum@0
   122
		$root_script_path .= (substr($root_script_path, -1, 1) == '/') ? '' : '/';
indvdum@0
   123
indvdum@0
   124
		$page_array += array(
indvdum@0
   125
			'page_name'			=> $page_name,
indvdum@0
   126
			'page_dir'			=> $page_dir,
indvdum@0
   127
indvdum@0
   128
			'query_string'		=> $query_string,
indvdum@0
   129
			'script_path'		=> str_replace(' ', '%20', htmlspecialchars($script_path)),
indvdum@0
   130
			'root_script_path'	=> str_replace(' ', '%20', htmlspecialchars($root_script_path)),
indvdum@0
   131
indvdum@0
   132
			'page'				=> $page,
indvdum@0
   133
			'forum'				=> (isset($_REQUEST['f']) && $_REQUEST['f'] > 0) ? (int) $_REQUEST['f'] : 0,
indvdum@0
   134
		);
indvdum@0
   135
indvdum@0
   136
		return $page_array;
indvdum@0
   137
	}
indvdum@0
   138
indvdum@0
   139
	/**
indvdum@0
   140
	* Get valid hostname/port. HTTP_HOST is used, SERVER_NAME if HTTP_HOST not present.
indvdum@0
   141
	*/
indvdum@0
   142
	function extract_current_hostname()
indvdum@0
   143
	{
indvdum@0
   144
		global $config;
indvdum@0
   145
indvdum@0
   146
		// Get hostname
indvdum@0
   147
		$host = (!empty($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME'));
indvdum@0
   148
indvdum@0
   149
		// Should be a string and lowered
indvdum@0
   150
		$host = (string) strtolower($host);
indvdum@0
   151
indvdum@0
   152
		// If host is equal the cookie domain or the server name (if config is set), then we assume it is valid
indvdum@0
   153
		if ((isset($config['cookie_domain']) && $host === $config['cookie_domain']) || (isset($config['server_name']) && $host === $config['server_name']))
indvdum@0
   154
		{
indvdum@0
   155
			return $host;
indvdum@0
   156
		}
indvdum@0
   157
indvdum@0
   158
		// Is the host actually a IP? If so, we use the IP... (IPv4)
indvdum@0
   159
		if (long2ip(ip2long($host)) === $host)
indvdum@0
   160
		{
indvdum@0
   161
			return $host;
indvdum@0
   162
		}
indvdum@0
   163
indvdum@0
   164
		// 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
indvdum@0
   165
		$host = @parse_url('http://' . $host);
indvdum@0
   166
		$host = (!empty($host['host'])) ? $host['host'] : '';
indvdum@0
   167
indvdum@0
   168
		// Remove any portions not removed by parse_url (#)
indvdum@0
   169
		$host = str_replace('#', '', $host);
indvdum@0
   170
indvdum@0
   171
		// If, by any means, the host is now empty, we will use a "best approach" way to guess one
indvdum@0
   172
		if (empty($host))
indvdum@0
   173
		{
indvdum@0
   174
			if (!empty($config['server_name']))
indvdum@0
   175
			{
indvdum@0
   176
				$host = $config['server_name'];
indvdum@0
   177
			}
indvdum@0
   178
			else if (!empty($config['cookie_domain']))
indvdum@0
   179
			{
indvdum@0
   180
				$host = (strpos($config['cookie_domain'], '.') === 0) ? substr($config['cookie_domain'], 1) : $config['cookie_domain'];
indvdum@0
   181
			}
indvdum@0
   182
			else
indvdum@0
   183
			{
indvdum@0
   184
				// Set to OS hostname or localhost
indvdum@0
   185
				$host = (function_exists('php_uname')) ? php_uname('n') : 'localhost';
indvdum@0
   186
			}
indvdum@0
   187
		}
indvdum@0
   188
indvdum@0
   189
		// It may be still no valid host, but for sure only a hostname (we may further expand on the cookie domain... if set)
indvdum@0
   190
		return $host;
indvdum@0
   191
	}
indvdum@0
   192
indvdum@0
   193
	/**
indvdum@0
   194
	* Start session management
indvdum@0
   195
	*
indvdum@0
   196
	* This is where all session activity begins. We gather various pieces of
indvdum@0
   197
	* information from the client and server. We test to see if a session already
indvdum@0
   198
	* exists. If it does, fine and dandy. If it doesn't we'll go on to create a
indvdum@0
   199
	* new one ... pretty logical heh? We also examine the system load (if we're
indvdum@0
   200
	* running on a system which makes such information readily available) and
indvdum@0
   201
	* halt if it's above an admin definable limit.
indvdum@0
   202
	*
indvdum@0
   203
	* @param bool $update_session_page if true the session page gets updated.
indvdum@0
   204
	*			This can be set to circumvent certain scripts to update the users last visited page.
indvdum@0
   205
	*/
indvdum@0
   206
	function session_begin($update_session_page = true)
indvdum@0
   207
	{
indvdum@0
   208
		global $phpEx, $SID, $_SID, $_EXTRA_URL, $db, $config, $phpbb_root_path;
indvdum@0
   209
indvdum@0
   210
		// Give us some basic information
indvdum@0
   211
		$this->time_now				= time();
indvdum@0
   212
		$this->cookie_data			= array('u' => 0, 'k' => '');
indvdum@0
   213
		$this->update_session_page	= $update_session_page;
indvdum@0
   214
		$this->browser				= (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
indvdum@0
   215
		$this->referer				= (!empty($_SERVER['HTTP_REFERER'])) ? htmlspecialchars((string) $_SERVER['HTTP_REFERER']) : '';
indvdum@0
   216
		$this->forwarded_for		= (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? htmlspecialchars((string) $_SERVER['HTTP_X_FORWARDED_FOR']) : '';
indvdum@0
   217
indvdum@0
   218
		$this->host					= $this->extract_current_hostname();
indvdum@0
   219
		$this->page					= $this->extract_current_page($phpbb_root_path);
indvdum@0
   220
indvdum@0
   221
		// if the forwarded for header shall be checked we have to validate its contents
indvdum@0
   222
		if ($config['forwarded_for_check'])
indvdum@0
   223
		{
indvdum@0
   224
			$this->forwarded_for = preg_replace('# {2,}#', ' ', str_replace(',', ' ', $this->forwarded_for));
indvdum@0
   225
indvdum@0
   226
			// split the list of IPs
indvdum@0
   227
			$ips = explode(' ', $this->forwarded_for);
indvdum@0
   228
			foreach ($ips as $ip)
indvdum@0
   229
			{
indvdum@0
   230
				// check IPv4 first, the IPv6 is hopefully only going to be used very seldomly
indvdum@0
   231
				if (!empty($ip) && !preg_match(get_preg_expression('ipv4'), $ip) && !preg_match(get_preg_expression('ipv6'), $ip))
indvdum@0
   232
				{
indvdum@0
   233
					// contains invalid data, don't use the forwarded for header
indvdum@0
   234
					$this->forwarded_for = '';
indvdum@0
   235
					break;
indvdum@0
   236
				}
indvdum@0
   237
			}
indvdum@0
   238
		}
indvdum@0
   239
		else
indvdum@0
   240
		{
indvdum@0
   241
			$this->forwarded_for = '';
indvdum@0
   242
		}
indvdum@0
   243
indvdum@0
   244
		if (isset($_COOKIE[$config['cookie_name'] . '_sid']) || isset($_COOKIE[$config['cookie_name'] . '_u']))
indvdum@0
   245
		{
indvdum@0
   246
			$this->cookie_data['u'] = request_var($config['cookie_name'] . '_u', 0, false, true);
indvdum@0
   247
			$this->cookie_data['k'] = request_var($config['cookie_name'] . '_k', '', false, true);
indvdum@0
   248
			$this->session_id 		= request_var($config['cookie_name'] . '_sid', '', false, true);
indvdum@0
   249
indvdum@0
   250
			$SID = (defined('NEED_SID')) ? '?sid=' . $this->session_id : '?sid=';
indvdum@0
   251
			$_SID = (defined('NEED_SID')) ? $this->session_id : '';
indvdum@0
   252
indvdum@0
   253
			if (empty($this->session_id))
indvdum@0
   254
			{
indvdum@0
   255
				$this->session_id = $_SID = request_var('sid', '');
indvdum@0
   256
				$SID = '?sid=' . $this->session_id;
indvdum@0
   257
				$this->cookie_data = array('u' => 0, 'k' => '');
indvdum@0
   258
			}
indvdum@0
   259
		}
indvdum@0
   260
		else
indvdum@0
   261
		{
indvdum@0
   262
			$this->session_id = $_SID = request_var('sid', '');
indvdum@0
   263
			$SID = '?sid=' . $this->session_id;
indvdum@0
   264
		}
indvdum@0
   265
indvdum@0
   266
		$_EXTRA_URL = array();
indvdum@0
   267
indvdum@0
   268
		// Why no forwarded_for et al? Well, too easily spoofed. With the results of my recent requests
indvdum@0
   269
		// it's pretty clear that in the majority of cases you'll at least be left with a proxy/cache ip.
indvdum@0
   270
		$this->ip = (!empty($_SERVER['REMOTE_ADDR'])) ? (string) $_SERVER['REMOTE_ADDR'] : '';
indvdum@0
   271
		$this->ip = preg_replace('# {2,}#', ' ', str_replace(',', ' ', $this->ip));
indvdum@0
   272
indvdum@0
   273
		// split the list of IPs
indvdum@0
   274
		$ips = explode(' ', trim($this->ip));
indvdum@0
   275
indvdum@0
   276
		// Default IP if REMOTE_ADDR is invalid
indvdum@0
   277
		$this->ip = '127.0.0.1';
indvdum@0
   278
indvdum@0
   279
		foreach ($ips as $ip)
indvdum@0
   280
		{
indvdum@0
   281
			if (preg_match(get_preg_expression('ipv4'), $ip))
indvdum@0
   282
			{
indvdum@0
   283
				$this->ip = $ip;
indvdum@0
   284
			}
indvdum@0
   285
			else if (preg_match(get_preg_expression('ipv6'), $ip))
indvdum@0
   286
			{
indvdum@0
   287
				// Quick check for IPv4-mapped address in IPv6
indvdum@0
   288
				if (stripos($ip, '::ffff:') === 0)
indvdum@0
   289
				{
indvdum@0
   290
					$ipv4 = substr($ip, 7);
indvdum@0
   291
indvdum@0
   292
					if (preg_match(get_preg_expression('ipv4'), $ipv4))
indvdum@0
   293
					{
indvdum@0
   294
						$ip = $ipv4;
indvdum@0
   295
					}
indvdum@0
   296
				}
indvdum@0
   297
indvdum@0
   298
				$this->ip = $ip;
indvdum@0
   299
			}
indvdum@0
   300
			else
indvdum@0
   301
			{
indvdum@0
   302
				// We want to use the last valid address in the chain
indvdum@0
   303
				// Leave foreach loop when address is invalid
indvdum@0
   304
				break;
indvdum@0
   305
			}
indvdum@0
   306
		}
indvdum@0
   307
indvdum@0
   308
		$this->load = false;
indvdum@0
   309
indvdum@0
   310
		// Load limit check (if applicable)
indvdum@0
   311
		if ($config['limit_load'] || $config['limit_search_load'])
indvdum@0
   312
		{
indvdum@0
   313
			if ((function_exists('sys_getloadavg') && $load = sys_getloadavg()) || ($load = explode(' ', @file_get_contents('/proc/loadavg'))))
indvdum@0
   314
			{
indvdum@0
   315
				$this->load = array_slice($load, 0, 1);
indvdum@0
   316
				$this->load = floatval($this->load[0]);
indvdum@0
   317
			}
indvdum@0
   318
			else
indvdum@0
   319
			{
indvdum@0
   320
				set_config('limit_load', '0');
indvdum@0
   321
				set_config('limit_search_load', '0');
indvdum@0
   322
			}
indvdum@0
   323
		}
indvdum@0
   324
indvdum@0
   325
		// Is session_id is set or session_id is set and matches the url param if required
indvdum@0
   326
		if (!empty($this->session_id) && (!defined('NEED_SID') || (isset($_GET['sid']) && $this->session_id === $_GET['sid'])))
indvdum@0
   327
		{
indvdum@0
   328
			$sql = 'SELECT u.*, s.*
indvdum@0
   329
				FROM ' . SESSIONS_TABLE . ' s, ' . USERS_TABLE . " u
indvdum@0
   330
				WHERE s.session_id = '" . $db->sql_escape($this->session_id) . "'
indvdum@0
   331
					AND u.user_id = s.session_user_id";
indvdum@0
   332
			$result = $db->sql_query($sql);
indvdum@0
   333
			$this->data = $db->sql_fetchrow($result);
indvdum@0
   334
			$db->sql_freeresult($result);
indvdum@0
   335
indvdum@0
   336
			// Did the session exist in the DB?
indvdum@0
   337
			if (isset($this->data['user_id']))
indvdum@0
   338
			{
indvdum@0
   339
				// Validate IP length according to admin ... enforces an IP
indvdum@0
   340
				// check on bots if admin requires this
indvdum@0
   341
//				$quadcheck = ($config['ip_check_bot'] && $this->data['user_type'] & USER_BOT) ? 4 : $config['ip_check'];
indvdum@0
   342
indvdum@0
   343
				if (strpos($this->ip, ':') !== false && strpos($this->data['session_ip'], ':') !== false)
indvdum@0
   344
				{
indvdum@0
   345
					$s_ip = short_ipv6($this->data['session_ip'], $config['ip_check']);
indvdum@0
   346
					$u_ip = short_ipv6($this->ip, $config['ip_check']);
indvdum@0
   347
				}
indvdum@0
   348
				else
indvdum@0
   349
				{
indvdum@0
   350
					$s_ip = implode('.', array_slice(explode('.', $this->data['session_ip']), 0, $config['ip_check']));
indvdum@0
   351
					$u_ip = implode('.', array_slice(explode('.', $this->ip), 0, $config['ip_check']));
indvdum@0
   352
				}
indvdum@0
   353
indvdum@0
   354
				$s_browser = ($config['browser_check']) ? trim(strtolower(substr($this->data['session_browser'], 0, 149))) : '';
indvdum@0
   355
				$u_browser = ($config['browser_check']) ? trim(strtolower(substr($this->browser, 0, 149))) : '';
indvdum@0
   356
indvdum@0
   357
				$s_forwarded_for = ($config['forwarded_for_check']) ? substr($this->data['session_forwarded_for'], 0, 254) : '';
indvdum@0
   358
				$u_forwarded_for = ($config['forwarded_for_check']) ? substr($this->forwarded_for, 0, 254) : '';
indvdum@0
   359
indvdum@0
   360
				// referer checks
indvdum@0
   361
				// The @ before $config['referer_validation'] suppresses notices present while running the updater
indvdum@0
   362
				$check_referer_path = (@$config['referer_validation'] == REFERER_VALIDATE_PATH);
indvdum@0
   363
				$referer_valid = true;
indvdum@0
   364
indvdum@0
   365
				// we assume HEAD and TRACE to be foul play and thus only whitelist GET
indvdum@0
   366
				if (@$config['referer_validation'] && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) !== 'get')
indvdum@0
   367
				{
indvdum@0
   368
					$referer_valid = $this->validate_referer($check_referer_path);
indvdum@0
   369
				}
indvdum@0
   370
indvdum@0
   371
				if ($u_ip === $s_ip && $s_browser === $u_browser && $s_forwarded_for === $u_forwarded_for && $referer_valid)
indvdum@0
   372
				{
indvdum@0
   373
					$session_expired = false;
indvdum@0
   374
indvdum@0
   375
					// Check whether the session is still valid if we have one
indvdum@0
   376
					$method = basename(trim($config['auth_method']));
indvdum@0
   377
					include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
indvdum@0
   378
indvdum@0
   379
					$method = 'validate_session_' . $method;
indvdum@0
   380
					if (function_exists($method))
indvdum@0
   381
					{
indvdum@0
   382
						if (!$method($this->data))
indvdum@0
   383
						{
indvdum@0
   384
							$session_expired = true;
indvdum@0
   385
						}
indvdum@0
   386
					}
indvdum@0
   387
indvdum@0
   388
					if (!$session_expired)
indvdum@0
   389
					{
indvdum@0
   390
						// Check the session length timeframe if autologin is not enabled.
indvdum@0
   391
						// Else check the autologin length... and also removing those having autologin enabled but no longer allowed board-wide.
indvdum@0
   392
						if (!$this->data['session_autologin'])
indvdum@0
   393
						{
indvdum@0
   394
							if ($this->data['session_time'] < $this->time_now - ($config['session_length'] + 60))
indvdum@0
   395
							{
indvdum@0
   396
								$session_expired = true;
indvdum@0
   397
							}
indvdum@0
   398
						}
indvdum@0
   399
						else if (!$config['allow_autologin'] || ($config['max_autologin_time'] && $this->data['session_time'] < $this->time_now - (86400 * (int) $config['max_autologin_time']) + 60))
indvdum@0
   400
						{
indvdum@0
   401
							$session_expired = true;
indvdum@0
   402
						}
indvdum@0
   403
					}
indvdum@0
   404
indvdum@0
   405
					if (!$session_expired)
indvdum@0
   406
					{
indvdum@0
   407
						// Only update session DB a minute or so after last update or if page changes
indvdum@0
   408
						if ($this->time_now - $this->data['session_time'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page']))
indvdum@0
   409
						{
indvdum@0
   410
							$sql_ary = array('session_time' => $this->time_now);
indvdum@0
   411
indvdum@0
   412
							if ($this->update_session_page)
indvdum@0
   413
							{
indvdum@0
   414
								$sql_ary['session_page'] = substr($this->page['page'], 0, 199);
indvdum@0
   415
								$sql_ary['session_forum_id'] = $this->page['forum'];
indvdum@0
   416
							}
indvdum@0
   417
indvdum@0
   418
							$db->sql_return_on_error(true);
indvdum@0
   419
indvdum@0
   420
							$sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
indvdum@0
   421
								WHERE session_id = '" . $db->sql_escape($this->session_id) . "'";
indvdum@0
   422
							$result = $db->sql_query($sql);
indvdum@0
   423
indvdum@0
   424
							$db->sql_return_on_error(false);
indvdum@0
   425
indvdum@0
   426
							// If the database is not yet updated, there will be an error due to the session_forum_id
indvdum@0
   427
							// @todo REMOVE for 3.0.2
indvdum@0
   428
							if ($result === false)
indvdum@0
   429
							{
indvdum@0
   430
								unset($sql_ary['session_forum_id']);
indvdum@0
   431
indvdum@0
   432
								$sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
indvdum@0
   433
									WHERE session_id = '" . $db->sql_escape($this->session_id) . "'";
indvdum@0
   434
								$db->sql_query($sql);
indvdum@0
   435
							}
indvdum@0
   436
indvdum@0
   437
							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'])
indvdum@0
   438
							{
indvdum@0
   439
								$this->leave_newly_registered();
indvdum@0
   440
							}
indvdum@0
   441
						}
indvdum@0
   442
indvdum@0
   443
						$this->data['is_registered'] = ($this->data['user_id'] != ANONYMOUS && ($this->data['user_type'] == USER_NORMAL || $this->data['user_type'] == USER_FOUNDER)) ? true : false;
indvdum@0
   444
						$this->data['is_bot'] = (!$this->data['is_registered'] && $this->data['user_id'] != ANONYMOUS) ? true : false;
indvdum@0
   445
						$this->data['user_lang'] = basename($this->data['user_lang']);
indvdum@0
   446
indvdum@0
   447
						return true;
indvdum@0
   448
					}
indvdum@0
   449
				}
indvdum@0
   450
				else
indvdum@0
   451
				{
indvdum@0
   452
					// Added logging temporarly to help debug bugs...
indvdum@0
   453
					if (defined('DEBUG_EXTRA') && $this->data['user_id'] != ANONYMOUS)
indvdum@0
   454
					{
indvdum@0
   455
						if ($referer_valid)
indvdum@0
   456
						{
indvdum@0
   457
							add_log('critical', 'LOG_IP_BROWSER_FORWARDED_CHECK', $u_ip, $s_ip, $u_browser, $s_browser, htmlspecialchars($u_forwarded_for), htmlspecialchars($s_forwarded_for));
indvdum@0
   458
						}
indvdum@0
   459
						else
indvdum@0
   460
						{
indvdum@0
   461
							add_log('critical', 'LOG_REFERER_INVALID', $this->referer);
indvdum@0
   462
						}
indvdum@0
   463
					}
indvdum@0
   464
				}
indvdum@0
   465
			}
indvdum@0
   466
		}
indvdum@0
   467
indvdum@0
   468
		// If we reach here then no (valid) session exists. So we'll create a new one
indvdum@0
   469
		return $this->session_create();
indvdum@0
   470
	}
indvdum@0
   471
indvdum@0
   472
	/**
indvdum@0
   473
	* Create a new session
indvdum@0
   474
	*
indvdum@0
   475
	* If upon trying to start a session we discover there is nothing existing we
indvdum@0
   476
	* jump here. Additionally this method is called directly during login to regenerate
indvdum@0
   477
	* the session for the specific user. In this method we carry out a number of tasks;
indvdum@0
   478
	* garbage collection, (search)bot checking, banned user comparison. Basically
indvdum@0
   479
	* though this method will result in a new session for a specific user.
indvdum@0
   480
	*/
indvdum@0
   481
	function session_create($user_id = false, $set_admin = false, $persist_login = false, $viewonline = true)
indvdum@0
   482
	{
indvdum@0
   483
		global $SID, $_SID, $db, $config, $cache, $phpbb_root_path, $phpEx;
indvdum@0
   484
indvdum@0
   485
		$this->data = array();
indvdum@0
   486
indvdum@0
   487
		/* Garbage collection ... remove old sessions updating user information
indvdum@0
   488
		// if necessary. It means (potentially) 11 queries but only infrequently
indvdum@0
   489
		if ($this->time_now > $config['session_last_gc'] + $config['session_gc'])
indvdum@0
   490
		{
indvdum@0
   491
			$this->session_gc();
indvdum@0
   492
		}*/
indvdum@0
   493
indvdum@0
   494
		// Do we allow autologin on this board? No? Then override anything
indvdum@0
   495
		// that may be requested here
indvdum@0
   496
		if (!$config['allow_autologin'])
indvdum@0
   497
		{
indvdum@0
   498
			$this->cookie_data['k'] = $persist_login = false;
indvdum@0
   499
		}
indvdum@0
   500
indvdum@0
   501
		/**
indvdum@0
   502
		* Here we do a bot check, oh er saucy! No, not that kind of bot
indvdum@0
   503
		* check. We loop through the list of bots defined by the admin and
indvdum@0
   504
		* see if we have any useragent and/or IP matches. If we do, this is a
indvdum@0
   505
		* bot, act accordingly
indvdum@0
   506
		*/
indvdum@0
   507
		$bot = false;
indvdum@0
   508
		$active_bots = $cache->obtain_bots();
indvdum@0
   509
indvdum@0
   510
		foreach ($active_bots as $row)
indvdum@0
   511
		{
indvdum@0
   512
			if ($row['bot_agent'] && preg_match('#' . str_replace('\*', '.*?', preg_quote($row['bot_agent'], '#')) . '#i', $this->browser))
indvdum@0
   513
			{
indvdum@0
   514
				$bot = $row['user_id'];
indvdum@0
   515
			}
indvdum@0
   516
indvdum@0
   517
			// If ip is supplied, we will make sure the ip is matching too...
indvdum@0
   518
			if ($row['bot_ip'] && ($bot || !$row['bot_agent']))
indvdum@0
   519
			{
indvdum@0
   520
				// Set bot to false, then we only have to set it to true if it is matching
indvdum@0
   521
				$bot = false;
indvdum@0
   522
indvdum@0
   523
				foreach (explode(',', $row['bot_ip']) as $bot_ip)
indvdum@0
   524
				{
indvdum@0
   525
					$bot_ip = trim($bot_ip);
indvdum@0
   526
indvdum@0
   527
					if (!$bot_ip)
indvdum@0
   528
					{
indvdum@0
   529
						continue;
indvdum@0
   530
					}
indvdum@0
   531
indvdum@0
   532
					if (strpos($this->ip, $bot_ip) === 0)
indvdum@0
   533
					{
indvdum@0
   534
						$bot = (int) $row['user_id'];
indvdum@0
   535
						break;
indvdum@0
   536
					}
indvdum@0
   537
				}
indvdum@0
   538
			}
indvdum@0
   539
indvdum@0
   540
			if ($bot)
indvdum@0
   541
			{
indvdum@0
   542
				break;
indvdum@0
   543
			}
indvdum@0
   544
		}
indvdum@0
   545
indvdum@0
   546
		$method = basename(trim($config['auth_method']));
indvdum@0
   547
		include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
indvdum@0
   548
indvdum@0
   549
		$method = 'autologin_' . $method;
indvdum@0
   550
		if (function_exists($method))
indvdum@0
   551
		{
indvdum@0
   552
			$this->data = $method();
indvdum@0
   553
indvdum@0
   554
			if (sizeof($this->data))
indvdum@0
   555
			{
indvdum@0
   556
				$this->cookie_data['k'] = '';
indvdum@0
   557
				$this->cookie_data['u'] = $this->data['user_id'];
indvdum@0
   558
			}
indvdum@0
   559
		}
indvdum@0
   560
indvdum@0
   561
		// If we're presented with an autologin key we'll join against it.
indvdum@0
   562
		// Else if we've been passed a user_id we'll grab data based on that
indvdum@0
   563
		if (isset($this->cookie_data['k']) && $this->cookie_data['k'] && $this->cookie_data['u'] && !sizeof($this->data))
indvdum@0
   564
		{
indvdum@0
   565
			$sql = 'SELECT u.*
indvdum@0
   566
				FROM ' . USERS_TABLE . ' u, ' . SESSIONS_KEYS_TABLE . ' k
indvdum@0
   567
				WHERE u.user_id = ' . (int) $this->cookie_data['u'] . '
indvdum@0
   568
					AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ")
indvdum@0
   569
					AND k.user_id = u.user_id
indvdum@0
   570
					AND k.key_id = '" . $db->sql_escape(md5($this->cookie_data['k'])) . "'";
indvdum@0
   571
			$result = $db->sql_query($sql);
indvdum@0
   572
			$this->data = $db->sql_fetchrow($result);
indvdum@0
   573
			$db->sql_freeresult($result);
indvdum@0
   574
			$bot = false;
indvdum@0
   575
		}
indvdum@0
   576
		else if ($user_id !== false && !sizeof($this->data))
indvdum@0
   577
		{
indvdum@0
   578
			$this->cookie_data['k'] = '';
indvdum@0
   579
			$this->cookie_data['u'] = $user_id;
indvdum@0
   580
indvdum@0
   581
			$sql = 'SELECT *
indvdum@0
   582
				FROM ' . USERS_TABLE . '
indvdum@0
   583
				WHERE user_id = ' . (int) $this->cookie_data['u'] . '
indvdum@0
   584
					AND user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')';
indvdum@0
   585
			$result = $db->sql_query($sql);
indvdum@0
   586
			$this->data = $db->sql_fetchrow($result);
indvdum@0
   587
			$db->sql_freeresult($result);
indvdum@0
   588
			$bot = false;
indvdum@0
   589
		}
indvdum@0
   590
indvdum@0
   591
		// Bot user, if they have a SID in the Request URI we need to get rid of it
indvdum@0
   592
		// otherwise they'll index this page with the SID, duplicate content oh my!
indvdum@0
   593
		if ($bot && isset($_GET['sid']))
indvdum@0
   594
		{
indvdum@0
   595
			send_status_line(301, 'Moved Permanently');
indvdum@0
   596
			redirect(build_url(array('sid')));
indvdum@0
   597
		}
indvdum@0
   598
indvdum@0
   599
		// If no data was returned one or more of the following occurred:
indvdum@0
   600
		// Key didn't match one in the DB
indvdum@0
   601
		// User does not exist
indvdum@0
   602
		// User is inactive
indvdum@0
   603
		// User is bot
indvdum@0
   604
		if (!sizeof($this->data) || !is_array($this->data))
indvdum@0
   605
		{
indvdum@0
   606
			$this->cookie_data['k'] = '';
indvdum@0
   607
			$this->cookie_data['u'] = ($bot) ? $bot : ANONYMOUS;
indvdum@0
   608
indvdum@0
   609
			if (!$bot)
indvdum@0
   610
			{
indvdum@0
   611
				$sql = 'SELECT *
indvdum@0
   612
					FROM ' . USERS_TABLE . '
indvdum@0
   613
					WHERE user_id = ' . (int) $this->cookie_data['u'];
indvdum@0
   614
			}
indvdum@0
   615
			else
indvdum@0
   616
			{
indvdum@0
   617
				// We give bots always the same session if it is not yet expired.
indvdum@0
   618
				$sql = 'SELECT u.*, s.*
indvdum@0
   619
					FROM ' . USERS_TABLE . ' u
indvdum@0
   620
					LEFT JOIN ' . SESSIONS_TABLE . ' s ON (s.session_user_id = u.user_id)
indvdum@0
   621
					WHERE u.user_id = ' . (int) $bot;
indvdum@0
   622
			}
indvdum@0
   623
indvdum@0
   624
			$result = $db->sql_query($sql);
indvdum@0
   625
			$this->data = $db->sql_fetchrow($result);
indvdum@0
   626
			$db->sql_freeresult($result);
indvdum@0
   627
		}
indvdum@0
   628
indvdum@0
   629
		if ($this->data['user_id'] != ANONYMOUS && !$bot)
indvdum@0
   630
		{
indvdum@0
   631
			$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());
indvdum@0
   632
		}
indvdum@0
   633
		else
indvdum@0
   634
		{
indvdum@0
   635
			$this->data['session_last_visit'] = $this->time_now;
indvdum@0
   636
		}
indvdum@0
   637
indvdum@0
   638
		// Force user id to be integer...
indvdum@0
   639
		$this->data['user_id'] = (int) $this->data['user_id'];
indvdum@0
   640
indvdum@0
   641
		// At this stage we should have a filled data array, defined cookie u and k data.
indvdum@0
   642
		// data array should contain recent session info if we're a real user and a recent
indvdum@0
   643
		// session exists in which case session_id will also be set
indvdum@0
   644
indvdum@0
   645
		// Is user banned? Are they excluded? Won't return on ban, exists within method
indvdum@0
   646
		if ($this->data['user_type'] != USER_FOUNDER)
indvdum@0
   647
		{
indvdum@0
   648
			if (!$config['forwarded_for_check'])
indvdum@0
   649
			{
indvdum@0
   650
				$this->check_ban($this->data['user_id'], $this->ip);
indvdum@0
   651
			}
indvdum@0
   652
			else
indvdum@0
   653
			{
indvdum@0
   654
				$ips = explode(' ', $this->forwarded_for);
indvdum@0
   655
				$ips[] = $this->ip;
indvdum@0
   656
				$this->check_ban($this->data['user_id'], $ips);
indvdum@0
   657
			}
indvdum@0
   658
		}
indvdum@0
   659
indvdum@0
   660
		$this->data['is_registered'] = (!$bot && $this->data['user_id'] != ANONYMOUS && ($this->data['user_type'] == USER_NORMAL || $this->data['user_type'] == USER_FOUNDER)) ? true : false;
indvdum@0
   661
		$this->data['is_bot'] = ($bot) ? true : false;
indvdum@0
   662
indvdum@0
   663
		// If our friend is a bot, we re-assign a previously assigned session
indvdum@0
   664
		if ($this->data['is_bot'] && $bot == $this->data['user_id'] && $this->data['session_id'])
indvdum@0
   665
		{
indvdum@0
   666
			// Only assign the current session if the ip, browser and forwarded_for match...
indvdum@0
   667
			if (strpos($this->ip, ':') !== false && strpos($this->data['session_ip'], ':') !== false)
indvdum@0
   668
			{
indvdum@0
   669
				$s_ip = short_ipv6($this->data['session_ip'], $config['ip_check']);
indvdum@0
   670
				$u_ip = short_ipv6($this->ip, $config['ip_check']);
indvdum@0
   671
			}
indvdum@0
   672
			else
indvdum@0
   673
			{
indvdum@0
   674
				$s_ip = implode('.', array_slice(explode('.', $this->data['session_ip']), 0, $config['ip_check']));
indvdum@0
   675
				$u_ip = implode('.', array_slice(explode('.', $this->ip), 0, $config['ip_check']));
indvdum@0
   676
			}
indvdum@0
   677
indvdum@0
   678
			$s_browser = ($config['browser_check']) ? trim(strtolower(substr($this->data['session_browser'], 0, 149))) : '';
indvdum@0
   679
			$u_browser = ($config['browser_check']) ? trim(strtolower(substr($this->browser, 0, 149))) : '';
indvdum@0
   680
indvdum@0
   681
			$s_forwarded_for = ($config['forwarded_for_check']) ? substr($this->data['session_forwarded_for'], 0, 254) : '';
indvdum@0
   682
			$u_forwarded_for = ($config['forwarded_for_check']) ? substr($this->forwarded_for, 0, 254) : '';
indvdum@0
   683
indvdum@0
   684
			if ($u_ip === $s_ip && $s_browser === $u_browser && $s_forwarded_for === $u_forwarded_for)
indvdum@0
   685
			{
indvdum@0
   686
				$this->session_id = $this->data['session_id'];
indvdum@0
   687
indvdum@0
   688
				// Only update session DB a minute or so after last update or if page changes
indvdum@0
   689
				if ($this->time_now - $this->data['session_time'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page']))
indvdum@0
   690
				{
indvdum@0
   691
					$this->data['session_time'] = $this->data['session_last_visit'] = $this->time_now;
indvdum@0
   692
indvdum@0
   693
					$sql_ary = array('session_time' => $this->time_now, 'session_last_visit' => $this->time_now, 'session_admin' => 0);
indvdum@0
   694
indvdum@0
   695
					if ($this->update_session_page)
indvdum@0
   696
					{
indvdum@0
   697
						$sql_ary['session_page'] = substr($this->page['page'], 0, 199);
indvdum@0
   698
						$sql_ary['session_forum_id'] = $this->page['forum'];
indvdum@0
   699
					}
indvdum@0
   700
indvdum@0
   701
					$sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
indvdum@0
   702
						WHERE session_id = '" . $db->sql_escape($this->session_id) . "'";
indvdum@0
   703
					$db->sql_query($sql);
indvdum@0
   704
indvdum@0
   705
					// Update the last visit time
indvdum@0
   706
					$sql = 'UPDATE ' . USERS_TABLE . '
indvdum@0
   707
						SET user_lastvisit = ' . (int) $this->data['session_time'] . '
indvdum@0
   708
						WHERE user_id = ' . (int) $this->data['user_id'];
indvdum@0
   709
					$db->sql_query($sql);
indvdum@0
   710
				}
indvdum@0
   711
indvdum@0
   712
				$SID = '?sid=';
indvdum@0
   713
				$_SID = '';
indvdum@0
   714
				return true;
indvdum@0
   715
			}
indvdum@0
   716
			else
indvdum@0
   717
			{
indvdum@0
   718
				// If the ip and browser does not match make sure we only have one bot assigned to one session
indvdum@0
   719
				$db->sql_query('DELETE FROM ' . SESSIONS_TABLE . ' WHERE session_user_id = ' . $this->data['user_id']);
indvdum@0
   720
			}
indvdum@0
   721
		}
indvdum@0
   722
indvdum@0
   723
		$session_autologin = (($this->cookie_data['k'] || $persist_login) && $this->data['is_registered']) ? true : false;
indvdum@0
   724
		$set_admin = ($set_admin && $this->data['is_registered']) ? true : false;
indvdum@0
   725
indvdum@0
   726
		// Create or update the session
indvdum@0
   727
		$sql_ary = array(
indvdum@0
   728
			'session_user_id'		=> (int) $this->data['user_id'],
indvdum@0
   729
			'session_start'			=> (int) $this->time_now,
indvdum@0
   730
			'session_last_visit'	=> (int) $this->data['session_last_visit'],
indvdum@0
   731
			'session_time'			=> (int) $this->time_now,
indvdum@0
   732
			'session_browser'		=> (string) trim(substr($this->browser, 0, 149)),
indvdum@0
   733
			'session_forwarded_for'	=> (string) $this->forwarded_for,
indvdum@0
   734
			'session_ip'			=> (string) $this->ip,
indvdum@0
   735
			'session_autologin'		=> ($session_autologin) ? 1 : 0,
indvdum@0
   736
			'session_admin'			=> ($set_admin) ? 1 : 0,
indvdum@0
   737
			'session_viewonline'	=> ($viewonline) ? 1 : 0,
indvdum@0
   738
		);
indvdum@0
   739
indvdum@0
   740
		if ($this->update_session_page)
indvdum@0
   741
		{
indvdum@0
   742
			$sql_ary['session_page'] = (string) substr($this->page['page'], 0, 199);
indvdum@0
   743
			$sql_ary['session_forum_id'] = $this->page['forum'];
indvdum@0
   744
		}
indvdum@0
   745
indvdum@0
   746
		$db->sql_return_on_error(true);
indvdum@0
   747
indvdum@0
   748
		$sql = 'DELETE
indvdum@0
   749
			FROM ' . SESSIONS_TABLE . '
indvdum@0
   750
			WHERE session_id = \'' . $db->sql_escape($this->session_id) . '\'
indvdum@0
   751
				AND session_user_id = ' . ANONYMOUS;
indvdum@0
   752
indvdum@0
   753
		if (!defined('IN_ERROR_HANDLER') && (!$this->session_id || !$db->sql_query($sql) || !$db->sql_affectedrows()))
indvdum@0
   754
		{
indvdum@0
   755
			// Limit new sessions in 1 minute period (if required)
indvdum@0
   756
			if (empty($this->data['session_time']) && $config['active_sessions'])
indvdum@0
   757
			{
indvdum@0
   758
//				$db->sql_return_on_error(false);
indvdum@0
   759
indvdum@0
   760
				$sql = 'SELECT COUNT(session_id) AS sessions
indvdum@0
   761
					FROM ' . SESSIONS_TABLE . '
indvdum@0
   762
					WHERE session_time >= ' . ($this->time_now - 60);
indvdum@0
   763
				$result = $db->sql_query($sql);
indvdum@0
   764
				$row = $db->sql_fetchrow($result);
indvdum@0
   765
				$db->sql_freeresult($result);
indvdum@0
   766
indvdum@0
   767
				if ((int) $row['sessions'] > (int) $config['active_sessions'])
indvdum@0
   768
				{
indvdum@0
   769
					send_status_line(503, 'Service Unavailable');
indvdum@0
   770
					trigger_error('BOARD_UNAVAILABLE');
indvdum@0
   771
				}
indvdum@0
   772
			}
indvdum@0
   773
		}
indvdum@0
   774
indvdum@0
   775
		// Since we re-create the session id here, the inserted row must be unique. Therefore, we display potential errors.
indvdum@0
   776
		// Commented out because it will not allow forums to update correctly
indvdum@0
   777
//		$db->sql_return_on_error(false);
indvdum@0
   778
indvdum@0
   779
		// Something quite important: session_page always holds the *last* page visited, except for the *first* visit.
indvdum@0
   780
		// We are not able to simply have an empty session_page btw, therefore we need to tell phpBB how to detect this special case.
indvdum@0
   781
		// 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.
indvdum@0
   782
		if (empty($this->data['session_id']))
indvdum@0
   783
		{
indvdum@0
   784
			// This is a temporary variable, only set for the very first visit
indvdum@0
   785
			$this->data['session_created'] = true;
indvdum@0
   786
		}
indvdum@0
   787
indvdum@0
   788
		$this->session_id = $this->data['session_id'] = md5(unique_id());
indvdum@0
   789
indvdum@0
   790
		$sql_ary['session_id'] = (string) $this->session_id;
indvdum@0
   791
		$sql_ary['session_page'] = (string) substr($this->page['page'], 0, 199);
indvdum@0
   792
		$sql_ary['session_forum_id'] = $this->page['forum'];
indvdum@0
   793
indvdum@0
   794
		$sql = 'INSERT INTO ' . SESSIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
indvdum@0
   795
		$db->sql_query($sql);
indvdum@0
   796
indvdum@0
   797
		$db->sql_return_on_error(false);
indvdum@0
   798
indvdum@0
   799
		// Regenerate autologin/persistent login key
indvdum@0
   800
		if ($session_autologin)
indvdum@0
   801
		{
indvdum@0
   802
			$this->set_login_key();
indvdum@0
   803
		}
indvdum@0
   804
indvdum@0
   805
		// refresh data
indvdum@0
   806
		$SID = '?sid=' . $this->session_id;
indvdum@0
   807
		$_SID = $this->session_id;
indvdum@0
   808
		$this->data = array_merge($this->data, $sql_ary);
indvdum@0
   809
indvdum@0
   810
		if (!$bot)
indvdum@0
   811
		{
indvdum@0
   812
			$cookie_expire = $this->time_now + (($config['max_autologin_time']) ? 86400 * (int) $config['max_autologin_time'] : 31536000);
indvdum@0
   813
indvdum@0
   814
			$this->set_cookie('u', $this->cookie_data['u'], $cookie_expire);
indvdum@0
   815
			$this->set_cookie('k', $this->cookie_data['k'], $cookie_expire);
indvdum@0
   816
			$this->set_cookie('sid', $this->session_id, $cookie_expire);
indvdum@0
   817
indvdum@0
   818
			unset($cookie_expire);
indvdum@0
   819
indvdum@0
   820
			$sql = 'SELECT COUNT(session_id) AS sessions
indvdum@0
   821
					FROM ' . SESSIONS_TABLE . '
indvdum@0
   822
					WHERE session_user_id = ' . (int) $this->data['user_id'] . '
indvdum@0
   823
					AND session_time >= ' . (int) ($this->time_now - (max($config['session_length'], $config['form_token_lifetime'])));
indvdum@0
   824
			$result = $db->sql_query($sql);
indvdum@0
   825
			$row = $db->sql_fetchrow($result);
indvdum@0
   826
			$db->sql_freeresult($result);
indvdum@0
   827
indvdum@0
   828
			if ((int) $row['sessions'] <= 1 || empty($this->data['user_form_salt']))
indvdum@0
   829
			{
indvdum@0
   830
				$this->data['user_form_salt'] = unique_id();
indvdum@0
   831
				// Update the form key
indvdum@0
   832
				$sql = 'UPDATE ' . USERS_TABLE . '
indvdum@0
   833
					SET user_form_salt = \'' . $db->sql_escape($this->data['user_form_salt']) . '\'
indvdum@0
   834
					WHERE user_id = ' . (int) $this->data['user_id'];
indvdum@0
   835
				$db->sql_query($sql);
indvdum@0
   836
			}
indvdum@0
   837
		}
indvdum@0
   838
		else
indvdum@0
   839
		{
indvdum@0
   840
			$this->data['session_time'] = $this->data['session_last_visit'] = $this->time_now;
indvdum@0
   841
indvdum@0
   842
			// Update the last visit time
indvdum@0
   843
			$sql = 'UPDATE ' . USERS_TABLE . '
indvdum@0
   844
				SET user_lastvisit = ' . (int) $this->data['session_time'] . '
indvdum@0
   845
				WHERE user_id = ' . (int) $this->data['user_id'];
indvdum@0
   846
			$db->sql_query($sql);
indvdum@0
   847
indvdum@0
   848
			$SID = '?sid=';
indvdum@0
   849
			$_SID = '';
indvdum@0
   850
		}
indvdum@0
   851
indvdum@0
   852
		return true;
indvdum@0
   853
	}
indvdum@0
   854
indvdum@0
   855
	/**
indvdum@0
   856
	* Kills a session
indvdum@0
   857
	*
indvdum@0
   858
	* This method does what it says on the tin. It will delete a pre-existing session.
indvdum@0
   859
	* It resets cookie information (destroying any autologin key within that cookie data)
indvdum@0
   860
	* and update the users information from the relevant session data. It will then
indvdum@0
   861
	* grab guest user information.
indvdum@0
   862
	*/
indvdum@0
   863
	function session_kill($new_session = true)
indvdum@0
   864
	{
indvdum@0
   865
		global $SID, $_SID, $db, $config, $phpbb_root_path, $phpEx;
indvdum@0
   866
indvdum@0
   867
		$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
indvdum@0
   868
			WHERE session_id = '" . $db->sql_escape($this->session_id) . "'
indvdum@0
   869
				AND session_user_id = " . (int) $this->data['user_id'];
indvdum@0
   870
		$db->sql_query($sql);
indvdum@0
   871
indvdum@0
   872
		// Allow connecting logout with external auth method logout
indvdum@0
   873
		$method = basename(trim($config['auth_method']));
indvdum@0
   874
		include_once($phpbb_root_path . 'includes/auth/auth_' . $method . '.' . $phpEx);
indvdum@0
   875
indvdum@0
   876
		$method = 'logout_' . $method;
indvdum@0
   877
		if (function_exists($method))
indvdum@0
   878
		{
indvdum@0
   879
			$method($this->data, $new_session);
indvdum@0
   880
		}
indvdum@0
   881
indvdum@0
   882
		if ($this->data['user_id'] != ANONYMOUS)
indvdum@0
   883
		{
indvdum@0
   884
			// Delete existing session, update last visit info first!
indvdum@0
   885
			if (!isset($this->data['session_time']))
indvdum@0
   886
			{
indvdum@0
   887
				$this->data['session_time'] = time();
indvdum@0
   888
			}
indvdum@0
   889
indvdum@0
   890
			$sql = 'UPDATE ' . USERS_TABLE . '
indvdum@0
   891
				SET user_lastvisit = ' . (int) $this->data['session_time'] . '
indvdum@0
   892
				WHERE user_id = ' . (int) $this->data['user_id'];
indvdum@0
   893
			$db->sql_query($sql);
indvdum@0
   894
indvdum@0
   895
			if ($this->cookie_data['k'])
indvdum@0
   896
			{
indvdum@0
   897
				$sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
indvdum@0
   898
					WHERE user_id = ' . (int) $this->data['user_id'] . "
indvdum@0
   899
						AND key_id = '" . $db->sql_escape(md5($this->cookie_data['k'])) . "'";
indvdum@0
   900
				$db->sql_query($sql);
indvdum@0
   901
			}
indvdum@0
   902
indvdum@0
   903
			// Reset the data array
indvdum@0
   904
			$this->data = array();
indvdum@0
   905
indvdum@0
   906
			$sql = 'SELECT *
indvdum@0
   907
				FROM ' . USERS_TABLE . '
indvdum@0
   908
				WHERE user_id = ' . ANONYMOUS;
indvdum@0
   909
			$result = $db->sql_query($sql);
indvdum@0
   910
			$this->data = $db->sql_fetchrow($result);
indvdum@0
   911
			$db->sql_freeresult($result);
indvdum@0
   912
		}
indvdum@0
   913
indvdum@0
   914
		$cookie_expire = $this->time_now - 31536000;
indvdum@0
   915
		$this->set_cookie('u', '', $cookie_expire);
indvdum@0
   916
		$this->set_cookie('k', '', $cookie_expire);
indvdum@0
   917
		$this->set_cookie('sid', '', $cookie_expire);
indvdum@0
   918
		unset($cookie_expire);
indvdum@0
   919
indvdum@0
   920
		$SID = '?sid=';
indvdum@0
   921
		$this->session_id = $_SID = '';
indvdum@0
   922
indvdum@0
   923
		// To make sure a valid session is created we create one for the anonymous user
indvdum@0
   924
		if ($new_session)
indvdum@0
   925
		{
indvdum@0
   926
			$this->session_create(ANONYMOUS);
indvdum@0
   927
		}
indvdum@0
   928
indvdum@0
   929
		return true;
indvdum@0
   930
	}
indvdum@0
   931
indvdum@0
   932
	/**
indvdum@0
   933
	* Session garbage collection
indvdum@0
   934
	*
indvdum@0
   935
	* This looks a lot more complex than it really is. Effectively we are
indvdum@0
   936
	* deleting any sessions older than an admin definable limit. Due to the
indvdum@0
   937
	* way in which we maintain session data we have to ensure we update user
indvdum@0
   938
	* data before those sessions are destroyed. In addition this method
indvdum@0
   939
	* removes autologin key information that is older than an admin defined
indvdum@0
   940
	* limit.
indvdum@0
   941
	*/
indvdum@0
   942
	function session_gc()
indvdum@0
   943
	{
indvdum@0
   944
		global $db, $config, $phpbb_root_path, $phpEx;
indvdum@0
   945
indvdum@0
   946
		$batch_size = 10;
indvdum@0
   947
indvdum@0
   948
		if (!$this->time_now)
indvdum@0
   949
		{
indvdum@0
   950
			$this->time_now = time();
indvdum@0
   951
		}
indvdum@0
   952
indvdum@0
   953
		// Firstly, delete guest sessions
indvdum@0
   954
		$sql = 'DELETE FROM ' . SESSIONS_TABLE . '
indvdum@0
   955
			WHERE session_user_id = ' . ANONYMOUS . '
indvdum@0
   956
				AND session_time < ' . (int) ($this->time_now - $config['session_length']);
indvdum@0
   957
		$db->sql_query($sql);
indvdum@0
   958
indvdum@0
   959
		// Get expired sessions, only most recent for each user
indvdum@0
   960
		$sql = 'SELECT session_user_id, session_page, MAX(session_time) AS recent_time
indvdum@0
   961
			FROM ' . SESSIONS_TABLE . '
indvdum@0
   962
			WHERE session_time < ' . ($this->time_now - $config['session_length']) . '
indvdum@0
   963
			GROUP BY session_user_id, session_page';
indvdum@0
   964
		$result = $db->sql_query_limit($sql, $batch_size);
indvdum@0
   965
indvdum@0
   966
		$del_user_id = array();
indvdum@0
   967
		$del_sessions = 0;
indvdum@0
   968
indvdum@0
   969
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   970
		{
indvdum@0
   971
			$sql = 'UPDATE ' . USERS_TABLE . '
indvdum@0
   972
				SET user_lastvisit = ' . (int) $row['recent_time'] . ", user_lastpage = '" . $db->sql_escape($row['session_page']) . "'
indvdum@0
   973
				WHERE user_id = " . (int) $row['session_user_id'];
indvdum@0
   974
			$db->sql_query($sql);
indvdum@0
   975
indvdum@0
   976
			$del_user_id[] = (int) $row['session_user_id'];
indvdum@0
   977
			$del_sessions++;
indvdum@0
   978
		}
indvdum@0
   979
		$db->sql_freeresult($result);
indvdum@0
   980
indvdum@0
   981
		if (sizeof($del_user_id))
indvdum@0
   982
		{
indvdum@0
   983
			// Delete expired sessions
indvdum@0
   984
			$sql = 'DELETE FROM ' . SESSIONS_TABLE . '
indvdum@0
   985
				WHERE ' . $db->sql_in_set('session_user_id', $del_user_id) . '
indvdum@0
   986
					AND session_time < ' . ($this->time_now - $config['session_length']);
indvdum@0
   987
			$db->sql_query($sql);
indvdum@0
   988
		}
indvdum@0
   989
indvdum@0
   990
		if ($del_sessions < $batch_size)
indvdum@0
   991
		{
indvdum@0
   992
			// Less than 10 users, update gc timer ... else we want gc
indvdum@0
   993
			// called again to delete other sessions
indvdum@0
   994
			set_config('session_last_gc', $this->time_now, true);
indvdum@0
   995
indvdum@0
   996
			if ($config['max_autologin_time'])
indvdum@0
   997
			{
indvdum@0
   998
				$sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
indvdum@0
   999
					WHERE last_login < ' . (time() - (86400 * (int) $config['max_autologin_time']));
indvdum@0
  1000
				$db->sql_query($sql);
indvdum@0
  1001
			}
indvdum@0
  1002
indvdum@0
  1003
			// only called from CRON; should be a safe workaround until the infrastructure gets going
indvdum@0
  1004
			if (!class_exists('phpbb_captcha_factory'))
indvdum@0
  1005
			{
indvdum@0
  1006
				include($phpbb_root_path . "includes/captcha/captcha_factory." . $phpEx);
indvdum@0
  1007
			}
indvdum@0
  1008
			phpbb_captcha_factory::garbage_collect($config['captcha_plugin']);
indvdum@0
  1009
indvdum@0
  1010
			$sql = 'DELETE FROM ' . LOGIN_ATTEMPT_TABLE . '
indvdum@0
  1011
				WHERE attempt_time < ' . (time() - (int) $config['ip_login_limit_time']);
indvdum@0
  1012
			$db->sql_query($sql);
indvdum@0
  1013
		}
indvdum@0
  1014
indvdum@0
  1015
		return;
indvdum@0
  1016
	}
indvdum@0
  1017
indvdum@0
  1018
	/**
indvdum@0
  1019
	* Sets a cookie
indvdum@0
  1020
	*
indvdum@0
  1021
	* 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.
indvdum@0
  1022
	*
indvdum@0
  1023
	* @param string $name		Name of the cookie, will be automatically prefixed with the phpBB cookie name. track becomes [cookie_name]_track then.
indvdum@0
  1024
	* @param string $cookiedata	The data to hold within the cookie
indvdum@0
  1025
	* @param int $cookietime	The expiration time as UNIX timestamp. If 0 is provided, a session cookie is set.
indvdum@0
  1026
	*/
indvdum@0
  1027
	function set_cookie($name, $cookiedata, $cookietime)
indvdum@0
  1028
	{
indvdum@0
  1029
		global $config;
indvdum@0
  1030
indvdum@0
  1031
		$name_data = rawurlencode($config['cookie_name'] . '_' . $name) . '=' . rawurlencode($cookiedata);
indvdum@0
  1032
		$expire = gmdate('D, d-M-Y H:i:s \\G\\M\\T', $cookietime);
indvdum@0
  1033
		$domain = (!$config['cookie_domain'] || $config['cookie_domain'] == 'localhost' || $config['cookie_domain'] == '127.0.0.1') ? '' : '; domain=' . $config['cookie_domain'];
indvdum@0
  1034
indvdum@0
  1035
		header('Set-Cookie: ' . $name_data . (($cookietime) ? '; expires=' . $expire : '') . '; path=' . $config['cookie_path'] . $domain . ((!$config['cookie_secure']) ? '' : '; secure') . '; HttpOnly', false);
indvdum@0
  1036
	}
indvdum@0
  1037
indvdum@0
  1038
	/**
indvdum@0
  1039
	* Check for banned user
indvdum@0
  1040
	*
indvdum@0
  1041
	* Checks whether the supplied user is banned by id, ip or email. If no parameters
indvdum@0
  1042
	* are passed to the method pre-existing session data is used. If $return is false
indvdum@0
  1043
	* this routine does not return on finding a banned user, it outputs a relevant
indvdum@0
  1044
	* message and stops execution.
indvdum@0
  1045
	*
indvdum@0
  1046
	* @param string|array	$user_ips	Can contain a string with one IP or an array of multiple IPs
indvdum@0
  1047
	*/
indvdum@0
  1048
	function check_ban($user_id = false, $user_ips = false, $user_email = false, $return = false)
indvdum@0
  1049
	{
indvdum@0
  1050
		global $config, $db;
indvdum@0
  1051
indvdum@0
  1052
		if (defined('IN_CHECK_BAN'))
indvdum@0
  1053
		{
indvdum@0
  1054
			return;
indvdum@0
  1055
		}
indvdum@0
  1056
indvdum@0
  1057
		$banned = false;
indvdum@0
  1058
		$cache_ttl = 3600;
indvdum@0
  1059
		$where_sql = array();
indvdum@0
  1060
indvdum@0
  1061
		$sql = 'SELECT ban_ip, ban_userid, ban_email, ban_exclude, ban_give_reason, ban_end
indvdum@0
  1062
			FROM ' . BANLIST_TABLE . '
indvdum@0
  1063
			WHERE ';
indvdum@0
  1064
indvdum@0
  1065
		// Determine which entries to check, only return those
indvdum@0
  1066
		if ($user_email === false)
indvdum@0
  1067
		{
indvdum@0
  1068
			$where_sql[] = "ban_email = ''";
indvdum@0
  1069
		}
indvdum@0
  1070
indvdum@0
  1071
		if ($user_ips === false)
indvdum@0
  1072
		{
indvdum@0
  1073
			$where_sql[] = "(ban_ip = '' OR ban_exclude = 1)";
indvdum@0
  1074
		}
indvdum@0
  1075
indvdum@0
  1076
		if ($user_id === false)
indvdum@0
  1077
		{
indvdum@0
  1078
			$where_sql[] = '(ban_userid = 0 OR ban_exclude = 1)';
indvdum@0
  1079
		}
indvdum@0
  1080
		else
indvdum@0
  1081
		{
indvdum@0
  1082
			$cache_ttl = ($user_id == ANONYMOUS) ? 3600 : 0;
indvdum@0
  1083
			$_sql = '(ban_userid = ' . $user_id;
indvdum@0
  1084
indvdum@0
  1085
			if ($user_email !== false)
indvdum@0
  1086
			{
indvdum@0
  1087
				$_sql .= " OR ban_email <> ''";
indvdum@0
  1088
			}
indvdum@0
  1089
indvdum@0
  1090
			if ($user_ips !== false)
indvdum@0
  1091
			{
indvdum@0
  1092
				$_sql .= " OR ban_ip <> ''";
indvdum@0
  1093
			}
indvdum@0
  1094
indvdum@0
  1095
			$_sql .= ')';
indvdum@0
  1096
indvdum@0
  1097
			$where_sql[] = $_sql;
indvdum@0
  1098
		}
indvdum@0
  1099
indvdum@0
  1100
		$sql .= (sizeof($where_sql)) ? implode(' AND ', $where_sql) : '';
indvdum@0
  1101
		$result = $db->sql_query($sql, $cache_ttl);
indvdum@0
  1102
indvdum@0
  1103
		$ban_triggered_by = 'user';
indvdum@0
  1104
		while ($row = $db->sql_fetchrow($result))
indvdum@0
  1105
		{
indvdum@0
  1106
			if ($row['ban_end'] && $row['ban_end'] < time())
indvdum@0
  1107
			{
indvdum@0
  1108
				continue;
indvdum@0
  1109
			}
indvdum@0
  1110
indvdum@0
  1111
			$ip_banned = false;
indvdum@0
  1112
			if (!empty($row['ban_ip']))
indvdum@0
  1113
			{
indvdum@0
  1114
				if (!is_array($user_ips))
indvdum@0
  1115
				{
indvdum@0
  1116
					$ip_banned = preg_match('#^' . str_replace('\*', '.*?', preg_quote($row['ban_ip'], '#')) . '$#i', $user_ips);
indvdum@0
  1117
				}
indvdum@0
  1118
				else
indvdum@0
  1119
				{
indvdum@0
  1120
					foreach ($user_ips as $user_ip)
indvdum@0
  1121
					{
indvdum@0
  1122
						if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($row['ban_ip'], '#')) . '$#i', $user_ip))
indvdum@0
  1123
						{
indvdum@0
  1124
							$ip_banned = true;
indvdum@0
  1125
							break;
indvdum@0
  1126
						}
indvdum@0
  1127
					}
indvdum@0
  1128
				}
indvdum@0
  1129
			}
indvdum@0
  1130
indvdum@0
  1131
			if ((!empty($row['ban_userid']) && intval($row['ban_userid']) == $user_id) ||
indvdum@0
  1132
				$ip_banned ||
indvdum@0
  1133
				(!empty($row['ban_email']) && preg_match('#^' . str_replace('\*', '.*?', preg_quote($row['ban_email'], '#')) . '$#i', $user_email)))
indvdum@0
  1134
			{
indvdum@0
  1135
				if (!empty($row['ban_exclude']))
indvdum@0
  1136
				{
indvdum@0
  1137
					$banned = false;
indvdum@0
  1138
					break;
indvdum@0
  1139
				}
indvdum@0
  1140
				else
indvdum@0
  1141
				{
indvdum@0
  1142
					$banned = true;
indvdum@0
  1143
					$ban_row = $row;
indvdum@0
  1144
indvdum@0
  1145
					if (!empty($row['ban_userid']) && intval($row['ban_userid']) == $user_id)
indvdum@0
  1146
					{
indvdum@0
  1147
						$ban_triggered_by = 'user';
indvdum@0
  1148
					}
indvdum@0
  1149
					else if ($ip_banned)
indvdum@0
  1150
					{
indvdum@0
  1151
						$ban_triggered_by = 'ip';
indvdum@0
  1152
					}
indvdum@0
  1153
					else
indvdum@0
  1154
					{
indvdum@0
  1155
						$ban_triggered_by = 'email';
indvdum@0
  1156
					}
indvdum@0
  1157
indvdum@0
  1158
					// Don't break. Check if there is an exclude rule for this user
indvdum@0
  1159
				}
indvdum@0
  1160
			}
indvdum@0
  1161
		}
indvdum@0
  1162
		$db->sql_freeresult($result);
indvdum@0
  1163
indvdum@0
  1164
		if ($banned && !$return)
indvdum@0
  1165
		{
indvdum@0
  1166
			global $template;
indvdum@0
  1167
indvdum@0
  1168
			// If the session is empty we need to create a valid one...
indvdum@0
  1169
			if (empty($this->session_id))
indvdum@0
  1170
			{
indvdum@0
  1171
				// This seems to be no longer needed? - #14971
indvdum@0
  1172
//				$this->session_create(ANONYMOUS);
indvdum@0
  1173
			}
indvdum@0
  1174
indvdum@0
  1175
			// Initiate environment ... since it won't be set at this stage
indvdum@0
  1176
			$this->setup();
indvdum@0
  1177
indvdum@0
  1178
			// Logout the user, banned users are unable to use the normal 'logout' link
indvdum@0
  1179
			if ($this->data['user_id'] != ANONYMOUS)
indvdum@0
  1180
			{
indvdum@0
  1181
				$this->session_kill();
indvdum@0
  1182
			}
indvdum@0
  1183
indvdum@0
  1184
			// We show a login box here to allow founders accessing the board if banned by IP
indvdum@0
  1185
			if (defined('IN_LOGIN') && $this->data['user_id'] == ANONYMOUS)
indvdum@0
  1186
			{
indvdum@0
  1187
				global $phpEx;
indvdum@0
  1188
indvdum@0
  1189
				$this->setup('ucp');
indvdum@0
  1190
				$this->data['is_registered'] = $this->data['is_bot'] = false;
indvdum@0
  1191
indvdum@0
  1192
				// Set as a precaution to allow login_box() handling this case correctly as well as this function not being executed again.
indvdum@0
  1193
				define('IN_CHECK_BAN', 1);
indvdum@0
  1194
indvdum@0
  1195
				login_box("index.$phpEx");
indvdum@0
  1196
indvdum@0
  1197
				// The false here is needed, else the user is able to circumvent the ban.
indvdum@0
  1198
				$this->session_kill(false);
indvdum@0
  1199
			}
indvdum@0
  1200
indvdum@0
  1201
			// Ok, we catch the case of an empty session id for the anonymous user...
indvdum@0
  1202
			// This can happen if the user is logging in, banned by username and the login_box() being called "again".
indvdum@0
  1203
			if (empty($this->session_id) && defined('IN_CHECK_BAN'))
indvdum@0
  1204
			{
indvdum@0
  1205
				$this->session_create(ANONYMOUS);
indvdum@0
  1206
			}
indvdum@0
  1207
indvdum@0
  1208
indvdum@0
  1209
			// Determine which message to output
indvdum@0
  1210
			$till_date = ($ban_row['ban_end']) ? $this->format_date($ban_row['ban_end']) : '';
indvdum@0
  1211
			$message = ($ban_row['ban_end']) ? 'BOARD_BAN_TIME' : 'BOARD_BAN_PERM';
indvdum@0
  1212
indvdum@0
  1213
			$message = sprintf($this->lang[$message], $till_date, '<a href="mailto:' . $config['board_contact'] . '">', '</a>');
indvdum@0
  1214
			$message .= ($ban_row['ban_give_reason']) ? '<br /><br />' . sprintf($this->lang['BOARD_BAN_REASON'], $ban_row['ban_give_reason']) : '';
indvdum@0
  1215
			$message .= '<br /><br /><em>' . $this->lang['BAN_TRIGGERED_BY_' . strtoupper($ban_triggered_by)] . '</em>';
indvdum@0
  1216
indvdum@0
  1217
			// To circumvent session_begin returning a valid value and the check_ban() not called on second page view, we kill the session again
indvdum@0
  1218
			$this->session_kill(false);
indvdum@0
  1219
indvdum@0
  1220
			// A very special case... we are within the cron script which is not supposed to print out the ban message... show blank page
indvdum@0
  1221
			if (defined('IN_CRON'))
indvdum@0
  1222
			{
indvdum@0
  1223
				garbage_collection();
indvdum@0
  1224
				exit_handler();
indvdum@0
  1225
				exit;
indvdum@0
  1226
			}
indvdum@0
  1227
indvdum@0
  1228
			trigger_error($message);
indvdum@0
  1229
		}
indvdum@0
  1230
indvdum@0
  1231
		return ($banned && $ban_row['ban_give_reason']) ? $ban_row['ban_give_reason'] : $banned;
indvdum@0
  1232
	}
indvdum@0
  1233
indvdum@0
  1234
	/**
indvdum@0
  1235
	* Check if ip is blacklisted
indvdum@0
  1236
	* This should be called only where absolutly necessary
indvdum@0
  1237
	*
indvdum@0
  1238
	* Only IPv4 (rbldns does not support AAAA records/IPv6 lookups)
indvdum@0
  1239
	*
indvdum@0
  1240
	* @author satmd (from the php manual)
indvdum@0
  1241
	* @param string $mode register/post - spamcop for example is ommitted for posting
indvdum@0
  1242
	* @return false if ip is not blacklisted, else an array([checked server], [lookup])
indvdum@0
  1243
	*/
indvdum@0
  1244
	function check_dnsbl($mode, $ip = false)
indvdum@0
  1245
	{
indvdum@0
  1246
		if ($ip === false)
indvdum@0
  1247
		{
indvdum@0
  1248
			$ip = $this->ip;
indvdum@0
  1249
		}
indvdum@0
  1250
indvdum@0
  1251
		// Neither Spamhaus nor Spamcop supports IPv6 addresses.
indvdum@0
  1252
		if (strpos($ip, ':') !== false)
indvdum@0
  1253
		{
indvdum@0
  1254
			return false;
indvdum@0
  1255
		}
indvdum@0
  1256
indvdum@0
  1257
		$dnsbl_check = array(
indvdum@0
  1258
			'sbl.spamhaus.org'	=> 'http://www.spamhaus.org/query/bl?ip=',
indvdum@0
  1259
		);
indvdum@0
  1260
indvdum@0
  1261
		if ($mode == 'register')
indvdum@0
  1262
		{
indvdum@0
  1263
			$dnsbl_check['bl.spamcop.net'] = 'http://spamcop.net/bl.shtml?';
indvdum@0
  1264
		}
indvdum@0
  1265
indvdum@0
  1266
		if ($ip)
indvdum@0
  1267
		{
indvdum@0
  1268
			$quads = explode('.', $ip);
indvdum@0
  1269
			$reverse_ip = $quads[3] . '.' . $quads[2] . '.' . $quads[1] . '.' . $quads[0];
indvdum@0
  1270
indvdum@0
  1271
			// Need to be listed on all servers...
indvdum@0
  1272
			$listed = true;
indvdum@0
  1273
			$info = array();
indvdum@0
  1274
indvdum@0
  1275
			foreach ($dnsbl_check as $dnsbl => $lookup)
indvdum@0
  1276
			{
indvdum@0
  1277
				if (phpbb_checkdnsrr($reverse_ip . '.' . $dnsbl . '.', 'A') === true)
indvdum@0
  1278
				{
indvdum@0
  1279
					$info = array($dnsbl, $lookup . $ip);
indvdum@0
  1280
				}
indvdum@0
  1281
				else
indvdum@0
  1282
				{
indvdum@0
  1283
					$listed = false;
indvdum@0
  1284
				}
indvdum@0
  1285
			}
indvdum@0
  1286
indvdum@0
  1287
			if ($listed)
indvdum@0
  1288
			{
indvdum@0
  1289
				return $info;
indvdum@0
  1290
			}
indvdum@0
  1291
		}
indvdum@0
  1292
indvdum@0
  1293
		return false;
indvdum@0
  1294
	}
indvdum@0
  1295
indvdum@0
  1296
	/**
indvdum@0
  1297
	* Check if URI is blacklisted
indvdum@0
  1298
	* This should be called only where absolutly necessary, for example on the submitted website field
indvdum@0
  1299
	* This function is not in use at the moment and is only included for testing purposes, it may not work at all!
indvdum@0
  1300
	* This means it is untested at the moment and therefore commented out
indvdum@0
  1301
	*
indvdum@0
  1302
	* @param string $uri URI to check
indvdum@0
  1303
	* @return true if uri is on blacklist, else false. Only blacklist is checked (~zero FP), no grey lists
indvdum@0
  1304
	function check_uribl($uri)
indvdum@0
  1305
	{
indvdum@0
  1306
		// Normally parse_url() is not intended to parse uris
indvdum@0
  1307
		// We need to get the top-level domain name anyway... change.
indvdum@0
  1308
		$uri = parse_url($uri);
indvdum@0
  1309
indvdum@0
  1310
		if ($uri === false || empty($uri['host']))
indvdum@0
  1311
		{
indvdum@0
  1312
			return false;
indvdum@0
  1313
		}
indvdum@0
  1314
indvdum@0
  1315
		$uri = trim($uri['host']);
indvdum@0
  1316
indvdum@0
  1317
		if ($uri)
indvdum@0
  1318
		{
indvdum@0
  1319
			// One problem here... the return parameter for the "windows" method is different from what
indvdum@0
  1320
			// we expect... this may render this check useless...
indvdum@0
  1321
			if (phpbb_checkdnsrr($uri . '.multi.uribl.com.', 'A') === true)
indvdum@0
  1322
			{
indvdum@0
  1323
				return true;
indvdum@0
  1324
			}
indvdum@0
  1325
		}
indvdum@0
  1326
indvdum@0
  1327
		return false;
indvdum@0
  1328
	}
indvdum@0
  1329
	*/
indvdum@0
  1330
indvdum@0
  1331
	/**
indvdum@0
  1332
	* Set/Update a persistent login key
indvdum@0
  1333
	*
indvdum@0
  1334
	* This method creates or updates a persistent session key. When a user makes
indvdum@0
  1335
	* use of persistent (formerly auto-) logins a key is generated and stored in the
indvdum@0
  1336
	* DB. When they revisit with the same key it's automatically updated in both the
indvdum@0
  1337
	* DB and cookie. Multiple keys may exist for each user representing different
indvdum@0
  1338
	* browsers or locations. As with _any_ non-secure-socket no passphrase login this
indvdum@0
  1339
	* remains vulnerable to exploit.
indvdum@0
  1340
	*/
indvdum@0
  1341
	function set_login_key($user_id = false, $key = false, $user_ip = false)
indvdum@0
  1342
	{
indvdum@0
  1343
		global $config, $db;
indvdum@0
  1344
indvdum@0
  1345
		$user_id = ($user_id === false) ? $this->data['user_id'] : $user_id;
indvdum@0
  1346
		$user_ip = ($user_ip === false) ? $this->ip : $user_ip;
indvdum@0
  1347
		$key = ($key === false) ? (($this->cookie_data['k']) ? $this->cookie_data['k'] : false) : $key;
indvdum@0
  1348
indvdum@0
  1349
		$key_id = unique_id(hexdec(substr($this->session_id, 0, 8)));
indvdum@0
  1350
indvdum@0
  1351
		$sql_ary = array(
indvdum@0
  1352
			'key_id'		=> (string) md5($key_id),
indvdum@0
  1353
			'last_ip'		=> (string) $this->ip,
indvdum@0
  1354
			'last_login'	=> (int) time()
indvdum@0
  1355
		);
indvdum@0
  1356
indvdum@0
  1357
		if (!$key)
indvdum@0
  1358
		{
indvdum@0
  1359
			$sql_ary += array(
indvdum@0
  1360
				'user_id'	=> (int) $user_id
indvdum@0
  1361
			);
indvdum@0
  1362
		}
indvdum@0
  1363
indvdum@0
  1364
		if ($key)
indvdum@0
  1365
		{
indvdum@0
  1366
			$sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . '
indvdum@0
  1367
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
indvdum@0
  1368
				WHERE user_id = ' . (int) $user_id . "
indvdum@0
  1369
					AND key_id = '" . $db->sql_escape(md5($key)) . "'";
indvdum@0
  1370
		}
indvdum@0
  1371
		else
indvdum@0
  1372
		{
indvdum@0
  1373
			$sql = 'INSERT INTO ' . SESSIONS_KEYS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
indvdum@0
  1374
		}
indvdum@0
  1375
		$db->sql_query($sql);
indvdum@0
  1376
indvdum@0
  1377
		$this->cookie_data['k'] = $key_id;
indvdum@0
  1378
indvdum@0
  1379
		return false;
indvdum@0
  1380
	}
indvdum@0
  1381
indvdum@0
  1382
	/**
indvdum@0
  1383
	* Reset all login keys for the specified user
indvdum@0
  1384
	*
indvdum@0
  1385
	* This method removes all current login keys for a specified (or the current)
indvdum@0
  1386
	* user. It will be called on password change to render old keys unusable
indvdum@0
  1387
	*/
indvdum@0
  1388
	function reset_login_keys($user_id = false)
indvdum@0
  1389
	{
indvdum@0
  1390
		global $config, $db;
indvdum@0
  1391
indvdum@0
  1392
		$user_id = ($user_id === false) ? (int) $this->data['user_id'] : (int) $user_id;
indvdum@0
  1393
indvdum@0
  1394
		$sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
indvdum@0
  1395
			WHERE user_id = ' . (int) $user_id;
indvdum@0
  1396
		$db->sql_query($sql);
indvdum@0
  1397
indvdum@0
  1398
		// If the user is logged in, update last visit info first before deleting sessions
indvdum@0
  1399
		$sql = 'SELECT session_time, session_page
indvdum@0
  1400
			FROM ' . SESSIONS_TABLE . '
indvdum@0
  1401
			WHERE session_user_id = ' . (int) $user_id . '
indvdum@0
  1402
			ORDER BY session_time DESC';
indvdum@0
  1403
		$result = $db->sql_query_limit($sql, 1);
indvdum@0
  1404
		$row = $db->sql_fetchrow($result);
indvdum@0
  1405
		$db->sql_freeresult($result);
indvdum@0
  1406
indvdum@0
  1407
		if ($row)
indvdum@0
  1408
		{
indvdum@0
  1409
			$sql = 'UPDATE ' . USERS_TABLE . '
indvdum@0
  1410
				SET user_lastvisit = ' . (int) $row['session_time'] . ", user_lastpage = '" . $db->sql_escape($row['session_page']) . "'
indvdum@0
  1411
				WHERE user_id = " . (int) $user_id;
indvdum@0
  1412
			$db->sql_query($sql);
indvdum@0
  1413
		}
indvdum@0
  1414
indvdum@0
  1415
		// Let's also clear any current sessions for the specified user_id
indvdum@0
  1416
		// If it's the current user then we'll leave this session intact
indvdum@0
  1417
		$sql_where = 'session_user_id = ' . (int) $user_id;
indvdum@0
  1418
		$sql_where .= ($user_id === (int) $this->data['user_id']) ? " AND session_id <> '" . $db->sql_escape($this->session_id) . "'" : '';
indvdum@0
  1419
indvdum@0
  1420
		$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
indvdum@0
  1421
			WHERE $sql_where";
indvdum@0
  1422
		$db->sql_query($sql);
indvdum@0
  1423
indvdum@0
  1424
		// We're changing the password of the current user and they have a key
indvdum@0
  1425
		// Lets regenerate it to be safe
indvdum@0
  1426
		if ($user_id === (int) $this->data['user_id'] && $this->cookie_data['k'])
indvdum@0
  1427
		{
indvdum@0
  1428
			$this->set_login_key($user_id);
indvdum@0
  1429
		}
indvdum@0
  1430
	}
indvdum@0
  1431
indvdum@0
  1432
indvdum@0
  1433
	/**
indvdum@0
  1434
	* Check if the request originated from the same page.
indvdum@0
  1435
	* @param bool $check_script_path If true, the path will be checked as well
indvdum@0
  1436
	*/
indvdum@0
  1437
	function validate_referer($check_script_path = false)
indvdum@0
  1438
	{
indvdum@0
  1439
		global $config;
indvdum@0
  1440
indvdum@0
  1441
		// no referer - nothing to validate, user's fault for turning it off (we only check on POST; so meta can't be the reason)
indvdum@0
  1442
		if (empty($this->referer) || empty($this->host))
indvdum@0
  1443
		{
indvdum@0
  1444
			return true;
indvdum@0
  1445
		}
indvdum@0
  1446
indvdum@0
  1447
		$host = htmlspecialchars($this->host);
indvdum@0
  1448
		$ref = substr($this->referer, strpos($this->referer, '://') + 3);
indvdum@0
  1449
indvdum@0
  1450
		if (!(stripos($ref, $host) === 0) && (!$config['force_server_vars'] || !(stripos($ref, $config['server_name']) === 0)))
indvdum@0
  1451
		{
indvdum@0
  1452
			return false;
indvdum@0
  1453
		}
indvdum@0
  1454
		else if ($check_script_path && rtrim($this->page['root_script_path'], '/') !== '')
indvdum@0
  1455
		{
indvdum@0
  1456
			$ref = substr($ref, strlen($host));
indvdum@0
  1457
			$server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT');
indvdum@0
  1458
indvdum@0
  1459
			if ($server_port !== 80 && $server_port !== 443 && stripos($ref, ":$server_port") === 0)
indvdum@0
  1460
			{
indvdum@0
  1461
				$ref = substr($ref, strlen(":$server_port"));
indvdum@0
  1462
			}
indvdum@0
  1463
indvdum@0
  1464
			if (!(stripos(rtrim($ref, '/'), rtrim($this->page['root_script_path'], '/')) === 0))
indvdum@0
  1465
			{
indvdum@0
  1466
				return false;
indvdum@0
  1467
			}
indvdum@0
  1468
		}
indvdum@0
  1469
indvdum@0
  1470
		return true;
indvdum@0
  1471
	}
indvdum@0
  1472
indvdum@0
  1473
indvdum@0
  1474
	function unset_admin()
indvdum@0
  1475
	{
indvdum@0
  1476
		global $db;
indvdum@0
  1477
		$sql = 'UPDATE ' . SESSIONS_TABLE . '
indvdum@0
  1478
			SET session_admin = 0
indvdum@0
  1479
			WHERE session_id = \'' . $db->sql_escape($this->session_id) . '\'';
indvdum@0
  1480
		$db->sql_query($sql);
indvdum@0
  1481
	}
indvdum@0
  1482
}
indvdum@0
  1483
indvdum@0
  1484
indvdum@0
  1485
/**
indvdum@0
  1486
* Base user class
indvdum@0
  1487
*
indvdum@0
  1488
* This is the overarching class which contains (through session extend)
indvdum@0
  1489
* all methods utilised for user functionality during a session.
indvdum@0
  1490
*
indvdum@0
  1491
* @package phpBB3
indvdum@0
  1492
*/
indvdum@0
  1493
class user extends session
indvdum@0
  1494
{
indvdum@0
  1495
	var $lang = array();
indvdum@0
  1496
	var $help = array();
indvdum@0
  1497
	var $theme = array();
indvdum@0
  1498
	var $date_format;
indvdum@0
  1499
	var $timezone;
indvdum@0
  1500
	var $dst;
indvdum@0
  1501
indvdum@0
  1502
	var $lang_name = false;
indvdum@0
  1503
	var $lang_id = false;
indvdum@0
  1504
	var $lang_path;
indvdum@0
  1505
	var $img_lang;
indvdum@0
  1506
	var $img_array = array();
indvdum@0
  1507
indvdum@0
  1508
	// Able to add new options (up to id 31)
indvdum@0
  1509
	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);
indvdum@0
  1510
	var $keyvalues = array();
indvdum@0
  1511
indvdum@0
  1512
	/**
indvdum@0
  1513
	* Constructor to set the lang path
indvdum@0
  1514
	*/
indvdum@0
  1515
	function user()
indvdum@0
  1516
	{
indvdum@0
  1517
		global $phpbb_root_path;
indvdum@0
  1518
indvdum@0
  1519
		$this->lang_path = $phpbb_root_path . 'language/';
indvdum@0
  1520
	}
indvdum@0
  1521
indvdum@0
  1522
	/**
indvdum@0
  1523
	* Function to set custom language path (able to use directory outside of phpBB)
indvdum@0
  1524
	*
indvdum@0
  1525
	* @param string $lang_path New language path used.
indvdum@0
  1526
	* @access public
indvdum@0
  1527
	*/
indvdum@0
  1528
	function set_custom_lang_path($lang_path)
indvdum@0
  1529
	{
indvdum@0
  1530
		$this->lang_path = $lang_path;
indvdum@0
  1531
indvdum@0
  1532
		if (substr($this->lang_path, -1) != '/')
indvdum@0
  1533
		{
indvdum@0
  1534
			$this->lang_path .= '/';
indvdum@0
  1535
		}
indvdum@0
  1536
	}
indvdum@0
  1537
indvdum@0
  1538
	/**
indvdum@0
  1539
	* Setup basic user-specific items (style, language, ...)
indvdum@0
  1540
	*/
indvdum@0
  1541
	function setup($lang_set = false, $style = false)
indvdum@0
  1542
	{
indvdum@0
  1543
		global $db, $template, $config, $auth, $phpEx, $phpbb_root_path, $cache;
indvdum@0
  1544
indvdum@0
  1545
		if ($this->data['user_id'] != ANONYMOUS)
indvdum@0
  1546
		{
indvdum@0
  1547
			$this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']);
indvdum@0
  1548
indvdum@0
  1549
			$this->date_format = $this->data['user_dateformat'];
indvdum@0
  1550
			$this->timezone = $this->data['user_timezone'] * 3600;
indvdum@0
  1551
			$this->dst = $this->data['user_dst'] * 3600;
indvdum@0
  1552
		}
indvdum@0
  1553
		else
indvdum@0
  1554
		{
indvdum@0
  1555
			$this->lang_name = basename($config['default_lang']);
indvdum@0
  1556
			$this->date_format = $config['default_dateformat'];
indvdum@0
  1557
			$this->timezone = $config['board_timezone'] * 3600;
indvdum@0
  1558
			$this->dst = $config['board_dst'] * 3600;
indvdum@0
  1559
indvdum@0
  1560
			/**
indvdum@0
  1561
			* If a guest user is surfing, we try to guess his/her language first by obtaining the browser language
indvdum@0
  1562
			* If re-enabled we need to make sure only those languages installed are checked
indvdum@0
  1563
			* Commented out so we do not loose the code.
indvdum@0
  1564
indvdum@0
  1565
			if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
indvdum@0
  1566
			{
indvdum@0
  1567
				$accept_lang_ary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
indvdum@0
  1568
indvdum@0
  1569
				foreach ($accept_lang_ary as $accept_lang)
indvdum@0
  1570
				{
indvdum@0
  1571
					// Set correct format ... guess full xx_YY form
indvdum@0
  1572
					$accept_lang = substr($accept_lang, 0, 2) . '_' . strtoupper(substr($accept_lang, 3, 2));
indvdum@0
  1573
					$accept_lang = basename($accept_lang);
indvdum@0
  1574
indvdum@0
  1575
					if (file_exists($this->lang_path . $accept_lang . "/common.$phpEx"))
indvdum@0
  1576
					{
indvdum@0
  1577
						$this->lang_name = $config['default_lang'] = $accept_lang;
indvdum@0
  1578
						break;
indvdum@0
  1579
					}
indvdum@0
  1580
					else
indvdum@0
  1581
					{
indvdum@0
  1582
						// No match on xx_YY so try xx
indvdum@0
  1583
						$accept_lang = substr($accept_lang, 0, 2);
indvdum@0
  1584
						$accept_lang = basename($accept_lang);
indvdum@0
  1585
indvdum@0
  1586
						if (file_exists($this->lang_path . $accept_lang . "/common.$phpEx"))
indvdum@0
  1587
						{
indvdum@0
  1588
							$this->lang_name = $config['default_lang'] = $accept_lang;
indvdum@0
  1589
							break;
indvdum@0
  1590
						}
indvdum@0
  1591
					}
indvdum@0
  1592
				}
indvdum@0
  1593
			}
indvdum@0
  1594
			*/
indvdum@0
  1595
		}
indvdum@0
  1596
indvdum@0
  1597
		// We include common language file here to not load it every time a custom language file is included
indvdum@0
  1598
		$lang = &$this->lang;
indvdum@0
  1599
indvdum@0
  1600
		// Do not suppress error if in DEBUG_EXTRA mode
indvdum@0
  1601
		$include_result = (defined('DEBUG_EXTRA')) ? (include $this->lang_path . $this->lang_name . "/common.$phpEx") : (@include $this->lang_path . $this->lang_name . "/common.$phpEx");
indvdum@0
  1602
indvdum@0
  1603
		if ($include_result === false)
indvdum@0
  1604
		{
indvdum@0
  1605
			die('Language file ' . $this->lang_path . $this->lang_name . "/common.$phpEx" . " couldn't be opened.");
indvdum@0
  1606
		}
indvdum@0
  1607
indvdum@0
  1608
		$this->add_lang($lang_set);
indvdum@0
  1609
		unset($lang_set);
indvdum@0
  1610
indvdum@0
  1611
		if (!empty($_GET['style']) && $auth->acl_get('a_styles') && !defined('ADMIN_START'))
indvdum@0
  1612
		{
indvdum@0
  1613
			global $SID, $_EXTRA_URL;
indvdum@0
  1614
indvdum@0
  1615
			$style = request_var('style', 0);
indvdum@0
  1616
			$SID .= '&amp;style=' . $style;
indvdum@0
  1617
			$_EXTRA_URL = array('style=' . $style);
indvdum@0
  1618
		}
indvdum@0
  1619
		else
indvdum@0
  1620
		{
indvdum@0
  1621
			// Set up style
indvdum@0
  1622
			$style = ($style) ? $style : ((!$config['override_user_style']) ? $this->data['user_style'] : $config['default_style']);
indvdum@0
  1623
		}
indvdum@0
  1624
indvdum@0
  1625
		$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
indvdum@0
  1626
			FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . " i
indvdum@0
  1627
			WHERE s.style_id = $style
indvdum@0
  1628
				AND t.template_id = s.template_id
indvdum@0
  1629
				AND c.theme_id = s.theme_id
indvdum@0
  1630
				AND i.imageset_id = s.imageset_id";
indvdum@0
  1631
		$result = $db->sql_query($sql, 3600);
indvdum@0
  1632
		$this->theme = $db->sql_fetchrow($result);
indvdum@0
  1633
		$db->sql_freeresult($result);
indvdum@0
  1634
indvdum@0
  1635
		// User has wrong style
indvdum@0
  1636
		if (!$this->theme && $style == $this->data['user_style'])
indvdum@0
  1637
		{
indvdum@0
  1638
			$style = $this->data['user_style'] = $config['default_style'];
indvdum@0
  1639
indvdum@0
  1640
			$sql = 'UPDATE ' . USERS_TABLE . "
indvdum@0
  1641
				SET user_style = $style
indvdum@0
  1642
				WHERE user_id = {$this->data['user_id']}";
indvdum@0
  1643
			$db->sql_query($sql);
indvdum@0
  1644
indvdum@0
  1645
			$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
indvdum@0
  1646
				FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . " i
indvdum@0
  1647
				WHERE s.style_id = $style
indvdum@0
  1648
					AND t.template_id = s.template_id
indvdum@0
  1649
					AND c.theme_id = s.theme_id
indvdum@0
  1650
					AND i.imageset_id = s.imageset_id";
indvdum@0
  1651
			$result = $db->sql_query($sql, 3600);
indvdum@0
  1652
			$this->theme = $db->sql_fetchrow($result);
indvdum@0
  1653
			$db->sql_freeresult($result);
indvdum@0
  1654
		}
indvdum@0
  1655
indvdum@0
  1656
		if (!$this->theme)
indvdum@0
  1657
		{
indvdum@0
  1658
			trigger_error('Could not get style data', E_USER_ERROR);
indvdum@0
  1659
		}
indvdum@0
  1660
indvdum@0
  1661
		// Now parse the cfg file and cache it
indvdum@0
  1662
		$parsed_items = $cache->obtain_cfg_items($this->theme);
indvdum@0
  1663
indvdum@0
  1664
		// We are only interested in the theme configuration for now
indvdum@0
  1665
		$parsed_items = $parsed_items['theme'];
indvdum@0
  1666
indvdum@0
  1667
		$check_for = array(
indvdum@0
  1668
			'parse_css_file'	=> (int) 0,
indvdum@0
  1669
			'pagination_sep'	=> (string) ', '
indvdum@0
  1670
		);
indvdum@0
  1671
indvdum@0
  1672
		foreach ($check_for as $key => $default_value)
indvdum@0
  1673
		{
indvdum@0
  1674
			$this->theme[$key] = (isset($parsed_items[$key])) ? $parsed_items[$key] : $default_value;
indvdum@0
  1675
			settype($this->theme[$key], gettype($default_value));
indvdum@0
  1676
indvdum@0
  1677
			if (is_string($default_value))
indvdum@0
  1678
			{
indvdum@0
  1679
				$this->theme[$key] = htmlspecialchars($this->theme[$key]);
indvdum@0
  1680
			}
indvdum@0
  1681
		}
indvdum@0
  1682
indvdum@0
  1683
		// If the style author specified the theme needs to be cached
indvdum@0
  1684
		// (because of the used paths and variables) than make sure it is the case.
indvdum@0
  1685
		// For example, if the theme uses language-specific images it needs to be stored in db.
indvdum@0
  1686
		if (!$this->theme['theme_storedb'] && $this->theme['parse_css_file'])
indvdum@0
  1687
		{
indvdum@0
  1688
			$this->theme['theme_storedb'] = 1;
indvdum@0
  1689
indvdum@0
  1690
			$stylesheet = file_get_contents("{$phpbb_root_path}styles/{$this->theme['theme_path']}/theme/stylesheet.css");
indvdum@0
  1691
			// Match CSS imports
indvdum@0
  1692
			$matches = array();
indvdum@0
  1693
			preg_match_all('/@import url\(["\'](.*)["\']\);/i', $stylesheet, $matches);
indvdum@0
  1694
indvdum@0
  1695
			if (sizeof($matches))
indvdum@0
  1696
			{
indvdum@0
  1697
				$content = '';
indvdum@0
  1698
				foreach ($matches[0] as $idx => $match)
indvdum@0
  1699
				{
indvdum@0
  1700
					if ($content = @file_get_contents("{$phpbb_root_path}styles/{$this->theme['theme_path']}/theme/" . $matches[1][$idx]))
indvdum@0
  1701
					{
indvdum@0
  1702
						$content = trim($content);
indvdum@0
  1703
					}
indvdum@0
  1704
					else
indvdum@0
  1705
					{
indvdum@0
  1706
						$content = '';
indvdum@0
  1707
					}
indvdum@0
  1708
					$stylesheet = str_replace($match, $content, $stylesheet);
indvdum@0
  1709
				}
indvdum@0
  1710
				unset($content);
indvdum@0
  1711
			}
indvdum@0
  1712
indvdum@0
  1713
			$stylesheet = str_replace('./', 'styles/' . $this->theme['theme_path'] . '/theme/', $stylesheet);
indvdum@0
  1714
indvdum@0
  1715
			$sql_ary = array(
indvdum@0
  1716
				'theme_data'	=> $stylesheet,
indvdum@0
  1717
				'theme_mtime'	=> time(),
indvdum@0
  1718
				'theme_storedb'	=> 1
indvdum@0
  1719
			);
indvdum@0
  1720
indvdum@0
  1721
			$sql = 'UPDATE ' . STYLES_THEME_TABLE . '
indvdum@0
  1722
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
indvdum@0
  1723
				WHERE theme_id = ' . $this->theme['theme_id'];
indvdum@0
  1724
			$db->sql_query($sql);
indvdum@0
  1725
indvdum@0
  1726
			unset($sql_ary);
indvdum@0
  1727
		}
indvdum@0
  1728
indvdum@0
  1729
		$template->set_template();
indvdum@0
  1730
indvdum@0
  1731
		$this->img_lang = (file_exists($phpbb_root_path . 'styles/' . $this->theme['imageset_path'] . '/imageset/' . $this->lang_name)) ? $this->lang_name : $config['default_lang'];
indvdum@0
  1732
indvdum@0
  1733
		// Same query in style.php
indvdum@0
  1734
		$sql = 'SELECT *
indvdum@0
  1735
			FROM ' . STYLES_IMAGESET_DATA_TABLE . '
indvdum@0
  1736
			WHERE imageset_id = ' . $this->theme['imageset_id'] . "
indvdum@0
  1737
			AND image_filename <> ''
indvdum@0
  1738
			AND image_lang IN ('" . $db->sql_escape($this->img_lang) . "', '')";
indvdum@0
  1739
		$result = $db->sql_query($sql, 3600);
indvdum@0
  1740
indvdum@0
  1741
		$localised_images = false;
indvdum@0
  1742
		while ($row = $db->sql_fetchrow($result))
indvdum@0
  1743
		{
indvdum@0
  1744
			if ($row['image_lang'])
indvdum@0
  1745
			{
indvdum@0
  1746
				$localised_images = true;
indvdum@0
  1747
			}
indvdum@0
  1748
indvdum@0
  1749
			$row['image_filename'] = rawurlencode($row['image_filename']);
indvdum@0
  1750
			$this->img_array[$row['image_name']] = $row;
indvdum@0
  1751
		}
indvdum@0
  1752
		$db->sql_freeresult($result);
indvdum@0
  1753
indvdum@0
  1754
		// there were no localised images, try to refresh the localised imageset for the user's language
indvdum@0
  1755
		if (!$localised_images)
indvdum@0
  1756
		{
indvdum@0
  1757
			// Attention: this code ignores the image definition list from acp_styles and just takes everything
indvdum@0
  1758
			// that the config file contains
indvdum@0
  1759
			$sql_ary = array();
indvdum@0
  1760
indvdum@0
  1761
			$db->sql_transaction('begin');
indvdum@0
  1762
indvdum@0
  1763
			$sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . '
indvdum@0
  1764
				WHERE imageset_id = ' . $this->theme['imageset_id'] . '
indvdum@0
  1765
					AND image_lang = \'' . $db->sql_escape($this->img_lang) . '\'';
indvdum@0
  1766
			$result = $db->sql_query($sql);
indvdum@0
  1767
indvdum@0
  1768
			if (@file_exists("{$phpbb_root_path}styles/{$this->theme['imageset_path']}/imageset/{$this->img_lang}/imageset.cfg"))
indvdum@0
  1769
			{
indvdum@0
  1770
				$cfg_data_imageset_data = parse_cfg_file("{$phpbb_root_path}styles/{$this->theme['imageset_path']}/imageset/{$this->img_lang}/imageset.cfg");
indvdum@0
  1771
				foreach ($cfg_data_imageset_data as $image_name => $value)
indvdum@0
  1772
				{
indvdum@0
  1773
					if (strpos($value, '*') !== false)
indvdum@0
  1774
					{
indvdum@0
  1775
						if (substr($value, -1, 1) === '*')
indvdum@0
  1776
						{
indvdum@0
  1777
							list($image_filename, $image_height) = explode('*', $value);
indvdum@0
  1778
							$image_width = 0;
indvdum@0
  1779
						}
indvdum@0
  1780
						else
indvdum@0
  1781
						{
indvdum@0
  1782
							list($image_filename, $image_height, $image_width) = explode('*', $value);
indvdum@0
  1783
						}
indvdum@0
  1784
					}
indvdum@0
  1785
					else
indvdum@0
  1786
					{
indvdum@0
  1787
						$image_filename = $value;
indvdum@0
  1788
						$image_height = $image_width = 0;
indvdum@0
  1789
					}
indvdum@0
  1790
indvdum@0
  1791
					if (strpos($image_name, 'img_') === 0 && $image_filename)
indvdum@0
  1792
					{
indvdum@0
  1793
						$image_name = substr($image_name, 4);
indvdum@0
  1794
						$sql_ary[] = array(
indvdum@0
  1795
							'image_name'		=> (string) $image_name,
indvdum@0
  1796
							'image_filename'	=> (string) $image_filename,
indvdum@0
  1797
							'image_height'		=> (int) $image_height,
indvdum@0
  1798
							'image_width'		=> (int) $image_width,
indvdum@0
  1799
							'imageset_id'		=> (int) $this->theme['imageset_id'],
indvdum@0
  1800
							'image_lang'		=> (string) $this->img_lang,
indvdum@0
  1801
						);
indvdum@0
  1802
					}
indvdum@0
  1803
				}
indvdum@0
  1804
			}
indvdum@0
  1805
indvdum@0
  1806
			if (sizeof($sql_ary))
indvdum@0
  1807
			{
indvdum@0
  1808
				$db->sql_multi_insert(STYLES_IMAGESET_DATA_TABLE, $sql_ary);
indvdum@0
  1809
				$db->sql_transaction('commit');
indvdum@0
  1810
				$cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
indvdum@0
  1811
indvdum@0
  1812
				add_log('admin', 'LOG_IMAGESET_LANG_REFRESHED', $this->theme['imageset_name'], $this->img_lang);
indvdum@0
  1813
			}
indvdum@0
  1814
			else
indvdum@0
  1815
			{
indvdum@0
  1816
				$db->sql_transaction('commit');
indvdum@0
  1817
				add_log('admin', 'LOG_IMAGESET_LANG_MISSING', $this->theme['imageset_name'], $this->img_lang);
indvdum@0
  1818
			}
indvdum@0
  1819
		}
indvdum@0
  1820
indvdum@0
  1821
		// Call phpbb_user_session_handler() in case external application want to "bend" some variables or replace classes...
indvdum@0
  1822
		// After calling it we continue script execution...
indvdum@0
  1823
		phpbb_user_session_handler();
indvdum@0
  1824
indvdum@0
  1825
		// If this function got called from the error handler we are finished here.
indvdum@0
  1826
		if (defined('IN_ERROR_HANDLER'))
indvdum@0
  1827
		{
indvdum@0
  1828
			return;
indvdum@0
  1829
		}
indvdum@0
  1830
indvdum@0
  1831
		// Disable board if the install/ directory is still present
indvdum@0
  1832
		// For the brave development army we do not care about this, else we need to comment out this everytime we develop locally
indvdum@0
  1833
		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'))
indvdum@0
  1834
		{
indvdum@0
  1835
			// Adjust the message slightly according to the permissions
indvdum@0
  1836
			if ($auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_'))
indvdum@0
  1837
			{
indvdum@0
  1838
				$message = 'REMOVE_INSTALL';
indvdum@0
  1839
			}
indvdum@0
  1840
			else
indvdum@0
  1841
			{
indvdum@0
  1842
				$message = (!empty($config['board_disable_msg'])) ? $config['board_disable_msg'] : 'BOARD_DISABLE';
indvdum@0
  1843
			}
indvdum@0
  1844
			trigger_error($message);
indvdum@0
  1845
		}
indvdum@0
  1846
indvdum@0
  1847
		// Is board disabled and user not an admin or moderator?
indvdum@0
  1848
		if ($config['board_disable'] && !defined('IN_LOGIN') && !$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
indvdum@0
  1849
		{
indvdum@0
  1850
			if ($this->data['is_bot'])
indvdum@0
  1851
			{
indvdum@0
  1852
				send_status_line(503, 'Service Unavailable');
indvdum@0
  1853
			}
indvdum@0
  1854
indvdum@0
  1855
			$message = (!empty($config['board_disable_msg'])) ? $config['board_disable_msg'] : 'BOARD_DISABLE';
indvdum@0
  1856
			trigger_error($message);
indvdum@0
  1857
		}
indvdum@0
  1858
indvdum@0
  1859
		// Is load exceeded?
indvdum@0
  1860
		if ($config['limit_load'] && $this->load !== false)
indvdum@0
  1861
		{
indvdum@0
  1862
			if ($this->load > floatval($config['limit_load']) && !defined('IN_LOGIN') && !defined('IN_ADMIN'))
indvdum@0
  1863
			{
indvdum@0
  1864
				// Set board disabled to true to let the admins/mods get the proper notification
indvdum@0
  1865
				$config['board_disable'] = '1';
indvdum@0
  1866
indvdum@0
  1867
				if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
indvdum@0
  1868
				{
indvdum@0
  1869
					if ($this->data['is_bot'])
indvdum@0
  1870
					{
indvdum@0
  1871
						send_status_line(503, 'Service Unavailable');
indvdum@0
  1872
					}
indvdum@0
  1873
					trigger_error('BOARD_UNAVAILABLE');
indvdum@0
  1874
				}
indvdum@0
  1875
			}
indvdum@0
  1876
		}
indvdum@0
  1877
indvdum@0
  1878
		if (isset($this->data['session_viewonline']))
indvdum@0
  1879
		{
indvdum@0
  1880
			// Make sure the user is able to hide his session
indvdum@0
  1881
			if (!$this->data['session_viewonline'])
indvdum@0
  1882
			{
indvdum@0
  1883
				// Reset online status if not allowed to hide the session...
indvdum@0
  1884
				if (!$auth->acl_get('u_hideonline'))
indvdum@0
  1885
				{
indvdum@0
  1886
					$sql = 'UPDATE ' . SESSIONS_TABLE . '
indvdum@0
  1887
						SET session_viewonline = 1
indvdum@0
  1888
						WHERE session_user_id = ' . $this->data['user_id'];
indvdum@0
  1889
					$db->sql_query($sql);
indvdum@0
  1890
					$this->data['session_viewonline'] = 1;
indvdum@0
  1891
				}
indvdum@0
  1892
			}
indvdum@0
  1893
			else if (!$this->data['user_allow_viewonline'])
indvdum@0
  1894
			{
indvdum@0
  1895
				// the user wants to hide and is allowed to  -> cloaking device on.
indvdum@0
  1896
				if ($auth->acl_get('u_hideonline'))
indvdum@0
  1897
				{
indvdum@0
  1898
					$sql = 'UPDATE ' . SESSIONS_TABLE . '
indvdum@0
  1899
						SET session_viewonline = 0
indvdum@0
  1900
						WHERE session_user_id = ' . $this->data['user_id'];
indvdum@0
  1901
					$db->sql_query($sql);
indvdum@0
  1902
					$this->data['session_viewonline'] = 0;
indvdum@0
  1903
				}
indvdum@0
  1904
			}
indvdum@0
  1905
		}
indvdum@0
  1906
indvdum@0
  1907
indvdum@0
  1908
		// Does the user need to change their password? If so, redirect to the
indvdum@0
  1909
		// ucp profile reg_details page ... of course do not redirect if we're already in the ucp
indvdum@0
  1910
		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))
indvdum@0
  1911
		{
indvdum@0
  1912
			if (strpos($this->page['query_string'], 'mode=reg_details') === false && $this->page['page_name'] != "ucp.$phpEx")
indvdum@0
  1913
			{
indvdum@0
  1914
				redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=profile&amp;mode=reg_details'));
indvdum@0
  1915
			}
indvdum@0
  1916
		}
indvdum@0
  1917
indvdum@0
  1918
		return;
indvdum@0
  1919
	}
indvdum@0
  1920
indvdum@0
  1921
	/**
indvdum@0
  1922
	* More advanced language substitution
indvdum@0
  1923
	* Function to mimic sprintf() with the possibility of using phpBB's language system to substitute nullar/singular/plural forms.
indvdum@0
  1924
	* Params are the language key and the parameters to be substituted.
indvdum@0
  1925
	* This function/functionality is inspired by SHS` and Ashe.
indvdum@0
  1926
	*
indvdum@0
  1927
	* Example call: <samp>$user->lang('NUM_POSTS_IN_QUEUE', 1);</samp>
indvdum@0
  1928
	*/
indvdum@0
  1929
	function lang()
indvdum@0
  1930
	{
indvdum@0
  1931
		$args = func_get_args();
indvdum@0
  1932
		$key = $args[0];
indvdum@0
  1933
indvdum@0
  1934
		if (is_array($key))
indvdum@0
  1935
		{
indvdum@0
  1936
			$lang = &$this->lang[array_shift($key)];
indvdum@0
  1937
indvdum@0
  1938
			foreach ($key as $_key)
indvdum@0
  1939
			{
indvdum@0
  1940
				$lang = &$lang[$_key];
indvdum@0
  1941
			}
indvdum@0
  1942
		}
indvdum@0
  1943
		else
indvdum@0
  1944
		{
indvdum@0
  1945
			$lang = &$this->lang[$key];
indvdum@0
  1946
		}
indvdum@0
  1947
indvdum@0
  1948
		// Return if language string does not exist
indvdum@0
  1949
		if (!isset($lang) || (!is_string($lang) && !is_array($lang)))
indvdum@0
  1950
		{
indvdum@0
  1951
			return $key;
indvdum@0
  1952
		}
indvdum@0
  1953
indvdum@0
  1954
		// If the language entry is a string, we simply mimic sprintf() behaviour
indvdum@0
  1955
		if (is_string($lang))
indvdum@0
  1956
		{
indvdum@0
  1957
			if (sizeof($args) == 1)
indvdum@0
  1958
			{
indvdum@0
  1959
				return $lang;
indvdum@0
  1960
			}
indvdum@0
  1961
indvdum@0
  1962
			// Replace key with language entry and simply pass along...
indvdum@0
  1963
			$args[0] = $lang;
indvdum@0
  1964
			return call_user_func_array('sprintf', $args);
indvdum@0
  1965
		}
indvdum@0
  1966
indvdum@0
  1967
		// It is an array... now handle different nullar/singular/plural forms
indvdum@0
  1968
		$key_found = false;
indvdum@0
  1969
indvdum@0
  1970
		// We now get the first number passed and will select the key based upon this number
indvdum@0
  1971
		for ($i = 1, $num_args = sizeof($args); $i < $num_args; $i++)
indvdum@0
  1972
		{
indvdum@0
  1973
			if (is_int($args[$i]))
indvdum@0
  1974
			{
indvdum@0
  1975
				$numbers = array_keys($lang);
indvdum@0
  1976
indvdum@0
  1977
				foreach ($numbers as $num)
indvdum@0
  1978
				{
indvdum@0
  1979
					if ($num > $args[$i])
indvdum@0
  1980
					{
indvdum@0
  1981
						break;
indvdum@0
  1982
					}
indvdum@0
  1983
indvdum@0
  1984
					$key_found = $num;
indvdum@0
  1985
				}
indvdum@0
  1986
				break;
indvdum@0
  1987
			}
indvdum@0
  1988
		}
indvdum@0
  1989
indvdum@0
  1990
		// Ok, let's check if the key was found, else use the last entry (because it is mostly the plural form)
indvdum@0
  1991
		if ($key_found === false)
indvdum@0
  1992
		{
indvdum@0
  1993
			$numbers = array_keys($lang);
indvdum@0
  1994
			$key_found = end($numbers);
indvdum@0
  1995
		}
indvdum@0
  1996
indvdum@0
  1997
		// Use the language string we determined and pass it to sprintf()
indvdum@0
  1998
		$args[0] = $lang[$key_found];
indvdum@0
  1999
		return call_user_func_array('sprintf', $args);
indvdum@0
  2000
	}
indvdum@0
  2001
indvdum@0
  2002
	/**
indvdum@0
  2003
	* Add Language Items - use_db and use_help are assigned where needed (only use them to force inclusion)
indvdum@0
  2004
	*
indvdum@0
  2005
	* @param mixed $lang_set specifies the language entries to include
indvdum@0
  2006
	* @param bool $use_db internal variable for recursion, do not use
indvdum@0
  2007
	* @param bool $use_help internal variable for recursion, do not use
indvdum@0
  2008
	*
indvdum@0
  2009
	* Examples:
indvdum@0
  2010
	* <code>
indvdum@0
  2011
	* $lang_set = array('posting', 'help' => 'faq');
indvdum@0
  2012
	* $lang_set = array('posting', 'viewtopic', 'help' => array('bbcode', 'faq'))
indvdum@0
  2013
	* $lang_set = array(array('posting', 'viewtopic'), 'help' => array('bbcode', 'faq'))
indvdum@0
  2014
	* $lang_set = 'posting'
indvdum@0
  2015
	* $lang_set = array('help' => 'faq', 'db' => array('help:faq', 'posting'))
indvdum@0
  2016
	* </code>
indvdum@0
  2017
	*/
indvdum@0
  2018
	function add_lang($lang_set, $use_db = false, $use_help = false)
indvdum@0
  2019
	{
indvdum@0
  2020
		global $phpEx;
indvdum@0
  2021
indvdum@0
  2022
		if (is_array($lang_set))
indvdum@0
  2023
		{
indvdum@0
  2024
			foreach ($lang_set as $key => $lang_file)
indvdum@0
  2025
			{
indvdum@0
  2026
				// Please do not delete this line.
indvdum@0
  2027
				// We have to force the type here, else [array] language inclusion will not work
indvdum@0
  2028
				$key = (string) $key;
indvdum@0
  2029
indvdum@0
  2030
				if ($key == 'db')
indvdum@0
  2031
				{
indvdum@0
  2032
					$this->add_lang($lang_file, true, $use_help);
indvdum@0
  2033
				}
indvdum@0
  2034
				else if ($key == 'help')
indvdum@0
  2035
				{
indvdum@0
  2036
					$this->add_lang($lang_file, $use_db, true);
indvdum@0
  2037
				}
indvdum@0
  2038
				else if (!is_array($lang_file))
indvdum@0
  2039
				{
indvdum@0
  2040
					$this->set_lang($this->lang, $this->help, $lang_file, $use_db, $use_help);
indvdum@0
  2041
				}
indvdum@0
  2042
				else
indvdum@0
  2043
				{
indvdum@0
  2044
					$this->add_lang($lang_file, $use_db, $use_help);
indvdum@0
  2045
				}
indvdum@0
  2046
			}
indvdum@0
  2047
			unset($lang_set);
indvdum@0
  2048
		}
indvdum@0
  2049
		else if ($lang_set)
indvdum@0
  2050
		{
indvdum@0
  2051
			$this->set_lang($this->lang, $this->help, $lang_set, $use_db, $use_help);
indvdum@0
  2052
		}
indvdum@0
  2053
	}
indvdum@0
  2054
indvdum@0
  2055
	/**
indvdum@0
  2056
	* Set language entry (called by add_lang)
indvdum@0
  2057
	* @access private
indvdum@0
  2058
	*/
indvdum@0
  2059
	function set_lang(&$lang, &$help, $lang_file, $use_db = false, $use_help = false)
indvdum@0
  2060
	{
indvdum@0
  2061
		global $phpEx;
indvdum@0
  2062
indvdum@0
  2063
		// Make sure the language name is set (if the user setup did not happen it is not set)
indvdum@0
  2064
		if (!$this->lang_name)
indvdum@0
  2065
		{
indvdum@0
  2066
			global $config;
indvdum@0
  2067
			$this->lang_name = basename($config['default_lang']);
indvdum@0
  2068
		}
indvdum@0
  2069
indvdum@0
  2070
		// $lang == $this->lang
indvdum@0
  2071
		// $help == $this->help
indvdum@0
  2072
		// - add appropriate variables here, name them as they are used within the language file...
indvdum@0
  2073
		if (!$use_db)
indvdum@0
  2074
		{
indvdum@0
  2075
			if ($use_help && strpos($lang_file, '/') !== false)
indvdum@0
  2076
			{
indvdum@0
  2077
				$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;
indvdum@0
  2078
			}
indvdum@0
  2079
			else
indvdum@0
  2080
			{
indvdum@0
  2081
				$language_filename = $this->lang_path . $this->lang_name . '/' . (($use_help) ? 'help_' : '') . $lang_file . '.' . $phpEx;
indvdum@0
  2082
			}
indvdum@0
  2083
indvdum@0
  2084
			if (!file_exists($language_filename))
indvdum@0
  2085
			{
indvdum@0
  2086
				global $config;
indvdum@0
  2087
indvdum@0
  2088
				if ($this->lang_name == 'en')
indvdum@0
  2089
				{
indvdum@0
  2090
					// 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.
indvdum@0
  2091
					$language_filename = str_replace($this->lang_path . 'en', $this->lang_path . $this->data['user_lang'], $language_filename);
indvdum@0
  2092
					trigger_error('Language file ' . $language_filename . ' couldn\'t be opened.', E_USER_ERROR);
indvdum@0
  2093
				}
indvdum@0
  2094
				else if ($this->lang_name == basename($config['default_lang']))
indvdum@0
  2095
				{
indvdum@0
  2096
					// Fall back to the English Language
indvdum@0
  2097
					$this->lang_name = 'en';
indvdum@0
  2098
					$this->set_lang($lang, $help, $lang_file, $use_db, $use_help);
indvdum@0
  2099
				}
indvdum@0
  2100
				else if ($this->lang_name == $this->data['user_lang'])
indvdum@0
  2101
				{
indvdum@0
  2102
					// Fall back to the board default language
indvdum@0
  2103
					$this->lang_name = basename($config['default_lang']);
indvdum@0
  2104
					$this->set_lang($lang, $help, $lang_file, $use_db, $use_help);
indvdum@0
  2105
				}
indvdum@0
  2106
indvdum@0
  2107
				// Reset the lang name
indvdum@0
  2108
				$this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']);
indvdum@0
  2109
				return;
indvdum@0
  2110
			}
indvdum@0
  2111
indvdum@0
  2112
			// Do not suppress error if in DEBUG_EXTRA mode
indvdum@0
  2113
			$include_result = (defined('DEBUG_EXTRA')) ? (include $language_filename) : (@include $language_filename);
indvdum@0
  2114
indvdum@0
  2115
			if ($include_result === false)
indvdum@0
  2116
			{
indvdum@0
  2117
				trigger_error('Language file ' . $language_filename . ' couldn\'t be opened.', E_USER_ERROR);
indvdum@0
  2118
			}
indvdum@0
  2119
		}
indvdum@0
  2120
		else if ($use_db)
indvdum@0
  2121
		{
indvdum@0
  2122
			// Get Database Language Strings
indvdum@0
  2123
			// Put them into $lang if nothing is prefixed, put them into $help if help: is prefixed
indvdum@0
  2124
			// For example: help:faq, posting
indvdum@0
  2125
		}
indvdum@0
  2126
	}
indvdum@0
  2127
indvdum@0
  2128
	/**
indvdum@0
  2129
	* Format user date
indvdum@0
  2130
	*
indvdum@0
  2131
	* @param int $gmepoch unix timestamp
indvdum@0
  2132
	* @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.
indvdum@0
  2133
	* @param bool $forcedate force non-relative date format.
indvdum@0
  2134
	*
indvdum@0
  2135
	* @return mixed translated date
indvdum@0
  2136
	*/
indvdum@0
  2137
	function format_date($gmepoch, $format = false, $forcedate = false)
indvdum@0
  2138
	{
indvdum@0
  2139
		static $midnight;
indvdum@0
  2140
		static $date_cache;
indvdum@0
  2141
indvdum@0
  2142
		$format = (!$format) ? $this->date_format : $format;
indvdum@0
  2143
		$now = time();
indvdum@0
  2144
		$delta = $now - $gmepoch;
indvdum@0
  2145
indvdum@0
  2146
		if (!isset($date_cache[$format]))
indvdum@0
  2147
		{
indvdum@0
  2148
			// Is the user requesting a friendly date format (i.e. 'Today 12:42')?
indvdum@0
  2149
			$date_cache[$format] = array(
indvdum@0
  2150
				'is_short'		=> strpos($format, '|'),
indvdum@0
  2151
				'format_short'	=> substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1),
indvdum@0
  2152
				'format_long'	=> str_replace('|', '', $format),
indvdum@0
  2153
				'lang'			=> $this->lang['datetime'],
indvdum@0
  2154
			);
indvdum@0
  2155
indvdum@0
  2156
			// Short representation of month in format? Some languages use different terms for the long and short format of May
indvdum@0
  2157
			if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
indvdum@0
  2158
			{
indvdum@0
  2159
				$date_cache[$format]['lang']['May'] = $this->lang['datetime']['May_short'];
indvdum@0
  2160
			}
indvdum@0
  2161
		}
indvdum@0
  2162
indvdum@0
  2163
		// Zone offset
indvdum@0
  2164
		$zone_offset = $this->timezone + $this->dst;
indvdum@0
  2165
indvdum@0
  2166
		// Show date <= 1 hour ago as 'xx min ago' but not greater than 60 seconds in the future
indvdum@0
  2167
		// A small tolerence is given for times in the future but in the same minute are displayed as '< than a minute ago'
indvdum@0
  2168
		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']))
indvdum@0
  2169
		{
indvdum@0
  2170
			return $this->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
indvdum@0
  2171
		}
indvdum@0
  2172
indvdum@0
  2173
		if (!$midnight)
indvdum@0
  2174
		{
indvdum@0
  2175
			list($d, $m, $y) = explode(' ', gmdate('j n Y', time() + $zone_offset));
indvdum@0
  2176
			$midnight = gmmktime(0, 0, 0, $m, $d, $y) - $zone_offset;
indvdum@0
  2177
		}
indvdum@0
  2178
indvdum@0
  2179
		if ($date_cache[$format]['is_short'] !== false && !$forcedate && !($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800))
indvdum@0
  2180
		{
indvdum@0
  2181
			$day = false;
indvdum@0
  2182
indvdum@0
  2183
			if ($gmepoch > $midnight + 86400)
indvdum@0
  2184
			{
indvdum@0
  2185
				$day = 'TOMORROW';
indvdum@0
  2186
			}
indvdum@0
  2187
			else if ($gmepoch > $midnight)
indvdum@0
  2188
			{
indvdum@0
  2189
				$day = 'TODAY';
indvdum@0
  2190
			}
indvdum@0
  2191
			else if ($gmepoch > $midnight - 86400)
indvdum@0
  2192
			{
indvdum@0
  2193
				$day = 'YESTERDAY';
indvdum@0
  2194
			}
indvdum@0
  2195
indvdum@0
  2196
			if ($day !== false)
indvdum@0
  2197
			{
indvdum@0
  2198
				return str_replace('||', $this->lang['datetime'][$day], strtr(@gmdate($date_cache[$format]['format_short'], $gmepoch + $zone_offset), $date_cache[$format]['lang']));
indvdum@0
  2199
			}
indvdum@0
  2200
		}
indvdum@0
  2201
indvdum@0
  2202
		return strtr(@gmdate($date_cache[$format]['format_long'], $gmepoch + $zone_offset), $date_cache[$format]['lang']);
indvdum@0
  2203
	}
indvdum@0
  2204
indvdum@0
  2205
	/**
indvdum@0
  2206
	* Get language id currently used by the user
indvdum@0
  2207
	*/
indvdum@0
  2208
	function get_iso_lang_id()
indvdum@0
  2209
	{
indvdum@0
  2210
		global $config, $db;
indvdum@0
  2211
indvdum@0
  2212
		if (!empty($this->lang_id))
indvdum@0
  2213
		{
indvdum@0
  2214
			return $this->lang_id;
indvdum@0
  2215
		}
indvdum@0
  2216
indvdum@0
  2217
		if (!$this->lang_name)
indvdum@0
  2218
		{
indvdum@0
  2219
			$this->lang_name = $config['default_lang'];
indvdum@0
  2220
		}
indvdum@0
  2221
indvdum@0
  2222
		$sql = 'SELECT lang_id
indvdum@0
  2223
			FROM ' . LANG_TABLE . "
indvdum@0
  2224
			WHERE lang_iso = '" . $db->sql_escape($this->lang_name) . "'";
indvdum@0
  2225
		$result = $db->sql_query($sql);
indvdum@0
  2226
		$this->lang_id = (int) $db->sql_fetchfield('lang_id');
indvdum@0
  2227
		$db->sql_freeresult($result);
indvdum@0
  2228
indvdum@0
  2229
		return $this->lang_id;
indvdum@0
  2230
	}
indvdum@0
  2231
indvdum@0
  2232
	/**
indvdum@0
  2233
	* Get users profile fields
indvdum@0
  2234
	*/
indvdum@0
  2235
	function get_profile_fields($user_id)
indvdum@0
  2236
	{
indvdum@0
  2237
		global $db;
indvdum@0
  2238
indvdum@0
  2239
		if (isset($this->profile_fields))
indvdum@0
  2240
		{
indvdum@0
  2241
			return;
indvdum@0
  2242
		}
indvdum@0
  2243
indvdum@0
  2244
		$sql = 'SELECT *
indvdum@0
  2245
			FROM ' . PROFILE_FIELDS_DATA_TABLE . "
indvdum@0
  2246
			WHERE user_id = $user_id";
indvdum@0
  2247
		$result = $db->sql_query_limit($sql, 1);
indvdum@0
  2248
		$this->profile_fields = (!($row = $db->sql_fetchrow($result))) ? array() : $row;
indvdum@0
  2249
		$db->sql_freeresult($result);
indvdum@0
  2250
	}
indvdum@0
  2251
indvdum@0
  2252
	/**
indvdum@0
  2253
	* Specify/Get image
indvdum@0
  2254
	* $suffix is no longer used - we know it. ;) It is there for backward compatibility.
indvdum@0
  2255
	*/
indvdum@0
  2256
	function img($img, $alt = '', $width = false, $suffix = '', $type = 'full_tag')
indvdum@0
  2257
	{
indvdum@0
  2258
		static $imgs;
indvdum@0
  2259
		global $phpbb_root_path;
indvdum@0
  2260
indvdum@0
  2261
		$img_data = &$imgs[$img];
indvdum@0
  2262
indvdum@0
  2263
		if (empty($img_data))
indvdum@0
  2264
		{
indvdum@0
  2265
			if (!isset($this->img_array[$img]))
indvdum@0
  2266
			{
indvdum@0
  2267
				// Do not fill the image to let designers decide what to do if the image is empty
indvdum@0
  2268
				$img_data = '';
indvdum@0
  2269
				return $img_data;
indvdum@0
  2270
			}
indvdum@0
  2271
indvdum@0
  2272
			// Use URL if told so
indvdum@0
  2273
			$root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
indvdum@0
  2274
indvdum@0
  2275
			$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'];
indvdum@0
  2276
indvdum@0
  2277
			$img_data['src'] = $root_path . $path;
indvdum@0
  2278
			$img_data['width'] = $this->img_array[$img]['image_width'];
indvdum@0
  2279
			$img_data['height'] = $this->img_array[$img]['image_height'];
indvdum@0
  2280
indvdum@0
  2281
			// We overwrite the width and height to the phpbb logo's width
indvdum@0
  2282
			// and height here if the contents of the site_logo file are
indvdum@0
  2283
			// really equal to the phpbb_logo
indvdum@0
  2284
			// This allows us to change the dimensions of the phpbb_logo without
indvdum@0
  2285
			// modifying the imageset.cfg and causing a conflict for everyone
indvdum@0
  2286
			// who modified it for their custom logo on updating
indvdum@0
  2287
			if ($img == 'site_logo' && file_exists($phpbb_root_path . $path))
indvdum@0
  2288
			{
indvdum@0
  2289
				global $cache;
indvdum@0
  2290
indvdum@0
  2291
				$img_file_hashes = $cache->get('imageset_site_logo_md5');
indvdum@0
  2292
indvdum@0
  2293
				if ($img_file_hashes === false)
indvdum@0
  2294
				{
indvdum@0
  2295
					$img_file_hashes = array();
indvdum@0
  2296
				}
indvdum@0
  2297
indvdum@0
  2298
				$key = $this->theme['imageset_path'] . '::' . $this->img_array[$img]['image_lang'];
indvdum@0
  2299
				if (!isset($img_file_hashes[$key]))
indvdum@0
  2300
				{
indvdum@0
  2301
					$img_file_hashes[$key] = md5(file_get_contents($phpbb_root_path . $path));
indvdum@0
  2302
					$cache->put('imageset_site_logo_md5', $img_file_hashes);
indvdum@0
  2303
				}
indvdum@0
  2304
indvdum@0
  2305
				$phpbb_logo_hash = '0c461a32cd3621643105f0d02a772c10';
indvdum@0
  2306
indvdum@0
  2307
				if ($phpbb_logo_hash == $img_file_hashes[$key])
indvdum@0
  2308
				{
indvdum@0
  2309
					$img_data['width'] = '149';
indvdum@0
  2310
					$img_data['height'] = '52';
indvdum@0
  2311
				}
indvdum@0
  2312
			}
indvdum@0
  2313
		}
indvdum@0
  2314
indvdum@0
  2315
		$alt = (!empty($this->lang[$alt])) ? $this->lang[$alt] : $alt;
indvdum@0
  2316
indvdum@0
  2317
		switch ($type)
indvdum@0
  2318
		{
indvdum@0
  2319
			case 'src':
indvdum@0
  2320
				return $img_data['src'];
indvdum@0
  2321
			break;
indvdum@0
  2322
indvdum@0
  2323
			case 'width':
indvdum@0
  2324
				return ($width === false) ? $img_data['width'] : $width;
indvdum@0
  2325
			break;
indvdum@0
  2326
indvdum@0
  2327
			case 'height':
indvdum@0
  2328
				return $img_data['height'];
indvdum@0
  2329
			break;
indvdum@0
  2330
indvdum@0
  2331
			default:
indvdum@0
  2332
				$use_width = ($width === false) ? $img_data['width'] : $width;
indvdum@0
  2333
indvdum@0
  2334
				return '<img src="' . $img_data['src'] . '"' . (($use_width) ? ' width="' . $use_width . '"' : '') . (($img_data['height']) ? ' height="' . $img_data['height'] . '"' : '') . ' alt="' . $alt . '" title="' . $alt . '" />';
indvdum@0
  2335
			break;
indvdum@0
  2336
		}
indvdum@0
  2337
	}
indvdum@0
  2338
indvdum@0
  2339
	/**
indvdum@0
  2340
	* Get option bit field from user options
indvdum@0
  2341
	*/
indvdum@0
  2342
	function optionget($key, $data = false)
indvdum@0
  2343
	{
indvdum@0
  2344
		if (!isset($this->keyvalues[$key]))
indvdum@0
  2345
		{
indvdum@0
  2346
			$var = ($data) ? $data : $this->data['user_options'];
indvdum@0
  2347
			$this->keyvalues[$key] = ($var & 1 << $this->keyoptions[$key]) ? true : false;
indvdum@0
  2348
		}
indvdum@0
  2349
indvdum@0
  2350
		return $this->keyvalues[$key];
indvdum@0
  2351
	}
indvdum@0
  2352
indvdum@0
  2353
	/**
indvdum@0
  2354
	* Set option bit field for user options
indvdum@0
  2355
	*/
indvdum@0
  2356
	function optionset($key, $value, $data = false)
indvdum@0
  2357
	{
indvdum@0
  2358
		$var = ($data) ? $data : $this->data['user_options'];
indvdum@0
  2359
indvdum@0
  2360
		if ($value && !($var & 1 << $this->keyoptions[$key]))
indvdum@0
  2361
		{
indvdum@0
  2362
			$var += 1 << $this->keyoptions[$key];
indvdum@0
  2363
		}
indvdum@0
  2364
		else if (!$value && ($var & 1 << $this->keyoptions[$key]))
indvdum@0
  2365
		{
indvdum@0
  2366
			$var -= 1 << $this->keyoptions[$key];
indvdum@0
  2367
		}
indvdum@0
  2368
		else
indvdum@0
  2369
		{
indvdum@0
  2370
			return ($data) ? $var : false;
indvdum@0
  2371
		}
indvdum@0
  2372
indvdum@0
  2373
		if (!$data)
indvdum@0
  2374
		{
indvdum@0
  2375
			$this->data['user_options'] = $var;
indvdum@0
  2376
			return true;
indvdum@0
  2377
		}
indvdum@0
  2378
		else
indvdum@0
  2379
		{
indvdum@0
  2380
			return $var;
indvdum@0
  2381
		}
indvdum@0
  2382
	}
indvdum@0
  2383
indvdum@0
  2384
	/**
indvdum@0
  2385
	* Funtion to make the user leave the NEWLY_REGISTERED system group.
indvdum@0
  2386
	* @access public
indvdum@0
  2387
	*/
indvdum@0
  2388
	function leave_newly_registered()
indvdum@0
  2389
	{
indvdum@0
  2390
		global $db;
indvdum@0
  2391
indvdum@0
  2392
		if (empty($this->data['user_new']))
indvdum@0
  2393
		{
indvdum@0
  2394
			return false;
indvdum@0
  2395
		}
indvdum@0
  2396
indvdum@0
  2397
		if (!function_exists('remove_newly_registered'))
indvdum@0
  2398
		{
indvdum@0
  2399
			global $phpbb_root_path, $phpEx;
indvdum@0
  2400
indvdum@0
  2401
			include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
indvdum@0
  2402
		}
indvdum@0
  2403
		if ($group = remove_newly_registered($this->data['user_id'], $this->data))
indvdum@0
  2404
		{
indvdum@0
  2405
			$this->data['group_id'] = $group;
indvdum@0
  2406
indvdum@0
  2407
		}
indvdum@0
  2408
		$this->data['user_permissions'] = '';
indvdum@0
  2409
		$this->data['user_new'] = 0;
indvdum@0
  2410
indvdum@0
  2411
		return true;
indvdum@0
  2412
	}
indvdum@0
  2413
indvdum@0
  2414
	/**
indvdum@0
  2415
	* Returns all password protected forum ids the user is currently NOT authenticated for.
indvdum@0
  2416
	*
indvdum@0
  2417
	* @return array		Array of forum ids
indvdum@0
  2418
	* @access public
indvdum@0
  2419
	*/
indvdum@0
  2420
	function get_passworded_forums()
indvdum@0
  2421
	{
indvdum@0
  2422
		global $db;
indvdum@0
  2423
indvdum@0
  2424
		$sql = 'SELECT f.forum_id, fa.user_id
indvdum@0
  2425
			FROM ' . FORUMS_TABLE . ' f
indvdum@0
  2426
			LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa
indvdum@0
  2427
				ON (fa.forum_id = f.forum_id
indvdum@0
  2428
					AND fa.session_id = '" . $db->sql_escape($this->session_id) . "')
indvdum@0
  2429
			WHERE f.forum_password <> ''";
indvdum@0
  2430
		$result = $db->sql_query($sql);
indvdum@0
  2431
indvdum@0
  2432
		$forum_ids = array();
indvdum@0
  2433
		while ($row = $db->sql_fetchrow($result))
indvdum@0
  2434
		{
indvdum@0
  2435
			$forum_id = (int) $row['forum_id'];
indvdum@0
  2436
indvdum@0
  2437
			if ($row['user_id'] != $this->data['user_id'])
indvdum@0
  2438
			{
indvdum@0
  2439
				$forum_ids[$forum_id] = $forum_id;
indvdum@0
  2440
			}
indvdum@0
  2441
		}
indvdum@0
  2442
		$db->sql_freeresult($result);
indvdum@0
  2443
indvdum@0
  2444
		return $forum_ids;
indvdum@0
  2445
	}
indvdum@0
  2446
}
indvdum@0
  2447
indvdum@0
  2448
?>