Feat(bugs): notifications Discord, invitations et comptes

Envoie chaque signalement dans un canal forum Discord (post avec
résumé + logs par morceaux), met à jour le post quand le statut
change côté admin, et ajoute l'inscription par code d'invitation à
usage unique + changement de mot de passe dans le panneau.
This commit is contained in:
BlackAngelTV committed 2026-09-07 13:47:59 +02:00
1 parent f7605ae6f0
commit f94e6e1f15
7 files changed
+885 -7

No files matched your search

+197
View File
@@ -11,6 +11,7 @@
declare(strict_types=1);
require __DIR__ . '/../../api/bugs/db.php';
require_once __DIR__ . '/../../api/bugs/discord.php';
session_set_cookie_params([
'httponly' => true,
@@ -84,6 +85,7 @@ function excerpt(string $s, int $len = 140): string
$error = null;
$notice = null;
$freshInvite = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
@@ -123,9 +125,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = 'Statut inconnu.';
} else {
$pdo = fws_bugs_db();
$st = $pdo->prepare('SELECT status, discord_thread_id FROM bug_reports WHERE id = :id');
$st->execute([':id' => $id]);
$row = $st->fetch();
$st = $pdo->prepare('UPDATE bug_reports SET status = :s WHERE id = :id');
$st->execute([':s' => $status, ':id' => $id]);
$notice = "Signalement #$id marqué « " . $STATUSES[$status] . " ».";
/* Prévenir Discord (best-effort) */
if ($row !== false && !empty($row['discord_thread_id'])) {
try {
$actor = current_user()['name'] ?? '';
fws_bugs_discord_status_change([
'thread_id' => (string) $row['discord_thread_id'],
'old_status' => (string) ($row['status'] ?? ''),
'new_status' => $status,
'actor' => (string) $actor,
]);
} catch (Throwable $e) {
error_log('[fws-bugs] discord statut : ' . $e->getMessage());
}
}
header('Location: index.php?id=' . $id);
exit;
}
@@ -137,6 +157,46 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$notice = "Signalement #$id supprimé.";
header('Location: index.php');
exit;
} elseif ($action === 'gen_invite') {
$days = (int) ($_POST['days'] ?? 7);
if (!in_array($days, [0, 1, 7, 30, 90], true)) {
$days = 7;
}
$pdo = fws_bugs_db();
try {
$freshInvite = fws_bugs_create_invite($pdo, (string) (current_user()['name'] ?? ''), $days);
$notice = 'Code d\'invitation généré.';
} catch (Throwable $e) {
$error = 'Erreur : ' . $e->getMessage();
}
} elseif ($action === 'revoke_invite') {
$inviteId = (int) ($_POST['invite_id'] ?? 0);
$pdo = fws_bugs_db();
$st = $pdo->prepare("UPDATE invite_codes SET revoked_at = datetime('now') WHERE id = :id AND used_by IS NULL AND revoked_at IS NULL");
$st->execute([':id' => $inviteId]);
$notice = $st->rowCount() > 0
? 'Code d\'invitation révoqué : il ne fonctionnera plus.'
: 'Impossible de révoquer ce code (déjà utilisé ou déjà révoqué).';
} elseif ($action === 'change_password') {
$cur = (string) ($_POST['current_password'] ?? '');
$new = (string) ($_POST['new_password'] ?? '');
$confirm = (string) ($_POST['confirm_password'] ?? '');
$uid = (int) current_user()['id'];
$pdo = fws_bugs_db();
$st = $pdo->prepare('SELECT password_hash FROM admin_users WHERE id = :id');
$st->execute([':id' => $uid]);
$row = $st->fetch();
if ($row === false || !password_verify($cur, $row['password_hash'])) {
$error = 'Mot de passe actuel incorrect.';
} elseif (strlen($new) < 8) {
$error = 'Nouveau mot de passe trop court (8 caractères minimum).';
} elseif ($new !== $confirm) {
$error = 'Les deux nouveaux mots de passe ne correspondent pas.';
} else {
$up = $pdo->prepare('UPDATE admin_users SET password_hash = :h WHERE id = :id');
$up->execute([':h' => password_hash($new, PASSWORD_DEFAULT), ':id' => $uid]);
$notice = 'Mot de passe modifié avec succès.';
}
}
}
}
@@ -153,6 +213,7 @@ try {
}
$view_id = (int) ($_GET['id'] ?? 0);
$view_invites = ($_GET['view'] ?? '') === 'invites' && $view_id === 0;
/* Compteurs pour les filtres */
$counts = ['tous' => 0];
@@ -239,6 +300,18 @@ input[type=text], input[type=password] {
label { font-size: .85rem; color: #cbd5e1; }
.back { margin-bottom: 12px; display: inline-block; }
.pager { display: flex; gap: 8px; margin-top: 16px; }
.navtabs { display: flex; gap: 8px; margin: 0 0 16px; }
.navtabs a { border: 1px solid #334155; padding: 5px 14px; border-radius: 99px; font-size: .85rem; text-decoration: none; color: #cbd5e1; }
.navtabs a.active { background: #0284c7; border-color: #0284c7; color: #fff; }
.invite-form { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; margin-top: 10px; }
.invite-form label { margin: 0; }
.invite-form select { background: #0b1120; color: #e2e8f0; border: 1px solid #334155; border-radius: 8px; padding: 9px 11px; font-size: .9rem; }
.invite-box { margin-top: 14px; padding: 14px; border: 1px dashed #38bdf8; border-radius: 10px; background: #0c1a2b; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: .9rem; }
.invite-code { font-size: 1.2rem; letter-spacing: .08em; font-weight: 700; color: #7dd3fc; font-family: ui-monospace, Consolas, monospace; }
table.list { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.list th, table.list td { text-align: left; padding: 7px 8px; border-bottom: 1px solid #1e293b; vertical-align: middle; }
table.list th { color: #94a3b8; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; }
.login-register { text-align: center; margin-top: 14px; font-size: .85rem; }
</style>
</head>
<body>
@@ -249,6 +322,7 @@ label { font-size: .85rem; color: #cbd5e1; }
<div class="login-box">
<div class="card">
<h1>🔒 Panneau des signalements</h1>
<?php if (($_GET['registered'] ?? '') === '1'): ?><div class="flash ok">✅ Compte créé ! Connectez-vous avec vos identifiants.</div><?php endif; ?>
<?php if ($error): ?><div class="flash err"><?= h($error) ?></div><?php endif; ?>
<?php if ($counts['tous'] === 0): ?>
<p style="font-size:.85rem;color:#94a3b8">
@@ -264,6 +338,7 @@ label { font-size: .85rem; color: #cbd5e1; }
<input type="password" id="password" name="password" required>
<button class="btn primary" type="submit" style="width:100%;padding:10px">Se connecter</button>
</form>
<p class="login-register">Pas encore de compte ? <a href="register.php">S'inscrire avec un code d'invitation</a></p>
</div>
</div>
@@ -281,6 +356,11 @@ label { font-size: .85rem; color: #cbd5e1; }
</form>
</header>
<div class="navtabs">
<a href="index.php" class="<?= $view_invites ? '' : 'active' ?>">🐞 Signalements</a>
<a href="index.php?view=invites" class="<?= $view_invites ? 'active' : '' ?>">👥 Invitations & comptes</a>
</div>
<?php if ($error): ?><div class="flash err"><?= h($error) ?></div><?php endif; ?>
<?php if ($notice): ?><div class="flash ok"><?= h($notice) ?></div><?php endif; ?>
@@ -319,6 +399,11 @@ label { font-size: .85rem; color: #cbd5e1; }
<?php endif; ?>
<tr><td class="k">IP</td><td><?= h($r['ip'] !== '' ? $r['ip'] : '?') ?></td></tr>
<tr><td class="k">User-agent</td><td style="font-size:.78rem;color:#94a3b8"><?= h($r['user_agent']) ?></td></tr>
<?php if (!empty($r['discord_post_url'])): ?>
<tr><td class="k">Discord</td><td><a href="<?= h($r['discord_post_url']) ?>" target="_blank" rel="noopener">Voir le post dans le forum ↗</a></td></tr>
<?php elseif (!empty($r['discord_thread_id'])): ?>
<tr><td class="k">Discord</td><td style="color:#94a3b8">post créé (lien indisponible)</td></tr>
<?php endif; ?>
</table>
<?php if ($r['logs'] !== ''): ?>
@@ -351,6 +436,118 @@ label { font-size: .85rem; color: #cbd5e1; }
</div>
<?php endif; /* fin détail */ ?>
<?php elseif ($view_invites): /* ─────────── INVITATIONS & COMPTES ─────────── */
$invites = $pdo->query('SELECT * FROM invite_codes ORDER BY id DESC LIMIT 200')->fetchAll();
$users = $pdo->query('SELECT id, username, created_at FROM admin_users ORDER BY id')->fetchAll();
$userNames = [];
foreach ($users as $u) {
$userNames[(int) $u['id']] = (string) $u['username'];
}
$now = date('Y-m-d H:i:s');
?>
<div class="card">
<h3>🎟️ Générer un code d'invitation</h3>
<p style="font-size:.85rem;color:#94a3b8">Le code est <strong>à usage unique</strong> : donnez-le à la personne, elle l'utilise sur la page
<a href="register.php" target="_blank" rel="noopener">admin/bugs/register.php</a> pour créer son compte (nom + mot de passe).</p>
<form method="post" class="invite-form">
<input type="hidden" name="action" value="gen_invite">
<input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
<label for="days">Valable</label>
<select name="days" id="days">
<option value="1">1 jour</option>
<option value="7" selected>7 jours</option>
<option value="30">30 jours</option>
<option value="90">90 jours</option>
<option value="0">Sans expiration</option>
</select>
<button class="btn primary" type="submit">Générer le code</button>
</form>
<?php if ($freshInvite !== ''): ?>
<div class="invite-box">
<span>🎉 Nouveau code à donner :</span>
<code class="invite-code" id="freshCode"><?= h($freshInvite) ?></code>
<button class="btn small" type="button"
onclick="var t=document.getElementById('freshCode').textContent.trim();if(navigator.clipboard){navigator.clipboard.writeText(t);}this.textContent='✅ Copié';">📋 Copier</button>
</div>
<?php endif; ?>
</div>
<div class="card">
<h3>Codes générés (<?= count($invites) ?>)</h3>
<?php if (!$invites): ?>
<p class="empty">Aucun code pour l'instant.</p>
<?php else: ?>
<table class="list">
<tr><th>Code</th><th>Créé</th><th>Expire</th><th>Statut</th><th>Utilisé par</th><th></th></tr>
<?php foreach ($invites as $inv):
$isRevoked = $inv['revoked_at'] !== null;
$isUsed = $inv['used_by'] !== null;
$isExpired = !$isUsed && !$isRevoked && $inv['expires_at'] !== null && $inv['expires_at'] < $now;
if ($isRevoked) { $stLabel = 'Révoqué'; $stColor = '#f87171'; }
elseif ($isUsed) { $stLabel = 'Utilisé'; $stColor = '#94a3b8'; }
elseif ($isExpired) { $stLabel = 'Expiré'; $stColor = '#fbbf24'; }
else { $stLabel = 'Actif'; $stColor = '#4ade80'; }
?>
<tr>
<td style="font-family:ui-monospace,Consolas,monospace;letter-spacing:.05em"><?= h($inv['code']) ?></td>
<td><?= h(format_date((string) $inv['created_at'])) ?><?= $inv['created_by'] !== '' ? ' · ' . h($inv['created_by']) : '' ?></td>
<td><?= $inv['expires_at'] !== null ? h(format_date((string) $inv['expires_at'])) : '∞ jamais' ?></td>
<td><span class="badge" style="background:<?= $stColor ?>22;color:<?= $stColor ?>"><?= $stLabel ?></span></td>
<td><?= $inv['used_by'] !== null
? h($userNames[(int) $inv['used_by']] ?? '#'.$inv['used_by']) . ' · ' . h(format_date((string) $inv['used_at']))
: '' ?></td>
<td style="text-align:right">
<?php if (!$isUsed && !$isRevoked && !$isExpired): ?>
<form class="inline" method="post" onsubmit="return confirm('Révoquer ce code ? Il ne fonctionnera plus.')">
<input type="hidden" name="action" value="revoke_invite">
<input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
<input type="hidden" name="invite_id" value="<?= (int) $inv['id'] ?>">
<button class="btn small danger" type="submit">Révoquer</button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
<div class="card">
<h3>👥 Comptes existants (<?= count($users) ?>)</h3>
<p style="font-size:.85rem;color:#94a3b8">Tous les comptes peuvent consulter et traiter les signalements. Chacun peut modifier son propre mot de passe ci-dessous.</p>
<?php if (!$users): ?>
<p class="empty">Aucun compte.</p>
<?php else: ?>
<table class="list">
<tr><th>Nom</th><th>Créé le</th></tr>
<?php foreach ($users as $u): ?>
<tr>
<td><?= h($u['username']) ?></td>
<td><?= h(format_date((string) $u['created_at'])) ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
<div class="card">
<h3>🔑 Mon mot de passe</h3>
<p style="font-size:.85rem;color:#94a3b8">Connecté en tant que <strong><?= h($user['name']) ?></strong>. Le nouveau mot de passe doit faire au moins 8 caractères.</p>
<form method="post" style="display:grid;gap:2px;max-width:420px">
<input type="hidden" name="action" value="change_password">
<input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
<label for="current_password">Mot de passe actuel</label>
<input type="password" id="current_password" name="current_password" required autocomplete="current-password">
<label for="new_password">Nouveau mot de passe</label>
<input type="password" id="new_password" name="new_password" required minlength="8" autocomplete="new-password">
<label for="confirm_password">Confirmer le nouveau mot de passe</label>
<input type="password" id="confirm_password" name="confirm_password" required minlength="8" autocomplete="new-password">
<button class="btn primary" type="submit" style="justify-self:start;margin-top:4px">Mettre à jour le mot de passe</button>
</form>
</div>
<?php else: /* ─────────── LISTE ─────────── */
$filter = $_GET['status'] ?? 'tous';
+4 -3
View File
@@ -93,9 +93,10 @@ if ($existing !== false) {
$up->execute([':h' => $hash, ':id' => $existing['id']]);
echo "✅ Mot de passe de « $user » réinitialisé.\n";
} else {
$ins = $pdo->prepare('INSERT INTO admin_users (username, password_hash) VALUES (:u, :h)');
$ins->execute([':u' => $user, ':h' => $hash]);
echo "✅ Utilisateur « $user » créé.\n";
$uid = fws_bugs_next_id($pdo, 'admin_users');
$ins = $pdo->prepare('INSERT INTO admin_users (id, username, password_hash) VALUES (:id, :u, :h)');
$ins->execute([':id' => $uid, ':u' => $user, ':h' => $hash]);
echo "✅ Utilisateur « $user » créé (id $uid).\n";
}
echo "\nPanneau : https://fwslinux.fr/admin/bugs/\n";
+157
View File
@@ -0,0 +1,157 @@
<?php
/**
* FWS Linux — Inscription au panneau des signalements.
*
* La personne reçoit un code d'invitation À USAGE UNIQUE (généré dans le
* panneau admin, onglet « Invitations & comptes »), puis crée ici son
* compte avec un nom d'utilisateur et un mot de passe.
*
* URL : https://fwslinux.fr/admin/bugs/register.php
*/
declare(strict_types=1);
require __DIR__ . '/../../api/bugs/db.php';
function h(?string $s): string
{
return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8');
}
$error = null;
$ok = false;
/* Petit anti-spam : champ piège */
$honeypot = trim((string) ($_POST['website'] ?? ''));
$submitted = ($_SERVER['REQUEST_METHOD'] === 'POST') && $honeypot === '';
if ($submitted) {
$code = trim((string) ($_POST['code'] ?? ''));
$username = trim((string) ($_POST['username'] ?? ''));
$password = (string) ($_POST['password'] ?? '');
$confirm = (string) ($_POST['password2'] ?? '');
try {
$pdo = fws_bugs_db();
/* 1) Le code est-il valable ? */
$invite = fws_bugs_find_invite($pdo, $code);
if ($invite === null) {
$error = 'Ce code d\'invitation est inconnu. Vérifiez qu\'il a bien été copié.';
} elseif ($invite['revoked_at'] !== null) {
$error = 'Ce code d\'invitation a été révoqué. Demandez-en un nouveau.';
} elseif ($invite['used_by'] !== null) {
$error = 'Ce code d\'invitation a déjà été utilisé.';
} elseif ($invite['expires_at'] !== null && $invite['expires_at'] < date('Y-m-d H:i:s')) {
$error = 'Ce code d\'invitation a expiré. Demandez-en un nouveau.';
} elseif (!preg_match('/^[A-Za-z0-9_.\-]{2,32}$/', $username)) {
$error = 'Nom d\'utilisateur invalide (2 à 32 caractères : lettres, chiffres, . _ -).';
} elseif (strlen($password) < 8) {
$error = 'Mot de passe trop court (8 caractères minimum).';
} elseif ($password !== $confirm) {
$error = 'Les deux mots de passe ne correspondent pas.';
}
/* 2) Création du compte + consommation du code (transaction) */
if ($error === null) {
$pdo->beginTransaction();
try {
$hash = password_hash($password, PASSWORD_DEFAULT);
$uid = fws_bugs_next_id($pdo, 'admin_users');
$ins = $pdo->prepare('INSERT INTO admin_users (id, username, password_hash) VALUES (:id, :u, :h)');
$ins->execute([':id' => $uid, ':u' => $username, ':h' => $hash]);
$claim = $pdo->prepare("UPDATE invite_codes SET used_by = :uid, used_at = datetime('now') WHERE id = :id AND used_by IS NULL");
$claim->execute([':uid' => $uid, ':id' => (int) $invite['id']]);
if ($claim->rowCount() !== 1) {
throw new RuntimeException('Code déjà utilisé entre-temps.');
}
$pdo->commit();
$ok = true;
} catch (Throwable $e) {
$pdo->rollBack();
if ($pdo->errorInfo()[1] === 19 && strpos((string) $e->getMessage(), 'UNIQUE') !== false) {
$error = 'Ce nom d\'utilisateur est déjà pris. Choisissez-en un autre.';
} else {
error_log('[fws-bugs] inscription : ' . $e->getMessage());
$error = 'Erreur interne lors de l\'inscription. Réessayez.';
}
}
}
} catch (Throwable $e) {
$error = 'Erreur base de données : ' . h($e->getMessage());
}
}
/* Succès → retour à la page de connexion avec un message */
if ($ok) {
header('Location: index.php?registered=1');
exit;
}
$baseUrl = 'https://' . ($_SERVER['HTTP_HOST'] ?? 'fwslinux.fr');
$codeValue = $submitted ? $code : '';
$userValue = $submitted ? $username : '';
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Créer un compte — Signalements FWS</title>
<style>
:root { color-scheme: dark; }
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; background: #0f172a; color: #e2e8f0; }
.wrap { max-width: 520px; margin: 0 auto; padding: 40px 16px; }
.card { background: #111c33; border: 1px solid #1e293b; border-radius: 14px; padding: 28px; }
h1 { font-size: 1.25rem; margin-top: 0; }
label { font-size: .85rem; color: #cbd5e1; display: block; margin-top: 14px; }
input[type=text], input[type=password], input[type=code] {
width: 100%; padding: 10px 12px; margin: 6px 0 2px;
background: #0b1120; border: 1px solid #334155; border-radius: 8px; color: #e2e8f0; font-size: .95rem;
}
input.code { font-family: ui-monospace, Consolas, monospace; letter-spacing: .08em; text-transform: uppercase; }
button { margin-top: 20px; padding: 11px 18px; border: 0; border-radius: 8px; background: #0284c7; color: #fff; font-size: 1rem; cursor: pointer; width: 100%; }
button:hover { background: #0369a1; }
.flash { padding: 10px 12px; border-radius: 8px; font-size: .88rem; margin-bottom: 14px; }
.flash.err { background: #3d1414; color: #ffa198; }
.hint { font-size: .8rem; color: #64748b; margin-top: 4px; }
.back { display: inline-block; margin-top: 18px; font-size: .85rem; color: #94a3b8; text-decoration: none; }
.note { font-size: .85rem; color: #94a3b8; background: #0b1120; border: 1px solid #1e293b; border-radius: 8px; padding: 10px 12px; margin-bottom: 16px; line-height: 1.5; }
</style>
</head>
<body>
<div class="wrap">
<div class="card">
<h1>🎟️ Créer un compte</h1>
<p class="note">Tu as reçu un <strong>code d'invitation à usage unique</strong> de l'équipe FWS.
Saisis-le ci-dessous puis choisis ton nom d'utilisateur et ton mot de passe.</p>
<?php if ($error): ?><div class="flash err"><?= h($error) ?></div><?php endif; ?>
<form method="post" autocomplete="on">
<label for="code">Code d'invitation</label>
<input type="text" id="code" name="code" class="code" required autofocus
value="<?= h($codeValue) ?>" placeholder="K7QM-2H4D-X9BP">
<label for="username">Nom d'utilisateur</label>
<input type="text" id="username" name="username" required value="<?= h($userValue) ?>"
placeholder="ex : alice" pattern="[A-Za-z0-9_.\-]{2,32}">
<div class="hint">2 à 32 caractères : lettres, chiffres, . _ -</div>
<label for="password">Mot de passe</label>
<input type="password" id="password" name="password" required minlength="8" placeholder="8 caractères minimum">
<label for="password2">Confirmer le mot de passe</label>
<input type="password" id="password2" name="password2" required minlength="8">
<input name="website" style="display:none" tabindex="-1" autocomplete="off">
<button type="submit">Créer mon compte</button>
</form>
<a class="back" href="index.php">← Retour à la connexion</a>
</div>
</div>
</body>
</html>