Files
decdata/tables/_createTable.php
T
2026-06-30 21:34:42 -04:00

101 lines
2.3 KiB
PHP

<style>
body {
background-color: #333333;
color: white;
font-family: sans-serif;
padding: 1em;
}
</style>
<?php
define("INITIALIZATION_FILE_NAME", "../xml/settings.xml");
// Retrieve and validate table name.
$table = trim($_GET["table"] ?? "");
if ($table === "") {
echo "No table name given. Table not created.";
exit();
}
if (!preg_match('/^[a-zA-Z0-9_]+$/', $table)) {
echo "Invalid table name. Table not created.";
exit();
}
$tableFile = __DIR__ . "/{$table}.php";
if (!is_file($tableFile)) {
echo htmlspecialchars("{$table}.php not found. Table not created.", ENT_QUOTES, "UTF-8");
exit();
}
// Load table SQL.
$SQL = null;
include $tableFile;
if (empty($SQL)) {
echo htmlspecialchars("No SQL found in {$table}.php. Table not created.", ENT_QUOTES, "UTF-8");
exit();
}
// Retrieve Application Settings.
$applicationSettings = simplexml_load_file(INITIALIZATION_FILE_NAME);
if (empty($applicationSettings)) {
echo "Unable to load Application Settings.";
exit();
}
// Connect to the Database.
$host = ((string) $applicationSettings->Environment === "Production") ? "localhost" : "192.168.1.190";
$database = (string) $applicationSettings->DatabaseName;
$user = (string) $applicationSettings->DatabaseUser;
$password = (string) $applicationSettings->DatabasePassword;
try {
$connection = new PDO(
"mysql:host={$host};dbname={$database};charset=utf8mb4",
$user,
$password,
[
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
} catch (PDOException | Exception $e) {
echo "Unable to connect to the database.";
exit();
}
// Drop and recreate table.
try {
$connection->exec("SET FOREIGN_KEY_CHECKS = 0");
$connection->exec("DROP TABLE IF EXISTS `{$table}`");
$connection->exec($SQL);
$connection->exec("SET FOREIGN_KEY_CHECKS = 1");
echo htmlspecialchars("Table {$table} was created.", ENT_QUOTES, "UTF-8");
} catch (PDOException | Exception $e) {
try {
$connection->exec("SET FOREIGN_KEY_CHECKS = 1");
} catch (Exception $ignored) {
}
echo "Error creating table.";
exit();
}
?>