<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <meta content="width=device-width, initial-scale=1" name="viewport"/>
        <link rel="stylesheet" href="main.css"/>
        <title>The Safest Space Social Network</title>
    </head>
    <body>
        <header class="mainHeader">The Safest Space Social Network</header>

<br></br>

<!-- HTML form for submitting a comment -->
<form method="post">
  <textarea name="comment"></textarea>
  <button type="submit" name="submit">Post Comment</button>
</form>


<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

if(isset($_SERVER['REMOTE_ADDR'])) {
  $ip_address = $_SERVER['REMOTE_ADDR'];
} else {
  $ip_address = '';
}

$filename = 'comments.csv'; // Name of the CSV file where comments will be saved

// Check if form is submitted
if(isset($_POST['submit'])) {
  $comment = $_POST['comment']; // Get the comment
  $ip_address = $_SERVER['REMOTE_ADDR']; // Get the IP address of the user
  
  // Save the comment and IP address to the CSV file
  $fp = fopen($filename, 'a');
  fputcsv($fp, array($ip_address, $comment));
  fclose($fp);
}

// Check if a comment has been reported
if(isset($_POST['report'])) {
  $row_number = $_POST['row_number']; // Get the row number of the comment
  $rows = array_map('str_getcsv', file($filename)); // Get all the rows from the CSV file
  $rows[$row_number][1] = 'reported post removed'; // Replace the comment with "reported post removed"
  $fp = fopen($filename, 'w'); // Clear the CSV file
  fclose($fp);
  $fp = fopen($filename, 'a'); // Write the modified rows back to the CSV file
  foreach($rows as $row) {
    fputcsv($fp, $row);
  }
  fclose($fp);
}

// Get the IP address of the user
$ip_address = $_SERVER['REMOTE_ADDR'];

// Read the CSV file and display the comments from the same IP address
$rows = array_map('str_getcsv', file($filename));
foreach($rows as $row_number => $row) {
  if($row[0] == $ip_address) { // Display only comments from the same IP address
    echo '<div>';
    echo '<p>'.$row[1].'</p>';
    echo '<form method="post">';
    echo '<input type="hidden" name="row_number" value="'.$row_number.'">';
    echo '<button type="submit" name="report">Report</button>';
    echo '</form>';
    echo '</div>';
  }
}

?>

</body>
</html>
