Files
pterodactyl-panel/app/Extensions/Backups/BackupManager.php
T

233 lines
6.2 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Extensions\Backups;
2020-05-03 20:49:09 -07:00
use Aws\S3\S3Client;
2026-10-05 18:10:18 -04:00
use Closure;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Foundation\Application;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Arr;
use InvalidArgumentException;
2022-10-14 10:59:20 -06:00
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
use Pterodactyl\Support\JsonValueGuard;
class BackupManager
{
2022-10-14 10:59:20 -06:00
protected ConfigRepository $config;
/**
* The array of resolved backup drivers.
2026-10-05 18:10:18 -04:00
*
* @var array<string, FilesystemAdapter>
*/
2022-10-14 10:59:20 -06:00
protected array $adapters = [];
2026-10-05 18:10:18 -04:00
/** @var array<string, Closure(Application, BackupDiskConfig): FilesystemAdapter> Registered custom driver creators. */
protected array $customCreators = [];
/**
* BackupManager constructor.
*/
2022-10-14 10:59:20 -06:00
public function __construct(protected Application $app)
{
2022-10-14 10:59:20 -06:00
$this->config = $app->make(ConfigRepository::class);
}
/**
* Returns a backup adapter instance.
*/
2024-10-21 19:18:20 -06:00
public function adapter(?string $name = null): FilesystemAdapter
{
return $this->get($name ?: $this->getDefaultAdapter());
}
/**
* Set the given backup adapter instance.
*/
2022-10-14 10:59:20 -06:00
public function set(string $name, FilesystemAdapter $disk): self
{
$this->adapters[$name] = $disk;
return $this;
}
/**
2026-10-05 18:10:18 -04:00
* Creates a new Wings adapter.
*
* @param BackupDiskConfig $config
*/
2026-10-05 18:10:18 -04:00
public function createWingsAdapter(array $config): FilesystemAdapter
{
2026-10-05 18:10:18 -04:00
return new InMemoryFilesystemAdapter;
}
/**
2026-10-05 18:10:18 -04:00
* Creates a new S3 adapter.
*
* @param S3BackupDiskConfig $config
*/
2026-10-05 18:10:18 -04:00
public function createS3Adapter(array $config): FilesystemAdapter
{
2026-10-05 18:10:18 -04:00
$config['version'] = 'latest';
2026-10-05 18:10:18 -04:00
if (! empty($config['key']) && ! empty($config['secret'])) {
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
}
2026-10-05 18:10:18 -04:00
$client = new S3Client($config);
2026-10-05 18:10:18 -04:00
return new S3Filesystem($client, $config['bucket'], $config['prefix'] ?? '', $config['options'] ?? []);
}
/**
2026-10-05 18:10:18 -04:00
* Get the default backup driver name.
*/
2026-10-05 18:10:18 -04:00
public function getDefaultAdapter(): string
{
2026-10-05 18:10:18 -04:00
return JsonValueGuard::string($this->config->get('backups.default'));
}
/**
2026-10-05 18:10:18 -04:00
* Set the default session driver name.
*/
2026-10-05 18:10:18 -04:00
public function setDefaultAdapter(string $name): void
{
2026-10-05 18:10:18 -04:00
$this->config->set('backups.default', $name);
}
/**
2026-10-05 18:10:18 -04:00
* Unset the given adapter instances.
*
* @param string|string[] $adapter
*/
2026-10-05 18:10:18 -04:00
public function forget(array|string $adapter): self
{
2026-10-05 18:10:18 -04:00
$adapters = is_string($adapter) ? [$adapter] : $adapter;
foreach ($adapters as $adapterName) {
unset($this->adapters[$adapterName]);
}
2026-10-05 18:10:18 -04:00
return $this;
}
2026-10-05 18:10:18 -04:00
/**
* Register a custom adapter creator closure.
*/
/** @param Closure(Application, BackupDiskConfig): FilesystemAdapter $callback */
public function extend(string $adapter, Closure $callback): self
{
$this->customCreators[$adapter] = $callback;
return $this;
}
/**
2026-10-05 18:10:18 -04:00
* Gets a backup adapter.
*/
2026-10-05 18:10:18 -04:00
protected function get(string $name): FilesystemAdapter
{
2026-10-05 18:10:18 -04:00
return $this->adapters[$name] = $this->resolve($name);
}
/**
2026-10-05 18:10:18 -04:00
* Resolve the given backup disk.
*/
2026-10-05 18:10:18 -04:00
protected function resolve(string $name): FilesystemAdapter
{
2026-10-05 18:10:18 -04:00
$config = $this->getConfig($name);
throw_if(empty($config['adapter']), InvalidArgumentException::class, "Backup disk [$name] does not have a configured adapter.");
$adapter = $config['adapter'];
if (isset($this->customCreators[$adapter])) {
return $this->callCustomCreator($config);
}
return match ($adapter) {
'wings' => $this->createWingsAdapter($config),
's3' => $this->createS3Adapter($this->s3Config($config)),
default => throw new InvalidArgumentException("Adapter [$adapter] is not supported."),
};
}
/**
2026-10-05 18:10:18 -04:00
* Calls a custom creator for a given adapter type.
*
* @param BackupDiskConfig&array{adapter: string} $config
*/
2026-10-05 18:10:18 -04:00
protected function callCustomCreator(array $config): FilesystemAdapter
{
2026-10-05 18:10:18 -04:00
return $this->customCreators[$config['adapter']]($this->app, $config);
}
/**
2026-10-05 18:10:18 -04:00
* Returns the configuration associated with a given backup type.
*
2026-10-05 18:10:18 -04:00
* @return BackupDiskConfig
*/
2026-10-05 18:10:18 -04:00
protected function getConfig(string $name): array
{
2026-10-05 18:10:18 -04:00
$config = $this->config->get("backups.disks.$name") ?: [];
JsonValueGuard::assertValue($config);
if (! is_array($config)) {
return [];
}
2026-10-05 18:10:18 -04:00
$adapter = $config['adapter'] ?? null;
if (! is_string($adapter) || $adapter === '') {
return [];
}
$normalized = ['adapter' => $adapter];
foreach ($config as $key => $value) {
if ($key === 'adapter') {
continue;
}
if (in_array($key, ['key', 'secret', 'token', 'bucket', 'prefix'], true)) {
if (is_string($value)) {
$normalized[$key] = $value;
}
continue;
}
if ($key === 'options') {
if (is_array($value)) {
$options = [];
foreach ($value as $option => $optionValue) {
if (is_string($option)) {
$options[$option] = $optionValue;
}
}
$normalized['options'] = $options;
}
continue;
}
$normalized[$key] = $value;
}
return $normalized;
}
/**
2026-10-05 18:10:18 -04:00
* @param BackupDiskConfig&array{adapter: string} $config
* @return S3BackupDiskConfig
*/
2026-10-05 18:10:18 -04:00
private function s3Config(array $config): array
{
2026-10-05 18:10:18 -04:00
$bucket = $config['bucket'] ?? null;
throw_if(! is_string($bucket) || $bucket === '', InvalidArgumentException::class, 'The S3 backup adapter requires a bucket.');
2026-10-05 18:10:18 -04:00
return array_merge($config, ['bucket' => $bucket]);
}
}