2020-08-22 18:13:59 -07:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2020-08-22 18:13:59 -07:00
namespace Pterodactyl\Http\Controllers\Api\Client\Servers ;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Attributes\Endpoint ;
use Knuckles\Scribe\Attributes\Group ;
use Knuckles\Scribe\Attributes\Response as ScribeResponse ;
use Knuckles\Scribe\Attributes\Subgroup ;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer ;
2022-05-29 19:26:28 -04:00
use Pterodactyl\Facades\Activity ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Fractal ;
2020-08-22 18:13:59 -07:00
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController ;
2020-08-25 19:22:17 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\GetStartupRequest ;
2020-08-22 18:13:59 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\EggVariable ;
use Pterodactyl\Models\Server ;
use Pterodactyl\Services\Servers\StartupCommandService ;
use Pterodactyl\Support\JsonValueGuard ;
use Pterodactyl\Transformers\Api\Client\EggVariableTransformer ;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException ;
use UnexpectedValueException ;
2020-08-22 18:13:59 -07:00
2026-10-05 18:10:18 -04:00
#[ Group ( 'Client API' , 'Endpoints authenticated as a panel user using a client API token.' )]
#[ Subgroup ( 'Server Startup' , 'View startup command data and update editable startup variables for an accessible server.' )]
2020-08-22 18:13:59 -07:00
class StartupController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array BAD_REQUEST_ERROR = [
'errors' => [
[
'code' => 'BadRequestHttpException' ,
'status' => '400' ,
'detail' => 'The environment variable you are trying to edit is read-only.' ,
],
],
];
2020-08-25 19:22:17 -07:00
/**
2022-10-14 10:59:20 -06:00
* Returns the startup information for the server including all the variables.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2020-08-25 19:22:17 -07:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Get startup configuration' , 'Returns the rendered startup command, available Docker images, raw startup command, and user-visible variables for the server.' )]
#[ ResponseFromTransformer ( EggVariableTransformer :: class , EggVariable :: class , description : 'Startup configuration returned.' , collection : true , factoryStates : [ 'viewable' , 'editable' ], resourceKey : 'egg_variable' , meta : [ 'startup_command' => 'java -Xms128M -Xmx1024M -jar paper.jar' , 'docker_images' => [ 'Java 23' => 'ghcr.io/pterodactyl/yolks:java_23' ], 'raw_startup_command' => 'java -Xms128M -Xmx{{SERVER_MEMORY}}M -jar {{SERVER_JARFILE}}' ])]
public function index ( GetStartupRequest $request , StartupCommandService $startupCommand , Server $server ) : array
2020-08-25 19:22:17 -07:00
{
2026-10-05 18:10:18 -04:00
$startup = $startupCommand -> handle ( $server );
$egg = $server -> egg ?? throw new UnexpectedValueException ( 'The server does not have an egg relationship.' );
2020-08-25 19:22:17 -07:00
2026-10-05 18:10:18 -04:00
return Fractal :: collection (
2020-09-22 21:12:00 -07:00
$server -> variables () -> where ( 'user_viewable' , true ) -> get ()
)
2020-08-25 19:22:17 -07:00
-> transformWith ( $this -> getTransformer ( EggVariableTransformer :: class ))
-> addMeta ([
'startup_command' => $startup ,
2026-10-05 18:10:18 -04:00
'docker_images' => $egg -> docker_images ,
2020-08-25 19:22:17 -07:00
'raw_startup_command' => $server -> startup ,
])
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2020-08-22 18:13:59 -07:00
}
/**
* Updates a single variable for a server.
*
2026-10-05 18:10:18 -04:00
* @return ApiPayload
2020-08-22 18:13:59 -07:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Update startup variable' , 'Updates one editable startup variable for the server and returns the updated rendered startup command.' )]
#[ ResponseFromTransformer ( EggVariableTransformer :: class , EggVariable :: class , description : 'Startup variable updated.' , factoryStates : [ 'viewable' , 'editable' ], resourceKey : 'egg_variable' , meta : [ 'startup_command' => 'java -Xms128M -Xmx1024M -jar paper.jar' , 'raw_startup_command' => 'java -Xms128M -Xmx{{SERVER_MEMORY}}M -jar {{SERVER_JARFILE}}' ])]
#[ ScribeResponse ( self :: BAD_REQUEST_ERROR , status : 400 , description : 'The variable does not exist, is hidden, or is read-only.' )]
public function update ( UpdateStartupVariableRequest $request , StartupCommandService $startupCommand , Server $server ) : array
2020-08-22 18:13:59 -07:00
{
2026-10-05 18:10:18 -04:00
$variable = $request -> variable ();
throw_if ( ! $variable instanceof EggVariable || ! $variable -> user_viewable , BadRequestHttpException :: class , 'The environment variable you are trying to edit does not exist.' );
2020-08-22 18:13:59 -07:00
2026-10-05 18:10:18 -04:00
throw_unless ( $variable -> user_editable , BadRequestHttpException :: class , 'The environment variable you are trying to edit is read-only.' );
2020-08-22 18:13:59 -07:00
2025-12-20 15:55:13 -08:00
$original = $variable -> server_value ;
2026-10-05 18:10:18 -04:00
$value = JsonValueGuard :: nullableScalarString ( $request -> validated ( 'value' ));
2025-12-20 15:55:13 -08:00
2026-10-05 18:10:18 -04:00
$server -> serverVariables () -> updateOrCreate (
[ 'variable_id' => $variable -> id ],
[ 'variable_value' => $value ?? '' ],
);
2020-08-22 18:13:59 -07:00
2026-10-05 18:10:18 -04:00
$variable -> refresh ();
$variable -> server_value = $value ;
2020-08-22 18:13:59 -07:00
2026-10-05 18:10:18 -04:00
$startup = $startupCommand -> handle ( $server );
2020-08-25 19:22:17 -07:00
2026-10-05 18:10:18 -04:00
if ( $original !== $value ) {
2022-05-29 19:26:28 -04:00
Activity :: event ( 'server:startup.edit' )
-> subject ( $variable )
-> property ([
'variable' => $variable -> env_variable ,
'old' => $original ,
2026-10-05 18:10:18 -04:00
'new' => $value ,
2022-05-29 19:26:28 -04:00
])
-> log ();
}
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $variable )
2020-08-22 18:13:59 -07:00
-> transformWith ( $this -> getTransformer ( EggVariableTransformer :: class ))
2020-08-25 19:22:17 -07:00
-> addMeta ([
'startup_command' => $startup ,
'raw_startup_command' => $server -> startup ,
])
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2020-08-22 18:13:59 -07:00
}
}