forked from Glennmen/PMSF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.php
61 lines (53 loc) · 1.22 KB
/
utils.php
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
<?php
$localeData = null;
function i8ln($word)
{
global $locale;
if ($locale == "en") {
return $word;
}
global $localeData;
if ($localeData == null) {
$filepath = 'static/dist/locales/' . $locale . '.min.json';
if (file_exists($filepath)) {
$json_contents = file_get_contents($filepath);
$localeData = json_decode($json_contents, true);
} else {
return $word;
}
}
if (isset($localeData[$word])) {
return $localeData[$word];
} else {
return $word;
}
}
function setSessionCsrfToken()
{
if (empty($_SESSION['token'])) {
generateToken();
}
}
function refreshCsrfToken()
{
global $sessionLifetime;
if (time() - $_SESSION['c'] > $sessionLifetime) {
session_regenerate_id(true);
generateToken();
}
return $_SESSION['token'];
}
function generateToken()
{
$_SESSION['token'] = base64_encode(openssl_random_pseudo_bytes(32));
$_SESSION['c'] = time();
}
function validateToken($token)
{
global $enableCsrf;
if ((!$enableCsrf) || ($enableCsrf && isset($token) && $token === $_SESSION['token'])) {
return true;
} else {
return false;
}
}