Compare commits
106 Commits
Author | SHA1 | Date | |
---|---|---|---|
980afdc10a | |||
791128c0fd | |||
639430383f | |||
8e98c5c0ab | |||
539884cd5d | |||
8ce4adb1b1 | |||
b787a8002e | |||
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 | |||
c221d9cb42 | |||
7574ad33f1 | |||
d80ff1959b | |||
694994c3c2 | |||
01429c4486 | |||
6032e5df52 | |||
d77e0b1f2a | |||
a4176fcf74 | |||
909d1a3773 | |||
d6c6de0f83 | |||
ac95e1d4b1 | |||
c844d0a440 | |||
f20fc3c3b7 | |||
0cb34f33b0 | |||
843f367540 | |||
94c5f0a03b | |||
6a522b7043 | |||
3d1383e8ae | |||
62e44cd388 | |||
459901319c | |||
1c542e1164 | |||
dc3e0c6495 | |||
bd208c45a8 | |||
9614209d83 | |||
90088c80fc | |||
06c713fbb4 | |||
162a2a87cd | |||
fe3df8a640 | |||
b87b05e29d | |||
cdf64984b7 | |||
458e7cafef | |||
ce96122ef2 | |||
c4c69b0a47 | |||
6b573121a5 | |||
e34e191d32 | |||
7dd05f5389 | |||
de0d9408d6 | |||
cb45fe2847 | |||
1fec709d05 | |||
35cbc6226c | |||
7c38520965 | |||
277e3baff0 | |||
798f29e635 | |||
f2e07fe6f2 | |||
ab786e3b0e | |||
f206bb66cc | |||
3340af5c43 | |||
bc3fdda50b | |||
b2340539d2 | |||
34a84af209 | |||
3ad62e4d0d | |||
a16743a230 | |||
4f46a609ba | |||
b085af6e00 |
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace Gregwar\CaptchaBundle\Controller;
|
namespace Gregwar\CaptchaBundle\Controller;
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a captcha via a URL
|
* 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
|
* Action that is used to generate the captcha, save its code, and stream the image
|
||||||
*
|
*
|
||||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
|
||||||
* @param string $key
|
* @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');
|
$options = $this->container->getParameter('gregwar_captcha.config');
|
||||||
$session = $this->get('session');
|
$session = $this->get('session');
|
||||||
@ -36,7 +37,7 @@ class CaptchaController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$isOk) {
|
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 */
|
/* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
|
||||||
@ -52,8 +53,31 @@ class CaptchaController extends Controller
|
|||||||
|
|
||||||
$response = new Response($generator->generate($options));
|
$response = new Response($generator->generate($options));
|
||||||
$response->headers->set('Content-type', 'image/jpeg');
|
$response->headers->set('Content-type', 'image/jpeg');
|
||||||
|
$response->headers->set('Pragma', '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;
|
return $response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ class Configuration implements ConfigurationInterface
|
|||||||
->scalarNode('web_path')->defaultValue('%kernel.root_dir%/../web')->end()
|
->scalarNode('web_path')->defaultValue('%kernel.root_dir%/../web')->end()
|
||||||
->scalarNode('gc_freq')->defaultValue(100)->end()
|
->scalarNode('gc_freq')->defaultValue(100)->end()
|
||||||
->scalarNode('expiration')->defaultValue(60)->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('invalid_message')->defaultValue('Bad code value')->end()
|
||||||
->scalarNode('bypass_code')->defaultValue(null)->end()
|
->scalarNode('bypass_code')->defaultValue(null)->end()
|
||||||
->scalarNode('whitelist_key')->defaultValue('captcha_whitelist_key')->end()
|
->scalarNode('whitelist_key')->defaultValue('captcha_whitelist_key')->end()
|
||||||
@ -44,11 +44,12 @@ class Configuration implements ConfigurationInterface
|
|||||||
->scalarNode('interpolation')->defaultValue(true)->end()
|
->scalarNode('interpolation')->defaultValue(true)->end()
|
||||||
->arrayNode('text_color')->prototype('scalar')->end()->end()
|
->arrayNode('text_color')->prototype('scalar')->end()->end()
|
||||||
->arrayNode('background_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('disabled')->defaultValue(false)->end()
|
||||||
|
->scalarNode('ignore_all_effects')->defaultValue(false)->end()
|
||||||
->end()
|
->end()
|
||||||
;
|
;
|
||||||
|
|
||||||
return $treeBuilder;
|
return $treeBuilder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ class GregwarCaptchaExtension extends Extension
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param array $configs
|
* @param array $configs
|
||||||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
|
* @param ContainerBuilder $container
|
||||||
*/
|
*/
|
||||||
public function load(array $configs, ContainerBuilder $container)
|
public function load(array $configs, ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
@ -34,6 +34,6 @@ class GregwarCaptchaExtension extends Extension
|
|||||||
$container->setParameter('gregwar_captcha.config.whitelist_key', $config['whitelist_key']);
|
$container->setParameter('gregwar_captcha.config.whitelist_key', $config['whitelist_key']);
|
||||||
|
|
||||||
$resources = $container->getParameter('twig.form.resources');
|
$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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
namespace Gregwar\CaptchaBundle\Generator;
|
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\HttpFoundation\Session\SessionInterface;
|
||||||
use Symfony\Component\Routing\RouterInterface;
|
use Symfony\Component\Routing\RouterInterface;
|
||||||
|
|
||||||
@ -18,7 +19,7 @@ use Gregwar\Captcha\PhraseBuilderInterface;
|
|||||||
class CaptchaGenerator
|
class CaptchaGenerator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \Symfony\Component\Routing\RouterInterface
|
* @var RouterInterface
|
||||||
*/
|
*/
|
||||||
protected $router;
|
protected $router;
|
||||||
|
|
||||||
@ -38,12 +39,17 @@ class CaptchaGenerator
|
|||||||
protected $imageFileHandler;
|
protected $imageFileHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Symfony\Component\Routing\RouterInterface $router
|
* @param RouterInterface $router
|
||||||
* @param CaptchaBuilderInterface $builder
|
* @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->router = $router;
|
||||||
$this->builder = $builder;
|
$this->builder = $builder;
|
||||||
$this->phraseBuilder = $phraseBuilder;
|
$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
|
* Get the captcha URL, stream, or filename that will go in the image's src attribute
|
||||||
*
|
*
|
||||||
* @param $key
|
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
@ -71,7 +76,8 @@ class CaptchaGenerator
|
|||||||
|
|
||||||
// Returns the image generation URL
|
// Returns the image generation URL
|
||||||
if ($options['as_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));
|
return 'data:image/jpeg;base64,' . base64_encode($this->generate($options));
|
||||||
@ -86,7 +92,6 @@ class CaptchaGenerator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
@ -120,6 +125,9 @@ class CaptchaGenerator
|
|||||||
|
|
||||||
$fingerprint = isset($options['fingerprint']) ? $options['fingerprint'] : null;
|
$fingerprint = isset($options['fingerprint']) ? $options['fingerprint'] : null;
|
||||||
|
|
||||||
|
$this->builder->setBackgroundImages($options['background_images']);
|
||||||
|
$this->builder->setIgnoreAllEffects($options['ignore_all_effects']);
|
||||||
|
|
||||||
$content = $this->builder->build(
|
$content = $this->builder->build(
|
||||||
$options['width'],
|
$options['width'],
|
||||||
$options['height'],
|
$options['height'],
|
||||||
@ -142,7 +150,6 @@ class CaptchaGenerator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
@ -160,4 +167,3 @@ class CaptchaGenerator
|
|||||||
return $phrase;
|
return $phrase;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,4 +103,3 @@ class ImageFileHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
49
README.md
49
README.md
@ -1,12 +1,19 @@
|
|||||||
Gregwar's CaptchaBundle
|
Gregwar's CaptchaBundle
|
||||||
=====================
|
=====================
|
||||||
|
|
||||||
The `GregwarCaptchaBundle` adds support for a "captcha" form type for the
|
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YUXRLWHQSWS6L)
|
||||||
Symfony2 form component.
|
|
||||||
|
|
||||||
Important note: the master of this repository is containing current development
|
The `GregwarCaptchaBundle` adds support for a captcha form type for the
|
||||||
in order to work with Symfony 2.1. If you are using Symfony 2.0 please checkout
|
Symfony form component.
|
||||||
the 2.0 branch.
|
|
||||||
|
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
|
Installation
|
||||||
============
|
============
|
||||||
@ -17,7 +24,15 @@ Ultimately, the GregwarCaptchaBundle files should be downloaded to the
|
|||||||
'vendor/bundles/Gregwar/CaptchaBundle' directory.
|
'vendor/bundles/Gregwar/CaptchaBundle' directory.
|
||||||
|
|
||||||
You can accomplish this several ways, depending on your personal preference.
|
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***
|
***Using the vendors script***
|
||||||
|
|
||||||
@ -45,16 +60,6 @@ $ git submodule add git://github.com/Gregwar/CaptchaBundle.git vendor/bundles/Gr
|
|||||||
$ git submodule update --init
|
$ 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
|
### Step 2: Configure the Autoloader
|
||||||
|
|
||||||
If you use composer, you can skip this step.
|
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
|
||||||
<?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');
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -126,7 +134,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
|
* **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
|
still appear but won't be editable and won't be checked
|
||||||
* **length**: the length of the captcha (number of chars, default 5)
|
* **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)
|
* **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
|
* **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)
|
* **keep_value**: the value will be the same until the form is posted, even if the page is refreshed (default=true)
|
||||||
@ -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
|
* **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)
|
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_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
|
* **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 :
|
Example :
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
use Gregwar\CaptchaBundle\Type\CaptchaType;
|
||||||
// ...
|
// ...
|
||||||
$builder->add('captcha', 'captcha', array(
|
$builder->add('captcha', CaptchaType::class, array(
|
||||||
'width' => 200,
|
'width' => 200,
|
||||||
'height' => 50,
|
'height' => 50,
|
||||||
'length' => 6,
|
'length' => 6,
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
gregwar_captcha.generate_captcha:
|
gregwar_captcha.generate_captcha:
|
||||||
pattern: /generate-captcha/{key}
|
path: /generate-captcha/{key}
|
||||||
defaults: { _controller: GregwarCaptchaBundle:Captcha:generateCaptcha }
|
defaults: { _controller: GregwarCaptchaBundle:Captcha:generateCaptcha }
|
@ -1,32 +1,44 @@
|
|||||||
|
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:
|
services:
|
||||||
captcha.type:
|
captcha.type:
|
||||||
class: Gregwar\CaptchaBundle\Type\CaptchaType
|
class: '%gregwar_captcha.captcha_type.class%'
|
||||||
|
public: true
|
||||||
arguments:
|
arguments:
|
||||||
- @session
|
- '@session'
|
||||||
- @gregwar_captcha.generator
|
- '@gregwar_captcha.generator'
|
||||||
- @translator
|
- '@translator'
|
||||||
- %gregwar_captcha.config%
|
- '%gregwar_captcha.config%'
|
||||||
tags:
|
tags:
|
||||||
- { name: form.type, alias: captcha }
|
- { name: form.type, alias: captcha }
|
||||||
|
|
||||||
gregwar_captcha.generator:
|
gregwar_captcha.generator:
|
||||||
class: Gregwar\CaptchaBundle\Generator\CaptchaGenerator
|
class: '%gregwar_captcha.captcha_generator.class%'
|
||||||
|
public: true
|
||||||
arguments:
|
arguments:
|
||||||
- @router
|
- '@router'
|
||||||
- @gregwar_captcha.captcha_builder
|
- '@gregwar_captcha.captcha_builder'
|
||||||
- @gregwar_captcha.phrase_builder
|
- '@gregwar_captcha.phrase_builder'
|
||||||
- @gregwar_captcha.image_file_handler
|
- '@gregwar_captcha.image_file_handler'
|
||||||
|
|
||||||
gregwar_captcha.image_file_handler:
|
gregwar_captcha.image_file_handler:
|
||||||
class: Gregwar\CaptchaBundle\Generator\ImageFileHandler
|
class: '%gregwar_captcha.image_file_handler.class%'
|
||||||
|
public: true
|
||||||
arguments:
|
arguments:
|
||||||
- %gregwar_captcha.config.image_folder%
|
- '%gregwar_captcha.config.image_folder%'
|
||||||
- %gregwar_captcha.config.web_path%
|
- '%gregwar_captcha.config.web_path%'
|
||||||
- %gregwar_captcha.config.gc_freq%
|
- '%gregwar_captcha.config.gc_freq%'
|
||||||
- %gregwar_captcha.config.expiration%
|
- '%gregwar_captcha.config.expiration%'
|
||||||
|
|
||||||
gregwar_captcha.captcha_builder:
|
gregwar_captcha.captcha_builder:
|
||||||
class: Gregwar\Captcha\CaptchaBuilder
|
class: '%gregwar_captcha.captcha_builder.class%'
|
||||||
|
public: true
|
||||||
|
|
||||||
gregwar_captcha.phrase_builder:
|
gregwar_captcha.phrase_builder:
|
||||||
class: Gregwar\Captcha\PhraseBuilder
|
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.bg.yml
Normal file
1
Resources/translations/gregwar_captcha.bg.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Renew: Обнови
|
1
Resources/translations/gregwar_captcha.cs.yml
Normal file
1
Resources/translations/gregwar_captcha.cs.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Renew: Obnovit
|
1
Resources/translations/gregwar_captcha.pl.yml
Normal file
1
Resources/translations/gregwar_captcha.pl.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Renew: Przeładuj
|
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/gregwar_captcha.uk.yml
Normal file
1
Resources/translations/gregwar_captcha.uk.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Renew: Оновити
|
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.bg.yml
Normal file
1
Resources/translations/validators.bg.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Bad code value: Грешен код
|
1
Resources/translations/validators.cs.yml
Normal file
1
Resources/translations/validators.cs.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Bad code value: Špatný kontrolní kód
|
1
Resources/translations/validators.pl.yml
Normal file
1
Resources/translations/validators.pl.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Bad code value: Kod jest niepoprawny
|
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
|
1
Resources/translations/validators.uk.yml
Normal file
1
Resources/translations/validators.uk.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
Bad code value: Невірний код
|
@ -3,7 +3,7 @@
|
|||||||
-
|
-
|
||||||
{% else %}
|
{% else %}
|
||||||
{% spaceless %}
|
{% spaceless %}
|
||||||
<img id="{{ image_id }}" src="{{ captcha_code }}" alt="" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
|
<img class="captcha_image" id="{{ image_id }}" src="{{ captcha_code }}" alt="" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
|
||||||
{% if reload %}
|
{% if reload %}
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
function reload_{{ image_id }}() {
|
function reload_{{ image_id }}() {
|
||||||
@ -17,4 +17,3 @@
|
|||||||
{% endspaceless %}
|
{% endspaceless %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ use Symfony\Component\Form\FormView;
|
|||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
use Symfony\Component\Form\AbstractType;
|
use Symfony\Component\Form\AbstractType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||||
use Symfony\Component\Form\FormEvents;
|
use Symfony\Component\Form\FormEvents;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
@ -22,18 +23,12 @@ use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
|
|||||||
class CaptchaType extends AbstractType
|
class CaptchaType extends AbstractType
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
* @var SessionInterface
|
||||||
*/
|
*/
|
||||||
protected $session;
|
protected $session;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The session key
|
* @var CaptchaGenerator
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $key = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator
|
|
||||||
*/
|
*/
|
||||||
protected $generator;
|
protected $generator;
|
||||||
|
|
||||||
@ -49,8 +44,9 @@ class CaptchaType extends AbstractType
|
|||||||
private $options = array();
|
private $options = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
|
* @param SessionInterface $session
|
||||||
* @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator
|
* @param CaptchaGenerator $generator
|
||||||
|
* @param TranslatorInterface $translator
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*/
|
*/
|
||||||
public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options)
|
public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options)
|
||||||
@ -62,53 +58,48 @@ class CaptchaType extends AbstractType
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Symfony\Component\Form\FormBuilderInterface $builder
|
* {@inheritdoc}
|
||||||
* @param array $options
|
|
||||||
*/
|
*/
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
{
|
{
|
||||||
$this->key = 'gcb_'.$builder->getForm()->getName();
|
|
||||||
|
|
||||||
$validator = new CaptchaValidator(
|
$validator = new CaptchaValidator(
|
||||||
$this->translator,
|
$this->translator,
|
||||||
$this->session,
|
$this->session,
|
||||||
$this->key,
|
sprintf('gcb_%s', $builder->getForm()->getName()),
|
||||||
$options['invalid_message'],
|
$options['invalid_message'],
|
||||||
$options['bypass_code'],
|
$options['bypass_code'],
|
||||||
$options['humanity']
|
$options['humanity']
|
||||||
);
|
);
|
||||||
|
$event = \Symfony\Component\HttpKernel\Kernel::VERSION >= 2.3 ? FormEvents::POST_SUBMIT : FormEvents::POST_BIND;
|
||||||
$builder->addEventListener(FormEvents::POST_BIND, array($validator, 'validate'));
|
$builder->addEventListener($event, array($validator, 'validate'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Symfony\Component\Form\FormView $view
|
* {@inheritdoc}
|
||||||
* @param \Symfony\Component\Form\FormInterface $form
|
|
||||||
* @param array $options
|
|
||||||
*/
|
*/
|
||||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||||
{
|
{
|
||||||
$isHuman = false;
|
|
||||||
|
|
||||||
if ($options['reload'] && !$options['as_url']) {
|
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');
|
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());
|
||||||
|
$isHuman = false;
|
||||||
|
|
||||||
if ($options['humanity'] > 0) {
|
if ($options['humanity'] > 0) {
|
||||||
$humanityKey = $this->key.'_humanity';
|
$humanityKey = sprintf('%s_humanity', $sessionKey);
|
||||||
if ($this->session->get($humanityKey, 0) > 0) {
|
if ($this->session->get($humanityKey, 0) > 0) {
|
||||||
$isHuman = true;
|
$isHuman = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($options['as_url']) {
|
if ($options['as_url']) {
|
||||||
$key = $this->key;
|
|
||||||
$keys = $this->session->get($options['whitelist_key'], array());
|
$keys = $this->session->get($options['whitelist_key'], array());
|
||||||
if (!in_array($key, $keys)) {
|
if (!in_array($sessionKey, $keys)) {
|
||||||
$keys[] = $key;
|
$keys[] = $sessionKey;
|
||||||
}
|
}
|
||||||
$this->session->set($options['whitelist_key'], $keys);
|
$this->session->set($options['whitelist_key'], $keys);
|
||||||
$options['session_key'] = $this->key;
|
$options['session_key'] = $sessionKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
$view->vars = array_merge($view->vars, array(
|
$view->vars = array_merge($view->vars, array(
|
||||||
@ -122,34 +113,53 @@ class CaptchaType extends AbstractType
|
|||||||
));
|
));
|
||||||
|
|
||||||
$persistOptions = array();
|
$persistOptions = array();
|
||||||
foreach (array('phrase', 'width', 'height', 'distortion', 'length', 'quality') as $key) {
|
foreach (array('phrase', 'width', 'height', 'distortion', 'length',
|
||||||
|
'quality', 'background_color', 'background_images', 'text_color') as $key) {
|
||||||
$persistOptions[$key] = $options[$key];
|
$persistOptions[$key] = $options[$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->session->set($this->key, $persistOptions);
|
$this->session->set($sessionKey, $persistOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
{
|
{
|
||||||
$this->options['mapped'] = false;
|
$this->options['mapped'] = false;
|
||||||
$resolver->setDefaults($this->options);
|
$resolver->setDefaults($this->options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
* BC for SF < 2.7
|
||||||
|
*/
|
||||||
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||||
|
{
|
||||||
|
$this->configureOptions($resolver);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getParent()
|
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
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
|
{
|
||||||
|
return $this->getBlockPrefix();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBlockPrefix()
|
||||||
{
|
{
|
||||||
return 'captcha';
|
return 'captcha';
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ use Symfony\Component\Translation\TranslatorInterface;
|
|||||||
class CaptchaValidator
|
class CaptchaValidator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
* @var SessionInterface
|
||||||
*/
|
*/
|
||||||
private $session;
|
private $session;
|
||||||
|
|
||||||
@ -47,10 +47,12 @@ class CaptchaValidator
|
|||||||
private $translator;
|
private $translator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
|
* @param TranslatorInterface $translator
|
||||||
|
* @param SessionInterface $session
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param string $invalidMessage
|
* @param string $invalidMessage
|
||||||
* @param string|null $bypassCode
|
* @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)
|
||||||
{
|
{
|
||||||
@ -58,7 +60,7 @@ class CaptchaValidator
|
|||||||
$this->session = $session;
|
$this->session = $session;
|
||||||
$this->key = $key;
|
$this->key = $key;
|
||||||
$this->invalidMessage = $invalidMessage;
|
$this->invalidMessage = $invalidMessage;
|
||||||
$this->bypassCode = $bypassCode;
|
$this->bypassCode = (string)$bypassCode;
|
||||||
$this->humanity = $humanity;
|
$this->humanity = $humanity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +69,7 @@ class CaptchaValidator
|
|||||||
*/
|
*/
|
||||||
public function validate(FormEvent $event)
|
public function validate(FormEvent $event)
|
||||||
{
|
{
|
||||||
$form = $form = $event->getForm();
|
$form = $event->getForm();
|
||||||
|
|
||||||
$code = $form->getData();
|
$code = $form->getData();
|
||||||
$expectedCode = $this->getExpectedCode();
|
$expectedCode = $this->getExpectedCode();
|
||||||
@ -80,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')));
|
$form->addError(new FormError($this->translator->trans($this->invalidMessage, array(), 'validators')));
|
||||||
} else {
|
} else {
|
||||||
if ($this->humanity > 0) {
|
if ($this->humanity > 0) {
|
||||||
@ -112,7 +114,7 @@ class CaptchaValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retreive the humanity
|
* Retrieve the humanity
|
||||||
*
|
*
|
||||||
* @return mixed|null
|
* @return mixed|null
|
||||||
*/
|
*/
|
||||||
@ -157,6 +159,6 @@ class CaptchaValidator
|
|||||||
*/
|
*/
|
||||||
protected function compare($code, $expectedCode)
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,14 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.0",
|
"php": ">=5.3.9",
|
||||||
"gregwar/captcha": "v1.0.10"
|
"gregwar/captcha": "~1.1",
|
||||||
|
"symfony/framework-bundle": "~2.8|~3.0|~4.0",
|
||||||
|
"symfony/form": "~2.8|~3.0|~4.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": {
|
"psr-4": {
|
||||||
"Gregwar\\CaptchaBundle": ""
|
"Gregwar\\CaptchaBundle\\": "/"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"target-dir": "Gregwar/CaptchaBundle"
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user