-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.php
More file actions
125 lines (115 loc) · 7.71 KB
/
Copy pathdashboard.php
File metadata and controls
125 lines (115 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
require_once 'auth_check.php';
require_once 'db.php';
require_once 'csrf_handler.php'; // Inclui nosso novo handler
// --- LÓGICA DO BACK-END ---
// Lógica para CRIAR uma nova chave
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_key'])) {
validate_csrf_token(); // Valida o token antes de qualquer ação
$assigned_user = $_POST['assigned_user'];
$expires_at = $_POST['expires_at'];
$new_key = 'PYIT-' . strtoupper(bin2hex(random_bytes(16)));
if (!empty($assigned_user) && !empty($expires_at)) {
$stmt = $pdo->prepare("INSERT INTO `keys` (access_key, assigned_user, expires_at) VALUES (?, ?, ?)");
$stmt->execute([$new_key, $assigned_user, $expires_at]);
}
header("Location: dashboard.php");
exit;
}
// Lógica para DELETAR uma chave
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_key'])) {
validate_csrf_token(); // Valida o token antes de qualquer ação
$key_id = $_POST['key_id'];
$stmt = $pdo->prepare("DELETE FROM `keys` WHERE id = ?");
$stmt->execute([$key_id]);
header("Location: dashboard.php");
exit;
}
$stmt = $pdo->query("SELECT id, access_key, assigned_user, status, expires_at, created_at FROM `keys` ORDER BY id DESC");
$keys = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard - Painel PyIt</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/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="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<style>
/* Os estilos permanecem os mesmos */
body { background-color: #0d1117; color: #c9d1d9; } .table { --bs-table-bg: #161b22; --bs-table-striped-bg: #21262d; --bs-table-color: #c9d1d9; --bs-table-border-color: #30363d; } .btn-custom { background-color: #238636; border-color: #238636; color: white; } .btn-custom:hover { background-color: #2ea043; border-color: #2ea043; } .card { background-color: #161b22; border: 1px solid #30363d; } .navbar { background-color: #161b22; border-bottom: 1px solid #30363d; } .modal-content { background-color: #161b22; border: 1px solid #30363d; } .form-control { background-color: #0d1117; color: #c9d1d9; border-color: #30363d; } .form-control:focus { background-color: #0d1117; color: #c9d1d9; border-color: #58a6ff; box-shadow: none; } .flatpickr-calendar { background: #161b22; border-color: #30363d; } .flatpickr-day.selected { background: #238636; border-color: #238636; } .flatpickr-day:hover { background: #21262d; } .flatpickr-time input:hover, .flatpickr-time .flatpickr-am-pm:hover { background: #21262d !important; } span.flatpickr-weekday, .flatpickr-day, .numInput, .flatpickr-current-month, .flatpickr-time input, .flatpickr-time .flatpickr-am-pm { color: #c9d1d9 !important; }
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container-fluid"><a class="navbar-brand" href="dashboard.php">Painel PyIt</a><div class="d-flex align-items-center"><span class="navbar-text me-3">Logado como: <strong><?php echo $current_user; ?></strong></span><a href="register.php" class="btn btn-outline-info me-2">Registrar Admin</a><a href="logout.php" class="btn btn-outline-danger">Sair</a></div></div>
</nav>
<div class="container py-5">
<header class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h2">Gerenciador de Chaves</h1>
<button type="button" class="btn btn-custom" data-bs-toggle="modal" data-bs-target="#createKeyModal"><i class="bi bi-plus-circle-fill me-2"></i>Gerar Nova Chave</button>
</header>
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-hover align-middle">
<thead><tr><th>ID</th><th>Usuário</th><th>Chave</th><th>Status</th><th>Expira em</th><th>Ações</th></tr></thead>
<tbody>
<?php foreach ($keys as $key): ?>
<?php
$is_expired = !empty($key['expires_at']) && strtotime($key['expires_at']) < time();
$status_class = $is_expired ? 'bg-danger' : 'bg-success';
$status_text = $is_expired ? 'Vencida' : 'Ativa';
?>
<tr>
<td><?php echo htmlspecialchars($key['id']); ?></td>
<td><?php echo htmlspecialchars($key['assigned_user']); ?></td>
<td><code><?php echo htmlspecialchars($key['access_key']); ?></code></td>
<td><span class="badge <?php echo $status_class; ?>"><?php echo $status_text; ?></span></td>
<td><?php echo !empty($key['expires_at']) ? date("d/m/Y", strtotime($key['expires_at'])) : 'N/A'; ?></td>
<td>
<form method="POST" action="dashboard.php" onsubmit="return confirm('Tem certeza?');">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="key_id" value="<?php echo $key['id']; ?>">
<button type="submit" name="delete_key" class="btn btn-danger btn-sm"><i class="bi bi-trash-fill"></i></button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="modal fade" id="createKeyModal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form method="POST" action="dashboard.php">
<input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
<div class="modal-header">
<h5 class="modal-title">Gerar Nova Chave</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3"><label for="assigned_user" class="form-label">Nome do Usuário</label><input type="text" class="form-control" id="assigned_user" name="assigned_user" required></div>
<div class="mb-3"><label for="expires_at" class="form-label">Data de Validade</label><input type="text" class="form-control" id="expires_at" name="expires_at" required></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
<button type="submit" name="create_key" class="btn btn-custom">Criar Chave</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script src="https://npmcdn.com/flatpickr/dist/l10n/pt.js"></script>
<script>
flatpickr("#expires_at", { dateFormat: "Y-m-d", locale: "pt", minDate: "today" });
</script>
</body>
</html>