forked from FWS/FWS-TEMPLATE-Website-OnePage
The API is consumed by curl/scripts that need to push full installation logs, but field truncation (150k chars) and a 25-per-hour IP cap were rejecting or silently cutting those payloads. Discord log chunks are now streamed instead of being built in memory, so oversized logs no longer blow up the notifier.
278 lines
11 KiB
PHP
278 lines
11 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)
|
|
*
|
|
* Aucun plafond de taille ni de débit : l'API est faite pour curl/scripts
|
|
* (envoi de logs volumineux) et les seuls garde-fous restants sont le champ
|
|
* piège « website » et le contrôle de contenu vide.
|
|
*
|
|
* 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';
|
|
require __DIR__ . '/bugs/discord.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'] ?? ''),
|
|
];
|
|
|
|
/* Aucun plafond sur les champs : les logs d'installation peuvent peser
|
|
très lourd et un client curl doit pouvoir tout envoyer. */
|
|
$title = trim($fields['title']);
|
|
$description = trim($fields['description']);
|
|
$logs = trim($fields['logs']);
|
|
$app = trim($fields['app']);
|
|
$version = trim($fields['version']);
|
|
$email = trim($fields['email']);
|
|
$systemInfo = trim($fields['system_info']);
|
|
$type = trim($fields['type']);
|
|
|
|
/* 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;
|
|
}
|
|
|
|
/* Connexion à la base (aucun anti-flood : l'API est ouverte par design) */
|
|
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();
|
|
|
|
/* Insertion — l'id repart de 1 et saute ceux déjà pris */
|
|
try {
|
|
$bugId = fws_bugs_next_id($pdo, 'bug_reports');
|
|
$stmt = $pdo->prepare(
|
|
'INSERT INTO bug_reports
|
|
(id, app, version, type, title, description, email, system_info, logs, ip, user_agent)
|
|
VALUES
|
|
(:id, :app, :version, :type, :title, :description, :email, :system_info, :logs, :ip, :ua)'
|
|
);
|
|
$stmt->execute([
|
|
':id' => $bugId,
|
|
':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' => $bugId,
|
|
'message' => 'Signalement enregistré. Merci !',
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
/* Envoie la réponse au client tout de suite ; la notif Discord suit en arrière-plan. */
|
|
if (function_exists('fastcgi_finish_request')) {
|
|
fastcgi_finish_request();
|
|
} else {
|
|
flush();
|
|
}
|
|
|
|
/* Notification Discord (best-effort : n'affecte jamais la réponse de l'API) */
|
|
try {
|
|
$discord = fws_bugs_discord_notify([
|
|
'id' => $bugId,
|
|
'app' => $app,
|
|
'version' => $version,
|
|
'type' => $type !== '' ? $type : 'bug',
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'email' => $email,
|
|
'system_info' => $systemInfo,
|
|
'logs' => $logs,
|
|
'status' => 'nouveau',
|
|
]);
|
|
if (is_array($discord) && !empty($discord['thread_id'])) {
|
|
fws_bugs_save_discord_info($pdo, $bugId, (string) $discord['thread_id'], (string) ($discord['post_url'] ?? ''));
|
|
}
|
|
} catch (Throwable $e) {
|
|
error_log('[fws-bugs] discord : ' . $e->getMessage());
|
|
}
|
|
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
|
|
}
|