Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix installer setting pterodactyl settings without encryption, causing 500 Error in settings page. #1

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/Console/Commands/GetSettingCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GetSettingCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'settings:get {class : Settings Class (Example: GeneralSettings)} {key} {--sameline : Outputs the result without newline, useful for implementing in scripts.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Gets value of a setting key and decrypts it if needed.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{

$class = $this->argument('class');
$key = $this->argument('key');
$sameline = $this->option('sameline');

try {
$settings_class = "App\\Settings\\$class";
$settings = new $settings_class();

$this->output->write($settings->$key, !$sameline);

return Command::SUCCESS;
} catch (\Throwable $th) {
$this->error('Error: ' . $th->getMessage());
return Command::FAILURE;
}

return Command::SUCCESS;
}
}
52 changes: 52 additions & 0 deletions app/Console/Commands/SetSettingCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Console\Commands;

use Exception;
use Illuminate\Console\Command;

class SetSettingCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'settings:set {class : Settings Class (Example: GeneralSettings)} {key : Unique setting key} {value : Value to set}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Set value of a setting key.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{

$class = $this->argument('class');
$key = $this->argument('key');
$value = $this->argument('value');

try {
$settings_class = "App\\Settings\\$class";
$settings = new $settings_class();

$settings->$key = $value;

$settings->save();

$this->info("Successfully updated '$key'.");
} catch (\Throwable $th) {
$this->error('Error: ' . $th->getMessage());
return Command::FAILURE;
}

return Command::SUCCESS;
}
}
84 changes: 40 additions & 44 deletions public/install/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,30 @@
$mail = new PHPMailer(true);

//Server settings
$mail->isSMTP(); // Send using SMTP
$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
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = $_POST['port']; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS`

//Recipients
// Send using SMTP
$mail->isSMTP();
$mail->Host = $_POST['host'];
// Enable SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = $_POST['user'];
$mail->Password = $_POST['pass'];
$mail->SMTPSecure = $_POST['encryption'];
$mail->Port = (int) $_POST['port'];

// Test E-mail metadata
$mail->setFrom($_POST['user'], $_POST['user']);
$mail->addAddress($_POST['user'], $_POST['user']); // Add a recipient
$mail->addAddress($_POST['user'], $_POST['user']);

// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'It Worked!';
// Set email format to HTML
$mail->isHTML(true);
$mail->Subject = 'It Worked! - Test E-Mail from Ctrlpanel.gg';
$mail->Body = 'Your E-Mail Settings are correct!';

$mail->send();
} catch (Exception $e) {
wh_log($mail->ErrorInfo, 'error');
header('LOCATION: index.php?step=4&message=Something wasnt right when sending the E-Mail!');
header('LOCATION: index.php?step=4&message=Something went wrong while sending test E-Mail!<br>' . $mail->ErrorInfo);
exit();
}

Expand All @@ -140,8 +143,7 @@
];

foreach ($values as $key => $value) {
$query = 'UPDATE `' . getenv('DB_DATABASE') . "`.`settings` SET `payload` = '$value' WHERE `name` = '$key' AND `group` = 'mail'";
$db->query($query);
run_console("php artisan settings:set 'MailSettings' '$key' '$value'");
}

wh_log('Database updated', 'debug');
Expand Down Expand Up @@ -197,34 +199,22 @@
exit();
} else {
wh_log('Pterodactyl Settings are correct', 'debug');
wh_log('Updating Database', 'debug');

$key = $key;
$clientkey = $clientkey;

$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')";

$db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
if ($db->connect_error) {
wh_log($db->connect_error, 'error');
header('LOCATION: index.php?step=5&message=Could not connect to the Database');
exit();
}

if ($db->query($query1) && $db->query($query2) && $db->query($query3)) {
try {
run_console("php artisan settings:set 'PterodactylSettings' 'panel_url' '$url'");
run_console("php artisan settings:set 'PterodactylSettings' 'admin_token' '$key'");
run_console("php artisan settings:set 'PterodactylSettings' 'user_token' '$clientkey'");
wh_log('Database updated', 'debug');
header('LOCATION: index.php?step=6');
} else {
wh_log($db->error, 'error');
header('LOCATION: index.php?step=5&message=Something went wrong when communicating with the Database!');
} catch (\Throwable $th) {
wh_log("Setting Pterodactyl information failed.", 'error');
header("LOCATION: index.php?step=5&message=" . $th->getMessage() . " <br>Please check the installer.log file in /var/www/controlpanel/storage/logs!");
}
}
}

if (isset($_POST['createUser'])) {
wh_log('Creating User', 'debug');
wh_log('Getting Pterodactyl User', 'debug');
$db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
if ($db->connect_error) {
wh_log($db->connect_error, 'error');
Expand All @@ -236,19 +226,26 @@
$pass = $_POST['pass'];
$repass = $_POST['repass'];

$key = $db->query('SELECT `payload` FROM `' . getenv('DB_DATABASE') . "`.`settings` WHERE `name` = 'admin_token' AND `group` = 'pterodactyl'")->fetch_assoc();
$key = removeQuotes($key['payload']);
$pterobaseurl = $db->query('SELECT `payload` FROM `' . getenv('DB_DATABASE') . "`.`settings` WHERE `name` = 'panel_url' AND `group` = 'pterodactyl'")->fetch_assoc();
try {
$panel_url = run_console("php artisan settings:get 'PterodactylSettings' 'panel_url' --sameline");
$admin_token = run_console("php artisan settings:get 'PterodactylSettings' 'admin_token' --sameline");
wh_log('Database updated', 'debug');
header('LOCATION: index.php?step=6');
} catch (\Throwable $th) {
wh_log("Getting Pterodactyl information failed.", 'error');
header("LOCATION: index.php?step=5&message=" . $th->getMessage() . " <br>Please check the installer.log file in /var/www/controlpanel/storage/logs!");
}

$panel_api_url = $panel_url . '/api/application/users/' . $pteroID;

$pteroURL = removeQuotes($pterobaseurl['payload']) . '/api/application/users/' . $pteroID;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $pteroURL);
curl_setopt($ch, CURLOPT_URL, $panel_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $key,
'Authorization: Bearer ' . $admin_token,
]);
$response = curl_exec($ch);
$result = json_decode($response, true);
Expand All @@ -267,15 +264,14 @@
$name = $result['attributes']['username'];
$pass = password_hash($pass, PASSWORD_DEFAULT);

$pteroURL = removeQuotes($pterobaseurl['payload']) . '/api/application/users/' . $pteroID;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $pteroURL);
curl_setopt($ch, CURLOPT_URL, $panel_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $key,
'Authorization: Bearer ' . $admin_token,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'email' => $mail,
Expand Down
58 changes: 35 additions & 23 deletions public/install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,36 @@ function cardStart($title, $subtitle = null)

<li class="<?php echo checkWriteable() == true ? 'ok' : 'not-ok'; ?> check">Write-permissions on .env-file</li>

<li class="<?php echo checkPhpVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check"> php
version: <?php echo phpversion(); ?> (minimum required <?php echo $requirements['minPhp']; ?>)</li>
<li class="<?php echo checkPhpVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check">
php version: <?php echo phpversion(); ?> (minimum required <?php echo $requirements['minPhp']; ?>)
</li>

<li class="<?php echo getMySQLVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check"> mysql
version: <?php echo getMySQLVersion(); ?> (minimum required <?php echo $requirements['mysql']; ?>)</li>
<li class="<?php echo getMySQLVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check">
mysql version: <?php echo getMySQLVersion(); ?> (minimum required <?php echo $requirements['mysql']; ?>)
</li>

<li class="<?php echo count(checkExtensions()) == 0 ? 'ok' : 'not-ok'; ?> check"> Missing
php-extentions: <?php echo count(checkExtensions()) == 0 ? 'none' : '';
foreach (checkExtensions() as $ext) {
echo $ext . ', ';
}

echo count(checkExtensions()) == 0 ? '' : '(Proceed anyway)'; ?></li>
<li class="<?php echo count(checkExtensions()) == 0 ? 'ok' : 'not-ok'; ?> check">
Missing php-extentions:
<?php echo count(checkExtensions()) == 0 ? 'none' : '';
foreach (checkExtensions() as $ext) {
echo $ext . ', ';
}
echo count(checkExtensions()) == 0 ? '' : '(Proceed anyway)'; ?>
</li>


<!-- <li class="<?php echo getZipVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check"> Zip
version: <?php echo getZipVersion(); ?> </li> -->

<li class="<?php echo getGitVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check"> Git
version: <?php echo getGitVersion(); ?> </li>
<li class="<?php echo getGitVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check">
Git version:
<?php echo getGitVersion(); ?>
</li>

<li class="<?php echo getTarVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check"> Tar
version: <?php echo getTarVersion(); ?> </li>
<li class="<?php echo getTarVersion() === 'OK' ? 'ok' : 'not-ok'; ?> check">
Tar version:
<?php echo getTarVersion(); ?>
</li>
</ul>

</div>
Expand Down Expand Up @@ -143,7 +150,7 @@ function cardStart($title, $subtitle = null)
<div class="form-group">
<div class="flex flex-col mb-3">
<label for="databaseuser">Database User</label>
<input x-model="databaseuser" id="databaseuser" name="databaseuser" type="text" required value="controlpaneluser" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
<input x-model="databaseuser" id="databaseuser" name="databaseuser" type="text" required value="ctrlpaneluser" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
</div>
</div>
<div class="form-group">
Expand All @@ -156,7 +163,7 @@ function cardStart($title, $subtitle = null)
<div class="form-group">
<div class="flex flex-col">
<label for="database">Database</label>
<input x-model="database" id="database" name="database" type="text" required value="controlpanel" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
<input x-model="database" id="database" name="database" type="text" required value="ctrlpanel" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
</div>
</div>

Expand Down Expand Up @@ -213,8 +220,8 @@ function cardStart($title, $subtitle = null)
</div>
<div class="form-group">
<div class="flex flex-col">
<label for="name">Host Name</label>
<input id="name" name="name" type="text" required value="" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
<label for="name">Dashboard Name</label>
<input id="name" name="name" type="text" required value="CtrlPanel" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
</div>
</div>

Expand Down Expand Up @@ -248,8 +255,9 @@ function cardStart($title, $subtitle = null)
<div class="form-group">
<div class="flex flex-col mb-3">
<label for="method">Your E-Mail Method</label>
<input id="method" name="method" type="text" required value="smtp" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">

<select id="method" name="method" required class="px-2 py-2 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
<option value="smtp" selected>SMTP</option>
</select>
</div>
</div>
<div class="form-group">
Expand Down Expand Up @@ -284,7 +292,11 @@ function cardStart($title, $subtitle = null)
<div class="form-group">
<div class="flex flex-col">
<label for="encryption">Your Mail encryption method</label>
<input id="encryption" name="encryption" type="text" required value="tls" class="px-2 py-1 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
<select id="encryption" name="encryption" required class="px-2 py-2 bg-[#1D2125] border-2 focus:border-sky-500 box-border rounded-md border-transparent outline-none">
<option value="tls" selected>TLS</option>
<option value="ssl">SSL</option>
<option value="null">None</option>
</select>
</div>
</div>

Expand Down Expand Up @@ -431,4 +443,4 @@ function cardStart($title, $subtitle = null)
?>
</body>

</html>
</html>