small-package/luci-app-nekobox/htdocs/nekobox/check_update.php

63 lines
1.5 KiB
PHP
Raw Normal View History

2024-10-26 10:38:22 +08:00
<?php
function getCurrentVersion() {
$packageName = 'luci-app-nekobox';
$command = "opkg list-installed | grep $packageName";
$output = shell_exec($command . ' 2>&1');
if ($output === null || empty($output)) {
return "Error";
}
$parts = explode(' - ', $output);
2024-11-17 10:57:40 +08:00
if (count($parts) >= 2) {
return cleanVersion($parts[1]);
2024-10-26 10:38:22 +08:00
}
2024-11-17 10:57:40 +08:00
return "Error";
2024-10-26 10:38:22 +08:00
}
function getLatestVersion() {
2024-11-17 10:57:40 +08:00
$url = "https://github.com/Thaolga/openwrt-nekobox/releases";
2024-11-24 09:42:42 +08:00
$output = shell_exec("wget -qO- $url");
2024-10-26 10:38:22 +08:00
2024-11-24 09:42:42 +08:00
if ($output === null || empty($output)) {
2024-10-26 10:38:22 +08:00
return "Error";
}
2024-11-24 09:42:42 +08:00
preg_match('/\/releases\/tag\/([\d\.]+)/', $output, $matches);
2024-11-17 10:57:40 +08:00
if (isset($matches[1])) {
return cleanVersion($matches[1]);
}
return "Error";
}
function cleanVersion($version) {
2024-11-24 09:42:42 +08:00
$version = explode('-', $version)[0];
return preg_replace('/[^0-9\.]/', '', $version);
2024-10-26 10:38:22 +08:00
}
$currentVersion = getCurrentVersion();
$latestVersion = getLatestVersion();
2024-11-17 10:57:40 +08:00
if ($currentVersion === "Error" || $latestVersion === "Error") {
$response = [
'currentVersion' => $currentVersion,
'latestVersion' => $latestVersion,
'hasUpdate' => false,
'error' => 'Failed to fetch version information'
];
} else {
$hasUpdate = (version_compare($currentVersion, $latestVersion, '<')) ? true : false;
$response = [
'currentVersion' => $currentVersion,
'latestVersion' => $latestVersion,
'hasUpdate' => $hasUpdate
];
}
2024-10-26 10:38:22 +08:00
header('Content-Type: application/json');
echo json_encode($response);
?>