Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Croker committed Feb 17, 2021
1 parent e9ffb50 commit 06604fe
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/services/SnaptchaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ public function isIpDenied(): bool
return in_array(Craft::$app->getRequest()->getUserIP(), $denyList);
}

/**
* Returns whether a record is expired.
*
* @param SnaptchaRecord $record
* @return bool
*/
public function isExpired(SnaptchaRecord $record): bool
{
return $record->timestamp + ($record->expirationTime * 60) < time();
}

/**
* Returns whether a record is submitted too soon.
*
* @param SnaptchaRecord $record
* @return bool
*/
public function isTooSoon(SnaptchaRecord $record): bool
{
return $record->timestamp + $record->minimumSubmitTime > time();
}

/**
* Validates a submitted field.
*
Expand Down Expand Up @@ -174,17 +196,15 @@ public function validateField(string $value = null): bool
return false;
}

$now = time();

// Check if field has expired
if ($record->timestamp + ($record->expirationTime * 60) < $now) {
if ($this->isExpired($record)) {
$this->_reject('Expiration time of {minutes} minute(s) has passed.', ['minutes' => $record->expirationTime]);

return false;
}

// Check if minimum submit time has not passed
if ($record->timestamp + $record->minimumSubmitTime > $now) {
if ($this->isTooSoon($record)) {
$this->_reject('Minimum submit time of {second} second(s) has not yet passed.', ['second' => $record->minimumSubmitTime]);

return false;
Expand All @@ -197,7 +217,7 @@ public function validateField(string $value = null): bool

// Delete all expired records
SnaptchaRecord::deleteAll([
'<', 'timestamp', $now - ($record->expirationTime * 60)
'<', 'timestamp', time() - ($record->expirationTime * 60)
]);

// Fire an after event
Expand All @@ -216,7 +236,6 @@ public function validateField(string $value = null): bool
*/
private function _getSnaptchaRecord(SnaptchaModel $model)
{
$now = time();
$hashedIpAddress = $this->_getHashedIpAddress();

// Get most recent record with IP address from DB
Expand All @@ -227,7 +246,7 @@ private function _getSnaptchaRecord(SnaptchaModel $model)
->one();

// If record does not exist or one time key is enabled or the expiration time has passed
if ($record === null || Snaptcha::$plugin->settings->oneTimeKey || $record->timestamp + ($record->expirationTime * 60) < $now) {
if ($record === null || Snaptcha::$plugin->settings->oneTimeKey || $this->isExpired($record)) {
// Set key to random string
$model->key = StringHelper::randomString(16);
$model->value = $this->_getHashedValue($model->key, Snaptcha::$plugin->settings->salt);
Expand All @@ -236,7 +255,7 @@ private function _getSnaptchaRecord(SnaptchaModel $model)
$model->ipAddress = $hashedIpAddress;

// Set timestamp to current time
$model->timestamp = $now;
$model->timestamp = time();

// Set optional fields from settings if not defined
$model->expirationTime = $model->expirationTime ?? Snaptcha::$plugin->settings->expirationTime;
Expand All @@ -250,7 +269,7 @@ private function _getSnaptchaRecord(SnaptchaModel $model)
}

// Refresh timestamp
$record->timestamp = $now;
$record->timestamp = time();

if (!$record->save()) {
return null;
Expand All @@ -266,7 +285,7 @@ private function _getSnaptchaRecord(SnaptchaModel $model)
* @param string $salt
* @return string
*/
private function _getHashedValue(string $key, string $salt)
private function _getHashedValue(string $key, string $salt): string
{
return base64_encode($key.$salt);
}
Expand Down

0 comments on commit 06604fe

Please sign in to comment.