<?php
// TEMPORARY ERROR CATCHER - DELETE AFTER FIXING
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php_error.log');

// Catch all errors
set_error_handler(function($errno, $errstr, $errfile, $errline) {
    $msg = "Error [$errno]: $errstr in $errfile on line $errline\n";
    file_put_contents(__DIR__ . '/php_error.log', date('Y-m-d H:i:s') . " - " . $msg, FILE_APPEND);
    echo "<pre style='background:red;color:white;padding:20px;'>$msg</pre>";
});

// Catch fatal errors
register_shutdown_function(function() {
    $error = error_get_last();
    if ($error !== NULL && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
        $msg = "Fatal Error [{$error['type']}]: {$error['message']} in {$error['file']} on line {$error['line']}\n";
        file_put_contents(__DIR__ . '/php_error.log', date('Y-m-d H:i:s') . " - " . $msg, FILE_APPEND);
        echo "<pre style='background:red;color:white;padding:20px;'>$msg</pre>";
    }
});

// Only show HTML comment if NOT an AJAX request
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
          strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';

$isJsonRequest = (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) ||
                 (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) ||
                 strpos($_SERVER['REQUEST_URI'], '/get_') !== false ||
                 strpos($_SERVER['REQUEST_URI'], '_ajax') !== false ||
                 strpos($_SERVER['REQUEST_URI'], '_data') !== false;

if (!$isAjax && !$isJsonRequest) {
    echo "<!-- Error catcher active -->\n";
}
?>
