Skip to content

Commit

Permalink
chore: Remove template and throw a bad request exception instead
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynotsolucky committed Dec 1, 2024
1 parent b237d10 commit f5b3f5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
21 changes: 10 additions & 11 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use craft\web\Application;
use craft\web\Request;
use craft\web\Response;
use craft\web\View;
use fostercommerce\honeypot\models\Settings;
use fostercommerce\honeypot\web\twig\Honeypot;
use yii\base\Event;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;

/**
* @method static Plugin getInstance()
Expand Down Expand Up @@ -149,25 +149,24 @@ function (Event $event): void {
}
}

if ($settings->spamDetectedRedirect === null && $settings->spamDetectedTemplate === null) {
$firstReason = reset($spamReasons);
throw new BadRequestHttpException(sprintf('Invalid form submission: %s', $firstReason));
}

/** @var Response $response */
$response = Craft::$app->getResponse();
if ($settings->spamDetectedRedirect !== null) {
$response->redirect($settings->spamDetectedRedirect);
} else {
$data = [
/** @var string $template */
$template = $settings->spamDetectedTemplate;
$response->content = Craft::$app->view->renderTemplate($template, [
'reasons' => $spamReasons,
'action' => $action,
'ip' => $userIp,
'userAgent' => $userAgent,
];
if ($settings->spamDetectedTemplate !== null) {
$rendered = Craft::$app->view->renderTemplate($settings->spamDetectedTemplate, $data);
} else {
Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
$rendered = Craft::$app->view->renderTemplate('honeypot/_spam.twig', $data);
}

$response->content = $rendered;
]);
}

Craft::$app->end();
Expand Down
7 changes: 0 additions & 7 deletions src/templates/_spam.twig

This file was deleted.

0 comments on commit f5b3f5b

Please sign in to comment.