105 lines
3.4 KiB
PHP
105 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Saqel Application Bootstrap Loader
|
|
* Handles PSR-4 Autoloading, security settings, and strict error handling.
|
|
*/
|
|
|
|
// Define absolute path to application root (saqel/backend)
|
|
define('APP_ROOT', dirname(__DIR__));
|
|
|
|
// 1. PSR-4 Autoloader
|
|
spl_autoload_register(function ($class) {
|
|
$prefix = 'App\\';
|
|
$base_dir = APP_ROOT . '/app/';
|
|
|
|
$len = strlen($prefix);
|
|
if (strncmp($prefix, $class, $len) !== 0) {
|
|
return;
|
|
}
|
|
|
|
$relative_class = substr($class, $len);
|
|
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
|
|
|
|
if (file_exists($file)) {
|
|
require_once $file;
|
|
}
|
|
});
|
|
|
|
// 2. Load Environment Variables with Multi-Path Fallback
|
|
try {
|
|
$candidatePaths = [
|
|
'/home/intaleqapp-saqel/.env',
|
|
'/home/intaleqapp-saqel/htdocs/saqel.intaleqapp.com/.env',
|
|
'/home/intaleqapp-saqel/htdocs/saqel.intaleqapp.com/saqel/.env',
|
|
'/home/intaleqapp-saqel/htdocs/saqel.intaleqapp.com/saqel/backend/.env',
|
|
APP_ROOT . '/.env',
|
|
APP_ROOT . '/../.env'
|
|
];
|
|
|
|
$loaded = false;
|
|
foreach ($candidatePaths as $path) {
|
|
if (file_exists($path)) {
|
|
define('LOADED_ENV_PATH', $path);
|
|
\App\Core\Env::load($path);
|
|
$loaded = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$loaded) {
|
|
define('LOADED_ENV_PATH', 'NOT_FOUND');
|
|
error_log("⚠️ [Env Warning] .env file not found in any candidate path");
|
|
}
|
|
} catch (\Exception $e) {
|
|
error_log('Env Load Error: ' . $e->getMessage());
|
|
if (!defined('LOADED_ENV_PATH')) {
|
|
define('LOADED_ENV_PATH', 'ERROR: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
// 3. Configure Error Reporting based on environment
|
|
$isDebug = filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
if ($isDebug) {
|
|
ini_set('display_errors', '1');
|
|
ini_set('display_startup_errors', '1');
|
|
error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
|
|
} else {
|
|
ini_set('display_errors', '0');
|
|
error_reporting(0);
|
|
}
|
|
|
|
// 4. Global Uncaught Exception Handler (JSON for APIs / Clean HTML for web)
|
|
set_exception_handler(function (\Throwable $e) {
|
|
$isDebug = filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
error_log('[EXCEPTION] ' . get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
|
|
|
|
$isApi = str_starts_with($_SERVER['REQUEST_URI'] ?? '', '/api');
|
|
|
|
if (!headers_sent()) {
|
|
http_response_code(500);
|
|
if ($isApi) {
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
} else {
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
}
|
|
}
|
|
|
|
if ($isApi) {
|
|
$body = [
|
|
'status' => 'error',
|
|
'message' => $isDebug ? $e->getMessage() : 'حدث خطأ غير متوقع في الخادم',
|
|
'debug' => $isDebug ? [
|
|
'exception' => get_class($e),
|
|
'file' => $e->getFile(),
|
|
'line' => $e->getLine(),
|
|
] : null
|
|
];
|
|
echo json_encode($body, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
} else {
|
|
echo "<!DOCTYPE html><html dir='rtl' lang='ar'><head><meta charset='UTF-8'><title>خطأ في الخادم</title><style>body{font-family:sans-serif;background:#0B132B;color:#fff;padding:40px;text-align:center;}</style></head><body><h2>⚠️ حدث خطأ في الخادم</h2><p>" . htmlspecialchars($e->getMessage()) . "</p></body></html>";
|
|
}
|
|
exit(1);
|
|
});
|