From 1321b4e142998438dfbe62472b8fb053b60f2ecd Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 7 Jan 2025 18:04:32 +0100 Subject: [PATCH] feat(go.php): Detect browser language to redirect to user manual in preferred language Signed-off-by: provokateurin --- go.php | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/go.php b/go.php index 2b8116154..ce059051e 100644 --- a/go.php +++ b/go.php @@ -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//... + array_splice($subPathParts, 2, 0, [$language]); + $subPath = implode('/', $subPathParts); + break; + } } } + +header('Location: ' . $location . $subPath);