<?php 
// Matikan error reporting di production untuk mencegah informasi sensitif bocor
error_reporting(0);
ini_set('display_errors', 0);

ob_start();
session_start();

include_once 'sw-library/sw-config.php';
include_once 'sw-library/sw-function.php';
//ob_start("minify_html");

$dbhostsql      = DB_HOST;
$dbusersql      = DB_USER;
$dbpasswordsql  = DB_PASSWD;
$dbnamesql      = DB_NAME;
$connection     = mysqli_connect($dbhostsql, $dbusersql, $dbpasswordsql, $dbnamesql) or die("Koneksi database gagal.");

if (!isset($_COOKIE['COOKIES_MEMBER']) && !isset($_COOKIE['COOKIES_COOKIES'])) {
    //header('location:./login/');
} else {
    $COOKIES_COOKIES = '';
    $COOKIES_MEMBER  = '';
    
    if (!empty($_COOKIE['COOKIES_COOKIES'])) {
        $COOKIES_COOKIES = mysqli_real_escape_string($connection, $_COOKIE['COOKIES_COOKIES']);
    }
    if (!empty($_COOKIE['COOKIES_MEMBER'])) {
        $COOKIES_MEMBER  = epm_decode($_COOKIE['COOKIES_MEMBER']);
    }
    
    if (!empty($_COOKIE['date_v3'])) {
        $date_v3 = mysqli_real_escape_string($connection, $_COOKIE['date_v3']);
        $filter  = "AND presence_date='$date_v3'";
    } else {
        $safe_date = isset($date) ? mysqli_real_escape_string($connection, $date) : date('Y-m-d');
        $filter    = "AND presence_date_out='$safe_date'"; 
    }

    require_once 'sw-mod/out/sw-cookies.php';
    
    if (!empty($_COOKIE['date_v3'])) {
        $date_v3 = mysqli_real_escape_string($connection, $_COOKIE['date_v3']);
    }

    // Amankan ID user dari potensi SQL Injection
    $user_id = isset($row_user['id']) ? intval($row_user['id']) : 0;

    $query_absent  = "SELECT employees_id, time_in, time_out FROM presence WHERE employees_id='$user_id' $filter";
    $result_absent = $connection->query($query_absent);
    // $row_absent = $result_absent->fetch_assoc();
}

// Amankan pengambilan data dari $row_site agar tidak notice
$website_url      = isset($row_site['site_url']) ? $row_site['site_url'] : '';
$website_name     = isset($row_site['site_name']) ? $row_site['site_name'] : '';
$website_phone    = isset($row_site['site_phone']) ? $row_site['site_phone'] : '';
$website_addres   = isset($row_site['site_address']) ? $row_site['site_address'] : '';
$meta_description = isset($row_site['site_description']) ? $row_site['site_description'] : '';
$meta_keyword     = isset($row_site['site_description']) ? $row_site['site_description'] : '';
$website_logo     = isset($row_site['site_logo']) ? $row_site['site_logo'] : '';
$website_email    = isset($row_site['site_email']) ? $row_site['site_email'] : '';

if (!empty($_GET['alert'])) {
    $alert = mysqli_real_escape_string($connection, $_GET['alert']);
}

$messages = '';
if (!empty($_SESSION['messages'])) {
    $messages = $_SESSION['messages'];
}

// -------------------------------------------------------------------------
// PENGAMANAN MODUL (Mencegah Path Traversal / LFI tanpa membatasi nama menu)
// -------------------------------------------------------------------------
$mod = "home";
if (!empty($_GET['mod'])) {
    // Bersihkan karakter berbahaya untuk mencegah direktori traversal (seperti ../)
    $clean_mod = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_GET['mod']);
    if (!empty($clean_mod)) {
        $mod = $clean_mod;
    }
}

// Cek apakah file modulnya benar-benar ada di dalam folder sw-mod/
if (file_exists("sw-mod/$mod.php")) {
    require_once("sw-mod/$mod.php");
} else {
    require_once("sw-mod/home.php");
}
?>