feat(go.php): Detect browser language to redirect to user manual in preferred language

Signed-off-by: provokateurin <kate@provokateurin.de>
This commit is contained in:
provokateurin
2025-01-07 18:04:32 +01:00
parent 82b00ab8f6
commit 1321b4e142

49
go.php
View File

@@ -82,13 +82,48 @@ $location = "$proto://$name$port$path";
header('HTTP/1.1 302 Moved Temporarily');
if (array_key_exists($from, $mapping)) {
header('Location: ' . $location . $mapping[$from]);
$subPath = $mapping[$from];
} else if (str_starts_with($from, 'admin-')) {
$subPath = '/admin_manual';
} else if (str_starts_with($from, 'developer-')) {
$subPath = '/developer_manual';
} else {
if (strpos($from, 'admin-') === 0) {
header('Location: ' . $location . '/admin_manual');
} else if (strpos($from, 'developer-') === 0) {
header('Location: ' . $location . '/developer_manual');
} else {
header('Location: ' . $location . '/user_manual');
$subPath = '/user_manual';
}
$subPathParts = explode('/', $subPath);
$manual = $subPathParts[1] ?? '';
if ($manual === 'user_manual') {
// Sort accepted languages by their weight
$acceptLanguages = array_reduce(
explode(', ', $_SERVER['HTTP_ACCEPT_LANGUAGE']),
static function ($out, $element) {
[$language, $q] = array_merge(explode(';q=', $element), [1]);
$out[$language] = (float)$q;
return $out;
},
[],
);
arsort($acceptLanguages);
foreach ($acceptLanguages as $language => $weight) {
if (!preg_match('/^[a-z-]+$/im', $language)) {
// Skip any invalid languages and prevent directory traversals
continue;
}
$language = str_replace('-', '_', $language);
// To test locally add '/_build/html/' to the path.
if (file_exists(__DIR__ . '/user_manual/' . $language)) {
// Insert the language /user_manual/<language>/...
array_splice($subPathParts, 2, 0, [$language]);
$subPath = implode('/', $subPathParts);
break;
}
}
}
header('Location: ' . $location . $subPath);