Priority File Manager

📁 public_html
Base Directory:
/home/ecedu/public_html/cce/api
NameTypeSizeActions
📁 .. Folder -
📄 about_controller.php File 1241
Edit Download
📄 ads_controller.php File 2645
Edit Download
📄 contact_controller.php File 1712
Edit Download
📄 data_controller.php File 3269
Edit Download
📄 error_log.txt File 1332
Edit Download
📄 header.php File 3994
Edit Download
📄 image_controller.php File 2510
Edit Download
📄 news_controller.php File 15794
Edit Download
📄 social_controller.php File 1717
Edit Download
📄 upload_pdf_thumbnail.php File 1844
Edit Download
📄 validate.php File 510
Edit Download

View File: header.php

<?php
// ========================
// إعداد عرض الأخطاء
// ========================
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// ========================
// منع أي إخراج قبل headers
// ========================
// تأكد أن هذا الملف يبدأ مباشرة بـ <?php بدون أي فراغ أو سطر

// ========================
// ملف سجل الأخطاء
// ========================
define('ERROR_LOG_FILE', __DIR__ . '/error_log.txt');

// ========================
// دالة لإرجاع الخطأ بصيغة JSON
// ========================
function returnErrorJson($message, $file = null, $line = null, $type = "ERROR", $trace = null) {
    if (!headers_sent()) {
        http_response_code(500);
        header('Content-Type: application/json; charset=UTF-8');
    }

    $errorData = [
        "status"  => "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