From bbb2ad13cacf5b4d9209f658ac79a24f83bcf58f Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sat, 13 Nov 2021 22:32:40 +0100 Subject: [PATCH] chg: [internal] Simplified remove version checking --- app/Controller/ServersController.php | 28 +---------- app/Model/Server.php | 74 +++++++++++++++++----------- 2 files changed, 45 insertions(+), 57 deletions(-) diff --git a/app/Controller/ServersController.php b/app/Controller/ServersController.php index f6a79f6a8..bff2a883b 100644 --- a/app/Controller/ServersController.php +++ b/app/Controller/ServersController.php @@ -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 diff --git a/app/Model/Server.php b/app/Model/Server.php index e33f46ab5..677a6e866 100644 --- a/app/Model/Server.php +++ b/app/Model/Server.php @@ -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//"');