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