From cb45fe2847258cb8bc73d0307bb0dc22082fde74 Mon Sep 17 00:00:00 2001 From: Rajini Kanth Date: Wed, 14 May 2014 10:32:02 +0530 Subject: [PATCH 01/14] separated class names to parameters Easier to override the services of this bundle without compiler pass. (http://symfony.com/doc/current/cookbook/bundles/override.html) --- Resources/config/services.yml | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 64827f0..786a6de 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -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: captcha.type: - class: Gregwar\CaptchaBundle\Type\CaptchaType + class: %gregwar_captcha.captch_type.class% arguments: - @session - @gregwar_captcha.generator @@ -10,7 +17,7 @@ services: - { name: form.type, alias: captcha } gregwar_captcha.generator: - class: Gregwar\CaptchaBundle\Generator\CaptchaGenerator + class: %gregwar_captcha.captch_generator.class% arguments: - @router - @gregwar_captcha.captcha_builder @@ -18,7 +25,7 @@ services: - @gregwar_captcha.image_file_handler gregwar_captcha.image_file_handler: - class: Gregwar\CaptchaBundle\Generator\ImageFileHandler + class: %gregwar_captch.image_file_handler.class% arguments: - %gregwar_captcha.config.image_folder% - %gregwar_captcha.config.web_path% @@ -26,7 +33,7 @@ services: - %gregwar_captcha.config.expiration% gregwar_captcha.captcha_builder: - class: Gregwar\Captcha\CaptchaBuilder + class: %gregwar_captcha.captcha_builder.class% gregwar_captcha.phrase_builder: - class: Gregwar\Captcha\PhraseBuilder + class: %gregwar_captcha.phrase_builder.class% From de0d9408d6df925ab72f76e704cd4b34c527097f Mon Sep 17 00:00:00 2001 From: Rajini Kanth Date: Wed, 14 May 2014 10:35:18 +0530 Subject: [PATCH 02/14] corrected typos --- Resources/config/services.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 786a6de..7b552ca 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -7,7 +7,7 @@ parameters: services: captcha.type: - class: %gregwar_captcha.captch_type.class% + class: %gregwar_captcha.captcha_type.class% arguments: - @session - @gregwar_captcha.generator @@ -17,7 +17,7 @@ services: - { name: form.type, alias: captcha } gregwar_captcha.generator: - class: %gregwar_captcha.captch_generator.class% + class: %gregwar_captcha.captcha_generator.class% arguments: - @router - @gregwar_captcha.captcha_builder @@ -25,7 +25,7 @@ services: - @gregwar_captcha.image_file_handler gregwar_captcha.image_file_handler: - class: %gregwar_captch.image_file_handler.class% + class: %gregwar_captcha.image_file_handler.class% arguments: - %gregwar_captcha.config.image_folder% - %gregwar_captcha.config.web_path% From e34e191d3280b308c55e764c1226e3b77f65f6b1 Mon Sep 17 00:00:00 2001 From: dypa Date: Thu, 3 Jul 2014 12:50:49 +0400 Subject: [PATCH 03/14] Update CaptchaValidator.php fix typo (lol) --- Validator/CaptchaValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 6e498d9..11d2402 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -67,7 +67,7 @@ class CaptchaValidator */ public function validate(FormEvent $event) { - $form = $form = $event->getForm(); + $form = $event->getForm(); $code = $form->getData(); $expectedCode = $this->getExpectedCode(); From c4c69b0a47e6d4741d3aed09788a01ee1a9ed4bf Mon Sep 17 00:00:00 2001 From: Abdellatif AitBoudad Date: Thu, 10 Jul 2014 10:30:57 +0100 Subject: [PATCH 04/14] fixed typo. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1febba6..8c4c95c 100644 --- a/README.md +++ b/README.md @@ -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 still appear but won't be editable and won't be checked * **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) * **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) From 458e7cafef715da650ac4da7ce190a6299c26d78 Mon Sep 17 00:00:00 2001 From: Piotr Antosik Date: Mon, 21 Jul 2014 14:46:14 +0200 Subject: [PATCH 05/14] fix multiple captcha on page --- Type/CaptchaType.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index 3a18eb3..7991ea2 100644 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -26,12 +26,6 @@ class CaptchaType extends AbstractType */ protected $session; - /** - * The session key - * @var string - */ - protected $key = null; - /** * @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator */ @@ -67,12 +61,10 @@ class CaptchaType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { - $this->key = 'gcb_'.$builder->getForm()->getName(); - $validator = new CaptchaValidator( $this->translator, $this->session, - $this->key, + sprintf('gcp_%s', $builder->getForm()->getName()), $options['invalid_message'], $options['bypass_code'], $options['humanity'] @@ -94,21 +86,22 @@ class CaptchaType extends AbstractType throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information'); } + $key = sprintf('gcp_%s', $form->getName()); + if ($options['humanity'] > 0) { - $humanityKey = $this->key.'_humanity'; + $humanityKey = sprintf('%s_humanity', $key); if ($this->session->get($humanityKey, 0) > 0) { $isHuman = true; } } if ($options['as_url']) { - $key = $this->key; $keys = $this->session->get($options['whitelist_key'], array()); if (!in_array($key, $keys)) { $keys[] = $key; } $this->session->set($options['whitelist_key'], $keys); - $options['session_key'] = $this->key; + $options['session_key'] = $key; } $view->vars = array_merge($view->vars, array( @@ -126,7 +119,7 @@ class CaptchaType extends AbstractType $persistOptions[$key] = $options[$key]; } - $this->session->set($this->key, $persistOptions); + $this->session->set($key, $persistOptions); } /** From b87b05e29d92f593fd7bc84e8c16803463547061 Mon Sep 17 00:00:00 2001 From: Arnaud Dezandee Date: Tue, 29 Jul 2014 19:03:41 +0200 Subject: [PATCH 06/14] Fix session key variable naming --- Type/CaptchaType.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index 7991ea2..e533a42 100644 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -80,16 +80,15 @@ class CaptchaType extends AbstractType */ public function buildView(FormView $view, FormInterface $form, array $options) { - $isHuman = false; - 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'); } - $key = sprintf('gcp_%s', $form->getName()); + $sessionKey = sprintf('gcp_%s', $form->getName()); + $isHuman = false; if ($options['humanity'] > 0) { - $humanityKey = sprintf('%s_humanity', $key); + $humanityKey = sprintf('%s_humanity', $sessionKey); if ($this->session->get($humanityKey, 0) > 0) { $isHuman = true; } @@ -97,11 +96,11 @@ class CaptchaType extends AbstractType if ($options['as_url']) { $keys = $this->session->get($options['whitelist_key'], array()); - if (!in_array($key, $keys)) { - $keys[] = $key; + if (!in_array($sessionKey, $keys)) { + $keys[] = $sessionKey; } $this->session->set($options['whitelist_key'], $keys); - $options['session_key'] = $key; + $options['session_key'] = $sessionKey; } $view->vars = array_merge($view->vars, array( @@ -119,7 +118,7 @@ class CaptchaType extends AbstractType $persistOptions[$key] = $options[$key]; } - $this->session->set($key, $persistOptions); + $this->session->set($sessionKey, $persistOptions); } /** From 162a2a87cd658753585ce73c8519f3275ce17857 Mon Sep 17 00:00:00 2001 From: Gregwar Date: Thu, 31 Jul 2014 16:41:27 +0200 Subject: [PATCH 07/14] Changing gcp to gcb --- Type/CaptchaType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index e533a42..967df7d 100644 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -64,7 +64,7 @@ class CaptchaType extends AbstractType $validator = new CaptchaValidator( $this->translator, $this->session, - sprintf('gcp_%s', $builder->getForm()->getName()), + sprintf('gcb_%s', $builder->getForm()->getName()), $options['invalid_message'], $options['bypass_code'], $options['humanity'] @@ -84,7 +84,7 @@ class CaptchaType extends AbstractType throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information'); } - $sessionKey = sprintf('gcp_%s', $form->getName()); + $sessionKey = sprintf('gcb_%s', $form->getName()); $isHuman = false; if ($options['humanity'] > 0) { From 06c713fbb489c3f7e5962d6fa19c67e7ce88b8eb Mon Sep 17 00:00:00 2001 From: Stefan Bofirov Date: Sun, 17 Aug 2014 16:17:41 +0300 Subject: [PATCH 08/14] Added bulgarian translation --- Resources/translations/gregwar_captcha.bg.yml | 1 + Resources/translations/validators.bg.yml | 1 + 2 files changed, 2 insertions(+) create mode 100644 Resources/translations/gregwar_captcha.bg.yml create mode 100644 Resources/translations/validators.bg.yml diff --git a/Resources/translations/gregwar_captcha.bg.yml b/Resources/translations/gregwar_captcha.bg.yml new file mode 100644 index 0000000..c721782 --- /dev/null +++ b/Resources/translations/gregwar_captcha.bg.yml @@ -0,0 +1 @@ +Renew: Обнови diff --git a/Resources/translations/validators.bg.yml b/Resources/translations/validators.bg.yml new file mode 100644 index 0000000..5eb0d66 --- /dev/null +++ b/Resources/translations/validators.bg.yml @@ -0,0 +1 @@ +Bad code value: Грешен код From 9614209d8380c98a078d0dde6e14cc69d3b65811 Mon Sep 17 00:00:00 2001 From: Piotr Antosik Date: Mon, 21 Jul 2014 14:33:31 +0200 Subject: [PATCH 09/14] update docblock, fix typos, improve composer.json --- Controller/CaptchaController.php | 10 +++++----- DependencyInjection/Configuration.php | 1 - Generator/CaptchaGenerator.php | 1 - Generator/ImageFileHandler.php | 1 - Type/CaptchaType.php | 20 +++++++++----------- Validator/CaptchaValidator.php | 14 ++++++++------ composer.json | 4 +++- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/Controller/CaptchaController.php b/Controller/CaptchaController.php index acdfa48..5cc45db 100644 --- a/Controller/CaptchaController.php +++ b/Controller/CaptchaController.php @@ -2,9 +2,9 @@ namespace Gregwar\CaptchaBundle\Controller; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * 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 * - * @param \Symfony\Component\HttpFoundation\Request $request * @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'); $session = $this->get('session'); @@ -56,4 +57,3 @@ class CaptchaController extends Controller return $response; } } - diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 0ef9877..06c869d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -51,4 +51,3 @@ class Configuration implements ConfigurationInterface return $treeBuilder; } } - diff --git a/Generator/CaptchaGenerator.php b/Generator/CaptchaGenerator.php index 60db59b..d58c00b 100644 --- a/Generator/CaptchaGenerator.php +++ b/Generator/CaptchaGenerator.php @@ -160,4 +160,3 @@ class CaptchaGenerator return $phrase; } } - diff --git a/Generator/ImageFileHandler.php b/Generator/ImageFileHandler.php index 1a47420..71dda0f 100644 --- a/Generator/ImageFileHandler.php +++ b/Generator/ImageFileHandler.php @@ -103,4 +103,3 @@ class ImageFileHandler } } } - diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index 967df7d..17b4e2f 100644 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -22,12 +22,12 @@ use Gregwar\CaptchaBundle\Generator\CaptchaGenerator; class CaptchaType extends AbstractType { /** - * @var \Symfony\Component\HttpFoundation\Session\SessionInterface + * @var SessionInterface */ protected $session; /** - * @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator + * @var CaptchaGenerator */ protected $generator; @@ -43,9 +43,10 @@ class CaptchaType extends AbstractType private $options = array(); /** - * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session - * @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator - * @param array $options + * @param SessionInterface $session + * @param CaptchaGenerator $generator + * @param TranslatorInterface $translator + * @param array $options */ public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options) { @@ -56,8 +57,7 @@ class CaptchaType extends AbstractType } /** - * @param \Symfony\Component\Form\FormBuilderInterface $builder - * @param array $options + * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { @@ -74,9 +74,7 @@ class CaptchaType extends AbstractType } /** - * @param \Symfony\Component\Form\FormView $view - * @param \Symfony\Component\Form\FormInterface $form - * @param array $options + * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { @@ -122,7 +120,7 @@ class CaptchaType extends AbstractType } /** - * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver + * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 11d2402..4469cec 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -15,7 +15,7 @@ use Symfony\Component\Translation\TranslatorInterface; class CaptchaValidator { /** - * @var \Symfony\Component\HttpFoundation\Session\SessionInterface + * @var SessionInterface */ private $session; @@ -47,10 +47,12 @@ class CaptchaValidator private $translator; /** - * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session - * @param string $key - * @param string $invalidMessage - * @param string|null $bypassCode + * @param TranslatorInterface $translator + * @param SessionInterface $session + * @param string $key + * @param string $invalidMessage + * @param string $bypassCode + * @param int $humanity */ public function __construct(TranslatorInterface $translator, SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity) { @@ -112,7 +114,7 @@ class CaptchaValidator } /** - * Retreive the humanity + * Retrieve the humanity * * @return mixed|null */ diff --git a/composer.json b/composer.json index 81abbee..f466389 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,9 @@ ], "require": { "php": ">=5.3.0", - "gregwar/captcha": "v1.0.11" + "gregwar/captcha": "~1.0.11", + "symfony/framework-bundle": "~2.1", + "symfony/form": "~2.1" }, "autoload": { "psr-0": { From dc3e0c64954b1c0df4e770e2513199f3ed97de74 Mon Sep 17 00:00:00 2001 From: Hannes Giesenow Date: Wed, 27 Aug 2014 12:29:12 +0200 Subject: [PATCH 10/14] Added no-cache header This prevents Varnish from caching the captcha image --- Controller/CaptchaController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/CaptchaController.php b/Controller/CaptchaController.php index 5cc45db..8d2fb1c 100644 --- a/Controller/CaptchaController.php +++ b/Controller/CaptchaController.php @@ -53,6 +53,7 @@ class CaptchaController extends Controller $response = new Response($generator->generate($options)); $response->headers->set('Content-type', 'image/jpeg'); + $response->headers->set('Pragma', 'no-cache'); return $response; } From 459901319c40fecfaa8017c21ab9be9ace7a6602 Mon Sep 17 00:00:00 2001 From: Hannes Giesenow Date: Wed, 27 Aug 2014 14:34:57 +0200 Subject: [PATCH 11/14] HTTP 1.1 equivalent to Pragma: no-cache See http://stackoverflow.com/questions/10314174/difference-between-pragma-and-cache-control-headers for detailed explanation --- Controller/CaptchaController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/CaptchaController.php b/Controller/CaptchaController.php index 8d2fb1c..345e447 100644 --- a/Controller/CaptchaController.php +++ b/Controller/CaptchaController.php @@ -54,6 +54,7 @@ class CaptchaController extends Controller $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'); return $response; } From 6a522b7043d434bd713194d6ed9f2cf9a41263ce Mon Sep 17 00:00:00 2001 From: Alexandr Zagorulya Date: Fri, 26 Sep 2014 22:03:38 +0300 Subject: [PATCH 12/14] Added ukrainian translation --- Resources/translations/gregwar_captcha.uk.yml | 1 + Resources/translations/validators.uk.yml | 1 + 2 files changed, 2 insertions(+) create mode 100644 Resources/translations/gregwar_captcha.uk.yml create mode 100644 Resources/translations/validators.uk.yml diff --git a/Resources/translations/gregwar_captcha.uk.yml b/Resources/translations/gregwar_captcha.uk.yml new file mode 100644 index 0000000..513b80c --- /dev/null +++ b/Resources/translations/gregwar_captcha.uk.yml @@ -0,0 +1 @@ +Renew: Оновити \ No newline at end of file diff --git a/Resources/translations/validators.uk.yml b/Resources/translations/validators.uk.yml new file mode 100644 index 0000000..9492eb3 --- /dev/null +++ b/Resources/translations/validators.uk.yml @@ -0,0 +1 @@ +Bad code value: Невірний код \ No newline at end of file From 843f367540e2c233cfcb59870c7ae7ef396a6e0a Mon Sep 17 00:00:00 2001 From: waldermort Date: Tue, 13 Jan 2015 20:10:53 +0800 Subject: [PATCH 13/14] Fix: Bypass code incorrectly compared When the bypass code is given the value of '0', PHP equates it to boolean false causing the validation to fail. --- Validator/CaptchaValidator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 4469cec..b3466bb 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -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'))); } else { if ($this->humanity > 0) { @@ -159,6 +159,6 @@ class CaptchaValidator */ 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)); } } From 0cb34f33b05266956bae15c11059c0566c06e17d Mon Sep 17 00:00:00 2001 From: waldermort Date: Tue, 13 Jan 2015 20:16:15 +0800 Subject: [PATCH 14/14] Fix: Numeric bypass code defaults to integer type When adding a numeric only bypass code into config.yml, it is treated as an integer which causes validation to fail. A workaround is to surround the parameter with double quotes but many users may not be aware of this. This fix will explicitly convert it to a string. --- Validator/CaptchaValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 4469cec..2c4485f 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -60,7 +60,7 @@ class CaptchaValidator $this->session = $session; $this->key = $key; $this->invalidMessage = $invalidMessage; - $this->bypassCode = $bypassCode; + $this->bypassCode = (string)$bypassCode; $this->humanity = $humanity; }