Files
intaleq_v3_pure_php/Admin/v2/security/audit_logs.php
2026-05-10 02:12:00 +03:00

58 lines
2.3 KiB
PHP

<?php
// Admin/v2/security/audit_logs.php
// ── سجل تتبع ────────────────────────────────────────────
$debugFile = __DIR__ . '/../../../logs/audit_debug.txt';
$logDir = dirname($debugFile);
if (!is_dir($logDir)) @mkdir($logDir, 0777, true);
@file_put_contents($debugFile, "[" . date('Y-m-d H:i:s') . "] === REQUEST START ===\n", FILE_APPEND);
try {
require_once __DIR__ . '/../../../connect.php';
@file_put_contents($debugFile, " → connect.php OK. user_id=$user_id | role=$role\n", FILE_APPEND);
} catch (Exception $e) {
@file_put_contents($debugFile, " → connect.php FAILED: " . $e->getMessage() . "\n", FILE_APPEND);
http_response_code(500);
echo json_encode(['status' => 'failure', 'message' => 'connect failed: ' . $e->getMessage()]);
exit;
}
// ── فحص الصلاحيات ────────────────────────────────────────
if ($role !== 'super_admin' && $role !== 'admin') {
@file_put_contents($debugFile, " → BLOCKED: role=$role\n", FILE_APPEND);
jsonError("Unauthorized. role=$role", 403);
}
try {
$tableExists = $con->query("SHOW TABLES LIKE 'admin_audit_log'")->rowCount() > 0;
if (!$tableExists) {
@file_put_contents($debugFile, " → Table NOT FOUND\n", FILE_APPEND);
jsonSuccess([], 'Audit log table not found');
}
// إضافة COLLATE لحل تعارض الترميز بين جدول admin_audit_log و employee
$stmt = $con->prepare("
SELECT
l.id, l.admin_id, l.action, l.table_name, l.record_id, l.details, l.created_at,
e.name as admin_name
FROM admin_audit_log l
LEFT JOIN employee e ON l.admin_id COLLATE utf8mb4_general_ci = e.id COLLATE utf8mb4_general_ci
ORDER BY l.created_at DESC
LIMIT 100
");
$stmt->execute();
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$count = count($logs);
@file_put_contents($debugFile, " → SUCCESS: fetched $count logs\n", FILE_APPEND);
jsonSuccess($logs);
} catch (Exception $e) {
@file_put_contents($debugFile, " → QUERY ERROR: " . $e->getMessage() . "\n", FILE_APPEND);
jsonError('Query failed: ' . $e->getMessage(), 500);
}
?>