<?php
date_default_timezone_set('America/New_York');

$comment = trim($_POST['comment'] ?? '');
if ($comment === '') {
  die("Empty comment.");
}

// Get user IP
$ip = $_SERVER['REMOTE_ADDR'];

// Check if IP is banned
$bannedFile = '../banned-ips.txt';
$bannedIPs = file_exists($bannedFile) ? file($bannedFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : [];

if (in_array($ip, $bannedIPs)) {
  http_response_code(403);
  die("Your IP has been blocked from commenting.");
}

// Build comment entry
$entry = "IP: $ip\nDate: " . date('Y-m-d H:i:s') . "\n" . htmlspecialchars($comment) . "\nApproved: false";

// Save to file
file_put_contents('../comments.txt', $entry . "\n---\n", FILE_APPEND);

// Redirect
header("Location: ../index.php?submitted=true");
exit;
?>
