1<?php
2/**
3 * Loads common data and performs various functions necessary for the site to work properly.
4 *
5 * @copyright (C) 2008-2012 PunBB, partially based on code (C) 2008-2009 FluxBB.org
6 * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
7 * @package PunBB
8 */
9
10
11if (!defined('FORUM_ROOT'))
12	exit('The constant FORUM_ROOT must be defined and point to a valid PunBB installation root directory.');
13
14if (!defined('FORUM_ESSENTIALS_LOADED'))
15	require FORUM_ROOT.'include/essentials.php';
16
17// Strip slashes from GET/POST/COOKIE (if magic_quotes_gpc is enabled), since 5.4.0 always false
18if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
19{
20	function stripslashes_array($array)
21	{
22		return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
23	}
24
25	$_GET = stripslashes_array($_GET);
26	$_POST = stripslashes_array($_POST);
27	$_COOKIE = stripslashes_array($_COOKIE);
28}
29
30// Strip out "bad" UTF-8 characters
31forum_remove_bad_characters();
32
33// If a cookie name is not specified in config.php, we use the default (forum_cookie)
34if (empty($cookie_name))
35	$cookie_name = 'forum_cookie';
36
37// Enable output buffering
38if (!defined('FORUM_DISABLE_BUFFERING'))
39{
40	// For some very odd reason, "Norton Internet Security" unsets this
41	$_SERVER['HTTP_ACCEPT_ENCODING'] = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
42
43	// Should we use gzip output compression?
44	if ($forum_config['o_gzip'] && extension_loaded('zlib') && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false || strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') !== false))
45		ob_start('ob_gzhandler');
46	else
47		ob_start();
48}
49
50// Define standard date/time formats
51$forum_time_formats = array($forum_config['o_time_format'], 'H:i:s', 'H:i', 'g:i:s a', 'g:i a');
52$forum_date_formats = array($forum_config['o_date_format'], 'Y-m-d', 'Y-d-m', 'd-m-Y', 'm-d-Y', 'M j Y', 'jS M Y');
53
54// Create forum_page array
55$forum_page = array();
56
57// Login and fetch user info
58$forum_user = array();
59cookie_login($forum_user);
60
61// Attempt to load the common language file
62if (file_exists(FORUM_ROOT.'lang/'.$forum_user['language'].'/common.php'))
63	include FORUM_ROOT.'lang/'.$forum_user['language'].'/common.php';
64else
65	error('There is no valid language pack \''.forum_htmlencode($forum_user['language']).'\' installed.<br />Please reinstall a language of that name.');
66
67// Setup the URL rewriting scheme
68if ($forum_config['o_sef'] != 'Default' && file_exists(FORUM_ROOT.'include/url/'.$forum_config['o_sef'].'/forum_urls.php'))
69	require FORUM_ROOT.'include/url/'.$forum_config['o_sef'].'/forum_urls.php';
70else
71	require FORUM_ROOT.'include/url/Default/forum_urls.php';
72
73// A good place to modify the URL scheme
74($hook = get_hook('co_modify_url_scheme')) ? eval($hook) : null;
75
76// Check if we are to display a maintenance message
77if ($forum_config['o_maintenance'] && $forum_user['g_id'] > FORUM_ADMIN && !defined('FORUM_TURN_OFF_MAINT'))
78	maintenance_message();
79
80// Load cached updates info
81if ($forum_user['g_id'] == FORUM_ADMIN)
82{
83	if (file_exists(FORUM_CACHE_DIR.'cache_updates.php'))
84		include FORUM_CACHE_DIR.'cache_updates.php';
85
86	// Regenerate cache only if automatic updates are enabled and if the cache is more than 12 hours old
87	if ($forum_config['o_check_for_updates'] == '1' && (!defined('FORUM_UPDATES_LOADED') || $forum_updates['cached'] < (time() - 43200)))
88	{
89		if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
90			require FORUM_ROOT.'include/cache.php';
91
92		generate_updates_cache();
93		require FORUM_CACHE_DIR.'cache_updates.php';
94	}
95}
96
97// Load cached bans
98if (file_exists(FORUM_CACHE_DIR.'cache_bans.php'))
99	include FORUM_CACHE_DIR.'cache_bans.php';
100
101if (!defined('FORUM_BANS_LOADED'))
102{
103	if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
104		require FORUM_ROOT.'include/cache.php';
105
106	generate_bans_cache();
107	require FORUM_CACHE_DIR.'cache_bans.php';
108}
109
110// Check if current user is banned
111check_bans();
112
113// Update online list
114update_users_online();
115
116// Check to see if we logged in without a cookie being set
117if ($forum_user['is_guest'] && isset($_GET['login']))
118	message($lang_common['No cookie']);
119
120// If we're an administrator or moderator, make sure the CSRF token in $_POST is valid (token in post.php is dealt with in post.php)
121if (!empty($_POST) && (isset($_POST['confirm_cancel']) || (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== generate_form_token(get_current_url()))) && !defined('FORUM_SKIP_CSRF_CONFIRM'))
122	csrf_confirm_form();
123
124
125($hook = get_hook('co_common')) ? eval($hook) : null;
126