Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Servers/StartupModificationService.php
10276 views
1
<?php
2
3
namespace Pterodactyl\Services\Servers;
4
5
use Illuminate\Support\Arr;
6
use Pterodactyl\Models\Egg;
7
use Pterodactyl\Models\User;
8
use Pterodactyl\Models\Server;
9
use Pterodactyl\Models\ServerVariable;
10
use Illuminate\Database\ConnectionInterface;
11
use Pterodactyl\Traits\Services\HasUserLevels;
12
13
class StartupModificationService
14
{
15
use HasUserLevels;
16
17
/**
18
* StartupModificationService constructor.
19
*/
20
public function __construct(private ConnectionInterface $connection, private VariableValidatorService $validatorService)
21
{
22
}
23
24
/**
25
* Process startup modification for a server.
26
*
27
* @throws \Throwable
28
*/
29
public function handle(Server $server, array $data): Server
30
{
31
return $this->connection->transaction(function () use ($server, $data) {
32
if (!empty($data['environment'])) {
33
$egg = $this->isUserLevel(User::USER_LEVEL_ADMIN) ? ($data['egg_id'] ?? $server->egg_id) : $server->egg_id;
34
35
$results = $this->validatorService
36
->setUserLevel($this->getUserLevel())
37
->handle($egg, $data['environment']);
38
39
foreach ($results as $result) {
40
ServerVariable::query()->updateOrCreate(
41
[
42
'server_id' => $server->id,
43
'variable_id' => $result->id,
44
],
45
['variable_value' => $result->value ?? '']
46
);
47
}
48
}
49
50
if ($this->isUserLevel(User::USER_LEVEL_ADMIN)) {
51
$this->updateAdministrativeSettings($data, $server);
52
}
53
54
// Calling ->refresh() rather than ->fresh() here causes it to return the
55
// variables as triplicates for some reason? Not entirely sure, should dig
56
// in more to figure it out, but luckily we have a test case covering this
57
// specific call so we can be assured we're not breaking it _here_ at least.
58
//
59
// TODO(dane): this seems like a red-flag for the code powering the relationship
60
// that should be looked into more.
61
return $server->fresh();
62
});
63
}
64
65
/**
66
* Update certain administrative settings for a server in the DB.
67
*/
68
protected function updateAdministrativeSettings(array $data, Server &$server): void
69
{
70
$eggId = Arr::get($data, 'egg_id');
71
72
if (is_digit($eggId) && $server->egg_id !== (int) $eggId) {
73
/** @var Egg $egg */
74
$egg = Egg::query()->findOrFail($data['egg_id']);
75
76
$server = $server->forceFill([
77
'egg_id' => $egg->id,
78
'nest_id' => $egg->nest_id,
79
]);
80
}
81
82
$server->fill([
83
'startup' => $data['startup'] ?? $server->startup,
84
'skip_scripts' => $data['skip_scripts'] ?? isset($data['skip_scripts']),
85
'image' => $data['docker_image'] ?? $server->image,
86
])->save();
87
}
88
}
89
90