Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Helpers/SoftwareVersionService.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Services\Helpers;
4
5
use GuzzleHttp\Client;
6
use Carbon\CarbonImmutable;
7
use Illuminate\Support\Arr;
8
use Illuminate\Contracts\Cache\Repository as CacheRepository;
9
use Pterodactyl\Exceptions\Service\Helper\CdnVersionFetchingException;
10
11
class SoftwareVersionService
12
{
13
public const VERSION_CACHE_KEY = 'pterodactyl:versioning_data';
14
15
private static array $result;
16
17
/**
18
* SoftwareVersionService constructor.
19
*/
20
public function __construct(
21
protected CacheRepository $cache,
22
protected Client $client,
23
) {
24
self::$result = $this->cacheVersionData();
25
}
26
27
/**
28
* Get the latest version of the panel from the CDN servers.
29
*/
30
public function getPanel(): string
31
{
32
return Arr::get(self::$result, 'panel') ?? 'error';
33
}
34
35
/**
36
* Get the latest version of the daemon from the CDN servers.
37
*/
38
public function getDaemon(): string
39
{
40
return Arr::get(self::$result, 'wings') ?? 'error';
41
}
42
43
/**
44
* Get the URL to the discord server.
45
*/
46
public function getDiscord(): string
47
{
48
return Arr::get(self::$result, 'discord') ?? 'https://pterodactyl.io/discord';
49
}
50
51
/**
52
* Get the URL for donations.
53
*/
54
public function getDonations(): string
55
{
56
return Arr::get(self::$result, 'donations') ?? 'https://github.com/sponsors/matthewpi';
57
}
58
59
/**
60
* Determine if the current version of the panel is the latest.
61
*/
62
public function isLatestPanel(): bool
63
{
64
if (config('app.version') === 'canary') {
65
return true;
66
}
67
68
return version_compare(config('app.version'), $this->getPanel()) >= 0;
69
}
70
71
/**
72
* Determine if a passed daemon version string is the latest.
73
*/
74
public function isLatestDaemon(string $version): bool
75
{
76
if ($version === 'develop') {
77
return true;
78
}
79
80
return version_compare($version, $this->getDaemon()) >= 0;
81
}
82
83
/**
84
* Keeps the versioning cache up-to-date with the latest results from the CDN.
85
*/
86
protected function cacheVersionData(): array
87
{
88
return $this->cache->remember(self::VERSION_CACHE_KEY, CarbonImmutable::now()->addMinutes(config('pterodactyl.cdn.cache_time', 60)), function () {
89
try {
90
$response = $this->client->request('GET', config('pterodactyl.cdn.url'));
91
92
if ($response->getStatusCode() === 200) {
93
return json_decode($response->getBody(), true);
94
}
95
96
throw new CdnVersionFetchingException();
97
} catch (\Exception) {
98
return [];
99
}
100
});
101
}
102
}
103
104