misc_pterodactyl-panel/app/Services/Helpers/SoftwareVersionService.php

192 lines
4.7 KiB
PHP
Raw Normal View History

2017-08-24 02:34:11 +00:00
<?php
namespace Pterodactyl\Services\Helpers;
use Exception;
use GuzzleHttp\Client;
2020-06-25 04:54:56 +00:00
use Carbon\CarbonImmutable;
2019-12-22 22:03:49 +00:00
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
2017-08-24 02:34:11 +00:00
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Pterodactyl\Exceptions\Service\Helper\CdnVersionFetchingException;
class SoftwareVersionService
{
2019-12-22 22:03:49 +00:00
const VERSION_CACHE_KEY = 'pterodactyl:versioning_data';
/**
* @var array
*/
private static array $result;
2017-08-24 02:34:11 +00:00
/**
* @var \Illuminate\Contracts\Cache\Repository
*/
protected CacheRepository $cache;
2017-08-24 02:34:11 +00:00
/**
* @var \GuzzleHttp\Client
*/
protected Client $client;
2017-08-24 02:34:11 +00:00
/**
* SoftwareVersionService constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param \GuzzleHttp\Client $client
2017-08-24 02:34:11 +00:00
*/
public function __construct(
CacheRepository $cache,
2019-12-22 22:03:49 +00:00
Client $client
2017-08-24 02:34:11 +00:00
) {
$this->cache = $cache;
$this->client = $client;
2019-12-22 22:03:49 +00:00
self::$result = $this->cacheVersionData();
2017-08-24 02:34:11 +00:00
}
/**
* Gets the current version of the panel that is being used.
2017-08-24 02:34:11 +00:00
*
* @return string
*/
public function getVersion(): string
2017-08-24 02:34:11 +00:00
{
return config()->get('app.version');
2017-08-24 02:34:11 +00:00
}
/**
* Get the latest version of the panel from the CDN servers.
2017-08-24 02:34:11 +00:00
*
* @return string
*/
public function getLatestPanel(): string
2017-08-24 02:34:11 +00:00
{
return Arr::get(self::$result, 'panel') ?? 'error';
2019-12-22 22:03:49 +00:00
}
/**
* Get the latest version of wings from the CDN servers.
2019-12-22 22:03:49 +00:00
*
* @return string
*/
public function getLatestWings(): string
2019-12-22 22:03:49 +00:00
{
return Arr::get(self::$result, 'wings') ?? 'error';
2017-08-24 02:34:11 +00:00
}
/**
* Determine if the current version of the panel is the latest.
*
* @return bool
*/
public function isLatestPanel()
{
$version = $this->getVersion();
if ($version === 'canary') {
2017-08-24 02:34:11 +00:00
return true;
}
return version_compare($version, $this->getLatestPanel()) >= 0;
2017-08-24 02:34:11 +00:00
}
/**
* Determine if a passed wings version is the latest.
2017-08-24 02:34:11 +00:00
*
* @param string $version
*
2017-08-24 02:34:11 +00:00
* @return bool
*/
public function isLatestWings(string $version)
2017-08-24 02:34:11 +00:00
{
// If the version is 'canary' or starts with 'dev-', mark it as the latest.
if ($version === 'canary' || Str::startsWith($version, 'dev-')) {
2017-08-24 02:34:11 +00:00
return true;
}
return version_compare($version, $this->getLatestWings()) >= 0;
2017-08-24 02:34:11 +00:00
}
/**
* Keeps the versioning cache up-to-date with the latest results from the CDN.
2019-12-22 22:03:49 +00:00
*
* @return array
2017-08-24 02:34:11 +00:00
*/
protected function cacheVersionData()
{
2020-06-25 04:54:56 +00:00
return $this->cache->remember(self::VERSION_CACHE_KEY, CarbonImmutable::now()->addMinutes(config()->get('pterodactyl.cdn.cache_time', 60)), function () {
2017-08-24 02:34:11 +00:00
try {
2019-12-22 22:03:49 +00:00
$response = $this->client->request('GET', config()->get('pterodactyl.cdn.url'));
2017-08-24 02:34:11 +00:00
if ($response->getStatusCode() !== 200) {
throw new CdnVersionFetchingException;
2017-08-24 02:34:11 +00:00
}
return json_decode($response->getBody(), true);
2017-08-24 02:34:11 +00:00
} catch (Exception $exception) {
2019-12-22 22:03:49 +00:00
return [];
2017-08-24 02:34:11 +00:00
}
});
}
/**
* Return version information for the footer.
*
* @return array
*/
protected function versionData(): array
{
return $this->cache->remember('git-version', 5, function () {
$configVersion = config()->get('app.version');
if (file_exists(base_path('.git/HEAD'))) {
$head = explode(' ', file_get_contents(base_path('.git/HEAD')));
if (array_key_exists(1, $head)) {
$path = base_path('.git/' . trim($head[1]));
}
}
if (isset($path) && file_exists($path)) {
return [
'version' => substr(file_get_contents($path), 0, 8),
'is_git' => true,
];
}
return [
'version' => $configVersion,
'is_git' => false,
];
});
}
/**
* ?
*
* @return array
*/
public function getVersionData(): array
{
$versionData = $this->versionData();
if ($versionData['is_git']) {
$git = $versionData['version'];
} else {
$git = null;
}
return [
'panel' => [
'current' => $this->getVersion(),
'latest' => $this->getLatestPanel(),
],
'wings' => [
'latest' => $this->getLatestWings(),
],
'git' => $git,
];
}
2017-08-24 02:34:11 +00:00
}