From f63d8080a3b714908d100b2cdfb9647a033324de Mon Sep 17 00:00:00 2001 From: Jens Date: Thu, 30 Mar 2023 13:00:17 +0200 Subject: [PATCH] (Refactor) Use laravel's logging system, specified a few log levels and refactor the function calls --- public/install/forms.php | 20 ++++++++++---------- public/install/functions.php | 31 ++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/public/install/forms.php b/public/install/forms.php index 1660e95f..4abda8ec 100644 --- a/public/install/forms.php +++ b/public/install/forms.php @@ -27,7 +27,7 @@ if (isset($_POST['checkDB'])) { $db = new mysqli($_POST['databasehost'], $_POST['databaseuser'], $_POST['databaseuserpass'], $_POST['database'], $_POST['databaseport']); 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'); exit(); } @@ -71,7 +71,7 @@ if (isset($_POST['feedDB'])) { $logs .= run_console('php artisan migrate --seed --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) { 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')); 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: '); exit(); } @@ -171,11 +171,11 @@ if (isset($_POST['checkPtero'])) { 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!'); - wh_log('API CALL ERROR: ' . $result['errors'][0]['code']); + wh_log('API CALL ERROR: ' . $result['errors'][0]['code'], 'error'); exit(); } 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!'); - wh_log('API CALL ERROR: ' . $callresult['errors'][0]['code']); + wh_log('API CALL ERROR: ' . $callresult['errors'][0]['code'], 'error'); exit(); } else { $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')); 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'); exit(); } @@ -195,7 +195,7 @@ if (isset($_POST['checkPtero'])) { if ($db->query($query1) && $db->query($query2) && $db->query($query3)) { header('LOCATION: index.php?step=6'); } 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!'); } } @@ -204,7 +204,7 @@ if (isset($_POST['checkPtero'])) { if (isset($_POST['createUser'])) { $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); + wh_log($db->connect_error, 'error'); header('LOCATION: index.php?step=6&message=Could not connect to the Database'); 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')"; 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'); } 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'); } } diff --git a/public/install/functions.php b/public/install/functions.php index 629e9141..c231d2be 100644 --- a/public/install/functions.php +++ b/public/install/functions.php @@ -3,6 +3,7 @@ require '../../vendor/autoload.php'; use Illuminate\Encryption\Encrypter; use Illuminate\Support\Str; +use Illuminate\Support\Facades\Log; $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 - * @param string $log_msg the message to log - * @return void No output. + * Log to the default laravel.log file + * @param string $message The message to log + * @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'; - if (!file_exists($log_filename)) { - // create directory/folder uploads. - mkdir($log_filename, 0777, true); + switch ($level) { + case 'info': + Log::info($message); + 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 * @param int $length The length of the random string