| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:57:05 +00:00
										 |  |  | $databaseFile = __DIR__ . '/poll.db'; | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  | try { | 
					
						
							|  |  |  |     // If the database file does not exist, create it and set it up
 | 
					
						
							|  |  |  |     $dbExists = file_exists($databaseFile); | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  |     // Initialize the PDO connection
 | 
					
						
							|  |  |  |     $db = new PDO('sqlite:' . $databaseFile); | 
					
						
							|  |  |  |     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  |     // If the DB didn't exist before, create the required tables
 | 
					
						
							|  |  |  |     if (!$dbExists) { | 
					
						
							|  |  |  |         // Create 'users' table
 | 
					
						
							|  |  |  |         $db->exec("
 | 
					
						
							|  |  |  |             CREATE TABLE IF NOT EXISTS users ( | 
					
						
							|  |  |  |                 id INTEGER PRIMARY KEY AUTOINCREMENT, | 
					
						
							|  |  |  |                 username TEXT UNIQUE NOT NULL, | 
					
						
							|  |  |  |                 password TEXT NOT NULL | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         ");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 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 | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         ");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 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) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         ");
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  |         // 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); | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  |         $insertUser = $db->prepare("
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  |             INSERT INTO users (username, password) | 
					
						
							|  |  |  |             VALUES (:username, :password) | 
					
						
							|  |  |  |         ");
 | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  |         $insertUser->bindValue(':username', $adminUsername, PDO::PARAM_STR); | 
					
						
							|  |  |  |         $insertUser->bindValue(':password', $adminHashedPassword, PDO::PARAM_STR); | 
					
						
							|  |  |  |         $insertUser->execute(); | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-25 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 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; | 
					
						
							| 
									
										
										
										
											2025-01-25 01:32:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | ?>
 |