Template
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.
246 lines
9.6 KiB
PHP
246 lines
9.6 KiB
PHP
<?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
|
|
}
|