Files
sld-php-template/config/db.php
T
2026-08-19 17:26:06 +01:00

104 lines
4.3 KiB
PHP

<?php
// Exceptions PHP-MYSQLI
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Check if first_start.php installer file exists and database setup is needed
$first_start_path = ROOT_DIR . "first_start.php";
if (file_exists($first_start_path) && basename($_SERVER['SCRIPT_FILENAME'] ?? '') !== 'first_start.php') {
// Check connection first
try {
$test_conn = @new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($test_conn->connect_error) {
header("Location: first_start.php");
exit;
}
$test_conn->close();
} catch (Exception $ex) {
header("Location: first_start.php");
exit;
}
}
try {
// Create Connection to MySQL host first (in case DB doesn't exist yet)
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS);
// Create Database if not exists
$db_name_escaped = str_replace('`', '``', DB_NAME);
$conn->query("CREATE DATABASE IF NOT EXISTS `$db_name_escaped` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
$conn->select_db(DB_NAME);
// Ensure BLOCKNOTES_FOLD directory exists
if (!is_dir(BLOCKNOTES_FOLD)) {
@mkdir(BLOCKNOTES_FOLD, 0777, true);
}
// Auto-create users table if missing (with ENUM containing 'owner')
$sql_table = "CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role ENUM('owner', 'admin', 'user') NOT NULL DEFAULT 'user',
theme_pri INT DEFAULT 98,
theme_sec INT DEFAULT 207,
theme_thi INT DEFAULT 280,
language VARCHAR(5) NOT NULL DEFAULT 'it',
default_blocknote VARCHAR(100) NOT NULL DEFAULT '',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
$conn->query($sql_table);
// Auto-migrate role column ENUM if missing 'owner'
$conn->query("ALTER TABLE users MODIFY COLUMN role ENUM('owner', 'admin', 'user') NOT NULL DEFAULT 'user'");
// Auto-migrate columns if missing in existing table
$check_lang = $conn->query("SHOW COLUMNS FROM users LIKE 'language'");
if ($check_lang->num_rows === 0) {
$conn->query("ALTER TABLE users ADD COLUMN language VARCHAR(5) NOT NULL DEFAULT 'it'");
}
$check_def_bn = $conn->query("SHOW COLUMNS FROM users LIKE 'default_blocknote'");
if ($check_def_bn->num_rows === 0) {
$conn->query("ALTER TABLE users ADD COLUMN default_blocknote VARCHAR(100) NOT NULL DEFAULT ''");
}
// Auto-create site_settings table
$sql_settings = "CREATE TABLE IF NOT EXISTS site_settings (
setting_key VARCHAR(50) PRIMARY KEY,
setting_value TEXT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
$conn->query($sql_settings);
// Seed default site settings if missing
$conn->query("INSERT IGNORE INTO site_settings (setting_key, setting_value) VALUES ('site_title', 'Blocknotes')");
$conn->query("INSERT IGNORE INTO site_settings (setting_key, setting_value) VALUES ('site_favicon', 'assets/img/favicon.png')");
// Seed default owner account if table is empty or ensure admin user is owner if no owner exists
$res = $conn->query("SELECT COUNT(*) AS total FROM users");
$row = $res->fetch_assoc();
if ((int)$row['total'] === 0) {
$default_pass = password_hash("admin123", PASSWORD_DEFAULT);
$admin_user = "admin";
$stmt = $conn->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, 'owner')");
$stmt->bind_param("ss", $admin_user, $default_pass);
$stmt->execute();
$stmt->close();
} else {
// Check if an owner exists, if not, promote the first user to owner
$check_owner = $conn->query("SELECT COUNT(*) AS total_owners FROM users WHERE role = 'owner'");
$owner_row = $check_owner->fetch_assoc();
if ((int)$owner_row['total_owners'] === 0) {
$conn->query("UPDATE users SET role = 'owner' ORDER BY id ASC LIMIT 1");
}
}
} catch (mysqli_sql_exception $e) {
if (file_exists($first_start_path) && basename($_SERVER['SCRIPT_FILENAME'] ?? '') !== 'first_start.php') {
header("Location: first_start.php");
exit;
}
include(PAGES_FOLD . "db-error.php");
debugStatusMes("Database setup error: " . $e->getMessage(), "High");
die();
}
?>