Merge pull request #1307 from HB9HIL/installer_better_debug

[Installer] better debugging
This commit is contained in:
Fabian Berg
2024-12-04 17:45:06 +01:00
committed by GitHub
6 changed files with 144 additions and 44 deletions

View File

@@ -8,22 +8,34 @@ class Core
{
// Counter variable
$counter = 0;
$errors = [];
// Validate the hostname
if (isset($data['db_hostname']) and !empty($data['db_hostname'])) {
$counter++;
} else {
$errors[] = "DB Hostname is missing.";
}
// Validate the username
if (isset($data['db_username']) and !empty($data['db_username'])) {
$counter++;
} else {
$errors[] = "DB Username is missing.";
}
// Validate the password
if (isset($data['db_password']) and !empty($data['db_password'])) {
// pass
$counter++;
} else {
$errors[] = "DB Password is missing.";
}
// Validate the database
if (isset($data['db_name']) and !empty($data['db_name'])) {
$counter++;
} else {
$errors[] = "DB Name is missing.";
}
if ($data['directory'] ?? '' != "") {
@@ -31,36 +43,46 @@ class Core
//pass folders real
$counter++;
} else {
echo "Directory " . $data['directory'] . " cannot be found";
exit;
$errors[] = "Directory " . $data['directory'] . " does not exist.";
}
} else {
// directory is not set so nothing to check here
$counter++;
}
// Validate First Name
if (isset($data['firstname']) && !empty($data['firstname'])) {
$counter++;
} else {
$errors[] = "First Name is missing.";
}
// Validate Last Name
if (isset($data['lastname']) && !empty($data['lastname'])) {
$counter++;
} else {
$errors[] = "Last Name is missing.";
}
// Validate Username
if (isset($data['username']) && !empty($data['username'])) {
$counter++;
} else {
$errors[] = "Username is missing.";
}
// Validate Callsign
if (isset($data['callsign']) && !empty($data['callsign'])) {
$counter++;
} else {
$errors[] = "Callsign is missing.";
}
// Validate Password
if (isset($data['password']) && !empty($data['password'])) {
$counter++;
} else {
$errors[] = "User Password is missing.";
}
// Validate Locator
@@ -72,12 +94,14 @@ class Core
$errors[] = "Invalid Maidenhead Locator format.";
}
} else {
$errors[] = "Locator is required.";
$errors[] = "Locator is missing.";
}
// Validate Confirm Password
if (isset($data['cnfm_password']) && !empty($data['cnfm_password'])) {
$counter++;
} else {
$errors[] = "Confirm Password is missing.";
}
// Validate Email Address
@@ -90,58 +114,81 @@ class Core
// Validate Timezone
if (isset($data['timezone']) && is_numeric($data['timezone'])) {
$counter++;
} else {
$errors[] = "Invalid Timezone.";
}
// Check if all the required fields have been entered
if ($counter == '13') {
if ($counter == '14') {
log_message('info', 'Data validation passed.');
return true;
} else {
log_message('error', 'Failed to validate POST data');
log_message('error', 'Data validation failed.');
foreach ($errors as $error) {
log_message('error', $error);
}
return false;
}
}
// Function to write the config file
// Function to write the database config file
function write_config($data) {
$template_path = 'config/database.php';
$output_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $data['directory'] . '/application/config/database.php';
if (isset($_ENV['CI_ENV'])) {
$output_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $data['directory'] . '/application/config/'.$_ENV['CI_ENV'].'/database.php';
log_message('info', 'CI_ENV is set to ' . $_ENV['CI_ENV'] . '. Using ' . $_ENV['CI_ENV'] . ' database.php config path.');
} else {
log_message('info', 'CI_ENV is not set. Using default database.php config path.');
}
if (!file_exists($template_path)) {
log_message('error', 'database.php template file not found.');
return false;
}
// Open the file
$database_file = file_get_contents($template_path);
if ($database_file === false) {
log_message('error', 'Failed to read database.php template file.');
return false;
}
log_message('info', 'database.php template file read successfully.');
// Sanitize DB Password from single quotes
$sanitized_db_pwd = preg_replace("/\\\\/i",'\\\\\\\\',$data['db_password']); // Escape the Escape char ( '\' becomes '\\' )
$sanitized_db_pwd = preg_replace("/\'/i",'\\\\\'',$sanitized_db_pwd); // Escape the ' ( ' becomes \' )
$sanitized_db_pwd = preg_replace("/\\\\/i",'\\\\\\\\',$data['db_password']); // Escape the Escape char ( '\' becomes '\\' )
$sanitized_db_pwd = preg_replace("/\'/i",'\\\\\'',$sanitized_db_pwd); // Escape the ' ( ' becomes \' )
$new = str_replace("%HOSTNAME%", $data['db_hostname'], $database_file);
$new = str_replace("%USERNAME%", $data['db_username'], $new);
$new = str_replace("%PASSWORD%", $sanitized_db_pwd, $new);
$new = str_replace("%DATABASE%", $data['db_name'], $new);
log_message('info', 'Database config file prepared successfully. Writing to file...');
// Write the new database.php file
$handle = fopen($output_path, 'w+');
// Chmod the file, in case the user forgot
@chmod($output_path, 0777);
if ($handle === false) {
log_message('error', 'Failed to open target path for writing the database.php file.');
return false;
}
// Verify file permissions
if (is_writable($output_path)) {
// Write the file
if (fwrite($handle, $new)) {
if(file_exists($output_path)) {
log_message('info', 'database.php file written successfully.');
return true;
} else {
log_message('error', 'database.php file not found after writing.');
return false;
}
} else {
return false;
}
} else {
log_message('error', 'database.php path is not writable.');
return false;
}
}
@@ -153,15 +200,23 @@ class Core
$output_path = '../application/config/config.php';
if (isset($_ENV['CI_ENV'])) {
$output_path = '../application/config/'.$_ENV['CI_ENV'].'/config.php';
log_message('info', 'CI_ENV is set to ' . $_ENV['CI_ENV'] . '. Using ' . $_ENV['CI_ENV'] . ' config.php config path.');
} else {
log_message('info', 'CI_ENV is not set. Using default config.php config path.');
}
// Open the file
$database_file = file_get_contents($template_path);
$config_file = file_get_contents($template_path);
if ($config_file === false) {
log_message('error', 'Failed to read config.php template file.');
return false;
}
log_message('info', 'config.php template file read successfully.');
// creating a unique encryption key
$encryptionkey = uniqid(bin2hex(random_bytes(8)), false);
$new = str_replace("%baselocator%", strtoupper($data['userlocator']), $database_file);
$new = str_replace("%baselocator%", strtoupper($data['userlocator']), $config_file);
$new = str_replace("%websiteurl%", $data['websiteurl'], $new);
$new = str_replace("%directory%", $data['directory'], $new);
$new = str_replace("%callbook%", $data['global_call_lookup'], $new);
@@ -190,24 +245,31 @@ class Core
$new = str_replace("%encryptionkey%", $encryptionkey, $new);
$new = str_replace("'%log_threshold%'", $data['log_threshold'], $new);
log_message('info', 'Config.php file prepared successfully. Writing to file...');
// Write the new config.php file
$handle = fopen($output_path, 'w+');
if ($handle === false) {
log_message('error', 'Failed to open target path for writing the config.php file.');
return false;
}
// Verify file permissions
if (is_writable($output_path)) {
// Write the file
if (fwrite($handle, $new)) {
if(file_exists($output_path)) {
log_message('info', 'config.php file written successfully.');
return true;
} else {
log_message('error', 'config.php file not found after writing.');
return false;
}
} else {
return false;
}
} else {
log_message('error', 'config.php path is not writable.');
return false;
}
}

View File

@@ -8,11 +8,16 @@ class Database {
$mysqli = new mysqli($data['db_hostname'], $data['db_username'], $data['db_password'], $data['db_name']);
// Check for errors
if (mysqli_connect_errno())
if (mysqli_connect_errno()) {
log_message('error', 'Database connection error: ' . mysqli_connect_error());
return false;
}
// Open the default SQL file
$query = file_get_contents('assets/install.sql');
if (!$query = file_get_contents('assets/install.sql')) {
log_message('error', 'Failed to read install.sql file.');
return false;
}
$newpw = password_hash($data['password'], PASSWORD_DEFAULT);
$newquery = str_replace("%%FIRSTUSER_NAME%%", str_replace("'", "\\'", $data['username']), $query);
@@ -26,24 +31,37 @@ class Database {
$newquery = str_replace("%%FIRSTUSER_DXCC%%", $data['dxcc'], $newquery);
$newquery = str_replace("%%FIRSTUSER_CITY%%", str_replace("'", "\\'", $data['city']), $newquery);
$newquery = str_replace("%%FIRSTUSER_USERLANGUAGE%%", $data['userlanguage'], $newquery);
log_message('info', 'SQL queries prepared successfully. Writing to database...');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Execute a multi query
$mysqli->multi_query($newquery);
try {
// Execute a multi query
$mysqli->multi_query($newquery);
// MultiQuery is NON-Blocking,so wait until everything is done
do {
null;
} while ($mysqli->next_result());
// MultiQuery is NON-Blocking,so wait until everything is done
do {
null;
} while ($mysqli->next_result());
$result = $mysqli->store_result();
$mysqli->store_result();
// Close the connection
$mysqli->close();
// Close the connection
$mysqli->close();
return true;
log_message('info', 'Database tables created successfully.');
return true;
} catch (mysqli_sql_exception $e) {
log_message('error', 'Database Error: ' . $e->getMessage());
if ($mysqli->ping()) {
$mysqli->close();
}
return false;
}
}
function database_check($data) {
@@ -63,7 +81,6 @@ class Database {
throw new Exception(__("Unable to create database: ") . $link->error);
}
// Wählen Sie die Datenbank aus
if (!$link->select_db($data['db_name'])) {
throw new Exception(__("Unable to select database: ") . $link->error);
}
@@ -74,12 +91,30 @@ class Database {
throw new Exception(__("Database is not empty."));
}
$mysql_version = $link->server_info;
$version_query = $link->query("SELECT VERSION() as version")->fetch_assoc(); // $link->server_info sometimes returns wrong version or additional (in this case unnecessary) information, e.g. 5.5.5-10.3.29-MariaDB-0+deb10u1
if (!$version_query) {
throw new Exception(__("Unable to get Database version: ") . $link->error);
}
if (!isset($version_query['version'])) {
throw new Exception(__("Database version could not be retrieved."));
}
$mysql_version = $version_query['version'];
// in case of a previous failed installation it can happen that still the migration lockfile is existent
// this would prevent the migration from running or at least would cause a unnecessary delay
// so we delete it here
$lockfile = sys_get_temp_dir() . '/.migration_running';
if (file_exists($lockfile)) {
log_message('info', 'Removing migration lockfile. Not expected to be present at this point.');
unlink($lockfile);
}
$link->close();
return $mysql_version;
} catch (Exception $e) {
log_message('error', 'Database Check Error: ' . $e->getMessage());
return 'Error: ' . $e->getMessage();
}
}