<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'sekolah_app';

$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
    die("Koneksi gagal: " . mysqli_connect_error());
}

// Session settings untuk Webuzo
ini_set('session.gc_maxlifetime', 7200);
ini_set('session.cookie_lifetime', 7200);
ini_set('session.cookie_httponly', 1);
ini_set('session.use_only_cookies', 1);

// Gunakan session name yang unik
session_name('SIM_ISTIQLAL');

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Fungsi cek akses
function cek_akses($roles_izinkan) {
    if(!isset($_SESSION['user'])) {
        header("Location: index.php?error=session_expired");
        exit();
    }
    if(!in_array($_SESSION['role'], $roles_izinkan)) {
        echo "<script>alert('Akses ditolak!'); window.location='dashboard.php';</script>";
        exit();
    }
}

function filter_by_unit($column = 'unit') {
    if(in_array($_SESSION['role'], ['Kepala', 'Guru'])) {
        return " AND $column = '{$_SESSION['unit']}' ";
    }
    return "";
}
?>
