Compare commits

..

4 Commits

Author SHA1 Message Date
jrenslin 3b5f20aa96 Add missing "static" keyword in MD_STD 2021-04-14 18:16:13 +02:00
jrenslin 56f4fdc88a Add function get_user_lang for getting user language based on cookies 2021-04-14 17:59:11 +02:00
jrenslin 919ffdb1b5 Use ++$i over $i++
This slightly improves performance.
2021-04-11 21:20:44 +02:00
jrenslin 36bdb36986 Use consistent first uppercase char in MD_STD reference to exception 2021-04-09 13:45:19 +02:00
2 changed files with 43 additions and 4 deletions
+40 -1
View File
@@ -301,7 +301,7 @@ final class MD_STD {
public static function runCurlMulti(array $urls, int $timeout = 1200):array {
if (!($mh = curl_multi_init())) {
throw new exception("Failed to set up multi handle");
throw new Exception("Failed to set up multi handle");
}
$curl_array = [];
@@ -334,6 +334,45 @@ final class MD_STD {
}
/**
* Sets and returns user language based on a session cookie.
*
* @param array<string> $allowed_langs Allowed languages.
* @param string $default_lang Default language.
*
* @return string
*/
public static function get_user_lang(array $allowed_langs, string $default_lang):string {
$cookie_options = [
'expires' => self::strtotime("+1 month"),
'path' => '/',
'domain' => $_SERVER['SERVER_NAME'],
'secure' => true,
'httponly' => true,
'samesite' => 'Lax' // None || Lax || Strict
];
if (isset($_GET['navlang']) and in_array($_GET['navlang'], $allowed_langs, true)) {
if (!(setcookie('lang', $_GET['navlang'], $cookie_options))) {
throw new Exception("Failed to set language");
}
$lang = $_GET['navlang'];
}
else if (isset($_COOKIE['lang']) and in_array($_COOKIE['lang'], $allowed_langs, true)) {
$lang = $_COOKIE['lang'];
}
else {
$lang = self::lang_getfrombrowser($allowed_langs, $default_lang, "", false);
if (!(setcookie('lang', $lang, $cookie_options))) {
throw new Exception("Failed to set language");
}
}
return $lang;
}
/**
* Function lang_getfrombrowser gets the browser language based on HTTP headers.
*
+3 -3
View File
@@ -92,11 +92,11 @@ final class MD_STD_SEC {
}
// Increase counters and update timers
$loginLog['common']['count']++;
++$loginLog['common']['count'];
$loginLog['common']['time'] = \time();
$loginLog['usr'][$hash_user]['count']++;
++$loginLog['usr'][$hash_user]['count'];
$loginLog['usr'][$hash_user]['time'] = \time();
$loginLog['ip'][$hash_ip]['count']++;
++$loginLog['ip'][$hash_ip]['count'];
$loginLog['ip'][$hash_ip]['time'] = \time();
// Update the log file