2011-08-25 22:50:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
{
|
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
2011-08-25 22:50:59 +02:00
|
|
|
*/
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Session key to store the code
|
|
|
|
*/
|
|
|
|
private $key;
|
|
|
|
|
2012-06-29 16:23:05 +02:00
|
|
|
/**
|
|
|
|
* Error message text for non-matching submissions
|
|
|
|
*/
|
|
|
|
private $invalidMessage;
|
|
|
|
|
2012-06-30 18:18:05 +02:00
|
|
|
/**
|
|
|
|
* Configuration parameter used to bypass a required code match
|
|
|
|
*/
|
|
|
|
private $bypassCode;
|
|
|
|
|
2012-12-04 12:20:23 +01:00
|
|
|
/**
|
|
|
|
* Number of form that the user can submit without captcha
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $humanity;
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
|
|
|
|
* @param string $key
|
|
|
|
* @param string $invalidMessage
|
|
|
|
* @param string|null $bypassCode
|
|
|
|
*/
|
2012-12-04 12:20:23 +01:00
|
|
|
public function __construct(SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
$this->session = $session;
|
|
|
|
$this->key = $key;
|
|
|
|
$this->invalidMessage = $invalidMessage;
|
|
|
|
$this->bypassCode = $bypassCode;
|
2012-12-04 12:20:23 +01:00
|
|
|
$this->humanity = $humanity;
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* @param FormEvent $event
|
|
|
|
*/
|
|
|
|
public function validate(FormEvent $event)
|
2011-08-25 22:50:59 +02:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
$form = $form = $event->getForm();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:55:11 +01:00
|
|
|
if (!($code && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) {
|
2012-06-29 16:23:05 +02:00
|
|
|
$form->addError(new FormError($this->invalidMessage));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive the humanity
|
|
|
|
*
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
protected function getHumanity()
|
|
|
|
{
|
|
|
|
return $this->session->get($this->key . '_humanity', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the humanity
|
|
|
|
*/
|
|
|
|
protected function updateHumanity($newValue)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2012-11-14 04:33:36 +01:00
|
|
|
|
2011-08-25 22:50:59 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the codes
|
2012-11-14 04:33:36 +01:00
|
|
|
*
|
|
|
|
* @param $code
|
|
|
|
*
|
|
|
|
* @return string
|
2011-08-25 22:50:59 +02:00
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected function niceize($code)
|
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
|
|
|
*
|
|
|
|
* @param $code
|
|
|
|
* @param $expectedCode
|
|
|
|
*
|
|
|
|
* @return bool
|
2012-06-30 18:18:05 +02:00
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected function compare($code, $expectedCode)
|
2012-06-30 18:18:05 +02:00
|
|
|
{
|
|
|
|
return ($expectedCode && is_string($expectedCode) && $this->niceize($code) == $this->niceize($expectedCode));
|
|
|
|
}
|
2011-08-25 22:50:59 +02:00
|
|
|
}
|