forked from imabutahersiddik/CodeStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.php
More file actions
34 lines (28 loc) · 1.08 KB
/
security.php
File metadata and controls
34 lines (28 loc) · 1.08 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
<?php
class Security {
public static function sanitizeInput($input) {
if (is_array($input)) {
return array_map([self::class, 'sanitizeInput'], $input);
}
return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
}
public static function generateCSRFToken() {
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
public static function validateCSRFToken($token) {
return isset($_SESSION['csrf_token']) && hash_equals($_SESSION['csrf_token'], $token);
}
public static function rateLimit($key, $maxAttempts = 60, $decayMinutes = 1) {
$attempts = isset($_SESSION[$key]) ? $_SESSION[$key] : ['count' => 0, 'time' => time()];
if (time() - $attempts['time'] > $decayMinutes * 60) {
$attempts = ['count' => 0, 'time' => time()];
}
$attempts['count']++;
$_SESSION[$key] = $attempts;
return $attempts['count'] <= $maxAttempts;
}
}
?>