Files
wevads-platform/scripts/framework_AuditLog.php
2026-02-26 04:53:11 +01:00

57 lines
2.7 KiB
PHP
Executable File

<?php
namespace IR\App\Helpers;
use PDO;
use PDOException;
class AuditLog {
private static $db = null;
private static function getDb() {
if (self::$db === null) {
try {
self::$db = new PDO("pgsql:host=localhost;dbname=adx_system", "postgres", "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
} catch (PDOException $e) { return null; }
}
return self::$db;
}
public static function log($action, $targetType = null, $targetId = null, $targetName = null, $details = []) {
$db = self::getDb();
if (!$db) { self::logToFile($action, $targetType, $targetId, $details); return; }
try {
$stmt = $db->prepare("INSERT INTO audit_logs (user_id, user_name, user_ip, action_type, target_type, target_id, target_name, details) VALUES (?,?,?,?,?,?,?,?)");
$stmt->execute([
$_SESSION['user_id'] ?? null,
$_SESSION['user_name'] ?? 'System',
$_SERVER['REMOTE_ADDR'] ?? 'CLI',
$action, $targetType, $targetId, $targetName,
!empty($details) ? json_encode($details) : null
]);
} catch (PDOException $e) { self::logToFile($action, $targetType, $targetId, $details); }
}
public static function create($type, $id, $name = null, $details = []) { self::log('CREATE', $type, $id, $name, $details); }
public static function update($type, $id, $name = null, $details = []) { self::log('UPDATE', $type, $id, $name, $details); }
public static function delete($type, $id, $name = null, $details = []) { self::log('DELETE', $type, $id, $name, $details); }
public static function login($userId, $userName) { self::log('LOGIN', 'auth', $userId, $userName); }
public static function logout($userId, $userName) { self::log('LOGOUT', 'auth', $userId, $userName); }
public static function getRecent($limit = 100) {
$db = self::getDb();
if (!$db) return [];
$stmt = $db->query("SELECT * FROM audit_logs ORDER BY action_time DESC LIMIT " . intval($limit));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
private static function logToFile($action, $type, $id, $details) {
$entry = date('Y-m-d H:i:s') . " | " . ($_SERVER['REMOTE_ADDR'] ?? 'CLI') . " | $action | $type:$id | " . json_encode($details) . "\n";
@file_put_contents('/opt/wevads/logs/audit.log', $entry, FILE_APPEND);
}
public static function registerLog($processId, $processType, $action, $status, $details = []) {
self::log($action, $processType, $processId, $status, is_array($details) ? $details : ['info' => $details]);
}
}