Skip to content

Commit

Permalink
ltb-project#128: Added audit log date-based retrieval limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
abpai94 committed Jul 30, 2024
1 parent 1c0fea1 commit 8cb1bca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
3 changes: 1 addition & 2 deletions htdocs/auditlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
require_once("../lib/date.inc.php");
require_once("../lib/audit.inc.php");

#TODO: Calculate getting logs for number of days
#TODO: Order based on newest to oldest
#TODO: Highlight user with Name and link rather than full CN
#TODO: Test comments and results to appear correctly

$entries = array();
[$entries,$nb_entries] = displayauditlog($audit_log_file);
[$entries,$nb_entries] = displayauditlog($audit_log_file, $audit_log_days);

if (!empty($entries)) {
$smarty->assign("page_title", "auditlogtitle");
Expand Down
2 changes: 1 addition & 1 deletion lang/en.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#==============================================================================

$messages['auditlogs'] = "Audit logs";
$messages['auditlogtitle'] = "Audit log for the last $auditlogdays days";
$messages['auditlogtitle'] = "Audit log for the last $audit_log_days days";
$messages['accountlocked'] = "Account is locked";
$messages['accountnotlocked'] = "Fail to lock account";
$messages['accountnotunlocked'] = "Fail to unlock account";
Expand Down
11 changes: 9 additions & 2 deletions lib/audit.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ function auditlog($file, $dn, $admin, $action, $result, $comment) {
file_put_contents($file, json_encode($log, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL, FILE_APPEND | LOCK_EX);
}

function displayauditlog($audit_log_file) {
function displayauditlog($audit_log_file, $audit_log_days) {

$entries = array();

#Date calculation to limit oldest audit logs
$olddatelog = new DateTime();
date_sub( $olddatelog, new DateInterval('P'.$audit_log_days.'D') );

foreach(file($audit_log_file) as $line) {
$json = json_decode($line, true);
array_push($entries, $json);
$logdate = DateTimeImmutable::createFromFormat("D, d M Y H:i:s", $json['date']);
if ($logdate > $olddatelog) {
array_push($entries, $json);
}
}

$nb_entries = sizeof($entries);
Expand Down

0 comments on commit 8cb1bca

Please sign in to comment.