aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/index.php
blob: 9a57105d5745df6ef83963b1f017084d847e7e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
*
* @package phpBB3
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

/**
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Handle the display of extension front pages
if ($ext = $request->variable('ext', ''))
{
	$class = 'phpbb_ext_' . str_replace('/', '_', $ext) . '_controller';

	if (!$phpbb_extension_manager->available($ext))
	{
		send_status_line(404, 'Not Found');
		trigger_error($user->lang('EXTENSION_DOES_NOT_EXIST', $ext));	
	}
	else if (!$phpbb_extension_manager->enabled($ext))
	{
		send_status_line(404, 'Not Found');
		trigger_error($user->lang('EXTENSION_DISABLED', $ext));
	}
	else if (!class_exists($class))
	{
		send_status_line(404, 'Not Found');
		trigger_error($user->lang('EXTENSION_CONTROLLER_MISSING', $ext));
	}

	$controller = new $class;

	if (!($controller instanceof phpbb_extension_controller_interface))
	{
		send_status_line(500, 'Internal Server Error');
		trigger_error($user->lang('EXTENSION_CLASS_WRONG_TYPE', $class));
	}

	$controller->handle();
	exit_handler();
}

include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

$user->add_lang('viewforum');

display_forums('', $config['load_moderators']);

$order_legend = ($config['legend_sort_groupname']) ? 'group_name' : 'group_legend';
// Grab group details for legend display
if ($auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
{
	$sql = 'SELECT group_id, group_name, group_colour, group_type, group_legend
		FROM ' . GROUPS_TABLE . '
		WHERE group_legend > 0
		ORDER BY ' . $order_legend . ' ASC';
}
else
{
	$sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend
		FROM ' . GROUPS_TABLE . ' g
		LEFT JOIN ' . USER_GROUP_TABLE . ' ug
			ON (
				g.group_id = ug.group_id
				AND ug.user_id = ' . $user->data['user_id'] . '
				AND ug.user_pending = 0
			)
		WHERE g.group_legend > 0
			AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $user->data['user_id'] . ')
		ORDER BY g.' . $order_legend . ' ASC';
}
$result = $db->sql_query($sql);

$legend = array();
while ($row = $db->sql_fetchrow($result))
{
	$colour_text = ($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . '"' : '';
	$group_name = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];

	if ($row['group_name'] == 'BOTS' || ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile')))
	{
		$legend[] = '<span' . $colour_text . '>' . $group_name . '</span>';
	}
	else
	{
		$legend[] = '<a' . $colour_text . ' href="' . append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&amp;g=' . $row['group_id']) . '">' . $group_name . '</a>';
	}
}
$db->sql_freeresult($result);

$legend = implode(', ', $legend);

// Generate birthday list if required ...
$birthday_list = array();
if ($config['load_birthdays'] && $config['allow_birthdays'] && $auth->acl_gets('u_viewprofile', 'a_user', 'a_useradd', 'a_userdel'))
{
	$now = phpbb_gmgetdate(time() + $user->timezone + $user->dst);

	// Display birthdays of 29th february on 28th february in non-leap-years
	$leap_year_birthdays = '';
	if ($now['mday'] == 28 && $now['mon'] == 2 && !$user->format_date(time(), 'L'))
	{
		$leap_year_birthdays = " OR u.user_birthday LIKE '" . $db->sql_escape(sprintf('%2d-%2d-', 29, 2)) . "%'";
	}

	$sql = 'SELECT u.user_id, u.username, u.user_colour, u.user_birthday
		FROM ' . USERS_TABLE . ' u
		LEFT JOIN ' . BANLIST_TABLE . " b ON (u.user_id = b.ban_userid)
		WHERE (b.ban_id IS NULL
			OR b.ban_exclude = 1)
			AND (u.user_birthday LIKE '" . $db->sql_escape(sprintf('%2d-%2d-', $now['mday'], $now['mon'])) . "%' $leap_year_birthdays)
			AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')';
	$result = $db->sql_query($sql);

	while ($row = $db->sql_fetchrow($result))
	{
		$birthday_username	= get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']);
		$birthday_year		= (int) substr($row['user_birthday'], -4);
		$birthday_age		= ($birthday_year) ? max(0, $now['year'] - $birthday_year) : '';

		$template->assign_block_vars('birthdays', array(
			'USERNAME'	=> $birthday_username,
			'AGE'		=> $birthday_age,
		));

		// For 3.0 compatibility
		if ($age = (int) substr($row['user_birthday'], -4))
		{
			$birthday_list[] = $birthday_username . (($birthday_year) ? ' (' . $birthday_age . ')' : '');
		}
	}
	$db->sql_freeresult($result);
}

// Assign index specific vars
$template->assign_vars(array(
	'TOTAL_POSTS'	=> $user->lang('TOTAL_POSTS_COUNT', (int) $config['num_posts']),
	'TOTAL_TOPICS'	=> $user->lang('TOTAL_TOPICS', (int) $config['num_topics']),
	'TOTAL_USERS'	=> $user->lang('TOTAL_USERS', (int) $config['num_users']),
	'NEWEST_USER'	=> $user->lang('NEWEST_USER', get_username_string('full', $config['newest_user_id'], $config['newest_username'], $config['newest_user_colour'])),

	'LEGEND'		=> $legend,
	'BIRTHDAY_LIST'	=> (empty($birthday_list)) ? '' : implode(', ', $birthday_list),

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_UNREAD_POSTS'),
	'FORUM_UNREAD_IMG'			=> $user->img('forum_unread', 'UNREAD_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_UNREAD_POSTS_LOCKED'),
	'FORUM_UNREAD_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'UNREAD_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,

	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
);

// Output page
page_header($user->lang['INDEX']);

$template->set_filenames(array(
	'body' => 'index_body.html')
);

page_footer();
ass="hl com"> * @param array $class_names An array of migration class names * @return null */ public function set_migrations($class_names) { $this->migrations = $class_names; } /** * Runs a single update step from the next migration to be applied. * * The update step can either be a schema or a (partial) data update. To * check if update() needs to be called again use the finished() method. * * @return null */ public function update() { foreach ($this->migrations as $name) { if (!isset($this->migration_state[$name]) || !$this->migration_state[$name]['migration_schema_done'] || !$this->migration_state[$name]['migration_data_done']) { if (!$this->try_apply($name)) { continue; } else { return; } } } } /** * Attempts to apply a step of the given migration or one of its dependencies * * @param string The class name of the migration * @return bool Whether any update step was successfully run */ protected function try_apply($name) { if (!class_exists($name)) { return false; } $migration = $this->get_migration($name); $state = (isset($this->migration_state[$name])) ? $this->migration_state[$name] : array( 'migration_depends_on' => $migration->depends_on(), 'migration_schema_done' => false, 'migration_data_done' => false, 'migration_data_state' => '', 'migration_start_time' => 0, 'migration_end_time' => 0, ); foreach ($state['migration_depends_on'] as $depend) { if ($this->unfulfillable($depend) !== false) { throw new \phpbb\db\migration\exception('MIGRATION_NOT_FULFILLABLE', $name, $depend); } if (!isset($this->migration_state[$depend]) || !$this->migration_state[$depend]['migration_schema_done'] || !$this->migration_state[$depend]['migration_data_done']) { return $this->try_apply($depend); } } $this->last_run_migration = array( 'name' => $name, 'class' => $migration, 'state' => $state, 'task' => '', ); if (!isset($this->migration_state[$name])) { if ($state['migration_start_time'] == 0 && $migration->effectively_installed()) { $state = array( 'migration_depends_on' => $migration->depends_on(), 'migration_schema_done' => true, 'migration_data_done' => true, 'migration_data_state' => '', 'migration_start_time' => 0, 'migration_end_time' => 0, ); $this->last_run_migration['effectively_installed'] = true; } else { $state['migration_start_time'] = time(); } } $this->set_migration_state($name, $state); if (!$state['migration_schema_done']) { $this->last_run_migration['task'] = 'apply_schema_changes'; $this->apply_schema_changes($migration->update_schema()); $state['migration_schema_done'] = true; } else if (!$state['migration_data_done']) { try { $this->last_run_migration['task'] = 'process_data_step'; $result = $this->process_data_step($migration->update_data(), $state['migration_data_state']); $state['migration_data_state'] = ($result === true) ? '' : $result; $state['migration_data_done'] = ($result === true); $state['migration_end_time'] = ($result === true) ? time() : 0; } catch (\phpbb\db\migration\exception $e) { // Revert the schema changes $this->revert($name); // Rethrow exception throw $e; } } $this->set_migration_state($name, $state); return true; } /** * Runs a single revert step from the last migration installed * * YOU MUST ADD/SET ALL MIGRATIONS THAT COULD BE DEPENDENT ON THE MIGRATION TO REVERT TO BEFORE CALLING THIS METHOD! * The revert step can either be a schema or a (partial) data revert. To * check if revert() needs to be called again use the migration_state() method. * * @param string $migration String migration name to revert (including any that depend on this migration) * @return null */ public function revert($migration) { if (!isset($this->migration_state[$migration])) { // Not installed return; } foreach ($this->migration_state as $name => $state) { if (!empty($state['migration_depends_on']) && in_array($migration, $state['migration_depends_on'])) { $this->revert($name); } } $this->try_revert($migration); } /** * Attempts to revert a step of the given migration or one of its dependencies * * @param string The class name of the migration * @return bool Whether any update step was successfully run */ protected function try_revert($name) { if (!class_exists($name)) { return false; } $migration = $this->get_migration($name); $state = $this->migration_state[$name]; $this->last_run_migration = array( 'name' => $name, 'class' => $migration, 'task' => '', ); if ($state['migration_data_done']) { if ($state['migration_data_state'] !== 'revert_data') { $result = $this->process_data_step($migration->update_data(), $state['migration_data_state'], true); $state['migration_data_state'] = ($result === true) ? 'revert_data' : $result; } else { $result = $this->process_data_step($migration->revert_data(), '', false); $state['migration_data_state'] = ($result === true) ? '' : $result; $state['migration_data_done'] = ($result === true) ? false : true; } $this->set_migration_state($name, $state); } else { $this->apply_schema_changes($migration->revert_schema()); $sql = 'DELETE FROM ' . $this->migrations_table . " WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; $this->db->sql_query($sql); unset($this->migration_state[$name]); } return true; } /** * Apply schema changes from a migration * * Just calls db_tools->perform_schema_changes * * @param array $schema_changes from migration */ protected function apply_schema_changes($schema_changes) { $this->db_tools->perform_schema_changes($schema_changes); } /** * Process the data step of the migration * * @param array $steps The steps to run * @param bool|string $state Current state of the migration * @param bool $revert true to revert a data step * @return bool|string migration state. True if completed, serialized array if not finished */ protected function process_data_step($steps, $state, $revert = false) { $state = ($state) ? unserialize($state) : false; // reverse order of steps if reverting if ($revert === true) { $steps = array_reverse($steps); } foreach ($steps as $step_identifier => $step) { $last_result = 0; if ($state) { // Continue until we reach the step that matches the last step called if ($state['step'] != $step_identifier) { continue; } // We send the result from last time to the callable function $last_result = $state['result']; // Set state to false since we reached the point we were at $state = false; } try { // Result will be null or true if everything completed correctly $result = $this->run_step($step, $last_result, $revert); if ($result !== null && $result !== true) { return serialize(array( 'result' => $result, 'step' => $step_identifier, )); } } catch (\phpbb\db\migration\exception $e) { // We should try rolling back here foreach ($steps as $reverse_step_identifier => $reverse_step) { // If we've reached the current step we can break because we reversed everything that was run if ($reverse_step_identifier == $step_identifier) { break; } // Reverse the step that was run $result = $this->run_step($reverse_step, false, !$revert); } // rethrow the exception throw $e; } } return true; } /** * Run a single step * * An exception should be thrown if an error occurs * * @param mixed $step Data step from migration * @param mixed $last_result Result to pass to the callable (only for 'custom' method) * @param bool $reverse False to install, True to attempt uninstallation by reversing the call * @return null */ protected function run_step($step, $last_result = 0, $reverse = false) { $callable_and_parameters = $this->get_callable_from_step($step, $last_result, $reverse); if ($callable_and_parameters === false) { return; } $callable = $callable_and_parameters[0]; $parameters = $callable_and_parameters[1]; return call_user_func_array($callable, $parameters); } /** * Get a callable statement from a data step * * @param array $step Data step from migration * @param mixed $last_result Result to pass to the callable (only for 'custom' method) * @param bool $reverse False to install, True to attempt uninstallation by reversing the call * @return array Array with parameters for call_user_func_array(), 0 is the callable, 1 is parameters */ protected function get_callable_from_step(array $step, $last_result = 0, $reverse = false) { $type = $step[0]; $parameters = $step[1]; $parts = explode('.', $type); $class = $parts[0]; $method = false; if (isset($parts[1])) { $method = $parts[1]; } switch ($class) { case 'if': if (!isset($parameters[0])) { throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_MISSING_CONDITION', $step); } if (!isset($parameters[1])) { throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_MISSING_STEP', $step); } $condition = $parameters[0]; if (!$condition) { return false; } $step = $parameters[1]; return $this->get_callable_from_step($step); break; case 'custom': if (!is_callable($parameters[0])) { throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_CUSTOM_NOT_CALLABLE', $step); } return array( $parameters[0], array($last_result), ); break; default: if (!$method) { throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_UNKNOWN_TYPE', $step); } if (!isset($this->tools[$class])) { throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_UNDEFINED_TOOL', $step); } if (!method_exists(get_class($this->tools[$class]), $method)) { throw new \phpbb\db\migration\exception('MIGRATION_INVALID_DATA_UNDEFINED_METHOD', $step); } // Attempt to reverse operations if ($reverse) { array_unshift($parameters, $method); return array( array($this->tools[$class], 'reverse'), $parameters, ); } return array( array($this->tools[$class], $method), $parameters, ); break; } } /** * Insert/Update migration row into the database * * @param string $name Name of the migration * @param array $state * @return null */ protected function set_migration_state($name, $state) { $migration_row = $state; $migration_row['migration_depends_on'] = serialize($state['migration_depends_on']); if (isset($this->migration_state[$name])) { $sql = 'UPDATE ' . $this->migrations_table . ' SET ' . $this->db->sql_build_array('UPDATE', $migration_row) . " WHERE migration_name = '" . $this->db->sql_escape($name) . "'"; $this->db->sql_query($sql); } else { $migration_row['migration_name'] = $name; $sql = 'INSERT INTO ' . $this->migrations_table . ' ' . $this->db->sql_build_array('INSERT', $migration_row); $this->db->sql_query($sql); } $this->migration_state[$name] = $state; $this->last_run_migration['state'] = $state; } /** * Checks if a migration's dependencies can even theoretically be satisfied. * * @param string $name The class name of the migration * @return bool|string False if fulfillable, string of missing migration name if unfulfillable */ public function unfulfillable($name) { if (isset($this->migration_state[$name])) { return false; } if (!class_exists($name)) { return $name; } $migration = $this->get_migration($name); $depends = $migration->depends_on(); foreach ($depends as $depend) { $unfulfillable = $this->unfulfillable($depend); if ($unfulfillable !== false) { return $unfulfillable; } } return false; } /** * Checks whether all available, fulfillable migrations have been applied. * * @return bool Whether the migrations have been applied */ public function finished() { foreach ($this->migrations as $name) { if (!isset($this->migration_state[$name])) { // skip unfulfillable migrations, but fulfillables mean we // are not finished yet if ($this->unfulfillable($name) !== false) { continue; } return false; } $migration = $this->migration_state[$name]; if (!$migration['migration_schema_done'] || !$migration['migration_data_done']) { return false; } } return true; } /** * Gets a migration state (whether it is installed and to what extent) * * @param string $migration String migration name to check if it is installed * @return bool|array False if the migration has not at all been installed, array */ public function migration_state($migration) { if (!isset($this->migration_state[$migration])) { return false; } return $this->migration_state[$migration]; } /** * Helper to get a migration * * @param string $name Name of the migration * @return \phpbb\db\migration\migration */ protected function get_migration($name) { return new $name($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix); } /** * This function adds all migrations sent to it to the migrations table * * THIS SHOULD NOT GENERALLY BE USED! THIS IS FOR THE PHPBB INSTALLER. * THIS WILL THROW ERRORS IF MIGRATIONS ALREADY EXIST IN THE TABLE, DO NOT CALL MORE THAN ONCE! * * @param array $migrations Array of migrations (names) to add to the migrations table * @return null */ public function populate_migrations($migrations) { foreach ($migrations as $name) { if ($this->migration_state($name) === false) { $state = array( 'migration_depends_on' => $name::depends_on(), 'migration_schema_done' => true, 'migration_data_done' => true, 'migration_data_state' => '', 'migration_start_time' => time(), 'migration_end_time' => time(), ); $this->set_migration_state($name, $state); } } } /** * Load migration data files from a directory * * @param \phpbb\extension\finder $finder * @param string $path Path to migration data files * @param bool $check_fulfillable If TRUE (default), we will check * if all of the migrations are fulfillable after loading them. * If FALSE, we will not check. You SHOULD check at least once * to prevent errors (if including multiple directories, check * with the last call to prevent throwing errors unnecessarily). * @return array Array of migration names */ public function load_migrations(\phpbb\extension\finder $finder, $path, $check_fulfillable = true) { if (!is_dir($path)) { throw new \phpbb\db\migration\exception('DIRECTORY INVALID', $path); } $migrations = array(); $files = $finder ->extension_directory("/") ->find_from_paths(array('/' => $path)); foreach ($files as $file) { $migrations[$file['path'] . $file['filename']] = ''; } $migrations = $finder->get_classes_from_files($migrations); foreach ($migrations as $migration) { if (!in_array($migration, $this->migrations)) { $this->migrations[] = $migration; } } if ($check_fulfillable) { foreach ($this->migrations as $name) { $unfulfillable = $this->unfulfillable($name); if ($unfulfillable !== false) { throw new \phpbb\db\migration\exception('MIGRATION_NOT_FULFILLABLE', $name, $unfulfillable); } } } return $this->migrations; } }