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\Provider; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
16
|
|
|
|
17
|
|
|
final class SessionTimeProvider implements TimeProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Session |
21
|
|
|
*/ |
22
|
|
|
private $session; |
23
|
|
|
|
24
|
|
|
public function __construct(Session $session) |
25
|
|
|
{ |
26
|
|
|
$this->session = $session; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function createFormProtection(string $name): void |
30
|
|
|
{ |
31
|
|
|
$startTime = new DateTime(); |
32
|
|
|
$key = $this->getSessionKey($name); |
33
|
|
|
$this->session->set($key, $startTime); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function isValid(string $name, array $options): bool |
37
|
|
|
{ |
38
|
|
|
$startTime = $this->getFormTime($name); |
39
|
|
|
|
40
|
|
|
if (null === $startTime) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$currentTime = new DateTime(); |
45
|
|
|
|
46
|
|
|
if (\array_key_exists('min', $options) && null !== $options['min']) { |
47
|
|
|
$minTime = clone $startTime; |
48
|
|
|
$minTime->modify(sprintf('+%d seconds', $options['min'])); |
49
|
|
|
|
50
|
|
|
if ($minTime > $currentTime) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (\array_key_exists('max', $options) && null !== $options['max']) { |
56
|
|
|
$maxTime = clone $startTime; |
57
|
|
|
$maxTime->modify(sprintf('+%d seconds', $options['max'])); |
58
|
|
|
|
59
|
|
|
if ($maxTime < $currentTime) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function removeFormProtection(string $name): void |
68
|
|
|
{ |
69
|
|
|
$key = $this->getSessionKey($name); |
70
|
|
|
$this->session->remove($key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if a form has a time protection. |
75
|
|
|
*/ |
76
|
|
|
private function hasFormProtection(string $name): bool |
77
|
|
|
{ |
78
|
|
|
$key = $this->getSessionKey($name); |
79
|
|
|
|
80
|
|
|
return $this->session->has($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the form time for specified form. |
85
|
|
|
* |
86
|
|
|
* @param string $name Name of form to get |
87
|
|
|
*/ |
88
|
|
|
private function getFormTime(string $name): ?DateTime |
89
|
|
|
{ |
90
|
|
|
$key = $this->getSessionKey($name); |
91
|
|
|
|
92
|
|
|
if ($this->hasFormProtection($name)) { |
93
|
|
|
return $this->session->get($key); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getSessionKey(string $name): string |
100
|
|
|
{ |
101
|
|
|
return 'antispam_'.$name; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|