Add key parameter to URL generation method.

This commit is contained in:
Jeremy Livingston 2012-12-03 14:49:17 -05:00
parent a41e4dd865
commit 9fc82c8453
6 changed files with 34 additions and 33 deletions

View File

@ -17,19 +17,21 @@ 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
*/
public function generateCaptchaAction(Request $request)
public function generateCaptchaAction(Request $request, $key)
{
$options = $this->container->getParameter('gregwar_captcha.config');
if (!$options['as_url']) {
if (!$options['as_url'] || !in_array($key, $options['valid_keys'])) {
return $this->createNotFoundException('Unable to generate a captcha via a URL without the proper configuration.');
}
/* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
$generator = $this->container->get('gregwar_captcha.generator');
return new Response($generator->generate($options));
return new Response($generator->generate($key, $options));
}
}

View File

@ -15,7 +15,7 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('gregwar_captcha', 'array');
$rootNode = $treeBuilder->root('gregwar_captcha');
$rootNode
->addDefaultsIfNotSet()
@ -28,7 +28,6 @@ class Configuration implements ConfigurationInterface
->scalarNode('charset')->defaultValue('abcdefhjkmnprstuvwxyz23456789')->end()
->scalarNode('as_file')->defaultValue(false)->end()
->scalarNode('as_url')->defaultValue(false)->end()
->scalarNode('url')->defaultValue('/generate-captcha')->end()
->scalarNode('image_folder')->defaultValue('captcha')->end()
->scalarNode('web_path')->defaultValue('%kernel.root_dir%/../web')->end()
->scalarNode('gc_freq')->defaultValue(100)->end()
@ -36,8 +35,10 @@ class Configuration implements ConfigurationInterface
->scalarNode('quality')->defaultValue(15)->end()
->scalarNode('invalid_message')->defaultValue('Bad code value')->end()
->scalarNode('bypass_code')->defaultValue(null)->end()
->arrayNode('valid_keys')->defaultValue(array('captcha'))->prototype('scalar')->end()
->end()
;
return $treeBuilder;
}
}

View File

@ -31,7 +31,6 @@ class GregwarCaptchaExtension extends Extension
$container->setParameter('gregwar_captcha.config.web_path', $config['web_path']);
$container->setParameter('gregwar_captcha.config.gc_freq', $config['gc_freq']);
$container->setParameter('gregwar_captcha.config.expiration', $config['expiration']);
$container->setParameter('gregwar_captcha.config.url', $config['url']);
$resources = $container->getParameter('twig.form.resources');
$container->setParameter('twig.form.resources', array_merge(array('GregwarCaptchaBundle::captcha.html.twig'), $resources));

View File

@ -4,6 +4,7 @@ namespace Gregwar\CaptchaBundle\Generator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Generates a CAPTCHA image
@ -15,6 +16,11 @@ class CaptchaGenerator
*/
protected $session;
/**
* @var \Symfony\Component\Routing\RouterInterface
*/
protected $router;
/**
* Name of folder for captcha images
* @var string
@ -51,28 +57,22 @@ class CaptchaGenerator
*/
protected $useFingerprint;
/**
* The key used to store the value to the session
* @var string
*/
protected $key = 'captcha';
/**
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Symfony\Component\Routing\RouterInterface $router
* @param string $imageFolder
* @param string $webPath
* @param int $gcFreq
* @param int $expiration
* @param string $url
*/
public function __construct(SessionInterface $session, $imageFolder, $webPath, $gcFreq, $expiration, $url)
public function __construct(SessionInterface $session, RouterInterface $router, $imageFolder, $webPath, $gcFreq, $expiration)
{
$this->session = $session;
$this->router = $router;
$this->imageFolder = $imageFolder;
$this->webPath = $webPath;
$this->gcFreq = $gcFreq;
$this->expiration = $expiration;
$this->url = $url;
}
/**
@ -85,42 +85,40 @@ class CaptchaGenerator
*/
public function getCaptchaCode($key, array $options)
{
$this->key = $key;
// Randomly execute garbage collection and returns the image filename
if ($options['as_file']) {
if (mt_rand(1, $this->gcFreq) == 1) {
$this->garbageCollection();
}
return $this->generate($options);
return $this->generate($key, $options);
}
// Returns the configured URL for image generation
if ($options['as_url']) {
return $this->url;
return $this->router->generate('gregwar_captcha.generate_captcha', array('key' => $key));
}
return 'data:image/jpeg;base64,' . base64_encode($this->generate($options));
return 'data:image/jpeg;base64,' . base64_encode($this->generate($key, $options));
}
/**
* Generate the image
*/
public function generate(array $options)
public function generate($key, array $options)
{
$width = $options['width'];
$height = $options['height'];
if ($options['keep_value'] && $this->session->has($this->key.'_fingerprint')) {
$this->fingerprint = $this->session->get($this->key.'_fingerprint');
if ($options['keep_value'] && $this->session->has($key . '_fingerprint')) {
$this->fingerprint = $this->session->get($key . '_fingerprint');
$this->useFingerprint = true;
} else {
$this->fingerprint = null;
$this->useFingerprint = false;
}
$captchaValue = $this->getCaptchaValue($options['keep_value'], $options['charset'], $options['length']);
$captchaValue = $this->getCaptchaValue($key, $options['keep_value'], $options['charset'], $options['length']);
$i = imagecreatetruecolor($width,$height);
$col = imagecolorallocate($i, $this->rand(0,110), $this->rand(0,110), $this->rand(0,110));
@ -185,7 +183,7 @@ class CaptchaGenerator
}
if ($options['keep_value']) {
$this->session->set($this->key.'_fingerprint', $this->fingerprint);
$this->session->set($key . '_fingerprint', $this->fingerprint);
}
// Renders it
@ -211,16 +209,17 @@ class CaptchaGenerator
/**
* Generate a new captcha value or pull the existing one from the session
*
* @param string $key
* @param bool $keepValue
* @param string $charset
* @param int $length
*
* @return mixed|string
*/
protected function getCaptchaValue($keepValue, $charset, $length)
protected function getCaptchaValue($key, $keepValue, $charset, $length)
{
if ($keepValue && $this->session->has($this->key)) {
return $this->session->get($this->key);
if ($keepValue && $this->session->has($key)) {
return $this->session->get($key);
}
$value = '';
@ -230,7 +229,7 @@ class CaptchaGenerator
$value .= $chars[array_rand($chars)];
}
$this->session->set($this->key, $value);
$this->session->set($key, $value);
return $value;
}

View File

@ -1,3 +1,3 @@
gregwar_captcha.generate_captcha:
pattern: %gregwar_captcha.config.url%
defaults: { _controller: GregwarCaptchaBundle:Captcha:generateCaptcha }
pattern: /generate-captcha/{key}
defaults: { _controller: GregwarCaptchaBundle:Captcha:generateCaptcha }

View File

@ -14,8 +14,8 @@ services:
class: Gregwar\CaptchaBundle\Generator\CaptchaGenerator
arguments:
- @session
- @router
- %gregwar_captcha.config.image_folder%
- %gregwar_captcha.config.web_path%
- %gregwar_captcha.config.gc_freq%
- %gregwar_captcha.config.expiration%
- %gregwar_captcha.config.url%
- %gregwar_captcha.config.expiration%