272 lines
12 KiB
PHP
272 lines
12 KiB
PHP
<?php
|
||
/**
|
||
* Outil temporaire : modifier key_chain_http pour un pk_key_chain.
|
||
* Accès 1 : /ms1_kc_set.php?k=<KEYCHAIN_HTTP_EDIT_SECRET>
|
||
* Accès 2 : compte connecté dont com_id est dans KEYCHAIN_HTTP_EDIT_ALLOWED_COM_IDS
|
||
*/
|
||
$blnNoMaintenance = true;
|
||
session_start();
|
||
|
||
require_once __DIR__ . '/php/inc_fonctions.php';
|
||
|
||
header('X-Robots-Tag: noindex, nofollow');
|
||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||
|
||
global $objDatabase, $vDomaine;
|
||
|
||
function kc_set_allowed_com_ids()
|
||
{
|
||
if (!defined('KEYCHAIN_HTTP_EDIT_ALLOWED_COM_IDS') || KEYCHAIN_HTTP_EDIT_ALLOWED_COM_IDS === '') {
|
||
return [];
|
||
}
|
||
$arr = array_map('intval', explode(',', KEYCHAIN_HTTP_EDIT_ALLOWED_COM_IDS));
|
||
return array_values(array_filter($arr, function ($id) {
|
||
return $id > 0;
|
||
}));
|
||
}
|
||
|
||
function kc_set_com_id_authorized()
|
||
{
|
||
if (!isset($_SESSION['com_id']) || (int) $_SESSION['com_id'] <= 0) {
|
||
return false;
|
||
}
|
||
return in_array((int) $_SESSION['com_id'], kc_set_allowed_com_ids(), true);
|
||
}
|
||
|
||
$strSecret = isset($_GET['k']) ? (string) $_GET['k'] : '';
|
||
$blnAuthSecret = ($strSecret !== '' && hash_equals(KEYCHAIN_HTTP_EDIT_SECRET, $strSecret));
|
||
$blnAuthUser = kc_set_com_id_authorized();
|
||
$blnAuth = $blnAuthSecret || $blnAuthUser;
|
||
$blnUserMode = $blnAuthUser;
|
||
|
||
if (!$blnAuth) {
|
||
if (!isset($_SESSION['com_id']) && kc_set_allowed_com_ids() !== []) {
|
||
$_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI'];
|
||
header('Location: ' . $vDomaine . '/compte');
|
||
exit;
|
||
}
|
||
http_response_code(403);
|
||
echo '<!DOCTYPE html><html lang="fr"><head><meta charset="UTF-8"><title>403</title></head><body>';
|
||
echo '<p>Accès refusé.</p>';
|
||
if (kc_set_allowed_com_ids() !== []) {
|
||
echo '<p><a href="' . htmlspecialchars($vDomaine . '/compte', ENT_QUOTES, 'UTF-8') . '">Connexion compte</a></p>';
|
||
}
|
||
echo '</body></html>';
|
||
exit;
|
||
}
|
||
|
||
if (empty($_SESSION['kc_edit_csrf'])) {
|
||
$_SESSION['kc_edit_csrf'] = bin2hex(random_bytes(16));
|
||
}
|
||
$strCsrf = $_SESSION['kc_edit_csrf'];
|
||
$strBaseUrl = $_SERVER['PHP_SELF'];
|
||
if ($blnAuthSecret) {
|
||
$strBaseUrl .= '?k=' . rawurlencode($strSecret);
|
||
}
|
||
$strBaseUrl = htmlspecialchars($strBaseUrl, ENT_QUOTES, 'UTF-8');
|
||
|
||
$strUserName = '';
|
||
if ($blnUserMode && !empty($_SESSION['com_prenom'])) {
|
||
$strUserName = trim((string) $_SESSION['com_prenom']);
|
||
} elseif ($blnUserMode && !empty($_SESSION['com_info']['com_prenom'])) {
|
||
$strUserName = trim((string) $_SESSION['com_info']['com_prenom']);
|
||
}
|
||
|
||
$strMessage = '';
|
||
$strMessageClass = '';
|
||
$intPk = 0;
|
||
$strCurrentHttp = '';
|
||
$blnShowUrlForm = false;
|
||
|
||
function kc_set_resolve_pk($input)
|
||
{
|
||
return intval($input) + KEYCHAIN_HTTP_EDIT_SHIFT;
|
||
}
|
||
|
||
function kc_set_validate_url($url)
|
||
{
|
||
$url = trim($url);
|
||
if ($url === '') {
|
||
return false;
|
||
}
|
||
if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
||
return false;
|
||
}
|
||
$parts = parse_url($url);
|
||
if (empty($parts['scheme']) || !in_array(strtolower($parts['scheme']), ['http', 'https'], true)) {
|
||
return false;
|
||
}
|
||
return $url;
|
||
}
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$strPostedCsrf = isset($_POST['csrf']) ? (string) $_POST['csrf'] : '';
|
||
if (!hash_equals($strCsrf, $strPostedCsrf)) {
|
||
$strMessage = 'Session expirée. Rechargez la page.';
|
||
$strMessageClass = 'err';
|
||
} else {
|
||
$strAction = isset($_POST['action']) ? (string) $_POST['action'] : '';
|
||
|
||
if ($strAction === 'load') {
|
||
$intPk = kc_set_resolve_pk(isset($_POST['pk_input']) ? $_POST['pk_input'] : 0);
|
||
if ($intPk <= 0) {
|
||
$strMessage = $blnUserMode ? 'Numéro invalide.' : 'Numéro invalide.';
|
||
$strMessageClass = 'err';
|
||
} else {
|
||
$sql = 'SELECT key_chain_http FROM key_chain WHERE pk_key_chain = ' . $intPk;
|
||
$row = $objDatabase->fxGetRow($sql);
|
||
if ($row === null) {
|
||
if ($blnUserMode) {
|
||
$strMessage = 'Aucun lien trouvé pour ce numéro.';
|
||
} else {
|
||
$strMessage = 'Aucune ligne pour pk ' . $intPk . ' (saisie + ' . KEYCHAIN_HTTP_EDIT_SHIFT . ').';
|
||
}
|
||
$strMessageClass = 'err';
|
||
} else {
|
||
$strCurrentHttp = $row['key_chain_http'];
|
||
$blnShowUrlForm = true;
|
||
}
|
||
}
|
||
} elseif ($strAction === 'save') {
|
||
$intPk = intval(isset($_POST['pk']) ? $_POST['pk'] : 0);
|
||
$strNewHttp = kc_set_validate_url(isset($_POST['key_chain_http']) ? $_POST['key_chain_http'] : '');
|
||
if ($intPk <= 0) {
|
||
$strMessage = 'Référence invalide.';
|
||
$strMessageClass = 'err';
|
||
} elseif ($strNewHttp === false) {
|
||
$strMessage = 'Adresse Web invalide (http ou https requis).';
|
||
$strMessageClass = 'err';
|
||
} else {
|
||
$sqlCheck = 'SELECT pk_key_chain FROM key_chain WHERE pk_key_chain = ' . $intPk;
|
||
if ($objDatabase->fxGetRow($sqlCheck) === null) {
|
||
$strMessage = $blnUserMode ? 'Lien introuvable.' : 'Ligne introuvable (pk ' . $intPk . ').';
|
||
$strMessageClass = 'err';
|
||
} else {
|
||
$sqlUpdate = "UPDATE key_chain SET key_chain_http = '" . $objDatabase->fxEscape($strNewHttp) . "' WHERE pk_key_chain = " . $intPk;
|
||
if ($objDatabase->fxQuery($sqlUpdate) === false) {
|
||
$strMessage = 'Erreur lors de l’enregistrement.';
|
||
$strMessageClass = 'err';
|
||
} else {
|
||
if ($blnUserMode) {
|
||
$strMessage = 'Adresse enregistrée.';
|
||
} else {
|
||
$strMessage = 'Enregistré — pk ' . $intPk . ' → ' . htmlspecialchars($strNewHttp, ENT_QUOTES, 'UTF-8');
|
||
}
|
||
$strMessageClass = 'ok';
|
||
$strCurrentHttp = $strNewHttp;
|
||
$blnShowUrlForm = true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="fr">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="robots" content="noindex, nofollow">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title><?php echo $blnUserMode ? 'Modifier le lien de redirection' : 'Key chain HTTP'; ?></title>
|
||
<style>
|
||
body { font-family: system-ui, sans-serif; max-width: 560px; margin: 2rem auto; padding: 0 1rem; color: #222; }
|
||
h1 { font-size: 1.35rem; margin-bottom: 0.5rem; }
|
||
.user-bar { color: #444; font-size: 0.95rem; margin-bottom: 1rem; }
|
||
label { display: block; margin-top: 1.25rem; font-weight: 600; font-size: 1.05rem; }
|
||
input[type="text"], input[type="number"] { width: 100%; box-sizing: border-box; padding: 0.6rem; font-size: 1.05rem; }
|
||
.credits {
|
||
background: #f0f4f8;
|
||
border: 1px solid #c5d0dc;
|
||
border-radius: 6px;
|
||
padding: 1rem 1.15rem;
|
||
margin: 0 0 1.25rem;
|
||
font-size: 1rem;
|
||
line-height: 1.5;
|
||
}
|
||
.credits strong { display: block; margin-bottom: 0.5rem; font-size: 1.05rem; }
|
||
.credits ul { margin: 0.5rem 0 0; padding-left: 1.25rem; }
|
||
.credits li { margin: 0.35rem 0; }
|
||
.hint { color: #555; font-size: 0.95rem; margin-top: 0.4rem; }
|
||
.msg { margin: 1rem 0; padding: 0.85rem; border-radius: 4px; font-size: 1rem; }
|
||
.msg.ok { background: #e8f5e9; color: #1b5e20; }
|
||
.msg.err { background: #ffebee; color: #b71c1c; }
|
||
.pk-badge { font-family: monospace; background: #e8e8e8; padding: 0.15rem 0.45rem; border-radius: 3px; font-size: 1.05em; }
|
||
.pk-line { font-size: 1.05rem; margin: 0.5rem 0 1rem; }
|
||
.back { margin-top: 1.5rem; font-size: 0.95rem; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<?php if ($blnUserMode) { ?>
|
||
<h1>Modifier le lien de redirection</h1>
|
||
<?php if ($strUserName !== '') { ?>
|
||
<p class="user-bar">Bonjour <?php echo htmlspecialchars($strUserName, ENT_QUOTES, 'UTF-8'); ?>.</p>
|
||
<?php } ?>
|
||
<div class="credits">
|
||
<strong>Comment faire</strong>
|
||
<ul>
|
||
<li>Entrez le <strong>numéro indiqué sur le QR</strong> (sans ajouter <?php echo (int) KEYCHAIN_HTTP_EDIT_SHIFT; ?> — le système s’en charge).</li>
|
||
<li>Appuyez sur <strong>Entrée</strong> pour voir l’adresse actuelle, modifiez-la, puis <strong>Entrée</strong> pour enregistrer.</li>
|
||
</ul>
|
||
</div>
|
||
<?php } else { ?>
|
||
<h1>Key chain — URL de destination</h1>
|
||
<div class="credits">
|
||
<strong>Indications</strong>
|
||
<ul>
|
||
<li>Numéro affiché <strong>+ <?php echo (int) KEYCHAIN_HTTP_EDIT_SHIFT; ?></strong> = <code>pk_key_chain</code> en base (ex. : vous tapez <span class="pk-badge">8</span> → <span class="pk-badge">108</span>).</li>
|
||
<li><strong>Entrée</strong> : charger l’URL actuelle, puis enregistrer la nouvelle.</li>
|
||
<li>Seule la colonne <code>key_chain_http</code> est modifiée (outil temporaire MS1).</li>
|
||
</ul>
|
||
</div>
|
||
<?php } ?>
|
||
|
||
<?php if ($strMessage !== '') { ?>
|
||
<div class="msg <?php echo htmlspecialchars($strMessageClass, ENT_QUOTES, 'UTF-8'); ?>"><?php
|
||
echo $strMessageClass === 'ok' ? $strMessage : htmlspecialchars($strMessage, ENT_QUOTES, 'UTF-8');
|
||
?></div>
|
||
<?php } ?>
|
||
|
||
<?php if (!$blnShowUrlForm) { ?>
|
||
<form method="post" action="<?php echo $strBaseUrl; ?>" id="frm_load">
|
||
<input type="hidden" name="csrf" value="<?php echo htmlspecialchars($strCsrf, ENT_QUOTES, 'UTF-8'); ?>">
|
||
<input type="hidden" name="action" value="load">
|
||
<label for="pk_input"><?php echo $blnUserMode ? 'Numéro du lien QR' : 'Numéro (sans le +' . (int) KEYCHAIN_HTTP_EDIT_SHIFT . ')'; ?></label>
|
||
<input type="number" id="pk_input" name="pk_input" min="1" step="1" required autofocus>
|
||
<p class="hint"><?php echo $blnUserMode ? 'Entrée pour afficher l’adresse actuelle.' : 'Entrée pour charger l’URL actuelle.'; ?></p>
|
||
</form>
|
||
<?php } else { ?>
|
||
<?php if (!$blnUserMode) { ?>
|
||
<p class="pk-line">pk_key_chain en base : <span class="pk-badge"><?php echo (int) $intPk; ?></span></p>
|
||
<?php } ?>
|
||
<form method="post" action="<?php echo $strBaseUrl; ?>" id="frm_save">
|
||
<input type="hidden" name="csrf" value="<?php echo htmlspecialchars($strCsrf, ENT_QUOTES, 'UTF-8'); ?>">
|
||
<input type="hidden" name="action" value="save">
|
||
<input type="hidden" name="pk" value="<?php echo (int) $intPk; ?>">
|
||
<label for="key_chain_http"><?php echo $blnUserMode ? 'Adresse Web de destination' : 'key_chain_http'; ?></label>
|
||
<input type="url" id="key_chain_http" name="key_chain_http" value="<?php echo htmlspecialchars($strCurrentHttp, ENT_QUOTES, 'UTF-8'); ?>" required autofocus>
|
||
<p class="hint">Entrée pour enregistrer.</p>
|
||
</form>
|
||
<p class="back"><a href="<?php echo $strBaseUrl; ?>">← <?php echo $blnUserMode ? 'Autre numéro' : 'Autre numéro'; ?></a></p>
|
||
<?php } ?>
|
||
|
||
<script>
|
||
(function () {
|
||
document.addEventListener('keydown', function (e) {
|
||
if (e.key !== 'Enter') return;
|
||
var load = document.getElementById('frm_load');
|
||
var save = document.getElementById('frm_save');
|
||
if (load && document.activeElement && load.contains(document.activeElement)) {
|
||
e.preventDefault();
|
||
load.submit();
|
||
} else if (save && document.activeElement && save.contains(document.activeElement)) {
|
||
e.preventDefault();
|
||
save.submit();
|
||
}
|
||
});
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|