<?php
function sanitize_input($data) {
    global $conn;
    if (!$conn) {
        // Fallback if conn is not available
        return htmlspecialchars(stripslashes(trim($data)));
    }
    return mysqli_real_escape_string($conn, htmlspecialchars(stripslashes(trim($data))));
}

function check_login() {
    if (!isset($_SESSION['user_id'])) {
        header("Location: " . BASE_URL . "/index.php");
        exit();
    }
}

function check_role($role) {
    if (!isset($_SESSION['role']) || $_SESSION['role'] !== $role) {
        header("Location: " . BASE_URL . "/index.php");
        exit();
    }
}

// Encryption functions for hiding IDs in URL
const ENCRYPTION_KEY = 'worker_sec_key_wfsl'; 
const ENCRYPTION_METHOD = 'AES-128-CTR';
const ENCRYPTION_IV = '8765432109876543';

function encrypt_id($id) {
    $encrypted = openssl_encrypt($id, ENCRYPTION_METHOD, ENCRYPTION_KEY, 0, ENCRYPTION_IV);
    return bin2hex($encrypted); // Using hex to avoid URL encoding issues
}

function decrypt_id($encrypted_id) {
    $encrypted = hex2bin($encrypted_id);
    if ($encrypted === false) return false;
    return openssl_decrypt($encrypted, ENCRYPTION_METHOD, ENCRYPTION_KEY, 0, ENCRYPTION_IV);
}
?>
