Initial commit with updated Auth and media ignored
This commit is contained in:
90
core/bootstrap.php
Normal file
90
core/bootstrap.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// core/bootstrap.php
|
||||
// البوابة الرئيسية الموحدة لكل التطبيق
|
||||
// ============================================================
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// 1. إعدادات الأخطاء والـ Headers الأساسية
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '0');
|
||||
ini_set('log_errors', '1');
|
||||
|
||||
// تحديد مسار اللوج بشكل ديناميكي (محلياً أو سيرفر)
|
||||
$logPath = '/home/intaleq-api/logs/php_errors.log';
|
||||
if (!file_exists(dirname($logPath)) || !is_writable(dirname($logPath))) {
|
||||
$logPath = __DIR__ . '/../logs/php_errors.log';
|
||||
}
|
||||
ini_set('error_log', $logPath);
|
||||
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
header('X-Frame-Options: DENY');
|
||||
header('Strict-Transport-Security: max-age=31536000; includeSubDomains');
|
||||
|
||||
// CORS (يجب تخصيصه في endpoints مخصصة إن لزم، لكن هذا افتراضي)
|
||||
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Device-FP, X-HMAC-Auth, X-Internal-Key');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 2. Autoload
|
||||
$vendorPath = realpath(__DIR__ . '/../../vendor/autoload.php');
|
||||
if ($vendorPath) require_once $vendorPath;
|
||||
|
||||
// 3. Helpers & Env
|
||||
require_once __DIR__ . '/helpers.php';
|
||||
|
||||
// تحديد مسار الـ .env بشكل ديناميكي
|
||||
$envFile = '/home/intaleq-api/env/.env';
|
||||
if (!file_exists($envFile)) {
|
||||
$envFile = __DIR__ . '/../.env'; // مسار محلي افتراضي
|
||||
}
|
||||
loadEnvironment($envFile);
|
||||
|
||||
// 4. Redis Connection (Singleton)
|
||||
$redis = null;
|
||||
try {
|
||||
if (extension_loaded('redis')) {
|
||||
$redis = new Redis();
|
||||
$redisHost = getenv('REDIS_HOST') ?: '127.0.0.1';
|
||||
$redisPort = (int)(getenv('REDIS_PORT') ?: 6379);
|
||||
$redisPass = getenv('REDIS_PASSWORD');
|
||||
|
||||
if ($redis->connect($redisHost, $redisPort, 1.5)) {
|
||||
if ($redisPass) $redis->auth($redisPass);
|
||||
$redis->setOption(Redis::OPT_PREFIX, 'intaleq:');
|
||||
} else {
|
||||
$redis = null;
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("[REDIS] Connection failed: " . $e->getMessage());
|
||||
$redis = null;
|
||||
}
|
||||
|
||||
// 5. تحميل الـ Services الأساسية
|
||||
require_once __DIR__ . '/Security/EncryptionHelper.php';
|
||||
require_once __DIR__ . '/Database/Database.php';
|
||||
require_once __DIR__ . '/Auth/RateLimiter.php';
|
||||
require_once __DIR__ . '/Auth/JwtService.php';
|
||||
// لا نحمّل OtpService و FcmService إلا عند الحاجة (Lazy)
|
||||
|
||||
// 6. تهيئة Encryption Helper العام (للتوافقية)
|
||||
// يتم استخدام .enckey (32 بايت) لتشفير البيانات
|
||||
$encKey = trim(@file_get_contents('/home/intaleq-api/.enckey') ?: '');
|
||||
if (!$encKey) {
|
||||
$encKey = getenv('ENC_KEY') ?: '';
|
||||
}
|
||||
|
||||
if (!$encKey || strlen($encKey) !== 32) {
|
||||
error_log("[FATAL] Encryption key (.enckey) is missing or invalid length (must be 32 bytes).");
|
||||
http_response_code(500);
|
||||
exit(json_encode(['error' => 'Server configuration error: Encryption key issue']));
|
||||
}
|
||||
|
||||
$encryptionHelper = new EncryptionHelper($encKey);
|
||||
Reference in New Issue
Block a user