<?php
$name = trim((string)($_POST['name'] ?? ''));
$email = trim((string)($_POST['email'] ?? ''));
$song = trim((string)($_POST['song'] ?? ''));
$artist = trim((string)($_POST['artist'] ?? ''));
$dedication = trim((string)($_POST['dedication'] ?? ''));

$clean = function ($value) {
    $value = str_replace(["\r\n", "\r"], "\n", (string)$value);
    $value = preg_replace("/\n{3,}/", "\n\n", $value);
    return trim($value);
};

$saveFile = __DIR__ . '/data/requests.txt';
$entry = "Name: " . $clean($name) . "\n"
       . "Email: " . $clean($email) . "\n"
       . "Song: " . $clean($song) . "\n"
       . "Artist: " . $clean($artist) . "\n"
       . "Dedication: " . $clean($dedication) . "\n---\n";
file_put_contents($saveFile, $entry, FILE_APPEND | LOCK_EX);

header("Location: requests.php?sent=1");
exit;
