2019-12-09 22:03:10 -08:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2019-12-09 22:03:10 -08:00
namespace Pterodactyl\Http\Controllers\Api\Client\Servers ;
2020-04-03 14:43:24 -07:00
use Illuminate\Http\JsonResponse ;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Response ;
use Knuckles\Scribe\Attributes\BodyParam ;
use Knuckles\Scribe\Attributes\Endpoint ;
use Knuckles\Scribe\Attributes\Group ;
use Knuckles\Scribe\Attributes\Response as ScribeResponse ;
use Knuckles\Scribe\Attributes\Subgroup ;
use Pterodactyl\Contracts\Servers\ReinstallsServers ;
2022-05-29 19:26:28 -04:00
use Pterodactyl\Facades\Activity ;
2019-12-09 22:03:10 -08:00
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\ReinstallServerRequest ;
2019-12-09 22:03:10 -08:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\RenameServerRequest ;
2020-12-13 11:07:29 -08:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\SetDockerImageRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server ;
use Pterodactyl\Support\JsonValueGuard ;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException ;
use Throwable ;
use UnexpectedValueException ;
2019-12-09 22:03:10 -08: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 Settings' , 'Rename, reinstall, and update Docker image settings for an accessible server.' )]
2019-12-09 22:03:10 -08:00
class SettingsController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array BAD_REQUEST_ERROR = [
'errors' => [
[
'code' => 'BadRequestHttpException' ,
'status' => '400' ,
'detail' => "This server's Docker image has been manually set by an administrator and cannot be updated." ,
],
],
];
2019-12-09 22:03:10 -08:00
/**
* Renames a server.
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Rename server' , 'Updates the server name and optionally its description.' )]
#[ ScribeResponse ( status : 204 , description : 'Server renamed.' )]
2022-10-14 10:59:20 -06:00
public function rename ( RenameServerRequest $request , Server $server ) : JsonResponse
2019-12-09 22:03:10 -08:00
{
2026-10-05 18:10:18 -04:00
$previousName = $server -> name ;
$previousDescription = $server -> description ;
$server -> update ([
'name' => $request -> string ( 'name' ) -> toString (),
'description' => $request -> has ( 'description' ) ? $request -> description () : $server -> description ,
2019-12-09 22:03:10 -08:00
]);
2026-10-05 18:10:18 -04:00
if ( $server -> wasChanged ( 'name' )) {
2022-05-29 19:26:28 -04:00
Activity :: event ( 'server:settings.rename' )
2026-10-05 18:10:18 -04:00
-> property ([ 'old' => $previousName , 'new' => $server -> name ])
2022-11-21 13:41:26 -07:00
-> log ();
}
2026-10-05 18:10:18 -04:00
if ( $server -> wasChanged ( 'description' )) {
2022-11-21 13:41:26 -07:00
Activity :: event ( 'server:settings.description' )
2026-10-05 18:10:18 -04:00
-> property ([ 'old' => $previousDescription , 'new' => $server -> description ])
2022-05-29 19:26:28 -04:00
-> log ();
}
2020-06-28 10:40:41 -07:00
return new JsonResponse ([], Response :: HTTP_NO_CONTENT );
2020-04-03 14:43:24 -07:00
}
/**
* Reinstalls the server on the daemon.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
2020-04-03 14:43:24 -07:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Reinstall server' , 'Queues a reinstall of the server on Wings.' )]
#[ ScribeResponse ( status : 202 , description : 'Server reinstall queued.' )]
public function reinstall ( ReinstallServerRequest $request , ReinstallsServers $reinstall , Server $server ) : JsonResponse
2020-04-03 14:43:24 -07:00
{
2026-10-05 18:10:18 -04:00
$reinstall -> reinstall ( $server );
2020-04-03 14:43:24 -07:00
2022-05-29 19:26:28 -04:00
Activity :: event ( 'server:reinstall' ) -> log ();
2020-06-28 10:40:41 -07:00
return new JsonResponse ([], Response :: HTTP_ACCEPTED );
2019-12-09 22:03:10 -08:00
}
2020-12-13 11:07:29 -08:00
/**
* Changes the Docker image in use by the server.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
2020-12-13 11:07:29 -08:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Set server Docker image' , 'Updates the server Docker image to one of the images allowed by the server egg.' )]
#[ BodyParam ( 'docker_image' , 'string' , 'The Docker image to use.' , required : true , example : 'ghcr.io/pterodactyl/yolks:java_21' )]
#[ ScribeResponse ( status : 204 , description : 'Docker image updated.' )]
#[ ScribeResponse ( self :: BAD_REQUEST_ERROR , status : 400 , description : 'The server image was manually set by an administrator and cannot be changed through this endpoint.' )]
2022-10-14 10:59:20 -06:00
public function dockerImage ( SetDockerImageRequest $request , Server $server ) : JsonResponse
2020-12-13 11:07:29 -08:00
{
2026-10-05 18:10:18 -04:00
$egg = $server -> egg ?? throw new UnexpectedValueException ( 'The server does not have an egg relationship.' );
throw_unless ( in_array ( $server -> image , $egg -> docker_images , true ), BadRequestHttpException :: class , "This server's Docker image has been manually set by an administrator and cannot be updated." );
2020-12-13 11:07:29 -08:00
2022-05-29 19:26:28 -04:00
$original = $server -> image ;
2026-10-05 18:10:18 -04:00
$dockerImage = JsonValueGuard :: string ( $request -> validated ( 'docker_image' ));
$server -> forceFill ([ 'image' => $dockerImage ]) -> saveOrFail ();
2020-12-13 11:07:29 -08:00
2022-05-29 19:26:28 -04:00
if ( $original !== $server -> image ) {
Activity :: event ( 'server:startup.image' )
2026-10-05 18:10:18 -04:00
-> property ([ 'old' => $original , 'new' => $dockerImage ])
2022-05-29 19:26:28 -04:00
-> log ();
}
2020-12-13 11:07:29 -08:00
return new JsonResponse ([], Response :: HTTP_NO_CONTENT );
}
2019-12-09 22:03:10 -08:00
}