<?php
$requestsFile = __DIR__ . '/data/requests.txt';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: /requests.php', true, 302);
    exit;
}
$name = trim((string)($_POST['name'] ?? ''));
$song = trim((string)($_POST['song'] ?? ''));
$message = trim((string)($_POST['message'] ?? ''));
if ($name === '' || $message === '') {
    header('Location: /requests.php', true, 302);
    exit;
}
$id = uniqid('req_', true);
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
$timestamp = date('Y-m-d H:i:s');
$approved = 'no';
$line = implode('|', [$id, str_replace('|','/',$name), str_replace('|','/',$song), str_replace('|','/',$message), str_replace('|','/',$ip), $timestamp, $approved]) . PHP_EOL;
file_put_contents($requestsFile, $line, FILE_APPEND | LOCK_EX);
header('Location: /requests.php?sent=1', true, 302);
exit;
