68 lines
2.0 KiB
PHP
68 lines
2.0 KiB
PHP
<?php
|
||
/*
|
||
* 版本号比较 by sam 20170412
|
||
* @param $version1 版本A 如:5.3.2
|
||
* @param $version2 版本B 如:5.3.0
|
||
* @return int -1版本A小于版本B , 0版本A等于版本B, 1版本A大于版本B
|
||
*
|
||
* 版本号格式注意:
|
||
* 1.要求只包含:点和大于等于0小于等于2147483646的整数 的组合
|
||
* 2.boole型 true置1,false置0
|
||
* 3.不设位默认补0计算,如:版本号5等于版号5.0.0
|
||
* 4.不包括数字 或 负数 的版本号 ,统一按0处理
|
||
*
|
||
* @example:
|
||
* if (versionCompare('5.2.2','5.3.0')<0) {
|
||
* echo '版本1小于版本2';
|
||
* }
|
||
*/
|
||
function versionCompare($versionA, $versionB)
|
||
{
|
||
if ($versionA > 2147483646 || $versionB > 2147483646) {
|
||
throw new Exception('版本号,位数太大暂不支持!', '101');
|
||
}
|
||
$dm = '.';
|
||
$verListA = explode($dm, (string) $versionA);
|
||
$verListB = explode($dm, (string) $versionB);
|
||
|
||
$len = max(count($verListA), count($verListB));
|
||
$i = -1;
|
||
while ($i++ < $len) {
|
||
$verListA[$i] = intval(@$verListA[$i]);
|
||
if ($verListA[$i] < 0) {
|
||
$verListA[$i] = 0;
|
||
}
|
||
$verListB[$i] = intval(@$verListB[$i]);
|
||
if ($verListB[$i] < 0) {
|
||
$verListB[$i] = 0;
|
||
}
|
||
|
||
if ($verListA[$i] > $verListB[$i]) {
|
||
return 1;
|
||
} else if ($verListA[$i] < $verListB[$i]) {
|
||
return -1;
|
||
} else if ($i == ($len - 1)) {
|
||
return 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
$version = $_GET['version'];
|
||
$latestVersion = "0.1.2";
|
||
|
||
if (versionCompare($version, $latestVersion) < 0) {
|
||
$result = [
|
||
'status' => 1,
|
||
'downloadURL' => 'https://tool.y-bi.top/server-monitor-xp-' . $latestVersion . '.zip',
|
||
'newVersion' => $latestVersion,
|
||
];
|
||
} else {
|
||
$result = [
|
||
'status' => 0,
|
||
];
|
||
}
|
||
|
||
header('Content-Type:application/json; charset=utf-8');
|
||
|
||
echo json_encode($result, JSON_UNESCAPED_SLASHES);
|