|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CCDNForum ForumBundle |
|
5
|
|
|
* |
|
6
|
|
|
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* Available on github <http://www.github.com/codeconsortium/> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace CCDNForum\ForumBundle\Component; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Security\Core\SecurityContextInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @category CCDNForum |
|
22
|
|
|
* @package ForumBundle |
|
23
|
|
|
* |
|
24
|
|
|
* @author Reece Fowell <[email protected]> |
|
25
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
26
|
|
|
* @version Release: 2.0 |
|
27
|
|
|
* @link https://github.com/codeconsortium/CCDNForumForumBundle |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
class FloodControl |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @access protected |
|
35
|
|
|
* @var \Symfony\Component\Security\Core\SecurityContextInterface $securityContext |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $securityContext; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @access protected |
|
42
|
|
|
* @var \Symfony\Component\HttpFoundation\Session\Session $session |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $session; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* @access protected |
|
49
|
|
|
* @var string $kernelEnv |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $kernelEnv; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* @access protected |
|
56
|
|
|
* @var int $postLimit |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $postLimit; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* |
|
62
|
|
|
* @access protected |
|
63
|
|
|
* @var int $blockTimeInMinutes |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $blockTimeInMinutes; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @access public |
|
70
|
|
|
* @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext |
|
71
|
|
|
* @param \Symfony\Component\HttpFoundation\Session\Session $session |
|
72
|
|
|
* @param string $kernelEnv |
|
73
|
|
|
* @param int $postLimit |
|
74
|
|
|
* @param int $blockTimeInMinutes |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(SecurityContextInterface $securityContext, Session $session, $kernelEnv, $postLimit, $blockTimeInMinutes) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->securityContext = $securityContext; |
|
79
|
|
|
$this->session = $session; |
|
80
|
|
|
$this->kernelEnv = $kernelEnv; |
|
81
|
|
|
|
|
82
|
|
|
if (! $this->session->has('flood_control_forum_post_count')) { |
|
83
|
|
|
$this->session->set('flood_control_forum_post_count', array()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->postLimit = $postLimit; |
|
87
|
|
|
$this->blockTimeInMinutes = $blockTimeInMinutes; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* |
|
92
|
|
|
* @access public |
|
93
|
|
|
*/ |
|
94
|
|
|
public function incrementCounter() |
|
95
|
|
|
{ |
|
96
|
|
|
if (! $this->securityContext->isGranted('ROLE_MODERATOR') || $this->kernelEnv != 'prod') { |
|
97
|
|
|
$postCount = $this->session->get('flood_control_forum_post_count'); |
|
98
|
|
|
|
|
99
|
|
|
$postCount[] = new \DateTime('now'); |
|
100
|
|
|
|
|
101
|
|
|
$this->session->set('flood_control_forum_post_count', $postCount); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* |
|
107
|
|
|
* @access public |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function isFlooded() |
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->postLimit < 1 || ! $this->securityContext->isGranted('ROLE_MODERATOR') || $this->kernelEnv != 'prod') { |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if ($this->session->has('flood_control_forum_post_count')) { |
|
117
|
|
|
$attempts = $this->session->get('flood_control_forum_post_count'); |
|
118
|
|
|
|
|
119
|
|
|
// Iterate over attempts and only reveal attempts that fall within the $timeLimit. |
|
120
|
|
|
$freshenedAttempts = array(); |
|
121
|
|
|
|
|
122
|
|
|
$timeLimit = new \DateTime('-' . $this->blockTimeInMinutes . ' minutes'); |
|
123
|
|
|
$limit = $timeLimit->getTimestamp(); |
|
124
|
|
|
|
|
125
|
|
|
foreach ($attempts as $attempt) { |
|
126
|
|
|
$date = $attempt->getTimestamp(); |
|
127
|
|
|
|
|
128
|
|
|
if ($date > $limit) { |
|
129
|
|
|
$freshenedAttempts[] = $attempt; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (count($freshenedAttempts) > $this->postLimit) { |
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|