Skip to content

Commit 0a43db5

Browse files
authored
Merge pull request #1317 from shivammathur/downloads-detect-os
Detect OS and OS variant on downloads page
2 parents c0dda8e + 0844a09 commit 0a43db5

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

downloads.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,43 @@ function option(string $value, string $desc, $attributes = []): string
9999
'default' => 'OS default version',
100100
];
101101

102+
103+
$platform = $_SERVER['HTTP_SEC_CH_UA_PLATFORM'] ?? '';
104+
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
105+
$auto_os = null;
106+
$auto_osvariant = null;
107+
108+
if (!empty($platform) || !empty($ua)) {
109+
$platform = strtolower(trim($platform, '"'));
110+
if ($platform === 'windows' || stripos($ua, 'Windows') !== false) {
111+
$auto_os = 'windows';
112+
} elseif ($platform === 'macos' || stripos($ua, 'Mac') !== false) {
113+
$auto_os = 'osx';
114+
} elseif ($platform === 'linux' || stripos($ua, 'Linux') !== false) {
115+
$auto_os = 'linux';
116+
if (stripos($ua, 'Ubuntu') !== false) {
117+
$auto_osvariant = 'linux-ubuntu';
118+
} elseif (stripos($ua, 'Debian') !== false) {
119+
$auto_osvariant = 'linux-debian';
120+
} elseif (stripos($ua, 'Fedora') !== false) {
121+
$auto_osvariant = 'linux-fedora';
122+
} elseif (stripos($ua, 'Red Hat') !== false || stripos($ua, 'RedHat') !== false) {
123+
$auto_osvariant = 'linux-redhat';
124+
}
125+
}
126+
}
127+
102128
$defaults = [
103-
'os' => 'linux',
129+
'os' => $auto_os ?? 'linux',
104130
'version' => 'default',
105131
'usage' => 'web',
106132
];
107133

108134
$options = array_merge($defaults, $_GET);
109-
if (!array_key_exists('osvariant', $options) || !array_key_exists($options['osvariant'], $os[$options['os']]['variants'])) {
135+
136+
if ($auto_osvariant && (!array_key_exists('osvariant', $options) || !array_key_exists($options['osvariant'], $os[$options['os']]['variants']))) {
137+
$options['osvariant'] = $auto_osvariant;
138+
} elseif (!array_key_exists('osvariant', $options) || !array_key_exists($options['osvariant'], $os[$options['os']]['variants'])) {
110139
$options['osvariant'] = array_key_first($os[$options['os']]['variants']);
111140
}
112141
?>

0 commit comments

Comments
 (0)