From a389264ec4165a8a08cd0fa320d97ab23fe563ec Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 3 Mar 2023 10:05:55 +1100 Subject: [PATCH] No cosmetic layout fixes, no functional changes --- .editorconfig | 7 +- app/Http/Controllers/VersionController.php | 155 +++++++++++---------- 2 files changed, 83 insertions(+), 79 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6537ca4..a98075a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,9 +3,9 @@ root = true [*] charset = utf-8 end_of_line = lf -insert_final_newline = true -indent_style = space indent_size = 4 +indent_style = tab +insert_final_newline = false trim_trailing_whitespace = true [*.md] @@ -13,3 +13,6 @@ trim_trailing_whitespace = false [*.{yml,yaml}] indent_size = 2 + +[docker-compose.yml] +indent_size = 4 diff --git a/app/Http/Controllers/VersionController.php b/app/Http/Controllers/VersionController.php index 89f1445..0c86ee7 100644 --- a/app/Http/Controllers/VersionController.php +++ b/app/Http/Controllers/VersionController.php @@ -9,104 +9,105 @@ use Illuminate\Support\Facades\Log; class VersionController extends Controller { - const CACHE_TIME = 10; // Time to cache version + const CACHE_TIME = 10; // Time to cache version - const GL_PROJECT = 2; // Gitlab project number - const GL_URL = 'https://dev.dege.au/api/v4'; // Gitlab URL + const GL_PROJECT = 2; // Gitlab project number + const GL_URL = 'https://dev.dege.au/api/v4'; // Gitlab URL - const VERSION_REGEX = '/^v([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?)-([a-z]{3})-([a-z0-9]{8})$/'; + const VERSION_REGEX = '/^v([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?)-([a-z]{3})-([a-z0-9]{8})$/'; - public function main(Request $request,string $version=NULL) { - // Our version is in the format of either: - // v1.2.3-xxx-abcdef01 - Log::info(sprintf('Connection from [%s] reporting version [%s]',$this->getUserIpAddr(),$version)); + public function main(Request $request,string $version=NULL) { + // Our version is in the format of either: + // v1.2.3-xxx-abcdef01 + Log::info(sprintf('Connection from [%s] reporting version [%s]',$this->getUserIpAddr(),$version)); - $matches = []; - if (preg_match(self::VERSION_REGEX,$version,$matches)) { - // If xxx is "dev" we are a development version - switch($matches[3]) { - case 'dev': - $current = Cache::remember('dev',self::CACHE_TIME,function() { - $client = new Client; - $url = sprintf('%s/projects/%d/repository/commits',self::GL_URL,self::GL_PROJECT); + $matches = []; + if (preg_match(self::VERSION_REGEX,$version,$matches)) { + // If xxx is "dev" we are a development version + switch($matches[3]) { + case 'dev': + $current = Cache::remember('dev',self::CACHE_TIME,function() { + $client = new Client; + $url = sprintf('%s/projects/%d/repository/commits',self::GL_URL,self::GL_PROJECT); - $response = $client->request('GET',$url,['form_params'=>['ref_name'=>'BRANCH-2.0','order'=>'default']]); + $response = $client->request('GET',$url,['form_params'=>['ref_name'=>'BRANCH-2.0','order'=>'default']]); - if ($response->getStatusCode() === 200) { - $result = collect(json_decode($response->getBody())); + if ($response->getStatusCode() === 200) { + $result = collect(json_decode($response->getBody())); - return $result->first(); - } + return $result->first(); + } - return NULL; - }); + return NULL; + }); - if ($current) { - $repository = sprintf('v%s-rel-%s',$matches[1],$current->short_id); + if ($current) { + $repository = sprintf('v%s-rel-%s',$matches[1],$current->short_id); - // Find the tag associated with version $matches[1] and see if it is more recent than $matches[4] - return ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)]; + // Find the tag associated with version $matches[1] and see if it is more recent than $matches[4] + return ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)]; - } else - return ['unknown'=>'vn.n.n-dev-hhhhhhhh']; + } else + return ['unknown'=>'vn.n.n-dev-hhhhhhhh']; - case 'rel': - $current = Cache::remember('dev',self::CACHE_TIME,function() { - $client = new Client; - $url = sprintf('%s/projects/%d/repository/tags',self::GL_URL,self::GL_PROJECT); + case 'rel': + $current = Cache::remember('dev',self::CACHE_TIME,function() { + $client = new Client; + $url = sprintf('%s/projects/%d/repository/tags',self::GL_URL,self::GL_PROJECT); - // Find the tag associated with version $matches[1] and see if there is a more recent version number - $response = $client->request('GET',$url,['form_params'=>['ref_name'=>'master','sort'=>'desc']]); + // Find the tag associated with version $matches[1] and see if there is a more recent version number + $response = $client->request('GET',$url,['form_params'=>['ref_name'=>'master','sort'=>'desc']]); - if ($response->getStatusCode() === 200) { - $result = collect(json_decode($response->getBody())); + if ($response->getStatusCode() === 200) { + $result = collect(json_decode($response->getBody())); - return $result->first(); - } + return $result->first(); + } - return NULL; - }); + return NULL; + }); - if ($current) { - $repository = sprintf('v%s-rel-%s',$current->name,$current->commit->short_id); + if ($current) { + $repository = sprintf('v%s-rel-%s',$current->name,$current->commit->short_id); - // If $matches[1] is smaller, "upgrade available" - if ($matches[1] < $current->name) - return ['upgrade'=>$repository]; + // If $matches[1] is smaller, "upgrade available" + if ($matches[1] < $current->name) + return ['upgrade'=>$repository]; - // If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error - elseif ($matches[1] === $current->name) - return ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository]; + // If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error + elseif ($matches[1] === $current->name) + return ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository]; - // if $matches[1] is higher, abort - else - return ['unknown'=>$repository]; + // if $matches[1] is higher, abort + else + return ['unknown'=>$repository]; - } else - return ['unknown'=>'vn.n.n-rel-hhhhhhhh']; - } - } + } else + return ['unknown'=>'vn.n.n-rel-hhhhhhhh']; + } + } - // Return the current version - return ['unknown'=>'vn.n.n-xxxx-hhhhhhhh']; - } + // Return the current version + return ['unknown'=>'vn.n.n-xxxx-hhhhhhhh']; + } - public function getUserIpAddr(){ - $ipaddress = ''; - if (isset($_SERVER['HTTP_CLIENT_IP'])) - $ipaddress = $_SERVER['HTTP_CLIENT_IP']; - else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) - $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; - else if(isset($_SERVER['HTTP_X_FORWARDED'])) - $ipaddress = $_SERVER['HTTP_X_FORWARDED']; - else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) - $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; - else if(isset($_SERVER['HTTP_FORWARDED'])) - $ipaddress = $_SERVER['HTTP_FORWARDED']; - else if(isset($_SERVER['REMOTE_ADDR'])) - $ipaddress = $_SERVER['REMOTE_ADDR']; - else - $ipaddress = 'UNKNOWN'; - return $ipaddress; - } + public function getUserIpAddr(): string + { + if (isset($_SERVER['HTTP_CLIENT_IP'])) + $ipaddress = $_SERVER['HTTP_CLIENT_IP']; + else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) + $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; + else if(isset($_SERVER['HTTP_X_FORWARDED'])) + $ipaddress = $_SERVER['HTTP_X_FORWARDED']; + else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) + $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; + else if(isset($_SERVER['HTTP_FORWARDED'])) + $ipaddress = $_SERVER['HTTP_FORWARDED']; + else if(isset($_SERVER['REMOTE_ADDR'])) + $ipaddress = $_SERVER['REMOTE_ADDR']; + else + $ipaddress = 'UNKNOWN'; + + return $ipaddress; + } }