{$context}."); return; } try { $db->exec($sql); } catch (PDOException $e) { logLine($logs, "❌ Error running {$context}: " . 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."; ?> Create System Tables
" . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . ""); renderPage($logs, false); exit(1); } $applicationSettings = @simplexml_load_file(INITIALIZATION_FILE_NAME); if (!$applicationSettings) { logLine($logs, "❌ Unable to load Application Settings from " . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . "."); 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.
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 " . htmlspecialchars(__DIR__, ENT_QUOTES) . "."); renderPage($logs, true); exit(0); } // -------------------- // Process Each File // -------------------- foreach ($tableFiles as $tableFile) { $tableName = pathinfo($tableFile, PATHINFO_FILENAME); logLine($logs, "Processing " . htmlspecialchars($tableName, ENT_QUOTES) . "..."); // Drop view + table try { $db->exec("DROP VIEW IF EXISTS `view_{$tableName}`"); } catch (PDOException $e) { logLine($logs, "⚠️ Error dropping view view_{$tableName}: " . htmlspecialchars($e->getMessage(), ENT_QUOTES)); } try { $db->exec("DROP TABLE IF EXISTS `{$tableName}`"); } catch (PDOException $e) { logLine($logs, "⚠️ Error dropping table {$tableName}: " . 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 " . htmlspecialchars($tableFile, ENT_QUOTES) . ". Skipping."); continue; } } catch (Throwable $e) { logLine($logs, "❌ Exception including " . htmlspecialchars($tableFile, ENT_QUOTES) . ": " . 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...', '

Find by Name, Email, or Role

', 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...', '

Find by Name or Description

', 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...', '

Find by Name, Email, or Role

', 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...', '

Find by Name, Title or Placeholder

', 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...', '

Find by Name, Email, or Role

', 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...', '

Find by Name

', 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...', '

Search for dropdown types

', 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);