CaptchaBundle/Validator/CaptchaValidator.php

168 lines
3.9 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Gregwar\CaptchaBundle\Validator;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpFoundation\Request;
/**
2020-01-03 00:12:44 +01:00
* Captcha validator.
*
* @author Gregwar <g.passault@gmail.com>
*/
class CaptchaValidator
{
/** @var SessionInterface */
private $session;
/**
2020-01-03 00:12:44 +01:00
* Session key to store the code.
*
* @var string
*/
private $key;
/**
2020-01-03 00:12:44 +01:00
* Error message text for non-matching submissions.
*
* @var string
*/
private $invalidMessage;
/**
2020-01-03 00:12:44 +01:00
* Configuration parameter used to bypass a required code match.
*
* @var string
*/
private $bypassCode;
2012-12-04 12:20:23 +01:00
/**
2020-01-03 00:12:44 +01:00
* Number of form that the user can submit without captcha.
*
2012-12-04 12:20:23 +01:00
* @var int
*/
private $humanity;
/**
2020-01-03 00:12:44 +01:00
* Translator.
*
* @var TranslatorInterface
*/
private $translator;
/**
* Request
*
* @var Request
*/
private $req;
public function __construct(
TranslatorInterface $translator,
SessionInterface $session,
string $key,
string $invalidMessage,
?string $bypassCode,
int $humanity,
?Request $req
) {
2020-01-03 00:12:44 +01:00
$this->translator = $translator;
$this->session = $session;
$this->key = $key;
$this->invalidMessage = $invalidMessage;
$this->bypassCode = $bypassCode;
$this->humanity = $humanity;
2022-02-09 10:19:13 +01:00
$this->req = $req;
}
public function validate(FormEvent $event): void
{
$form = $event->getForm();
$code = $form->getData();
$expectedCode = $this->getExpectedCode();
2012-12-04 12:20:23 +01:00
if ($this->humanity > 0) {
$humanity = $this->getHumanity();
if ($humanity > 0) {
2020-01-03 00:12:44 +01:00
$this->updateHumanity($humanity - 1);
2012-12-04 12:20:23 +01:00
return;
}
}
2020-01-03 00:12:44 +01:00
if (!(null !== $code && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) {
$form->addError(new FormError($this->translator->trans($this->invalidMessage, array(), 'validators')));
2012-12-04 12:20:23 +01:00
} else {
if ($this->humanity > 0) {
$this->updateHumanity($this->humanity);
2012-12-04 12:20:23 +01:00
}
}
if (null == $this->req || 1 < $this->req->get('flow_registration_step')) {
$this->session->remove($this->key);
if ($this->session->has($this->key.'_fingerprint')) {
$this->session->remove($this->key.'_fingerprint');
}
}
}
/**
2020-01-03 00:12:44 +01:00
* Retrieve the expected CAPTCHA code.
*
* @return mixed|null
*/
protected function getExpectedCode()
{
$options = $this->session->get($this->key, array());
if (is_array($options) && isset($options['phrase'])) {
return $options['phrase'];
}
return null;
}
/**
2020-01-03 00:12:44 +01:00
* Retrieve the humanity.
*
* @return mixed|null
*/
protected function getHumanity()
{
2020-01-03 00:12:44 +01:00
return $this->session->get($this->key.'_humanity', 0);
}
protected function updateHumanity(int $newValue): void
{
if ($newValue > 0) {
2020-01-03 00:12:44 +01:00
$this->session->set($this->key.'_humanity', $newValue);
} else {
2020-01-03 00:12:44 +01:00
$this->session->remove($this->key.'_humanity');
}
}
protected function niceize(string $code): string
{
return strtr(strtolower($code), 'oil', '01l');
}
/**
2020-01-03 00:12:44 +01:00
* Run a match comparison on the provided code and the expected code.
*
2020-01-03 00:12:44 +01:00
* @param string $code
* @param string|null $expectedCode
2020-01-03 00:12:44 +01:00
*
* @return bool
*/
protected function compare($code, $expectedCode): bool
{
2020-01-03 00:12:44 +01:00
return null !== $expectedCode && is_string($expectedCode) && $this->niceize($code) == $this->niceize($expectedCode);
}
}