This commit is contained in:
2025-11-30 01:48:31 +01:00
parent e6ab5a0a1f
commit 3ebfb8c7f6
4 changed files with 187 additions and 3 deletions

View File

@@ -0,0 +1,155 @@
<?php
// /api/target/browser.quick.test.php
declare(strict_types=1);
function browser_quick_test_handle_request(): array
{
// 1. JSON einlesen
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
if (!is_array($data)) {
http_response_code(400);
return [
'ok' => false,
'error' => 'Invalid JSON payload',
];
}
// 2. Session / User
// (falls index.php evtl. schon session_start() macht, ist das idempotent)
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
$userId = $_SESSION['user_id'] ?? null;
$isLoggedIn = $userId ? 1 : 0;
$sessionId = session_id() ?: null;
$ipAddress = $_SERVER['REMOTE_ADDR'] ?? null;
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? null;
// 3. DB-Verbindung
// Dokumentroot der API-Subdomain zeigt auf /api,
// config liegt ein Level darüber: /config/db.php
require $_SERVER['DOCUMENT_ROOT'] . '/../config/db.php'; // $pdo
if ($pdo instanceof PDO) {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
// 4. Werte aus dem Report aggregieren (minimal)
$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);
}
// Browser/OS & Stick-Infos erstmal noch leer, später aus meta/parsing füllen
$browserName = null;
$browserVersion = null;
$osName = null;
$osVersion = null;
$volumeLabel = null;
$manufacturer = null;
$modelName = null;
$usbType = null;
$filesystem = null;
$advCapacityBytes = null;
$capacityStatus = 'unknown';
// Kompletten Report als JSON-String speichern
$testReportJson = $raw;
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,
:test_report_json,
:ip_address,
:session_id
)
";
$stmt = $pdo->prepare($sql);
$ok = $stmt->execute([
'user_id' => $userId,
'is_logged_in' => $isLoggedIn,
'usb_device_id' => null,
'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,
]);
if (!$ok) {
$info = $stmt->errorInfo();
throw new RuntimeException($info[2] ?? 'Unknown DB error during insert');
}
return [
'ok' => true,
'id' => (int)$pdo->lastInsertId(),
];
} catch (Throwable $e) {
http_response_code(500);
return [
'ok' => false,
'error' => 'DB error: ' . $e->getMessage(),
];
}
}