Files
ms1inscription-v5/qr_test.php
2026-05-13 09:43:32 -04:00

382 lines
8.0 KiB
PHP

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once('php/inc_start_time.php');
require_once('php/inc_fonctions.php');
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once('php/inc_fx_panier.php');
ini_set('display_errors',1);
error_reporting(E_ALL);
global $objDatabase;
function fxShowQrParticipantDebug($strToken, $strLangue = 'fr')
{
global $objDatabase;
$sql = "
SELECT
id,
token_hash,
qr_public_id,
com_id,
pec_id,
pec_id_original,
ach_id,
status,
token_enc
FROM qr_tokens
WHERE qr_public_id = '" . $objDatabase->fxEscape($strToken) . "'
LIMIT 1
";
$tabToken = $objDatabase->fxGetRow($sql);
if (!$tabToken) {
return '<div style="color:red;">TOKEN INVALIDE</div>';
}
/*
============================================
PANIER
============================================
*/
$sqlPanier = "
SELECT no_panier
FROM inscriptions_panier_acheteurs
WHERE ach_id = " . intval($tabToken['ach_id']) . "
LIMIT 1
";
$tabAcheteurQr = $objDatabase->fxGetRow($sqlPanier);
if (!$tabAcheteurQr) {
return '<div style="color:red;">PANIER INTROUVABLE</div>';
}
$tabPanier = fxListPanier($tabAcheteurQr['no_panier']);
if (!$tabPanier) {
return '<div style="color:red;">PANIER INTROUVABLE</div>';
}
$tabParticipants = $tabPanier[4];
$tabQuestions = $tabPanier[5];
/*
============================================
RECHERCHE PARTICIPANT
============================================
*/
$tabParticipantTrouve = null;
foreach ($tabParticipants as $intEpreuve => $tabListeParticipants) {
if (!is_array($tabListeParticipants)) {
continue;
}
foreach ($tabListeParticipants as $tabParticipant) {
if (
intval($tabParticipant['pec_id']) === intval($tabToken['pec_id_original'])
) {
$tabParticipantTrouve = $tabParticipant;
break 2;
}
}
}
if (!$tabParticipantTrouve) {
return '<div style="color:red;">PARTICIPANT INTROUVABLE</div>';
}
/*
============================================
HTML
============================================
*/
$strRetour = '';
if ($tabParticipantTrouve['rol_nom_' . $strLangue] != '') {
$strRetour .= '
<strong>' .
fxUnescape($tabParticipantTrouve['rol_nom_' . $strLangue]) .
':</strong>
';
}
$strRetour .=
fxUnescape($tabParticipantTrouve['par_nom']) .
', ' .
fxUnescape($tabParticipantTrouve['par_prenom']);
if ($tabParticipantTrouve['par_courriel'] != '') {
$strRetour .= '
<br>(' .
fxUnescape($tabParticipantTrouve['par_courriel']) .
')
';
}
$strSexe = fxGetGender(
$tabParticipantTrouve['par_sexe'],
$strLangue
);
if (
$tabParticipantTrouve['par_naissance'] != '' &&
$tabParticipantTrouve['par_naissance'] != '0000-00-00'
) {
$strRetour .= '
<br>
Date de naissance:
' .
fxShowDate(
$tabParticipantTrouve['par_naissance'],
$strLangue
) .
' - ' .
$strSexe;
}
/*
============================================
QUESTIONS
============================================
*/
$intParId = $tabParticipantTrouve['par_id'];
if (isset($tabQuestions[$intParId])) {
foreach ($tabQuestions[$intParId] as $tabQuestion) {
if (
$tabQuestion['que_cart_show'] == 1 &&
trim($tabQuestion['que_choix_' . $strLangue]) != ''
) {
$strRetour .= '
<br>' .
fxUnescape(
$tabQuestion['que_cart_label_' . $strLangue]
) .
': ' .
fxUnescape(
$tabQuestion['que_choix_' . $strLangue]
);
}
}
}
return $strRetour;
}
/*
=====================================================
AJAX DEBUG QR
=====================================================
*/
if (isset($_GET['ajax']) && $_GET['ajax'] == 1) {
header('Content-Type: application/json');
if (empty($_GET['t'])) {
echo json_encode([
'status' => 'error',
'message' => 'TOKEN MANQUANT'
]);
exit;
}
$strToken = $_GET['t'];
$sql = "
SELECT
id,
com_id,
pec_id,
pec_id_original,
ach_id,
status
FROM qr_tokens
WHERE qr_public_id = '" . $objDatabase->fxEscape($strToken) . "'
LIMIT 1
";
$tabQr = $objDatabase->fxGetRow($sql);
if (!$tabQr) {
echo json_encode([
'status' => 'error',
'message' => 'TOKEN INVALIDE'
]);
exit;
}
echo json_encode([
'status' => 'ok',
'data' => $tabQr,
'html' => fxShowQrParticipantDebug($strToken, 'fr')
]);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QR TEST</title>
<script src="https://unpkg.com/html5-qrcode"></script>
</head>
<body>
<h2>QR TEST</h2>
<div id="qr-reader" style="width:500px;"></div>
<hr>
<input type="file" id="qr-image-input" accept="image/*">
<hr>
<div id="qr-result">
Aucun QR détecté
</div>
<div id="qr-file-reader" style="display:none;"></div>
<script>
let lastQr = '';
function afficherQr(decodedText) {
if (decodedText === lastQr) {
return;
}
lastQr = decodedText;
let html = `
<div>
<strong>QR :</strong><br>
${decodedText}
</div>
<hr>
`;
document.getElementById('qr-result').innerHTML = html;
let url;
try {
url = new URL(decodedText);
} catch(e) {
return;
}
let token = url.searchParams.get('t');
if (!token) {
return;
}
fetch('qr_test.php?ajax=1&t=' + encodeURIComponent(token))
.then(response => response.json())
.then(data => {
if (data.status !== 'ok') {
document.getElementById('qr-result').innerHTML += `
<div style="color:red;">
${data.message}
</div>
`;
return;
}
let qr = data.data;
document.getElementById('qr-result').innerHTML =
data.html;
});
}
/*
=====================================================
CAMERA
=====================================================
*/
const qrCamera = new Html5Qrcode("qr-reader");
qrCamera.start(
{ facingMode: "environment" },
{
fps: 5,
qrbox: 140,
aspectRatio: 1.0
},
function(decodedText) {
afficherQr(decodedText);
}
);
/*
=====================================================
IMAGE FILE
=====================================================
*/
const qrFile = new Html5Qrcode("qr-file-reader");
document.getElementById('qr-image-input')
.addEventListener('change', function(e) {
if (!e.target.files.length) {
return;
}
const file = e.target.files[0];
qrFile.scanFile(file, true)
.then(function(decodedText) {
afficherQr(decodedText);
})
.catch(function(err) {
document.getElementById('qr-result').innerHTML =
'Aucun code QR détecté';
});
});
</script>
</body>
</html>