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 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-08 17:22:14 -04:00
use Illuminate\Support\Facades\DB ;
2026-10-05 18:10:18 -04:00
use PDOException ;
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 database settings for the Panel.' )]
#[ Signature ( 'p:environment:database
2017-09-22 21:19:57 -05:00
{--host= : The connection address for the MySQL server.}
2017-11-04 17:41:52 -05:00
{--port= : The connection port for the MySQL server.}
{--database= : The database to use.}
{--username= : Username to use when connecting.}
2026-10-05 18:10:18 -04:00
{--password= : Password to use for this database.}' )]
class DatabaseSettingsCommand extends Command
{
use EnvironmentWriterTrait ;
2017-09-22 21:19:57 -05:00
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
/**
* DatabaseSettingsCommand constructor.
*/
2026-10-08 17:22:14 -04: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
$this -> output -> note ( 'It is highly recommended to not use "localhost" as your database host as we have seen frequent socket connection issues. If you want to use a local connection you should be using "127.0.0.1".' );
2026-10-05 18:10:18 -04:00
$this -> variables [ 'DB_HOST' ] = JsonValueGuard :: string ( $this -> option ( 'host' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Database Host' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableScalarString ( config ( 'database.connections.mysql.host' , '127.0.0.1' ))
));
2017-09-22 21:19:57 -05:00
2026-10-05 18:10:18 -04:00
$this -> variables [ 'DB_PORT' ] = JsonValueGuard :: scalar ( $this -> option ( 'port' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Database Port' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableScalarString ( config ( 'database.connections.mysql.port' , 3306 ))
));
2017-09-22 21:19:57 -05:00
2026-10-05 18:10:18 -04:00
$this -> variables [ 'DB_DATABASE' ] = JsonValueGuard :: string ( $this -> option ( 'database' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Database Name' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableScalarString ( config ( 'database.connections.mysql.database' , 'panel' ))
));
2017-09-22 21:19:57 -05:00
2022-05-12 17:53:29 -04:00
$this -> output -> note ( 'Using the "root" account for MySQL connections is not only highly frowned upon, it is also not allowed by this application. You\'ll need to have created a MySQL user for this software.' );
2026-10-05 18:10:18 -04:00
$this -> variables [ 'DB_USERNAME' ] = JsonValueGuard :: string ( $this -> option ( 'username' ) ?? $this -> ask (
2022-05-12 17:53:29 -04:00
'Database Username' ,
2026-10-05 18:10:18 -04:00
JsonValueGuard :: nullableScalarString ( config ( 'database.connections.mysql.username' , 'pterodactyl' ))
));
2017-09-22 21:19:57 -05:00
$askForMySQLPassword = true ;
2026-10-05 18:10:18 -04:00
if ( ! empty ( config ( 'database.connections.mysql.password' )) && $this -> input -> isInteractive ()) {
$this -> variables [ 'DB_PASSWORD' ] = JsonValueGuard :: scalar ( config ( 'database.connections.mysql.password' ));
2022-05-12 17:53:29 -04:00
$askForMySQLPassword = $this -> confirm ( 'It appears you already have a MySQL connection password defined, would you like to change it?' );
2017-09-22 21:19:57 -05:00
}
if ( $askForMySQLPassword ) {
2026-10-05 18:10:18 -04:00
$this -> variables [ 'DB_PASSWORD' ] = JsonValueGuard :: scalar ( $this -> option ( 'password' ) ?? $this -> secret ( 'Database Password' ));
2017-09-22 21:19:57 -05:00
}
try {
$this -> testMySQLConnection ();
2026-10-05 18:10:18 -04:00
} catch ( PDOException $pdoException ) {
$this -> output -> error ( sprintf ( 'Unable to connect to the MySQL server using the provided credentials. The error returned was "%s".' , $pdoException -> getMessage ()));
2022-05-12 17:53:29 -04:00
$this -> output -> error ( 'Your connection credentials have NOT been saved. You will need to provide valid connection information before proceeding.' );
2017-09-22 21:19:57 -05:00
2022-05-12 17:53:29 -04:00
if ( $this -> confirm ( 'Go back and try again?' )) {
2026-10-08 17:22:14 -04:00
DB :: disconnect ( '_pterodactyl_command_test' );
2017-09-22 21:19:57 -05:00
return $this -> handle ();
}
return 1 ;
}
$this -> writeToEnvironment ( $this -> variables );
$this -> info ( $this -> console -> output ());
return 0 ;
}
/**
* Test that we can connect to the provided MySQL instance and perform a selection.
*/
2026-10-05 18:10:18 -04:00
private function testMySQLConnection () : void
2017-09-22 21:19:57 -05:00
{
2022-05-12 17:53:29 -04:00
config () -> set ( 'database.connections._pterodactyl_command_test' , [
2019-09-05 21:32:57 -07:00
'driver' => 'mysql' ,
'host' => $this -> variables [ 'DB_HOST' ],
'port' => $this -> variables [ 'DB_PORT' ],
'database' => $this -> variables [ 'DB_DATABASE' ],
'username' => $this -> variables [ 'DB_USERNAME' ],
'password' => $this -> variables [ 'DB_PASSWORD' ],
'charset' => 'utf8mb4' ,
'collation' => 'utf8mb4_unicode_ci' ,
'strict' => true ,
2017-09-22 21:19:57 -05:00
]);
2026-10-08 17:22:14 -04:00
DB :: connection ( '_pterodactyl_command_test' ) -> getPdo ();
2017-09-22 21:19:57 -05:00
}
}