-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.php
110 lines (93 loc) · 2.59 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
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
<?php
$localeData = null;
function i8ln($word)
{
global $locale;
$locale = !empty($_COOKIE["LocaleCookie"]) ? $_COOKIE["LocaleCookie"] : $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;
}
}
function destroyCookiesAndSessions()
{
global $manualdb;
$manualdb->update("users", ["session_id" => null], ["id" => $_SESSION['user']->id]);
unset($_SESSION);
unset($_COOKIE['LoginCookie']);
setcookie("LoginCookie", "", time() - 3600);
session_destroy();
session_write_close();
}
function validateCookie($cookie)
{
global $manualdb;
$info = $manualdb->query(
"SELECT id, user, access_level FROM users WHERE session_id = :session_id", [
":session_id" => $cookie
]
)->fetch();
if (!empty($info['user'])) {
$_SESSION['user'] = new \stdClass();
$_SESSION['user']->id = $info['id'];
$_SESSION['user']->user = htmlspecialchars($info['user'], ENT_QUOTES, 'UTF-8');
$_SESSION['user']->access_level = $info['access_level'];
setcookie("LoginCookie", $cookie, time() + 60 * 60 * 24 * 7);
if (!isset($_SESSION['already_refreshed'])) {
$_SESSION['already_refreshed'] = true;
return false;
} else {
return true;
}
} elseif (!empty($_SESSION['user'])) {
destroyCookiesAndSessions();
return false;
} else {
unset($_COOKIE['LoginCookie']);
setcookie("LoginCookie", "", time() - 3600);
return false;
}
}