Files

113 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Servers;
use Illuminate\Support\Facades\DB;
2026-10-11 09:09:51 -04:00
use Illuminate\Support\Facades\Validator;
2026-10-05 18:10:18 -04:00
use LogicException;
use Pterodactyl\Contracts\Servers\ChangesServerEgg;
2026-10-08 19:17:30 -04:00
use Pterodactyl\Contracts\Servers\UpdatesServerDockerImage;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Servers\UpdatesServerStartup;
2026-10-11 03:13:43 -04:00
use Pterodactyl\Contracts\Servers\UpdatesServerVariable;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\User;
use Pterodactyl\Services\Servers\VariableValidatorService;
2026-10-11 20:11:13 -04:00
use UnexpectedValueException;
2026-10-05 18:10:18 -04:00
final readonly class UpdateServerStartup implements UpdatesServerStartup
{
public function __construct(
private VariableValidatorService $validatorService,
private ChangesServerEgg $eggs,
2026-10-08 19:17:30 -04:00
private UpdatesServerDockerImage $dockerImage,
2026-10-11 03:13:43 -04:00
private UpdatesServerVariable $variables,
2026-10-05 18:10:18 -04:00
) {}
/**
2026-10-11 23:50:53 -04:00
* The environment maps each egg variable to its value, which the egg's own variable rules
* check. Below the admin level the environment is the only field.
2026-10-11 09:09:51 -04:00
*
* @return NormalizedValidationRules
*/
public static function rules(int $userLevel = User::USER_LEVEL_USER): array
{
if ($userLevel !== User::USER_LEVEL_ADMIN) {
return ['environment' => ['present', 'array']];
}
$rules = CreateServer::rules();
return [
'startup' => $rules['startup'],
'environment' => ['present', 'array'],
'egg_id' => ['required', 'exists:eggs,id'],
'docker_image' => $rules['image'],
'skip_scripts' => ['present', 'boolean'],
];
}
2026-10-11 23:18:58 -04:00
public function update(Server $server, array $input, int $userLevel = User::USER_LEVEL_USER): Server
2026-10-11 09:09:51 -04:00
{
2026-10-11 23:18:58 -04:00
// Only the fields given are validated, and fields above the user level are ignored.
$rules = array_map(fn (array $rules): array => ['sometimes', ...$rules], self::rules($userLevel));
$validated = Validator::make($input, $rules)->safe();
2026-10-11 09:09:51 -04:00
// Laravel's typed accessors would turn every value into one type, but the egg's
// variable rules have to see each value as it was sent.
2026-10-11 20:11:13 -04:00
$environment = [];
2026-10-11 23:18:58 -04:00
foreach ($validated->array('environment') as $key => $value) {
2026-10-11 20:11:13 -04:00
throw_unless(is_string($key) && ($value === null || is_scalar($value)), UnexpectedValueException::class, 'Server environment variables must map names to scalar values.');
$environment[$key] = $value;
}
2026-10-11 09:09:51 -04:00
2026-10-11 23:18:58 -04:00
return DB::transaction(function () use ($server, $validated, $environment, $userLevel): Server {
$eggId = $validated->has('egg_id') ? $validated->integer('egg_id') : null;
2026-10-05 18:10:18 -04:00
if ($userLevel === User::USER_LEVEL_ADMIN && $eggId !== null && $server->egg_id !== $eggId) {
// Only administrators may move a server to another egg. Values the
// new egg still accepts are kept, and anything submitted below
// replaces the defaults the move resets.
$server = $this->eggs->change($server, Egg::query()->findOrFail($eggId), keepVariables: true);
}
2026-10-11 23:18:58 -04:00
if ($environment !== []) {
2026-10-05 18:10:18 -04:00
$egg = $server->relationLoaded('egg') && $server->egg instanceof Egg
? $server->egg
: Egg::query()->findOrFail($server->egg_id);
2026-10-12 00:21:31 -04:00
$results = $this->validatorService->validate($egg, $environment, $userLevel);
2026-10-05 18:10:18 -04:00
foreach ($results as $result) {
2026-10-11 03:13:43 -04:00
$this->variables->update($server, $result->id, $result->value === null ? '' : (string) $result->value);
2026-10-05 18:10:18 -04:00
}
}
if ($userLevel === User::USER_LEVEL_ADMIN) {
$server->fill([
2026-10-11 23:18:58 -04:00
'startup' => $validated['startup'] ?? $server->startup,
'skip_scripts' => $validated['skip_scripts'] ?? false,
2026-10-05 18:10:18 -04:00
])->save();
2026-10-08 19:17:30 -04:00
// Image changes go through their own contract so extensions wrapping
// it see this path too. Skip it when the image is unchanged.
2026-10-11 23:18:58 -04:00
$image = $validated->has('docker_image') ? $validated->string('docker_image')->toString() : null;
2026-10-08 19:17:30 -04:00
if ($image !== null && $image !== $server->image) {
$server = $this->dockerImage->update($server, $image);
}
2026-10-05 18:10:18 -04:00
}
2026-10-08 17:22:14 -04:00
// Use fresh() rather than refresh(). refresh() reloads every loaded
// relation through an eager load, which rebuilds Server::variables()
// on a blank model. That relation captures $this->id in its join, so
// the rebuilt join loses the server filter and the reloaded variables
// come back wrong. fresh() returns a new instance and leaves the
// relation unloaded.
2026-10-05 18:10:18 -04:00
$fresh = $server->fresh();
throw_if($fresh === null, LogicException::class, 'The modified server no longer exists.');
return $fresh;
});
}
}