-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\Forms\Controls; | ||
|
||
use Nette\Forms\Controls\HiddenField; | ||
use Nette\Utils\Html; | ||
|
||
class ProtectionFastInput extends HiddenField | ||
{ | ||
|
||
/** @var string */ | ||
private $diff; | ||
|
||
public function __construct(string $diff = '+5 second', string $message = 'Form was submitted to fast. Are you robot?') | ||
{ | ||
parent::__construct(); | ||
|
||
$this->diff = $diff; | ||
|
||
$this->setOmitted() | ||
->setRequired(false) | ||
->addRule([$this, 'validateInput'], $message); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setValue($value): self | ||
{ | ||
return $this; | ||
} | ||
|
||
public function loadHttpData(): void | ||
{ | ||
$this->value = $this->getHttpData(Form::DATA_TEXT); | ||
} | ||
|
||
public function getControl(): Html | ||
{ | ||
return parent::getControl()->value(time()); | ||
} | ||
|
||
public function validateInput(ProtectionFastInput $control): bool | ||
{ | ||
$value = (string) $control->getValue(); | ||
|
||
$d1 = new DateTime('@' . $value); | ||
$d1->modify($this->diff); | ||
|
||
$d2 = new DateTime('@' . time()); | ||
|
||
return $d1 <= $d2; | ||
} | ||
|
||
} |