Skip to content

Commit 281ea47

Browse files
committed
chore: Modernize version check script
1 parent 89ff98f commit 281ea47

File tree

5 files changed

+180
-363
lines changed

5 files changed

+180
-363
lines changed

server/.htaccess

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
RewriteEngine On
2+
RewriteRule ^version-check/?$ /version-check.php [L]

server/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM php:8.4-apache
2+
RUN a2enmod rewrite
3+
RUN rm -f /var/www/html/index.html
4+
COPY version-check.php /var/www/html/version-check.php
5+
COPY .htaccess /var/www/html/.htaccess
6+
RUN chown -R www-data:www-data /var/www/html/
7+
EXPOSE 80

server/survey.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

server/version-check.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
/**
4+
* Version upgrade path manager for SyncTrayzor
5+
*
6+
* Clients request this with their current version, arch, and variant (portable, etc)
7+
* and this gives them a version to upgrade to (if any), along with the method of
8+
* upgrading to it (manual navigation to Github release page, automatic silent upgrade,
9+
* etc).
10+
*
11+
* $versions is a record of all of the current releases, which we might want to upgrade
12+
* people to. It has the structure:
13+
* [
14+
* version => [
15+
* variant => [
16+
* 'url' => [
17+
* arch => 'url',
18+
* ...
19+
* ],
20+
* ],
21+
* ...
22+
* 'release_notes' => release_notes,
23+
* ],
24+
* ...
25+
* ]
26+
*
27+
* version: version string e.g. '1.2.3'
28+
* variant: e.g. 'portable', 'installed'. Matched against the variant provided by the
29+
* client, or '*' can be used to specify a default.
30+
* arch: e.g. 'x86', 'x64'. Matched against the arch provided by the client, or '*'
31+
* can used to specify a default.
32+
* release_notes: Release notes to display to the user.
33+
*
34+
* $upgrades is a map of old_version => new_version, and specifies the formatter to
35+
* use to communicate with old_version. It also allows various overrides to be
36+
* specified (e.g. release notes)
37+
* It has the structure:
38+
* [
39+
* old_version => ['to' => new_version, 'formatter' => formatter_version, 'overrides' => [overrides]],
40+
* ...
41+
* ]
42+
*
43+
* old_version: version being upgraded from
44+
* new_version: version to upgrade to
45+
* formatter_version: formatter version to use (in $response_formatters)
46+
* overrides: optional overrides, used by the formatter
47+
*/
48+
49+
set_error_handler('error_handler');
50+
date_default_timezone_set('UTC');
51+
header('Content-Type: application/json');
52+
53+
// Allowed values for arch and variant
54+
$allowed_arches = ['x64', 'arm64'];
55+
$allowed_variants = ['installed', 'portable'];
56+
57+
function error_handler($severity, $message, $filename, $lineno)
58+
{
59+
throw new ErrorException($message, 0, $severity, $filename, $lineno);
60+
}
61+
62+
function get_with_wildcard($src, $value, $default = null)
63+
{
64+
if (isset($src[$value]))
65+
return $src[$value];
66+
if (isset($src['*']))
67+
return $src['*'];
68+
return $default;
69+
}
70+
71+
$versions = [
72+
'2.0.0' => [
73+
'base_url' => 'https://github.com/GermanCoding/SyncTrayzor/releases/download',
74+
'installed' => [
75+
'direct_download_url' => [
76+
'x64' => "{base_url}/v{version}/SyncTrayzorSetup-x64.exe",
77+
'arm64' => "{base_url}/v{version}/SyncTrayzorSetup-arm64.exe",
78+
],
79+
],
80+
'portable' => [
81+
'direct_download_url' => [
82+
'x64' => "{base_url}/v{version}/SyncTrayzorPortable-x64.zip",
83+
'arm' => "{base_url}/v{version}/SyncTrayzorPortable-arm64.zip",
84+
],
85+
],
86+
'sha512sum_download_url' => "{base_url}/v{version}/sha512sum.txt.asc",
87+
'release_page_url' => 'https://github.com/GermanCoding/SyncTrayzor/releases/tag/v{version}',
88+
'release_notes' => "N/A",
89+
]
90+
];
91+
92+
$upgrades = [
93+
'1.1.29' => ['to' => 'latest', 'formatter' => '5']
94+
];
95+
96+
$response_formatters = [
97+
// Base for 2.0.0 releases (same as the last 1.x releases)
98+
'5' => function($arch, $variant, $to_version, $to_version_info, $overrides)
99+
{
100+
$variant_info = isset($overrides[$variant]) ? get_with_wildcard($overrides, $variant) : get_with_wildcard($to_version_info, $variant);
101+
102+
$data = [
103+
'version' => $to_version,
104+
'direct_download_url' => get_with_wildcard($variant_info['direct_download_url'], $arch),
105+
'sha512sum_download_url' => $to_version_info['sha512sum_download_url'],
106+
'release_page_url' => $to_version_info['release_page_url'],
107+
'release_notes' => isset($overrides['release_notes']) ? $overrides['release_notes'] : $to_version_info['release_notes'],
108+
];
109+
110+
return $data;
111+
},
112+
];
113+
114+
$error = null;
115+
$loggable_error = null;
116+
$data = null;
117+
118+
try
119+
{
120+
// Use filter_input for better input handling
121+
$version = isset($_GET['version']) ? trim(strip_tags($_GET['version'])) : null;
122+
$arch = isset($_GET['arch']) ? trim(strip_tags($_GET['arch'])) : null;
123+
$variant = isset($_GET['variant']) ? trim(strip_tags($_GET['variant'])) : null;
124+
125+
// Validate inputs
126+
if (empty($version) || empty($arch) || empty($variant))
127+
{
128+
$error = ['code' => 1, 'message' => 'version, arch, or variant not specified'];
129+
}
130+
// Only allow known arches and variants
131+
else if (!in_array($arch, $allowed_arches, true) || !in_array($variant, $allowed_variants, true))
132+
{
133+
$error = ['code' => 3, 'message' => 'Invalid arch or variant'];
134+
}
135+
else if (isset($upgrades[$version]))
136+
{
137+
$to_version = $upgrades[$version]['to'];
138+
if ($to_version === 'latest')
139+
$to_version = array_keys($versions)[0];
140+
141+
$formatter = $response_formatters[$upgrades[$version]['formatter']];
142+
$overrides = $upgrades[$version]['overrides'] ?? [];
143+
144+
$base_url = $overrides['base_url'] ?? $versions[$to_version]['base_url'];
145+
146+
array_walk_recursive($versions[$to_version], function(&$value, $key) use ($to_version, $base_url) {
147+
$value = str_replace('{version}', $to_version, $value);
148+
$value = str_replace('{base_url}', $base_url, $value);
149+
});
150+
$to_version_info = $versions[$to_version];
151+
152+
$data = $formatter($arch, $variant, $to_version, $to_version_info, $overrides);
153+
}
154+
}
155+
catch (Exception $e)
156+
{
157+
$error = ['code' => 2, 'message' => 'Unhandled error. Please try again later'];
158+
error_log($e->getMessage() . "\n" . $e->getTraceAsString());
159+
}
160+
161+
$rsp = [];
162+
if ($data != null)
163+
$rsp['data'] = $data;
164+
if ($error != null)
165+
$rsp['error'] = $error;
166+
167+
$output = json_encode($rsp, JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT | JSON_PRETTY_PRINT);
168+
169+
$date = date('c');
170+
171+
echo $output;

0 commit comments

Comments
 (0)