2017-09-22 21:19:57 -05:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2017-09-22 21:19:57 -05:00
namespace Pterodactyl\Console\Commands\Environment ;
2026-10-05 18:10:18 -04:00
use DateTimeZone ;
use Illuminate\Console\Attributes\Description ;
use Illuminate\Console\Attributes\Signature ;
2017-09-22 21:19:57 -05:00
use Illuminate\Console\Command ;
use Illuminate\Contracts\Console\Kernel ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Str ;
use InvalidArgumentException ;
use Pterodactyl\Exceptions\PterodactylException ;
use Pterodactyl\Support\JsonValueGuard ;
2017-09-22 21:19:57 -05:00
use Pterodactyl\Traits\Commands\EnvironmentWriterTrait ;
2026-10-05 18:10:18 -04:00
#[ Description ( 'Configure basic environment settings for the Panel.' )]
#[ Signature ( 'p:environment:setup
{--new-salt : Whether or not to generate a new salt for Hashids.}
{--author= : The email that services created on this instance should be linked to.}
{--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.}
{--redis-host= : Redis host to use for connections.}
{--redis-pass= : Password used to connect to redis.}
{--redis-port= : Port to connect to redis over.}
{--settings-ui= : Enable or disable the settings UI.}
{--telemetry= : Enable or disable anonymous telemetry.}' )]
2017-09-22 21:19:57 -05:00
class AppSettingsCommand extends Command
{
use EnvironmentWriterTrait ;
2026-10-05 18:10:18 -04:00
public const array CACHE_DRIVERS = [
2017-11-23 15:08:35 -06:00
'redis' => 'Redis (recommended)' ,
2017-11-23 21:08:43 +00:00
'memcached' => 'Memcached' ,
2018-03-01 18:46:59 -06:00
'file' => 'Filesystem' ,
2017-11-23 15:08:35 -06:00
];
2026-10-05 18:10:18 -04:00
public const array SESSION_DRIVERS = [
2017-11-23 15:08:35 -06:00
'redis' => 'Redis (recommended)' ,
'memcached' => 'Memcached' ,
'database' => 'MySQL Database' ,
'file' => 'Filesystem' ,
'cookie' => 'Cookie' ,
];
2026-10-05 18:10:18 -04:00
public const array QUEUE_DRIVERS = [
2017-11-23 15:08:35 -06:00
'redis' => 'Redis (recommended)' ,
'database' => 'MySQL Database' ,
'sync' => 'Sync' ,
];
2026-10-05 18:10:18 -04:00
/** @var array<string, EnvironmentValue> */
2022-10-14 10:59:20 -06:00
protected array $variables = [];
2017-09-22 21:19:57 -05:00
/**
* AppSettingsCommand constructor.
*/
2022-10-14 10:59:20 -06:00
public function __construct ( private Kernel $console )
2017-09-22 21:19:57 -05:00
{
parent :: __construct ();
}
/**
* Handle command execution.
*
2026-10-05 18:10:18 -04:00
* @throws PterodactylException
2017-09-22 21:19:57 -05:00
*/
2022-10-14 10:59:20 -06:00
public function handle () : int
2017-09-22 21:19:57 -05:00
{
2022-05-12 17:53:29 -04:00
if ( empty ( config ( 'hashids.salt' )) || $this -> option ( 'new-salt' )) {
2026-10-05 18:10:18 -04:00
$this -> variables [ 'HASHIDS_SALT' ] = Str :: random ( 20 );
2017-11-05 12:58:25 -06:00
}
2022-05-12 17:53:29 -04:00
$this -> output -> comment ( 'Provide the email address that eggs exported by this Panel should be from. This should be a valid email address.' );
2026-10-05 18:10:18 -04:00
$this -> variables [ 'APP_SERVICE_AUTHOR' ] = JsonValueGuard :: string ( $this -> option ( 'author' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Egg Author Email' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableString ( config ( 'pterodactyl.service.author' , 'unknown@unknown.com' ))
));
2021-12-04 20:52:09 +02:00
2026-10-05 18:10:18 -04:00
if ( ! filter_var ( $this -> variables [ 'APP_SERVICE_AUTHOR' ], FILTER_VALIDATE_EMAIL )) {
2022-05-12 17:53:29 -04:00
$this -> output -> error ( 'The service author email provided is invalid.' );
2022-05-04 19:01:29 -04:00
2021-12-04 20:52:09 +02:00
return 1 ;
}
2022-05-12 17:53:29 -04: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.' );
2026-10-05 18:10:18 -04:00
$this -> variables [ 'APP_URL' ] = JsonValueGuard :: string ( $this -> option ( 'url' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Application URL' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableString ( config ( 'app.url' , 'https://example.com' ))
));
2017-09-22 21:19:57 -05:00
2026-10-05 18:10:18 -04: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' ] = JsonValueGuard :: string ( $this -> option ( 'timezone' ) ?? $this -> anticipate (
2022-05-12 17:53:29 -04:00
'Application Timezone' ,
2026-10-05 18:10:18 -04:00
DateTimeZone :: listIdentifiers (),
JsonValueGuard :: nullableString ( config ( 'app.timezone' ))
));
2017-09-22 21:19:57 -05:00
2026-10-05 18:10:18 -04:00
$selected = JsonValueGuard :: string ( config ( 'cache.default' , 'redis' ));
$cacheDriver = $this -> option ( 'cache' ) ?? $this -> choice (
2022-05-12 17:53:29 -04:00
'Cache Driver' ,
self :: CACHE_DRIVERS ,
array_key_exists ( $selected , self :: CACHE_DRIVERS ) ? $selected : null
2021-01-23 12:33:34 -08:00
);
2026-10-05 18:10:18 -04:00
JsonValueGuard :: assertScalar ( $cacheDriver );
$this -> variables [ 'CACHE_DRIVER' ] = $this -> stringValue ( $cacheDriver );
2017-09-22 21:19:57 -05:00
2026-10-05 18:10:18 -04:00
$selected = JsonValueGuard :: string ( config ( 'session.driver' , 'redis' ));
$sessionDriver = $this -> option ( 'session' ) ?? $this -> choice (
2022-05-12 17:53:29 -04:00
'Session Driver' ,
self :: SESSION_DRIVERS ,
array_key_exists ( $selected , self :: SESSION_DRIVERS ) ? $selected : null
2021-01-23 12:33:34 -08:00
);
2026-10-05 18:10:18 -04:00
JsonValueGuard :: assertScalar ( $sessionDriver );
$this -> variables [ 'SESSION_DRIVER' ] = $this -> stringValue ( $sessionDriver );
2017-09-22 21:19:57 -05:00
2026-10-05 18:10:18 -04:00
$selected = JsonValueGuard :: string ( config ( 'queue.default' , 'redis' ));
$queueDriver = $this -> option ( 'queue' ) ?? $this -> choice (
2022-05-12 17:53:29 -04:00
'Queue Driver' ,
self :: QUEUE_DRIVERS ,
array_key_exists ( $selected , self :: QUEUE_DRIVERS ) ? $selected : null
2021-01-23 12:33:34 -08:00
);
2026-10-05 18:10:18 -04:00
JsonValueGuard :: assertScalar ( $queueDriver );
$this -> variables [ 'QUEUE_CONNECTION' ] = $this -> stringValue ( $queueDriver );
2017-12-30 16:33:00 -06:00
2026-10-05 18:10:18 -04:00
if (( $this -> option ( 'settings-ui' )) !== null ) {
$this -> variables [ 'APP_ENVIRONMENT_ONLY' ] = $this -> option ( 'settings-ui' ) === 'true' ? 'false' : 'true' ;
2017-12-30 16:33:00 -06:00
} else {
2022-05-12 17:53:29 -04:00
$this -> variables [ 'APP_ENVIRONMENT_ONLY' ] = $this -> confirm ( 'Enable UI based settings editor?' , true ) ? 'false' : 'true' ;
2017-12-30 16:33:00 -06:00
}
2017-09-22 21:19:57 -05:00
2026-10-05 18:10:18 -04:00
$this -> output -> comment ( 'Please reference https://pterodactyl.io/docs/v2/panel/additional-configuration#telemetry for more detailed information regarding telemetry data and collection.' );
if (( $this -> option ( 'telemetry' )) !== null ) {
$this -> variables [ 'PTERODACTYL_TELEMETRY_ENABLED' ] = $this -> option ( 'telemetry' ) === 'true' ? 'true' : 'false' ;
} else {
$this -> variables [ 'PTERODACTYL_TELEMETRY_ENABLED' ] = $this -> confirm (
'Enable sending anonymous telemetry data?' ,
JsonValueGuard :: boolean ( config ( 'pterodactyl.telemetry.enabled' , true ))
) ? 'true' : 'false' ;
}
2022-12-04 14:59:45 -07:00
2020-10-25 21:15:49 +01:00
// Make sure session cookies are set as "secure" when using HTTPS
2022-10-14 10:59:20 -06:00
if ( str_starts_with ( $this -> variables [ 'APP_URL' ], 'https://' )) {
2020-10-25 21:15:49 +01:00
$this -> variables [ 'SESSION_SECURE_COOKIE' ] = 'true' ;
}
2017-09-22 21:19:57 -05:00
$this -> checkForRedis ();
$this -> writeToEnvironment ( $this -> variables );
2022-10-14 10:59:20 -06:00
$this -> info ( $this -> console -> output ());
return 0 ;
2017-09-22 21:19:57 -05:00
}
/**
* Check if redis is selected, if so, request connection details and verify them.
*/
2026-10-05 18:10:18 -04:00
private function checkForRedis () : void
2017-09-22 21:19:57 -05:00
{
2026-10-05 18:10:18 -04:00
$items = collect ( $this -> variables ) -> filter ( fn ( bool | float | int | string | null $item ) : bool => $item === 'redis' );
2017-09-22 21:19:57 -05:00
// Redis was not selected, no need to continue.
if ( count ( $items ) === 0 ) {
return ;
}
2026-10-05 18:10:18 -04: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." );
$this -> variables [ 'REDIS_HOST' ] = JsonValueGuard :: string ( $this -> option ( 'redis-host' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Redis Host' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableScalarString ( config ( 'database.redis.default.host' ))
));
2017-09-22 21:19:57 -05:00
$askForRedisPassword = true ;
2026-10-05 18:10:18 -04:00
if ( ! empty ( config ( 'database.redis.default.password' ))) {
$this -> variables [ 'REDIS_PASSWORD' ] = JsonValueGuard :: scalar ( config ( 'database.redis.default.password' ));
2022-05-12 17:53:29 -04:00
$askForRedisPassword = $this -> confirm ( 'It seems a password is already defined for Redis, would you like to change it?' );
2017-09-22 21:19:57 -05:00
}
if ( $askForRedisPassword ) {
2022-05-12 17:53:29 -04: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.' );
2026-10-05 18:10:18 -04:00
$this -> variables [ 'REDIS_PASSWORD' ] = JsonValueGuard :: scalar ( $this -> option ( 'redis-pass' ) ?? $this -> output -> askHidden (
2022-05-12 17:53:29 -04:00
'Redis Password'
2026-10-05 18:10:18 -04:00
));
2017-09-22 21:19:57 -05:00
}
2017-11-04 16:40:48 -05:00
if ( empty ( $this -> variables [ 'REDIS_PASSWORD' ])) {
2017-11-04 16:46:18 -05:00
$this -> variables [ 'REDIS_PASSWORD' ] = 'null' ;
2017-11-04 16:40:48 -05:00
}
2026-10-05 18:10:18 -04:00
$this -> variables [ 'REDIS_PORT' ] = JsonValueGuard :: scalar ( $this -> option ( 'redis-port' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Redis Port' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableScalarString ( config ( 'database.redis.default.port' ))
));
}
/** @param ApiScalar $value */
private function stringValue ( bool | float | int | string | null $value ) : string
{
throw_unless ( is_string ( $value ), InvalidArgumentException :: class , 'Expected a single environment option value.' );
return $value ;
2017-09-22 21:19:57 -05:00
}
}