| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 
 | <?php
 
namespace Gregwar\CaptchaBundle\Type;
 
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\DependencyInjection\ContainerInterface;
 
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormViewInterface;
 
use Gregwar\CaptchaBundle\Validator\CaptchaValidator;
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
use Gregwar\CaptchaBundle\DataTransformer\EmptyTransformer;
 
/**
 * Captcha type
 *
 * @author Gregwar <g.passault@gmail.com>
 */
class CaptchaType extends AbstractType
{
    /**
     * Options
     * @var array
     */
    private $options = array();
 
    /**
     * Session key
     * @var string
     */
    private $key = 'captcha';
 
    public function __construct(Session $session, $config)
    {
        $this->session = $session;
        $this->options = $config;
    }
 
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->key = $builder->getForm()->getName();
 
        $builder->addValidator(
            new CaptchaValidator($this->session,
                                 $this->key,
                                 $options['invalid_message'],
                                 $options['bypass_code'])
        );
    }
 
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $fingerprint = null;
 
        if ($options['keep_value'] && $this->session->has($this->key.'_fingerprint')) {
            $fingerprint = $this->session->get($this->key.'_fingerprint');
        }
 
        $generator = new CaptchaGenerator($this->generateCaptchaValue(),
                                          $options['image_folder'],
                                          $options['web_path'],
                                          $options['gc_freq'],
                                          $options['expiration'],
                                          $options['font'],
                                          $fingerprint,
                                          $options['quality']);
 
        if ($options['as_file']) {
            $captchaCode = $generator->getFile($options['width'], $options['height']);
        } else {
            $captchaCode = $generator->getCode($options['width'], $options['height']);
        }
 
        if ($options['keep_value']) {
            $this->session->set($this->key.'_fingerprint', $generator->getFingerprint());
        }
 
        $fieldVars = array(
            'captcha_width'     => $options['width'],
            'captcha_height'    => $options['height'],
            'captcha_code'      => $captchaCode,
            'value'             => '',
        );
 
        foreach($fieldVars as $name => $value){
            $view->set($name,$value);    
        }
    }
 
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $this->options['property_path'] = false;
        $resolver->setDefaults($this->options);
    }
 
    public function getParent()
    {
        return 'text';
    }
 
    public function getName()
    {
        return 'captcha';
    }
 
    private function generateCaptchaValue()
    {
        if (!$this->options['keep_value'] || !$this->session->has($this->key)) {
            $value = '';
            $chars = str_split($this->options['charset']);
 
            for ($i=0; $i<$this->options['length']; $i++) {
                $value.= $chars[array_rand($chars)];
            }
 
            $this->session->set($this->key, $value);
        } else {
            $value = $this->session->get($this->key);
        }
 
        return $value;
    }
} | 
Partager