First Commit
This commit is contained in:
@@ -54,8 +54,7 @@
|
||||
$full_path = $const_before . $const_value;
|
||||
|
||||
if (!is_dir($full_path) && !is_file($full_path)) {
|
||||
debugStatusMes("The folder/file declared into '$const_name' constant is missing or not accessible. Please check it.", "High");
|
||||
die;
|
||||
@mkdir($full_path, 0777, true);
|
||||
}
|
||||
|
||||
if (!defined($const_name)) {
|
||||
|
||||
+97
-14
@@ -1,21 +1,104 @@
|
||||
<?php
|
||||
// Exceptions PHP-MSQLI
|
||||
// Exceptions PHP-MYSQLI
|
||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||
|
||||
try {
|
||||
// Create Connection
|
||||
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
|
||||
|
||||
// Connections Successfully
|
||||
if(DB_DEBUG == "yes"){
|
||||
echo "Connected successfully";
|
||||
// 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;
|
||||
}
|
||||
} catch (mysqli_sql_exception $e) {
|
||||
// Access Deny
|
||||
include(PAGES_FOLD . "db-error.php");
|
||||
debugStatusMes("Database connection failed.", "High");
|
||||
|
||||
}
|
||||
|
||||
die();
|
||||
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();
|
||||
}
|
||||
?>
|
||||
@@ -22,7 +22,7 @@ function get_current_page() {
|
||||
$page = $_GET["page"] ?? "home";
|
||||
|
||||
$page = strtolower(trim($page));
|
||||
$page = preg_replace("/[^a-z0-9\-]/", "", $page);
|
||||
$page = preg_replace("/[^a-z0-9\-_]/", "", $page);
|
||||
|
||||
if ($page === "") {
|
||||
$page = "home";
|
||||
|
||||
+7
-5
@@ -5,12 +5,14 @@
|
||||
switch($env){
|
||||
/*------ DON'T TOUCH THIS PART ------ */
|
||||
case "generic":
|
||||
define('WEB_ROOT', $_SERVER['DOCUMENT_ROOT']);
|
||||
define('DOMAIN', $_SERVER['HTTP_HOST']);
|
||||
$doc_root = !empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : dirname(__DIR__);
|
||||
define('WEB_ROOT', $doc_root);
|
||||
$host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
|
||||
define('DOMAIN', $host);
|
||||
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
|
||||
define('FULL_DOMAIN', $protocol . $_SERVER['HTTP_HOST']);
|
||||
define("ROOT_URL", "http://" . DOMAIN );
|
||||
define("ROOT_DIR", WEB_ROOT . "/");
|
||||
define('FULL_DOMAIN', $protocol . DOMAIN);
|
||||
define("ROOT_URL", $protocol . DOMAIN );
|
||||
define("ROOT_DIR", rtrim(WEB_ROOT, '/') . "/");
|
||||
break;
|
||||
/* ----------------------------------- */
|
||||
default:
|
||||
|
||||
+6
-6
@@ -1,15 +1,15 @@
|
||||
<?php
|
||||
// -- GENERAL SETTING --
|
||||
$debug="on";
|
||||
$php_debug="on";
|
||||
$debug="off";
|
||||
$php_debug="off";
|
||||
|
||||
// --- DB SETTINGS ---
|
||||
$db_needed="no";
|
||||
$db_needed="yes";
|
||||
$db_debug="yes";
|
||||
$db_host="localhost";
|
||||
$db_name="database-name";
|
||||
$db_user="database-user";
|
||||
$db_pass="database-user-password";
|
||||
$db_name="blocknotes_db";
|
||||
$db_user="simone";
|
||||
$db_pass="Angela";
|
||||
|
||||
//-------------------------------------------------------------------------------------
|
||||
// --- DON'T CHANGE ALL THE CODE BELOW!!! ---
|
||||
|
||||
Reference in New Issue
Block a user