misc_pterodactyl-panel/app/Services/Servers/StartupModificationService.php

113 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Services\Servers;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\User;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Models\Server;
use Pterodactyl\Models\ServerVariable;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Traits\Services\HasUserLevels;
class StartupModificationService
{
use HasUserLevels;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Pterodactyl\Services\Servers\VariableValidatorService
*/
private $validatorService;
/**
* StartupModificationService constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
*/
public function __construct(ConnectionInterface $connection, VariableValidatorService $validatorService)
{
2017-09-27 03:54:34 +00:00
$this->connection = $connection;
$this->validatorService = $validatorService;
}
/**
* Process startup modification for a server.
*
* @param \Pterodactyl\Models\Server $server
2019-09-06 04:32:57 +00:00
* @param array $data
2018-01-31 02:36:59 +00:00
* @return \Pterodactyl\Models\Server
*
* @throws \Throwable
*/
2018-01-31 02:36:59 +00:00
public function handle(Server $server, array $data): Server
{
return $this->connection->transaction(function () use ($server, $data) {
if (! empty($data['environment'])) {
$egg = $this->isUserLevel(User::USER_LEVEL_ADMIN) ? ($data['egg_id'] ?? $server->egg_id) : $server->egg_id;
$results = $this->validatorService
->setUserLevel($this->getUserLevel())
->handle($egg, $data['environment']);
foreach ($results as $result) {
ServerVariable::query()->updateOrCreate(
[
'server_id' => $server->id,
'variable_id' => $result->id,
],
['variable_value' => $result->value ?? '']
);
}
}
if ($this->isUserLevel(User::USER_LEVEL_ADMIN)) {
$this->updateAdministrativeSettings($data, $server);
}
// Calling ->refresh() rather than ->fresh() here causes it to return the
// variables as triplicates for some reason? Not entirely sure, should dig
// in more to figure it out, but luckily we have a test case covering this
// specific call so we can be assured we're not breaking it _here_ at least.
//
// TODO(dane): this seems like a red-flag for the code powering the relationship
// that should be looked into more.
return $server->fresh();
});
}
/**
* Update certain administrative settings for a server in the DB.
*
2019-09-06 04:32:57 +00:00
* @param array $data
* @param \Pterodactyl\Models\Server $server
*/
protected function updateAdministrativeSettings(array $data, Server &$server)
{
2018-01-31 02:36:59 +00:00
if (
is_digit(Arr::get($data, 'egg_id'))
2018-01-31 02:36:59 +00:00
&& $data['egg_id'] != $server->egg_id
&& is_null(Arr::get($data, 'nest_id'))
2018-01-31 02:36:59 +00:00
) {
/** @var \Pterodactyl\Models\Egg $egg */
$egg = Egg::query()->select(['nest_id'])->findOrFail($data['egg_id']);
2018-01-31 02:36:59 +00:00
$data['nest_id'] = $egg->nest_id;
}
$server->forceFill([
'installed' => 0,
'startup' => $data['startup'] ?? $server->startup,
'nest_id' => $data['nest_id'] ?? $server->nest_id,
'egg_id' => $data['egg_id'] ?? $server->egg_id,
'skip_scripts' => $data['skip_scripts'] ?? isset($data['skip_scripts']),
'image' => $data['docker_image'] ?? $server->image,
])->save();
}
}