Files
usbcheck.it/api/result/browser-quick-test.php
2025-11-30 01:48:31 +01:00

166 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// /public/api/result/browser-quick-test.php
declare(strict_types=1);
session_start();
// DB einbinden bitte Pfad an DEINE Struktur anpassen!
require $_SERVER['DOCUMENT_ROOT']. '/../config/fileload.php'; // z.B. stellt $pdo (PDO) bereit
header('Content-Type: application/json; charset=utf-8');
// ---------------------------------------------------------------------
// 1. JSON einlesen
// ---------------------------------------------------------------------
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
if (!is_array($data)) {
http_response_code(400);
echo json_encode([
'ok' => false,
'error' => 'Invalid JSON payload',
]);
exit;
}
// ---------------------------------------------------------------------
// 2. User / Session ermitteln
// ---------------------------------------------------------------------
$userId = $_SESSION['user_id'] ?? null; // abhängig von deiner Login-Implementierung
$isLoggedIn = $userId ? 1 : 0;
$sessionId = session_id() ?: null;
$ipAddress = $_SERVER['REMOTE_ADDR'] ?? null;
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? null;
// ---------------------------------------------------------------------
// 3. Grobe Auswertung aus dem Report (optional)
// → vorerst alles im JSON speichern, Felder in web_quicktests eher neutral lassen
// ---------------------------------------------------------------------
$modeRequested = $data['mode_requested'] ?? 'unknown';
$meta = $data['meta'] ?? [];
// Hier könntest du später Browser/OS parsen
$browserName = null;
$browserVersion = null;
$osName = null;
$osVersion = null;
// Beispiel: Gesamtmenge geschriebener/verifizierter Bytes aggregieren
$measuredBytes = 0;
if (!empty($data['quick']) && is_array($data['quick'])) {
$measuredBytes += (int)($data['quick']['size_bytes'] ?? 0);
}
if (!empty($data['benchmark']) && is_array($data['benchmark'])) {
$measuredBytes += (int)($data['benchmark']['size_bytes'] ?? 0);
}
if (!empty($data['writeverify']) && is_array($data['writeverify'])) {
$measuredBytes += (int)($data['writeverify']['total_bytes'] ?? 0);
}
// Kapazitätsstatus vorerst neutral
$capacityStatus = 'unknown';
// Volume-/Stick-Daten hast du aktuell im Browser noch nicht separat,
// darum bleiben diese Felder (erstmal) NULL:
$volumeLabel = null;
$manufacturer = null;
$modelName = null;
$usbType = null;
$filesystem = null;
// advertised_capacity_bytes kennen wir im Browser noch nicht:
$advCapacityBytes = null;
// test_report_json = kompletter Report
$testReportJson = $raw; // direkt als JSON-String speichern
// ---------------------------------------------------------------------
// 4. Insert in web_quicktests
// ---------------------------------------------------------------------
try {
$sql = "
INSERT INTO web_quicktests (
user_id,
is_logged_in,
usb_device_id,
browser_name,
browser_version,
os_name,
os_version,
volume_label,
manufacturer,
model_name,
usb_type,
advertised_capacity_bytes,
measured_capacity_bytes,
capacity_status,
filesystem,
test_report_json,
ip_address,
session_id
)
VALUES (
:user_id,
:is_logged_in,
:usb_device_id,
:browser_name,
:browser_version,
:os_name,
:os_version,
:volume_label,
:manufacturer,
:model_name,
:usb_type,
:adv_capacity,
:measured_capacity,
:capacity_status,
:filesystem,
CAST(:test_report_json AS JSON),
:ip_address,
:session_id
)
";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'user_id' => $userId,
'is_logged_in' => $isLoggedIn,
'usb_device_id' => null, // Browser-Test ist erstmal nicht an gespeicherten Stick gekoppelt
'browser_name' => $browserName,
'browser_version' => $browserVersion,
'os_name' => $osName,
'os_version' => $osVersion,
'volume_label' => $volumeLabel,
'manufacturer' => $manufacturer,
'model_name' => $modelName,
'usb_type' => $usbType,
'adv_capacity' => $advCapacityBytes,
'measured_capacity' => $measuredBytes ?: null,
'capacity_status' => $capacityStatus,
'filesystem' => $filesystem,
'test_report_json' => $testReportJson,
'ip_address' => $ipAddress,
'session_id' => $sessionId,
]);
$id = (int)$pdo->lastInsertId();
echo json_encode([
'ok' => true,
'id' => $id,
]);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'ok' => false,
'error' => 'DB error',
// 'debug' => $e->getMessage(), // nur zum Debuggen aktivieren
]);
}