From 73a39f4f3f463ba7542b47847622917738e25c39 Mon Sep 17 00:00:00 2001 From: DAProgs Date: Wed, 11 Mar 2026 11:08:24 -0400 Subject: [PATCH] Adding version checking --- includes/functions.php | 58 ++++++++++++++++++++++++++++++++++++++++++ includes/style.php | 6 +++-- index.php | 11 ++++++++ version.php | 2 +- 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 586a785..2df04d5 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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 ─────────────────────────────────────────────────────────────── /** diff --git a/includes/style.php b/includes/style.php index 9a866d9..d6a33db 100644 --- a/includes/style.php +++ b/includes/style.php @@ -72,8 +72,10 @@ button[type=submit]:hover { opacity: .85; } .link-btn.danger { color: var(--red); } .alert { padding: .65rem 1rem; border-radius: 6px; font-size: .875rem; } -.alert.ok { background: rgba(63,185,80,.15); color: var(--green); border: 1px solid rgba(63,185,80,.4); } -.alert.err { background: rgba(248,81,73,.15); color: var(--red); border: 1px solid rgba(248,81,73,.4); } +.alert.ok { background: rgba(63,185,80,.15); color: var(--green); border: 1px solid rgba(63,185,80,.4); } +.alert.err { background: rgba(248,81,73,.15); color: var(--red); border: 1px solid rgba(248,81,73,.4); } +.alert.warn { background: rgba(210,153,34,.15); color: var(--yellow); border: 1px solid rgba(210,153,34,.4); } +.alert.warn a { color: var(--yellow); } .badge { display: inline-block; font-size: .7rem; padding: .1rem .45rem; border-radius: 3px; background: var(--border); color: var(--muted); } .badge.ok { background: rgba(63,185,80,.2); color: var(--green); } diff --git a/index.php b/index.php index 85b1b79..2e4ab28 100644 --- a/index.php +++ b/index.php @@ -20,6 +20,9 @@ if ($filter_node) { $max_ip_cnt = $t_ips ? max(array_column($t_ips, 'cnt')) : 1; $max_port_cnt = $t_ports ? max(array_column($t_ports, 'cnt')) : 1; + +$upstream_version = fetch_upstream_version(); +$update_available = $upstream_version && is_newer_version($upstream_version, APP_VERSION); ?> @@ -46,6 +49,14 @@ $max_port_cnt = $t_ports ? max(array_column($t_ports, 'cnt')) : 1;
+ +
+ Update available: v — + you are running v. + View release ↗ +
+ +
diff --git a/version.php b/version.php index dd12e06..265a469 100644 --- a/version.php +++ b/version.php @@ -1,2 +1,2 @@