|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) Christian Gripp <[email protected]> |
|
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 Core23\AntiSpamBundle\Form\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Core23\AntiSpamBundle\Form\EventListener\AntiSpamHoneypotListener; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
|
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
19
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
21
|
|
|
use Symfony\Component\Form\FormInterface; |
|
22
|
|
|
use Symfony\Component\Form\FormView; |
|
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
24
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class HoneypotFormExtension extends AbstractTypeExtension |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var TranslatorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $translator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array<string, mixed> |
|
35
|
|
|
*/ |
|
36
|
|
|
private $defaults; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(TranslatorInterface $translator, array $defaults) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->translator = $translator; |
|
41
|
|
|
$this->defaults = $defaults; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
|
45
|
|
|
{ |
|
46
|
|
|
if (!$options['antispam_honeypot']) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$builder |
|
51
|
|
|
->setAttribute('antispam_honeypot_factory', $builder->getFormFactory()) |
|
52
|
|
|
->addEventSubscriber(new AntiSpamHoneypotListener($this->translator, $options['antispam_honeypot_field'])) |
|
53
|
|
|
; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function finishView(FormView $view, FormInterface $form, array $options): void |
|
57
|
|
|
{ |
|
58
|
|
|
if ($view->parent || !$options['antispam_honeypot'] || !$options['compound']) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($form->has($options['antispam_honeypot_field'])) { |
|
63
|
|
|
throw new RuntimeException(sprintf('Honeypot field "%s" is already used.', $options['antispam_honeypot_field'])); |
|
64
|
|
|
} |
|
65
|
|
|
$factory = $form->getConfig()->getAttribute('antispam_honeypot_factory'); |
|
66
|
|
|
|
|
67
|
|
|
if (!$factory instanceof FormFactoryInterface) { |
|
68
|
|
|
throw new RuntimeException('Invalid form factory to create a honeyput.'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$formOptions = $this->createViewOptions($options); |
|
72
|
|
|
|
|
73
|
|
|
$formView = $factory |
|
74
|
|
|
->createNamed($options['antispam_honeypot_field'], TextType::class, null, $formOptions) |
|
75
|
|
|
->createView($view) |
|
76
|
|
|
; |
|
77
|
|
|
|
|
78
|
|
|
$view->children[$options['antispam_honeypot_field']] = $formView; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
|
82
|
|
|
{ |
|
83
|
|
|
$resolver |
|
84
|
|
|
->setDefaults([ |
|
85
|
|
|
'antispam_honeypot' => $this->defaults['global'], |
|
86
|
|
|
'antispam_honeypot_class' => $this->defaults['class'], |
|
87
|
|
|
'antispam_honeypot_field' => $this->defaults['field'], |
|
88
|
|
|
]) |
|
89
|
|
|
->setAllowedTypes('antispam_honeypot', 'bool') |
|
90
|
|
|
->setAllowedTypes('antispam_honeypot_class', ['string', 'null']) |
|
91
|
|
|
->setAllowedTypes('antispam_honeypot_field', 'string') |
|
92
|
|
|
; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getExtendedType() |
|
96
|
|
|
{ |
|
97
|
|
|
foreach (static::getExtendedTypes() as $extendedType) { |
|
98
|
|
|
return $extendedType; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public static function getExtendedTypes(): iterable |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
FormType::class, |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function createViewOptions(array $options): array |
|
110
|
|
|
{ |
|
111
|
|
|
$formOptions = [ |
|
112
|
|
|
'mapped' => false, |
|
113
|
|
|
'label' => false, |
|
114
|
|
|
'required' => false, |
|
115
|
|
|
]; |
|
116
|
|
|
|
|
117
|
|
|
if (!\array_key_exists('antispam_honeypot_class', $options) || null === $options['antispam_honeypot_class']) { |
|
118
|
|
|
$formOptions['attr'] = [ |
|
119
|
|
|
'style' => 'display:none', |
|
120
|
|
|
]; |
|
121
|
|
|
} else { |
|
122
|
|
|
$formOptions['attr'] = [ |
|
123
|
|
|
'class' => $options['antispam_honeypot_class'], |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $formOptions; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|