Path: blob/1.0-develop/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php
14056 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;34use Carbon\CarbonImmutable;5use Illuminate\Http\Request;6use Illuminate\Http\Response;7use Pterodactyl\Models\Server;8use Illuminate\Http\JsonResponse;9use Pterodactyl\Http\Controllers\Controller;10use Pterodactyl\Exceptions\Http\HttpForbiddenException;11use Pterodactyl\Repositories\Eloquent\ServerRepository;12use Pterodactyl\Events\Server\Installed as ServerInstalled;13use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;14use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest;1516class ServerInstallController extends Controller17{18/**19* ServerInstallController constructor.20*/21public function __construct(private ServerRepository $repository, private EventDispatcher $eventDispatcher)22{23}2425/**26* Returns installation information for a server.27*28* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException29*/30public function index(Request $request, string $uuid): JsonResponse31{32$server = $this->repository->getByUuid($uuid);33$egg = $server->egg;3435if (! $server->node->is($request->attributes->get('node'))) {36throw new HttpForbiddenException('Requesting node does not have permission to access this server.');37}3839return new JsonResponse([40'container_image' => $egg->copy_script_container,41'entrypoint' => $egg->copy_script_entry,42'script' => $egg->copy_script_install,43]);44}4546/**47* Updates the installation state of a server.48*49* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException50* @throws \Pterodactyl\Exceptions\Model\DataValidationException51*/52public function store(InstallationDataRequest $request, string $uuid): JsonResponse53{54$server = $this->repository->getByUuid($uuid);55$status = null;5657if (! $server->node->is($request->attributes->get('node'))) {58throw new HttpForbiddenException('Requesting node does not have permission to access this server.');59}6061// Make sure the type of failure is accurate62if (!$request->boolean('successful')) {63$status = Server::STATUS_INSTALL_FAILED;6465if ($request->boolean('reinstall')) {66$status = Server::STATUS_REINSTALL_FAILED;67}68}6970// Keep the server suspended if it's already suspended71if ($server->status === Server::STATUS_SUSPENDED) {72$status = Server::STATUS_SUSPENDED;73}7475$this->repository->update($server->id, ['status' => $status, 'installed_at' => CarbonImmutable::now()], true, true);7677// If the server successfully installed, fire installed event.78// This logic allows individually disabling install and reinstall notifications separately.79$isInitialInstall = is_null($server->installed_at);80if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) {81$this->eventDispatcher->dispatch(new ServerInstalled($server));82} elseif (!$isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) {83$this->eventDispatcher->dispatch(new ServerInstalled($server));84}8586return new JsonResponse([], Response::HTTP_NO_CONTENT);87}88}899091