Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Ivan Lackovic 2015-05-13 16:24:05 +02:00
commit 6a147a2ea3
8 changed files with 48 additions and 22 deletions

View File

@ -2,8 +2,8 @@
namespace Gregwar\CaptchaBundle\Controller; namespace Gregwar\CaptchaBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
@ -37,7 +37,7 @@ class CaptchaController extends Controller
} }
if (!$isOk) { if (!$isOk) {
throw $this->createNotFoundException('Unable to generate a captcha via an URL with this session key.'); return $this->error($options);
} }
/* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */ /* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
@ -58,4 +58,26 @@ class CaptchaController extends Controller
return $response; return $response;
} }
/**
* Returns an empty image with status code 428 Precondition Required
*
* @param array $options
*
* @return Response
*/
protected function error($options)
{
/* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
$generator = $this->container->get('gregwar_captcha.generator');
$generator->setPhrase('');
$response = new Response($generator->generate($options));
$response->setStatusCode(428);
$response->headers->set('Content-type', 'image/jpeg');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Cache-Control', 'no-cache');
return $response;
}
} }

View File

@ -16,7 +16,7 @@ class GregwarCaptchaExtension extends Extension
{ {
/** /**
* @param array $configs * @param array $configs
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container * @param ContainerBuilder $container
*/ */
public function load(array $configs, ContainerBuilder $container) public function load(array $configs, ContainerBuilder $container)
{ {

View File

@ -2,7 +2,8 @@
namespace Gregwar\CaptchaBundle\Generator; namespace Gregwar\CaptchaBundle\Generator;
use Symfony\Component\Finder\Finder; use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
@ -18,7 +19,7 @@ use Gregwar\Captcha\PhraseBuilderInterface;
class CaptchaGenerator class CaptchaGenerator
{ {
/** /**
* @var \Symfony\Component\Routing\RouterInterface * @var RouterInterface
*/ */
protected $router; protected $router;
@ -38,12 +39,17 @@ class CaptchaGenerator
protected $imageFileHandler; protected $imageFileHandler;
/** /**
* @param \Symfony\Component\Routing\RouterInterface $router * @param RouterInterface $router
* @param CaptchaBuilderInterface $builder * @param CaptchaBuilderInterface $builder
* @param ImageFileHandlerInterface $imageFileHandler * @param PhraseBuilderInterface $phraseBuilder
* @param ImageFileHandler $imageFileHandler
*/ */
public function __construct(RouterInterface $router, CaptchaBuilderInterface $builder, PhraseBuilderInterface $phraseBuilder, ImageFileHandler $imageFileHandler) public function __construct(
{ RouterInterface $router,
CaptchaBuilderInterface $builder,
PhraseBuilderInterface $phraseBuilder,
ImageFileHandler $imageFileHandler
) {
$this->router = $router; $this->router = $router;
$this->builder = $builder; $this->builder = $builder;
$this->phraseBuilder = $phraseBuilder; $this->phraseBuilder = $phraseBuilder;
@ -53,7 +59,6 @@ class CaptchaGenerator
/** /**
* Get the captcha URL, stream, or filename that will go in the image's src attribute * Get the captcha URL, stream, or filename that will go in the image's src attribute
* *
* @param $key
* @param array $options * @param array $options
* *
* @return array * @return array
@ -86,7 +91,6 @@ class CaptchaGenerator
} }
/** /**
* @param string $key
* @param array $options * @param array $options
* *
* @return string * @return string
@ -148,7 +152,6 @@ class CaptchaGenerator
} }
/** /**
* @param string $key
* @param array $options * @param array $options
* *
* @return string * @return string

View File

@ -1,4 +1,4 @@
Copyright (c) <2011-2013> Grégoire Passault Copyright (c) <2011-2015> Grégoire Passault
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

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

View File

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

View File

@ -60,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;
} }
@ -82,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) {
@ -159,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,14 +18,13 @@
], ],
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.3.0",
"gregwar/captcha": "~1.0.11", "gregwar/captcha": "~1.1",
"symfony/framework-bundle": "~2.1", "symfony/framework-bundle": "~2.1",
"symfony/form": "~2.1" "symfony/form": "~2.1"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-4": {
"Gregwar\\CaptchaBundle": "" "Gregwar\\CaptchaBundle\\": "/"
}
} }
},
"target-dir": "Gregwar/CaptchaBundle"
} }