Compare commits
52 Commits
Author | SHA1 | Date | |
---|---|---|---|
e91cb1a3b7 | |||
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 | |||
c221d9cb42 | |||
7574ad33f1 | |||
d80ff1959b | |||
694994c3c2 | |||
01429c4486 | |||
6032e5df52 | |||
d77e0b1f2a | |||
a4176fcf74 | |||
909d1a3773 | |||
3d1383e8ae | |||
798f29e635 | |||
34a84af209 | |||
3ad62e4d0d | |||
a16743a230 | |||
4f46a609ba | |||
b085af6e00 |
@ -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;
|
||||
|
||||
/**
|
||||
@ -37,7 +37,7 @@ class CaptchaController extends Controller
|
||||
}
|
||||
|
||||
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 */
|
||||
@ -54,7 +54,29 @@ 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');
|
||||
$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;
|
||||
}
|
||||
|
@ -44,7 +44,9 @@ 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()
|
||||
->end()
|
||||
;
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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;
|
||||
|
||||
@ -18,7 +19,7 @@ use Gregwar\Captcha\PhraseBuilderInterface;
|
||||
class CaptchaGenerator
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Component\Routing\RouterInterface
|
||||
* @var RouterInterface
|
||||
*/
|
||||
protected $router;
|
||||
|
||||
@ -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;
|
||||
@ -53,7 +59,6 @@ class CaptchaGenerator
|
||||
/**
|
||||
* Get the captcha URL, stream, or filename that will go in the image's src attribute
|
||||
*
|
||||
* @param $key
|
||||
* @param array $options
|
||||
*
|
||||
* @return array
|
||||
@ -71,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));
|
||||
@ -86,7 +92,6 @@ class CaptchaGenerator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
@ -120,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'],
|
||||
@ -142,7 +150,6 @@ class CaptchaGenerator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
|
2
LICENSE
2
LICENSE
@ -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
|
||||
|
43
README.md
43
README.md
@ -1,12 +1,15 @@
|
||||
Gregwar's CaptchaBundle
|
||||
=====================
|
||||
|
||||
The `GregwarCaptchaBundle` adds support for a "captcha" form type for the
|
||||
Symfony2 form component.
|
||||
The `GregwarCaptchaBundle` adds support for a captcha form type for the
|
||||
Symfony form component.
|
||||
|
||||
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.
|
||||
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 +20,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 +56,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 +102,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 +145,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: GregwarCaptchaBundle:Captcha:generateCaptcha }
|
||||
|
@ -9,9 +9,9 @@ services:
|
||||
captcha.type:
|
||||
class: %gregwar_captcha.captcha_type.class%
|
||||
arguments:
|
||||
- @session
|
||||
- @gregwar_captcha.generator
|
||||
- @translator
|
||||
- '@session'
|
||||
- '@gregwar_captcha.generator'
|
||||
- '@translator'
|
||||
- %gregwar_captcha.config%
|
||||
tags:
|
||||
- { name: form.type, alias: captcha }
|
||||
@ -19,10 +19,10 @@ services:
|
||||
gregwar_captcha.generator:
|
||||
class: %gregwar_captcha.captcha_generator.class%
|
||||
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%
|
||||
|
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
|
@ -17,4 +17,3 @@
|
||||
{% endspaceless %}
|
||||
{% 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;
|
||||
@ -69,8 +70,8 @@ class CaptchaType extends AbstractType
|
||||
$options['bypass_code'],
|
||||
$options['humanity']
|
||||
);
|
||||
|
||||
$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'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,18 +123,28 @@ class CaptchaType extends AbstractType
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$this->options['mapped'] = false;
|
||||
$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';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,15 +17,14 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.0",
|
||||
"gregwar/captcha": "~1.0.12",
|
||||
"symfony/framework-bundle": "~2.1",
|
||||
"symfony/form": "~2.1"
|
||||
"php": ">=5.3.9",
|
||||
"gregwar/captcha": "~1.1",
|
||||
"symfony/framework-bundle": "~2.1|~3.0",
|
||||
"symfony/form": "~2.1|~3.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Gregwar\\CaptchaBundle": ""
|
||||
"psr-4": {
|
||||
"Gregwar\\CaptchaBundle\\": "/"
|
||||
}
|
||||
},
|
||||
"target-dir": "Gregwar/CaptchaBundle"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user