228 lines
8.6 KiB
PHP
228 lines
8.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Create System Tables
|
|
* - Loads DB settings from ../xml/settings.xml
|
|
* - Scans current dir for table definition files (*.php), excludes bootstrap scripts
|
|
* - Drops related view + table, then runs $SQL from each included file
|
|
* - Seeds data for certain tables (popovers, users)
|
|
*
|
|
* NOTE: Your table definition files should set: $SQL = "CREATE TABLE ...";
|
|
*/
|
|
define('INITIALIZATION_FILE_NAME', __DIR__ . '/../../xml/settings.xml');
|
|
|
|
$logs = [];
|
|
|
|
function logLine(array &$logs, string $msg): void {
|
|
$logs[] = $msg;
|
|
}
|
|
|
|
function runSQL(PDO $db, ?string $sql, string $context, array &$logs): void {
|
|
if ($sql === null || trim($sql) === '') {
|
|
logLine($logs, "⚠️ Skipping empty SQL for <strong>{$context}</strong>.");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$db->exec($sql);
|
|
} catch (PDOException $e) {
|
|
logLine($logs, "❌ Error running <strong>{$context}</strong>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
|
renderPage($logs, false);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
function renderPage(array $logs, bool $ok): void {
|
|
$status = $ok ? "✅ System Tables Created." : "❌ Script stopped due to an error.";
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Create System Tables</title>
|
|
<style>
|
|
body {
|
|
background-color:#333;
|
|
color:#fff;
|
|
font-family:sans-serif;
|
|
padding:1em;
|
|
}
|
|
.log {
|
|
margin-bottom: .5em;
|
|
}
|
|
.done {
|
|
margin-top: 1em;
|
|
font-weight: bold;
|
|
}
|
|
code {
|
|
background: rgba(255,255,255,.08);
|
|
padding: .15em .35em;
|
|
border-radius: 6px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<?php foreach ($logs as $line): ?>
|
|
<div class="log"><?= $line ?></div>
|
|
<?php endforeach; ?>
|
|
<div class="done"><?= $status ?></div>
|
|
</body>
|
|
</html>
|
|
<?php
|
|
}
|
|
|
|
// --------------------
|
|
// Load App Settings
|
|
// --------------------
|
|
if (!file_exists(INITIALIZATION_FILE_NAME)) {
|
|
logLine($logs, "❌ Initialization file not found: <code>" . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . "</code>");
|
|
renderPage($logs, false);
|
|
exit(1);
|
|
}
|
|
|
|
$applicationSettings = @simplexml_load_file(INITIALIZATION_FILE_NAME);
|
|
if (!$applicationSettings) {
|
|
logLine($logs, "❌ Unable to load Application Settings from <code>" . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . "</code>.");
|
|
renderPage($logs, false);
|
|
exit(1);
|
|
}
|
|
|
|
// --------------------
|
|
// Database Connection
|
|
// --------------------
|
|
$host = (string) ($applicationSettings->DatabaseServer ?? '');
|
|
$database = (string) ($applicationSettings->DatabaseName ?? '');
|
|
$user = (string) ($applicationSettings->DatabaseUser ?? '');
|
|
$pass = (string) ($applicationSettings->DatabasePassword ?? '');
|
|
|
|
if ($host === '' || $database === '' || $user === '') {
|
|
logLine($logs, "❌ Missing DB settings in settings.xml (DatabaseServer/DatabaseName/DatabaseUser).");
|
|
renderPage($logs, false);
|
|
exit(1);
|
|
}
|
|
|
|
try {
|
|
$dsn = "mysql:host={$host};dbname={$database};charset=utf8mb4";
|
|
$db = new PDO($dsn, $user, $pass, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]);
|
|
|
|
$db->exec("SET FOREIGN_KEY_CHECKS = 0");
|
|
} catch (Throwable $e) {
|
|
logLine($logs, "❌ Unable to connect to the database.<br/>Error: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
|
renderPage($logs, false);
|
|
exit(1);
|
|
}
|
|
|
|
// --------------------
|
|
// Find Table Files
|
|
// --------------------
|
|
$exclude = [
|
|
'.', '..',
|
|
'_createSystemTable.php',
|
|
'_createTable.php',
|
|
];
|
|
|
|
$dirFiles = scandir(__DIR__) ?: [];
|
|
$tableFiles = array_values(array_filter($dirFiles, function ($f) use ($exclude) {
|
|
if (in_array($f, $exclude, true))
|
|
return false;
|
|
if (pathinfo($f, PATHINFO_EXTENSION) !== 'php')
|
|
return false;
|
|
return is_file(__DIR__ . '/' . $f);
|
|
}));
|
|
|
|
if (!$tableFiles) {
|
|
logLine($logs, "⚠️ No table definition files found in <code>" . htmlspecialchars(__DIR__, ENT_QUOTES) . "</code>.");
|
|
renderPage($logs, true);
|
|
exit(0);
|
|
}
|
|
|
|
// --------------------
|
|
// Process Each File
|
|
// --------------------
|
|
foreach ($tableFiles as $tableFile) {
|
|
|
|
$tableName = pathinfo($tableFile, PATHINFO_FILENAME);
|
|
logLine($logs, "Processing <strong>" . htmlspecialchars($tableName, ENT_QUOTES) . "</strong>...");
|
|
|
|
// Drop view + table
|
|
try {
|
|
$db->exec("DROP VIEW IF EXISTS `view_{$tableName}`");
|
|
} catch (PDOException $e) {
|
|
logLine($logs, "⚠️ Error dropping view <code>view_{$tableName}</code>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
|
}
|
|
|
|
try {
|
|
$db->exec("DROP TABLE IF EXISTS `{$tableName}`");
|
|
} catch (PDOException $e) {
|
|
logLine($logs, "⚠️ Error dropping table <code>{$tableName}</code>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
|
}
|
|
|
|
// Include file (expects it to set $SQL)
|
|
$SQL = null;
|
|
$fullPath = __DIR__ . '/' . $tableFile;
|
|
|
|
try {
|
|
$result = include $fullPath;
|
|
if ($result === false) {
|
|
logLine($logs, "❌ Failed to include <code>" . htmlspecialchars($tableFile, ENT_QUOTES) . "</code>. Skipping.");
|
|
continue;
|
|
}
|
|
} catch (Throwable $e) {
|
|
logLine($logs, "❌ Exception including <code>" . htmlspecialchars($tableFile, ENT_QUOTES) . "</code>: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
|
renderPage($logs, false);
|
|
exit(1);
|
|
}
|
|
|
|
runSQL($db, $SQL, $tableName, $logs);
|
|
|
|
// --------------------
|
|
// Seeds / Extras
|
|
// --------------------
|
|
if ($tableFile === 'popovers.php') {
|
|
|
|
$sqlQueries = [
|
|
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
|
VALUES ('find_user', 'Find User', 'Find User...', '<p>Find by Name, Email, or Role</p>', CURDATE(), 'Init Script')",
|
|
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
|
VALUES ('find_report', 'Find Report', 'Find Report...', '<p>Find by Name or Description</p>', CURDATE(), 'Init Script')",
|
|
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
|
VALUES ('select_report', 'Select Report', 'Find Report...', '<p>Find by Name, Email, or Role</p>', CURDATE(), 'Init Script')",
|
|
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
|
VALUES ('find_popover', 'Find Popover', 'Find Popover...', '<p>Find by Name, Title or Placeholder</p>', CURDATE(), 'Init Script')",
|
|
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
|
VALUES ('find_journal', 'Find Journal', 'Find Journal...', '<p>Find by Name, Email, or Role</p>', CURDATE(), 'Init Script')",
|
|
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
|
VALUES ('find_status', 'Find Status', 'Find Status...', '<p>Find by Name</p>', CURDATE(), 'Init Script')",
|
|
"INSERT INTO popovers (popover_name, popover_title, popover_placeholder, popover_text, popover_created, popover_creator)
|
|
VALUES ('find_dropdowntype', 'Find Dropdown Types', 'Find Dropdown Types...', '<p>Search for dropdown types</p>', CURDATE(), 'Init Script')",
|
|
];
|
|
|
|
foreach ($sqlQueries as $sql) {
|
|
runSQL($db, $sql, "popovers data", $logs);
|
|
}
|
|
}
|
|
|
|
if ($tableFile === 'users.php') {
|
|
|
|
$adminPasswordHash = password_hash("349Jamot@", PASSWORD_DEFAULT);
|
|
|
|
$sql = "INSERT INTO users (user_id, user_name, user_email, user_password, user_role, user_change_password, user_active, user_creator, user_created)
|
|
VALUES ('jric11', 'James Richie', 'james.richie@onesourceits.com', " . $db->quote($adminPasswordHash) . ", 'Administrator', 0, 1, 'Init Script', CURDATE())";
|
|
|
|
runSQL($db, $sql, "initial admin user", $logs);
|
|
}
|
|
}
|
|
|
|
try {
|
|
$db->exec("SET FOREIGN_KEY_CHECKS = 1");
|
|
} catch (PDOException $e) {
|
|
logLine($logs, "⚠️ Could not re-enable FOREIGN_KEY_CHECKS: " . htmlspecialchars($e->getMessage(), ENT_QUOTES));
|
|
}
|
|
|
|
renderPage($logs, true);
|