-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtector.php
More file actions
361 lines (316 loc) · 8.94 KB
/
Protector.php
File metadata and controls
361 lines (316 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
namespace CaptchaProtector;
use Gregwar\Captcha\CaptchaBuilder;
/**
* Class Protector
* @package CaptchaProtector
*/
class Protector
{
/** @var string */
protected $storageDir;
/** @var bool */
protected $needCaptcha = false;
/** @var bool */
protected $captchaShown = false;
/** @var int */
protected $requestLimit = 0;
/** @var int */
protected $timeLimit;
/** @var int */
protected $requestCount = 0;
/** @var CaptchaBuilder */
protected $captchaBuilder;
/** @var string */
public $captchaCookieName = 'captcha_protector';
/** @var int */
public $gcFrequency = 10;
/** @var string */
protected $request;
/** @var int */
public $clientInfoExpires = 3600;
/** @var int */
public $captchaInfoExpires = 3600;
/**
* @param string $storageDir
* @param int $requestLimit
* @param int|null $timeLimit
*/
public function __construct($storageDir = '/tmp', $requestLimit = 3, $timeLimit = null)
{
$this->storageDir = $storageDir .'/captcha_protector';
$this->requestLimit = $requestLimit;
$this->timeLimit = $timeLimit;
}
/**
* @param null|string $request
* @return $this
*/
public function protect($request = null)
{
if($request === null) {
$uri = $_SERVER['REQUEST_METHOD'] . '/ ' . $_SERVER['REQUEST_URI'];
$request = explode('?', $uri)[0];
}
if(!$request) {
throw new \RuntimeException('Unable to protect empty request: '. var_export($request, true));
}
$this->request = $request;
$clientIp = $this->getClientIp();
$info = $this->processClientInfo($this->getClientInfo($clientIp));
$this->setClientInfo($clientIp, $info);
$this->needCaptcha = $this->requestCount > $this->requestLimit;
if($this->needCaptcha) {
$this->captchaShown = $this->requestCount - $this->requestLimit === 1 ? false : true;
}
return $this;
}
/**
* @param $info
* @return mixed
*/
protected function processClientInfo($info)
{
if($this->timeLimit !== null) {
if(!isset($info[$this->request])) {
$info[$this->request] = [
time()
];
}
else {
$info[$this->request][] = time();
}
$actualRequests = [];
$actualTime = time() - $this->timeLimit;
foreach($info[$this->request] as $time) {
if($time > $actualTime) {
$this->requestCount++;
$actualRequests[] = $time;
}
}
$info[$this->request] = $actualRequests;
}
else {
if(!isset($info[$this->request])) {
$info[$this->request] = 1;
}
else {
$info[$this->request] += 1;
}
$this->requestCount = $info[$this->request];
}
return $info;
}
/**
* @return int
*/
public function getRequestCount()
{
return $this->requestCount;
}
/**
* @param null|string $request
* @return $this
*/
public function forgive($request = null)
{
if($request === null) {
$uri = $_SERVER['REQUEST_METHOD'] . '/ ' . $_SERVER['REQUEST_URI'];
$request = explode('?', $uri)[0];
}
if(!$request) {
throw new \RuntimeException('Unable to protect empty request: '. var_export($request, true));
}
$this->request = $request;
$clientIp = $this->getClientIp();
$info = $this->getClientInfo($clientIp);
if(isset($info[$request])) {
$info[$request] = $this->timeLimit === null ? 0 : [];
$this->setClientInfo($clientIp, $info);
}
$this->needCaptcha = false;
return $this;
}
/**
* @return boolean
*/
public function isNeedCaptcha()
{
return $this->needCaptcha;
}
/**
* @param $ip
* @return array
*/
public function getClientInfo($ip)
{
$file = $this->getClientFilename($ip);
if(!file_exists($file)) {
if(!is_dir(dirname($file))) {
if(!mkdir(dirname($file), 0777, true)) {
throw new \RuntimeException('Unable to create storage dir: '. $this->storageDir);
}
}
file_put_contents($file, json_encode([]));
}
$info = file_get_contents($file);
$json = json_decode($info, true);
return $json ? $json : [];
}
/**
* @param $ip
* @param array $info
* @return int
*/
public function setClientInfo($ip, $info = [])
{
if(mt_rand(0, 100) < $this->gcFrequency) {
$this->runGC();
}
return file_put_contents($this->getClientFilename($ip), json_encode($info));
}
/**
* @return int
*/
public function runGC()
{
$ipDir = $this->storageDir;
$time = time() - $this->clientInfoExpires;
$c = $this->clearUnused($ipDir, $time);
$captchaDir = $this->storageDir .'/captcha';
$time = time() - $this->captchaInfoExpires;
$c += $this->clearUnused($captchaDir, $time);
return $c;
}
/**
* @param $dir
* @param $modifiedLaterThan
* @return int
*/
protected function clearUnused($dir, $modifiedLaterThan)
{
$c = 0;
if(is_dir($dir)) {
$dh = opendir($dir);
while ($f = readdir($dh)) {
$file = $dir .'/'. $f;
if(is_file($file) && (filemtime($file) < $modifiedLaterThan)) {
unlink($file);
$c++;
}
}
}
return $c;
}
/**
* @param $ip
* @return string
*/
public function getClientFilename($ip)
{
return $this->storageDir .'/'. $ip .'.json';
}
/**
* @return string
*/
public function getClientIp()
{
$client = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : '';
$forward = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $forward;
}
else {
$ip = $remote;
}
return $ip;
}
/**
* @return CaptchaBuilder
*/
protected function getCaptchaBuilder()
{
if($this->request === null) {
throw new \RuntimeException('Unable to start captcha protecting without calling protect method');
}
if($this->captchaBuilder === null) {
$this->captchaBuilder = new CaptchaBuilder();
}
return $this->captchaBuilder;
}
/**
* @param $width
* @param $height
* @param null $phrase
* @param int $quality
* @return string
*/
public function getCaptcha($width, $height, $phrase = null, $quality = 90)
{
if($phrase) {
$this->getCaptchaBuilder()->setPhrase($phrase);
}
$content = $this->getCaptchaBuilder()->build($width, $height)->inline($quality);
$this->saveCaptchaRequest();
return $content;
}
/**
* @return int
*/
protected function saveCaptchaRequest()
{
$file = $this->getCaptchaRequestFile($this->getCaptchaRequestUid());
if(!is_dir(dirname($file))) {
mkdir(dirname($file), 0777, true);
}
return file_put_contents($file, $this->getCaptchaBuilder()->getPhrase());
}
/**
* @return string
*/
protected function getCaptchaRequestUid()
{
if(!isset($_COOKIE[$this->captchaCookieName]) || !$_COOKIE[$this->captchaCookieName]) {
$requestUid = md5(mt_rand(10000, 999999) .'_'. time());
setcookie($this->captchaCookieName, $requestUid, time() + 3600, '/');
}
else {
$requestUid = $_COOKIE[$this->captchaCookieName];
}
return $requestUid;
}
/**
* @param $requestId
* @return string
*/
public function getCaptchaRequestFile($requestId)
{
return $this->storageDir .'/captcha/'. $requestId;
}
/**
* @param $userInput
* @return bool
*/
public function isCaptchaResolved($userInput)
{
$file = $this->getCaptchaRequestFile($this->getCaptchaRequestUid());
if(file_exists($file) && trim($userInput)) {
if ($userInput == file_get_contents($file)) {
unlink($file);
return true;
}
}
return false;
}
/**
* @return boolean
*/
public function isCaptchaShown()
{
return $this->captchaShown;
}
}