Comments (3)
Nice catch - you are right that the POST and COOKIE value should be honored even if HTTP_ACCEPT_LANGUAGE is not set.
I tend to do a slightly different fix - instead of making the function even more confusing with array_merge(), it should handle POST and COOKIE first.
--- a/functions.inc.php
+++ b/functions.inc.php
@@ -139,16 +139,22 @@ function _flash_string($type, $string) {
function check_language ($use_post = 1) {
global $supported_languages; # from languages/languages.php
+ if ( $use_post && safepost('lang')) {
+ if(array_key_exists(safepost('lang'), $supported_languages)) {
+ return safepost('lang'); # first preference: $_POST['lang']
+ }
+ }
+
+ if (safecookie('lang')) {
+ if(array_key_exists(safecookie('lang'), $supported_languages)) {
+ return safecookie('lang'); # second-best: $_COOKIE['lang']
+ }
+ }
+
$lang = Config::read('default_language');
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$lang_array = preg_split ('/(\s*,\s*)/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
- if (safecookie('lang')) {
- array_unshift($lang_array, safecookie('lang')); # prefer language from cookie
- }
- if ( $use_post && safepost('lang')) {
- array_unshift($lang_array, safepost('lang')); # but prefer $_POST['lang'] even more
- }
for($i = 0; $i < count($lang_array); $i++) {
$lang_next = $lang_array[$i];
I gave this patch only minimal testing (which is also the reason why I didn't commit it yet), so feel free to test and report back ;-)
from postfixadmin.
In the meantime I have replaced the function with my own version, that respects HTTP_ACCEPT_LANGUAGE priorities as well. I have also made some basic cleanup like the order of tests etc.
`
function check_language ($use_post = 1) {
global $supported_languages; # from languages/languages.php
$lang_array = array();
$lang = Config::read('default_language');
if ( $use_post && safepost('lang')) {
$lang_array[strtolower(safepost('lang'))] = 1;
}
if (safecookie('lang')) {
$lang_array[strtolower(safecookie('lang')] = 1;
}
if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langtags = explode(',',strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
for($i = 0; $i < count($langtags); $i++ ) {
$langcode = NULL;
$langpriority = NULL;
$langdef = explode(';',$langtags[$i]);
if (isset($langdef[0])) {
$langcode = trim($langdef[0]);
}
if (isset($langdef[1])) {
$langpriority = trim($langdef[1]);
}
if (!empty($langcode)) {
$langcode = substr($langcode,0,2);
if (ctype_alpha($langcode)) {
$xpriority = 1;
if (!empty($langpriority) && strpos($langpriority,'q=') === 0) {
$q = (float)trim(substr($langpriority,2));
if (is_numeric($q) && $q >= 0 && $q <= 1) {
$xpriority = $q;
}
}
if (!isset($lang_array[$langcode])) {
$lang_array[$langcode] = $xpriority;
}
elseif ($lang_array[$langcode] < $xpriority) {
$lang_array[$langcode] = $xpriority;
}
}
}
}
}
if (count($lang_array) > 0) {
$bestpriority = -1;
foreach ($lang_array as $langcode => $langpriority) {
if (array_key_exists($langcode, $supported_languages)) {
if ($bestpriority < $langpriority) {
$lang = $langcode;
$bestpriority = $langpriority;
}
}
}
}
return $lang;
}
`
from postfixadmin.
If $_POST['lang'] or $_COOKIE['lang'] is set, this means the user selected a language manually in the dropdown now ($_POST) or some days ago ($_COOKIE). Therefore $_POST and $_COOKIE should always win - if they contain a valid language, there's no need to check HTTP_ACCEPT_LANGUAGE.
Besides that - honoring the priorities is a nice addition, thanks!
It's clearly worth the additional CPU cycles, but it reminds me to
common.php: $language = check_language (); # TODO: storing the language only at login instead of calling check_language() on every page would save some processor cycles ;-)
;-) which should be doable by storing the language in the session.
from postfixadmin.
Related Issues (20)
- Enhancement: selectively enable/disable services HOT 6
- Domain admins disapeared HOT 1
- app passwords HOT 2
- Delete user needs to delete forwards too HOT 4
- delate app-passwords fails HOT 2
- TOTP Exceptions => blank page
- App Passwords not working with example query HOT 3
- Missing Domain Signing menu HOT 1
- TOTP login form autocomplete
- Fatal php error on deleting dkim-entry HOT 1
- Postfixadmin using replicated database HOT 3
- Migrate away from PHP's imap extension? HOT 2
- Migrate from vimbadmin to postfixadmin HOT 4
- Creating the mailbox failed! HOT 3
- vacation.pl - replace_string() is produced parsing errors HOT 1
- Have installed 3.3.13 install and setup ok but cant login HOT 1
- Mailbox active toggle (editactive.php) crashes HOT 1
- Setting md5crypt does not work HOT 3
- Vacation.pl script will not send messages if $no_vacation_pattern is null string HOT 1
- Postfix losing connection HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from postfixadmin.