Update export_log.php

This commit is contained in:
jakani24
2024-03-09 15:39:13 +01:00
parent 0e98e3f368
commit 1fbc7a92c7

View File

@@ -18,6 +18,14 @@ if ($perms[2] !== "1") {
} else { } else {
$block = 0; $block = 0;
} }
// Handle filter submission
$loglevel = isset($_GET["loglevel"]) ? $_GET["loglevel"] : "";
$logtext = isset($_GET["logtext"]) ? $_GET["logtext"] : "";
$machine_id = isset($_GET["machine_id"]) ? $_GET["machine_id"] : "";
$time = isset($_GET["time"]) ? $_GET["time"] : "";
$filter_query = "&loglevel=$loglevel&logtext=$logtext&machine_id=$machine_id&time=$time";
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@@ -25,107 +33,65 @@ if ($perms[2] !== "1") {
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"> integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
</script> crossorigin="anonymous"></script>
<title>Export Log</title> <title>View Log</title>
</head> </head>
<body> <body>
<div class="container mt-5"> <div class="container-fluid">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-md-12"> <div class="col-md-12">
<div class="card"> <div class="card">
<div class="card-header"> <div class="card-header">
<h4>Export Log</h4> <h4>View log</h4>
</div> </div>
<div class="card-body" style="overflow-x:auto"> <div class="card-body" style="overflow-x:auto">
<p>You can use filters before you export the log. The filter preview is below.</p> <!-- Export log -->
<a href="export_log.php?export=true">Export log</a> <a href="export_log.php<?php echo $filter_query; ?>" class="btn btn-primary mb-3">Export</a>
<!-- table with all users => delete button -->
<!-- Table with log entries -->
<?php <?php
//include db pw //include db pw
include "../../../config.php"; include "../../../config.php";
//delete entry if requested and if user has rights to do that
if (isset($_GET["export"])) {
$conn = new mysqli($DB_SERVERNAME, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$export_file_path = "/var/www/html/export/cyberhex_log_export.csv";
$filter_query = "";
// Apply filters if present
if (isset($_GET["loglevel"])) {
$filter_query .= " AND loglevel LIKE '%" . $conn->real_escape_string($_GET["loglevel"]) . "%'";
}
if (isset($_GET["logtext"])) {
$filter_query .= " AND logtext LIKE '%" . $conn->real_escape_string($_GET["logtext"]) . "%'";
}
if (isset($_GET["machine_id"])) {
$filter_query .= " AND machine_id LIKE '%" . $conn->real_escape_string($_GET["machine_id"]) . "%'";
}
if (isset($_GET["time"])) {
$filter_query .= " AND time LIKE '%" . $conn->real_escape_string($_GET["time"]) . "%'";
}
$export_file = fopen($export_file_path, 'w');
if ($export_file === false) {
echo '<br><div class="alert alert-danger" role="alert">
Error creating export file.
</div>';
} else {
fwrite($export_file, "id;loglevel;logtext;machine_id;time\r\n");
$sql = "SELECT * FROM log WHERE 1=1 $filter_query";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
fwrite($export_file, "{$row['id']};{$row['loglevel']};{$row['logtext']};{$row['machine_id']};{$row['time']}\r\n");
}
}
fclose($export_file);
echo '<br><div class="alert alert-success" role="alert">
Export file created <a href="' . $export_file_path . '" download>Download</a>
</div>';
}
$conn->close();
}
?>
<!-- Display log entries with filters -->
<?php
include "../../../config.php";
// Define page size and current page // Define page size and current page
$page_size = 50; $page_size = 50;
$current_page = isset($_GET['page']) ? intval($_GET['page']) : 1; $current_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($current_page - 1) * $page_size; $offset = ($current_page - 1) * $page_size;
// Get total number of log entries // Get total number of log entries based on filters
$conn = new mysqli($DB_SERVERNAME, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE); $conn = new mysqli($DB_SERVERNAME, $DB_USERNAME, $DB_PASSWORD, $DB_DATABASE);
if ($conn->connect_error) { if ($conn->connect_error) {
die("Connection failed: " . $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 ?";
$sql = "SELECT count(*) AS log_count FROM log"; $stmt = $conn->prepare($sql);
$result = $conn->query($sql); $loglevel = "%" . $loglevel . "%";
$logtext = "%" . $logtext . "%";
$machine_id = "%" . $machine_id . "%";
$time = "%" . $time . "%";
$stmt->bind_param("ssss", $loglevel, $logtext, $machine_id, $time);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
$total_entries = $row["log_count"]; $total_entries = $row["log_count"];
// Calculate total pages // Calculate total pages
$total_pages = ceil($total_entries / $page_size); $total_pages = ceil($total_entries / $page_size);
// Query log entries for the current page // Query log entries for the current page with filters
$sql = "SELECT * FROM log ORDER BY id DESC LIMIT ?, ?"; $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); $stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $offset, $page_size); $loglevel = "%" . $loglevel . "%";
$logtext = "%" . $logtext . "%";
$machine_id = "%" . $machine_id . "%";
$time = "%" . $time . "%";
$stmt->bind_param("ssssii", $loglevel, $logtext, $machine_id, $time, $offset, $page_size);
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
@@ -133,23 +99,21 @@ if ($perms[2] !== "1") {
echo '<table class="table" style="overflow-x:auto">'; echo '<table class="table" style="overflow-x:auto">';
echo '<thead>'; echo '<thead>';
echo '<tr>'; echo '<tr>';
echo '<th>Entry id</th><th>Loglevel</th><th>Logtext</th><th>Machine id</th><th>Time & date</th>'; echo '<th>Entry id</th><th>Loglevel</th><th>Logtext</th><th>Machine id</th><th>Time & date</th><th>Delete entry</th>';
echo '</tr>'; echo '</tr>';
echo '</thead>'; echo '</thead>';
echo '<tbody>'; echo '<tbody>';
// Display filter options // Display filter options
$loglevel_ss = isset($_GET["loglevel"]) ? $_GET["loglevel"] : "Loglevel";
$logtext_ss = isset($_GET["logtext"]) ? $_GET["logtext"] : "Logtext";
$machine_id_ss = isset($_GET["machine_id"]) ? $_GET["machine_id"] : "Machine id";
$time_ss = isset($_GET["time"]) ? $_GET["time"] : "Date & time";
echo '<tr>'; echo '<tr>';
echo '<form action="export_log.php" method="get">'; echo '<form action="view_log.php" method="get">';
echo '<input type="hidden" name="filter_submit" value="true">';
echo '<td><button type="submit" class="btn btn-primary btn-block">Filter</button></td>'; echo '<td><button type="submit" class="btn btn-primary btn-block">Filter</button></td>';
echo '<td><input type="text" class="form-control" name="loglevel" placeholder="' . $loglevel_ss . '"></td>'; echo '<td><input type="text" class="form-control" name="loglevel" placeholder="' . str_replace("%", "", $loglevel) . '"></td>';
echo '<td><input type="text" class="form-control" name="logtext" placeholder="' . $logtext_ss . '"></td>'; echo '<td><input type="text" class="form-control" name="logtext" placeholder="' . str_replace("%", "", $logtext) . '"></td>';
echo '<td><input type="text" class="form-control" name="machine_id" placeholder="' . $machine_id_ss . '"></td>'; echo '<td><input type="text" class="form-control" name="machine_id" placeholder="' . str_replace("%", "", $machine_id) . '"></td>';
echo '<td><input type="text" class="form-control" name="time" placeholder="' . $time_ss . '"></td>'; echo '<td><input type="text" class="form-control" name="time" placeholder="' . str_replace("%", "", $time) . '"></td>';
echo '<td>---</td>';
echo '</form>'; echo '</form>';
echo '</tr>'; echo '</tr>';
@@ -160,6 +124,7 @@ if ($perms[2] !== "1") {
echo '<td>' . $row["logtext"] . '</td>'; echo '<td>' . $row["logtext"] . '</td>';
echo '<td>' . $row["machine_id"] . '</td>'; echo '<td>' . $row["machine_id"] . '</td>';
echo '<td>' . $row["time"] . '</td>'; echo '<td>' . $row["time"] . '</td>';
echo '<td><a href="view_log.php?delete=' . $row["id"] . $filter_query . '&page=' . $current_page . '">delete</a></td>';
echo '</tr>'; echo '</tr>';
} }
@@ -167,11 +132,11 @@ if ($perms[2] !== "1") {
echo '</table>'; echo '</table>';
$conn->close(); $conn->close();
// Display pagination links // Display pagination links with filter query
echo '<nav aria-label="Page navigation">'; echo '<nav aria-label="Page navigation">';
echo '<ul class="pagination justify-content-center">'; echo '<ul class="pagination justify-content-center">';
for ($i = 1; $i <= $total_pages; $i++) { for ($i = 1; $i <= $total_pages; $i++) {
echo '<li class="page-item ' . ($i == $current_page ? 'active' : '') . '"><a class="page-link" href="?page=' . $i . '">' . $i . '</a></li>'; echo '<li class="page-item ' . ($i == $current_page ? 'active' : '') . '"><a class="page-link" href="view_log.php?page=' . $i . $filter_query . '">' . $i . '</a></li>';
} }
echo '</ul>'; echo '</ul>';
echo '</nav>'; echo '</nav>';