<?php
require_once 'inc/config.php';

header('Content-Type: text/plain; charset=utf-8');

$secretToken = 'serverintern';

$ct = $_SERVER['CONTENT_TYPE'] ?? $_SERVER['HTTP_CONTENT_TYPE'] ?? '';
if (stripos($ct, 'application/json') !== false) {
    $raw    = file_get_contents('php://input');
    $input  = json_decode($raw, true);
    if (!is_array($input)) $input = [];
} else {
    $input = $_POST;
}

if (!isset($input['token']) || $input['token'] !== $secretToken) {
    http_response_code(403);
    exit('Forbidden');
}

$uploadId      = trim($input['uploadId'] ?? '');
$uploader      = trim($input['uploader_email'] ?? '');
$recipientRaw  = trim($input['recipient_email'] ?? '');

$recipients = preg_split('/[\s,;]+/', $recipientRaw);
$recipients = array_unique(array_filter(array_map('trim', $recipients), function ($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}));

$fileData = file_exists($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
if (!isset($fileData[$uploadId])) {
    $fileData[$uploadId] = [
        'uploader_email'  => $uploader,
        'recipient_email' => $recipients,
        'keys'            => []
    ];
}

if (!isset($fileData[$uploadId]['keys']) || !is_array($fileData[$uploadId]['keys'])) {
    $fileData[$uploadId]['keys'] = [];
}

if (!in_array($key, $fileData[$uploadId]['keys'])) {
    $fileData[$uploadId]['keys'][] = $key;
}

file_put_contents($dataFile, json_encode($fileData, JSON_PRETTY_PRINT));
http_response_code(200);
