-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_logs.php
More file actions
46 lines (37 loc) · 1.09 KB
/
Copy pathexport_logs.php
File metadata and controls
46 lines (37 loc) · 1.09 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
<?php
require_once 'db.php';
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
// Download logs of the user
$stmt = $pdo->prepare("
SELECT logs.*, projects.name AS project_name
FROM logs
JOIN projects ON logs.project_id = projects.id
WHERE logs.user_id = :user_id
ORDER BY logs.timestamp DESC
");
$stmt->execute(['user_id' => $_SESSION['user_id']]);
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="logs.csv"');
header('Content-Encoding: UTF-8');
// Open a stream for writing CSV
$output = fopen('php://output', 'w');
// Set the separator to semicolon (for Excel)
$delimiter = ';';
// Add column headers
fputcsv($output, ['Timestamp', 'Project', 'Severity', 'Message'], $delimiter);
foreach ($logs as $log) {
$log_data = [
$log['timestamp'],
$log['project_name'],
$log['severity'],
$log['message']
];
fputcsv($output, $log_data, $delimiter);
}
fclose($output);
exit;