diff --git a/functions/functions.php b/functions/functions.php index 064334f..878aca5 100644 --- a/functions/functions.php +++ b/functions/functions.php @@ -44,7 +44,7 @@ function lang_getfrombrowser(array $allowed_languages, string $default_language, // Alle Infos über diese Sprache rausholen // phpcs:disable Generic.Strings.UnnecessaryStringConcat - $res = preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)' . '(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches); + $res = preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches); // phpcs:enable // war die Syntax gültig? @@ -66,9 +66,7 @@ function lang_getfrombrowser(array $allowed_languages, string $default_language, } // Bis der Sprachcode leer ist... - // phpcs:disable Squiz.PHP.DisallowSizeFunctionsInLoops - while (count($lang_code)) { - // phpcs:enable + while (!empty($lang_code)) { // mal sehen, ob der Sprachcode angeboten wird if (in_array(strtolower(join('-', $lang_code)), $allowed_languages)) { // Qualität anschauen @@ -252,3 +250,39 @@ function identical_values(array $arrayA, array $arrayB):bool { return $arrayA == $arrayB; } + +/** + * Function for retrieving the anti-csrf token or generating it if need be. + * + * @return string + */ +function getAntiCsrfToken():string { + + if (empty($_SESSION['anti-csrf-token'])) { + $_SESSION['anti-csrf-token'] = bin2hex(random_bytes(32)); + } + + return $_SESSION['anti-csrf-token']; + +} + +/** + * Function for validating anti-csrf tokens. Each anti-csrf token is removed + * after use. + * + * @return boolean + */ +function validateAntiCsrfToken():bool { + + $validity = false; + if (!empty($_POST['anti-csrf-token']) + && !empty($_SESSION['anti-csrf-token']) + && hash_equals($_SESSION['anti-csrf-token'], $_POST['anti-csrf-token']) === true + ) { + $validity = true; + } + $_SESSION['anti-csrf-token'] = null; unset($_SESSION['anti-csrf-token']); + + return $validity; + +} diff --git a/index.php b/index.php index da6d05f..23837ad 100644 --- a/index.php +++ b/index.php @@ -43,6 +43,7 @@ echo '