-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_sessions.php
More file actions
170 lines (158 loc) Β· 7.6 KB
/
active_sessions.php
File metadata and controls
170 lines (158 loc) Β· 7.6 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
session_start();
include './config.php';
$query = new Database();
$query->check_session('user');
$active_sessions = $query->select('active_sessions', '*', 'user_id = ?', [$_SESSION['user']['id']]);
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (
!isset($_POST['csrf_token']) ||
!isset($_SESSION['csrf_token']) ||
!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])
) {
echo json_encode(['status' => 'error', 'title' => 'Invalid CSRF Token', 'message' => 'Invalid CSRF token!']);
exit;
}
header('Content-Type: application/json');
if ($_POST['action'] === 'edit' && isset($_POST['device_name'])) {
$device_name = trim($_POST['device_name']);
if (empty($device_name)) {
echo json_encode(['status' => 'error', 'message' => 'Device name cannot be empty!']);
exit;
}
$updated = $query->update('active_sessions', ['device_name' => $device_name], 'session_token = ? AND user_id = ?', [session_id(), $_SESSION['user']['id']]);
if ($updated) {
echo json_encode(['status' => 'success', 'message' => 'Device name updated!', 'device_name' => $device_name]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to update device name. Try again!']);
}
exit;
} elseif ($_POST['action'] === 'delete' && isset($_POST['token'])) {
$deleted = $query->delete('active_sessions', 'user_id = ? AND session_token = ?', [$_SESSION['user']['id'], $_POST['token']]);
if ($deleted) {
echo json_encode(['status' => 'success', 'message' => 'Session deleted!', 'token' => $_POST['token']]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to delete session. Try again!']);
}
exit;
}
}
$query->generate_csrf_token();
?>
<?php include './header.php'; ?>
<div class="row justify-content-center">
<div class="table-responsive" style="overflow-x: auto;">
<table class="table table-striped table-hover table-bordered" style="white-space: nowrap;">
<thead class="bg-dark text-white text-center">
<tr>
<th> β</th>
<th><i class="fas fa-desktop"></i> Device Name</th>
<th><i class="fas fa-network-wired"></i> IP Address</th>
<th><i class="fas fa-clock"></i> Last Activity</th>
<th><i class="fas fa-cog"></i> Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($active_sessions as $index => $session): ?>
<tr id="session-<?= htmlspecialchars($session['session_token']); ?>" class="text-center">
<td><?= $index + 1 ?></td>
<td id="device-name-<?= htmlspecialchars($session['session_token']); ?>">
<?= htmlspecialchars($session['device_name']); ?>
</td>
<td><?= htmlspecialchars($session['ip_address']); ?></td>
<td><?= date('H:i:s d-m-Y', strtotime($session['last_activity'])); ?></td>
<td>
<div class="d-flex justify-content-center align-items-center gap-2 flex-nowrap"
style="min-width: 140px;">
<?php if (session_id() == $session['session_token']): ?>
<button class="btn btn-warning btn-sm px-3 d-flex align-items-center p-2"
onclick="openEditModal('<?= htmlspecialchars($session['session_token']); ?>')">
<i class="fas fa-edit"></i>
</button>
<?php endif; ?>
<button class="btn btn-danger btn-sm px-3 d-flex align-items-center ml-1 p-2"
onclick="confirmRemoval('<?= htmlspecialchars($session['session_token']); ?>')">
<i class="fas fa-trash"></i>
</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<script>
function openEditModal(token) {
let deviceName = document.getElementById(`device-name-${token}`).textContent.trim();
Swal.fire({
title: 'Edit Device Name',
input: 'text',
inputValue: deviceName,
showCancelButton: true,
confirmButtonText: 'Save',
preConfirm: (newName) => {
if (!newName.trim()) {
Swal.showValidationMessage('Device name cannot be empty!');
}
return newName.trim();
}
}).then((result) => {
if (result.isConfirmed) {
let formData = new FormData();
formData.append('action', 'edit');
formData.append('device_name', result.value);
formData.append('csrf_token', '<?= $_SESSION['csrf_token'] ?>');
fetch('', { method: 'POST', body: formData })
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
document.getElementById(`device-name-${token}`).textContent = data.device_name;
}
Swal.fire({
icon: data.status === 'success' ? 'success' : 'error',
title: data.status === 'success' ? 'Success!' : 'Error!',
text: data.message,
showConfirmButton: data.status !== 'success',
timer: 1500
});
})
.catch(error => console.error('Fetch error:', error));
}
});
}
function confirmRemoval(token) {
Swal.fire({
title: 'Are you sure?',
text: 'This cannot be undone!',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, remove it!'
}).then((result) => {
if (result.isConfirmed) {
let formData = new FormData();
formData.append('action', 'delete');
formData.append('token', token);
formData.append('csrf_token', '<?= $_SESSION['csrf_token'] ?>');
fetch('', { method: 'POST', body: formData })
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
document.getElementById(`session-${data.token}`).remove();
Swal.fire({ title: 'Deleted!', text: data.message, icon: 'success', showConfirmButton: false, timer: 1500 }).then(() => {
if ('<?= session_id() ?>' === token) {
window.location.reload();
}
});
} else {
Swal.fire({ title: 'Error!', text: data.message, icon: 'error', showConfirmButton: true });
}
})
.catch(error => console.error('Fetch error:', error));
}
});
}
</script>
<?php include './footer.php'; ?>