[Configuration] The charset is configurable (fixes #8)

This commit is contained in:
Gregwar 2011-12-02 18:41:16 +01:00
parent c0530a0b26
commit 537716fe38
3 changed files with 11 additions and 3 deletions

View File

@ -22,6 +22,7 @@ class Configuration implements ConfigurationInterface
->scalarNode('length')->defaultValue(5)->end()
->scalarNode('width')->defaultValue(120)->end()
->scalarNode('height')->defaultValue(40)->end()
->scalarNode('charset')->defaultValue('abcdefhjkmnprstuvwxyz23456789')->end()
->scalarNode('as_file')->defaultValue(false)->end()
->scalarNode('image_folder')->defaultValue('captcha')->end()
->scalarNode('web_path')->defaultValue('%kernel.root_dir%/../web')->end()

View File

@ -100,6 +100,7 @@ You can define the following type option :
* **width**: the width of the captcha image (default=120)
* **height**: the height of the captcha image (default=40)
* **length**: the length of the captcha (number of chars, default 5)
* **charset**: the charset used for code generation (default=abcdefhjkmnprstuvwxyz23456789)
* **as_file**: if set to true an image file will be created instead of embedding to please IE6/7 (default=false)
* **image_folder**: name of folder for captcha images relative to public web folder in case **as_file** ist set to true (default="captcha")
* **web_path**: absolute path to public web folder (default="%kernel.root_dir%/../web")

View File

@ -45,6 +45,12 @@ class CaptchaType extends AbstractType
*/
protected $asFile;
/**
* Charset used
* @var string
*/
protected $charset;
/**
* Folder to save captcha images in,
* relative to public web folder
@ -78,7 +84,7 @@ class CaptchaType extends AbstractType
/**
* Session key
* @var String
* @var string
*/
private $key = 'captcha';
@ -91,6 +97,7 @@ class CaptchaType extends AbstractType
$this->height = $config['height'];
$this->length = $config['length'];
$this->asFile = $config['as_file'];
$this->charset = $config['charset'];
$this->imageFolder = $config['image_folder'];
$this->webPath = $config['web_path'];
$this->gcFreq = $config['gc_freq'];
@ -156,8 +163,7 @@ class CaptchaType extends AbstractType
{
if (!$this->session->has($this->key)) {
$value = '';
$charset = 'abcdefhjkmnprstuvwxyz23456789';
$chars = str_split($charset);
$chars = str_split($this->charset);
for ($i=0; $i<$this->length; $i++) {
$value.= $chars[array_rand($chars)];