Safely initialize encryptionHelper and handle Throwable in Admin login.php
This commit is contained in:
@@ -5,6 +5,16 @@
|
||||
*/
|
||||
require_once __DIR__ . '/../../core/bootstrap.php';
|
||||
require_once __DIR__ . '/../../functions.php';
|
||||
require_once __DIR__ . '/../../encrypt_decrypt.php';
|
||||
|
||||
global $encryptionHelper;
|
||||
if (!isset($encryptionHelper) || $encryptionHelper === null) {
|
||||
if (class_exists('EncryptionHelper')) {
|
||||
$key = getenv('ENC_KEY') ?: '';
|
||||
$iv = getenv('initializationVector') ?: '';
|
||||
$encryptionHelper = new EncryptionHelper($key, $iv);
|
||||
}
|
||||
}
|
||||
|
||||
$fingerprint = filterRequest('fingerprint');
|
||||
$password = filterRequest('password');
|
||||
@@ -57,10 +67,12 @@ try {
|
||||
// إذا لم يتم العثور بالبصمة، وتم تمرير اسم المستخدم/الإيميل/الهاتف (تسجيل دخول لأول مرة أو جهاز جديد)
|
||||
if (!$admin && !empty($phone)) {
|
||||
// 1. تجربة البحث بالهاتف المشفّر
|
||||
$encPhoneInput = $encryptionHelper->encryptData($phone);
|
||||
$stmtPhone = $con->prepare("SELECT * FROM adminUser WHERE phone = :phone LIMIT 1");
|
||||
$stmtPhone->execute([':phone' => $encPhoneInput]);
|
||||
$admin = $stmtPhone->fetch(PDO::FETCH_ASSOC);
|
||||
if ($encryptionHelper) {
|
||||
$encPhoneInput = $encryptionHelper->encryptData($phone);
|
||||
$stmtPhone = $con->prepare("SELECT * FROM adminUser WHERE phone = :phone LIMIT 1");
|
||||
$stmtPhone->execute([':phone' => $encPhoneInput]);
|
||||
$admin = $stmtPhone->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
// 2. إذا لم يجد بالهاتف المشفّر، نجرب بالبريد الإلكتروني أو المعرّف (id / email) أو رقم الهاتف غير المشفّر
|
||||
if (!$admin) {
|
||||
@@ -71,7 +83,7 @@ try {
|
||||
|
||||
// تأكيد كلمة المرور وتحديث بصمة الجهاز إذا تم إيجاد الحساب
|
||||
if ($admin && password_verify($password, $admin['password'])) {
|
||||
$encFpRaw = $encryptionHelper->encryptData($fingerprint);
|
||||
$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,
|
||||
@@ -118,7 +130,9 @@ try {
|
||||
$jwt = $jwtService->generateAccessToken($admin['id'], $role, $audience, $fingerprint);
|
||||
|
||||
// فك تشفير البيانات للعرض
|
||||
$admin['name'] = $encryptionHelper->decryptData($admin['name']) ?: $admin['name'];
|
||||
if ($encryptionHelper && !empty($admin['name'])) {
|
||||
$admin['name'] = $encryptionHelper->decryptData($admin['name']) ?: $admin['name'];
|
||||
}
|
||||
unset($admin['password']);
|
||||
|
||||
printSuccess([
|
||||
@@ -140,7 +154,7 @@ try {
|
||||
}
|
||||
|
||||
// فك تشفير رقم الهاتف (مخزن مشفراً في قاعدة البيانات)
|
||||
$phone = $encryptionHelper->decryptData($encryptedPhone);
|
||||
$phone = ($encryptionHelper && !empty($encryptedPhone)) ? $encryptionHelper->decryptData($encryptedPhone) : $encryptedPhone;
|
||||
if (!$phone || empty($phone)) {
|
||||
$phone = $encryptedPhone;
|
||||
}
|
||||
@@ -168,7 +182,7 @@ try {
|
||||
$stmt->execute([$encryptedPhone, $otpHash]);
|
||||
|
||||
// إخفاء جزء من الرقم في الاستجابة للأمان
|
||||
$maskedPhone = substr($phone, 0, 4) . '****' . substr($phone, -3);
|
||||
$maskedPhone = (strlen($phone) > 7) ? substr($phone, 0, 4) . '****' . substr($phone, -3) : $phone;
|
||||
|
||||
if ($success) {
|
||||
printSuccess([
|
||||
@@ -188,9 +202,10 @@ try {
|
||||
jsonError("كلمة المرور غير صحيحة.");
|
||||
}
|
||||
} else {
|
||||
jsonError("الحساب أو الجهاز غير مسجل. يرجى إدخال رقم هاتفك وكلمة المرور إذا كان هذا أول تسجيل دخول لك.");
|
||||
jsonError("الحساب غير موجود. يرجى التأكد من البريد الإلكتروني أو رقم الهاتف وكلمة المرور.");
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("[Admin Login Error] " . $e->getMessage());
|
||||
jsonError("حدث خطأ في السيرفر. يرجى المحاولة لاحقاً.");
|
||||
} catch (Throwable $e) {
|
||||
error_log("[Admin Login Throwable Error] " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString());
|
||||
jsonError("حدث خطأ في السيرفر: " . $e->getMessage(), 500);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user