Base Directory:
/home/ecedu/public_html/cce/api
"error",
"type" => $type,
"message" => $message,
"file" => $file,
"line" => $line,
];
if ($trace) {
$errorData["trace"] = $trace;
}
// سجل الخطأ في ملف log
$logEntry = "[" . date("Y-m-d H:i:s") . "] $type: $message in " . ($file ?? '') . " on line " . ($line ?? '') . "\nTrace:\n" . ($trace ? implode("\n", $trace) : '') . "\n\n";
file_put_contents(ERROR_LOG_FILE, $logEntry, FILE_APPEND | LOCK_EX);
echo json_encode($errorData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
// ========================
// التقاط الأخطاء العادية
// ========================
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
returnErrorJson(
$errstr,
$errfile ?? null,
$errline ?? null,
"PHP_ERROR",
array_map(function($t){
return ($t['file'] ?? '') . ':' . ($t['line'] ?? '');
}, $trace)
);
});
// ========================
// التقاط الاستثناءات غير الملتقطة
// ========================
set_exception_handler(function ($e) {
returnErrorJson(
$e->getMessage(),
$e->getFile() ?? null,
$e->getLine() ?? null,
"EXCEPTION",
explode("\n", $e->getTraceAsString())
);
});
// ========================
// التقاط الأخطاء القاتلة
// ========================
register_shutdown_function(function () {
$error = error_get_last();
if ($error && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
returnErrorJson(
$error['message'],
$error['file'] ?? null,
$error['line'] ?? null,
"FATAL_ERROR"
);
}
});
// ========================
// إعدادات CORS
// ========================
header("Access-Control-Allow-Origin: https://sj.madarik.org");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With, DeviceId");
// ========================
// دوال الردود
// ========================
function respondText(string $message, int $statusCode = 200): void {
if (!headers_sent()) {
http_response_code($statusCode);
header('Content-Type: text/plain; charset=UTF-8');
}
echo $message;
exit;
}
function respondJson(array $data, int $statusCode = 200): void {
if (!headers_sent()) {
http_response_code($statusCode);
header('Content-Type: application/json; charset=UTF-8');
}
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
// ========================
// مثال لتجربة الخطأ
// ========================
// echo $undefinedVar; // تجربة PHP_ERROR
// throw new Exception("تجربة EXCEPTION"); // تجربة EXCEPTION