| 
17 | 17 | 
 
  | 
18 | 18 | $jsonDir = dirname(__DIR__) . '/archive/';  | 
19 | 19 | 
 
  | 
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) {  | 
22 | 23 |     // 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 | +        ];  | 
25 | 29 |     }  | 
 | 30 | +}  | 
26 | 31 | 
 
  | 
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  | 
38 | 35 | });  | 
39 | 36 | 
 
  | 
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');  | 
42 | 42 | 
 
  | 
43 |  | -// Prepare JSON string for JavaScript consumption  | 
 | 43 | +// Encode the list of files as JSON for frontend use  | 
44 | 44 | $filesJson = json_encode(array_values($files));  | 
0 commit comments