Files
Siro/backend/connect.php
2026-06-29 00:07:33 +03:00

43 lines
1.5 KiB
PHP

<?php
// ============================================================
// connect.php (النسخة الحديثة)
// بوابة التطبيقات (تستلزم JWT)
// ============================================================
require_once __DIR__ . '/core/bootstrap.php';
// --------- تحقق من تطابق المنطقة (Region Mismatch Guard) ---------
$requestHost = $_SERVER['HTTP_HOST'] ?? '';
$appDomain = getenv('APP_DOMAIN') ?: '';
if (!empty($appDomain) && !empty($requestHost) && strtolower($requestHost) !== strtolower($appDomain)) {
if ($requestHost !== 'localhost' && !preg_match('/^127\.0\.0\./', $requestHost)) {
http_response_code(400);
exit(json_encode([
'status' => 'failure',
'error_code' => 'REGION_MISMATCH',
'message' => "Region mismatch: Request host '$requestHost' does not match server domain '$appDomain'."
]));
}
}
require_once __DIR__ . '/functions.php';
// 1. Rate Limiting and JWT Authentication
$limiter = new RateLimiter($redis);
$limiter->enforce(RateLimiter::identifier(), 'api');
$jwtService = new JwtService($redis);
$decoded = $jwtService->authenticate();
// متغيرات مساعدة للمطور
$user_id = $decoded->user_id ?? null;
$role = $decoded->role ?? 'passenger';
// 3. Database Connection
try {
$con = Database::get('main');
} catch (Exception $e) {
http_response_code(500);
exit(json_encode(['error' => 'Database connection failed']));
}