exec("
CREATE TABLE IF NOT EXISTS guest (
hash TEXT, PRIMARY KEY(hash)
);
");
$conn->exec("
CREATE TABLE IF NOT EXISTS entry (
guestHash TEXT NOT NULL,
id INTEGER,
name TEXT,
website TEXT,
message TEXT,
published integer default (cast(strftime('%s', 'now') as int)),
FOREIGN KEY(guestHash) REFERENCES guest(hash),
PRIMARY KEY(id AUTOINCREMENT)
);
");
function stringLengthIsBetween($str, $lower, $upper) {
return strlen($str) >= $lower && strlen($str) <= $upper;
}
function submissionIsValid($data) {
if (!stringLengthIsBetween($data['name'], 1, 100)) {
return false;
}
if (!($data['website'] == null || stringLengthIsBetween($data['website'], 3, 100))) {
return false;
}
if (!stringLengthIsBetween($data['website'], 0, 1000)) {
return false;
}
return true;
}
function handleEntrySubmission($db) {
if (!submissionIsValid($_POST)) {
return SubmissionOutcome::Invalid;
}
if (preg_match(CHALLENGE_ANSWER_REGEX, $_POST["challengeQuestion"]) === 0) {
return SubmissionOutcome::Shadowban;
}
if (
preg_match(SHADOWBAN_REGEX, $_POST["name"]) == 1 ||
preg_match(SHADOWBAN_REGEX, $_POST["website"]) == 1 ||
preg_match(SHADOWBAN_REGEX, $_POST["message"]) == 1
) {
return SubmissionOutcome::Shadowban;
}
$client_fingerprint = hash('sha256', $_SERVER['REMOTE_ADDR'] . "#" . $_SERVER['REMOTE_ADDR']);
$latest_entry_stmt = $db->prepare("
select published from entry
where guestHash = :guestHash
order by published desc
limit 1
");
$latest_entry_stmt->bindValue(':guestHash', $client_fingerprint, SQLITE3_TEXT);
$latest_entry_time = $latest_entry_stmt->execute()->fetchArray()['published'];
if ($_SERVER['REQUEST_TIME'] - $latest_entry_time <= GUESTBOOK_RATE_LIMIT_SECONDS) {
return SubmissionOutcome::RateLimit;
}
$entry_insert_stmt = $db->prepare("
insert into entry(guestHash, name, website, message)
values (:guestHash, :name, :website, :message)
");
$entry_insert_stmt->bindValue(':guestHash', $client_fingerprint, SQLITE3_TEXT);
$entry_insert_stmt->bindValue(':name', $_POST["name"], SQLITE3_TEXT);
$entry_insert_stmt->bindValue(':website', $_POST["website"], SQLITE3_TEXT);
$entry_insert_stmt->bindValue(':message', $_POST["message"], SQLITE3_TEXT);
if (!$entry_insert_stmt->execute()) {
return SubmissionOutcome::Failure;
}
return SubmissionOutcome::Success;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$submission_outcome = handleEntrySubmission($conn);
}
?>
Sign the guestbook
Your entry doesn't seem right
Double check that you inputted the right values,
and are meeting the form's requirements.
Something went wrong...
An error occured on the server. Reach out the the
administrator to see this fixed!
Too many submissions
To prevent spam, we only allow people to submit one entry every
minutes. Try again later.
Success!
Your entry has been saved to the guestbook.
query('select count(*) as entryCount from entry');
$entry_count = $entry_count_result->fetchArray()['entryCount'];
$entry_list_stmt = $conn->prepare("
select id, name, website, message from entry
order by id desc
limit :limit offset :offset
");
$entry_list_stmt->bindValue('limit', GUESTBOOK_PAGE_SIZE, SQLITE3_INTEGER);
$entry_list_stmt->bindValue('offset', GUESTBOOK_PAGE_SIZE * $current_page, SQLITE3_INTEGER);
$entry_list_results = $entry_list_stmt->execute();
$next_entry = $entry_list_results->fetchArray();
while ($next_entry) {
?>
-
Published
fetchArray();
}
?>