This commit is contained in:
Flatlogic Bot 2025-12-01 09:10:54 +00:00
parent 88faa8fb09
commit 232ed5c114
21 changed files with 637 additions and 59 deletions

4
.env
View File

@ -1,5 +1,5 @@
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=local_db
DB_DATABASE=app
DB_USERNAME=root
DB_PASSWORD=password
DB_PASSWORD=password

47
admin/index.php Normal file
View File

@ -0,0 +1,47 @@
<?php
session_start();
require_once __DIR__ . '/../lib.php';
require_once __DIR__ . '/../db/config.php';
require_login();
$pdo = db();
$stmt = $pdo->query('SELECT * FROM leads');
$leads = $stmt->fetchAll();
?>
<!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>
</head>
<body>
<h1>Admin Dashboard</h1>
<p>Welcome to the admin dashboard.</p>
<p><a href="logout.php">Logout</a></p>
<h2>Leads</h2>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
<?php foreach ($leads as $lead): ?>
<tr>
<td><?= htmlspecialchars($lead['id']) ?></td>
<td><?= htmlspecialchars($lead['name']) ?></td>
<td><?= htmlspecialchars($lead['email']) ?></td>
<td><?= htmlspecialchars($lead['created_at']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

67
admin/login.php Normal file
View File

@ -0,0 +1,67 @@
<?php
session_start();
require_once __DIR__ . '/../lib.php';
require_once __DIR__ . '/../db/config.php';
if (is_logged_in()) {
header('Location: /admin/');
exit;
}
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if (login($username, $password)) {
header('Location: /admin/');
exit;
} else {
$error = 'Invalid username or password';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card mt-5">
<div class="card-header">
<h3 class="text-center">Admin Login</h3>
</div>
<div class="card-body">
<?php if ($error): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>
<form action="/admin/login.php" method="post">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</form>
<div class="text-center mt-3">
<p>Don't have an account? <a href="/registration.php">Register here</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

6
admin/logout.php Normal file
View File

@ -0,0 +1,6 @@
<?php
require_once __DIR__ . '/../lib.php';
logout();
header('Location: /admin/login.php');

3
assets/css/contact.css Normal file
View File

@ -0,0 +1,3 @@
.hero-section {
background-color: #f8f9fa;
}

3
assets/css/services.css Normal file
View File

@ -0,0 +1,3 @@
.hero-section {
background-color: #f8f9fa;
}

24
assets/js/contact.js Normal file
View File

@ -0,0 +1,24 @@
document.getElementById('contact-form').addEventListener('submit', function(event) {
event.preventDefault();
const form = event.target;
const formData = new FormData(form);
const formStatus = document.getElementById('form-status');
fetch('/contact_handler.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
form.reset();
formStatus.innerHTML = '<div class="alert alert-success">' + data.message + '</div>';
} else {
formStatus.innerHTML = '<div class="alert alert-danger">' + data.error + '</div>';
}
})
.catch(error => {
formStatus.innerHTML = '<div class="alert alert-danger">An error occurred. Please try again later.</div>';
});
});

78
contact.php Normal file
View File

@ -0,0 +1,78 @@
<?php
session_start();
require_once __DIR__ . '/lib.php';
require_once __DIR__ . '/db/config.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Us</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/contact.css">
</head>
<body>
<header class="bg-light p-3">
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" href="/">Your Logo</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/services.php">Services</a></li>
<li class="nav-item"><a class="nav-link" href="/contact.php">Contact</a></li>
</ul>
</div>
</nav>
</div>
</header>
<main>
<section class="hero-section text-center py-5">
<div class="container">
<h1 class="display-4">Contact Us</h1>
<p class="lead">We'd love to hear from you. Please fill out the form below.</p>
</div>
</section>
<section class="contact-form py-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<form id="contact-form">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone (Optional)</label>
<input type="tel" class="form-control" id="phone" name="phone">
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" id="message" name="message" rows="5" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div id="form-status" class="mt-3"></div>
</div>
</div>
</div>
</section>
</main>
<footer class="bg-light p-3 text-center">
<div class="container">
<p>&copy; 2025 Your Company. All Rights Reserved.</p>
</div>
</footer>
<script src="assets/js/contact.js"></script>
</body>
</html>

View File

@ -1,4 +1,5 @@
<?php
session_start();
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/mail/MailService.php';

41
dashboard.php Normal file
View File

@ -0,0 +1,41 @@
<?php
session_start();
if (!isset($_SESSION['user'])) {
header('Location: /login.php');
exit();
}
$user = $_SESSION['user'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="/">My App</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="/logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="mt-5">Welcome, <?php echo htmlspecialchars($user['username']); ?>!</h1>
<p>This is your dashboard.</p>
</div>
</div>
</div>
</body>
</html>

View File

@ -2,7 +2,7 @@
require_once __DIR__ . '/../lib.php';
load_env(__DIR__ . '/../.env');
$_ENV = array_merge($_ENV, load_env(__DIR__ . '/../.env'));
function db(): ?PDO
{
@ -12,11 +12,11 @@ function db(): ?PDO
return $pdo;
}
$host = getenv('DB_HOST');
$port = getenv('DB_PORT');
$db = getenv('DB_DATABASE');
$user = getenv('DB_USERNAME');
$pass = getenv('DB_PASSWORD');
$host = $_ENV['DB_HOST'] ?? null;
$port = $_ENV['DB_PORT'] ?? null;
$db = $_ENV['DB_DATABASE'] ?? null;
$user = $_ENV['DB_USERNAME'] ?? null;
$pass = $_ENV['DB_PASSWORD'] ?? null;
if (!$host || !$db || !$user) {
// You could log an error or throw an exception here
@ -37,7 +37,6 @@ function db(): ?PDO
} catch (PDOException $e) {
// In a real application, you'd log this error and show a generic error page.
// For development, it's okay to show the error.
// throw new PDOException($e->getMessage(), (int)$e->getCode());
error_log('DB Connection Error: ' . $e->getMessage());
return null;
}

22
db/migrate.php Normal file
View File

@ -0,0 +1,22 @@
<?php
require_once __DIR__ . '/config.php';
function run_migrations() {
$pdo = db();
$migrations_dir = __DIR__ . '/migrations';
$files = glob($migrations_dir . '/*.sql');
sort($files);
foreach ($files as $file) {
echo "Running migration: " . basename($file) . "\n";
$sql = file_get_contents($file);
try {
$pdo->exec($sql);
} catch (PDOException $e) {
echo "Error running migration: " . $e->getMessage() . "\n";
// You might want to log this or handle it more gracefully
}
}
}
run_migrations();

View File

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(255) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

View File

@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN name VARCHAR(255) NOT NULL, ADD COLUMN email VARCHAR(255) NOT NULL UNIQUE;

24
db/seed.php Normal file
View File

@ -0,0 +1,24 @@
<?php
require_once __DIR__ . '/config.php';
function seed_users()
{
$pdo = db();
$username = 'admin';
$password = password_hash('password', PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
try {
$stmt->execute([$username, $password]);
echo "Admin user created successfully.\n";
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) {
echo "Admin user already exists.\n";
} else {
echo "Error creating admin user: " . $e->getMessage() . "\n";
}
}
}
seed_users();

View File

@ -1,3 +1,4 @@
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
@ -28,7 +29,7 @@
<!-- Header -->
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm sticky-top">
<div class="container">
<a class="navbar-brand fw-bold" href="#">
<a class="navbar-brand fw-bold" href="/">
<i class="bi bi-heart-pulse-fill text-primary"></i> MedAgency
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
@ -36,10 +37,16 @@
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="#services">Services</a></li>
<li class="nav-item"><a class="nav-link" href="#about">About</a></li>
<li class="nav-item"><a class="nav-link" href="#testimonials">Testimonials</a></li>
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/services.php">Services</a></li>
<li class="nav-item"><a class="nav-link" href="/contact.php">Contact</a></li>
<?php if (isset($_SESSION['user'])): ?>
<li class="nav-item"><a class="nav-link" href="/dashboard.php">Dashboard</a></li>
<li class="nav-item"><a class="btn btn-primary ms-lg-2" href="/logout.php">Logout</a></li>
<?php else: ?>
<li class="nav-item"><a class="nav-link" href="/registration.php">Register</a></li>
<li class="nav-item"><a class="btn btn-primary ms-lg-2" href="/login.php">Login</a></li>
<?php endif; ?>
</ul>
</div>
</div>
@ -50,7 +57,6 @@
<div class="container text-center py-5">
<h1 class="display-4 fw-bold">Compassionate Care, Advanced Solutions</h1>
<p class="lead col-lg-8 mx-auto">We connect you with world-class medical experts and facilities, ensuring a seamless journey to better health.</p>
<a href="#contact" class="btn btn-primary btn-lg mt-3">Request a Free Quote</a>
</div>
</header>
@ -149,44 +155,7 @@
</section>
<!-- Contact Form -->
<section id="contact" class="py-5">
<div class="container">
<div class="text-center mb-5">
<h2 class="fw-bold">Get a Free Quote</h2>
<p class="text-muted">Fill out the form below, and we'll get back to you with a personalized treatment estimate.</p>
</div>
<div class="row justify-content-center">
<div class="col-lg-8">
<form id="contactForm" novalidate>
<div id="form-alert" class="alert d-none"></div>
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
<div class="invalid-feedback">Please enter your name.</div>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required>
<div class="invalid-feedback">Please enter a valid email.</div>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone Number (Optional)</label>
<input type="tel" class="form-control" id="phone" name="phone">
</div>
<div class="mb-3">
<label for="message" class="form-label">Your Message or Inquiry</label>
<textarea class="form-control" id="message" name="message" rows="5" required></textarea>
<div class="invalid-feedback">Please enter your message.</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary btn-lg">Submit Request</button>
</div>
</form>
</div>
</div>
</div>
</section>
</main>

64
lib.php
View File

@ -2,8 +2,9 @@
function load_env($path)
{
$vars = [];
if (!file_exists($path)) {
return;
return $vars;
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
@ -16,10 +17,61 @@ function load_env($path)
$name = trim($name);
$value = trim($value);
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value));
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
}
$vars[$name] = $value;
}
return $vars;
}
function login(string $username, string $password): bool
{
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
return true;
}
return false;
}
function logout(): void
{
$_SESSION = [];
session_destroy();
}
function is_logged_in(): bool
{
return isset($_SESSION['user_id']);
}
function require_login(): void
{
if (!is_logged_in()) {
header('Location: /admin/login.php');
exit;
}
}
function register(string $name, string $username, string $email, string $password): int|false
{
$pdo = db();
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (name, username, email, password) VALUES (?, ?, ?, ?)");
try {
$stmt->execute([$name, $username, $email, $hashed_password]);
return (int)$pdo->lastInsertId();
} catch (PDOException $e) {
// 1062 is the error code for duplicate entry
if ($e->errorInfo[1] == 1062) {
return false;
}
throw $e;
}
}

64
login.php Normal file
View File

@ -0,0 +1,64 @@
<?php
require_once 'lib.php';
session_start();
if (isset($_SESSION['user'])) {
header('Location: /dashboard.php');
exit();
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if (login($username, $password)) {
header('Location: /dashboard.php');
exit();
} else {
$error = 'Invalid username or password';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card mt-5">
<div class="card-header">
<h4>User Login</h4>
</div>
<div class="card-body">
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
<div class="mt-3">
<p>Don't have an account? <a href="registration.php">Register here</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

5
logout.php Normal file
View File

@ -0,0 +1,5 @@
<?php
session_start();
session_destroy();
header('Location: /login.php');
exit();

88
registration.php Normal file
View File

@ -0,0 +1,88 @@
<?php
session_start();
require_once __DIR__ . '/lib.php';
require_once __DIR__ . '/db/config.php';
if (is_logged_in()) {
header('Location: /admin/');
exit;
}
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$username = $_POST['username'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
if ($password !== $confirm_password) {
$error = 'Passwords do not match';
} else {
$user_id = register($name, $username, $email, $password);
if ($user_id) {
$_SESSION['user_id'] = $user_id;
header('Location: /admin/');
exit;
} else {
$error = 'Username or email already exists';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card mt-5">
<div class="card-header">
<h3 class="text-center">Register</h3>
</div>
<div class="card-body">
<?php if ($error): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>
<form action="/registration.php" method="post">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-3">
<label for="confirm_password" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Register</button>
</div>
</form>
<div class="text-center mt-3">
<p>Already have an account? <a href="/admin/login.php">Login here</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

78
services.php Normal file
View File

@ -0,0 +1,78 @@
<?php
session_start();
require_once __DIR__ . '/lib.php';
require_once __DIR__ . '/db/config.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Services</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/services.css">
</head>
<body>
<header class="bg-light p-3">
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" href="/">Your Logo</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/services.php">Services</a></li>
<li class="nav-item"><a class="nav-link" href="/contact.php">Contact</a></li>
</ul>
</div>
</nav>
</div>
</header>
<main>
<section class="hero-section text-center py-5">
<div class="container">
<h1 class="display-4">Our Services</h1>
<p class="lead">We offer a wide range of services to meet your needs.</p>
</div>
</section>
<section class="services-list py-5">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Service 1</h5>
<p class="card-text">Description of service 1.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Service 2</h5>
<p class="card-text">Description of service 2.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Service 3</h5>
<p class="card-text">Description of service 3.</p>
</div>
</div>
</div>
</div>
</div>
</section>
</main>
<footer class="bg-light p-3 text-center">
<div class="container">
<p>&copy; 2025 Your Company. All Rights Reserved.</p>
</div>
</footer>
</body>
</html>