chg: [internal] Simplified remove version checking

pull/7959/head
Jakub Onderka 2021-11-13 22:32:40 +01:00
parent 799f93d088
commit bbb2ad13ca
2 changed files with 45 additions and 57 deletions

View File

@ -1058,7 +1058,7 @@ class ServersController extends AppController
$this->set('advanced_attachments', $advanced_attachments);
// check if the current version of MISP is outdated or not
$version = $this->__checkVersion();
$version = $this->Server->checkRemoteVersion();
$this->set('version', $version);
$gitStatus = $this->Server->getCurrentGitStatus();
$this->set('branch', $gitStatus['branch']);
@ -1305,32 +1305,6 @@ class ServersController extends AppController
return $this->RestResponse->viewData($worker_array);
}
private function __checkVersion()
{
App::uses('SyncTool', 'Tools');
$syncTool = new SyncTool();
try {
$HttpSocket = $syncTool->setupHttpSocket();
$response = $HttpSocket->get('https://api.github.com/repos/MISP/MISP/tags');
$tags = $response->body;
} catch (Exception $e) {
return false;
}
if ($response->isOK() && !empty($tags)) {
$json_decoded_tags = json_decode($tags);
// find the latest version tag in the v[major].[minor].[hotfix] format
for ($i = 0; $i < count($json_decoded_tags); $i++) {
if (preg_match('/^v[0-9]+\.[0-9]+\.[0-9]+$/', $json_decoded_tags[$i]->name)) {
break;
}
}
return $this->Server->checkVersion($json_decoded_tags[$i]->name);
} else {
return false;
}
}
public function idTranslator($localId = null)
{
// We retrieve the list of remote servers that we can query

View File

@ -2335,36 +2335,6 @@ class Server extends AppModel
return true;
}
public function checkVersion($newest)
{
$version_array = $this->checkMISPVersion();
$current = 'v' . $version_array['major'] . '.' . $version_array['minor'] . '.' . $version_array['hotfix'];
$newest_array = $this->__dissectVersion($newest);
$upToDate = $this->__compareVersions(array($version_array['major'], $version_array['minor'], $version_array['hotfix']), $newest_array, 0);
return array('current' => $current, 'newest' => $newest, 'upToDate' => $upToDate);
}
private function __dissectVersion($version)
{
$version = substr($version, 1);
return explode('.', $version);
}
private function __compareVersions($current, $newest, $i)
{
if ($current[$i] == $newest[$i]) {
if ($i < 2) {
return $this->__compareVersions($current, $newest, $i+1);
} else {
return 'same';
}
} elseif ($current[$i] < $newest[$i]) {
return 'older';
} else {
return 'newer';
}
}
public function getFileRules()
{
$validItems = array(
@ -3827,6 +3797,50 @@ class Server extends AppModel
return true;
}
/**
* @param string $newest
* @return array
* @throws JsonException
*/
private function checkVersion($newest)
{
$version_array = $this->checkMISPVersion();
$current = implode('.', $version_array);
$upToDate = version_compare($current, substr(1, $newest));
if ($upToDate === 0) {
$upToDate = 'same';
} else {
$upToDate = $upToDate === -1 ? 'older' : 'newer';
}
return array('current' => 'v' . $current, 'newest' => $newest, 'upToDate' => $upToDate);
}
/**
* Fetch latest MISP version from GitHub
* @return array|false
* @throws HttpSocketJsonException
*/
public function checkRemoteVersion()
{
$HttpSocket = $this->setupHttpSocket(null, null, 3);
try {
$response = $HttpSocket->get('https://api.github.com/repos/MISP/MISP/tags?per_page=10');
} catch (Exception $e) {
return false;
}
if ($response->isOK()) {
$json_decoded_tags = $response->json();
// find the latest version tag in the v[major].[minor].[hotfix] format
foreach ($json_decoded_tags as $tag)
if (preg_match('/^v[0-9]+\.[0-9]+\.[0-9]+$/', $tag['name'])) {
return $this->checkVersion($tag['name']);
}
}
return false;
}
public function getCurrentGitStatus()
{
$latestCommit = exec('timeout 3 git ls-remote https://github.com/MISP/MISP | head -1 | sed "s/HEAD//"');