You've already forked postfixadmin
mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2026-10-24 12:19:19 +03:00
possible fix for https://github.com/postfixadmin/postfixadmin/issues/967 CSRF Token changes : rather than have one PFA_Token value in $_SESSION, store a list of them, where each value has a time stamp it was issued on. The aim is to allow the user to have multiple browser tabs open at once - and not have the last tab that loaded trample on others. Tokens expire after 1 hour. Adds a Smarty helper function `{CSRF_Token}` which will output `<input type=hidden name=CSRF_Token value=asdf123>` or `{CSRF_Token type=url}` which outputs a literal string like 'asdf123' suitable for placing in a URL. Adds a PHP exception handler in public/common.php, which could be used to display a more friendly error message/response (but at the moment is fairly minimal). Removes most uses of `die()` within the codebase, and instead moves to throw an exception
88 lines
3.3 KiB
PHP
88 lines
3.3 KiB
PHP
<?php
|
|
|
|
class Languages
|
|
{
|
|
// we sometimes mess with this in unit tests?
|
|
public static array $SUPPORTED_LANGUAGES = array(
|
|
# Format:
|
|
# 'code' => 'native language name - language name in english'
|
|
'bg' => 'Български - Bulgarian',
|
|
'ca' => 'Català - Catalan',
|
|
'cn' => '中文 - Chinese simplified (gb2312)',
|
|
'tw' => '中文 - Chinese traditional',
|
|
'cs' => 'Česky - Czech',
|
|
'da' => 'Dansk - Danish',
|
|
'de' => 'Deutsch - German',
|
|
'en' => 'English',
|
|
'es' => 'Español - Spanish',
|
|
'et' => 'Eesti - Estonian',
|
|
'eu' => 'Euskara - Basque',
|
|
'fi' => 'Suomi - Finnish',
|
|
'fo' => 'Faroese',
|
|
'fr' => 'Français - French',
|
|
'hr' => 'Hrvatski - Croatian',
|
|
'hu' => 'Magyar - Hungarian',
|
|
'gl' => 'Galego - Galician',
|
|
'is' => 'Icelandic',
|
|
'it' => 'Italiano - Italian',
|
|
'ja' => '日本語 - Japanese',
|
|
'lt' => 'Lietuvių - Lithuanian',
|
|
'mk' => 'Macedonian - Macedonian',
|
|
'nl' => 'Nederlands - Dutch',
|
|
'nb' => 'Norsk (bokmål) - Norwegian (bokmål)',
|
|
'nn' => 'Norsk (nynorsk) - Norwegian (nynorsk)',
|
|
'pl' => 'Polski - Polish',
|
|
'pt-br' => 'Português - Brazilian portuguese',
|
|
'pt-pt' => 'Português - European portuguese',
|
|
'ro' => 'Limba Română - Romanian',
|
|
'ru' => 'Русский - Russian',
|
|
'sk' => 'Slovenčina - Slovak',
|
|
'sl' => 'Slovenščina - Slovenian',
|
|
'sv' => 'Svenska - Swedish',
|
|
'tr' => 'Türkçe - Turkish',
|
|
'ua' => 'Українська - Ukrainian',
|
|
);
|
|
|
|
|
|
/**
|
|
* @param bool $use_post - set to 0 if $_POST should NOT be read
|
|
* @return string e.g en
|
|
* Try to figure out what language the user wants based on browser / cookie
|
|
*/
|
|
public static function check_language(bool $use_post = true): string
|
|
{
|
|
$supported_languages = self::$SUPPORTED_LANGUAGES;
|
|
|
|
// prefer a $_POST['lang'] if present
|
|
if ($use_post && safepost('lang')) {
|
|
$lang = safepost('lang');
|
|
if (is_string($lang) && array_key_exists($lang, $supported_languages)) {
|
|
return $lang;
|
|
}
|
|
}
|
|
|
|
// Failing that, is there a $_COOKIE['lang'] ?
|
|
if (safecookie('lang')) {
|
|
$lang = safecookie('lang');
|
|
if (!empty($lang) && array_key_exists($lang, $supported_languages)) {
|
|
return $lang;
|
|
}
|
|
}
|
|
|
|
$lang = Config::read_string('default_language');
|
|
|
|
// If not, did the browser give us any hint(s)?
|
|
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
|
$lang_array = preg_split('/(\s*,\s*)/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
|
foreach ($lang_array as $value) {
|
|
$lang_next = strtolower(trim($value));
|
|
$lang_next = preg_replace('/;.*$/', '', $lang_next); # remove things like ";q=0.8"
|
|
if (array_key_exists($lang_next, $supported_languages) && is_string($lang_next)) {
|
|
return $lang_next;
|
|
}
|
|
}
|
|
}
|
|
return $lang;
|
|
}
|
|
}
|