Pārlūkot izejas kodu

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

Jens 2 gadi atpakaļ
vecāks
revīzija
f63d8080a3
2 mainītis faili ar 30 papildinājumiem un 21 dzēšanām
  1. 10 10
      public/install/forms.php
  2. 20 11
      public/install/functions.php

+ 10 - 10
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');
     }
 }

+ 20 - 11
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