26 lines
1.0 KiB
PHP
26 lines
1.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
/**
|
|
* Logs an audit event.
|
|
*
|
|
* @param string $action The action performed (e.g., 'login_success', 'credential_create').
|
|
* @param int|null $userId The ID of the user who performed the action. Can be null.
|
|
* @param string|null $targetType The type of object the action was performed on (e.g., 'client', 'credential').
|
|
* @param int|null $targetId The ID of the object.
|
|
*/
|
|
function log_audit_event(string $action, ?int $userId, ?string $targetType = null, ?int $targetId = null)
|
|
{
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare(
|
|
"INSERT INTO audit_events (user_id, action, target_type, target_id) VALUES (?, ?, ?, ?)"
|
|
);
|
|
$stmt->execute([$userId, $action, $targetType, $targetId]);
|
|
} catch (PDOException $e) {
|
|
// In a real application, you would log this error to a file or monitoring service.
|
|
// For this example, we'll fail silently to not disrupt the user experience.
|
|
error_log('Audit log failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
?>
|