<?php
// config/library.php
// Fungsi umum untuk SISFO Akademik

// Anti-injection aman
if (!function_exists('anti_injection')) {
    function anti_injection($conn, $data){
        return mysqli_real_escape_string($conn, $data);
    }
}

// Menghitung rata-rata array
if (!function_exists('average')) {
    function average($arr){
        if (!is_array($arr)) return false;
        return array_sum($arr)/count($arr);
    }
}

// Cek session untuk admin
if (!function_exists('cek_session_admin')) {
    function cek_session_admin(){
        $level = $_SESSION['level'] ?? '';
        if ($level != 'superuser' && $level != 'kepala'){
            echo "<script>document.location='index.php';</script>";
            exit;
        }
    }
}

// Cek session untuk guru
if (!function_exists('cek_session_guru')) {
    function cek_session_guru(){
        $level = $_SESSION['level'] ?? '';
        if ($level != 'guru' && $level != 'superuser' && $level != 'kepala'){
            echo "<script>document.location='index.php';</script>";
            exit;
        }
    }
}

// Cek session untuk siswa
if (!function_exists('cek_session_siswa')) {
    function cek_session_siswa(){
        $level = $_SESSION['level'] ?? '';
        if ($level == ''){
            echo "<script>document.location='index.php';</script>";
            exit;
        }
    }
}
?>
