Files
2026-09-16 23:20:08 +07:00

127 lines
5.2 KiB
PHP

<?php
session_start();
if (empty($_SESSION['sess_userid']) || $_SESSION['sess_userid'] !== session_id()) {
header("Location: index.php");
exit();
}
$account = $_SESSION['account'];
$name = $_SESSION['name'];
$position = $_SESSION['position'];
$ip = $_SESSION['ip'];
$page_link = "2fa_authen.php";
$chk1 = preg_match('/itcenter/', $account);
if (!$chk1) {
echo "<center><font color='red'>ไม่มีสิทธิ์เข้าถึงหน้านี้</font></center>";
exit();
}
require_once("config/db.php");
require_once('inc/rfc6238.php');
$hos_user_2fa = $_POST['hos_user_2fa'] ?? '';
$save_2fa = $_POST['save_2fa'] ?? '';
$secretkey = '';
$hos_fullname = '';
$div_get_username = "block";
$div_gen_qrcode = "none";
if ($hos_user_2fa !== "") {
$div_get_username = "none";
$div_gen_qrcode = "block";
$secretkey = TokenAuth6238::generateRandomClue();
$stmt = $conn1->prepare("SELECT name AS hos_name_2fa FROM opduser WHERE loginname = ?");
if($stmt) {
$stmt->bind_param("s", $hos_user_2fa);
$stmt->execute();
$res = $stmt->get_result();
if($row = $res->fetch_assoc()) {
$hos_fullname = $row['hos_name_2fa'];
} else {
$hos_fullname = $hos_user_2fa; // fallback
}
$stmt->close();
}
}
if ($save_2fa === 'save_2fa') {
$hosxp_user = $_POST['hosxp_user'] ?? '';
$secretkey_post = $_POST['secretkey'] ?? '';
$stmt = $conn1->prepare("UPDATE opduser SET two_fa_secret_key_samui = ? WHERE loginname = ?");
if ($stmt) {
$stmt->bind_param("ss", $secretkey_post, $hosxp_user);
if ($stmt->execute()) {
echo "<script>alert('ตั้งค่า 2FA เรียบร้อยแล้ว'); window.location.href = '2fa_authen.php';</script>";
} else {
echo "<script>alert('ขออภัย !!! ... เกิดข้อผิดพลาด'); window.location.href = '2fa_authen.php';</script>";
}
$stmt->close();
}
exit();
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HOSxP Web Service | ทำ 2FA Authentication</title>
<link href="https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/style.css" type="text/css" />
</head>
<body>
<div class="app-container">
<?php require_once("includes/sidebar.php"); ?>
<main class="main-content">
<?php require_once("includes/header.php"); ?>
<div class="content-wrapper animate-fade-in">
<div class="card" style="max-width: 600px; margin: 0 auto;">
<div class="card-title">2 Factor Authentication</div>
<div style="display: <?php echo $div_get_username; ?>;">
<form method="post" action="2fa_authen.php">
<div class="form-group">
<label style="display:block; margin-bottom:0.5rem; font-weight:600;">HOSxP Username</label>
<input type="text" name="hos_user_2fa" class="form-control" required />
</div>
<div style="margin-top: 1.5rem;">
<button type="submit" class="btn btn-primary">Generate QR Code</button>
<a href="main.php" class="btn" style="background:#f3f4f6; color:var(--text-main); margin-left:0.5rem;">ยกเลิก</a>
</div>
</form>
</div>
<div style="display: <?php echo $div_gen_qrcode; ?>;">
<form method="post" action="2fa_authen.php">
<div style="text-align: center; margin-bottom: 2rem; background: #fff; padding: 1rem; border-radius: 8px; border: 1px solid var(--border);">
<?php if($hos_fullname) { print sprintf('<img src="%s" />', TokenAuth6238::getBarCodeUrl($hos_fullname, '', $secretkey, 'HOSxP-WebService')); } ?>
</div>
<div class="form-group">
<label style="display:block; margin-bottom:0.5rem; font-weight:600;">HOSxP Username</label>
<input type="text" name="hosxp_user" class="form-control" readonly value="<?php echo htmlspecialchars($hos_user_2fa); ?>" />
</div>
<div class="form-group">
<label style="display:block; margin-bottom:0.5rem; font-weight:600;">Secret Key</label>
<input type="text" name="secretkey" class="form-control" readonly value="<?php echo htmlspecialchars($secretkey); ?>" />
</div>
<input type="hidden" name="save_2fa" value="save_2fa">
<div style="margin-top: 1.5rem;">
<button type="submit" class="btn btn-primary">บันทึกข้อมูล</button>
<a href="main.php" class="btn" style="background:#f3f4f6; color:var(--text-main); margin-left:0.5rem;">ยกเลิก</a>
</div>
</form>
</div>
</div>
</div>
</main>
</div>
</body>
</html>