Skip to content

Commit

Permalink
feat(go.php): Detect browser language to redirect to user manual in p…
Browse files Browse the repository at this point in the history
…referred language

Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Jan 7, 2025
1 parent 82b00ab commit 45ab712
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions go.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,49 @@

header('HTTP/1.1 302 Moved Temporarily');
if (array_key_exists($from, $mapping)) {
header('Location: ' . $location . $mapping[$from]);
$subPath = $mapping[$from];
} else {
if (strpos($from, 'admin-') === 0) {
header('Location: ' . $location . '/admin_manual');
$subPath = '/admin_manual';
} else if (strpos($from, 'developer-') === 0) {
header('Location: ' . $location . '/developer_manual');
$subPath = '/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 use 'user_manual/_build/html/'
if (file_exists('user_manual/' . $language)) {
// Insert the language /user_manual/<language>/...
$subPath = implode('/', array_merge(array_slice($subPathParts, 0, 2), [$language], array_slice($subPathParts, 2)));
break;
}
}
}

header('Location: ' . $location . $subPath);

0 comments on commit 45ab712

Please sign in to comment.