<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>THE GUESTBOOK</title>
    <link href="style.css" rel="stylesheet">
    <link href="icon.png" rel="icon">
  </head>
  <body id="index">
    <?php
    // define variables and set to empty values
    $name = $email = $capcha = $comment = $website = $nameErr = $hosting = $capchaErr = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["name"])) {
        $nameErr = "Name is required";
      } else {
        $name = "\"".test_input($_POST["name"])."\"";
      }
      $email = "\"".test_input($_POST["email"])."\"";
      $website = "\"".test_input($_POST["website"])."\"";
      $comment = "\"".test_input($_POST["comment"])."\"";
      if ($_POST["capcha"] !== "I love me") {
        $capchaErr = "You stupid fucking dingus";
      } else {
        $capcha = test_input($_POST["capcha"]);
      }
      if(strpos($_SERVER["HTTP_HOST"], ".onion")){
      $hosting = "tor!";
      }
      elseif(strpos($_SERVER["HTTP_HOST"], ".i2p")){
      $hosting = "i2p!";
      }
      elseif(strpos($_SERVER["HTTP_HOST"], ".fai")){
      $hosting = "fai!";
      }
      else{$hosting = "???!";}
      $myfile = fopen("signs.csv", "a") or die("Unable to open file!");
      $txt = $name.",".$website.",".$email.",".$comment.",".date("Y-m-d").",".$hosting."\n";
      fwrite($myfile, $txt);
      fclose($myfile);
    }

    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    ?>
    <div class="centercubepleaseind">
      <h1>Cool signers:</h1>
      <p style="text-align: left;">
      <?php //CSV FRIEND READ
        $csv = 'signs.csv';
        $signers = [];
        $f = fopen($csv, 'r');
        if ($f === false) { die('Cannot open the file ' . $csv); }
        // read each line in CSV file at a time
        while (($row = fgetcsv($f)) !== false) { $signers[] = $row; }
        fclose($f);

        foreach($signers as $datasing){
        echo $datasing[5]."* At <span style=\"font-weight: bold;\">".$datasing[4]."</span> <a href=\"".$datasing[1]."\">".$datasing[0]."</a> said: <span style=\"font-weight: bold;\">".$datasing[3]."</span>.";
	if($datasing[2]){echo " Pester on: <a href=\"mailto:".$datasing[2]."\">".$datasing[2]."</a>";}
        echo "<br>";
        } ?>
      </p>
   </div>
   <div class="centerade">
      <h2>Be part of this cool babes!</h2>
      <p>
      <form style="text-align: center;" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      Name: <input type="text" name="name"><span style="color: red;" class="error">* <?php echo $nameErr;?></span>
      Website: <input type="text" name="website">
      E-mail: <input type="text" name="email"><br>
      Comment: <textarea name="comment" rows="1" cols="100"></textarea><br>
      Capcha(Say me that you love "me"): <input type="text" name="capcha"><span style="color: red;" class="error">* <?php echo $capchaErr;?></span><br>
      <input type="submit">
      </form>
      </p>
    </div>
  </body>
</html>