enforce(RateLimiter::identifier(), 'login'); // تتبع المحاولات الفاشلة لكل حساب if ($redis && !empty($phone)) { $accountKey = "login_attempts:account:" . hash('sha256', $phone); $accountAttempts = (int) $redis->get($accountKey); if ($accountAttempts >= 5) { $ttl = $redis->ttl($accountKey); $waitMinutes = ceil($ttl / 60); jsonError("تم تعليق تسجيل الدخول لهذا الحساب مؤقتاً. يرجى المحاولة بعد {$waitMinutes} دقيقة."); exit; } } // البحث عن المشرف باستخدام بصمة الجهاز (Fingerprint Hash) $fpHash = hash('sha256', $fingerprint); $isTrustedDevice = false; // تسجيل محاولة تسجيل الدخول للتدقيق $loginAuditData = [ 'ip' => $_SERVER['REMOTE_ADDR'] ?? 'unknown', 'fingerprint_hash' => $fpHash, 'phone_hash' => !empty($phone) ? hash('sha256', $phone) : null, 'timestamp' => date('Y-m-d H:i:s'), 'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'unknown', 'result' => 'pending' ]; error_log("[LOGIN_AUDIT] " . json_encode($loginAuditData)); try { $con = Database::get('main'); $stmt = $con->prepare("SELECT * FROM adminUser WHERE fingerprint_hash = :fp LIMIT 1"); $stmt->execute([':fp' => $fpHash]); $admin = $stmt->fetch(PDO::FETCH_ASSOC); if ($admin) { $isTrustedDevice = true; } else if (!empty($phone)) { // 1. بحث مباشر بالـ ID المعياري Unencrypted $stmtId = $con->prepare("SELECT * FROM adminUser WHERE id = :id LIMIT 1"); $stmtId->execute([':id' => $phone]); $admin = $stmtId->fetch(PDO::FETCH_ASSOC); // 2. إذا لم يتم العثور بالـ ID، نفحص الحقول المشفّرة (email / phone / name) عبر فك التشفير if (!$admin) { $stmtAll = $con->query("SELECT * FROM adminUser"); while ($row = $stmtAll->fetch(PDO::FETCH_ASSOC)) { $decPhone = ($encryptionHelper && !empty($row['phone'])) ? $encryptionHelper->decryptData($row['phone']) : $row['phone']; $decEmail = ($encryptionHelper && !empty($row['email'])) ? $encryptionHelper->decryptData($row['email']) : $row['email']; $decName = ($encryptionHelper && !empty($row['name'])) ? $encryptionHelper->decryptData($row['name']) : $row['name']; if ($phone === $decPhone || $phone === $decEmail || $phone === $decName || $phone === $row['phone'] || $phone === $row['email']) { $admin = $row; break; } } } // فحص ما إذا كانت بصمة الجهاز محفوظة ومطابقة للجهاز الحالي if ($admin && !empty($admin['fingerprint_hash']) && hash_equals($admin['fingerprint_hash'], $fpHash)) { $isTrustedDevice = true; } } if ($admin) { // 1. التحقق من حالة الحساب if (isset($admin['status'])) { if ($admin['status'] === 'pending') { jsonError("حسابك قيد المراجعة حالياً. يرجى الانتظار للموافقة."); exit; } elseif ($admin['status'] === 'suspended') { jsonError("هذا الحساب معلق. يرجى التواصل مع المدير."); exit; } elseif ($admin['status'] === 'rejected') { jsonError("تم رفض طلب الانضمام لهذا الحساب."); exit; } } // 2. التحقق من كلمة المرور if (password_verify($password, $admin['password'])) { // إذا كان الجهاز موثوقاً (البصمة محفوظة ومطابقة) أو طلب تجديد توكن تلقائي if ($isTrustedDevice || $isRenewal) { $encFpRaw = $encryptionHelper ? $encryptionHelper->encryptData($fingerprint) : $fingerprint; $updateStmt = $con->prepare("UPDATE adminUser SET fingerprint = :fp_raw, fingerprint_hash = :fp WHERE id = :id"); $updateStmt->execute([ ':fp_raw' => $encFpRaw, ':fp' => $fpHash, ':id' => $admin['id'] ]); $admin['fingerprint_hash'] = $fpHash; $jwtService = new JwtService($redis); $role = $admin['role'] ?? 'admin'; if ($redis) { $oldJti = $redis->get("active_jti:" . $admin['id']); if ($oldJti) { $jwtService->revokeToken($oldJti, 3600); } } $jwt = $jwtService->generateAccessToken($admin['id'], $role, $audience, $fingerprint); if ($encryptionHelper && !empty($admin['name'])) { $admin['name'] = $encryptionHelper->decryptData($admin['name']) ?: $admin['name']; } unset($admin['password']); printSuccess([ "message" => "Login successful", "admin" => $admin, "jwt" => $jwt, "expires_in" => 3600 ]); exit; } // 3. توليد رمز تحقق OTP (3 أرقام) وإرساله عبر نظام OTP الموحد (Nabeh API للواتساب) $otp = (string)random_int(100, 999); $encryptedPhone = $admin['phone'] ?? ''; $rawPhone = ($encryptionHelper && !empty($encryptedPhone)) ? $encryptionHelper->decryptData($encryptedPhone) : $encryptedPhone; if (!$rawPhone || empty($rawPhone)) { $rawPhone = $encryptedPhone; } // تحميل موزع خدمات OTP عبر Nabeh API require_once __DIR__ . '/../../auth/otp/providers.php'; $success = false; if (function_exists('sendNabehOtp')) { $success = sendNabehOtp($rawPhone, $otp, 'whatsapp', 'admin'); } // تخزين OTP (SHA-256 hash) في جدول token_verification_admin $otpHash = hash('sha256', $otp); $stmt = $con->prepare("INSERT INTO token_verification_admin (phone_number, token, expiration_time) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 10 MINUTE)) ON DUPLICATE KEY UPDATE token = VALUES(token), expiration_time = VALUES(expiration_time)"); $stmt->execute([$encryptedPhone, $otpHash]); $maskedPhone = (strlen($rawPhone) > 7) ? substr($rawPhone, 0, 4) . '****' . substr($rawPhone, -3) : $rawPhone; printSuccess([ "status" => "otp_required", "message" => $success ? "تم إرسال رمز التحقق إلى WhatsApp الخاص بك." : "فشل إرسال واتساب. تحقق من error_log لمعرفة OTP.", "phone" => $maskedPhone ]); exit; } else { jsonError("كلمة المرور غير صحيحة."); } } else { jsonError("الحساب غير موجود. يرجى التأكد من اسم المستخدم أو البريد الإلكتروني وكلمة المرور."); } } catch (Throwable $e) { error_log("[Admin Login Throwable Error] " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString()); jsonError("حدث خطأ في السيرفر: " . $e->getMessage(), 500); }