misc_pterodactyl-panel/app/Console/Commands/Environment/AppSettingsCommand.php

178 lines
7 KiB
PHP
Raw Permalink Normal View History

2017-09-23 02:19:57 +00:00
<?php
namespace Pterodactyl\Console\Commands\Environment;
use DateTimeZone;
2017-09-23 02:19:57 +00:00
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel;
use Pterodactyl\Traits\Commands\EnvironmentWriterTrait;
class AppSettingsCommand extends Command
{
use EnvironmentWriterTrait;
2022-05-12 21:53:29 +00:00
public const CACHE_DRIVERS = [
'redis' => 'Redis (recommended)',
'memcached' => 'Memcached',
'file' => 'Filesystem',
];
2022-05-12 21:53:29 +00:00
public const SESSION_DRIVERS = [
'redis' => 'Redis (recommended)',
'memcached' => 'Memcached',
'database' => 'MySQL Database',
'file' => 'Filesystem',
'cookie' => 'Cookie',
];
2022-05-12 21:53:29 +00:00
public const QUEUE_DRIVERS = [
'redis' => 'Redis (recommended)',
'database' => 'MySQL Database',
'sync' => 'Sync',
];
2017-09-23 02:19:57 +00:00
protected $description = 'Configure basic environment settings for the Panel.';
protected $signature = 'p:environment:setup
2018-05-13 14:50:56 +00:00
{--new-salt : Whether or not to generate a new salt for Hashids.}
2017-10-03 03:51:13 +00:00
{--author= : The email that services created on this instance should be linked to.}
2017-09-23 02:19:57 +00:00
{--url= : The URL that this Panel is running on.}
{--timezone= : The timezone to use for Panel times.}
{--cache= : The cache driver backend to use.}
{--session= : The session driver backend to use.}
{--queue= : The queue driver backend to use.}
2017-09-23 02:19:57 +00:00
{--redis-host= : Redis host to use for connections.}
{--redis-pass= : Password used to connect to redis.}
2017-12-30 22:33:00 +00:00
{--redis-port= : Port to connect to redis over.}
{--settings-ui= : Enable or disable the settings UI.}';
2017-09-23 02:19:57 +00:00
protected array $variables = [];
2017-09-23 02:19:57 +00:00
/**
* AppSettingsCommand constructor.
*/
public function __construct(private Kernel $console)
2017-09-23 02:19:57 +00:00
{
parent::__construct();
}
/**
* Handle command execution.
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function handle(): int
2017-09-23 02:19:57 +00:00
{
2022-05-12 21:53:29 +00:00
if (empty(config('hashids.salt')) || $this->option('new-salt')) {
$this->variables['HASHIDS_SALT'] = str_random(20);
}
2022-05-12 21:53:29 +00:00
$this->output->comment('Provide the email address that eggs exported by this Panel should be from. This should be a valid email address.');
2017-11-04 21:27:15 +00:00
$this->variables['APP_SERVICE_AUTHOR'] = $this->option('author') ?? $this->ask(
2022-05-12 21:53:29 +00:00
'Egg Author Email',
config('pterodactyl.service.author', 'unknown@unknown.com')
);
2022-05-12 21:53:29 +00:00
if (!filter_var($this->variables['APP_SERVICE_AUTHOR'], FILTER_VALIDATE_EMAIL)) {
$this->output->error('The service author email provided is invalid.');
return 1;
}
2022-05-12 21:53:29 +00:00
$this->output->comment('The application URL MUST begin with https:// or http:// depending on if you are using SSL or not. If you do not include the scheme your emails and other content will link to the wrong location.');
2017-09-23 02:19:57 +00:00
$this->variables['APP_URL'] = $this->option('url') ?? $this->ask(
2022-05-12 21:53:29 +00:00
'Application URL',
config('app.url', 'https://example.com')
2021-01-23 20:33:34 +00:00
);
2017-09-23 02:19:57 +00:00
$this->output->comment('The timezone should match one of PHP\'s supported timezones. If you are unsure, please reference https://php.net/manual/en/timezones.php.');
$this->variables['APP_TIMEZONE'] = $this->option('timezone') ?? $this->anticipate(
2022-05-12 21:53:29 +00:00
'Application Timezone',
DateTimeZone::listIdentifiers(),
2022-05-12 21:53:29 +00:00
config('app.timezone')
2021-01-23 20:33:34 +00:00
);
2017-09-23 02:19:57 +00:00
2022-05-12 21:53:29 +00:00
$selected = config('cache.default', 'redis');
2017-09-23 02:19:57 +00:00
$this->variables['CACHE_DRIVER'] = $this->option('cache') ?? $this->choice(
2022-05-12 21:53:29 +00:00
'Cache Driver',
self::CACHE_DRIVERS,
array_key_exists($selected, self::CACHE_DRIVERS) ? $selected : null
2021-01-23 20:33:34 +00:00
);
2017-09-23 02:19:57 +00:00
2022-05-12 21:53:29 +00:00
$selected = config('session.driver', 'redis');
2017-09-23 02:19:57 +00:00
$this->variables['SESSION_DRIVER'] = $this->option('session') ?? $this->choice(
2022-05-12 21:53:29 +00:00
'Session Driver',
self::SESSION_DRIVERS,
array_key_exists($selected, self::SESSION_DRIVERS) ? $selected : null
2021-01-23 20:33:34 +00:00
);
2017-09-23 02:19:57 +00:00
2022-05-12 21:53:29 +00:00
$selected = config('queue.default', 'redis');
2018-11-26 00:25:18 +00:00
$this->variables['QUEUE_CONNECTION'] = $this->option('queue') ?? $this->choice(
2022-05-12 21:53:29 +00:00
'Queue Driver',
self::QUEUE_DRIVERS,
array_key_exists($selected, self::QUEUE_DRIVERS) ? $selected : null
2021-01-23 20:33:34 +00:00
);
2017-12-30 22:33:00 +00:00
2021-01-23 20:33:34 +00:00
if (!is_null($this->option('settings-ui'))) {
$this->variables['APP_ENVIRONMENT_ONLY'] = $this->option('settings-ui') == 'true' ? 'false' : 'true';
2017-12-30 22:33:00 +00:00
} else {
2022-05-12 21:53:29 +00:00
$this->variables['APP_ENVIRONMENT_ONLY'] = $this->confirm('Enable UI based settings editor?', true) ? 'false' : 'true';
2017-12-30 22:33:00 +00:00
}
2017-09-23 02:19:57 +00:00
// Make sure session cookies are set as "secure" when using HTTPS
if (str_starts_with($this->variables['APP_URL'], 'https://')) {
$this->variables['SESSION_SECURE_COOKIE'] = 'true';
}
2017-09-23 02:19:57 +00:00
$this->checkForRedis();
$this->writeToEnvironment($this->variables);
$this->info($this->console->output());
return 0;
2017-09-23 02:19:57 +00:00
}
/**
* Check if redis is selected, if so, request connection details and verify them.
*/
private function checkForRedis()
{
$items = collect($this->variables)->filter(function ($item) {
return $item === 'redis';
});
// Redis was not selected, no need to continue.
if (count($items) === 0) {
return;
}
2022-05-12 21:53:29 +00:00
$this->output->note('You\'ve selected the Redis driver for one or more options, please provide valid connection information below. In most cases you can use the defaults provided unless you have modified your setup.');
2017-09-23 02:19:57 +00:00
$this->variables['REDIS_HOST'] = $this->option('redis-host') ?? $this->ask(
2022-05-12 21:53:29 +00:00
'Redis Host',
config('database.redis.default.host')
2021-01-23 20:33:34 +00:00
);
2017-09-23 02:19:57 +00:00
$askForRedisPassword = true;
2022-05-12 21:53:29 +00:00
if (!empty(config('database.redis.default.password'))) {
$this->variables['REDIS_PASSWORD'] = config('database.redis.default.password');
$askForRedisPassword = $this->confirm('It seems a password is already defined for Redis, would you like to change it?');
2017-09-23 02:19:57 +00:00
}
if ($askForRedisPassword) {
2022-05-12 21:53:29 +00:00
$this->output->comment('By default a Redis server instance has no password as it is running locally and inaccessible to the outside world. If this is the case, simply hit enter without entering a value.');
2017-09-23 02:19:57 +00:00
$this->variables['REDIS_PASSWORD'] = $this->option('redis-pass') ?? $this->output->askHidden(
2022-05-12 21:53:29 +00:00
'Redis Password'
2021-01-23 20:33:34 +00:00
);
2017-09-23 02:19:57 +00:00
}
2017-11-04 21:40:48 +00:00
if (empty($this->variables['REDIS_PASSWORD'])) {
2017-11-04 21:46:18 +00:00
$this->variables['REDIS_PASSWORD'] = 'null';
2017-11-04 21:40:48 +00:00
}
2017-09-23 02:19:57 +00:00
$this->variables['REDIS_PORT'] = $this->option('redis-port') ?? $this->ask(
2022-05-12 21:53:29 +00:00
'Redis Port',
config('database.redis.default.port')
2021-01-23 20:33:34 +00:00
);
2017-09-23 02:19:57 +00:00
}
}