From 1f29b92655effb23e4fff6065eb0bb10ec35eabf Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sun, 28 Sep 2025 18:10:47 +0000 Subject: [PATCH] 1 --- assets/css/custom.css | 83 +++++++++++++++- auth_footer.php | 3 + auth_header.php | 10 ++ db/migrate.php | 39 ++++++++ db/migrations/000_create_migrations_table.sql | 5 + db/migrations/001_create_users_table.sql | 8 ++ db/setup_db.php | 17 ++++ footer.php | 5 + header.php | 22 +++++ index.php | 33 +++---- login.php | 77 +++++++++++++++ logout.php | 14 +++ register.php | 98 +++++++++++++++++++ 13 files changed, 394 insertions(+), 20 deletions(-) create mode 100644 auth_footer.php create mode 100644 auth_header.php create mode 100644 db/migrate.php create mode 100644 db/migrations/000_create_migrations_table.sql create mode 100644 db/migrations/001_create_users_table.sql create mode 100644 db/setup_db.php create mode 100644 footer.php create mode 100644 header.php create mode 100644 login.php create mode 100644 logout.php create mode 100644 register.php diff --git a/assets/css/custom.css b/assets/css/custom.css index 1eb7b70..3f89661 100644 --- a/assets/css/custom.css +++ b/assets/css/custom.css @@ -1,4 +1,3 @@ - body { font-family: 'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif; background-color: #F4F7F6; @@ -50,3 +49,85 @@ body { border-top-left-radius: 8px; border-top-right-radius: 8px; } + +/* New styles for login/register form */ +.container { + display: flex; + justify-content: center; + align-items: center; + min-height: 100vh; +} + +.form-container { + width: 100%; + max-width: 400px; + padding: 2rem; + background-color: #FFFFFF; + border-radius: 8px; + box-shadow: 0 4px 6px rgba(0,0,0,0.1); +} + +.form-container h2 { + text-align: center; + margin-bottom: 1.5rem; + color: #333; +} + +.form-group { + margin-bottom: 1rem; +} + +.form-group label { + display: block; + margin-bottom: 0.5rem; + color: #555; + font-weight: 500; +} + +.form-group input { + width: 100%; + padding: 0.75rem; + border: 1px solid #ccc; + border-radius: 4px; + box-sizing: border-box; +} + +button[type="submit"] { + width: 100%; + padding: 0.75rem; + border: none; + border-radius: 4px; + background-color: #4A90E2; + color: white; + font-size: 1rem; + font-weight: 500; + cursor: pointer; + transition: background-color 0.2s; +} + +button[type="submit"]:hover { + background-color: #357ABD; +} + +.errors { + background-color: #f8d7da; + color: #721c24; + padding: 1rem; + border: 1px solid #f5c6cb; + border-radius: 4px; + margin-bottom: 1rem; +} + +.form-container p { + text-align: center; + margin-top: 1rem; +} + +.success { + background-color: #d4edda; + color: #155724; + padding: 1rem; + border: 1px solid #c3e6cb; + border-radius: 4px; + margin-bottom: 1rem; +} \ No newline at end of file diff --git a/auth_footer.php b/auth_footer.php new file mode 100644 index 0000000..d5f6fbc --- /dev/null +++ b/auth_footer.php @@ -0,0 +1,3 @@ + + + diff --git a/auth_header.php b/auth_header.php new file mode 100644 index 0000000..45b967e --- /dev/null +++ b/auth_header.php @@ -0,0 +1,10 @@ + + + + + + Water Tank Monitoring + + + +
diff --git a/db/migrate.php b/db/migrate.php new file mode 100644 index 0000000..c62073d --- /dev/null +++ b/db/migrate.php @@ -0,0 +1,39 @@ +exec($migrationTableSql); + echo "Migrations table ensured.\n"; + + // 2. Get executed migrations + $stmt = $pdo->query("SELECT migration FROM migrations"); + $executedMigrations = $stmt->fetchAll(PDO::FETCH_COLUMN); + + // 3. Find and execute new migrations + $migrationFiles = glob(__DIR__ . '/migrations/*.sql'); + sort($migrationFiles); + + foreach ($migrationFiles as $file) { + $migrationName = basename($file); + if (!in_array($migrationName, $executedMigrations)) { + $sql = file_get_contents($file); + $pdo->exec($sql); + + $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); + $stmt->execute([$migrationName]); + + echo "Executed migration: $migrationName\n"; + } + } + + echo "All migrations have been run.\n"; + +} catch (PDOException $e) { + die("Database migration failed: " . $e->getMessage() . "\n"); +} + diff --git a/db/migrations/000_create_migrations_table.sql b/db/migrations/000_create_migrations_table.sql new file mode 100644 index 0000000..b72508d --- /dev/null +++ b/db/migrations/000_create_migrations_table.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS `migrations` ( + `id` INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, + `migration` VARCHAR(255) NOT NULL, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/db/migrations/001_create_users_table.sql b/db/migrations/001_create_users_table.sql new file mode 100644 index 0000000..ed09b27 --- /dev/null +++ b/db/migrations/001_create_users_table.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS `users` ( + `id` INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, + `username` VARCHAR(255) NOT NULL UNIQUE, + `email` VARCHAR(255) NOT NULL UNIQUE, + `password` VARCHAR(255) NOT NULL, + `role` VARCHAR(50) NOT NULL DEFAULT 'user', + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/db/setup_db.php b/db/setup_db.php new file mode 100644 index 0000000..576422a --- /dev/null +++ b/db/setup_db.php @@ -0,0 +1,17 @@ + PDO::ERRMODE_EXCEPTION, + ]); + $pdo->exec("CREATE DATABASE IF NOT EXISTS `".DB_NAME."`"); + echo "Database `".DB_NAME."` created or already exists.\n"; +} catch (PDOException $e) { + die("Database setup failed: " . $e->getMessage() . "\n"); +} + diff --git a/footer.php b/footer.php new file mode 100644 index 0000000..5eb2eff --- /dev/null +++ b/footer.php @@ -0,0 +1,5 @@ + + + diff --git a/header.php b/header.php new file mode 100644 index 0000000..13dc58b --- /dev/null +++ b/header.php @@ -0,0 +1,22 @@ + + + + + + + Admin Dashboard - Water Tank Monitoring + + + + + diff --git a/index.php b/index.php index 10ff6b7..7df7839 100644 --- a/index.php +++ b/index.php @@ -1,14 +1,14 @@ - - - - - - Admin Dashboard - - - - - +
- - - - + \ No newline at end of file diff --git a/login.php b/login.php new file mode 100644 index 0000000..2aa7be7 --- /dev/null +++ b/login.php @@ -0,0 +1,77 @@ +prepare("SELECT * FROM users WHERE email = ?"); + $stmt->execute([$email]); + $user = $stmt->fetch(); + + if ($user && password_verify($password, $user['password'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $user['username']; + $_SESSION['role'] = $user['role']; + header('Location: index.php'); + exit; + } else { + $errors[] = 'Invalid email or password'; + } + } +} +?> + + +
+

Login

+ +
+

+
+ + +
+ +

+ +
+ +
+
+ + +
+
+ + +
+ +
+

Don't have an account? Register here.

+
+ + diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..9585214 --- /dev/null +++ b/logout.php @@ -0,0 +1,14 @@ +prepare("SELECT id FROM users WHERE username = ? OR email = ?"); + $stmt->execute([$username, $email]); + if ($stmt->fetch()) { + $errors[] = 'Username or email already exists'; + } else { + $hashed_password = password_hash($password, PASSWORD_DEFAULT); + // For now, the first registered user will be an admin + $role = 'user'; + $stmt_count = $pdo->query("SELECT COUNT(*) FROM users"); + $user_count = $stmt_count->fetchColumn(); + if ($user_count === 0) { + $role = 'admin'; + } + + $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)"); + if ($stmt->execute([$username, $email, $hashed_password, $role])) { + $_SESSION['message'] = 'Registration successful! Please login.'; + header('Location: login.php'); + exit; + } else { + $errors[] = 'Failed to register user'; + } + } + } +} +?> + + +
+

Register

+ +
+ +

+ +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+

Already have an account? Login here.

+
+ +