From 96bcda74334e849175d1b434727283c0b09b7154 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Mon, 29 Jan 2018 11:05:55 +0100 Subject: [PATCH] use Accept-Language header before logging into setup / user selected a language --- setup/inc/class.setup.inc.php | 19 +++++++++++++++++-- setup/inc/class.setup_html.inc.php | 3 +-- setup/inc/class.setup_translation.inc.php | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/setup/inc/class.setup.inc.php b/setup/inc/class.setup.inc.php index 635cd8b258..074853e0e2 100644 --- a/setup/inc/class.setup.inc.php +++ b/setup/inc/class.setup.inc.php @@ -195,9 +195,10 @@ class setup /** * Get configuration language from $_POST or $_SESSION and validate it * + * @param boolean $use_accept_language =false true: use Accept-Language header as fallback * @return string */ - static function get_lang() + static function get_lang($use_accept_language=false) { if (isset($_POST['ConfigLang'])) { @@ -208,11 +209,25 @@ class setup if (!isset($_SESSION)) self::session_start(); $ConfigLang = $_SESSION['ConfigLang']; } + $matches = null; + if ($use_accept_language && empty($ConfigLang) && + preg_match_all('/(^|,)([a-z-]+)/', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), $matches)) + { + $available = Api\Translation::get_available_langs(false); + foreach($matches[2] as $lang) + { + if (isset($available[$lang])) + { + $ConfigLang = $lang; + break; + } + } + } if (!preg_match('/^[a-z]{2}(-[a-z]{2})?$/',$ConfigLang)) { $ConfigLang = null; // not returning 'en', as it suppresses the language selection in check_install and manageheader } - //error_log(__METHOD__."() \$_POST['ConfigLang']=".array2string($_POST['ConfigLang']).", \$_SESSION['ConfigLang']=".array2string($_SESSION['ConfigLang'])." returning ".array2string($ConfigLang)); + //error_log(__METHOD__."() \$_POST['ConfigLang']=".array2string($_POST['ConfigLang']).", \$_SESSION['ConfigLang']=".array2string($_SESSION['ConfigLang']).", HTTP_ACCEPT_LANGUAGE=$_SERVER[HTTP_ACCEPT_LANGUAGE] --> returning ".array2string($ConfigLang)); return $ConfigLang; } diff --git a/setup/inc/class.setup_html.inc.php b/setup/inc/class.setup_html.inc.php index 844dce3d84..797c6df671 100644 --- a/setup/inc/class.setup_html.inc.php +++ b/setup/inc/class.setup_html.inc.php @@ -286,8 +286,7 @@ class setup_html { if (empty($ConfigLang)) { - $ConfigLang = setup::get_lang(); - if (empty($ConfigLang)) $ConfigLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2); + $ConfigLang = setup::get_lang(true); // true use Accept-Language header } return Api\Html::select('ConfigLang', $ConfigLang, Api\Translation::get_available_langs(false), true, $onChange ? ' onchange="this.form.submit();"' : ''); diff --git a/setup/inc/class.setup_translation.inc.php b/setup/inc/class.setup_translation.inc.php index e0f49f35d8..c9450b75cb 100644 --- a/setup/inc/class.setup_translation.inc.php +++ b/setup/inc/class.setup_translation.inc.php @@ -43,7 +43,7 @@ class setup_translation */ function __construct() { - $ConfigLang = setup::get_lang(); + $ConfigLang = setup::get_lang(true); if(!$ConfigLang) {