diff --git a/phpBB/docs/CHANGELOG.html b/phpBB/docs/CHANGELOG.html
index 37c3372125..e82930b3a5 100644
--- a/phpBB/docs/CHANGELOG.html
+++ b/phpBB/docs/CHANGELOG.html
@@ -75,6 +75,7 @@ p,ul,td {font-size:10pt;}
[Fix] Redirect to list if cancelling deletion of ranks, smilies or word censors
[Fix] Missing error message if an inactive user tried to login (Bug #1598)
[Fix] Do not alter post counts when just removing a poll (Bug #1602)
+[Sec] Improved filtering on language selection (also addresses a number of bug reports related to missing languages)
[Change] Backported more efficient highlighting code from Olympus
diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php
index 02b22df225..6bd88390bf 100644
--- a/phpBB/includes/functions.php
+++ b/phpBB/includes/functions.php
@@ -309,14 +309,14 @@ function make_jumpbox($action, $match_forum_id = 0)
function init_userprefs($userdata)
{
global $board_config, $theme, $images;
- global $template, $lang, $phpEx, $phpbb_root_path;
+ global $template, $lang, $phpEx, $phpbb_root_path, $db;
global $nav_links;
if ( $userdata['user_id'] != ANONYMOUS )
{
if ( !empty($userdata['user_lang']))
{
- $board_config['default_lang'] = $userdata['user_lang'];
+ $default_lang = phpbb_ltrim(basename(phpbb_rtrim($userdata['user_lang'])), "'");
}
if ( !empty($userdata['user_dateformat']) )
@@ -329,10 +329,60 @@ function init_userprefs($userdata)
$board_config['board_timezone'] = $userdata['user_timezone'];
}
}
-
- if ( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.'.$phpEx)) )
+ else
{
- $board_config['default_lang'] = 'english';
+ $default_lang = phpbb_ltrim(basename(phpbb_rtrim($board_config['default_lang'])), "'");
+ }
+
+ if ( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $default_lang . '/lang_main.'.$phpEx)) )
+ {
+ if ( $userdata['user_id'] != ANONYMOUS )
+ {
+ // For logged in users, try the board default language next
+ $default_lang = phpbb_ltrim(basename(phpbb_rtrim($board_config['default_lang'])), "'");
+ }
+ else
+ {
+ // For guests it means the default language is not present, try english
+ // This is a long shot since it means serious errors in the setup to reach here,
+ // but english is part of a new install so it's worth us trying
+ $default_lang = 'english';
+ }
+
+ if ( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $default_lang . '/lang_main.'.$phpEx)) )
+ {
+ message_die(CRITICAL_ERROR, 'Could not locate valid language pack');
+ }
+ }
+
+ // If we've had to change the value in any way then let's write it back to the database
+ // before we go any further since it means there is something wrong with it
+ if ( $userdata['user_id'] != ANONYMOUS && $userdata['user_lang'] !== $default_lang )
+ {
+ $sql = 'UPDATE ' . USERS_TABLE . "
+ SET user_lang = '" . $default_lang . "'
+ WHERE user_lang = '" . $userdata['user_lang'] . "'";
+
+ if ( !($result = $db->sql_query($sql)) )
+ {
+ message_die(CRITICAL_ERROR, 'Could not update user language info');
+ }
+
+ $board_config['default_lang'] = $default_lang;
+ $userdata['user_lang'] = $default_lang;
+ }
+ elseif ( $board_config['default_lang'] !== $default_lang )
+ {
+ $sql = 'UPDATE ' . CONFIG_TABLE . "
+ SET config_value = '" . $default_lang . "'
+ WHERE config_name = 'default_lang'";
+
+ if ( !($result = $db->sql_query($sql)) )
+ {
+ message_die(CRITICAL_ERROR, 'Could not update user language info');
+ }
+
+ $board_config['default_lang'] = $default_lang;
}
include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.' . $phpEx);
@@ -393,9 +443,9 @@ function setup_style($style)
{
global $db, $board_config, $template, $images, $phpbb_root_path;
- $sql = "SELECT *
- FROM " . THEMES_TABLE . "
- WHERE themes_id = $style";
+ $sql = 'SELECT *
+ FROM ' . THEMES_TABLE . '
+ WHERE themes_id = ' . (int) $style;
if ( !($result = $db->sql_query($sql)) )
{
message_die(CRITICAL_ERROR, 'Could not query database for theme info');
@@ -410,7 +460,7 @@ function setup_style($style)
{
$sql = 'SELECT *
FROM ' . THEMES_TABLE . '
- WHERE themes_id = ' . $board_config['default_style'];
+ WHERE themes_id = ' . (int) $board_config['default_style'];
if ( !($result = $db->sql_query($sql)) )
{
message_die(CRITICAL_ERROR, 'Could not query database for theme info');
@@ -421,7 +471,7 @@ function setup_style($style)
$db->sql_freeresult($result);
$sql = 'UPDATE ' . USERS_TABLE . '
- SET user_style = ' . $board_config['default_style'] . "
+ SET user_style = ' . (int) $board_config['default_style'] . "
WHERE user_style = $style";
if ( !($result = $db->sql_query($sql)) )
{