2023-03-02 23:01:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use Illuminate\Http\Request;
|
2024-01-10 05:09:42 +00:00
|
|
|
use Illuminate\Support\Facades\{Cache,Log};
|
|
|
|
use Illuminate\Support\Str;
|
2023-03-02 23:01:23 +00:00
|
|
|
|
2023-03-02 23:29:39 +00:00
|
|
|
use App\Models\Site;
|
|
|
|
use App\Models\SiteVersion;
|
|
|
|
|
2023-03-02 23:01:23 +00:00
|
|
|
class VersionController extends Controller
|
|
|
|
{
|
2023-03-03 04:22:52 +00:00
|
|
|
private const CACHE_TIME = 86400; // Time to cache version
|
2023-03-02 23:05:55 +00:00
|
|
|
|
2024-01-10 05:09:42 +00:00
|
|
|
private const GH_URL = 'https://api.github.com/repos'; // GitHub URL
|
|
|
|
private const GH_PROJECT = 'leenooks/phpldapadmin';
|
|
|
|
private const GH_TREE = 'master';
|
2023-03-02 23:05:55 +00:00
|
|
|
|
2023-03-03 04:22:52 +00:00
|
|
|
private const VERSION_REGEX = '/^v([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?)-([a-z]{3})-([a-z0-9]{8})$/';
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
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)) {
|
2023-03-03 01:15:20 +00:00
|
|
|
$so = Site::firstOrCreate([
|
|
|
|
'ip_address'=>$this->getUserIpAddr()
|
|
|
|
]);
|
|
|
|
|
|
|
|
$vo = new SiteVersion;
|
|
|
|
$vo->version = $version;
|
|
|
|
$so->versions()->save($vo);
|
|
|
|
|
2023-03-02 23:05:55 +00:00
|
|
|
// 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;
|
2024-01-10 05:09:42 +00:00
|
|
|
$url = sprintf('%s/%s/commits/%s',self::GH_URL,self::GH_PROJECT,self::GH_TREE);
|
2023-03-02 23:05:55 +00:00
|
|
|
|
2024-01-10 05:09:42 +00:00
|
|
|
$response = $client->request('GET',$url,['form_params'=>['per_page'=>'1']]);
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
if ($response->getStatusCode() === 200) {
|
|
|
|
$result = collect(json_decode($response->getBody()));
|
|
|
|
|
2024-01-10 05:09:42 +00:00
|
|
|
return Str::limit($result->get('sha'),8,NULL);
|
2023-03-02 23:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($current) {
|
2024-01-10 05:09:42 +00:00
|
|
|
$repository = sprintf('v%s-rel-%s',$matches[1],$current);
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
// Find the tag associated with version $matches[1] and see if it is more recent than $matches[4]
|
2024-01-10 05:09:42 +00:00
|
|
|
$response = ($matches[4] === $current)
|
2023-03-03 04:22:52 +00:00
|
|
|
? ['action'=>'current','version'=>$repository]
|
2024-01-10 05:09:42 +00:00
|
|
|
: ['action'=>'upgrade','version'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current)];
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
} else
|
2023-03-03 04:31:18 +00:00
|
|
|
$response = ['action'=>'unable','version'=>'vn.n.n-dev-hhhhhhhh'];
|
2023-03-02 23:29:39 +00:00
|
|
|
|
|
|
|
break;
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
case 'rel':
|
2023-03-03 05:00:36 +00:00
|
|
|
$current = Cache::remember('rel',self::CACHE_TIME,function() {
|
2023-03-02 23:05:55 +00:00
|
|
|
$client = new Client;
|
2024-01-10 05:09:42 +00:00
|
|
|
$url = sprintf('%s/%s/tags',self::GH_URL,self::GH_PROJECT);
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
// 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) {
|
2024-01-10 05:09:42 +00:00
|
|
|
$sha = Str::limit($current->commit->sha,8,NULL);
|
|
|
|
$repository = sprintf('v%s-rel-%s',$current->name,$sha);
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
// If $matches[1] is smaller, "upgrade available"
|
|
|
|
if ($matches[1] < $current->name)
|
2023-03-03 04:22:52 +00:00
|
|
|
$response = ['action'=>'upgrade','version'=>$repository];
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
// If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error
|
|
|
|
elseif ($matches[1] === $current->name)
|
2024-01-10 05:09:42 +00:00
|
|
|
$response = ($matches[4] === $sha) ? ['action'=>'current','version'=>$repository] : ['action'=>'mismatch','version'=>$repository];
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
// if $matches[1] is higher, abort
|
|
|
|
else
|
2023-03-03 04:22:52 +00:00
|
|
|
$response = ['action'=>'unknown','version'=>$repository];
|
2023-03-02 23:05:55 +00:00
|
|
|
|
|
|
|
} else
|
2023-03-03 04:31:18 +00:00
|
|
|
$response = ['action'=>'unable','version'=>'vn.n.n-rel-hhhhhhhh'];
|
2023-03-02 23:29:39 +00:00
|
|
|
|
|
|
|
break;
|
2023-03-02 23:05:55 +00:00
|
|
|
}
|
2023-03-02 23:29:39 +00:00
|
|
|
|
|
|
|
$vo->response = $response;
|
|
|
|
$vo->save();
|
|
|
|
|
|
|
|
return $response;
|
2023-03-02 23:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current version
|
2023-03-03 04:31:18 +00:00
|
|
|
return ['action'=>'invalid','version'=>'vn.n.n-xxxx-hhhhhhhh'];
|
2023-03-02 23:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getUserIpAddr(): string
|
|
|
|
{
|
|
|
|
if (isset($_SERVER['HTTP_CLIENT_IP']))
|
|
|
|
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
|
2023-03-02 23:29:39 +00:00
|
|
|
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
|
$ips = preg_split('/,\s*/',$_SERVER['HTTP_X_FORWARDED_FOR']);
|
|
|
|
$ipaddress = $ips[0];
|
|
|
|
}
|
2023-03-02 23:05:55 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-03-02 23:01:23 +00:00
|
|
|
}
|