@@ -36,6 +36,8 @@ public function __construct(Cache $cache)
36
36
*/
37
37
public function tooManyAttempts ($ key , $ maxAttempts )
38
38
{
39
+ $ key = $ this ->cleanRateLimiterKey ($ key );
40
+
39
41
if ($ this ->attempts ($ key ) >= $ maxAttempts ) {
40
42
if ($ this ->cache ->has ($ key .':timer ' )) {
41
43
return true ;
@@ -56,6 +58,8 @@ public function tooManyAttempts($key, $maxAttempts)
56
58
*/
57
59
public function hit ($ key , $ decaySeconds = 60 )
58
60
{
61
+ $ key = $ this ->cleanRateLimiterKey ($ key );
62
+
59
63
$ this ->cache ->add (
60
64
$ key .':timer ' , $ this ->availableAt ($ decaySeconds ), $ decaySeconds
61
65
);
@@ -79,6 +83,8 @@ public function hit($key, $decaySeconds = 60)
79
83
*/
80
84
public function attempts ($ key )
81
85
{
86
+ $ key = $ this ->cleanRateLimiterKey ($ key );
87
+
82
88
return $ this ->cache ->get ($ key , 0 );
83
89
}
84
90
@@ -90,6 +96,8 @@ public function attempts($key)
90
96
*/
91
97
public function resetAttempts ($ key )
92
98
{
99
+ $ key = $ this ->cleanRateLimiterKey ($ key );
100
+
93
101
return $ this ->cache ->forget ($ key );
94
102
}
95
103
@@ -102,6 +110,8 @@ public function resetAttempts($key)
102
110
*/
103
111
public function retriesLeft ($ key , $ maxAttempts )
104
112
{
113
+ $ key = $ this ->cleanRateLimiterKey ($ key );
114
+
105
115
$ attempts = $ this ->attempts ($ key );
106
116
107
117
return $ maxAttempts - $ attempts ;
@@ -115,6 +125,8 @@ public function retriesLeft($key, $maxAttempts)
115
125
*/
116
126
public function clear ($ key )
117
127
{
128
+ $ key = $ this ->cleanRateLimiterKey ($ key );
129
+
118
130
$ this ->resetAttempts ($ key );
119
131
120
132
$ this ->cache ->forget ($ key .':timer ' );
@@ -128,6 +140,19 @@ public function clear($key)
128
140
*/
129
141
public function availableIn ($ key )
130
142
{
143
+ $ key = $ this ->cleanRateLimiterKey ($ key );
144
+
131
145
return $ this ->cache ->get ($ key .':timer ' ) - $ this ->currentTime ();
132
146
}
147
+
148
+ /**
149
+ * Clean the rate limiter key from unicode characters.
150
+ *
151
+ * @param string $key
152
+ * @return string
153
+ */
154
+ public function cleanRateLimiterKey ($ key )
155
+ {
156
+ return preg_replace ('/&([a-z])[a-z]+;/i ' , '$1 ' , htmlentities ($ key ));
157
+ }
133
158
}
0 commit comments