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

150 lines
4.3 KiB
PHP
Raw Normal View History

2017-08-24 02:34:11 +00:00
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Helpers;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Exceptions\Service\Helper\CdnVersionFetchingException;
class SoftwareVersionService
{
const VERSION_CACHE_KEY = 'pterodactyl:versions';
/**
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;
/**
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* SoftwareVersionService constructor.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param \GuzzleHttp\Client $client
* @param \Illuminate\Contracts\Config\Repository $config
*/
public function __construct(
CacheRepository $cache,
Client $client,
ConfigRepository $config
) {
$this->cache = $cache;
$this->client = $client;
$this->config = $config;
$this->cacheVersionData();
}
/**
* Get the latest version of the panel from the CDN servers.
*
* @return string
*/
public function getPanel()
{
return object_get($this->cache->get(self::VERSION_CACHE_KEY), 'panel', 'error');
}
/**
* Get the latest version of the daemon from the CDN servers.
*
* @return string
*/
public function getDaemon()
{
return object_get($this->cache->get(self::VERSION_CACHE_KEY), 'daemon', 'error');
}
/**
* Get the URL to the discord server.
*
* @return string
*/
public function getDiscord()
{
return object_get($this->cache->get(self::VERSION_CACHE_KEY), 'discord', 'https://pterodactyl.io/discord');
}
/**
* Determine if the current version of the panel is the latest.
*
* @return bool
*/
public function isLatestPanel()
{
if ($this->config->get('app.version') === 'canary') {
return true;
}
return version_compare($this->config->get('app.version'), $this->getPanel()) >= 0;
}
/**
* Determine if a passed daemon version string is the latest.
*
* @param string $version
* @return bool
*/
public function isLatestDaemon($version)
{
if ($version === '0.0.0-canary') {
return true;
}
return version_compare($version, $this->getDaemon()) >= 0;
}
/**
* Keeps the versioning cache up-to-date with the latest results from the CDN.
*/
protected function cacheVersionData()
{
$this->cache->remember(self::VERSION_CACHE_KEY, $this->config->get('pterodactyl.cdn.cache_time'), function () {
try {
$response = $this->client->request('GET', $this->config->get('pterodactyl.cdn.url'));
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody());
}
throw new CdnVersionFetchingException;
} catch (Exception $exception) {
return (object) [];
}
});
}
}