Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/StartupController.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4
5
use Pterodactyl\Models\Server;
6
use Pterodactyl\Facades\Activity;
7
use Pterodactyl\Services\Servers\StartupCommandService;
8
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
9
use Pterodactyl\Transformers\Api\Client\EggVariableTransformer;
10
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\GetStartupRequest;
13
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest;
14
15
class StartupController extends ClientApiController
16
{
17
/**
18
* StartupController constructor.
19
*/
20
public function __construct(
21
private StartupCommandService $startupCommandService,
22
private ServerVariableRepository $repository,
23
) {
24
parent::__construct();
25
}
26
27
/**
28
* Returns the startup information for the server including all the variables.
29
*/
30
public function index(GetStartupRequest $request, Server $server): array
31
{
32
$startup = $this->startupCommandService->handle($server);
33
34
return $this->fractal->collection(
35
$server->variables()->where('user_viewable', true)->get()
36
)
37
->transformWith($this->getTransformer(EggVariableTransformer::class))
38
->addMeta([
39
'startup_command' => $startup,
40
'docker_images' => $server->egg->docker_images,
41
'raw_startup_command' => $server->startup,
42
])
43
->toArray();
44
}
45
46
/**
47
* Updates a single variable for a server.
48
*
49
* @throws \Illuminate\Validation\ValidationException
50
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
51
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
52
*/
53
public function update(UpdateStartupVariableRequest $request, Server $server): array
54
{
55
$variable = $server->variables()->where('env_variable', $request->input('key'))->first();
56
57
if (is_null($variable) || !$variable->user_viewable) {
58
throw new BadRequestHttpException('The environment variable you are trying to edit does not exist.');
59
} elseif (!$variable->user_editable) {
60
throw new BadRequestHttpException('The environment variable you are trying to edit is read-only.');
61
}
62
63
$original = $variable->server_value;
64
65
// Revalidate the variable value using the egg variable specific validation rules for it.
66
$this->validate($request, ['value' => $variable->rules]);
67
68
$this->repository->updateOrCreate([
69
'server_id' => $server->id,
70
'variable_id' => $variable->id,
71
], [
72
'variable_value' => $request->input('value') ?? '',
73
]);
74
75
$variable = $variable->refresh();
76
$variable->server_value = $request->input('value');
77
78
$startup = $this->startupCommandService->handle($server);
79
80
if ($variable->env_variable !== $request->input('value')) {
81
Activity::event('server:startup.edit')
82
->subject($variable)
83
->property([
84
'variable' => $variable->env_variable,
85
'old' => $original,
86
'new' => $request->input('value'),
87
])
88
->log();
89
}
90
91
return $this->fractal->item($variable)
92
->transformWith($this->getTransformer(EggVariableTransformer::class))
93
->addMeta([
94
'startup_command' => $startup,
95
'raw_startup_command' => $server->startup,
96
])
97
->toArray();
98
}
99
}
100
101