chore(*): remove duplicate cache rule + cleanup cache headers; cleanup + formatting
This commit is contained in:
parent
425fed6ff6
commit
0e565970a5
42 changed files with 223 additions and 217 deletions
|
@ -9,13 +9,13 @@ class ContactHandler extends BaseHandler
|
|||
{
|
||||
protected string $postgrestUrl;
|
||||
protected string $postgrestApiKey;
|
||||
|
||||
private string $forwardEmailApiKey;
|
||||
private Client $httpClient;
|
||||
|
||||
public function __construct(?Client $httpClient = null)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->httpClient = $httpClient ?? new Client();
|
||||
$this->forwardEmailApiKey = $_ENV["FORWARDEMAIL_API_KEY"] ?? getenv("FORWARDEMAIL_API_KEY");
|
||||
}
|
||||
|
@ -33,19 +33,16 @@ class ContactHandler extends BaseHandler
|
|||
if (strpos($contentType, "application/json") !== false) {
|
||||
$rawBody = file_get_contents("php://input");
|
||||
$formData = json_decode($rawBody, true);
|
||||
if (!$formData || !isset($formData["data"])) {
|
||||
throw new \Exception("Invalid JSON payload.");
|
||||
}
|
||||
|
||||
if (!$formData || !isset($formData["data"])) throw new \Exception("Invalid JSON payload.");
|
||||
|
||||
$formData = $formData["data"];
|
||||
} elseif (
|
||||
strpos($contentType, "application/x-www-form-urlencoded") !== false
|
||||
) {
|
||||
$formData = $_POST;
|
||||
} else {
|
||||
$this->sendErrorResponse(
|
||||
"Unsupported Content-Type. Use application/json or application/x-www-form-urlencoded.",
|
||||
400
|
||||
);
|
||||
$this->sendErrorResponse("Unsupported Content-Type. Use application/json or application/x-www-form-urlencoded.", 400);
|
||||
}
|
||||
|
||||
if (!empty($formData["hp_name"])) $this->sendErrorResponse("Invalid submission.", 400);
|
||||
|
@ -65,14 +62,8 @@ class ContactHandler extends BaseHandler
|
|||
if (empty($name)) $this->sendErrorResponse("Name is required.", 400);
|
||||
if (!$email) $this->sendErrorResponse("Valid email is required.", 400);
|
||||
if (empty($message)) $this->sendErrorResponse("Message is required.", 400);
|
||||
if (strlen($name) > 100) $this->sendErrorResponse(
|
||||
"Name is too long. Max 100 characters allowed.",
|
||||
400
|
||||
);
|
||||
if (strlen($message) > 1000) $this->sendErrorResponse(
|
||||
"Message is too long. Max 1000 characters allowed.",
|
||||
400
|
||||
);
|
||||
if (strlen($name) > 100) $this->sendErrorResponse("Name is too long. Max 100 characters allowed.", 400);
|
||||
if (strlen($message) > 1000) $this->sendErrorResponse("Message is too long. Max 1000 characters allowed.", 400);
|
||||
if ($this->isBlockedDomain($email)) $this->sendErrorResponse("Submission from blocked domain.", 400);
|
||||
|
||||
$contactData = [
|
||||
|
@ -87,6 +78,7 @@ class ContactHandler extends BaseHandler
|
|||
$this->sendRedirect("/contact/success");
|
||||
} catch (\Exception $e) {
|
||||
error_log("Error handling contact form submission: " . $e->getMessage());
|
||||
|
||||
$this->sendErrorResponse($e->getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
@ -95,6 +87,7 @@ class ContactHandler extends BaseHandler
|
|||
{
|
||||
$referer = $_SERVER["HTTP_REFERER"] ?? "";
|
||||
$allowedDomain = "coryd.dev";
|
||||
|
||||
if (!str_contains($referer, $allowedDomain)) throw new \Exception("Invalid submission origin.");
|
||||
}
|
||||
|
||||
|
@ -107,13 +100,12 @@ class ContactHandler extends BaseHandler
|
|||
|
||||
if (file_exists($cacheFile)) {
|
||||
$data = json_decode(file_get_contents($cacheFile), true);
|
||||
if (
|
||||
$data["timestamp"] + $rateLimitDuration > time() &&
|
||||
$data["count"] >= $maxRequests
|
||||
) {
|
||||
|
||||
if ($data["timestamp"] + $rateLimitDuration > time() && $data["count"] >= $maxRequests) {
|
||||
header("Location: /429", true, 302);
|
||||
exit();
|
||||
}
|
||||
|
||||
$data["count"]++;
|
||||
} else {
|
||||
$data = ["count" => 1, "timestamp" => time()];
|
||||
|
@ -130,6 +122,7 @@ class ContactHandler extends BaseHandler
|
|||
private function isBlockedDomain(string $email): bool
|
||||
{
|
||||
$domain = substr(strrchr($email, "@"), 1);
|
||||
|
||||
if (!$domain) return false;
|
||||
|
||||
$response = $this->httpClient->get(
|
||||
|
@ -145,7 +138,6 @@ class ContactHandler extends BaseHandler
|
|||
],
|
||||
]
|
||||
);
|
||||
|
||||
$blockedDomains = json_decode($response->getBody(), true);
|
||||
|
||||
return !empty($blockedDomains);
|
||||
|
@ -163,9 +155,8 @@ class ContactHandler extends BaseHandler
|
|||
|
||||
if ($response->getStatusCode() >= 400) {
|
||||
$errorResponse = json_decode($response->getBody(), true);
|
||||
throw new \Exception(
|
||||
"PostgREST error: " . ($errorResponse["message"] ?? "Unknown error")
|
||||
);
|
||||
|
||||
throw new \Exception("PostgREST error: " . ($errorResponse["message"] ?? "Unknown error"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -206,6 +197,7 @@ class ContactHandler extends BaseHandler
|
|||
$redirectUrl = "{$protocol}://{$host}{$path}";
|
||||
|
||||
header("Location: $redirectUrl", true, 302);
|
||||
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
@ -215,6 +207,8 @@ try {
|
|||
$handler->handleRequest();
|
||||
} catch (\Exception $e) {
|
||||
error_log("Contact form error: " . $e->getMessage());
|
||||
|
||||
echo json_encode(["error" => $e->getMessage()]);
|
||||
|
||||
http_response_code(500);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue