139 lines
5.2 KiB
PHP
139 lines
5.2 KiB
PHP
<?php
|
|
namespace Pterodactyl\Http\Controllers\Admin\Extensions\s3manager;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Illuminate\View\Factory as ViewFactory;
|
|
use Illuminate\Support\Facades\File;
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
use Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Admin\BlueprintAdminLibrary as BlueprintExtensionLibrary;
|
|
|
|
class s3managerExtensionController extends Controller
|
|
{
|
|
public function __construct(
|
|
private ViewFactory $view,
|
|
private BlueprintExtensionLibrary $blueprint,
|
|
) {}
|
|
|
|
/**
|
|
* Handle both GET and POST requests.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
$message = null;
|
|
|
|
// Handle backup driver selection
|
|
if ($request->has('save_driver')) {
|
|
\Artisan::call('config:clear');
|
|
$driver = $request->input('APP_BACKUP_DRIVER');
|
|
if (!in_array($driver, ['s3', 'wings'])) {
|
|
// Use the 'message' variable to pass a message to the view
|
|
$message = 'Invalid backup driver selected.';
|
|
return $this->view->make(
|
|
'admin.extensions.{identifier}.index', [
|
|
'root' => "/admin/extensions/{identifier}",
|
|
'blueprint' => $this->blueprint,
|
|
'message' => $message, // Pass the message to the view
|
|
]
|
|
);
|
|
}
|
|
|
|
$this->setEnvironmentValue([
|
|
'APP_BACKUP_DRIVER' => $driver,
|
|
]);
|
|
|
|
$message = 'Backup driver updated successfully!';
|
|
}
|
|
|
|
// Only validate and save S3-related values if `save_env` was used
|
|
if ($request->has('save_env')) {
|
|
\Artisan::call('config:clear');
|
|
$endpoint = rtrim($request->input('AWS_ENDPOINT'), '/');
|
|
$validatedUrl = filter_var($endpoint, FILTER_VALIDATE_URL);
|
|
|
|
if (!$validatedUrl) {
|
|
$message = 'Invalid endpoint URL.';
|
|
return $this->view->make(
|
|
'admin.extensions.{identifier}.index', [
|
|
'root' => "/admin/extensions/{identifier}",
|
|
'blueprint' => $this->blueprint,
|
|
'message' => $message,
|
|
]
|
|
);
|
|
}
|
|
|
|
$allowedStorageClasses = ['STANDARD', 'STANDARD_IA', 'GLACIER'];
|
|
$storageClass = strtoupper($request->input('AWS_BACKUPS_STORAGE_CLASS'));
|
|
|
|
if (!in_array($storageClass, $allowedStorageClasses)) {
|
|
$message = 'Invalid storage class selected.';
|
|
return $this->view->make(
|
|
'admin.extensions.{identifier}.index', [
|
|
'root' => "/admin/extensions/{identifier}",
|
|
'blueprint' => $this->blueprint,
|
|
'message' => $message,
|
|
]
|
|
);
|
|
}
|
|
|
|
$maxPartSizeGB = min((int) $request->input('BACKUP_MAX_PART_SIZE_GB'), 5);
|
|
$maxPartSizeBytes = $maxPartSizeGB * 1024 * 1024 * 1024;
|
|
|
|
$this->setEnvironmentValue([
|
|
'AWS_DEFAULT_REGION' => $request->input('AWS_DEFAULT_REGION'),
|
|
'AWS_ACCESS_KEY_ID' => $request->input('AWS_ACCESS_KEY_ID'),
|
|
'AWS_SECRET_ACCESS_KEY' => $request->input('AWS_SECRET_ACCESS_KEY'),
|
|
'AWS_BACKUPS_BUCKET' => $request->input('AWS_BACKUPS_BUCKET'),
|
|
'AWS_ENDPOINT' => $validatedUrl,
|
|
'AWS_USE_PATH_STYLE_ENDPOINT' => $request->input('AWS_USE_PATH_STYLE_ENDPOINT') === 'true' ? 'true' : 'false',
|
|
'AWS_BACKUPS_STORAGE_CLASS' => $storageClass,
|
|
'BACKUP_MAX_PART_SIZE' => $maxPartSizeBytes,
|
|
'BACKUP_PRESIGNED_URL_LIFESPAN' => (int) $request->input('BACKUP_PRESIGNED_URL_LIFESPAN'),
|
|
]);
|
|
|
|
$message = 'Environment variables updated successfully!';
|
|
}
|
|
|
|
// Return the view with the message
|
|
return $this->view->make(
|
|
'admin.extensions.{identifier}.index', [
|
|
'root' => "/admin/extensions/{identifier}",
|
|
'blueprint' => $this->blueprint,
|
|
'message' => $message, // Pass the message to the view
|
|
]
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* Handle POST requests by delegating to index().
|
|
*/
|
|
public function post(Request $request): View
|
|
{
|
|
return $this->index($request);
|
|
}
|
|
|
|
protected function setEnvironmentValue(array $values)
|
|
{
|
|
$envPath = base_path('.env');
|
|
$envContent = File::get($envPath);
|
|
|
|
foreach ($values as $key => $value) {
|
|
// If the key already exists, replace it with the new value.
|
|
$pattern = "/^$key=.*$/m";
|
|
$replacement = $key . '=' . $value;
|
|
|
|
if (preg_match($pattern, $envContent)) {
|
|
// If the key exists, replace the line.
|
|
$envContent = preg_replace($pattern, $replacement, $envContent);
|
|
} else {
|
|
// If the key doesn't exist, append it to the end of the file.
|
|
$envContent .= "\n$replacement";
|
|
}
|
|
}
|
|
|
|
// Write the updated content back to the .env file.
|
|
File::put($envPath, $envContent);
|
|
}
|
|
}
|