Merge branch 'master' of github.com:Gregwar/CaptchaBundle

Conflicts:
	composer.json
This commit is contained in:
Gregwar 2015-01-27 17:12:52 +01:00
commit d6c6de0f83
13 changed files with 57 additions and 53 deletions

View File

@ -2,9 +2,9 @@
namespace Gregwar\CaptchaBundle\Controller; namespace Gregwar\CaptchaBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* Generates a captcha via a URL * Generates a captcha via a URL
@ -16,12 +16,13 @@ class CaptchaController extends Controller
/** /**
* Action that is used to generate the captcha, save its code, and stream the image * Action that is used to generate the captcha, save its code, and stream the image
* *
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $key * @param string $key
* *
* @return \Symfony\Component\HttpFoundation\Response * @return Response
*
* @throws NotFoundHttpException
*/ */
public function generateCaptchaAction(Request $request, $key) public function generateCaptchaAction($key)
{ {
$options = $this->container->getParameter('gregwar_captcha.config'); $options = $this->container->getParameter('gregwar_captcha.config');
$session = $this->get('session'); $session = $this->get('session');
@ -52,8 +53,9 @@ class CaptchaController extends Controller
$response = new Response($generator->generate($options)); $response = new Response($generator->generate($options));
$response->headers->set('Content-type', 'image/jpeg'); $response->headers->set('Content-type', 'image/jpeg');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Cache-Control','no-cache');
return $response; return $response;
} }
} }

View File

@ -51,4 +51,3 @@ class Configuration implements ConfigurationInterface
return $treeBuilder; return $treeBuilder;
} }
} }

View File

@ -160,4 +160,3 @@ class CaptchaGenerator
return $phrase; return $phrase;
} }
} }

View File

@ -103,4 +103,3 @@ class ImageFileHandler
} }
} }
} }

View File

@ -126,7 +126,7 @@ You can define the following configuration options globally or on the CaptchaTyp
* **disabled**: disable globally the CAPTCHAs (can be useful in dev environment), it will * **disabled**: disable globally the CAPTCHAs (can be useful in dev environment), it will
still appear but won't be editable and won't be checked still appear but won't be editable and won't be checked
* **length**: the length of the captcha (number of chars, default 5) * **length**: the length of the captcha (number of chars, default 5)
* **quality**: jpeg quality of captchas (default=15) * **quality**: jpeg quality of captchas (default=30)
* **charset**: the charset used for code generation (default=abcdefhjkmnprstuvwxyz23456789) * **charset**: the charset used for code generation (default=abcdefhjkmnprstuvwxyz23456789)
* **font**: the font to use (default is random among some pre-provided fonts), this should be an absolute path * **font**: the font to use (default is random among some pre-provided fonts), this should be an absolute path
* **keep_value**: the value will be the same until the form is posted, even if the page is refreshed (default=true) * **keep_value**: the value will be the same until the form is posted, even if the page is refreshed (default=true)

View File

@ -1,6 +1,13 @@
parameters:
gregwar_captcha.captcha_type.class: Gregwar\CaptchaBundle\Type\CaptchaType
gregwar_captcha.captcha_generator.class: Gregwar\CaptchaBundle\Generator\CaptchaGenerator
gregwar_captcha.image_file_handler.class: Gregwar\CaptchaBundle\Generator\ImageFileHandler
gregwar_captcha.captcha_builder.class: Gregwar\Captcha\CaptchaBuilder
gregwar_captcha.phrase_builder.class: Gregwar\Captcha\PhraseBuilder
services: services:
captcha.type: captcha.type:
class: Gregwar\CaptchaBundle\Type\CaptchaType class: %gregwar_captcha.captcha_type.class%
arguments: arguments:
- @session - @session
- @gregwar_captcha.generator - @gregwar_captcha.generator
@ -10,7 +17,7 @@ services:
- { name: form.type, alias: captcha } - { name: form.type, alias: captcha }
gregwar_captcha.generator: gregwar_captcha.generator:
class: Gregwar\CaptchaBundle\Generator\CaptchaGenerator class: %gregwar_captcha.captcha_generator.class%
arguments: arguments:
- @router - @router
- @gregwar_captcha.captcha_builder - @gregwar_captcha.captcha_builder
@ -18,7 +25,7 @@ services:
- @gregwar_captcha.image_file_handler - @gregwar_captcha.image_file_handler
gregwar_captcha.image_file_handler: gregwar_captcha.image_file_handler:
class: Gregwar\CaptchaBundle\Generator\ImageFileHandler class: %gregwar_captcha.image_file_handler.class%
arguments: arguments:
- %gregwar_captcha.config.image_folder% - %gregwar_captcha.config.image_folder%
- %gregwar_captcha.config.web_path% - %gregwar_captcha.config.web_path%
@ -26,7 +33,7 @@ services:
- %gregwar_captcha.config.expiration% - %gregwar_captcha.config.expiration%
gregwar_captcha.captcha_builder: gregwar_captcha.captcha_builder:
class: Gregwar\Captcha\CaptchaBuilder class: %gregwar_captcha.captcha_builder.class%
gregwar_captcha.phrase_builder: gregwar_captcha.phrase_builder:
class: Gregwar\Captcha\PhraseBuilder class: %gregwar_captcha.phrase_builder.class%

View File

@ -0,0 +1 @@
Renew: Обнови

View File

@ -0,0 +1 @@
Renew: Оновити

View File

@ -0,0 +1 @@
Bad code value: Грешен код

View File

@ -0,0 +1 @@
Bad code value: Невірний код

View File

@ -22,18 +22,12 @@ use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
class CaptchaType extends AbstractType class CaptchaType extends AbstractType
{ {
/** /**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface * @var SessionInterface
*/ */
protected $session; protected $session;
/** /**
* The session key * @var CaptchaGenerator
* @var string
*/
protected $key = null;
/**
* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator
*/ */
protected $generator; protected $generator;
@ -49,8 +43,9 @@ class CaptchaType extends AbstractType
private $options = array(); private $options = array();
/** /**
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session * @param SessionInterface $session
* @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator * @param CaptchaGenerator $generator
* @param TranslatorInterface $translator
* @param array $options * @param array $options
*/ */
public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options) public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options)
@ -62,17 +57,14 @@ class CaptchaType extends AbstractType
} }
/** /**
* @param \Symfony\Component\Form\FormBuilderInterface $builder * {@inheritdoc}
* @param array $options
*/ */
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$this->key = 'gcb_'.$builder->getForm()->getName();
$validator = new CaptchaValidator( $validator = new CaptchaValidator(
$this->translator, $this->translator,
$this->session, $this->session,
$this->key, sprintf('gcb_%s', $builder->getForm()->getName()),
$options['invalid_message'], $options['invalid_message'],
$options['bypass_code'], $options['bypass_code'],
$options['humanity'] $options['humanity']
@ -82,33 +74,31 @@ class CaptchaType extends AbstractType
} }
/** /**
* @param \Symfony\Component\Form\FormView $view * {@inheritdoc}
* @param \Symfony\Component\Form\FormInterface $form
* @param array $options
*/ */
public function buildView(FormView $view, FormInterface $form, array $options) public function buildView(FormView $view, FormInterface $form, array $options)
{ {
$isHuman = false;
if ($options['reload'] && !$options['as_url']) { if ($options['reload'] && !$options['as_url']) {
throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information'); throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information');
} }
$sessionKey = sprintf('gcb_%s', $form->getName());
$isHuman = false;
if ($options['humanity'] > 0) { if ($options['humanity'] > 0) {
$humanityKey = $this->key.'_humanity'; $humanityKey = sprintf('%s_humanity', $sessionKey);
if ($this->session->get($humanityKey, 0) > 0) { if ($this->session->get($humanityKey, 0) > 0) {
$isHuman = true; $isHuman = true;
} }
} }
if ($options['as_url']) { if ($options['as_url']) {
$key = $this->key;
$keys = $this->session->get($options['whitelist_key'], array()); $keys = $this->session->get($options['whitelist_key'], array());
if (!in_array($key, $keys)) { if (!in_array($sessionKey, $keys)) {
$keys[] = $key; $keys[] = $sessionKey;
} }
$this->session->set($options['whitelist_key'], $keys); $this->session->set($options['whitelist_key'], $keys);
$options['session_key'] = $this->key; $options['session_key'] = $sessionKey;
} }
$view->vars = array_merge($view->vars, array( $view->vars = array_merge($view->vars, array(
@ -126,11 +116,11 @@ class CaptchaType extends AbstractType
$persistOptions[$key] = $options[$key]; $persistOptions[$key] = $options[$key];
} }
$this->session->set($this->key, $persistOptions); $this->session->set($sessionKey, $persistOptions);
} }
/** /**
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver * {@inheritdoc}
*/ */
public function setDefaultOptions(OptionsResolverInterface $resolver) public function setDefaultOptions(OptionsResolverInterface $resolver)
{ {

View File

@ -15,7 +15,7 @@ use Symfony\Component\Translation\TranslatorInterface;
class CaptchaValidator class CaptchaValidator
{ {
/** /**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface * @var SessionInterface
*/ */
private $session; private $session;
@ -47,10 +47,12 @@ class CaptchaValidator
private $translator; private $translator;
/** /**
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session * @param TranslatorInterface $translator
* @param SessionInterface $session
* @param string $key * @param string $key
* @param string $invalidMessage * @param string $invalidMessage
* @param string|null $bypassCode * @param string $bypassCode
* @param int $humanity
*/ */
public function __construct(TranslatorInterface $translator, SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity) public function __construct(TranslatorInterface $translator, SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity)
{ {
@ -58,7 +60,7 @@ class CaptchaValidator
$this->session = $session; $this->session = $session;
$this->key = $key; $this->key = $key;
$this->invalidMessage = $invalidMessage; $this->invalidMessage = $invalidMessage;
$this->bypassCode = $bypassCode; $this->bypassCode = (string)$bypassCode;
$this->humanity = $humanity; $this->humanity = $humanity;
} }
@ -67,7 +69,7 @@ class CaptchaValidator
*/ */
public function validate(FormEvent $event) public function validate(FormEvent $event)
{ {
$form = $form = $event->getForm(); $form = $event->getForm();
$code = $form->getData(); $code = $form->getData();
$expectedCode = $this->getExpectedCode(); $expectedCode = $this->getExpectedCode();
@ -80,7 +82,7 @@ class CaptchaValidator
} }
} }
if (!($code && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) { if (!($code !== null && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) {
$form->addError(new FormError($this->translator->trans($this->invalidMessage, array(), 'validators'))); $form->addError(new FormError($this->translator->trans($this->invalidMessage, array(), 'validators')));
} else { } else {
if ($this->humanity > 0) { if ($this->humanity > 0) {
@ -112,7 +114,7 @@ class CaptchaValidator
} }
/** /**
* Retreive the humanity * Retrieve the humanity
* *
* @return mixed|null * @return mixed|null
*/ */
@ -157,6 +159,6 @@ class CaptchaValidator
*/ */
protected function compare($code, $expectedCode) protected function compare($code, $expectedCode)
{ {
return ($expectedCode && is_string($expectedCode) && $this->niceize($code) == $this->niceize($expectedCode)); return ($expectedCode !== null && is_string($expectedCode) && $this->niceize($code) == $this->niceize($expectedCode));
} }
} }

View File

@ -18,7 +18,9 @@
], ],
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.3.0",
"gregwar/captcha": "v1.0.12" "gregwar/captcha": "~1.0.12",
"symfony/framework-bundle": "~2.1",
"symfony/form": "~2.1"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-0": {