-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_login.php
107 lines (85 loc) · 2.54 KB
/
manage_login.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
<?php
function isValidCookie($strCookie) {
$verifyHash = NULL;
if(!isset($strCookie->sessid) || !isset($strCookie->logind) || !isset($strCookie->user)) {
return false;
}
// Find password hash
$userAdmin = json_decode(file_get_contents("json/users_admin.json"));
foreach($userAdmin as $eachUser) {
if($eachUser->name == $strCookie->user) {
$verifyHash = $strCookie->user . $strCookie->logind . $eachUser->pass;
break;
}
}
if(is_null($verifyHash)) {
return false;
}
return hash("sha256", $verifyHash) == $strCookie->sessid;
}
function manageSession() {
switch($_POST["t"]) {
case "w":
// Verify valid session cookie
$userSessData = json_decode($_POST["s"]);
if(isValidCookie($userSessData)) {
// Write to session database
$sessLogs = json_decode(file_get_contents("json/sessions.json"));
$sessLogs[] = $userSessData;
$fh = fopen("json/sessions.json", "w");
fwrite($fh, json_encode($sessLogs, JSON_PRETTY_PRINT));
fclose($fh);
echo '{"sessid": "'.$userSessData->sessid.'"}';
}
else {
// Invalid session cookie
header("HTTP/1.1 403 Forbidden");
echo '{"error": "Invalid session cookie"}';
}
break;
case "r":
// Remove to main session database
$sessId = $_POST["sessid"];
$sessLogs = json_decode(file_get_contents("json/sessions.json"));
$sessKey = NULL;
foreach($sessLogs as $k => $eachLog) {
if($sessId == $eachLog->sessid) {
$sessKey = $k;
break;
}
}
if(!is_null($sessKey)) {
// Found session ID!
$sessVal = array_splice($sessLogs, $sessKey, 1)[0];
}
$fh = fopen("json/sessions.json", "w+");
fwrite($fh, json_encode($sessLogs, JSON_PRETTY_PRINT));
fclose($fh);
// Set/unset some variables
unset($sessVal->sessid);
$sessVal->logoutd = gmdate("D, d M Y H:i:s e");
// Move removed data to user logs
$userLogs = json_decode(file_get_contents("json/userlog.json"));
$userLogs[] = $sessVal;
$fh = fopen("json/userlog.json", "w");
fwrite($fh, json_encode($userLogs, JSON_PRETTY_PRINT));
fclose($fh);
break;
}
}
function receiveAjax() {
// Set JSON header
header("Content-Type: application/json; charset=UTF-8");
if(!isset($_POST["f"]) || !isset($_POST["t"])) {
header("HTTP/1.1 403 Forbidden");
echo '{"error": "Missing required arguments."}';
return;
}
switch($_POST["f"]) {
case "sessions":
manageSession();
break;
}
}
receiveAjax();
?>