Template
Feat(bugs): API de signalement + panneau admin SQLite
Ajoute un endpoint POST public (/api/bugs.php) qui stocke les rapports de bug (JSON ou formulaire, CORS ouvert) dans une base SQLite protégée, et un panneau admin (/admin/bugs/) avec comptes en base pour consulter, trier et traiter les signalements.
This commit is contained in:
1 parent
d5fd472ff5
commit
f7605ae6f0
6 files changed
+887
No files matched your search
@@ -0,0 +1,430 @@
|
||||
<?php
|
||||
/**
|
||||
* FWS Linux — Panneau admin des signalements de bugs.
|
||||
*
|
||||
* URL : https://fwslinux.fr/admin/bugs/
|
||||
*
|
||||
* Authentification : session PHP + utilisateurs stockés dans la base SQLite
|
||||
* (créés avec admin/bugs/init.php). Passwords hachés avec password_hash().
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/../../api/bugs/db.php';
|
||||
|
||||
session_set_cookie_params([
|
||||
'httponly' => true,
|
||||
'samesite' => 'Lax',
|
||||
'secure' => (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https'),
|
||||
]);
|
||||
session_start();
|
||||
|
||||
$STATUSES = [
|
||||
'nouveau' => '🆕 Nouveau',
|
||||
'en_cours' => '🔧 En cours',
|
||||
'resolu' => '✅ Résolu',
|
||||
'ignore' => '🙈 Ignoré',
|
||||
];
|
||||
$STATUS_COLORS = [
|
||||
'nouveau' => '#38bdf8',
|
||||
'en_cours' => '#fbbf24',
|
||||
'resolu' => '#4ade80',
|
||||
'ignore' => '#94a3b8',
|
||||
];
|
||||
|
||||
/* ───────────────────────── helpers ───────────────────────── */
|
||||
|
||||
function h(?string $s): string
|
||||
{
|
||||
return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function current_user(): ?array
|
||||
{
|
||||
return isset($_SESSION['admin_user_id']) ? [
|
||||
'id' => (int) $_SESSION['admin_user_id'],
|
||||
'name' => (string) $_SESSION['admin_username'],
|
||||
] : null;
|
||||
}
|
||||
|
||||
function csrf_token(): string
|
||||
{
|
||||
if (empty($_SESSION['csrf'])) {
|
||||
$_SESSION['csrf'] = bin2hex(random_bytes(32));
|
||||
}
|
||||
return $_SESSION['csrf'];
|
||||
}
|
||||
|
||||
function csrf_ok(): bool
|
||||
{
|
||||
return hash_equals($_SESSION['csrf'] ?? '', $_POST['csrf'] ?? '');
|
||||
}
|
||||
|
||||
function format_date(string $utc): string
|
||||
{
|
||||
try {
|
||||
$d = new DateTime($utc, new DateTimeZone('UTC'));
|
||||
$d->setTimezone(new DateTimeZone('Europe/Paris'));
|
||||
return $d->format('d/m/Y H:i');
|
||||
} catch (Throwable $e) {
|
||||
return $utc;
|
||||
}
|
||||
}
|
||||
|
||||
function excerpt(string $s, int $len = 140): string
|
||||
{
|
||||
$s = trim(preg_replace('/\s+/', ' ', $s) ?? '');
|
||||
if (mb_strlen($s) <= $len) {
|
||||
return $s;
|
||||
}
|
||||
return mb_substr($s, 0, $len - 1) . '…';
|
||||
}
|
||||
|
||||
/* ───────────────────────── actions POST ───────────────────────── */
|
||||
|
||||
$error = null;
|
||||
$notice = null;
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
if ($action === 'login') {
|
||||
$username = trim((string) ($_POST['username'] ?? ''));
|
||||
$password = (string) ($_POST['password'] ?? '');
|
||||
try {
|
||||
$pdo = fws_bugs_db();
|
||||
$st = $pdo->prepare('SELECT * FROM admin_users WHERE username = :u');
|
||||
$st->execute([':u' => $username]);
|
||||
$user = $st->fetch();
|
||||
if ($user !== false && password_verify($password, $user['password_hash'])) {
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['admin_user_id'] = (int) $user['id'];
|
||||
$_SESSION['admin_username'] = (string) $user['username'];
|
||||
$_SESSION['csrf'] = bin2hex(random_bytes(32));
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
}
|
||||
$error = 'Identifiants incorrects.';
|
||||
} catch (Throwable $e) {
|
||||
$error = 'Erreur base de données : ' . $e->getMessage();
|
||||
}
|
||||
} elseif (current_user() !== null) {
|
||||
if (!csrf_ok()) {
|
||||
$error = 'Jeton de sécurité invalide, rechargez la page.';
|
||||
} elseif ($action === 'logout') {
|
||||
$_SESSION = [];
|
||||
session_destroy();
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
} elseif ($action === 'set_status') {
|
||||
$id = (int) ($_POST['id'] ?? 0);
|
||||
$status = (string) ($_POST['status'] ?? '');
|
||||
if (!isset($STATUSES[$status])) {
|
||||
$error = 'Statut inconnu.';
|
||||
} else {
|
||||
$pdo = fws_bugs_db();
|
||||
$st = $pdo->prepare('UPDATE bug_reports SET status = :s WHERE id = :id');
|
||||
$st->execute([':s' => $status, ':id' => $id]);
|
||||
$notice = "Signalement #$id marqué « " . $STATUSES[$status] . " ».";
|
||||
header('Location: index.php?id=' . $id);
|
||||
exit;
|
||||
}
|
||||
} elseif ($action === 'delete') {
|
||||
$id = (int) ($_POST['id'] ?? 0);
|
||||
$pdo = fws_bugs_db();
|
||||
$st = $pdo->prepare('DELETE FROM bug_reports WHERE id = :id');
|
||||
$st->execute([':id' => $id]);
|
||||
$notice = "Signalement #$id supprimé.";
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$user = current_user();
|
||||
|
||||
/* ───────────────────────── routage vues ───────────────────────── */
|
||||
|
||||
try {
|
||||
$pdo = fws_bugs_db();
|
||||
} catch (Throwable $e) {
|
||||
http_response_code(500);
|
||||
exit('Erreur base de données : ' . h($e->getMessage()));
|
||||
}
|
||||
|
||||
$view_id = (int) ($_GET['id'] ?? 0);
|
||||
|
||||
/* Compteurs pour les filtres */
|
||||
$counts = ['tous' => 0];
|
||||
foreach ($STATUSES as $k => $v) {
|
||||
$counts[$k] = 0;
|
||||
}
|
||||
foreach ($pdo->query('SELECT status, COUNT(*) c FROM bug_reports GROUP BY status') as $row) {
|
||||
$s = $row['status'];
|
||||
if (isset($counts[$s])) {
|
||||
$counts[$s] = (int) $row['c'];
|
||||
$counts['tous'] += (int) $row['c'];
|
||||
}
|
||||
}
|
||||
|
||||
/* ───────────────────────── affichage ───────────────────────── */
|
||||
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
?><!doctype html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="robots" content="noindex,nofollow">
|
||||
<title>Signalements — FWS Linux</title>
|
||||
<style>
|
||||
:root { color-scheme: dark; }
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0; font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
|
||||
background: #0b1120; color: #e2e8f0; line-height: 1.5;
|
||||
}
|
||||
.wrap { max-width: 1000px; margin: 0 auto; padding: 24px 16px 80px; }
|
||||
header.top {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
gap: 12px; padding: 14px 0; border-bottom: 1px solid #1e293b; margin-bottom: 20px;
|
||||
}
|
||||
header.top h1 { font-size: 1.15rem; margin: 0; }
|
||||
header.top .sub { font-size: .8rem; color: #94a3b8; }
|
||||
a { color: #38bdf8; text-decoration: none; }
|
||||
a:hover { text-decoration: underline; }
|
||||
.btn {
|
||||
display: inline-block; border: 1px solid #334155; background: #1e293b; color: #e2e8f0;
|
||||
padding: 6px 12px; border-radius: 8px; cursor: pointer; font-size: .85rem;
|
||||
text-decoration: none; line-height: 1.4;
|
||||
}
|
||||
.btn:hover { background: #334155; text-decoration: none; }
|
||||
.btn.primary { background: #0284c7; border-color: #0284c7; color: #fff; }
|
||||
.btn.danger { background: transparent; border-color: #7f1d1d; color: #f87171; }
|
||||
.btn.danger:hover { background: #7f1d1d; color: #fff; }
|
||||
.btn.small { padding: 3px 9px; font-size: .78rem; }
|
||||
.card {
|
||||
background: #111a2e; border: 1px solid #1e293b; border-radius: 12px;
|
||||
padding: 16px; margin-bottom: 12px;
|
||||
}
|
||||
.card h3 { margin: 0 0 4px; font-size: 1rem; }
|
||||
.card .meta { font-size: .78rem; color: #94a3b8; display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 8px; }
|
||||
.badge {
|
||||
display: inline-block; padding: 1px 9px; border-radius: 99px; font-size: .75rem; font-weight: 600;
|
||||
}
|
||||
.badge.type { background: #1e293b; color: #cbd5e1; }
|
||||
.flash { padding: 10px 14px; border-radius: 10px; margin-bottom: 14px; font-size: .9rem; }
|
||||
.flash.err { background: #450a0a; color: #fca5a5; border: 1px solid #7f1d1d; }
|
||||
.flash.ok { background: #052e16; color: #86efac; border: 1px solid #166534; }
|
||||
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
|
||||
.filters a { border: 1px solid #334155; padding: 4px 12px; border-radius: 99px; font-size: .82rem; }
|
||||
.filters a.active { background: #0284c7; border-color: #0284c7; color: #fff; }
|
||||
.empty { text-align: center; color: #64748b; padding: 50px 0; }
|
||||
pre.logs {
|
||||
background: #0b1120; border: 1px solid #1e293b; border-radius: 8px;
|
||||
padding: 12px; overflow: auto; max-height: 360px; font-size: .78rem; white-space: pre-wrap; word-break: break-word;
|
||||
}
|
||||
table.detail td { padding: 6px 0; vertical-align: top; }
|
||||
table.detail td.k { color: #94a3b8; font-size: .8rem; width: 110px; padding-right: 12px; }
|
||||
form.inline { display: inline; }
|
||||
.status-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; align-items: center; }
|
||||
/* login */
|
||||
.login-box { max-width: 380px; margin: 12vh auto; }
|
||||
.login-box .card { padding: 28px; }
|
||||
.login-box h1 { font-size: 1.25rem; margin-top: 0; }
|
||||
input[type=text], input[type=password] {
|
||||
width: 100%; padding: 9px 11px; margin: 6px 0 14px;
|
||||
background: #0b1120; border: 1px solid #334155; border-radius: 8px; color: #e2e8f0; font-size: .95rem;
|
||||
}
|
||||
label { font-size: .85rem; color: #cbd5e1; }
|
||||
.back { margin-bottom: 12px; display: inline-block; }
|
||||
.pager { display: flex; gap: 8px; margin-top: 16px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
|
||||
<?php if ($user === null): /* ─────────── LOGIN ─────────── */ ?>
|
||||
|
||||
<div class="login-box">
|
||||
<div class="card">
|
||||
<h1>🔒 Panneau des signalements</h1>
|
||||
<?php if ($error): ?><div class="flash err"><?= h($error) ?></div><?php endif; ?>
|
||||
<?php if ($counts['tous'] === 0): ?>
|
||||
<p style="font-size:.85rem;color:#94a3b8">
|
||||
Aucun compte admin pour l'instant.<br>Créez-le dans le conteneur :
|
||||
</p>
|
||||
<pre class="logs">docker exec -it fws-files php /var/www/html/admin/bugs/init.php</pre>
|
||||
<?php endif; ?>
|
||||
<form method="post" autocomplete="on">
|
||||
<input type="hidden" name="action" value="login">
|
||||
<label for="username">Nom d'utilisateur</label>
|
||||
<input type="text" id="username" name="username" required autofocus>
|
||||
<label for="password">Mot de passe</label>
|
||||
<input type="password" id="password" name="password" required>
|
||||
<button class="btn primary" type="submit" style="width:100%;padding:10px">Se connecter</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php else: /* ─────────── ESPACE ADMIN ─────────── */ ?>
|
||||
|
||||
<header class="top">
|
||||
<div>
|
||||
<h1>🐞 Signalements de bugs</h1>
|
||||
<div class="sub">Connecté : <?= h($user['name']) ?> · Base SQLite · <a href="../../api/bugs.php" target="_blank">tester l'API</a></div>
|
||||
</div>
|
||||
<form class="inline" method="post">
|
||||
<input type="hidden" name="action" value="logout">
|
||||
<input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
|
||||
<button class="btn" type="submit">Déconnexion</button>
|
||||
</form>
|
||||
</header>
|
||||
|
||||
<?php if ($error): ?><div class="flash err"><?= h($error) ?></div><?php endif; ?>
|
||||
<?php if ($notice): ?><div class="flash ok"><?= h($notice) ?></div><?php endif; ?>
|
||||
|
||||
<?php if ($view_id > 0): /* ─────────── DÉTAIL ─────────── */
|
||||
|
||||
$st = $pdo->prepare('SELECT * FROM bug_reports WHERE id = :id');
|
||||
$st->execute([':id' => $view_id]);
|
||||
$r = $st->fetch();
|
||||
if ($r === false):
|
||||
?>
|
||||
<p class="empty">Signalement #<?= $view_id ?> introuvable.</p>
|
||||
<a href="index.php" class="btn">← Retour à la liste</a>
|
||||
<?php else: ?>
|
||||
<a href="index.php" class="back">← Retour à la liste</a>
|
||||
<div class="card">
|
||||
<div style="display:flex;justify-content:space-between;gap:10px;flex-wrap:wrap;align-items:baseline">
|
||||
<h3>#<?= (int) $r['id'] ?> — <?= h($r['title'] !== '' ? $r['title'] : '(sans titre)') ?></h3>
|
||||
<span class="badge" style="background:<?= $STATUS_COLORS[$r['status']] ?>33;color:<?= $STATUS_COLORS[$r['status']] ?>"><?= h($STATUSES[$r['status']] ?? $r['status']) ?></span>
|
||||
</div>
|
||||
<div class="meta">
|
||||
<span>📅 <?= h(format_date($r['created_at'])) ?></span>
|
||||
<span>📦 <?= h($r['app'] !== '' ? $r['app'] : '?') ?> <?= h($r['version']) ?></span>
|
||||
<span class="badge type"><?= h($r['type']) ?></span>
|
||||
</div>
|
||||
|
||||
<?php if ($r['description'] !== ''): ?>
|
||||
<p style="white-space:pre-wrap;margin:10px 0"><?= h($r['description']) ?></p>
|
||||
<?php endif; ?>
|
||||
|
||||
<table class="detail">
|
||||
<?php if ($r['email'] !== ''): ?>
|
||||
<tr><td class="k">E-mail</td><td><a href="mailto:<?= h($r['email']) ?>"><?= h($r['email']) ?></a></td></tr>
|
||||
<?php endif; ?>
|
||||
<?php if ($r['system_info'] !== ''): ?>
|
||||
<tr><td class="k">Système</td><td style="white-space:pre-wrap"><?= h($r['system_info']) ?></td></tr>
|
||||
<?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>
|
||||
</table>
|
||||
|
||||
<?php if ($r['logs'] !== ''): ?>
|
||||
<div style="margin-top:12px">
|
||||
<div style="font-size:.8rem;color:#94a3b8;margin-bottom:6px">📜 Logs :</div>
|
||||
<pre class="logs"><?= h($r['logs']) ?></pre>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="status-row">
|
||||
<span style="font-size:.85rem;color:#94a3b8">Changer le statut :</span>
|
||||
<?php foreach ($STATUSES as $sk => $sv):
|
||||
if ($sk === $r['status']) continue; ?>
|
||||
<form class="inline" method="post">
|
||||
<input type="hidden" name="action" value="set_status">
|
||||
<input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
|
||||
<input type="hidden" name="id" value="<?= (int) $r['id'] ?>">
|
||||
<input type="hidden" name="status" value="<?= $sk ?>">
|
||||
<button class="btn small" type="submit"><?= h($sv) ?></button>
|
||||
</form>
|
||||
<?php endforeach; ?>
|
||||
<span style="flex:1"></span>
|
||||
<form class="inline" method="post" onsubmit="return confirm('Supprimer définitivement le signalement #<?= (int) $r['id'] ?> ?')">
|
||||
<input type="hidden" name="action" value="delete">
|
||||
<input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
|
||||
<input type="hidden" name="id" value="<?= (int) $r['id'] ?>">
|
||||
<button class="btn small danger" type="submit">🗑 Supprimer</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; /* fin détail */ ?>
|
||||
|
||||
<?php else: /* ─────────── LISTE ─────────── */
|
||||
|
||||
$filter = $_GET['status'] ?? 'tous';
|
||||
if (!isset($counts[$filter])) $filter = 'tous';
|
||||
$page = max(1, (int) ($_GET['page'] ?? 1));
|
||||
$perPage = 25;
|
||||
|
||||
$where = '';
|
||||
$params = [];
|
||||
if ($filter !== 'tous') {
|
||||
$where = 'WHERE status = :status';
|
||||
$params[':status'] = $filter;
|
||||
}
|
||||
$st = $pdo->prepare("SELECT COUNT(*) FROM bug_reports $where");
|
||||
$st->execute($params);
|
||||
$total = (int) $st->fetchColumn();
|
||||
$pages = max(1, (int) ceil($total / $perPage));
|
||||
$page = min($page, $pages);
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
$st = $pdo->prepare(
|
||||
"SELECT * FROM bug_reports $where ORDER BY id DESC LIMIT :lim OFFSET :off"
|
||||
);
|
||||
$st->bindValue(':lim', $perPage, PDO::PARAM_INT);
|
||||
$st->bindValue(':off', $offset, PDO::PARAM_INT);
|
||||
foreach ($params as $k => $v) $st->bindValue($k, $v);
|
||||
$st->execute();
|
||||
$reports = $st->fetchAll();
|
||||
?>
|
||||
|
||||
<div class="filters">
|
||||
<?php foreach ($counts as $k => $c): ?>
|
||||
<a href="index.php?status=<?= $k ?>" class="<?= $filter === $k ? 'active' : '' ?>">
|
||||
<?= $k === 'tous' ? 'Tous' : h($STATUSES[$k]) ?> (<?= $c ?>)
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
|
||||
<?php if ($total === 0): ?>
|
||||
<p class="empty">Aucun signalement <?= $filter !== 'tous' ? 'avec ce statut' : '' ?> pour le moment. 🎉</p>
|
||||
<?php else: ?>
|
||||
<?php foreach ($reports as $r): ?>
|
||||
<a href="index.php?id=<?= (int) $r['id'] ?>" style="text-decoration:none;color:inherit">
|
||||
<div class="card">
|
||||
<div style="display:flex;justify-content:space-between;gap:10px;flex-wrap:wrap;align-items:baseline">
|
||||
<h3>#<?= (int) $r['id'] ?> — <?= h($r['title'] !== '' ? $r['title'] : '(sans titre)') ?></h3>
|
||||
<span class="badge" style="background:<?= $STATUS_COLORS[$r['status']] ?>33;color:<?= $STATUS_COLORS[$r['status']] ?>"><?= h($STATUSES[$r['status']] ?? $r['status']) ?></span>
|
||||
</div>
|
||||
<div class="meta">
|
||||
<span>📅 <?= h(format_date($r['created_at'])) ?></span>
|
||||
<span>📦 <?= h($r['app'] !== '' ? $r['app'] : '?') ?> <?= h($r['version']) ?></span>
|
||||
<span class="badge type"><?= h($r['type']) ?></span>
|
||||
<?php if ($r['email'] !== ''): ?><span>✉️ <?= h($r['email']) ?></span><?php endif; ?>
|
||||
</div>
|
||||
<?php if ($r['description'] !== ''): ?>
|
||||
<p style="margin:4px 0 0;font-size:.9rem;color:#cbd5e1"><?= h(excerpt($r['description'])) ?></p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<?php if ($pages > 1): ?>
|
||||
<div class="pager">
|
||||
<?php if ($page > 1): ?><a class="btn" href="index.php?status=<?= $filter ?>&page=<?= $page - 1 ?>">← Précédent</a><?php endif; ?>
|
||||
<span style="align-self:center;font-size:.85rem;color:#94a3b8">Page <?= $page ?> / <?= $pages ?></span>
|
||||
<?php if ($page < $pages): ?><a class="btn" href="index.php?status=<?= $filter ?>&page=<?= $page + 1 ?>">Suivant →</a><?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php endif; /* fin liste */ ?>
|
||||
|
||||
<?php endif; /* fin espace admin */ ?>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* FWS Linux — Création / réinitialisation des comptes admin du panneau des signalements.
|
||||
*
|
||||
* À lancer DANS le conteneur (uniquement en CLI) :
|
||||
*
|
||||
* docker exec -it fws-files php /var/www/html/admin/bugs/init.php
|
||||
*
|
||||
* Utilisation :
|
||||
* php init.php # création interactive (1er compte)
|
||||
* php init.php --user=admin --pass=mdp # création non interactive
|
||||
* php init.php --reset --user=admin --pass=nouveaumdp
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
http_response_code(403);
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
exit("Accès interdit : script CLI uniquement.\n");
|
||||
}
|
||||
|
||||
require __DIR__ . '/../../api/bugs/db.php';
|
||||
|
||||
function prompt(string $label): string
|
||||
{
|
||||
echo $label;
|
||||
$line = fgets(STDIN);
|
||||
return $line === false ? '' : rtrim($line, "\r\n");
|
||||
}
|
||||
|
||||
$args = $_SERVER['argv'] ?? [];
|
||||
$user = null;
|
||||
$pass = null;
|
||||
$reset = false;
|
||||
|
||||
foreach ($args as $i => $arg) {
|
||||
if (str_starts_with($arg, '--user=')) {
|
||||
$user = substr($arg, 7);
|
||||
} elseif (str_starts_with($arg, '--pass=')) {
|
||||
$pass = substr($arg, 7);
|
||||
} elseif ($arg === '--reset') {
|
||||
$reset = true;
|
||||
}
|
||||
}
|
||||
|
||||
echo "── Signalements FWS — gestion des comptes admin ──\n\n";
|
||||
|
||||
try {
|
||||
$pdo = fws_bugs_db();
|
||||
} catch (Throwable $e) {
|
||||
fwrite(STDERR, 'Erreur base de données : ' . $e->getMessage() . "\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$count = (int) $pdo->query('SELECT COUNT(*) FROM admin_users')->fetchColumn();
|
||||
echo 'Comptes admin existants : ' . $count . "\n\n";
|
||||
|
||||
/* Mode interactif */
|
||||
if ($user === null) {
|
||||
$user = trim(prompt('Nom d\'utilisateur : '));
|
||||
$pass = prompt('Mot de passe (min. 8 caractères) : ');
|
||||
if ($count > 0) {
|
||||
$confirm = strtolower(trim(prompt('Un compte existe déjà : voulez-vous AJOUTER ce compte ? (oui/non) : ')));
|
||||
if ($confirm !== 'oui' && $confirm !== 'o' && $confirm !== 'y' && $confirm !== 'yes') {
|
||||
exit("Annulé.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$user = trim((string) $user);
|
||||
if ($user === '' || !preg_match('/^[A-Za-z0-9_.\-]{2,32}$/', $user)) {
|
||||
fwrite(STDERR, "Nom d'utilisateur invalide (2 à 32 caractères : lettres, chiffres, . _ -).\n");
|
||||
exit(1);
|
||||
}
|
||||
if (strlen((string) $pass) < 8) {
|
||||
fwrite(STDERR, "Mot de passe trop court (8 caractères minimum).\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare('SELECT id FROM admin_users WHERE username = :u');
|
||||
$stmt->execute([':u' => $user]);
|
||||
$existing = $stmt->fetch();
|
||||
|
||||
$hash = password_hash((string) $pass, PASSWORD_DEFAULT);
|
||||
|
||||
if ($existing !== false) {
|
||||
if (!$reset) {
|
||||
fwrite(STDERR, "L'utilisateur « $user » existe déjà. Utilisez --reset pour changer son mot de passe.\n");
|
||||
exit(1);
|
||||
}
|
||||
$up = $pdo->prepare('UPDATE admin_users SET password_hash = :h WHERE id = :id');
|
||||
$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";
|
||||
}
|
||||
|
||||
echo "\nPanneau : https://fwslinux.fr/admin/bugs/\n";
|
||||
exit(0);
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
/**
|
||||
* FWS Linux — API publique de signalement de bugs.
|
||||
*
|
||||
* POST https://fwslinux.fr/api/bugs.php (JSON ou formulaire)
|
||||
* GET https://fwslinux.fr/api/bugs.php (petite page de test dans le navigateur)
|
||||
*
|
||||
* CORS ouvert : on accepte les requêtes de n'importe quelle origine
|
||||
* (curl, app d'installation, script, site…).
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/bugs/db.php';
|
||||
|
||||
/* Jamais d'avertissements/stack traces PHP visibles côté client */
|
||||
ini_set('display_errors', '0');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type, X-Requested-With');
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
||||
|
||||
/* Pré-vol CORS (fetch depuis le navigateur) */
|
||||
if ($method === 'OPTIONS') {
|
||||
http_response_code(204);
|
||||
exit;
|
||||
}
|
||||
|
||||
/* GET → page HTML de test, pratique pour envoyer un signalement depuis un navigateur */
|
||||
if ($method === 'GET') {
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
render_test_page();
|
||||
exit;
|
||||
}
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
if ($method !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['ok' => false, 'error' => 'Méthode non autorisée. Utilisez POST.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
/* Anti-spam : champ piège « website » rempli par les robots → on ignore sans le dire */
|
||||
$honeypot = trim((string) ($_POST['website'] ?? ''));
|
||||
if ($honeypot !== '') {
|
||||
http_response_code(201);
|
||||
echo json_encode(['ok' => true, 'id' => null, 'message' => 'Merci.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
/* Lecture du corps : JSON ou formulaire classique */
|
||||
$rawBody = file_get_contents('php://input');
|
||||
$ct = $_SERVER['CONTENT_TYPE'] ?? '';
|
||||
$data = $_POST;
|
||||
|
||||
if (stripos($ct, 'application/json') !== false) {
|
||||
$decoded = json_decode($rawBody === false ? '' : $rawBody, true);
|
||||
if (!is_array($decoded)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['ok' => false, 'error' => 'JSON invalide.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
$data = $decoded;
|
||||
}
|
||||
|
||||
/* Champs acceptés, avec des alias tolérés */
|
||||
$fields = [
|
||||
'app' => strval($data['app'] ?? $data['application'] ?? ''),
|
||||
'version' => strval($data['version'] ?? ''),
|
||||
'type' => strval($data['type'] ?? $data['category'] ?? 'bug'),
|
||||
'title' => strval($data['title'] ?? $data['subject'] ?? ''),
|
||||
'description' => strval($data['description'] ?? $data['message'] ?? $data['details'] ?? ''),
|
||||
'email' => strval($data['email'] ?? $data['contact'] ?? ''),
|
||||
'system_info' => strval($data['system_info'] ?? $data['system'] ?? $data['hardware'] ?? ''),
|
||||
'logs' => strval($data['logs'] ?? $data['log'] ?? $data['journal'] ?? ''),
|
||||
];
|
||||
|
||||
$title = mb_substr(trim($fields['title']), 0, 200);
|
||||
$description = mb_substr(trim($fields['description']), 0, 20000);
|
||||
$logs = mb_substr(trim($fields['logs']), 0, 150000);
|
||||
$app = mb_substr(trim($fields['app']), 0, 100);
|
||||
$version = mb_substr(trim($fields['version']), 0, 100);
|
||||
$email = mb_substr(trim($fields['email']), 0, 200);
|
||||
$systemInfo = mb_substr(trim($fields['system_info']), 0, 4000);
|
||||
$type = mb_substr(trim($fields['type']), 0, 50);
|
||||
|
||||
/* Un signalement doit contenir au moins un peu de contenu utile */
|
||||
if ($title === '' && $description === '' && $logs === '') {
|
||||
http_response_code(400);
|
||||
echo json_encode([
|
||||
'ok' => false,
|
||||
'error' => 'Contenu vide : envoyez au moins un titre, une description ou des logs.',
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
/* Anti-flood léger : 25 signalements / heure / IP */
|
||||
try {
|
||||
$pdo = fws_bugs_db();
|
||||
} catch (Throwable $e) {
|
||||
error_log('[fws-bugs] ' . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['ok' => false, 'error' => 'Erreur interne de la base de données.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
$ip = fws_bugs_client_ip();
|
||||
if (fws_bugs_count_recent_for_ip($pdo, $ip) >= 25) {
|
||||
http_response_code(429);
|
||||
echo json_encode(['ok' => false, 'error' => 'Trop de signalements depuis cette adresse, réessayez plus tard.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
/* Insertion */
|
||||
try {
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO bug_reports
|
||||
(app, version, type, title, description, email, system_info, logs, ip, user_agent)
|
||||
VALUES
|
||||
(:app, :version, :type, :title, :description, :email, :system_info, :logs, :ip, :ua)'
|
||||
);
|
||||
$stmt->execute([
|
||||
':app' => $app,
|
||||
':version' => $version,
|
||||
':type' => $type !== '' ? $type : 'bug',
|
||||
':title' => $title,
|
||||
':description' => $description,
|
||||
':email' => $email,
|
||||
':system_info' => $systemInfo,
|
||||
':logs' => $logs,
|
||||
':ip' => $ip,
|
||||
':ua' => mb_substr((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 300),
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
error_log('[fws-bugs] insertion : ' . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['ok' => false, 'error' => 'Erreur interne lors de l\'enregistrement.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(201);
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'id' => (int) $pdo->lastInsertId(),
|
||||
'message' => 'Signalement enregistré. Merci !',
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
|
||||
/* ──────────────────────────────────────────── */
|
||||
|
||||
function render_test_page(): void
|
||||
{
|
||||
$base = htmlspecialchars(strtok($_SERVER['REQUEST_URI'] ?? '/api/bugs.php', '?'), ENT_QUOTES, 'UTF-8');
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Envoyer un signalement — FWS Linux</title>
|
||||
<style>
|
||||
:root { color-scheme: light dark; }
|
||||
body { font-family: system-ui, sans-serif; max-width: 720px; margin: 40px auto; padding: 0 16px; line-height: 1.5; }
|
||||
h1 { font-size: 1.4rem; }
|
||||
label { display: block; margin: 14px 0 4px; font-weight: 600; }
|
||||
input, select, textarea { width: 100%; padding: 8px; box-sizing: border-box; border-radius: 6px; border: 1px solid #888; font: inherit; }
|
||||
textarea { min-height: 90px; }
|
||||
button { margin-top: 18px; padding: 10px 18px; border: 0; border-radius: 8px; background: #1f6feb; color: #fff; font-size: 1rem; cursor: pointer; }
|
||||
#result { margin-top: 16px; padding: 12px; border-radius: 8px; white-space: pre-wrap; font-family: monospace; font-size: .85rem; }
|
||||
#result.ok { background: #143d1e; color: #7ee787; }
|
||||
#result.err { background: #3d1414; color: #ffa198; }
|
||||
.note { font-size: .85rem; opacity: .7; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>🐞 Envoyer un signalement</h1>
|
||||
<p class="note">Page de test — le vrai envoi est fait par l'application (curl / script) sur <code>POST <?= $base ?></code>.</p>
|
||||
<form id="f">
|
||||
<label>Application / outil
|
||||
<input name="app" placeholder="ex : installateur FWS, PacManage, DeskManager, site…">
|
||||
</label>
|
||||
<label>Version
|
||||
<input name="version" placeholder="ex : 0.6.0">
|
||||
</label>
|
||||
<label>Type
|
||||
<select name="type">
|
||||
<option value="bug">Bug</option>
|
||||
<option value="crash">Crash</option>
|
||||
<option value="installation">Problème d'installation</option>
|
||||
<option value="suggestion">Suggestion</option>
|
||||
<option value="autre">Autre</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>Titre *
|
||||
<input name="title" required placeholder="Résumé court du problème">
|
||||
</label>
|
||||
<label>Description
|
||||
<textarea name="description" placeholder="Ce qui s'est passé, les étapes pour reproduire…"></textarea>
|
||||
</label>
|
||||
<label>E-mail (optionnel)
|
||||
<input name="email" type="email" placeholder="pour être recontacté">
|
||||
</label>
|
||||
<label>Système / matériel
|
||||
<input name="system_info" placeholder="ex : CPU, GPU, RAM, mode BIOS/UEFI…">
|
||||
</label>
|
||||
<label>Logs (optionnel)
|
||||
<textarea name="logs" placeholder="collez la sortie du journal d'installation…"></textarea>
|
||||
</label>
|
||||
<input name="website" style="display:none" tabindex="-1" autocomplete="off">
|
||||
<button type="submit">Envoyer le signalement</button>
|
||||
</form>
|
||||
<div id="result" hidden></div>
|
||||
<script>
|
||||
const form = document.getElementById('f');
|
||||
const res = document.getElementById('result');
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const body = Object.fromEntries(new FormData(form).entries());
|
||||
delete body.website;
|
||||
try {
|
||||
const r = await fetch(location.href, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
const j = await r.json();
|
||||
res.hidden = false;
|
||||
res.className = r.ok ? 'ok' : 'err';
|
||||
res.textContent = JSON.stringify(j, null, 2);
|
||||
} catch (err) {
|
||||
res.hidden = false;
|
||||
res.className = 'err';
|
||||
res.textContent = 'Erreur réseau : ' + err;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
# La base de données ne doit jamais être commitée
|
||||
*.sqlite
|
||||
*.sqlite-wal
|
||||
*.sqlite-shm
|
||||
@@ -0,0 +1,3 @@
|
||||
# Bloque tout accès direct (navigateur, curl…) à la base SQLite.
|
||||
# Seul le code PHP du serveur (api/admin) peut lire ce répertoire.
|
||||
Require all denied
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* FWS Linux — base de données des signalements de bugs.
|
||||
* SQLite, fichier stocké dans api/bugs/data/ (répertoire protégé par .htaccess).
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function fws_bugs_db(): PDO
|
||||
{
|
||||
static $pdo = null;
|
||||
if ($pdo instanceof PDO) {
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
$dir = __DIR__ . '/data';
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0775, true);
|
||||
}
|
||||
|
||||
$pdo = new PDO('sqlite:' . $dir . '/fws_bugs.sqlite');
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
|
||||
/* Sur un bind-mount Docker Desktop, tout appartient à root : on rend le
|
||||
fichier DB inscriptible par Apache (www-data) quoi qu'il arrive. */
|
||||
@chmod($dir . '/fws_bugs.sqlite', 0666);
|
||||
@chmod($dir, 0777);
|
||||
|
||||
/* Pas de WAL : sur un bind-mount Docker Desktop, les fichiers -wal/-shm
|
||||
créés tantôt par root (CLI) tantôt par www-data (Apache) se marchent
|
||||
dessus. Le mode journal classique + fichier 666 est plus fiable. */
|
||||
$pdo->exec('PRAGMA journal_mode = DELETE');
|
||||
$pdo->exec('PRAGMA busy_timeout = 5000');
|
||||
|
||||
fws_bugs_schema($pdo);
|
||||
@chmod($dir . '/fws_bugs.sqlite', 0666);
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
function fws_bugs_schema(PDO $pdo): void
|
||||
{
|
||||
$pdo->exec(
|
||||
'CREATE TABLE IF NOT EXISTS bug_reports (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
app TEXT NOT NULL DEFAULT \'\',
|
||||
version TEXT NOT NULL DEFAULT \'\',
|
||||
type TEXT NOT NULL DEFAULT \'bug\',
|
||||
title TEXT NOT NULL DEFAULT \'\',
|
||||
description TEXT NOT NULL DEFAULT \'\',
|
||||
email TEXT NOT NULL DEFAULT \'\',
|
||||
system_info TEXT NOT NULL DEFAULT \'\',
|
||||
logs TEXT NOT NULL DEFAULT \'\',
|
||||
ip TEXT NOT NULL DEFAULT \'\',
|
||||
user_agent TEXT NOT NULL DEFAULT \'\',
|
||||
status TEXT NOT NULL DEFAULT \'nouveau\',
|
||||
created_at TEXT NOT NULL DEFAULT (datetime(\'now\'))
|
||||
)'
|
||||
);
|
||||
$pdo->exec('CREATE INDEX IF NOT EXISTS idx_reports_created ON bug_reports(created_at DESC)');
|
||||
$pdo->exec('CREATE INDEX IF NOT EXISTS idx_reports_status ON bug_reports(status)');
|
||||
|
||||
$pdo->exec(
|
||||
'CREATE TABLE IF NOT EXISTS admin_users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime(\'now\'))
|
||||
)'
|
||||
);
|
||||
|
||||
/* Schéma (re)créé : s'assurer que le fichier reste inscriptible par Apache */
|
||||
@chmod(__DIR__ . '/data/fws_bugs.sqlite', 0666);
|
||||
}
|
||||
|
||||
/**
|
||||
* Nombre de signalements pour une IP sur la dernière heure (anti-flood).
|
||||
*/
|
||||
function fws_bugs_count_recent_for_ip(PDO $pdo, string $ip): int
|
||||
{
|
||||
$st = $pdo->prepare(
|
||||
"SELECT COUNT(*) FROM bug_reports
|
||||
WHERE ip = :ip AND created_at > datetime('now', '-1 hour')"
|
||||
);
|
||||
$st->execute([':ip' => $ip]);
|
||||
return (int) $st->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* IP réelle en tenant compte du proxy (X-Forwarded-For).
|
||||
*/
|
||||
function fws_bugs_client_ip(): string
|
||||
{
|
||||
$xff = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? '';
|
||||
if ($xff !== '') {
|
||||
$parts = explode(',', $xff);
|
||||
$ip = trim($parts[0]);
|
||||
if ($ip !== '') {
|
||||
return substr($ip, 0, 64);
|
||||
}
|
||||
}
|
||||
return substr((string) ($_SERVER['REMOTE_ADDR'] ?? ''), 0, 64);
|
||||
}
|
||||
Reference in new issue
Block a user