Skip to content

Commit

Permalink
chore: Neaten up timestamp logic
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynotsolucky committed Nov 27, 2024
1 parent f3d1a62 commit 017f998
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use fostercommerce\honeypot\models\Settings;
use fostercommerce\honeypot\web\twig\Honeypot;
use yii\base\Event;
use yii\base\Exception;
use yii\base\InvalidConfigException;

/**
* @method static Plugin getInstance()
Expand Down Expand Up @@ -52,6 +54,21 @@ protected function settingsHtml(): ?string
]);
}

/**
* @throws Exception
* @throws InvalidConfigException
*/
private function decodeTimestamp(string $value): false|int
{
$value = base64_decode($value, true);
if ($value === false) {
return false;
}

$value = Craft::$app->getSecurity()->decryptByKey($value);
return (int) $value;
}

private function attachEventHandlers(): void
{
Event::on(
Expand Down Expand Up @@ -84,16 +101,13 @@ function (Event $event): void {
$spamReasons = [];

if ($timetrapValue !== null) {
$timetrapValue = base64_decode($timetrapValue, true);
$timetrapValue = Craft::$app->getSecurity()->decryptByKey($timetrapValue);
$timetrapValue = $this->decodeTimestamp($timetrapValue);
if ($timetrapValue === false) {
// Timetrap value was tampered with. Mark as spam.
$isSpamSubmission = true;
$spamReasons[] = 'Tampered timetrap value';
}

$timetrapValue = (int) $timetrapValue;

if (! $isSpamSubmission) {
$currentTimestamp = (new \DateTimeImmutable())->format('Uv');

Expand Down

0 comments on commit 017f998

Please sign in to comment.