Compare commits
70 Commits
Author | SHA1 | Date | |
---|---|---|---|
d43cc35eda | |||
3ccfdf1c93 | |||
b703ed1a0c | |||
25de43ac90 | |||
1b4835eb4d | |||
be1ce45060 | |||
0f6cd70920 | |||
754310f488 | |||
c2d5468556 | |||
980afdc10a | |||
791128c0fd | |||
639430383f | |||
8e98c5c0ab | |||
539884cd5d | |||
8ce4adb1b1 | |||
b787a8002e | |||
ba9c0e6166 | |||
25b8840f2a | |||
045ba7e67e | |||
18c85d3a4f | |||
f6c45045f0 | |||
f95a951b26 | |||
e1ed228b8b | |||
0b3495a081 | |||
53c25b2e9a | |||
fcf8c4fd01 | |||
e91cb1a3b7 | |||
1f6c80c326 | |||
f06ff4d2c2 | |||
42cb26794d | |||
e10494a767 | |||
8682eee873 | |||
c508d510ff | |||
d328f215b2 | |||
a26f03cc93 | |||
1862776c9a | |||
c989422a82 | |||
52107b0c32 | |||
e335e2a924 | |||
0971f224f4 | |||
29610bb574 | |||
018fdd3f35 | |||
476530a212 | |||
87d38d98a4 | |||
3cc4b072c3 | |||
0f5e9870f1 | |||
c844cbcdbc | |||
50405a74a1 | |||
84760b0a9f | |||
ef281889ab | |||
8468f93194 | |||
fe1102f5c6 | |||
f1fdfc142f | |||
2e49f50c1a | |||
8d3ee7334f | |||
805e77f24a | |||
adab98ad84 | |||
4040d06508 | |||
448b812f65 | |||
4aba359e71 | |||
8c39274fa4 | |||
232168d408 | |||
6a147a2ea3 | |||
3d1383e8ae | |||
798f29e635 | |||
34a84af209 | |||
3ad62e4d0d | |||
a16743a230 | |||
4f46a609ba | |||
b085af6e00 |
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Gregwar\CaptchaBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@ -11,7 +11,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
*
|
||||
* @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
|
||||
*/
|
||||
class CaptchaController extends Controller
|
||||
class CaptchaController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Action that is used to generate the captcha, save its code, and stream the image
|
||||
|
@ -14,8 +14,14 @@ class Configuration implements ConfigurationInterface
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('gregwar_captcha');
|
||||
$treeBuilder = new TreeBuilder('gregwar_captcha');
|
||||
|
||||
if (method_exists($treeBuilder, 'getRootNode')) {
|
||||
$rootNode = $treeBuilder->getRootNode();
|
||||
} else {
|
||||
// BC for symfony/config <= 4.1
|
||||
$rootNode = $treeBuilder->root('gregwar_captcha');
|
||||
}
|
||||
|
||||
$rootNode
|
||||
->addDefaultsIfNotSet()
|
||||
@ -33,7 +39,7 @@ class Configuration implements ConfigurationInterface
|
||||
->scalarNode('web_path')->defaultValue('%kernel.root_dir%/../web')->end()
|
||||
->scalarNode('gc_freq')->defaultValue(100)->end()
|
||||
->scalarNode('expiration')->defaultValue(60)->end()
|
||||
->scalarNode('quality')->defaultValue(30)->end()
|
||||
->scalarNode('quality')->defaultValue(50)->end()
|
||||
->scalarNode('invalid_message')->defaultValue('Bad code value')->end()
|
||||
->scalarNode('bypass_code')->defaultValue(null)->end()
|
||||
->scalarNode('whitelist_key')->defaultValue('captcha_whitelist_key')->end()
|
||||
@ -44,7 +50,10 @@ class Configuration implements ConfigurationInterface
|
||||
->scalarNode('interpolation')->defaultValue(true)->end()
|
||||
->arrayNode('text_color')->prototype('scalar')->end()->end()
|
||||
->arrayNode('background_color')->prototype('scalar')->end()->end()
|
||||
->arrayNode('background_images')->prototype('scalar')->end()->end()
|
||||
->scalarNode('disabled')->defaultValue(false)->end()
|
||||
->scalarNode('ignore_all_effects')->defaultValue(false)->end()
|
||||
->scalarNode('session_key')->defaultValue('captcha')->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
|
@ -34,6 +34,6 @@ class GregwarCaptchaExtension extends Extension
|
||||
$container->setParameter('gregwar_captcha.config.whitelist_key', $config['whitelist_key']);
|
||||
|
||||
$resources = $container->getParameter('twig.form.resources');
|
||||
$container->setParameter('twig.form.resources', array_merge(array('GregwarCaptchaBundle::captcha.html.twig'), $resources));
|
||||
$container->setParameter('twig.form.resources', array_merge(array('@GregwarCaptcha/captcha.html.twig'), $resources));
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,8 @@ class CaptchaGenerator
|
||||
|
||||
// Returns the image generation URL
|
||||
if ($options['as_url']) {
|
||||
return $this->router->generate('gregwar_captcha.generate_captcha', array('key' => $options['session_key']));
|
||||
return $this->router->generate('gregwar_captcha.generate_captcha',
|
||||
array('key' => $options['session_key'], 'n' => md5(microtime(true).mt_rand())));
|
||||
}
|
||||
|
||||
return 'data:image/jpeg;base64,' . base64_encode($this->generate($options));
|
||||
@ -124,6 +125,9 @@ class CaptchaGenerator
|
||||
|
||||
$fingerprint = isset($options['fingerprint']) ? $options['fingerprint'] : null;
|
||||
|
||||
$this->builder->setBackgroundImages($options['background_images']);
|
||||
$this->builder->setIgnoreAllEffects($options['ignore_all_effects']);
|
||||
|
||||
$content = $this->builder->build(
|
||||
$options['width'],
|
||||
$options['height'],
|
||||
|
47
README.md
47
README.md
@ -1,12 +1,19 @@
|
||||
Gregwar's CaptchaBundle
|
||||
=====================
|
||||
|
||||
The `GregwarCaptchaBundle` adds support for a "captcha" form type for the
|
||||
Symfony2 form component.
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YUXRLWHQSWS6L)
|
||||
|
||||
Important note: the master of this repository is containing current development
|
||||
in order to work with Symfony 2.1. If you are using Symfony 2.0 please checkout
|
||||
the 2.0 branch.
|
||||
The `GregwarCaptchaBundle` adds support for a captcha form type for the
|
||||
Symfony form component.
|
||||
|
||||
It uses [gregwar/captcha](https://github.com/Gregwar/Captcha) as captcha generator, which is a separate standalone library that can be used for none-symfony projects.
|
||||
|
||||
Compatibility with Symfony
|
||||
==========================
|
||||
|
||||
If you are using Symfony `< 2.8`, you should use version `1.*`
|
||||
|
||||
If you are using SYmfony `>= 2.8`, you should use version `2.*`
|
||||
|
||||
Installation
|
||||
============
|
||||
@ -17,7 +24,15 @@ Ultimately, the GregwarCaptchaBundle files should be downloaded to the
|
||||
'vendor/bundles/Gregwar/CaptchaBundle' directory.
|
||||
|
||||
You can accomplish this several ways, depending on your personal preference.
|
||||
The first method is the standard Symfony2 method.
|
||||
The first method is the standard Symfony method.
|
||||
|
||||
***Using Composer***
|
||||
|
||||
Use composer require to download and install the package.
|
||||
|
||||
``` bash
|
||||
composer require gregwar/captcha-bundle
|
||||
```
|
||||
|
||||
***Using the vendors script***
|
||||
|
||||
@ -45,16 +60,6 @@ $ 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": "dev-master"
|
||||
```
|
||||
|
||||
And update your dependencies
|
||||
|
||||
### Step 2: Configure the Autoloader
|
||||
|
||||
If you use composer, you can skip this step.
|
||||
@ -101,8 +106,11 @@ You can use the "captcha" type in your forms this way:
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Gregwar\CaptchaBundle\Type\CaptchaType;
|
||||
// ...
|
||||
$builder->add('captcha', 'captcha'); // That's all !
|
||||
$builder->add('captcha', CaptchaType::class); // That's all !
|
||||
// If you're using php<5.5, you can use instead:
|
||||
$builder->add('captcha', 'Gregwar\CaptchaBundle\Type\CaptchaType');
|
||||
// ...
|
||||
```
|
||||
|
||||
@ -141,14 +149,17 @@ You can define the following configuration options globally or on the CaptchaTyp
|
||||
* **max_front_lines**, **max_behind_lines**: the maximum number of lines to draw on top/behind the image. `0` will draw no lines; `null` will use the default algorithm (the
|
||||
number of lines depends on the size of the image). (default=null)
|
||||
* **background_color**: sets the background color, if you want to force it, this should be an array of r,g &b, for instance [255, 255, 255] will force the background to be white
|
||||
* **background_images**: Sets custom user defined images as the captcha background (1 image is selected randomly). It is recommended to turn off all the effects on the image (ignore_all_effects). The full paths to the images must be passed.
|
||||
* **interpolation**: enable or disable the interpolation on the captcha
|
||||
* **ignore_all_effects**: Recommended to use when setting background images, will disable all image effects.
|
||||
|
||||
Example :
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Gregwar\CaptchaBundle\Type\CaptchaType;
|
||||
// ...
|
||||
$builder->add('captcha', 'captcha', array(
|
||||
$builder->add('captcha', CaptchaType::class, array(
|
||||
'width' => 200,
|
||||
'height' => 50,
|
||||
'length' => 6,
|
||||
|
@ -1,3 +1,3 @@
|
||||
gregwar_captcha.generate_captcha:
|
||||
pattern: /generate-captcha/{key}
|
||||
defaults: { _controller: GregwarCaptchaBundle:Captcha:generateCaptcha }
|
||||
path: /generate-captcha/{key}
|
||||
defaults: { _controller: Gregwar\CaptchaBundle\Controller\CaptchaController::generateCaptchaAction }
|
||||
|
@ -7,33 +7,38 @@ parameters:
|
||||
|
||||
services:
|
||||
captcha.type:
|
||||
class: %gregwar_captcha.captcha_type.class%
|
||||
class: '%gregwar_captcha.captcha_type.class%'
|
||||
public: true
|
||||
arguments:
|
||||
- @session
|
||||
- @gregwar_captcha.generator
|
||||
- @translator
|
||||
- %gregwar_captcha.config%
|
||||
- '@session'
|
||||
- '@gregwar_captcha.generator'
|
||||
- '@translator'
|
||||
- '%gregwar_captcha.config%'
|
||||
tags:
|
||||
- { name: form.type, alias: captcha }
|
||||
|
||||
gregwar_captcha.generator:
|
||||
class: %gregwar_captcha.captcha_generator.class%
|
||||
class: '%gregwar_captcha.captcha_generator.class%'
|
||||
public: true
|
||||
arguments:
|
||||
- @router
|
||||
- @gregwar_captcha.captcha_builder
|
||||
- @gregwar_captcha.phrase_builder
|
||||
- @gregwar_captcha.image_file_handler
|
||||
- '@router'
|
||||
- '@gregwar_captcha.captcha_builder'
|
||||
- '@gregwar_captcha.phrase_builder'
|
||||
- '@gregwar_captcha.image_file_handler'
|
||||
|
||||
gregwar_captcha.image_file_handler:
|
||||
class: %gregwar_captcha.image_file_handler.class%
|
||||
class: '%gregwar_captcha.image_file_handler.class%'
|
||||
public: true
|
||||
arguments:
|
||||
- %gregwar_captcha.config.image_folder%
|
||||
- %gregwar_captcha.config.web_path%
|
||||
- %gregwar_captcha.config.gc_freq%
|
||||
- %gregwar_captcha.config.expiration%
|
||||
- '%gregwar_captcha.config.image_folder%'
|
||||
- '%gregwar_captcha.config.web_path%'
|
||||
- '%gregwar_captcha.config.gc_freq%'
|
||||
- '%gregwar_captcha.config.expiration%'
|
||||
|
||||
gregwar_captcha.captcha_builder:
|
||||
class: %gregwar_captcha.captcha_builder.class%
|
||||
class: '%gregwar_captcha.captcha_builder.class%'
|
||||
public: true
|
||||
|
||||
gregwar_captcha.phrase_builder:
|
||||
class: %gregwar_captcha.phrase_builder.class%
|
||||
class: '%gregwar_captcha.phrase_builder.class%'
|
||||
public: true
|
||||
|
1
Resources/translations/gregwar_captcha.ar.yml
Normal file
1
Resources/translations/gregwar_captcha.ar.yml
Normal file
@ -0,0 +1 @@
|
||||
Renew: تجديد
|
1
Resources/translations/gregwar_captcha.tr.yml
Normal file
1
Resources/translations/gregwar_captcha.tr.yml
Normal file
@ -0,0 +1 @@
|
||||
Renew: Yenile
|
1
Resources/translations/validators.ar.yml
Normal file
1
Resources/translations/validators.ar.yml
Normal file
@ -0,0 +1 @@
|
||||
Bad code value: الرمز غير متطابق
|
1
Resources/translations/validators.tr.yml
Normal file
1
Resources/translations/validators.tr.yml
Normal file
@ -0,0 +1 @@
|
||||
Bad code value: Kod eşleşmiyor
|
@ -2,8 +2,8 @@
|
||||
{% if is_human %}
|
||||
-
|
||||
{% else %}
|
||||
{% spaceless %}
|
||||
<img id="{{ image_id }}" src="{{ captcha_code }}" alt="" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
|
||||
{% apply spaceless %}
|
||||
<img class="captcha_image" id="{{ image_id }}" src="{{ captcha_code }}" alt="" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
|
||||
{% if reload %}
|
||||
<script type="text/javascript">
|
||||
function reload_{{ image_id }}() {
|
||||
@ -14,7 +14,6 @@
|
||||
<a class="captcha_reload" href="javascript:reload_{{ image_id }}();">{{ 'Renew'|trans({}, 'gregwar_captcha') }}</a>
|
||||
{% endif %}
|
||||
{{ form_widget(form) }}
|
||||
{% endspaceless %}
|
||||
{% endapply %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
|
@ -7,6 +7,7 @@ use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
@ -21,6 +22,8 @@ use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
|
||||
*/
|
||||
class CaptchaType extends AbstractType
|
||||
{
|
||||
const SESSION_KEY_PREFIX = '_captcha_';
|
||||
|
||||
/**
|
||||
* @var SessionInterface
|
||||
*/
|
||||
@ -52,7 +55,7 @@ class CaptchaType extends AbstractType
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->generator = $generator;
|
||||
$this->translator = $translator;
|
||||
$this->translator = $translator;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
@ -64,13 +67,14 @@ class CaptchaType extends AbstractType
|
||||
$validator = new CaptchaValidator(
|
||||
$this->translator,
|
||||
$this->session,
|
||||
sprintf('gcb_%s', $builder->getForm()->getName()),
|
||||
sprintf('%s%s', self::SESSION_KEY_PREFIX, $options['session_key']),
|
||||
$options['invalid_message'],
|
||||
$options['bypass_code'],
|
||||
$options['humanity']
|
||||
$options['humanity'],
|
||||
$options['request']
|
||||
);
|
||||
|
||||
$builder->addEventListener(FormEvents::POST_BIND, array($validator, 'validate'));
|
||||
$event = \Symfony\Component\HttpKernel\Kernel::VERSION >= 2.3 ? FormEvents::POST_SUBMIT : FormEvents::POST_BIND;
|
||||
$builder->addEventListener($event, array($validator, 'validate'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,7 +86,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('gcb_%s', $form->getName());
|
||||
$sessionKey = sprintf('%s%s', self::SESSION_KEY_PREFIX, $options['session_key']);
|
||||
$isHuman = false;
|
||||
|
||||
if ($options['humanity'] > 0) {
|
||||
@ -112,7 +116,8 @@ class CaptchaType extends AbstractType
|
||||
));
|
||||
|
||||
$persistOptions = array();
|
||||
foreach (array('phrase', 'width', 'height', 'distortion', 'length', 'quality', 'background_color', 'text_color') as $key) {
|
||||
foreach (array('phrase', 'width', 'height', 'distortion', 'length',
|
||||
'quality', 'background_color', 'background_images', 'text_color') as $key) {
|
||||
$persistOptions[$key] = $options[$key];
|
||||
}
|
||||
|
||||
@ -122,24 +127,43 @@ class CaptchaType extends AbstractType
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$this->options['mapped'] = false;
|
||||
$this->options['request'] = null;
|
||||
$resolver->setDefaults($this->options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* BC for SF < 2.7
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$this->configureOptions($resolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return 'text';
|
||||
// Not using ::class to support Symfony 2.8 w/ php>=5.3.9
|
||||
return 'Symfony\Component\Form\Extension\Core\Type\TextType';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->getBlockPrefix();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'captcha';
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Captcha validator
|
||||
@ -46,6 +47,13 @@ class CaptchaValidator
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
/**
|
||||
* Request
|
||||
*
|
||||
* @var Request
|
||||
*/
|
||||
private $req;
|
||||
|
||||
/**
|
||||
* @param TranslatorInterface $translator
|
||||
* @param SessionInterface $session
|
||||
@ -54,7 +62,7 @@ class CaptchaValidator
|
||||
* @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, Request $req = null)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
$this->session = $session;
|
||||
@ -62,6 +70,7 @@ class CaptchaValidator
|
||||
$this->invalidMessage = $invalidMessage;
|
||||
$this->bypassCode = (string)$bypassCode;
|
||||
$this->humanity = $humanity;
|
||||
$this->req = $req;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,10 +99,11 @@ class CaptchaValidator
|
||||
}
|
||||
}
|
||||
|
||||
$this->session->remove($this->key);
|
||||
|
||||
if ($this->session->has($this->key . '_fingerprint')) {
|
||||
$this->session->remove($this->key . '_fingerprint');
|
||||
if (null == $this->req || 1 < $this->req->get('flow_registration_step')) {
|
||||
$this->session->remove($this->key);
|
||||
if ($this->session->has($this->key.'_fingerprint')) {
|
||||
$this->session->remove($this->key.'_fingerprint');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
{
|
||||
"name": "gregwar/captcha-bundle",
|
||||
"name": "cadoles/captcha",
|
||||
"type": "captcha-bundle",
|
||||
"description": "Captcha bundle",
|
||||
"keywords": ["symfony2", "captcha", "bot", "visual", "code", "security", "spam"],
|
||||
"homepage": "https://github.com/Gregwar/CaptchaBundle",
|
||||
"homepage": "https://github.com/Cadoles/CaptchaBundle",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
@ -17,10 +17,11 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"php": ">=5.3.9",
|
||||
"gregwar/captcha": "~1.1",
|
||||
"symfony/framework-bundle": "~2.1",
|
||||
"symfony/form": "~2.1"
|
||||
"symfony/framework-bundle": "~2.8|~3.0|~4.0",
|
||||
"symfony/form": "~2.8|~3.0|~4.0",
|
||||
"twig/twig": "^1.40|^2.9"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
Reference in New Issue
Block a user