Skip to content

Commit 4daa0cd

Browse files
authored
Merge pull request #6 from SubleXBle/Hotfix---Days-List--0.2.3
Update list-files.php Fix file date filtering to include today's JSON logs and ensure latest files are listed correctly.
2 parents 64c1c0a + af000bc commit 4daa0cd

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

includes/list-files.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@
1717

1818
$jsonDir = dirname(__DIR__) . '/archive/';
1919

20-
// Filter all matching JSON files by date in filename
21-
$files = array_filter(scandir($jsonDir), function($filename) use ($maxDays, $jsonDir) {
20+
// Collect all matching JSON files with their dates extracted from filenames
21+
$matchedFiles = [];
22+
foreach (scandir($jsonDir) as $filename) {
2223
// Match files like fail2ban-events-YYYYMMDD.json
23-
if (!preg_match('/^fail2ban-events-(\d{8})\.json$/', $filename, $matches)) {
24-
return false;
24+
if (preg_match('/^fail2ban-events-(\d{8})\.json$/', $filename, $matches)) {
25+
$matchedFiles[] = [
26+
'filename' => $filename,
27+
'date' => $matches[1], // Extracted date as string YYYYMMDD
28+
];
2529
}
30+
}
2631

27-
// Extract date from filename
28-
$fileDate = DateTime::createFromFormat('Ymd', $matches[1]);
29-
if (!$fileDate) {
30-
return false;
31-
}
32-
33-
$now = new DateTime();
34-
$interval = $now->diff($fileDate);
35-
36-
// Include only files not in the future and within maxDays
37-
return ($fileDate <= $now) && ($interval->days < $maxDays);
32+
// Sort files by date descending (newest first)
33+
usort($matchedFiles, function($a, $b) {
34+
return strcmp($b['date'], $a['date']); // descending order by date string
3835
});
3936

40-
// Sort files descending (newest first)
41-
rsort($files);
37+
// Take only the latest $maxDays files
38+
$latestFiles = array_slice($matchedFiles, 0, $maxDays);
39+
40+
// Extract only the filenames for JavaScript consumption
41+
$files = array_column($latestFiles, 'filename');
4242

43-
// Prepare JSON string for JavaScript consumption
43+
// Encode the list of files as JSON for frontend use
4444
$filesJson = json_encode(array_values($files));

0 commit comments

Comments
 (0)