diff --git a/polls/db.php b/polls/db.php index c02894e..2d8f7ac 100644 --- a/polls/db.php +++ b/polls/db.php @@ -1,166 +1,90 @@ query("SELECT COUNT(*) FROM users")->fetchColumn(); + // Initialize the PDO connection + $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 ($checkTotal > 0) { - ?> - - - - - Setup Admin User - - - -
-

Admin User Already Exists

-

- An admin user has already been created. No additional admins can be set up here. -

-

- Go back to the Polls site. -

-
- - - exec(" + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE NOT NULL, + password TEXT NOT NULL + ); + "); -// If we are here, no user exists yet, so show the form -if (isset($_POST['setup'])) { - $username = trim($_POST['username'] ?? ''); - $password = trim($_POST['password'] ?? ''); - $confirmPassword = trim($_POST['confirm_password'] ?? ''); + // Create 'poll_questions' table + $db->exec(" + CREATE TABLE IF NOT EXISTS poll_questions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + question_text TEXT NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP + ); + "); - // Basic validation - if ($username === '' || $password === '' || $confirmPassword === '') { - $error = 'All fields are required.'; - } elseif ($password !== $confirmPassword) { - $error = 'Passwords do not match.'; - } else { - // Create the first (and only) admin user - $hashedPassword = password_hash($password, PASSWORD_DEFAULT); - $insertStmt = $db->prepare(" + // Create 'poll_options' table + $db->exec(" + CREATE TABLE IF NOT EXISTS poll_options ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + question_id INTEGER NOT NULL, + option_text TEXT NOT NULL, + FOREIGN KEY (question_id) REFERENCES poll_questions(id) + ); + "); + + // 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) VALUES (:username, :password) "); - $insertStmt->bindValue(':username', $username, PDO::PARAM_STR); - $insertStmt->bindValue(':password', $hashedPassword, PDO::PARAM_STR); - $insertStmt->execute(); - - $success = "Admin user '$username' created successfully."; + $insertUser->bindValue(':username', $adminUsername, PDO::PARAM_STR); + $insertUser->bindValue(':password', $adminHashedPassword, PDO::PARAM_STR); + $insertUser->execute(); } + + // 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; } ?> - - - - - Setup Admin User - - - -
-

Setup Admin User

- - -
- - -
-

You can now go to the Admin page to log in.

- -
-
- - -
- -
- - -
- -
- - -
- -
- -
-
- -
- -