1 | <?php |
||
12 | class ProtectAgainstSpam |
||
13 | { |
||
14 | /** @var \Spatie\Honeypot\SpamResponder\SpamResponder */ |
||
15 | protected $spamResponder; |
||
16 | |||
17 | public function __construct(SpamResponder $spamResponder) |
||
21 | |||
22 | public function handle(Request $request, Closure $next): Response |
||
23 | { |
||
24 | if (! config('honeypot.enabled')) { |
||
25 | return $next($request); |
||
26 | } |
||
27 | |||
28 | if (! $request->isMethod('POST')) { |
||
29 | return $next($request); |
||
30 | } |
||
31 | |||
32 | $nameFieldName = config('honeypot.name_field_name'); |
||
33 | |||
34 | if (config('honeypot.randomize_name_field_name')) { |
||
35 | $nameFieldName = $this->getRandomizedNameFieldName($nameFieldName, $request->all()); |
||
36 | } |
||
37 | |||
38 | if (! $this->shouldCheckHoneypot($request, $nameFieldName)) { |
||
39 | return $next($request); |
||
40 | } |
||
41 | |||
42 | if (! $request->has($nameFieldName)) { |
||
43 | return $this->respondToSpam($request, $next); |
||
44 | } |
||
45 | |||
46 | $honeypotValue = $request->get($nameFieldName); |
||
47 | |||
48 | if (! empty($honeypotValue)) { |
||
49 | return $this->respondToSpam($request, $next); |
||
50 | } |
||
51 | |||
52 | $validFrom = $request->get(config('honeypot.valid_from_field_name')); |
||
53 | |||
54 | if (! $validFrom) { |
||
55 | return $this->respondToSpam($request, $next); |
||
56 | } |
||
57 | |||
58 | try { |
||
59 | $time = new EncryptedTime($validFrom); |
||
60 | } catch (Exception $decryptException) { |
||
61 | $time = null; |
||
62 | } |
||
63 | |||
64 | if (! $time || $time->isFuture()) { |
||
65 | return $this->respondToSpam($request, $next); |
||
66 | } |
||
67 | |||
68 | return $next($request); |
||
69 | } |
||
70 | |||
71 | private function getRandomizedNameFieldName($nameFieldName, $requestFields): ?String |
||
77 | |||
78 | protected function respondToSpam(Request $request, Closure $next): Response |
||
84 | |||
85 | private function shouldCheckHoneypot(Request $request, ?string $nameFieldName): bool |
||
93 | } |
||
94 |