<?php 
session_start();
date_default_timezone_set('Asia/Jakarta');
$id_login = $_SESSION['id'] ?? null;
$extplayer = $_SESSION['extplayer'] ?? null;
include 'function/connect.php'; 
$user_ip = $_SERVER['REMOTE_ADDR'];
$max_requests = 100;
$time_frame = 50;
$query_ip = mysqli_query($koneksi, "SELECT request_count, UNIX_TIMESTAMP(last_request) AS last_request_time FROM ip_requests WHERE ip_address = '$user_ip'");
if ($row = mysqli_fetch_array($query_ip)) {
    $last_request_time = $row['last_request_time'];
    $current_time = time();
    $time_diff = $current_time - $last_request_time;
    if ($time_diff < $time_frame) {
        $request_count = $row['request_count'] + 1;
        if ($request_count > $max_requests) {
            die("Anda telah melebihi batas permintaan. Silakan coba lagi nanti.");
        } else {
            mysqli_query($koneksi, "UPDATE ip_requests SET request_count = $request_count, last_request = NOW() WHERE ip_address = '$user_ip'");
        }
    } else {
        mysqli_query($koneksi, "UPDATE ip_requests SET request_count = 1, last_request = NOW() WHERE ip_address = '$user_ip'");
    }
} else {
    mysqli_query($koneksi, "INSERT INTO ip_requests (ip_address, request_count, last_request) VALUES ('$user_ip', 1, NOW())");
}
$query_maintenance = mysqli_query($koneksi, "SELECT mtweb FROM tb_web WHERE id = 1");
$maintenance = mysqli_fetch_array($query_maintenance);

if ($maintenance['mtweb'] == 'active') {
    header("Location: maintenance.php");
    exit();
}
$query1 = mysqli_query($koneksi, "SELECT active FROM tb_saldo WHERE id_user = '$extplayer' ");
$liat = mysqli_fetch_array($query1);
$query2 = mysqli_query($koneksi, "SELECT * FROM tb_user WHERE id = '$id_login' ");
$punya_user = mysqli_fetch_array($query2);
$query1010 = mysqli_query($koneksi, "SELECT * FROM tb_contact");
$ssa = mysqli_fetch_array($query1010);
$whatsapp = htmlspecialchars($ssa['no_whatsapp'] ?? '');
$id_livechat = htmlspecialchars($ssa['id_livechat'] ?? '');
$cuk = mysqli_query($koneksi, "SELECT * FROM tb_web");
$cek_web = mysqli_fetch_array($cuk);
$urlweb = htmlspecialchars($cek_web['url'] ?? '');
$logo = htmlspecialchars($cek_web['logo'] ?? '');
$min_depo = htmlspecialchars($cek_web['min_depo'] ?? '');
$min_wd = htmlspecialchars($cek_web['min_wd'] ?? '');
$icon = htmlspecialchars($cek_web['icon_web'] ?? '');
$title = htmlspecialchars($cek_web['title'] ?? '');
$deskripsi = htmlspecialchars($cek_web['deskripsi'] ?? '');
$keyword = htmlspecialchars($cek_web['keyword'] ?? '');
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo $title; ?> </title>                                  
    <meta name="description" content="<?php echo $deskripsi ?>">
    <meta name="keywords" content="<?php echo $keyword ?>">
    <meta property="og:description" content="<?php echo $deskripsi ?>" />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="<?php echo $urlweb ?>">
    <meta property="og:image" content="<?php echo $urlweb ?>/assets/img/<?php echo $logo ?>">
    <meta name="robots" content="index, follow">
    <meta name="author" content="<?php echo $urlweb ?>">
</head>
<body>
    <script>
        var userAgent = navigator.userAgent;
        var urlParams = new URLSearchParams(window.location.search);
        var reffParam = urlParams.get('reff');
        if (userAgent.match(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i)) {
            if (reffParam) {
                window.location.href = "mobile/index.php?page=daftar&reff=" + reffParam;
            } else {
                window.location.href = "mobile/index.php";
            }
        }
        else {
            if (reffParam) {
                window.location.href = "mobile/index.php?page=daftar&reff=" + reffParam;
            } else {
                window.location.href = "mobile/index.php";
            }
        }
    </script>
</body>
</html>