Adding version checking
This commit is contained in:
@@ -180,6 +180,64 @@ function run_fetch(): array {
|
||||
return $results;
|
||||
}
|
||||
|
||||
// ── Upstream version check ────────────────────────────────────────────────────
|
||||
|
||||
define('UPSTREAM_VERSION_URL', 'https://git.ny.daprogs.com/api/v1/repos/DAProgs/portspoof_concentrator/raw/version.php?ref=main');
|
||||
define('UPSTREAM_VERSION_CACHE', __DIR__ . '/../upstream_version.cache');
|
||||
define('UPSTREAM_VERSION_TTL', 3600); // seconds
|
||||
|
||||
/**
|
||||
* Fetch the upstream version string from the git repo, with a 1-hour file cache.
|
||||
* Returns null silently on any failure so the page always loads.
|
||||
*/
|
||||
function fetch_upstream_version(): ?string {
|
||||
// Return cached value if still fresh
|
||||
if (file_exists(UPSTREAM_VERSION_CACHE)) {
|
||||
$cached = json_decode(file_get_contents(UPSTREAM_VERSION_CACHE), true);
|
||||
if (isset($cached['version'], $cached['checked_at'])
|
||||
&& (time() - $cached['checked_at']) < UPSTREAM_VERSION_TTL) {
|
||||
return $cached['version'];
|
||||
}
|
||||
}
|
||||
|
||||
$ch = curl_init(UPSTREAM_VERSION_URL);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 5,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
]);
|
||||
$body = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($body === false || $code !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!preg_match("/define\s*\(\s*'APP_VERSION'\s*,\s*'([^']+)'\s*\)/", $body, $m)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$version = $m[1];
|
||||
file_put_contents(
|
||||
UPSTREAM_VERSION_CACHE,
|
||||
json_encode(['version' => $version, 'checked_at' => time()]),
|
||||
LOCK_EX
|
||||
);
|
||||
return $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $upstream is a newer version than $local.
|
||||
* Format: YYMM.N e.g. 2603.4
|
||||
*/
|
||||
function is_newer_version(string $upstream, string $local): bool {
|
||||
$parse = fn($v) => array_map('intval', explode('.', $v, 2));
|
||||
[$um, $un] = $parse($upstream);
|
||||
[$lm, $ln] = $parse($local);
|
||||
return $um > $lm || ($um === $lm && $un > $ln);
|
||||
}
|
||||
|
||||
// ── API queries ───────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user