diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 002a71e..1878e26 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -31,6 +31,7 @@ public function getConfigTreeBuilder() ->scalarNode('gc_freq')->defaultValue(100)->end() ->scalarNode('expiration')->defaultValue(60)->end() ->scalarNode('quality')->defaultValue(15)->end() + ->scalarNode('invalid_message')->defaultValue('Bad code value')->end() ->end() ; return $treeBuilder; diff --git a/LICENSE b/LICENSE index ae9440b..bcf9213 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) <2011> Grégoire Passault +Copyright (c) <2012> 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 diff --git a/README.md b/README.md index 4f4965a..f0fd05e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Add the following lines to your `deps` file: [GregwarCaptchaBundle] git=git://github.com/Gregwar/CaptchaBundle.git target=/bundles/Gregwar/CaptchaBundle + version=origin/2.0 <- add this if you are using Symfony 2.0 ``` Now, run the vendors script to download the bundle: @@ -44,8 +45,20 @@ $ git submodule add git://github.com/Gregwar/CaptchaBundle.git vendor/bundles/Gr $ git submodule update --init ``` +***Using Composer*** + +Add the following to the "require" section of your `composer.json` file: + +``` + "gregwar/captcha-bundle": "1.0.0" +``` + +And update your dependencies + ### Step 2: Configure the Autoloader +If you use composer, you can skip this step. + Now you will need to add the `Gregwar` namespace to your autoloader: ``` php @@ -113,6 +126,7 @@ You can define the following type option : * **web_path**: absolute path to public web folder (default="%kernel.root_dir%/../web") * **gc_freq**: frequency of garbage collection in fractions of 1 (default=100) * **expiration**: maximum lifetime of captcha image files in minutes (default=60) +* **invalid_message**: error message displayed when an non-matching code is submitted (default="Bad code value") Example : diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index e4f0e69..9ce243e 100755 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -2,7 +2,7 @@ namespace Gregwar\CaptchaBundle\Type; -use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Form\FormView; @@ -45,7 +45,7 @@ public function buildForm(FormBuilder $builder, array $options) $this->key = $builder->getForm()->getName(); $builder->addValidator( - new CaptchaValidator($this->session, $this->key) + new CaptchaValidator($this->session, $this->key, $options['invalid_message']) ); } diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index f815c90..abd52f6 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -3,7 +3,7 @@ namespace Gregwar\CaptchaBundle\Validator; use Symfony\Component\Form\FormValidatorInterface; -use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormError; @@ -24,10 +24,16 @@ class CaptchaValidator implements FormValidatorInterface */ private $key; - public function __construct(Session $session, $key) + /** + * Error message text for non-matching submissions + */ + private $invalidMessage; + + public function __construct(Session $session, $key, $invalidMessage) { $this->session = $session; $this->key = $key; + $this->invalidMessage = $invalidMessage; } public function validate(FormInterface $form) @@ -37,7 +43,7 @@ public function validate(FormInterface $form) if (!($code && $excepted_code && is_string($code) && is_string($excepted_code) && $this->niceize($code) == $this->niceize($excepted_code))) { - $form->addError(new FormError('Bad code value')); + $form->addError(new FormError($this->invalidMessage)); } $this->session->remove($this->key); @@ -61,7 +67,7 @@ private function getExceptedCode() /** * Process the codes */ - private function niceize($code) + private function niceize($code) { return strtr(strtolower($code), 'oil', '01l'); } diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1b4df7e --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "gregwar/captcha-bundle", + "type": "captcha-bundle", + "description": "Captcha bundle", + "keywords": ["symfony2", "captcha", "bot", "visual", "code", "security", "spam"], + "homepage": "https://github.com/Gregwar/ImageBundle", + "license": "MIT", + "authors": [ + { + "name": "Grégoire Passault", + "email": "g.passault@gmail.com", + "homepage": "http://www.gregwar.com/" + } + ], + "require": { + "php": ">=5.3.0" + }, + "autoload": { + "psr-0": { + "Gregwar\\CaptchaBundle": "" + } + }, + "target-dir": "Gregwar/CaptchaBundle" +}