87 lines
3.4 KiB
PHP
87 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Admin/auth/register.php
|
|
* التسجيل الذاتي للمشرفين (Admins) مع التحقق من الصلاحيات من ملف .env
|
|
*/
|
|
require_once __DIR__ . '/../../core/bootstrap.php';
|
|
require_once __DIR__ . '/../../functions.php';
|
|
|
|
$name = filterRequest('name');
|
|
$phone = filterRequest('phone');
|
|
$password = filterRequest('password');
|
|
$fingerprint = filterRequest('fingerprint');
|
|
|
|
if (empty($name) || empty($phone) || empty($password) || empty($fingerprint)) {
|
|
jsonError("جميع الحقول مطلوبة بما فيها بصمة الجهاز.");
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
// 1. التحقق من البيئة (Environment Whitelist)
|
|
$allowedPhonesStr = getenv('AUTHORIZED_ADMIN_PHONES');
|
|
if (!$allowedPhonesStr) {
|
|
// في حال لم يتم إعداد المتغير، نرفض الجميع للأمان
|
|
jsonError("غير مصرح لك بالتسجيل كمشرف (القائمة البيضاء غير معدة).");
|
|
exit;
|
|
}
|
|
|
|
$allowedPhones = array_map('trim', explode(',', $allowedPhonesStr));
|
|
if (!in_array($phone, $allowedPhones)) {
|
|
jsonError("أنت غير مصرح لك بالتسجيل كمشرف. يرجى مراجعة الإدارة.");
|
|
exit;
|
|
}
|
|
|
|
$con = Database::get('main');
|
|
|
|
// 2. التحقق من عدم وجود الحساب مسبقاً (عن طريق الهاتف أو البصمة)
|
|
$fpHash = hash('sha256', $fingerprint);
|
|
$encPhoneInput = $encryptionHelper->encryptData($phone);
|
|
|
|
$check = $con->prepare("SELECT id FROM adminUser WHERE phone = ? OR fingerprint_hash = ? LIMIT 1");
|
|
$check->execute([$encPhoneInput, $fpHash]);
|
|
|
|
if ($check->rowCount() > 0) {
|
|
jsonError("رقم الهاتف أو الجهاز مسجل مسبقاً.");
|
|
exit;
|
|
}
|
|
|
|
// 3. تجهيز البيانات
|
|
$uniqueId = bin2hex(random_bytes(16)); // UUID آمن (32 حرف hex عشوائي)
|
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
$encName = $encryptionHelper->encryptData($name);
|
|
$encPhone = $encPhoneInput;
|
|
$encFp = $encryptionHelper->encryptData($fingerprint);
|
|
|
|
// التأكد من وجود عمود phone و status في الجدول
|
|
try {
|
|
$con->exec("ALTER TABLE adminUser ADD COLUMN phone VARCHAR(255) NULL AFTER name");
|
|
$con->exec("ALTER TABLE adminUser ADD COLUMN status VARCHAR(50) DEFAULT 'pending' AFTER role");
|
|
} catch (Exception $e) { /* الأعمدة موجودة مسبقاً */ }
|
|
|
|
// 4. الإدخال في قاعدة البيانات بحالة pending
|
|
$sql = "INSERT INTO adminUser (id, fingerprint, fingerprint_hash, name, phone, password, role, status, created_at)
|
|
VALUES (:id, :fp, :fp_hash, :name, :phone, :pass, 'admin', 'pending', NOW())";
|
|
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute([
|
|
':id' => $uniqueId,
|
|
':fp' => $encFp,
|
|
':fp_hash' => $fpHash,
|
|
':name' => $encName,
|
|
':phone' => $encPhone,
|
|
':pass' => $hashedPassword
|
|
]);
|
|
|
|
printSuccess([
|
|
"status" => "pending",
|
|
"message" => "تم تسجيل حسابك بنجاح وهو الآن قيد المراجعة. يرجى انتظار تفعيل المشرف العام."
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("[Admin Register Error] " . $e->getMessage());
|
|
jsonError("خطأ في السيرفر: " . $e->getMessage());
|
|
}
|
|
|
|
exit();
|