From 2d855f38db3bf00f7d38f895f8f0b8b0b052c85c Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Mon, 29 Jan 2024 17:05:30 +0100 Subject: [PATCH] db connection test --- install/includes/database_class.php | 60 ++++++--- install/index.php | 194 +++++++++++++++++++--------- 2 files changed, 174 insertions(+), 80 deletions(-) diff --git a/install/includes/database_class.php b/install/includes/database_class.php index 94e2b28dc..84b81a913 100644 --- a/install/includes/database_class.php +++ b/install/includes/database_class.php @@ -1,19 +1,20 @@ query("CREATE DATABASE IF NOT EXISTS ".$data['db_name']); + $mysqli->query("CREATE DATABASE IF NOT EXISTS " . $data['db_name']); // Close the connection $mysqli->close(); @@ -25,24 +26,24 @@ class Database { function create_tables($data) { // Connect to the database - $mysqli = new mysqli($data['db_hostname'],$data['db_username'],$data['db_password'],$data['db_name']); + $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()) return false; // Open the default SQL file $query = file_get_contents('assets/install.sql'); - $newpw=password_hash($data['password'], PASSWORD_DEFAULT); - $newquery = str_replace("%%FIRSTUSER_NAME%%",$data['username'],$query); - $newquery = str_replace("%%FIRSTUSER_PASS%%",$newpw,$newquery); - $newquery = str_replace("%%FIRSTUSER_MAIL%%",$data['user_email'],$newquery); - $newquery = str_replace("%%FIRSTUSER_CALL%%",$data['callsign'],$newquery); - $newquery = str_replace("%%FIRSTUSER_LOCATOR%%",$data['userlocator'],$newquery); - $newquery = str_replace("%%FIRSTUSER_FIRSTNAME%%",$data['firstname'],$newquery); - $newquery = str_replace("%%FIRSTUSER_LASTNAME%%",$data['lastname'],$newquery); - $newquery = str_replace("%%FIRSTUSER_TIMEZONE%%",$data['timezone'],$newquery); + $newpw = password_hash($data['password'], PASSWORD_DEFAULT); + $newquery = str_replace("%%FIRSTUSER_NAME%%", $data['username'], $query); + $newquery = str_replace("%%FIRSTUSER_PASS%%", $newpw, $newquery); + $newquery = str_replace("%%FIRSTUSER_MAIL%%", $data['user_email'], $newquery); + $newquery = str_replace("%%FIRSTUSER_CALL%%", $data['callsign'], $newquery); + $newquery = str_replace("%%FIRSTUSER_LOCATOR%%", $data['userlocator'], $newquery); + $newquery = str_replace("%%FIRSTUSER_FIRSTNAME%%", $data['firstname'], $newquery); + $newquery = str_replace("%%FIRSTUSER_LASTNAME%%", $data['lastname'], $newquery); + $newquery = str_replace("%%FIRSTUSER_TIMEZONE%%", $data['timezone'], $newquery); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); @@ -51,7 +52,9 @@ class Database { $mysqli->multi_query($newquery); // MultiQuery is NON-Blocking,so wait until everything is done - do { null; } while($mysqli->next_result()); + do { + null; + } while ($mysqli->next_result()); $result = $mysqli->store_result(); @@ -60,5 +63,28 @@ class Database { return true; } + + function database_check($data) + { + try { + $timeout = 5; /* five seconds for timeout */ + $link = mysqli_init(); + + $link->options(MYSQLI_OPT_CONNECT_TIMEOUT, $timeout); + + $link->real_connect($data['db_hostname'], $data['db_username'], $data['db_password'], $data['db_name']); + + if ($link->connect_error) { + throw new Exception('Connection Error: ' . $link->connect_error); + } + + $mysql_version = $link->server_info; + + $link->close(); + + return $mysql_version; + } catch (Exception $e) { + return 'Error: ' . $e->getMessage(); + } + } } -?> diff --git a/install/index.php b/install/index.php index acd760ce8..fca947088 100644 --- a/install/index.php +++ b/install/index.php @@ -83,43 +83,50 @@ if ($_POST) { $core = new Core(); $database = new Database(); - // Validate the post data - if ($core->validate_post($_POST) == true) { + if ($_POST['database_check'] == true) { - // First create the database, then create tables, then write config file - if ($database->create_database($_POST) == false) { - $message = $core->show_message('error', "The database could not be created, please verify your settings."); - } elseif ($database->create_tables($_POST) == false) { - $message = $core->show_message('error', "The database tables could not be created, please verify your settings."); - } elseif ($core->write_config($_POST) == false) { - $message = $core->show_message('error', "The database configuration file could not be written, please chmod /application/config/database.php file to 777"); - } - - if ($core->write_configfile($_POST) == false) { - $message = $core->show_message('error', "The config configuration file could not be written, please chmod /application/config/config.php file to 777"); - } - - // If no errors, redirect to registration page - if (!isset($message)) { - sleep(1); - $ch = curl_init(); - $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http"; - list($realHost,) = explode(':', $_SERVER['HTTP_HOST']); - $wavelog_url = $protocol . "://" . $realHost . ":" . $_SERVER['SERVER_PORT']; - curl_setopt($ch, CURLOPT_URL, $wavelog_url); - curl_setopt($ch, CURLOPT_VERBOSE, 0); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $result = curl_exec($ch); - curl_setopt($ch, CURLOPT_URL, $wavelog_url . "/index.php/update/dxcc"); - $result = curl_exec($ch); - delDir(getcwd()); - header('Location: ' . $protocol . "://" . $_SERVER['HTTP_HOST'] . $_POST['directory']); - echo "

Install successful

"; - echo "

Please delete the install folder"; - exit; - } + $result = $database->database_check($_POST); + echo $result; + exit; } else { - $message = $core->show_message('error', 'Not all fields have been filled in correctly. The host, username, password, and database name are required.'); + // Validate the post data + if ($core->validate_post($_POST) == true) { + + // First create the database, then create tables, then write config file + if ($database->create_database($_POST) == false) { + $message = $core->show_message('error', "The database could not be created, please verify your settings."); + } elseif ($database->create_tables($_POST) == false) { + $message = $core->show_message('error', "The database tables could not be created, please verify your settings."); + } elseif ($core->write_config($_POST) == false) { + $message = $core->show_message('error', "The database configuration file could not be written, please chmod /application/config/database.php file to 777"); + } + + if ($core->write_configfile($_POST) == false) { + $message = $core->show_message('error', "The config configuration file could not be written, please chmod /application/config/config.php file to 777"); + } + + // If no errors, redirect to registration page + if (!isset($message)) { + sleep(1); + $ch = curl_init(); + $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http"; + list($realHost,) = explode(':', $_SERVER['HTTP_HOST']); + $wavelog_url = $protocol . "://" . $realHost . ":" . $_SERVER['SERVER_PORT']; + curl_setopt($ch, CURLOPT_URL, $wavelog_url); + curl_setopt($ch, CURLOPT_VERBOSE, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($ch); + curl_setopt($ch, CURLOPT_URL, $wavelog_url . "/index.php/update/dxcc"); + $result = curl_exec($ch); + delDir(getcwd()); + header('Location: ' . $protocol . "://" . $_SERVER['HTTP_HOST'] . $_POST['directory']); + echo "

Install successful

"; + echo "

Please delete the install folder"; + exit; + } + } else { + $message = $core->show_message('error', 'Not all fields have been filled in correctly. The host, username, password, and database name are required.'); + } } } global $wavelog_url; @@ -144,7 +151,7 @@ global $wavelog_url; ' . $message . '

'; + echo '

' . $message . '

'; // TODO Integrate Message into the design, Dialog??? } ?> @@ -359,30 +366,22 @@ global $wavelog_url;

Configure some basic parameters for your wavelog instance. You can change them later in 'application/config/config.php'

- +
" class="form-control" name="directory" aria-describedby="main-url" />
- + " class="form-control" name="websiteurl" />
- +
- + - Usually 'localhost'. Optional with '...:[port]'. Default Port: 3306 +
+
+
+ + +
+
+
+
+ + +
+
- + - Username of the DB User which has full access to the database.
- + - Password of the DB User.
-
- - - Name of the Database. +
+ +
@@ -625,8 +630,10 @@ global $wavelog_url; tabs.show(); let firstTabId = 'welcome-tab'; + let secondTabId = 'precheck-tab'; + let thirdTabId = 'configuration-tab'; + let fourthTabId = 'database-tab'; let lastTabId = 'finish-tab'; - let preCheckTabId = 'precheck-tab'; function nextTab() { const activeTab = $('.nav-link.active'); @@ -656,10 +663,12 @@ global $wavelog_url; if (prevTab.attr('id') !== firstTabId) { $('#ContinueButton').css('display', 'block'); + $('#ContinueButton').prop('disabled', false); $('#BackButton').css('display', 'block'); } else { $('#BackButton').css('display', 'none'); } + clear_db_testresult(); } $('#ContinueButton').on('click', nextTab); @@ -669,13 +678,14 @@ global $wavelog_url; // Check if the active tab is the precheck-tab and disable the ContinueButton if not all Checks passed $(document).on('shown.bs.tab', function(e) { const activeTabId = e.target.id; - if (activeTabId === 'precheck-tab') { + if (activeTabId === secondTabId) { $('#ContinueButton').prop('disabled', true); } else { $('#ContinueButton').prop('disabled', false); } }); + }); // [PWD] button show/hide // @@ -689,6 +699,64 @@ global $wavelog_url; } } $('.user_edit .btn-pwd-showhide').off('click').on('click', btn_pwd_showhide); + + function db_connection_test() { + var db_hostname = $('#db_hostname').val(); + var db_username = $('#db_username').val(); + var db_password = $('#db_password').val(); + var db_name = $('#db_name').val(); + + if (db_hostname === '' || db_username === '' || db_password === '' || db_name === '') { + $('#db_connection_testresult').addClass('alert-danger'); + $('#db_connection_testresult').html('Error: All fields are required.'); + $('#ContinueButton').prop('disabled', true); + return; + } + + var originalButtonText = $('#db_connection_test_button').html(); + $('#db_connection_test_button').html(' Connecting...').prop('disabled', true); + + clear_db_testresult(); + + $.ajax({ + type: 'POST', + url: 'index.php', + data: { + db_hostname: db_hostname, + db_username: db_username, + db_password: db_password, + db_name: db_name, + database_check: true + }, + success: function(response) { + $('#db_connection_testresult').html(response); + if (response.indexOf('Error') !== -1) { + $('#ContinueButton').prop('disabled', true); + $('#db_connection_testresult').addClass('alert-danger'); + $('#db_connection_test_button').html(originalButtonText).prop('disabled', false); + } else { + $('#ContinueButton').prop('disabled', false); + $('#db_connection_testresult').addClass('alert-success'); + $('#db_connection_test_button').html(originalButtonText).prop('disabled', false); + } + }, + error: function(error) { + $('#db_connection_testresult').html('Error: ' + error.statusText); + if ($('#db_connection_testresult').text().indexOf('Error') !== -1) { + $('#ContinueButton').prop('disabled', true); + $('#db_connection_testresult').addClass('alert-danger'); + } + } + }); + } + + function clear_db_testresult() { + $('#db_connection_testresult').html(''); + $('#db_connection_testresult').removeClass('alert-danger'); + $('#db_connection_testresult').removeClass('alert-success'); + } + +