<?php
// config.php

$host = 'localhost';
$username = 'smah6429_usedbsmansab';
$password = 'userdbsmansab#1';
$database = 'smah6429_smansab';

// Koneksi offline
//$host = 'localhost';
//$username = 'root';
//$password = 'root';
//$database = 'smansab_lms2';

$conn = new mysqli($host, $username, $password, $database);

if ($conn->connect_error) {
    die("Koneksi database gagal: " . $conn->connect_error);
}

session_start();

// Fungsi untuk mencegah SQL injection
function sanitize($data) {
    global $conn;
    return mysqli_real_escape_string($conn, trim($data));
}

// Fungsi untuk redirect
function redirect($url) {
    header("Location: $url");
    exit();
}

// Fungsi untuk mengecek login
function isLoggedIn() {
    return isset($_SESSION['user_id']);
}

// Fungsi untuk mendapatkan role user
function getUserRole() {
    return $_SESSION['role'] ?? null;
}

// Fungsi untuk mengecek role
function isAdmin() {
    return getUserRole() === 'admin';
}

function isGuru() {
    return getUserRole() === 'guru';
}

function isSiswa() {
    return getUserRole() === 'siswa';
}
?>