2017-07-25 02:34:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-07-25 02:34:10 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
2018-01-31 02:36:59 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-07-25 02:34:10 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
|
|
|
|
|
|
|
class StartupModificationService
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
use HasUserLevels;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $connection;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2018-01-31 02:36:59 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $eggRepository;
|
|
|
|
|
2017-07-25 02:34:10 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $environmentService;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $repository;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $serverVariableRepository;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $validatorService;
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2019-12-22 05:01:38 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService
|
|
|
|
*/
|
|
|
|
private $structureService;
|
|
|
|
|
2017-07-25 02:34:10 +00:00
|
|
|
/**
|
|
|
|
* StartupModificationService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $eggRepository
|
|
|
|
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
2019-12-22 05:01:38 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $structureService
|
2017-07-25 02:34:10 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-09-27 03:54:34 +00:00
|
|
|
ConnectionInterface $connection,
|
2018-01-31 02:36:59 +00:00
|
|
|
EggRepositoryInterface $eggRepository,
|
2017-07-25 02:34:10 +00:00
|
|
|
EnvironmentService $environmentService,
|
|
|
|
ServerRepositoryInterface $repository,
|
2019-12-22 05:01:38 +00:00
|
|
|
ServerConfigurationStructureService $structureService,
|
2017-07-25 02:34:10 +00:00
|
|
|
ServerVariableRepositoryInterface $serverVariableRepository,
|
2017-09-27 03:54:34 +00:00
|
|
|
VariableValidatorService $validatorService
|
2017-07-25 02:34:10 +00:00
|
|
|
) {
|
2017-09-27 03:54:34 +00:00
|
|
|
$this->connection = $connection;
|
2018-01-31 02:36:59 +00:00
|
|
|
$this->eggRepository = $eggRepository;
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->environmentService = $environmentService;
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->serverVariableRepository = $serverVariableRepository;
|
|
|
|
$this->validatorService = $validatorService;
|
2019-12-22 05:01:38 +00:00
|
|
|
$this->structureService = $structureService;
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process startup modification for a server.
|
|
|
|
*
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
2018-01-31 02:36:59 +00:00
|
|
|
* @return \Pterodactyl\Models\Server
|
2017-07-25 02:34:10 +00:00
|
|
|
*
|
2017-11-25 19:15:46 +00:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2017-07-25 02:34:10 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-08-13 19:55:09 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-07-25 02:34:10 +00:00
|
|
|
*/
|
2018-01-31 02:36:59 +00:00
|
|
|
public function handle(Server $server, array $data): Server
|
2017-07-25 02:34:10 +00:00
|
|
|
{
|
2017-09-27 03:54:34 +00:00
|
|
|
$this->connection->beginTransaction();
|
2017-10-26 03:33:28 +00:00
|
|
|
if (! is_null(array_get($data, 'environment'))) {
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->validatorService->setUserLevel($this->getUserLevel());
|
|
|
|
$results = $this->validatorService->handle(array_get($data, 'egg_id', $server->egg_id), array_get($data, 'environment', []));
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$results->each(function ($result) use ($server) {
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->serverVariableRepository->withoutFreshModel()->updateOrCreate([
|
2017-07-25 02:34:10 +00:00
|
|
|
'server_id' => $server->id,
|
2017-10-27 04:49:54 +00:00
|
|
|
'variable_id' => $result->id,
|
2017-07-25 02:34:10 +00:00
|
|
|
], [
|
2018-03-10 19:55:24 +00:00
|
|
|
'variable_value' => $result->value ?? '',
|
2017-07-25 02:34:10 +00:00
|
|
|
]);
|
2017-10-27 04:49:54 +00:00
|
|
|
});
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
if ($this->isUserLevel(User::USER_LEVEL_ADMIN)) {
|
2019-12-22 05:01:38 +00:00
|
|
|
$this->updateAdministrativeSettings($data, $server);
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 03:33:28 +00:00
|
|
|
$this->connection->commit();
|
2018-01-31 02:36:59 +00:00
|
|
|
|
|
|
|
return $server;
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update certain administrative settings for a server in the DB.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
2017-10-27 04:49:54 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2019-12-22 05:01:38 +00:00
|
|
|
private function updateAdministrativeSettings(array $data, Server &$server)
|
2017-10-27 04:49:54 +00:00
|
|
|
{
|
2018-01-31 02:36:59 +00:00
|
|
|
if (
|
|
|
|
is_digit(array_get($data, 'egg_id'))
|
|
|
|
&& $data['egg_id'] != $server->egg_id
|
|
|
|
&& is_null(array_get($data, 'nest_id'))
|
|
|
|
) {
|
|
|
|
$egg = $this->eggRepository->setColumns(['id', 'nest_id'])->find($data['egg_id']);
|
|
|
|
$data['nest_id'] = $egg->nest_id;
|
|
|
|
}
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$server = $this->repository->update($server->id, [
|
|
|
|
'installed' => 0,
|
|
|
|
'startup' => array_get($data, 'startup', $server->startup),
|
|
|
|
'nest_id' => array_get($data, 'nest_id', $server->nest_id),
|
|
|
|
'egg_id' => array_get($data, 'egg_id', $server->egg_id),
|
2018-01-31 02:36:59 +00:00
|
|
|
'skip_scripts' => array_get($data, 'skip_scripts') ?? isset($data['skip_scripts']),
|
2017-11-11 21:07:01 +00:00
|
|
|
'image' => array_get($data, 'docker_image', $server->image),
|
2017-10-27 04:49:54 +00:00
|
|
|
]);
|
|
|
|
}
|
2017-07-25 02:34:10 +00:00
|
|
|
}
|