Repository navigation
Expand file tree
/
Copy pathlogs.php
More file actions
379 lines (352 loc) · 18.3 KB
/
Copy pathlogs.php
File metadata and controls
379 lines (352 loc) · 18.3 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
require_once __DIR__ . '/inc/bootstrap.php';
require_once __DIR__ . '/inc/logging.php';
require_once __DIR__ . '/inc/timezone_selector.php';
requireLogin();
requireAdmin();
// Get filter parameters
$level_filter = $_GET['level'] ?? '';
$category_filter = $_GET['category'] ?? '';
$firewall_filter = $_GET['firewall_id'] ?? '';
$days_filter = (int)($_GET['days'] ?? 7);
$page = max(1, (int)($_GET['page'] ?? 1));
$per_page = 50;
// Build filters
$filters = [];
if ($level_filter) $filters['level'] = $level_filter;
if ($category_filter) $filters['category'] = $category_filter;
if ($firewall_filter) $filters['firewall_id'] = $firewall_filter;
// Date range filter
$filters['start_date'] = date('Y-m-d H:i:s', strtotime("-$days_filter days"));
// Get logs
$offset = ($page - 1) * $per_page;
$logs = get_logs($filters, $per_page, $offset);
// Get available categories and levels for filters
$stmt = db()->query("SELECT DISTINCT category FROM system_logs ORDER BY category");
$categories = $stmt->fetchAll(PDO::FETCH_COLUMN);
// Friendly names for categories
$category_names = [
'agent' => 'Agent Events',
'proxy' => 'Proxy Requests',
'command' => 'Commands',
'firewall' => 'Firewall Events',
'backup' => 'Backups',
'auth' => 'Authentication',
'system' => 'System',
'dashboard' => 'Dashboard',
'housekeeping' => 'Maintenance'
];
$stmt = db()->query("SELECT DISTINCT level FROM system_logs ORDER BY level");
$levels = $stmt->fetchAll(PDO::FETCH_COLUMN);
$stmt = db()->query("SELECT id, hostname FROM firewalls ORDER BY hostname");
$firewalls = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get total count for pagination
$count_filters = $filters;
$total_logs = db()->prepare("
SELECT COUNT(*) FROM system_logs sl
WHERE timestamp >= ?
" . ($level_filter ? " AND level = ?" : "") . "
" . ($category_filter ? " AND category = ?" : "") . "
" . ($firewall_filter ? " AND firewall_id = ?" : "")
);
$count_params = [$filters['start_date']];
if ($level_filter) $count_params[] = $level_filter;
if ($category_filter) $count_params[] = $category_filter;
if ($firewall_filter) $count_params[] = $firewall_filter;
$total_logs->execute($count_params);
$total_count = $total_logs->fetchColumn();
$total_pages = ceil($total_count / $per_page);
include __DIR__ . '/inc/header.php';
?>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="d-flex align-items-center justify-content-between gap-2 mb-3">
<small class="fw-bold mb-0">
<i class="fas fa-list-alt me-1"></i>System Logs
</small>
<div class="ms-auto">
<button type="button" id="autoRefreshBtn" class="btn btn-outline-info btn-sm me-2" onclick="toggleAutoRefresh()">
<i class="fas fa-sync-alt me-1"></i>Auto-Refresh: <span id="refreshStatus">OFF</span>
</button>
<button type="button" class="btn btn-outline-warning btn-sm me-2" onclick="cleanupLogs()">
<i class="fas fa-broom me-1"></i>Cleanup Old Logs
</button>
<button type="button" class="btn btn-outline-danger btn-sm me-2" onclick="clearAllLogs()">
<i class="fas fa-trash me-1"></i>Clear All Logs
</button>
<a href="/dashboard.php" class="btn btn-outline-light btn-sm">
<i class="fas fa-arrow-left me-1"></i>Back to Dashboard
</a>
</div>
</div>
<!-- Filters -->
<div class="card mb-3">
<div class="card-body">
<form method="get" class="row g-3">
<div class="col-md-2">
<label for="level" class="form-label">Level</label>
<select class="form-select" name="level" id="level">
<option value="">All Levels</option>
<?php foreach ($levels as $level): ?>
<option value="<?php echo htmlspecialchars($level); ?>"
<?php echo $level_filter === $level ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($level); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label for="category" class="form-label">Category</label>
<select class="form-select" name="category" id="category">
<option value="">All Categories</option>
<?php foreach ($categories as $category):
$display_name = $category_names[$category] ?? ucfirst($category);
?>
<option value="<?php echo htmlspecialchars($category); ?>"
<?php echo $category_filter === $category ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($display_name); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label for="firewall_id" class="form-label">Firewall</label>
<select class="form-select" name="firewall_id" id="firewall_id">
<option value="">All Firewalls</option>
<?php foreach ($firewalls as $firewall): ?>
<option value="<?php echo $firewall['id']; ?>"
<?php echo $firewall_filter == $firewall['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($firewall['hostname']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label for="days" class="form-label">Time Range</label>
<select class="form-select" name="days" id="days">
<option value="1" <?php echo $days_filter === 1 ? 'selected' : ''; ?>>Last 24 hours</option>
<option value="7" <?php echo $days_filter === 7 ? 'selected' : ''; ?>>Last 7 days</option>
<option value="30" <?php echo $days_filter === 30 ? 'selected' : ''; ?>>Last 30 days</option>
<option value="90" <?php echo $days_filter === 90 ? 'selected' : ''; ?>>Last 90 days</option>
</select>
</div>
<div class="col-md-2">
<label class="form-label"> </label>
<div>
<button type="submit" class="btn btn-primary btn-sm">
<i class="fas fa-search me-1"></i>Filter
</button>
<a href="/logs.php" class="btn btn-secondary btn-sm ms-1">
<i class="fas fa-times me-1"></i>Clear
</a>
</div>
</div>
</form>
</div>
</div>
<!-- Results Summary -->
<div class="d-flex justify-content-between align-items-center mb-3">
<small>
Showing <?php echo number_format(count($logs)); ?> of <?php echo number_format($total_count); ?> log entries
<?php if ($days_filter): ?>
(last <?php echo $days_filter; ?> days)
<?php endif; ?>
</small>
<?php if ($total_pages > 1): ?>
<nav aria-label="Log pagination">
<ul class="pagination pagination-sm mb-0">
<?php if ($page > 1): ?>
<li class="page-item">
<a class="page-link"
href="?<?php echo http_build_query(array_merge($_GET, ['page' => $page - 1])); ?>">Previous</a>
</li>
<?php endif; ?>
<?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
<li class="page-item <?php echo $i === $page ? 'active' : ''; ?>">
<a class="page-link"
href="?<?php echo http_build_query(array_merge($_GET, ['page' => $i])); ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<?php if ($page < $total_pages): ?>
<li class="page-item">
<a class="page-link"
href="?<?php echo http_build_query(array_merge($_GET, ['page' => $page + 1])); ?>">Next</a>
</li>
<?php endif; ?>
</ul>
</nav>
<?php endif; ?>
</div>
<!-- Logs Table -->
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Timestamp</th>
<th>Level</th>
<th>Category</th>
<th>Message</th>
<th>Firewall</th>
<th>IP Address</th>
</tr>
</thead>
<tbody>
<?php if (empty($logs)): ?>
<tr>
<td colspan="6" class="text-center text-muted">No logs found for the selected criteria</td>
</tr>
<?php else: ?>
<?php foreach ($logs as $log): ?>
<tr>
<td>
<small><?php echo convertToDisplayTimezone($log['timestamp'], 'M j, Y H:i:s'); ?></small>
</td>
<td>
<span class="badge <?php
echo match($log['level']) {
'ERROR' => 'bg-danger',
'WARNING' => 'bg-warning',
'INFO' => 'bg-info',
'DEBUG' => 'bg-secondary',
default => 'bg-light'
};
?>">
<?php echo htmlspecialchars($log['level']); ?>
</span>
</td>
<td>
<small><?php echo htmlspecialchars($log['category']); ?></small>
</td>
<td>
<?php echo htmlspecialchars($log['message']); ?>
<?php if (!empty($log['additional_data'])): ?>
<button class="btn btn-sm btn-outline-info ms-2"
onclick="showAdditionalData(<?php echo htmlspecialchars(json_encode($log['additional_data']), ENT_QUOTES); ?>)">
<i class="fas fa-info-circle"></i>
</button>
<?php endif; ?>
</td>
<td>
<?php if ($log['firewall_hostname']): ?>
<small><?php echo htmlspecialchars($log['firewall_hostname']); ?></small>
<?php else: ?>
<small class="text-muted">-</small>
<?php endif; ?>
</td>
<td>
<small><?php echo htmlspecialchars($log['ip_address']); ?></small>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Additional Data Modal -->
<div class="modal fade" id="additionalDataModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Additional Data</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<pre id="additionalDataContent" class="p-3 rounded"></pre>
</div>
</div>
</div>
</div>
<script>
const csrfToken = '<?php echo csrf_token(); ?>';
let autoRefreshInterval = null;
let refreshIntervalSeconds = 10; // Changed from 5 to 10 seconds
function toggleAutoRefresh() {
if (autoRefreshInterval) {
// Turn off auto-refresh
clearInterval(autoRefreshInterval);
autoRefreshInterval = null;
localStorage.setItem('logsAutoRefresh', 'false');
document.getElementById('refreshStatus').textContent = 'OFF';
document.getElementById('autoRefreshBtn').classList.remove('btn-success');
document.getElementById('autoRefreshBtn').classList.add('btn-outline-info');
} else {
// Turn on auto-refresh
localStorage.setItem('logsAutoRefresh', 'true');
autoRefreshInterval = setInterval(() => {
location.reload();
}, refreshIntervalSeconds * 1000);
document.getElementById('refreshStatus').textContent = `ON (${refreshIntervalSeconds}s)`;
document.getElementById('autoRefreshBtn').classList.remove('btn-outline-info');
document.getElementById('autoRefreshBtn').classList.add('btn-success');
}
}
// Check if auto-refresh was enabled and restart it on page load
document.addEventListener('DOMContentLoaded', function() {
if (localStorage.getItem('logsAutoRefresh') === 'true') {
// Auto-start the refresh
autoRefreshInterval = setInterval(() => {
location.reload();
}, refreshIntervalSeconds * 1000);
document.getElementById('refreshStatus').textContent = `ON (${refreshIntervalSeconds}s)`;
document.getElementById('autoRefreshBtn').classList.remove('btn-outline-info');
document.getElementById('autoRefreshBtn').classList.add('btn-success');
}
});
function showAdditionalData(data) {
document.getElementById('additionalDataContent').textContent = JSON.stringify(JSON.parse(data), null, 2);
new bootstrap.Modal(document.getElementById('additionalDataModal')).show();
}
function cleanupLogs() {
if (confirm('This will permanently delete logs older than 30 days. Are you sure?')) {
fetch('/api/cleanup_logs.php', {
method: 'POST',
headers: {'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert(`Successfully cleaned up ${data.deleted_count} old log entries.`);
location.reload();
} else {
alert('Error cleaning up logs: ' + data.message);
}
})
.catch(error => {
alert('Error cleaning up logs: ' + error.message);
});
}
}
function clearAllLogs() {
if (confirm('This will permanently delete ALL logs. This action cannot be undone. Are you sure?')) {
if (confirm('Are you absolutely sure? This will delete all system logs permanently.')) {
fetch('/api/clear_all_logs.php', {
method: 'POST',
headers: {'X-CSRF-Token': csrfToken,
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert(`Successfully cleared ${data.deleted_count} log entries.`);
location.reload();
} else {
alert('Error clearing logs: ' + data.message);
}
})
.catch(error => {
alert('Error clearing logs: ' + error.message);
});
}
}
}
</script>
<?php include __DIR__ . '/inc/footer.php'; ?>