pla-version/app/Http/Controllers/VersionController.php

136 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use App\Models\Site;
use App\Models\SiteVersion;
class VersionController extends Controller
{
const CACHE_TIME = 86400; // Time to cache version
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})$/';
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)) {
$so = Site::firstOrCreate([
'ip_address'=>$this->getUserIpAddr()
]);
$vo = new SiteVersion;
$vo->version = $version;
$so->versions()->save($vo);
// 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']]);
if ($response->getStatusCode() === 200) {
$result = collect(json_decode($response->getBody()));
return $result->first();
}
return NULL;
});
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]
$response = ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)];
} else
$response = ['unknown'=>'vn.n.n-dev-hhhhhhhh'];
break;
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']]);
if ($response->getStatusCode() === 200) {
$result = collect(json_decode($response->getBody()));
return $result->first();
}
return NULL;
});
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)
$response = ['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)
$response = ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository];
// if $matches[1] is higher, abort
else
$response = ['unknown'=>$repository];
} else
$response = ['unknown'=>'vn.n.n-rel-hhhhhhhh'];
break;
}
$vo->response = $response;
$vo->save();
return $response;
}
// Return the current version
return ['unknown'=>'vn.n.n-xxxx-hhhhhhhh'];
}
public function getUserIpAddr(): string
{
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = preg_split('/,\s*/',$_SERVER['HTTP_X_FORWARDED_FOR']);
$ipaddress = $ips[0];
}
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;
}
}