]> Raphaël G. Git Repositories - packbundle/blob - Form/CaptchaType.php
Add RAPSYSPACK_AGENT, RAPSYSPACK_HOSTNAME, RAPSYSPACK_RANGE, RAPSYSPACK_REDIRECT...
[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\Contracts\Translation\TranslatorInterface;
18 use Symfony\Component\Form\AbstractType;
19 use Symfony\Component\Form\Extension\Core\Type\HiddenType;
20 use Symfony\Component\Form\Extension\Core\Type\IntegerType;
21 use Symfony\Component\Form\FormBuilderInterface;
22 use Symfony\Component\Form\FormEvents;
23 use Symfony\Component\Form\FormEvent;
24 use Symfony\Component\Form\FormError;
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, protected SluggerUtil $slugger, protected TranslatorInterface $translator) {
40 }
41
42 /**
43 * Build form
44 *
45 * {@inheritdoc}
46 */
47 public function buildForm(FormBuilderInterface $builder, array $options): void {
48 //Set captcha
49 $captcha = $this->image->getCaptcha((new \DateTime('-1 year'))->getTimestamp());
50
51 //Add captcha token
52 $builder->add('_captcha_token', HiddenType::class, ['data' => $captcha['token'], 'empty_data' => $captcha['token']]);
53
54 //Add captcha
55 $builder->add('captcha', IntegerType::class, ['label_attr' => ['class' => 'captcha'], 'label' => '<img src="'.htmlentities($captcha['src']).'" alt="'.htmlentities($captcha['equation']).'" />', 'label_html' => true, 'translation_domain' => false]);
56
57 //Add event listener on captcha
58 $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'validateCaptcha']);
59 }
60
61 /**
62 * Validate captcha
63 *
64 * @param FormEvent $event The form event
65 */
66 public function validateCaptcha(FormEvent $event): void {
67 //Get form
68 $form = $event->getForm();
69
70 //Get event data
71 $data = $event->getData();
72
73 //Set token
74 $token = $form->get('_captcha_token')->getConfig()->getData();
75
76 //Without captcha
77 if (empty($data['captcha'])) {
78 //Add error on captcha
79 $form->addError(new FormError($this->translator->trans('Captcha is empty')));
80
81 //Reset captcha token
82 $data['_captcha_token'] = $token;
83
84 //Set event data
85 $event->setData($data);
86 //With invalid captcha
87 } elseif ($this->slugger->hash($data['captcha']) !== $data['_captcha_token']) {
88 //Add error on captcha
89 $form->addError(new FormError($this->translator->trans('Captcha is invalid')));
90
91 //Reset captcha token
92 $data['_captcha_token'] = $token;
93
94 //Set event data
95 $event->setData($data);
96 }
97 }
98 }