<?php
// Core utility functions
if (!defined("_VALID_ACCESS")) { exit("Direct access not allowed"); }

function sanitize_input($input) {
    if (is_array($input)) {
        return array_map("sanitize_input", $input);
    }
    return htmlspecialchars(trim($input), ENT_QUOTES, "UTF-8");
}

function redirect($url, $code = 302) {
    header("Location: " . $url, true, $code);
    exit();
}

function json_response($data, $code = 200) {
    http_response_code($code);
    header("Content-Type: application/json");
    echo json_encode($data);
    exit();
}

function error_response($message, $code = 400) {
    json_response(["error" => $message], $code);
}

function success_response($data = null, $message = "Success") {
    $response = ["success" => true, "message" => $message];
    if ($data !== null) {
        $response["data"] = $data;
    }
    json_response($response);
}
