-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.php
More file actions
151 lines (151 loc) · 3.29 KB
/
contact.php
File metadata and controls
151 lines (151 loc) · 3.29 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
require_once "db_connect.php";
session_start();
function yid(int $size = 11): string
{
return implode(
"",
array_map(
fn() => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"[
random_int(0, 63)
],
range(1, $size)
)
);
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$errors = [
"name" => empty(trim($_POST["name"]))
? "Your Internet Alias is required."
: null,
"email" => !filter_var(trim($_POST["email"]), FILTER_VALIDATE_EMAIL)
? "A valid email is required."
: null,
"category" => empty(trim($_POST["category"]))
? "Please select a category (What's your damage?)."
: null,
"message" => empty(trim($_POST["message"]))
? "The message (Spill the Tea) cannot be empty."
: null,
"captcha" => !preg_match(
'/^(?:piece of shit|[A-Za-z]{5}\s[A-Za-z]{2}\s[A-Za-z]{4})$/',
trim(strtolower($_POST["captcha"]))
)
? "Incorrect captcha. Are you a normie?"
: null,
];
$message = trim($_POST["message"]);
if (stripos($message, "my problem") !== false) {
$errors["my_problem"] =
"Make sure that this is our problem; if it is your problem, fuck you.";
}
$reporter_id = $_SESSION["user_id"] ?? 0;
if ($reporter_id && !array_filter($errors)) {
try {
$pdo = getDbConnection();
$stmt = $pdo->prepare("
SELECT
is_admin
FROM
users
WHERE
user_id = ?
LIMIT
1
");
$stmt->execute([$reporter_id]);
if ($stmt->fetchColumn()) {
$errors["admin"] = "Why are you contacting yourself, admin?";
}
} catch (\PDOException $e) {
throw $e;
header("Location: status.php");
exit();
} catch (\Exception $e) {
throw $e;
}
}
if ($error = array_filter($errors)) {
$error_key = array_key_first($error);
$_SESSION["404_code"] =
$error_key === "admin"
? "Bro WTF?"
: ($error_key === "my_problem"
? "We don't care"
: "You failed");
$_SESSION["404_message"] = reset($error);
header("Location: 404.php");
exit();
}
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$reason = trim($_POST["category"]);
$report_id = yid();
$content_type = $_POST["content_type"] ?? "user";
$content_id = $_POST["content_id"] ?? yid();
$details = "Submitted via contact form by: $name ($email)\n\n$message";
try {
$pdo = getDbConnection();
$stmt = $pdo->prepare("
INSERT INTO
reports (
report_id,
reporter_id,
content_type,
content_id,
reason,
details
)
VALUES
(?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$report_id,
$reporter_id,
$content_type,
$content_id,
$reason,
$details,
]);
$_SESSION["contact_success"] =
"Report submitted successfully. We'll look into it (maybe).";
} catch (\PDOException $e) {
throw $e;
header("Location: status.php");
exit();
} catch (\Exception $e) {
throw $e;
}
header("Location: contact.php");
exit();
}
$name = "";
$email = "";
$category = $_GET["category"] ?? "bug";
if (isset($_SESSION["user_id"])) {
try {
$pdo = getDbConnection();
$stmt = $pdo->prepare("
SELECT
handle,
email
FROM
users
WHERE
user_id = ?
");
$stmt->execute([$_SESSION["user_id"]]);
if ($user = $stmt->fetch()) {
$name = htmlspecialchars($user["handle"]);
$email = htmlspecialchars($user["email"]);
}
} catch (\PDOException $e) {
throw $e;
header("Location: status.php");
exit();
} catch (\Exception $e) {
throw $e;
}
}
include "contact.html";
?>