mirror of https://github.com/tildeclub/site.git
Update db.php
This commit is contained in:
parent
54e0502922
commit
095dd7ea70
230
polls/db.php
230
polls/db.php
|
@ -1,166 +1,90 @@
|
||||||
<?php
|
<?php
|
||||||
session_start();
|
/**
|
||||||
|
* db.php
|
||||||
|
*
|
||||||
|
* This file checks if the SQLite database file 'poll.db' exists.
|
||||||
|
* If not, it creates one and sets up the required tables for:
|
||||||
|
* - Users (including an admin user/password)
|
||||||
|
* - Poll questions
|
||||||
|
* - Poll options
|
||||||
|
* - Poll results
|
||||||
|
*/
|
||||||
|
|
||||||
// Include the database setup/connection
|
$databaseFile = __DIR__ . '../pollsdb/poll.db';
|
||||||
require_once 'db.php';
|
|
||||||
|
|
||||||
// Initialize variables
|
try {
|
||||||
$error = '';
|
// If the database file does not exist, create it and set it up
|
||||||
$success = '';
|
$dbExists = file_exists($databaseFile);
|
||||||
|
|
||||||
// Count how many users already exist in the database
|
// Initialize the PDO connection
|
||||||
$checkTotal = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
|
$db = new PDO('sqlite:' . $databaseFile);
|
||||||
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
// If at least one user exists, show a message and no form
|
// If the DB didn't exist before, create the required tables
|
||||||
if ($checkTotal > 0) {
|
if (!$dbExists) {
|
||||||
?>
|
// Create 'users' table
|
||||||
<!DOCTYPE html>
|
$db->exec("
|
||||||
<html>
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
<head>
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
<meta charset="UTF-8">
|
username TEXT UNIQUE NOT NULL,
|
||||||
<title>Setup Admin User</title>
|
password TEXT NOT NULL
|
||||||
<style>
|
);
|
||||||
body {
|
");
|
||||||
font-family: Arial, sans-serif;
|
|
||||||
margin: 20px;
|
|
||||||
}
|
|
||||||
.container {
|
|
||||||
max-width: 500px;
|
|
||||||
margin: 0 auto;
|
|
||||||
}
|
|
||||||
.info {
|
|
||||||
color: #333;
|
|
||||||
}
|
|
||||||
a {
|
|
||||||
color: blue;
|
|
||||||
text-decoration: underline;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
<h2>Admin User Already Exists</h2>
|
|
||||||
<p class="info">
|
|
||||||
An admin user has already been created. No additional admins can be set up here.
|
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
Go back to the <a href="index.php">Polls site</a>.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
<?php
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we are here, no user exists yet, so show the form
|
// Create 'poll_questions' table
|
||||||
if (isset($_POST['setup'])) {
|
$db->exec("
|
||||||
$username = trim($_POST['username'] ?? '');
|
CREATE TABLE IF NOT EXISTS poll_questions (
|
||||||
$password = trim($_POST['password'] ?? '');
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
$confirmPassword = trim($_POST['confirm_password'] ?? '');
|
question_text TEXT NOT NULL,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
");
|
||||||
|
|
||||||
// Basic validation
|
// Create 'poll_options' table
|
||||||
if ($username === '' || $password === '' || $confirmPassword === '') {
|
$db->exec("
|
||||||
$error = 'All fields are required.';
|
CREATE TABLE IF NOT EXISTS poll_options (
|
||||||
} elseif ($password !== $confirmPassword) {
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
$error = 'Passwords do not match.';
|
question_id INTEGER NOT NULL,
|
||||||
} else {
|
option_text TEXT NOT NULL,
|
||||||
// Create the first (and only) admin user
|
FOREIGN KEY (question_id) REFERENCES poll_questions(id)
|
||||||
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
);
|
||||||
$insertStmt = $db->prepare("
|
");
|
||||||
|
|
||||||
|
// Create 'poll_results' table
|
||||||
|
$db->exec("
|
||||||
|
CREATE TABLE IF NOT EXISTS poll_results (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
question_id INTEGER NOT NULL,
|
||||||
|
option_id INTEGER NOT NULL,
|
||||||
|
vote_count INTEGER NOT NULL DEFAULT 0,
|
||||||
|
FOREIGN KEY (question_id) REFERENCES poll_questions(id),
|
||||||
|
FOREIGN KEY (option_id) REFERENCES poll_options(id)
|
||||||
|
);
|
||||||
|
");
|
||||||
|
|
||||||
|
// Create a default admin user with a hashed password
|
||||||
|
// NOTE: In production, you should not hardcode these credentials.
|
||||||
|
// Instead, store them outside of your code or set them up once.
|
||||||
|
$adminUsername = 'admin';
|
||||||
|
$adminPlainPassword = 'password'; // Change this in production
|
||||||
|
$adminHashedPassword = password_hash($adminPlainPassword, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
|
$insertUser = $db->prepare("
|
||||||
INSERT INTO users (username, password)
|
INSERT INTO users (username, password)
|
||||||
VALUES (:username, :password)
|
VALUES (:username, :password)
|
||||||
");
|
");
|
||||||
$insertStmt->bindValue(':username', $username, PDO::PARAM_STR);
|
$insertUser->bindValue(':username', $adminUsername, PDO::PARAM_STR);
|
||||||
$insertStmt->bindValue(':password', $hashedPassword, PDO::PARAM_STR);
|
$insertUser->bindValue(':password', $adminHashedPassword, PDO::PARAM_STR);
|
||||||
$insertStmt->execute();
|
$insertUser->execute();
|
||||||
|
|
||||||
$success = "Admin user '$username' created successfully.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Optionally, you can return $db or leave it globally accessible
|
||||||
|
// for other parts of your application.
|
||||||
|
// Example:
|
||||||
|
// return $db;
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Database error: " . $e->getMessage();
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Setup Admin User</title>
|
|
||||||
<style>
|
|
||||||
body {
|
|
||||||
font-family: Arial, sans-serif;
|
|
||||||
margin: 20px;
|
|
||||||
}
|
|
||||||
.container {
|
|
||||||
max-width: 500px;
|
|
||||||
margin: 0 auto;
|
|
||||||
}
|
|
||||||
.error {
|
|
||||||
color: red;
|
|
||||||
}
|
|
||||||
.success {
|
|
||||||
color: green;
|
|
||||||
}
|
|
||||||
label {
|
|
||||||
display: inline-block;
|
|
||||||
width: 120px;
|
|
||||||
}
|
|
||||||
input[type=text],
|
|
||||||
input[type=password] {
|
|
||||||
width: 250px;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
|
||||||
button {
|
|
||||||
margin-top: 5px;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="container">
|
|
||||||
<h2>Setup Admin User</h2>
|
|
||||||
|
|
||||||
<?php if ($error): ?>
|
|
||||||
<div class="error"><?php echo htmlspecialchars($error); ?></div>
|
|
||||||
<?php endif; ?>
|
|
||||||
<?php if ($success): ?>
|
|
||||||
<div class="success"><?php echo htmlspecialchars($success); ?></div>
|
|
||||||
<p>You can now <a href="admin.php">go to the Admin page</a> to log in.</p>
|
|
||||||
<?php else: ?>
|
|
||||||
<form action="setup.php" method="post">
|
|
||||||
<div>
|
|
||||||
<label for="username">Admin Username:</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
name="username"
|
|
||||||
id="username"
|
|
||||||
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<label for="password">Password:</label>
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
name="password"
|
|
||||||
id="password"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<label for="confirm_password">Confirm Password:</label>
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
name="confirm_password"
|
|
||||||
id="confirm_password"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<button type="submit" name="setup">Save Admin User</button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
<?php endif; ?>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
Loading…
Reference in New Issue