Fix version.php to read from .version file

This commit is contained in:
2026-01-01 19:09:06 +11:00
parent d9faee1114
commit c9677d5e3e
2 changed files with 51 additions and 42 deletions

View File

@@ -37,7 +37,13 @@ if ($action === 'update') {
"cd $installDir && git reset --hard origin/main",
"cd $installDir && git pull origin main",
"cp -r $installDir/webui/* $webDir/",
"chown -R www-data:www-data $webDir"
"chown -R www-data:www-data $webDir",
// Create .version file with git info
"cd $installDir && git rev-parse --short HEAD > $webDir/.version",
"cd $installDir && git rev-parse HEAD >> $webDir/.version",
"cd $installDir && git rev-parse --abbrev-ref HEAD >> $webDir/.version",
"cd $installDir && git log -1 --format=%ci >> $webDir/.version",
"chown www-data:www-data $webDir/.version"
];
$output = [];

View File

@@ -2,54 +2,57 @@
/**
* UltyScan Web Interface - Git Version Info
* Returns current git commit ID and age
* Reads version from .version file (created during deployment)
*/
header('Content-Type: application/json');
$gitDir = '/usr/share/sniper';
$versionFile = __DIR__ . '/.version';
// Get commit hash
$commitHash = trim(shell_exec("cd $gitDir && git rev-parse --short HEAD 2>/dev/null"));
$fullHash = trim(shell_exec("cd $gitDir && git rev-parse HEAD 2>/dev/null"));
// Default values
$data = [
'commit' => '',
'fullCommit' => '',
'branch' => '',
'date' => '',
'age' => '',
'timestamp' => false
];
// Get commit date
$commitDate = trim(shell_exec("cd $gitDir && git log -1 --format=%ci 2>/dev/null"));
$commitTimestamp = strtotime($commitDate);
// Calculate age
$age = '';
if ($commitTimestamp) {
$diff = time() - $commitTimestamp;
if ($diff < 60) {
$age = $diff . ' seconds ago';
} elseif ($diff < 3600) {
$mins = floor($diff / 60);
$age = $mins . ' minute' . ($mins > 1 ? 's' : '') . ' ago';
} elseif ($diff < 86400) {
$hours = floor($diff / 3600);
$age = $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
} elseif ($diff < 604800) {
$days = floor($diff / 86400);
$age = $days . ' day' . ($days > 1 ? 's' : '') . ' ago';
} elseif ($diff < 2592000) {
$weeks = floor($diff / 604800);
$age = $weeks . ' week' . ($weeks > 1 ? 's' : '') . ' ago';
} else {
$months = floor($diff / 2592000);
$age = $months . ' month' . ($months > 1 ? 's' : '') . ' ago';
// Try reading from .version file first
if (file_exists($versionFile)) {
$lines = file($versionFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (count($lines) >= 4) {
$data['commit'] = trim($lines[0]);
$data['fullCommit'] = trim($lines[1]);
$data['branch'] = trim($lines[2]);
$data['date'] = trim($lines[3]);
$data['timestamp'] = strtotime($data['date']);
}
}
// Get branch
$branch = trim(shell_exec("cd $gitDir && git rev-parse --abbrev-ref HEAD 2>/dev/null"));
// Calculate age if we have a timestamp
if ($data['timestamp']) {
$diff = time() - $data['timestamp'];
echo json_encode([
'commit' => $commitHash,
'fullCommit' => $fullHash,
'branch' => $branch,
'date' => $commitDate,
'age' => $age,
'timestamp' => $commitTimestamp
]);
if ($diff < 60) {
$data['age'] = $diff . ' seconds ago';
} elseif ($diff < 3600) {
$mins = floor($diff / 60);
$data['age'] = $mins . ' minute' . ($mins > 1 ? 's' : '') . ' ago';
} elseif ($diff < 86400) {
$hours = floor($diff / 3600);
$data['age'] = $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
} elseif ($diff < 604800) {
$days = floor($diff / 86400);
$data['age'] = $days . ' day' . ($days > 1 ? 's' : '') . ' ago';
} elseif ($diff < 2592000) {
$weeks = floor($diff / 604800);
$data['age'] = $weeks . ' week' . ($weeks > 1 ? 's' : '') . ' ago';
} else {
$months = floor($diff / 2592000);
$data['age'] = $months . ' month' . ($months > 1 ? 's' : '') . ' ago';
}
}
echo json_encode($data);