36241-vm/admin.php
2025-11-25 04:46:09 +00:00

230 lines
10 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/db/db_setup.php';
// Handle Add User POST Request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_user'])) {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$role = $_POST['role'];
if (empty($username) || empty($email) || empty($password) || empty($role)) {
$_SESSION['toast'] = ['type' => 'danger', 'message' => 'All fields are required.'];
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$_SESSION['toast'] = ['type' => 'danger', 'message' => 'Invalid email format.'];
} else {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
$stmt->execute([$username, $email]);
if ($stmt->fetch()) {
$_SESSION['toast'] = ['type' => 'danger', 'message' => 'Username or email already exists.'];
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)");
$stmt->execute([$username, $email, $hashed_password, $role]);
$_SESSION['toast'] = ['type' => 'success', 'message' => 'User added successfully!'];
}
} catch (PDOException $e) {
$_SESSION['toast'] = ['type' => 'danger', 'message' => 'Database error: ' . $e->getMessage()];
}
}
header("Location: admin.php");
exit;
}
// Fetch all users
try {
$pdo = db();
$users_stmt = $pdo->query("SELECT id, username, email, role, status, created_at FROM users ORDER BY created_at DESC");
$users = $users_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$users = [];
$_SESSION['toast'] = ['type' => 'danger', 'message' => 'Could not fetch users.'];
}
$toast = $_SESSION['toast'] ?? null;
unset($_SESSION['toast']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Panel - IPTV Manager</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<div class="d-flex">
<!-- Sidebar -->
<nav class="sidebar bg-dark d-flex flex-column p-3">
<a href="/" class="d-flex align-items-center mb-4 text-white text-decoration-none">
<i class="bi bi-broadcast-pin me-2 fs-4"></i>
<span class="fs-4">IPTV Panel</span>
</a>
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="#" class="nav-link active" aria-current="page">
<i class="bi bi-people-fill me-2"></i> Users
</a>
</li>
<li>
<a href="#" class="nav-link text-white">
<i class="bi bi-person-badge me-2"></i> Resellers
</a>
</li>
<li>
<a href="#" class="nav-link text-white">
<i class="bi bi-box me-2"></i> Packages
</a>
</li>
<li>
<a href="#" class="nav-link text-white">
<i class="bi bi-list-task me-2"></i> Playlists
</a>
</li>
<li>
<a href="#" class="nav-link text-white">
<i class="bi bi-gear-fill me-2"></i> Settings
</a>
</li>
</ul>
<hr>
<div class="dropdown">
<a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" id="dropdownUser1" data-bs-toggle="dropdown" aria-expanded="false">
<img src="https://i.pravatar.cc/40?u=admin" alt="" width="32" height="32" class="rounded-circle me-2">
<strong>Superadmin</strong>
</a>
<ul class="dropdown-menu dropdown-menu-dark text-small shadow" aria-labelledby="dropdownUser1">
<li><a class="dropdown-item" href="#">Profile</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Sign out</a></li>
</ul>
</div>
</nav>
<!-- Main Content -->
<main class="main-content flex-grow-1 p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0 text-gray-800">User Management</h1>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addUserModal">
<i class="bi bi-plus-circle-fill me-2"></i>Add New User
</button>
</div>
<!-- Users Table -->
<div class="card shadow-sm">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th>Username</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th>Registered</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($users)): ?>
<tr><td colspan="6" class="text-center">No users found.</td></tr>
<?php else: ?>
<?php foreach ($users as $user): ?>
<tr>
<td><strong><?= htmlspecialchars($user['username']) ?></strong></td>
<td><?= htmlspecialchars($user['email']) ?></td>
<td><span class="badge bg-secondary"><?= htmlspecialchars(ucfirst($user['role'])) ?></span></td>
<td>
<span class="badge rounded-pill <?= $user['status'] === 'active' ? 'bg-success' : 'bg-danger' ?>">
<?= htmlspecialchars(ucfirst($user['status'])) ?>
</span>
</td>
<td><?= date("M d, Y", strtotime($user['created_at'])) ?></td>
<td>
<button class="btn btn-sm btn-outline-secondary"><i class="bi bi-pencil-square"></i></button>
<button class="btn btn-sm btn-outline-warning"><i class="bi bi-slash-circle"></i></button>
<button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</main>
</div>
<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1" aria-labelledby="addUserModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addUserModalLabel">Add New User</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="POST" action="admin.php">
<div class="modal-body">
<input type="hidden" name="add_user" value="1">
<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 address</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="role" class="form-label">Role</label>
<select class="form-select" id="role" name="role">
<option value="subscriber">Subscriber</option>
<option value="reseller">Reseller</option>
<option value="superadmin">Superadmin</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save User</button>
</div>
</form>
</div>
</div>
</div>
<!-- Toast Container -->
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<?php if ($toast): ?>
<div id="liveToast" class="toast show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header text-white bg-<?= $toast['type'] ?>">
<strong class="me-auto">Notification</strong>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
<?= htmlspecialchars($toast['message']) ?>
</div>
</div>
<?php endif; ?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>