forked from FWS/FWS-TEMPLATE-Website-OnePage
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
+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