Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
lackovic10 committed May 13, 2015
2 parents 3d1383e + c221d9c commit 6a147a2
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
28 changes: 25 additions & 3 deletions Controller/CaptchaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Gregwar\CaptchaBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ public function generateCaptchaAction($key)
}

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 */
Expand All @@ -54,7 +54,29 @@ public function generateCaptchaAction($key)
$response = new Response($generator->generate($options));
$response->headers->set('Content-type', 'image/jpeg');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Cache-Control','no-cache');
$response->headers->set('Cache-Control', 'no-cache');

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;
}
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/GregwarCaptchaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GregwarCaptchaExtension extends Extension
{
/**
* @param array $configs
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* @param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand Down
21 changes: 12 additions & 9 deletions Generator/CaptchaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

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\Routing\RouterInterface;

Expand All @@ -18,7 +19,7 @@
class CaptchaGenerator
{
/**
* @var \Symfony\Component\Routing\RouterInterface
* @var RouterInterface
*/
protected $router;

Expand All @@ -38,12 +39,17 @@ class CaptchaGenerator
protected $imageFileHandler;

/**
* @param \Symfony\Component\Routing\RouterInterface $router
* @param RouterInterface $router
* @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->builder = $builder;
$this->phraseBuilder = $phraseBuilder;
Expand All @@ -53,7 +59,6 @@ public function __construct(RouterInterface $router, CaptchaBuilderInterface $bu
/**
* Get the captcha URL, stream, or filename that will go in the image's src attribute
*
* @param $key
* @param array $options
*
* @return array
Expand Down Expand Up @@ -86,7 +91,6 @@ public function setPhrase($phrase)
}

/**
* @param string $key
* @param array $options
*
* @return string
Expand Down Expand Up @@ -148,7 +152,6 @@ public function generate(array &$options)
}

/**
* @param string $key
* @param array $options
*
* @return string
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/gregwar_captcha.uk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Renew: Оновити
1 change: 1 addition & 0 deletions Resources/translations/validators.uk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bad code value: Невірний код
6 changes: 3 additions & 3 deletions Validator/CaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(TranslatorInterface $translator, SessionInterface $s
$this->session = $session;
$this->key = $key;
$this->invalidMessage = $invalidMessage;
$this->bypassCode = $bypassCode;
$this->bypassCode = (string)$bypassCode;
$this->humanity = $humanity;
}

Expand All @@ -82,7 +82,7 @@ public function validate(FormEvent $event)
}
}

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')));
} else {
if ($this->humanity > 0) {
Expand Down Expand Up @@ -159,6 +159,6 @@ protected function niceize($code)
*/
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));
}
}
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
],
"require": {
"php": ">=5.3.0",
"gregwar/captcha": "~1.0.11",
"gregwar/captcha": "~1.1",
"symfony/framework-bundle": "~2.1",
"symfony/form": "~2.1"
},
"autoload": {
"psr-0": {
"Gregwar\\CaptchaBundle": ""
"psr-4": {
"Gregwar\\CaptchaBundle\\": "/"
}
},
"target-dir": "Gregwar/CaptchaBundle"
}
}

0 comments on commit 6a147a2

Please sign in to comment.