181 lines
4.0 KiB
PHP
181 lines
4.0 KiB
PHP
|
||
<?php
|
||
function fxGenerateQrPublicId($length = 12)
|
||
{
|
||
return substr(bin2hex(random_bytes(16)), 0, $length);
|
||
}
|
||
//MSIN-4290
|
||
function fxcreer_qr_token(array $data)
|
||
{
|
||
|
||
global $objDatabase;
|
||
|
||
$par_id = intval($data['par_id'] ?? 0);
|
||
$pec_id_original = intval($data['pec_id_original'] ?? 0);
|
||
$pec_id = intval($data['pec_id'] ?? $data['pec_id_original'] );
|
||
$ach_id = intval($data['ach_id'] ?? 0);
|
||
$com_id = intval($data['com_id'] ?? 0);
|
||
$eve_id = intval($data['eve_id'] ?? 0);
|
||
$status = $objDatabase->fxEscape($data['status'] ?? 'actif');
|
||
$expires_at = $data['expires_at'] ?? null;
|
||
|
||
/*
|
||
=====================================================
|
||
1️⃣ Vérifier si ce pec_id existe déjà
|
||
=====================================================
|
||
*/
|
||
$sqlCheck = "
|
||
SELECT id, token_enc, qr_public_id
|
||
FROM qr_tokens
|
||
WHERE par_id = $par_id
|
||
LIMIT 1
|
||
";
|
||
|
||
$existing = $objDatabase->fxGetRow($sqlCheck);
|
||
|
||
if (!empty($existing)) {
|
||
return [
|
||
'status' => 'exists',
|
||
'token_enc' => $existing['token_enc'],
|
||
'qr_public_id' => $existing['qr_public_id']
|
||
];
|
||
}
|
||
|
||
/*
|
||
=====================================================
|
||
2️⃣ Générer token UNIQUE
|
||
=====================================================
|
||
*/
|
||
do {
|
||
$token = bin2hex(random_bytes(32));
|
||
$token_hash = hash('sha256', $token);
|
||
|
||
$sqlCheckToken = "
|
||
SELECT id FROM qr_tokens
|
||
WHERE token_hash = '$token_hash'
|
||
LIMIT 1
|
||
";
|
||
|
||
$tokenExists = $objDatabase->fxGetRow($sqlCheckToken);
|
||
|
||
} while (!empty($tokenExists));
|
||
/*
|
||
=====================================================
|
||
2️⃣B Générer qr_public_id UNIQUE
|
||
=====================================================
|
||
*/
|
||
$intMaxTry = 10;
|
||
$strPublicId = '';
|
||
|
||
for ($i = 0; $i < $intMaxTry; $i++) {
|
||
|
||
$strTestId = fxGenerateQrPublicId(12);
|
||
|
||
$sqlCheckPublic = "
|
||
SELECT id
|
||
FROM qr_tokens
|
||
WHERE qr_public_id = '" . $objDatabase->fxEscape($strTestId) . "'
|
||
LIMIT 1
|
||
";
|
||
|
||
$tabCheckPublic = $objDatabase->fxGetRow($sqlCheckPublic);
|
||
|
||
if (!$tabCheckPublic) {
|
||
|
||
$strPublicId = $strTestId;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if ($strPublicId == '') {
|
||
die('ERREUR GENERATION QR PUBLIC ID');
|
||
}
|
||
|
||
$token_enc = fxencrypt_token($token);
|
||
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
|
||
$expires_sql = ($expires_at !== null) ? "'" . $objDatabase->fxEscape($expires_at) . "'" : "NULL";
|
||
|
||
/*
|
||
=====================================================
|
||
3️⃣ Insert
|
||
=====================================================
|
||
*/
|
||
$sqlInsert = "
|
||
INSERT INTO qr_tokens SET
|
||
token_hash = '$token_hash',
|
||
qr_public_id = '$strPublicId',
|
||
token_enc = '$token_enc',
|
||
par_id = $par_id,
|
||
eve_id = $eve_id,
|
||
com_id = $com_id,
|
||
pec_id = $pec_id,
|
||
pec_id_original = $pec_id_original,
|
||
ach_id = $ach_id,
|
||
status = '$status',
|
||
|
||
created_at = NOW(),
|
||
expires_at = $expires_sql,
|
||
adress_ip = '$ip'
|
||
";
|
||
|
||
$objDatabase->fxQuery($sqlInsert);
|
||
|
||
return [
|
||
'status' => 'created',
|
||
'token_enc' => $token_enc,
|
||
'qr_public_id' => $strPublicId
|
||
];
|
||
}
|
||
|
||
//MSIN-4290
|
||
//MSIN-4290
|
||
function fxgenerer_qr_image($strToken, $strBaseUrl, $strDossierSortie = 'qr')
|
||
{
|
||
// $strBaseUrl ex: https://tonsite.com/q.php
|
||
// $strDossierSortie ex: qr/
|
||
|
||
require_once __DIR__ . '/../libs/phpqrcode-master/qrlib.php';
|
||
// Création du dossier si inexistant
|
||
if (!is_dir($strDossierSortie)) {
|
||
mkdir($strDossierSortie, 0755, true);
|
||
}
|
||
|
||
// URL complète encodée dans le QR
|
||
$strQrUrl = rtrim($strBaseUrl, '/') . '?t=' . urlencode($strToken);
|
||
|
||
// Nom du fichier
|
||
$strFile = $strDossierSortie . '/qr_' . substr(hash('sha256', $strToken), 0, 20) . '.png';
|
||
|
||
// Génération du QR
|
||
QRcode::png(
|
||
$strQrUrl,
|
||
$strFile,
|
||
QR_ECLEVEL_Q, // bon niveau de correction
|
||
6 // taille
|
||
);
|
||
|
||
return $strFile;
|
||
}
|
||
//MSIN-4290
|
||
function fxencrypt_token($token)
|
||
{
|
||
return openssl_encrypt(
|
||
$token,
|
||
'AES-256-CBC',
|
||
QR_SECRET_KEY,
|
||
0,
|
||
substr(hash('sha256', QR_SECRET_KEY), 0, 16)
|
||
);
|
||
}
|
||
|
||
function fxdecrypt_token($token_enc)
|
||
{
|
||
return openssl_decrypt(
|
||
$token_enc,
|
||
'AES-256-CBC',
|
||
QR_SECRET_KEY,
|
||
0,
|
||
substr(hash('sha256', QR_SECRET_KEY), 0, 16)
|
||
);
|
||
}
|