(Refactor) Use laravel's logging system, specified a few log levels and refactor the function calls

This commit is contained in:
Jens 2023-03-30 13:00:17 +02:00 committed by IceToast
parent 470bc6d545
commit f63d8080a3
2 changed files with 30 additions and 21 deletions

View file

@ -27,7 +27,7 @@ if (isset($_POST['checkDB'])) {
$db = new mysqli($_POST['databasehost'], $_POST['databaseuser'], $_POST['databaseuserpass'], $_POST['database'], $_POST['databaseport']); $db = new mysqli($_POST['databasehost'], $_POST['databaseuser'], $_POST['databaseuserpass'], $_POST['database'], $_POST['databaseport']);
if ($db->connect_error) { if ($db->connect_error) {
wh_log($db->connect_error); wh_log($db->connect_error, 'error');
header('LOCATION: index.php?step=2&message=Could not connect to the Database'); header('LOCATION: index.php?step=2&message=Could not connect to the Database');
exit(); exit();
} }
@ -71,7 +71,7 @@ if (isset($_POST['feedDB'])) {
$logs .= run_console('php artisan migrate --seed --force'); $logs .= run_console('php artisan migrate --seed --force');
$logs .= run_console('php artisan db:seed --class=ExampleItemsSeeder --force'); $logs .= run_console('php artisan db:seed --class=ExampleItemsSeeder --force');
wh_log($logs); wh_log($logs, 'info');
if (strpos(getenv('APP_KEY'), 'base64') !== false) { if (strpos(getenv('APP_KEY'), 'base64') !== false) {
header('LOCATION: index.php?step=3'); header('LOCATION: index.php?step=3');
@ -110,7 +110,7 @@ if (isset($_POST['checkSMTP'])) {
$db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT')); $db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
if ($db->connect_error) { if ($db->connect_error) {
wh_log($db->connect_error); wh_log($db->connect_error, 'error');
header('LOCATION: index.php?step=4&message=Could not connect to the Database: '); header('LOCATION: index.php?step=4&message=Could not connect to the Database: ');
exit(); exit();
} }
@ -171,11 +171,11 @@ if (isset($_POST['checkPtero'])) {
if (!is_array($result) and $result['errors'][0] !== null) { if (!is_array($result) and $result['errors'][0] !== null) {
header('LOCATION: index.php?step=5&message=Couldn\'t connect to Pterodactyl. Make sure your API key has all read and write permissions!'); header('LOCATION: index.php?step=5&message=Couldn\'t connect to Pterodactyl. Make sure your API key has all read and write permissions!');
wh_log('API CALL ERROR: ' . $result['errors'][0]['code']); wh_log('API CALL ERROR: ' . $result['errors'][0]['code'], 'error');
exit(); exit();
} elseif (!is_array($callresult) and $callresult['errors'][0] !== null or $callresult['attributes']['admin'] == false) { } elseif (!is_array($callresult) and $callresult['errors'][0] !== null or $callresult['attributes']['admin'] == false) {
header('LOCATION: index.php?step=5&message=Your ClientAPI Key is wrong or the account is not an admin!'); header('LOCATION: index.php?step=5&message=Your ClientAPI Key is wrong or the account is not an admin!');
wh_log('API CALL ERROR: ' . $callresult['errors'][0]['code']); wh_log('API CALL ERROR: ' . $callresult['errors'][0]['code'], 'error');
exit(); exit();
} else { } else {
$key = encryptSettingsValue($key); $key = encryptSettingsValue($key);
@ -187,7 +187,7 @@ if (isset($_POST['checkPtero'])) {
$db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT')); $db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
if ($db->connect_error) { if ($db->connect_error) {
wh_log($db->connect_error); wh_log($db->connect_error, 'error');
header('LOCATION: index.php?step=5&message=Could not connect to the Database'); header('LOCATION: index.php?step=5&message=Could not connect to the Database');
exit(); exit();
} }
@ -195,7 +195,7 @@ if (isset($_POST['checkPtero'])) {
if ($db->query($query1) && $db->query($query2) && $db->query($query3)) { if ($db->query($query1) && $db->query($query2) && $db->query($query3)) {
header('LOCATION: index.php?step=6'); header('LOCATION: index.php?step=6');
} else { } else {
wh_log($db->error); wh_log($db->error, 'error');
header('LOCATION: index.php?step=5&message=Something went wrong when communicating with the Database!'); header('LOCATION: index.php?step=5&message=Something went wrong when communicating with the Database!');
} }
} }
@ -204,7 +204,7 @@ if (isset($_POST['checkPtero'])) {
if (isset($_POST['createUser'])) { if (isset($_POST['createUser'])) {
$db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT')); $db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'), getenv('DB_PORT'));
if ($db->connect_error) { if ($db->connect_error) {
wh_log($db->connect_error); wh_log($db->connect_error, 'error');
header('LOCATION: index.php?step=6&message=Could not connect to the Database'); header('LOCATION: index.php?step=6&message=Could not connect to the Database');
exit(); exit();
} }
@ -274,10 +274,10 @@ if (isset($_POST['createUser'])) {
$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')"; $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')";
if ($db->query($query1)) { if ($db->query($query1)) {
wh_log('[USER MAKER] Created user with Email ' . $mail . ' and pterodactyl ID ' . $pteroID); wh_log('[USER MAKER] Created user with Email ' . $mail . ' and pterodactyl ID ' . $pteroID, 'info');
header('LOCATION: index.php?step=7'); header('LOCATION: index.php?step=7');
} else { } else {
wh_log($db->error); wh_log($db->error, 'error');
header('LOCATION: index.php?step=6&message=Something went wrong when communicating with the Database'); header('LOCATION: index.php?step=6&message=Something went wrong when communicating with the Database');
} }
} }

View file

@ -3,6 +3,7 @@ require '../../vendor/autoload.php';
use Illuminate\Encryption\Encrypter; use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
$required_extensions = ['openssl', 'gd', 'mysql', 'PDO', 'mbstring', 'tokenizer', 'bcmath', 'xml', 'curl', 'zip', 'intl']; $required_extensions = ['openssl', 'gd', 'mysql', 'PDO', 'mbstring', 'tokenizer', 'bcmath', 'xml', 'curl', 'zip', 'intl'];
@ -198,22 +199,30 @@ function run_console(string $command, array $descriptors = null, string $cwd = n
} }
/** /**
* Log to installer.log in the install folder * Log to the default laravel.log file
* @param string $log_msg the message to log * @param string $message The message to log
* @return void No output. * @param string $level The log level to use (info, warning, error, critical)
* @return void Returns nothing.
*/ */
function wh_log(string $log_msg) function wh_log(string $message, $level = 'info')
{ {
$log_filename = 'logs'; switch ($level) {
if (!file_exists($log_filename)) { case 'info':
// create directory/folder uploads. Log::info($message);
mkdir($log_filename, 0777, true); break;
case 'warning':
Log::warning($message);
break;
case 'error':
Log::error($message);
break;
case 'critical':
Log::critical($message);
break;
} }
$log_file_data = $log_filename . '/installer.log';
// if you don't add `FILE_APPEND`, the file will be erased each time you add a log
file_put_contents($log_file_data, '[' . date('h:i:s') . '] ' . $log_msg . "\n", FILE_APPEND);
} }
/** /**
* Generate a random string * Generate a random string
* @param int $length The length of the random string * @param int $length The length of the random string