]> Raphaël G. Git Repositories - packbundle/blob - Form/CaptchaType.php
Version 0.5.0
[packbundle] / Form / CaptchaType.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Form;
13
14 use Rapsys\PackBundle\Util\ImageUtil;
15 use Rapsys\PackBundle\Util\SluggerUtil;
16
17 use Symfony\Component\Form\AbstractType;
18 use Symfony\Component\Form\Extension\Core\Type\HiddenType;
19 use Symfony\Component\Form\Extension\Core\Type\IntegerType;
20 use Symfony\Component\Form\FormBuilderInterface;
21 use Symfony\Component\Form\FormError;
22 use Symfony\Component\Form\FormEvent;
23 use Symfony\Component\Form\FormEvents;
24 use Symfony\Contracts\Translation\TranslatorInterface;
25
26 /**
27 * Captcha Type class definition
28 *
29 * @see https://symfony.com/doc/current/form/create_custom_field_type.html
30 */
31 class CaptchaType extends AbstractType {
32 /**
33 * Constructor
34 *
35 * @param ?ImageUtil $image
36 * @param ?SluggerUtil $slugger
37 * @param ?TranslatorInterface $translator The translator instance
38 */
39 public function __construct(protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) {
40 }
41
42 /**
43 * {@inheritdoc}
44 *
45 * Build form
46 */
47 public function buildForm(FormBuilderInterface $builder, array $options): void {
48 //With image, slugger and translator
49 if ($this->image !== null && $this->slugger !== null && $this->translator !== null) {
50 //Set captcha
51 $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp());
52
53 //Add captcha token
54 $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token'], 'mapped' => false]);
55
56 //Add captcha
57 $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => '<img src="'.htmlentities($captcha['src']).'" alt="'.htmlentities($captcha['equation']).'" />', 'label_html' => true, 'mapped' => false, 'translation_domain' => false]);
58
59 //Add event listener on captcha
60 $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']);
61 }
62 }
63
64 /**
65 * Validate captcha
66 *
67 * @param FormEvent $event The form event
68 */
69 public function validateCaptcha(FormEvent $event): void {
70 //Get form
71 $form = $event->getForm();
72
73 //Get event data
74 $data = $event->getData();
75
76 //Set token
77 $token = $form->get('_captcha_token')->getConfig()->getData();
78
79 //Without captcha
80 if (empty($data['captcha'])) {
81 //Add error on captcha
82 $form->addError(new FormError($this->translator->trans('Captcha is empty')));
83
84 //Reset captcha token
85 $data['_captcha_token'] = $token;
86
87 //Set event data
88 $event->setData($data);
89 //With invalid captcha
90 } elseif ($this->slugger->hash($data['captcha']) !== $data['_captcha_token']) {
91 //Add error on captcha
92 $form->addError(new FormError($this->translator->trans('Captcha is invalid')));
93
94 //Reset captcha token
95 $data['_captcha_token'] = $token;
96
97 //Set event data
98 $event->setData($data);
99 }
100 }
101 }