anime-backlog-list/php/get_logs.php
2025-01-07 12:23:54 +05:00

56 lines
1.6 KiB
PHP

<?php
include_once 'db_connect.php';
include_once 'check_allowed_ip.php';
$clientIP = $_SERVER['REMOTE_ADDR'];
if (!isAllowedIP($clientIP, $allowedSubnets)) {
http_response_code(403);
die("Access denied for {$clientIP}");
}
// Generate CSRF token if not set
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrf_token = $_SESSION['csrf_token'];
// Handle clear logs action
if (isset($_GET['action']) && $_GET['action'] === 'clear_logs') {
// Verify CSRF token
if (!isset($_GET['token']) || $_GET['token'] !== $_SESSION['csrf_token']) {
// Invalid CSRF token
die('Invalid CSRF token');
}
// Clear the logs
$stmt = $conn->prepare("TRUNCATE TABLE action_logs");
$stmt->execute();
// Regenerate CSRF token
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
// Redirect back to panel.php
header("Location: panel.php");
exit();
}
// Handle pagination parameters passed from logs.php
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
// Count total logs
$count_stmt = $conn->query("SELECT COUNT(*) AS total FROM action_logs");
$total_logs = (int)$count_stmt->fetchColumn();
// Calculate offset
$offset = ($page - 1) * $per_page;
// Fetch logs with LIMIT and OFFSET
$stmt = $conn->prepare("SELECT * FROM action_logs ORDER BY action_time DESC LIMIT :limit OFFSET :offset");
$stmt->bindValue(':limit', $per_page, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);