2017-07-25 02:34:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2020-10-08 04:56:44 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Pterodactyl\Models\Egg;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-10-08 04:56:44 +00:00
|
|
|
use Pterodactyl\Models\ServerVariable;
|
2017-07-25 02:34:10 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
class StartupModificationService
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
use HasUserLevels;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $validatorService;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* StartupModificationService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
|
|
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2020-10-08 04:56:44 +00:00
|
|
|
public function __construct(ConnectionInterface $connection, VariableValidatorService $validatorService)
|
|
|
|
{
|
2017-09-27 03:54:34 +00:00
|
|
|
$this->connection = $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->validatorService = $validatorService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process startup modification for a server.
|
|
|
|
*
|
2017-10-27 04:49:54 +00:00
|
|
|
* @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
|
2017-07-25 02:34:10 +00:00
|
|
|
*
|
2020-10-08 04:56:44 +00:00
|
|
|
* @throws \Throwable
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2018-01-31 02:36:59 +00:00
|
|
|
public function handle(Server $server, array $data): Server
|
2017-07-25 02:34:10 +00:00
|
|
|
{
|
2020-10-08 04:56:44 +00:00
|
|
|
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();
|
|
|
|
});
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update certain administrative settings for a server in the DB.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
*/
|
2020-10-08 04:56:44 +00:00
|
|
|
protected function updateAdministrativeSettings(array $data, Server &$server)
|
2017-10-27 04:49:54 +00:00
|
|
|
{
|
2020-10-09 03:38:21 +00:00
|
|
|
$eggId = Arr::get($data, 'egg_id');
|
|
|
|
|
|
|
|
if (is_digit($eggId) && $server->egg_id !== (int)$eggId) {
|
2020-10-08 04:56:44 +00:00
|
|
|
/** @var \Pterodactyl\Models\Egg $egg */
|
2020-10-09 03:38:21 +00:00
|
|
|
$egg = Egg::query()->findOrFail($data['egg_id']);
|
2020-10-08 04:56:44 +00:00
|
|
|
|
2020-10-09 03:38:21 +00:00
|
|
|
$server = $server->forceFill([
|
|
|
|
'egg_id' => $egg->id,
|
|
|
|
'nest_id' => $egg->nest_id,
|
|
|
|
]);
|
2018-01-31 02:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 03:12:34 +00:00
|
|
|
$server->fill([
|
2020-10-08 04:56:44 +00:00
|
|
|
'startup' => $data['startup'] ?? $server->startup,
|
|
|
|
'skip_scripts' => $data['skip_scripts'] ?? isset($data['skip_scripts']),
|
|
|
|
'image' => $data['docker_image'] ?? $server->image,
|
|
|
|
])->save();
|
2017-10-27 04:49:54 +00:00
|
|
|
}
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|