1
This commit is contained in:
parent
c6fdd282f8
commit
1f29b92655
@ -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;
|
||||
}
|
||||
3
auth_footer.php
Normal file
3
auth_footer.php
Normal file
@ -0,0 +1,3 @@
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
10
auth_header.php
Normal file
10
auth_header.php
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Water Tank Monitoring</title>
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
39
db/migrate.php
Normal file
39
db/migrate.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
try {
|
||||
$pdo = db();
|
||||
echo "Connected to database.\n";
|
||||
|
||||
// 1. Create migrations table if it doesn't exist
|
||||
$migrationTableSql = file_get_contents(__DIR__ . '/migrations/000_create_migrations_table.sql');
|
||||
$pdo->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");
|
||||
}
|
||||
|
||||
5
db/migrations/000_create_migrations_table.sql
Normal file
5
db/migrations/000_create_migrations_table.sql
Normal file
@ -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
|
||||
);
|
||||
8
db/migrations/001_create_users_table.sql
Normal file
8
db/migrations/001_create_users_table.sql
Normal file
@ -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
|
||||
);
|
||||
17
db/setup_db.php
Normal file
17
db/setup_db.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
// Generated by setup_mariadb_project.sh — edit as needed.
|
||||
define('DB_HOST', '127.0.0.1');
|
||||
define('DB_NAME', 'app_30903');
|
||||
define('DB_USER', 'app_30903');
|
||||
define('DB_PASS', '7d2d5a2d-6e5e-4580-a9b7-3f8e7288a494');
|
||||
|
||||
try {
|
||||
$pdo = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS, [
|
||||
PDO::ATTR_ERRMODE => 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");
|
||||
}
|
||||
|
||||
5
footer.php
Normal file
5
footer.php
Normal file
@ -0,0 +1,5 @@
|
||||
<script>
|
||||
feather.replace();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
22
header.php
Normal file
22
header.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
// Protect page
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Admin Dashboard - Water Tank Monitoring</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
33
index.php
33
index.php
@ -1,14 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Admin Dashboard</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<?php
|
||||
include 'header.php';
|
||||
|
||||
// Admin only page
|
||||
if ($_SESSION['role'] !== 'admin') {
|
||||
// You can redirect to a different page for non-admin users
|
||||
// For now, just show an error or redirect to login
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
|
||||
<div class="sidebar">
|
||||
<h4 class="px-3">TankMon</h4>
|
||||
@ -50,8 +50,8 @@
|
||||
<header class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h2>Dashboard</h2>
|
||||
<div class="d-flex align-items-center">
|
||||
<span class="me-3">Welcome, Admin!</span>
|
||||
<button class="btn btn-outline-primary">Logout</button>
|
||||
<span class="me-3">Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!</span>
|
||||
<a href="logout.php" class="btn btn-outline-primary">Logout</a>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
@ -89,9 +89,4 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
feather.replace()
|
||||
</script>
|
||||
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
||||
</body>
|
||||
</html>
|
||||
<?php include 'footer.php'; ?>
|
||||
77
login.php
Normal file
77
login.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
require_once 'db/config.php';
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
// If user is already logged in, redirect to dashboard
|
||||
if (isset($_SESSION['user_id'])) {
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
$errors = [];
|
||||
$message = $_SESSION['message'] ?? null;
|
||||
unset($_SESSION['message']);
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$email = $_POST['email'] ?? '';
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if (empty($email)) {
|
||||
$errors[] = 'Email is required';
|
||||
}
|
||||
if (empty($password)) {
|
||||
$errors[] = 'Password is required';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->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';
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<?php include 'auth_header.php'; ?>
|
||||
|
||||
<div class="form-container">
|
||||
<h2>Login</h2>
|
||||
<?php if ($message): ?>
|
||||
<div class="success">
|
||||
<p><?php echo htmlspecialchars($message); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if (!empty($errors)): ?>
|
||||
<div class="errors">
|
||||
<?php foreach ($errors as $error): ?>
|
||||
<p><?php echo htmlspecialchars($error); ?></p>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<form action="login.php" method="post">
|
||||
<div class="form-group">
|
||||
<label for="email">Email</label>
|
||||
<input type="email" name="email" id="email" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" name="password" id="password" required>
|
||||
</div>
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
<p>Don't have an account? <a href="register.php">Register here</a>.</p>
|
||||
</div>
|
||||
|
||||
<?php include 'auth_footer.php'; ?>
|
||||
14
logout.php
Normal file
14
logout.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
// Unset all of the session variables
|
||||
$_SESSION = [];
|
||||
|
||||
// Destroy the session.
|
||||
session_destroy();
|
||||
|
||||
// Redirect to login page
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
98
register.php
Normal file
98
register.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
require_once 'db/config.php';
|
||||
|
||||
// Start session
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
$errors = [];
|
||||
$success = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$username = $_POST['username'] ?? '';
|
||||
$email = $_POST['email'] ?? '';
|
||||
$password = $_POST['password'] ?? '';
|
||||
$password_confirm = $_POST['password_confirm'] ?? '';
|
||||
|
||||
if (empty($username)) {
|
||||
$errors[] = 'Username is required';
|
||||
}
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[] = 'Invalid email format';
|
||||
}
|
||||
if (empty($email)) {
|
||||
$errors[] = 'Email is required';
|
||||
}
|
||||
if (strlen($password) < 8) {
|
||||
$errors[] = 'Password must be at least 8 characters long';
|
||||
}
|
||||
if (empty($password)) {
|
||||
$errors[] = 'Password is required';
|
||||
}
|
||||
if ($password !== $password_confirm) {
|
||||
$errors[] = 'Passwords do not match';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->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';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<?php include 'auth_header.php'; ?>
|
||||
|
||||
<div class="form-container">
|
||||
<h2>Register</h2>
|
||||
<?php if (!empty($errors)): ?>
|
||||
<div class="errors">
|
||||
<?php foreach ($errors as $error): ?>
|
||||
<p><?php echo htmlspecialchars($error); ?></p>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<form action="register.php" method="post" novalidate>
|
||||
<div class="form-group">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" name="username" id="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="email">Email</label>
|
||||
<input type="email" name="email" id="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" name="password" id="password" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password_confirm">Confirm Password</label>
|
||||
<input type="password" name="password_confirm" id="password_confirm" required>
|
||||
</div>
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
<p>Already have an account? <a href="login.php">Login here</a>.</p>
|
||||
</div>
|
||||
|
||||
<?php include 'auth_footer.php'; ?>
|
||||
Loading…
x
Reference in New Issue
Block a user