Add honeypot field to poll creation form
An invisible "website" field is rendered off-screen in the create form. If a bot fills it in, the poll is not created and no mail is sent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
04cbc5bcd3
commit
ffe93a3256
5 files changed
+79
-1
No files matched your search
@@ -18,6 +18,12 @@ use Throwable;
|
||||
*/
|
||||
class HomeController extends AppController
|
||||
{
|
||||
/**
|
||||
* Name of the invisible honeypot field in the "create poll" form
|
||||
* (see templates/element/honeypot.php). Must stay empty.
|
||||
*/
|
||||
public const HONEYPOT_FIELD = 'website';
|
||||
|
||||
protected PollsTable $Polls;
|
||||
|
||||
/**
|
||||
@@ -56,7 +62,9 @@ class HomeController extends AppController
|
||||
$poll = $this->Polls->newEmptyEntity();
|
||||
$poll = $this->Polls->patchEntity($poll, (array)$this->request->getData(), ['fields' => ['title', 'email']]);
|
||||
|
||||
if (!$captcha->verify($this->request)) {
|
||||
if ($this->isHoneypotFilled()) {
|
||||
$this->Flash->error(__('Die Umfrage konnte nicht angelegt werden.'));
|
||||
} elseif (!$captcha->verify($this->request)) {
|
||||
$this->Flash->error(__('Die Sicherheitsabfrage war nicht korrekt. Bitte versuche es erneut.'));
|
||||
} elseif (!$poll->hasErrors() && !$this->throttle('create', 'Plenario.throttle.createPerHour')) {
|
||||
$this->Flash->error(__('Zu viele Umfragen in kurzer Zeit. Bitte versuche es später noch einmal.'));
|
||||
@@ -88,6 +96,19 @@ class HomeController extends AppController
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if the invisible honeypot field was filled in, i.e. the request
|
||||
* most likely comes from a bot.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isHoneypotFilled(): bool
|
||||
{
|
||||
$value = $this->request->getData(self::HONEYPOT_FIELD);
|
||||
|
||||
return $value !== null && $value !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirmation page after creating a poll.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user