Update export_log.php

This commit is contained in:
jakani24
2024-03-09 15:49:53 +01:00
parent 20ad0ca8b6
commit 27bd65c24d

View File

@@ -52,23 +52,55 @@ $filter_query = "&loglevel=$loglevel&logtext=$logtext&machine_id=$machine_id&tim
</div>
<div class="card-body" style="overflow-x:auto">
<!-- Export log -->
<a href="export_log.php?export=<?php echo $filter_query; ?>" class="btn btn-primary mb-3">Export</a>
<a href="export_log.php?<?php echo $filter_query; ?>&export=true" class="btn btn-primary mb-3">Export</a>
<!-- Table with log entries -->
<?php
//include db pw
//include db pw
include "../../../config.php";
$conn = new mysqli($DB_SERVERNAME, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//create the export file
if(isset($_GET["export"]))
{
$fp=fopen("/var/www/html/export/cyberhex_log_export.csv","w");
//do all the logic here and write into file
// Query log entries for the export file with filters
$sql = "SELECT * FROM log WHERE loglevel LIKE ? AND logtext LIKE ? AND machine_id LIKE ? AND time LIKE ? ORDER BY id DESC LIMIT ?, ?";
$stmt = $conn->prepare($sql);
$loglevel = "%" . $loglevel . "%";
$logtext = "%" . $logtext . "%";
$machine_id = "%" . $machine_id . "%";
$time = "%" . $time . "%";
$stmt->bind_param("ssssii", $loglevel, $logtext, $machine_id, $time, $offset, $page_size);
$stmt->execute();
$result = $stmt->get_result();
fwrite ($fp,"Entry id;Loglevel;Logtext;Machine id;Time & date\n");
//now add entrys
while ($row = $result->fetch_assoc()) {
echo $row["id"] . ';';
echo $row["loglevel"] . ';';
echo $row["logtext"] . ';';
echo $row["machine_id"] . ';';
echo $row["time"] . ';\n';
}
fclose($fp);
echo '<div class="alert alert-success" role="alert">
Log export finished. <a href="/export/cyberhex_log_export.csv" download>Download export</a>
</div>';
}
//now display the normal page
// Define page size and current page
$page_size = 50;
$current_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($current_page - 1) * $page_size;
// Get total number of log entries based on filters
$conn = new mysqli($DB_SERVERNAME, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT count(*) AS log_count FROM log WHERE loglevel LIKE ? AND logtext LIKE ? AND machine_id LIKE ? AND time LIKE ?";
$stmt = $conn->prepare($sql);
$loglevel = "%" . $loglevel . "%";