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

169 lines
4.4 KiB
PHP
Raw Normal View History

2017-08-24 02:34:11 +00:00
<?php
namespace Pterodactyl\Services\Helpers;
use Exception;
2020-06-25 04:54:56 +00:00
use Carbon\CarbonImmutable;
2019-12-22 22:03:49 +00:00
use Illuminate\Support\Arr;
2022-12-15 00:05:46 +00:00
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Http;
2017-08-24 02:34:11 +00:00
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Pterodactyl\Exceptions\Service\Helper\CdnVersionFetchingException;
class SoftwareVersionService
{
2021-01-23 20:33:34 +00:00
public const VERSION_CACHE_KEY = 'pterodactyl:versioning_data';
2022-12-15 00:05:46 +00:00
public const GIT_VERSION_CACHE_KEY = 'pterodactyl:git_data';
2019-12-22 22:03:49 +00:00
private static array $result;
2017-08-24 02:34:11 +00:00
/**
* SoftwareVersionService constructor.
*/
2022-12-15 00:05:46 +00:00
public function __construct(private CacheRepository $cache)
{
2019-12-22 22:03:49 +00:00
self::$result = $this->cacheVersionData();
2017-08-24 02:34:11 +00:00
}
/**
2022-12-15 00:05:46 +00:00
* Return the current version of the panel that is being used.
2017-08-24 02:34:11 +00:00
*/
2022-12-15 00:05:46 +00:00
public function getCurrentVersion(): string
{
return config('app.version');
}
/**
* Returns the latest version of the panel from the CDN servers.
*/
public function getLatestPanel(): string
2017-08-24 02:34:11 +00:00
{
2019-12-22 22:03:49 +00:00
return Arr::get(self::$result, 'panel') ?? 'error';
2017-08-24 02:34:11 +00:00
}
/**
2022-12-15 00:05:46 +00:00
* Returns the latest version of the Wings from the CDN servers.
2017-08-24 02:34:11 +00:00
*/
2022-12-15 00:05:46 +00:00
public function getLatestWings(): string
2017-08-24 02:34:11 +00:00
{
2020-04-13 01:14:59 +00:00
return Arr::get(self::$result, 'wings') ?? 'error';
2017-08-24 02:34:11 +00:00
}
/**
2022-12-15 00:05:46 +00:00
* Returns the URL to the discord server.
2017-08-24 02:34:11 +00:00
*/
public function getDiscord(): string
2017-08-24 02:34:11 +00:00
{
2019-12-22 22:03:49 +00:00
return Arr::get(self::$result, 'discord') ?? 'https://pterodactyl.io/discord';
}
/**
2022-12-15 00:05:46 +00:00
* Returns the URL for donations.
2019-12-22 22:03:49 +00:00
*/
public function getDonations(): string
2019-12-22 22:03:49 +00:00
{
return Arr::get(self::$result, 'donations') ?? 'https://github.com/sponsors/matthewpi';
2017-08-24 02:34:11 +00:00
}
/**
* Determine if the current version of the panel is the latest.
*/
public function isLatestPanel(): bool
2017-08-24 02:34:11 +00:00
{
2022-12-15 00:05:46 +00:00
$version = $this->getCurrentVersion();
if ($version === 'canary') {
2017-08-24 02:34:11 +00:00
return true;
}
2022-12-15 00:05:46 +00:00
return version_compare($version, $this->getLatestPanel()) >= 0;
2017-08-24 02:34:11 +00:00
}
/**
* Determine if a passed daemon version string is the latest.
*/
2022-12-15 00:05:46 +00:00
public function isLatestWings(string $version): bool
2017-08-24 02:34:11 +00:00
{
2022-12-15 00:05:46 +00:00
if ($version === 'develop' || Str::startsWith($version, 'dev-')) {
2017-08-24 02:34:11 +00:00
return true;
}
2022-12-15 00:05:46 +00:00
return version_compare($version, $this->getLatestWings()) >= 0;
}
/**
* ?
*/
public function getVersionData(): array
{
$versionData = $this->versionData();
if ($versionData['is_git']) {
$git = $versionData['version'];
} else {
$git = null;
}
return [
'panel' => [
'current' => $this->getCurrentVersion(),
'latest' => $this->getLatestPanel(),
],
'wings' => [
'latest' => $this->getLatestWings(),
],
'git' => $git,
];
}
/**
* Return version information for the footer.
*/
protected function versionData(): array
{
return $this->cache->remember(self::GIT_VERSION_CACHE_KEY, CarbonImmutable::now()->addSeconds(15), 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,
];
});
2017-08-24 02:34:11 +00:00
}
/**
* Keeps the versioning cache up-to-date with the latest results from the CDN.
*/
protected function cacheVersionData(): array
2017-08-24 02:34:11 +00:00
{
return $this->cache->remember(self::VERSION_CACHE_KEY, CarbonImmutable::now()->addMinutes(config('pterodactyl.cdn.cache_time', 60)), function () {
2017-08-24 02:34:11 +00:00
try {
2022-12-15 00:05:46 +00:00
$response = Http::get(config('pterodactyl.cdn.url'));
2017-08-24 02:34:11 +00:00
2022-12-15 00:05:46 +00:00
if ($response->status() === 200) {
return json_decode($response->body(), true);
2017-08-24 02:34:11 +00:00
}
2021-01-23 20:33:34 +00:00
throw new CdnVersionFetchingException();
} catch (Exception) {
2019-12-22 22:03:49 +00:00
return [];
2017-08-24 02:34:11 +00:00
}
});
}
}