aboutsummaryrefslogtreecommitdiff
path: root/guestbook/submit.php
blob: 09d040407e353dab6f3cb908f0d21d832c223ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    $returnCode = "success";
    if($_POST["botcheck"] != "isopod") {
      $returnCode = "botcheckfailed";
    } else if(!$_POST["name"]) {
      $returnCode = "noname";
    } else if(!$_POST["message"]) {
      $returnCode = "nomessage";
    } else if($_POST["website"] and !preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $_POST["website"])) {
      $returnCode = "invalidwebsite";
    } else if($_POST["cool"] and !preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $_POST["cool"])) {
      $returnCode = "invalidlink";
    } else {

      date_default_timezone_set("America/Edmonton");
      $currentPost = array(
        "date" => time(),
        "name" => htmlentities($_POST["name"]),
        "message" => htmlentities($_POST["message"]),
        "found" => htmlentities($_POST["found"]),
        "website" => $_POST["website"],
        "cool" => $_POST["cool"],
        "reply" => ""
      );
      
      $filename = "posts.json";

      $handle = @fopen($filename, 'r+');

      // create the file if needed
      if ($handle === null)
      {
          $handle = fopen($filename, 'w+');
      }

      if ($handle)
      {
          // seek to the end
          fseek($handle, 0, SEEK_END);

          // are we at the end of is the file empty
          if (ftell($handle) > 0)
          {
              // move back a byte
              fseek($handle, -2, SEEK_END);

              // add the trailing comma
              fwrite($handle, ",\n", 2);

              // add the new json string
              fwrite($handle, json_encode($currentPost) . "]\n");
          }
          else
          {
              // write the first event inside an array
              fwrite($handle, json_encode(array($currentPost)));
          }

              // close the handle on the file
              fclose($handle);
      }
    }
  }
  header("Location: ./?return=$returnCode");
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Guestbook</title>
    <link href="../style.css" rel="stylesheet" type="text/css" media="all">
    <style type="text/css">
      body {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }
    </style>
    <?php include($_SERVER['DOCUMENT_ROOT'] . '/seasonal.php'); ?>
  </head>
  <body>
    <p>Your post has been submitted! It's weird that you're seeing this page though. Maybe your browser can't process location headers? At any rate, click <?php echo "<a href='./?return=$returnCode'>here</a>"; ?> to get where you're supposed to be right now.</p>
  </body>
</html>