2011-12-20 15:38:43 +00:00
< ? php
2024-07-21 17:48:02 +02:00
class Database {
2011-12-20 15:38:43 +00:00
// Function to create the tables and fill them with the default data
2024-07-21 17:48:02 +02:00
function create_tables ( $data ) {
2011-12-20 15:38:43 +00:00
// 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' ]);
2011-12-20 15:38:43 +00:00
// Check for errors
2024-12-03 10:52:07 +01:00
if ( mysqli_connect_errno ()) {
log_message ( 'error' , 'Database connection error: ' . mysqli_connect_error ());
2011-12-20 15:38:43 +00:00
return false ;
2024-12-03 10:52:07 +01:00
}
2011-12-20 15:38:43 +00:00
// Open the default SQL file
2024-12-03 10:52:07 +01:00
if ( ! $query = file_get_contents ( 'assets/install.sql' )) {
log_message ( 'error' , 'Failed to read install.sql file.' );
return false ;
}
2011-12-20 15:38:43 +00:00
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 );
2024-03-05 19:54:24 +01:00
$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 );
2024-07-09 09:22:55 +02:00
$newquery = str_replace ( " %%FIRSTUSER_USERLANGUAGE%% " , $data [ 'userlanguage' ], $newquery );
2024-12-03 10:52:07 +01:00
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 );
2024-12-03 10:52:07 +01:00
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 ());
2011-12-20 15:38:43 +00:00
2024-12-03 10:52:07 +01:00
$mysqli -> store_result ();
2024-01-24 16:58:52 +00:00
2024-12-03 10:52:07 +01:00
// Close the connection
$mysqli -> close ();
2024-01-24 16:58:52 +00:00
2024-12-03 10:52:07 +01:00
log_message ( 'info' , 'Database tables created successfully.' );
return true ;
2011-12-20 15:38:43 +00:00
2024-12-03 10:52:07 +01:00
} catch ( mysqli_sql_exception $e ) {
log_message ( 'error' , 'Database Error: ' . $e -> getMessage ());
if ( $mysqli -> ping ()) {
$mysqli -> close ();
}
return false ;
}
2011-12-20 15:38:43 +00:00
}
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
2024-12-03 12:17:26 +01: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 );
2024-12-03 12:17:26 +01:00
}
if ( ! isset ( $version_query [ 'version' ])) {
2024-12-04 22:58:09 +01:00
throw new Exception ( " Database version could not be retrieved. " );
2024-12-03 12:17:26 +01:00
}
$mysql_version = $version_query [ 'version' ];
2024-12-04 16:23:15 +01:00
// 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-12-04 16:23:15 +01:00
2024-01-29 17:05:30 +01:00
} catch ( Exception $e ) {
2024-12-03 10:52:07 +01:00
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
}
}
2011-12-20 15:38:43 +00:00
}