wavelog/install/includes/core/database_class.php

122 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2024-07-21 17:48:02 +02:00
class Database {
// Function to create the tables and fill them with the default data
2024-07-21 17:48:02 +02:00
function create_tables($data) {
// Connect to the database
2024-01-29 17:05:30 +01:00
$mysqli = new mysqli($data['db_hostname'], $data['db_username'], $data['db_password'], $data['db_name']);
// Check for errors
if (mysqli_connect_errno()) {
log_message('error', 'Database connection error: ' . mysqli_connect_error());
return false;
}
// Open the default SQL file
if (!$query = file_get_contents('assets/install.sql')) {
log_message('error', 'Failed to read install.sql file.');
return false;
}
2024-01-29 17:05:30 +01:00
$newpw = password_hash($data['password'], PASSWORD_DEFAULT);
2024-11-16 15:22:59 +01:00
$newquery = str_replace("%%FIRSTUSER_NAME%%", str_replace("'", "\\'", $data['username']), $query);
2024-01-29 17:05:30 +01:00
$newquery = str_replace("%%FIRSTUSER_PASS%%", $newpw, $newquery);
$newquery = str_replace("%%FIRSTUSER_MAIL%%", $data['user_email'], $newquery);
2024-07-27 15:09:39 +02:00
$newquery = str_replace("%%FIRSTUSER_CALL%%", strtoupper($data['callsign']), $newquery);
$newquery = str_replace("%%FIRSTUSER_LOCATOR%%", strtoupper($data['userlocator']), $newquery);
2024-11-16 15:22:59 +01:00
$newquery = str_replace("%%FIRSTUSER_FIRSTNAME%%", str_replace("'", "\\'", $data['firstname']), $newquery);
$newquery = str_replace("%%FIRSTUSER_LASTNAME%%", str_replace("'", "\\'", $data['lastname']), $newquery);
2024-01-29 17:05:30 +01:00
$newquery = str_replace("%%FIRSTUSER_TIMEZONE%%", $data['timezone'], $newquery);
$newquery = str_replace("%%FIRSTUSER_DXCC%%", $data['dxcc'], $newquery);
2024-11-16 15:22:59 +01:00
$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...');
2024-01-28 18:45:10 +00:00
2024-01-24 16:58:52 +00:00
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
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());
$mysqli->store_result();
2024-01-24 16:58:52 +00:00
// Close the connection
$mysqli->close();
2024-01-24 16:58:52 +00:00
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;
}
}
2024-01-29 17:05:30 +01:00
2024-07-21 17:48:02 +02:00
function database_check($data) {
2024-01-29 17:05:30 +01:00
try {
$timeout = 5; /* five seconds for timeout */
$link = mysqli_init();
2024-07-24 16:48:26 +02:00
2024-01-29 17:05:30 +01:00
$link->options(MYSQLI_OPT_CONNECT_TIMEOUT, $timeout);
2024-07-24 16:48:26 +02:00
$link->real_connect($data['db_hostname'], $data['db_username'], $data['db_password']);
2024-01-29 17:05:30 +01:00
if ($link->connect_error) {
2024-12-04 22:58:09 +01:00
throw new Exception("Connection Error: " . $link->connect_error);
2024-01-29 17:05:30 +01:00
}
2024-07-24 16:48:26 +02:00
2024-12-29 16:27:44 +01:00
if (!$link->query("CREATE DATABASE IF NOT EXISTS `" . $data['db_name'] . "`")) {
2024-12-04 22:58:09 +01:00
throw new Exception("Unable to create database: " . $link->error);
2024-07-24 16:48:26 +02:00
}
if (!$link->select_db($data['db_name'])) {
2024-12-04 22:58:09 +01:00
throw new Exception("Unable to select database: " . $link->error);
2024-07-24 16:48:26 +02:00
}
2024-07-24 14:20:52 +02:00
$result = $link->query("SHOW TABLES");
if ($result->num_rows > 0) {
2024-12-04 22:58:09 +01:00
throw new Exception("Database is not empty.");
2024-07-24 14:20:52 +02:00
}
2024-07-24 16:48:26 +02:00
$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) {
2024-12-04 22:58:09 +01:00
throw new Exception("Unable to get Database version: " . $link->error);
}
if (!isset($version_query['version'])) {
2024-12-04 22:58:09 +01:00
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);
}
2024-01-29 17:05:30 +01:00
$link->close();
2024-07-24 16:48:26 +02:00
2024-01-29 17:05:30 +01:00
return $mysql_version;
2024-01-29 17:05:30 +01:00
} catch (Exception $e) {
log_message('error', 'Database Check Error: ' . $e->getMessage());
2024-07-24 15:29:35 +02:00
return 'Error: ' . $e->getMessage();
2024-01-29 17:05:30 +01:00
}
}
}