2022-01-11 18:54:32 +00:00
< ? php
2022-01-11 14:09:00 +00:00
2022-01-11 18:54:32 +00:00
use PHPMailer\PHPMailer\Exception ;
use PHPMailer\PHPMailer\PHPMailer ;
2022-01-11 15:12:48 +00:00
2022-01-11 18:54:32 +00:00
require 'phpmailer/Exception.php' ;
require 'phpmailer/PHPMailer.php' ;
require 'phpmailer/SMTP.php' ;
2022-01-11 15:12:48 +00:00
2023-01-05 17:01:42 +00:00
include 'functions.php' ;
2022-01-11 13:09:20 +00:00
2023-03-29 21:30:51 +00:00
2022-01-11 18:54:32 +00:00
if ( isset ( $_POST [ 'checkDB' ])) {
$values = [
//SETTINGS::VALUE => REQUEST-VALUE (coming from the html-form)
2023-01-05 17:01:42 +00:00
'DB_HOST' => 'databasehost' ,
'DB_DATABASE' => 'database' ,
'DB_USERNAME' => 'databaseuser' ,
'DB_PASSWORD' => 'databaseuserpass' ,
'DB_PORT' => 'databaseport' ,
'DB_CONNECTION' => 'databasedriver' ,
2022-01-11 18:54:32 +00:00
];
2023-03-30 20:04:17 +00:00
wh_log ( 'Trying to connect to the Database' , 'debug' );
2023-01-05 17:01:42 +00:00
$db = new mysqli ( $_POST [ 'databasehost' ], $_POST [ 'databaseuser' ], $_POST [ 'databaseuserpass' ], $_POST [ 'database' ], $_POST [ 'databaseport' ]);
2022-01-11 18:54:32 +00:00
if ( $db -> connect_error ) {
2023-03-30 11:00:17 +00:00
wh_log ( $db -> connect_error , 'error' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=2&message=Could not connect to the Database' );
exit ();
2022-01-11 18:54:32 +00:00
}
2022-01-11 13:09:20 +00:00
2022-01-11 18:54:32 +00:00
foreach ( $values as $key => $value ) {
$param = $_POST [ $value ];
2023-01-05 17:01:42 +00:00
// if ($key == "DB_PASSWORD") {
// $param = '"' . $_POST[$value] . '"';
// }
2023-03-30 21:08:08 +00:00
setenv ( $key , $param );
2022-01-11 18:54:32 +00:00
}
2022-01-20 08:26:46 +00:00
2023-03-30 20:04:17 +00:00
wh_log ( 'Database connection successful' , 'debug' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=2.5' );
2022-01-11 18:54:32 +00:00
}
2022-01-11 13:09:20 +00:00
2022-01-11 18:54:32 +00:00
if ( isset ( $_POST [ 'checkGeneral' ])) {
2023-03-30 20:04:17 +00:00
wh_log ( 'setting app settings' , 'debug' );
2023-03-29 23:11:49 +00:00
$appname = '"' . $_POST [ 'name' ] . '"' ;
2022-01-11 18:54:32 +00:00
$appurl = $_POST [ 'url' ];
2022-01-11 14:09:00 +00:00
2023-01-05 17:01:42 +00:00
if ( substr ( $appurl , - 1 ) === '/' ) {
$appurl = substr_replace ( $appurl , '' , - 1 );
2022-01-11 18:54:32 +00:00
}
2022-01-11 14:09:00 +00:00
2023-03-30 21:08:08 +00:00
setenv ( 'APP_NAME' , $appname );
setenv ( 'APP_URL' , $appurl );
2022-01-11 14:09:00 +00:00
2023-03-30 20:04:17 +00:00
wh_log ( 'App settings set' , 'debug' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=4' );
2022-01-11 18:54:32 +00:00
}
2022-01-24 11:25:30 +00:00
if ( isset ( $_POST [ 'feedDB' ])) {
2023-03-30 20:04:17 +00:00
wh_log ( 'Feeding the Database' , 'debug' );
2023-01-05 17:01:42 +00:00
$logs = '' ;
2022-01-20 08:26:46 +00:00
2023-03-30 21:08:08 +00:00
//$logs .= run_console(setenv('COMPOSER_HOME', dirname(__FILE__, 3) . '/vendor/bin/composer'));
2023-01-05 17:01:42 +00:00
//$logs .= run_console('composer install --no-dev --optimize-autoloader');
2023-03-30 21:08:08 +00:00
if ( ! str_contains ( getenv ( 'APP_KEY' ), 'base64' )) {
2022-02-07 14:44:59 +00:00
$logs .= run_console ( 'php artisan key:generate --force' );
2023-01-05 17:01:42 +00:00
} else {
$logs .= " Key already exists. Skipping \n " ;
2022-02-07 14:44:59 +00:00
}
2022-01-24 11:12:50 +00:00
$logs .= run_console ( 'php artisan storage:link' );
2023-03-15 09:48:10 +00:00
$logs .= run_console ( 'php artisan migrate --seed --force' );
$logs .= run_console ( 'php artisan db:seed --class=ExampleItemsSeeder --force' );
2022-01-24 11:12:50 +00:00
2023-03-30 20:04:17 +00:00
wh_log ( $logs , 'debug' );
2022-01-24 11:12:50 +00:00
2023-03-30 21:08:08 +00:00
if ( str_contains ( getenv ( 'APP_KEY' ), 'base64' )) {
2023-03-30 20:04:17 +00:00
wh_log ( 'Feeding the Database successful' , 'debug' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=3' );
2022-01-24 11:12:50 +00:00
} else {
2023-03-30 20:04:17 +00:00
wh_log ( 'Feeding the Database failed' , 'debug' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=2.5&message=There was an error. Please check the .txt file in /var/www/controlpanel/public/install/logs !' );
2022-01-24 11:12:50 +00:00
}
2022-01-20 08:26:46 +00:00
}
2022-01-11 18:54:32 +00:00
if ( isset ( $_POST [ 'checkSMTP' ])) {
2023-03-30 20:04:17 +00:00
wh_log ( 'Checking SMTP Settings' , 'debug' );
2022-01-11 18:54:32 +00:00
try {
2022-01-11 14:09:00 +00:00
$mail = new PHPMailer ( true );
//Server settings
$mail -> isSMTP (); // Send using SMTP
2022-01-11 18:54:32 +00:00
$mail -> Host = $_POST [ 'host' ]; // Set the SMTP server to send through
$mail -> SMTPAuth = true ; // Enable SMTP authentication
$mail -> Username = $_POST [ 'user' ]; // SMTP username
$mail -> Password = $_POST [ 'pass' ]; // SMTP password
2022-01-11 14:09:00 +00:00
$mail -> SMTPSecure = PHPMailer :: ENCRYPTION_STARTTLS ; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
2022-01-11 18:54:32 +00:00
$mail -> Port = $_POST [ 'port' ]; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS`
2022-01-11 14:09:00 +00:00
//Recipients
$mail -> setFrom ( $_POST [ 'user' ], $_POST [ 'user' ]);
2022-01-11 18:54:32 +00:00
$mail -> addAddress ( $_POST [ 'user' ], $_POST [ 'user' ]); // Add a recipient
2022-01-11 14:09:00 +00:00
// Content
$mail -> isHTML ( true ); // Set email format to HTML
$mail -> Subject = 'It Worked!' ;
2023-01-05 17:01:42 +00:00
$mail -> Body = 'Your E-Mail Settings are correct!' ;
2022-01-11 14:09:00 +00:00
$mail -> send ();
2022-01-11 18:54:32 +00:00
} catch ( Exception $e ) {
2023-03-30 20:04:17 +00:00
wh_log ( $mail -> ErrorInfo , 'error' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=4&message=Something wasnt right when sending the E-Mail!' );
exit ();
2022-01-11 14:09:00 +00:00
}
2023-03-30 20:04:17 +00:00
wh_log ( 'SMTP Settings are correct' , 'debug' );
wh_log ( 'Updating Database' , 'debug' );
2023-03-30 21:08:08 +00:00
$db = new mysqli ( getenv ( 'DB_HOST' ), getenv ( 'DB_USERNAME' ), getenv ( 'DB_PASSWORD' ), getenv ( 'DB_DATABASE' ), getenv ( 'DB_PORT' ));
2022-01-24 11:12:50 +00:00
if ( $db -> connect_error ) {
2023-03-30 11:00:17 +00:00
wh_log ( $db -> connect_error , 'error' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=4&message=Could not connect to the Database: ' );
exit ();
2022-01-24 11:12:50 +00:00
}
2022-01-11 18:54:32 +00:00
$values = [
2023-03-15 09:48:10 +00:00
'mail_mailer' => $_POST [ 'method' ],
'mail_host' => $_POST [ 'host' ],
'mail_port' => $_POST [ 'port' ],
'mail_username' => $_POST [ 'user' ],
2023-03-29 22:29:04 +00:00
'mail_password' => encryptSettingsValue ( $_POST [ 'pass' ]),
2023-03-15 09:48:10 +00:00
'mail_encryption' => $_POST [ 'encryption' ],
'mail_from_address' => $_POST [ 'user' ],
2022-01-11 18:54:32 +00:00
];
foreach ( $values as $key => $value ) {
2023-03-30 21:08:08 +00:00
$query = 'UPDATE `' . getenv ( 'DB_DATABASE' ) . " `.`settings` SET `payload` = ' $value ' WHERE `name` = ' $key ' AND `group` = mail " ;
2022-01-24 11:12:50 +00:00
$db -> query ( $query );
2022-01-11 18:54:32 +00:00
}
2022-01-24 11:12:50 +00:00
2023-03-30 20:04:17 +00:00
wh_log ( 'Database updated' , 'debug' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=5' );
2022-01-11 18:54:32 +00:00
}
if ( isset ( $_POST [ 'checkPtero' ])) {
2023-03-30 20:04:17 +00:00
wh_log ( 'Checking Pterodactyl Settings' , 'debug' );
2022-01-11 18:54:32 +00:00
$url = $_POST [ 'url' ];
$key = $_POST [ 'key' ];
2022-08-11 13:14:50 +00:00
$clientkey = $_POST [ 'clientkey' ];
2022-01-11 18:54:32 +00:00
2023-01-05 17:01:42 +00:00
if ( substr ( $url , - 1 ) === '/' ) {
$url = substr_replace ( $url , '' , - 1 );
2022-01-11 18:54:32 +00:00
}
2023-03-29 23:11:49 +00:00
$callpteroURL = $url . '/api/client/account' ;
2022-08-11 13:14:50 +00:00
$call = curl_init ();
curl_setopt ( $call , CURLOPT_URL , $callpteroURL );
curl_setopt ( $call , CURLOPT_RETURNTRANSFER , true );
2023-01-05 17:01:42 +00:00
curl_setopt ( $call , CURLOPT_HTTPHEADER , [
2023-03-29 21:30:51 +00:00
'Accept: Application/vnd.pterodactyl.v1+json' ,
2023-01-05 17:01:42 +00:00
'Content-Type: application/json' ,
2023-03-29 23:11:49 +00:00
'Authorization: Bearer ' . $clientkey ,
2023-01-05 17:01:42 +00:00
]);
2022-08-11 13:14:50 +00:00
$callresponse = curl_exec ( $call );
$callresult = json_decode ( $callresponse , true );
curl_close ( $call ); // Close the connection
2023-03-29 23:11:49 +00:00
$pteroURL = $url . '/api/application/users' ;
2022-01-11 18:54:32 +00:00
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $pteroURL );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
2023-01-05 17:01:42 +00:00
curl_setopt ( $ch , CURLOPT_HTTPHEADER , [
2023-03-29 21:30:51 +00:00
'Accept: Application/vnd.pterodactyl.v1+json' ,
2023-01-05 17:01:42 +00:00
'Content-Type: application/json' ,
2023-03-29 23:11:49 +00:00
'Authorization: Bearer ' . $key ,
2023-01-05 17:01:42 +00:00
]);
2022-01-11 18:54:32 +00:00
$response = curl_exec ( $ch );
$result = json_decode ( $response , true );
curl_close ( $ch ); // Close the connection
2023-03-29 23:11:49 +00:00
if ( ! is_array ( $result ) and $result [ 'errors' ][ 0 ] !== null ) {
2023-03-29 21:30:51 +00:00
header ( 'LOCATION: index.php?step=5&message=Couldn\'t connect to Pterodactyl. Make sure your API key has all read and write permissions!' );
2023-03-30 11:00:17 +00:00
wh_log ( 'API CALL ERROR: ' . $result [ 'errors' ][ 0 ][ 'code' ], 'error' );
2023-01-05 17:01:42 +00:00
exit ();
2023-03-29 23:11:49 +00:00
} elseif ( ! is_array ( $callresult ) and $callresult [ 'errors' ][ 0 ] !== null or $callresult [ 'attributes' ][ 'admin' ] == false ) {
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=5&message=Your ClientAPI Key is wrong or the account is not an admin!' );
2023-03-30 11:00:17 +00:00
wh_log ( 'API CALL ERROR: ' . $callresult [ 'errors' ][ 0 ][ 'code' ], 'error' );
2023-01-05 17:01:42 +00:00
exit ();
2022-01-11 18:54:32 +00:00
} else {
2023-03-30 20:04:17 +00:00
wh_log ( 'Pterodactyl Settings are correct' , 'debug' );
wh_log ( 'Updating Database' , 'debug' );
2023-03-29 22:29:04 +00:00
$key = encryptSettingsValue ( $key );
$clientkey = encryptSettingsValue ( $clientkey );
2022-01-24 11:12:50 +00:00
2023-03-30 21:08:08 +00:00
$query1 = 'UPDATE `' . getenv ( 'DB_DATABASE' ) . " `.`settings` SET `payload` = ' " . json_encode ( $url ) . " ' WHERE (`name` = 'panel_url' AND `group` = 'pterodactyl') " ;
$query2 = 'UPDATE `' . getenv ( 'DB_DATABASE' ) . " `.`settings` SET `payload` = ' " . json_encode ( $key ) . " ' WHERE (`name` = 'admin_token' AND `group` = 'pterodactyl') " ;
$query3 = 'UPDATE `' . getenv ( 'DB_DATABASE' ) . " `.`settings` SET `payload` = ' " . json_encode ( $clientkey ) . " ' WHERE (`name` = 'user_token' AND `group` = 'pterodactyl') " ;
2022-01-24 11:12:50 +00:00
2023-03-30 21:08:08 +00:00
$db = new mysqli ( getenv ( 'DB_HOST' ), getenv ( 'DB_USERNAME' ), getenv ( 'DB_PASSWORD' ), getenv ( 'DB_DATABASE' ), getenv ( 'DB_PORT' ));
2022-01-11 18:54:32 +00:00
if ( $db -> connect_error ) {
2023-03-30 11:00:17 +00:00
wh_log ( $db -> connect_error , 'error' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=5&message=Could not connect to the Database' );
exit ();
2022-01-11 18:54:32 +00:00
}
2022-08-11 13:14:50 +00:00
if ( $db -> query ( $query1 ) && $db -> query ( $query2 ) && $db -> query ( $query3 )) {
2023-03-30 20:04:17 +00:00
wh_log ( 'Database updated' , 'debug' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=6' );
2022-01-11 18:54:32 +00:00
} else {
2023-03-30 11:00:17 +00:00
wh_log ( $db -> error , 'error' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=5&message=Something went wrong when communicating with the Database!' );
2022-01-11 18:54:32 +00:00
}
}
}
if ( isset ( $_POST [ 'createUser' ])) {
2023-03-30 20:04:17 +00:00
wh_log ( 'Creating User' , 'debug' );
2023-03-30 21:08:08 +00:00
$db = new mysqli ( getenv ( 'DB_HOST' ), getenv ( 'DB_USERNAME' ), getenv ( 'DB_PASSWORD' ), getenv ( 'DB_DATABASE' ), getenv ( 'DB_PORT' ));
2022-01-11 18:54:32 +00:00
if ( $db -> connect_error ) {
2023-03-30 11:00:17 +00:00
wh_log ( $db -> connect_error , 'error' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=6&message=Could not connect to the Database' );
exit ();
2022-01-11 18:54:32 +00:00
}
$pteroID = $_POST [ 'pteroID' ];
$pass = $_POST [ 'pass' ];
$repass = $_POST [ 'repass' ];
2022-01-24 11:12:50 +00:00
2023-03-30 21:08:08 +00:00
$key = $db -> query ( 'SELECT `payload` FROM `' . getenv ( 'DB_DATABASE' ) . " `.`settings` WHERE `name` = 'admin_token' AND `group` = 'pterodactyl' " ) -> fetch_assoc ();
2023-03-29 22:29:04 +00:00
$key = encryptSettingsValue ( $key [ 'value' ]);
2023-03-30 21:08:08 +00:00
$pterobaseurl = $db -> query ( 'SELECT `payload` FROM `' . getenv ( 'DB_DATABASE' ) . " `.`settings` WHERE `name` = 'panel_url' AND `group` = 'pterodactyl' " ) -> fetch_assoc ();
2022-01-11 18:54:32 +00:00
2023-03-29 23:11:49 +00:00
$pteroURL = $pterobaseurl [ 'value' ] . '/api/application/users/' . $pteroID ;
2022-01-11 18:54:32 +00:00
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $pteroURL );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
2023-01-05 17:01:42 +00:00
curl_setopt ( $ch , CURLOPT_HTTPHEADER , [
'Accept: application/json' ,
'Content-Type: application/json' ,
2023-03-29 23:11:49 +00:00
'Authorization: Bearer ' . $key ,
2023-01-05 17:01:42 +00:00
]);
2022-01-11 18:54:32 +00:00
$response = curl_exec ( $ch );
$result = json_decode ( $response , true );
curl_close ( $ch ); // Close the connection
2023-03-29 23:11:49 +00:00
if ( ! $result [ 'attributes' ][ 'email' ]) {
header ( 'LOCATION: index.php?step=6&message=Could not find the user with pterodactyl ID ' . $pteroID );
2023-01-05 17:01:42 +00:00
exit ();
2022-01-11 18:54:32 +00:00
}
if ( $pass !== $repass ) {
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=6&message=The Passwords did not match!' );
exit ();
2022-01-11 18:54:32 +00:00
}
2023-01-05 17:01:42 +00:00
$mail = $result [ 'attributes' ][ 'email' ];
$name = $result [ 'attributes' ][ 'username' ];
2022-01-11 18:54:32 +00:00
$pass = password_hash ( $pass , PASSWORD_DEFAULT );
2023-03-29 23:11:49 +00:00
$pteroURL = $pterobaseurl [ 'value' ] . '/api/application/users/' . $pteroID ;
2022-01-11 18:54:32 +00:00
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $pteroURL );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
2023-01-05 17:01:42 +00:00
curl_setopt ( $ch , CURLOPT_HTTPHEADER , [
'Accept: application/json' ,
'Content-Type: application/json' ,
2023-03-29 23:11:49 +00:00
'Authorization: Bearer ' . $key [ 'value' ],
2023-01-05 17:01:42 +00:00
]);
curl_setopt ( $ch , CURLOPT_POSTFIELDS , [
'email' => $mail ,
'username' => $name ,
'first_name' => $name ,
'last_name' => $name ,
'password' => $pass ,
]);
2022-01-11 18:54:32 +00:00
$response = curl_exec ( $ch );
$result = json_decode ( $response , true );
curl_close ( $ch ); // Close the connection
2023-03-29 23:11:49 +00:00
if ( ! is_array ( $result ) or in_array ( $result [ 'errors' ][ 0 ][ 'code' ], $result )) {
2023-03-30 20:04:17 +00:00
header ( 'LOCATION: index.php?step=5&message=Couldn\'t connect to Pterodactyl. Make sure your API key has all read and write permissions!' );
2023-01-05 17:01:42 +00:00
exit ();
2022-01-11 18:54:32 +00:00
}
2022-06-07 15:30:38 +00:00
$random = generateRandomString ();
2023-03-30 21:08:08 +00:00
$query1 = 'INSERT INTO `' . getenv ( 'DB_DATABASE' ) . " `.`users` (`name`, `role`, `credits`, `server_limit`, `pterodactyl_id`, `email`, `password`, `created_at`, `referral_code`) VALUES (' $name ', 'admin', '250', '1', ' $pteroID ', ' $mail ', ' $pass ', CURRENT_TIMESTAMP, ' $random ') " ;
2022-01-11 18:54:32 +00:00
if ( $db -> query ( $query1 )) {
2023-03-30 20:04:17 +00:00
wh_log ( 'Created user with Email ' . $mail . ' and pterodactyl ID ' . $pteroID , 'info' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=7' );
2022-01-11 18:54:32 +00:00
} else {
2023-03-30 11:00:17 +00:00
wh_log ( $db -> error , 'error' );
2023-01-05 17:01:42 +00:00
header ( 'LOCATION: index.php?step=6&message=Something went wrong when communicating with the Database' );
2022-01-11 18:54:32 +00:00
}
2023-04-05 07:47:28 +00:00
}