-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofileprocess.php
More file actions
157 lines (134 loc) · 4.97 KB
/
Copy pathprofileprocess.php
File metadata and controls
157 lines (134 loc) · 4.97 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
<?php
$return = true;
require __DIR__ . '/configuration.php';
require __DIR__ . '/include.php';
requireSameOrigin('index.php');
require __DIR__ . '/includes/totp.php';
$task = sanitizeInput($_POST["task"] ?? $_GET["task"] ?? "");
$clientId = (int) ($_SESSION["clientid"] ?? 0);
if (!$clientId) {
header("Location: login.php");
exit;
}
if ($task === "2fa_enable") {
$secret = (string) ($_SESSION["2fa_setup"] ?? "");
$code = sanitizeInput($_POST["totpcode"] ?? "");
if ($secret !== "" && totpVerify($secret, $code)) {
$plain = totpRecoveryCodes(8);
dbExec("UPDATE `client` SET `totp` = '" . dbEscape($secret) . "', `totp_recovery` = '" . dbEscape(totpRecoveryStore($plain)) . "' WHERE `clientid` = '" . $clientId . "'");
unset($_SESSION["2fa_setup"]);
$_SESSION["2fa_codes"] = $plain;
$_SESSION["msg1"] = "Two-factor enabled";
$_SESSION["msg2"] = "Save your recovery codes below — each works once if you lose your app.";
} else {
$_SESSION["msg1"] = "Could not enable two-factor";
$_SESSION["msg2"] = "That code did not match. Try again with the current code.";
}
header("Location: profile.php");
exit;
}
if ($task === "2fa_regen") {
$cur = dbRow("SELECT `totp` FROM `client` WHERE `clientid` = '" . $clientId . "' LIMIT 1", true);
if (is_array($cur) && !empty($cur["totp"]) && totpVerify((string) $cur["totp"], sanitizeInput($_POST["totpcode"] ?? ""))) {
$plain = totpRecoveryCodes(8);
dbExec("UPDATE `client` SET `totp_recovery` = '" . dbEscape(totpRecoveryStore($plain)) . "' WHERE `clientid` = '" . $clientId . "'");
$_SESSION["2fa_codes"] = $plain;
$_SESSION["msg1"] = "New recovery codes";
$_SESSION["msg2"] = "The old codes no longer work.";
} else {
$_SESSION["msg1"] = "Could not regenerate";
$_SESSION["msg2"] = "Enter a current authenticator code to confirm.";
}
header("Location: profile.php");
exit;
}
if ($task === "2fa_disable") {
$cur = dbRow("SELECT `totp` FROM `client` WHERE `clientid` = '" . $clientId . "' LIMIT 1", true);
$code = sanitizeInput($_POST["totpcode"] ?? "");
if (is_array($cur) && !empty($cur["totp"]) && totpVerify((string) $cur["totp"], $code)) {
dbExec("UPDATE `client` SET `totp` = '' WHERE `clientid` = '" . $clientId . "'");
$_SESSION["msg1"] = "Two-factor disabled";
$_SESSION["msg2"] = "Your account no longer requires a code to sign in.";
} else {
$_SESSION["msg1"] = "Could not disable two-factor";
$_SESSION["msg2"] = "Enter a current code from your authenticator app to confirm.";
}
header("Location: profile.php");
exit;
}
if ($task !== "profile") {
header("Location: index.php");
exit;
}
$clientid = $_SESSION["clientid"];
$firstname = ucfirst(sanitizeInput($_POST["firstname"]));
$lastname = ucfirst(sanitizeInput($_POST["lastname"]));
$email = strtolower(sanitizeInput($_POST["email"]));
$password = sanitizeInput($_POST["password"]);
unset($_SESSION["msg1"], $_SESSION["msg2"]);
$_SESSION["firstname"] = $firstname;
$_SESSION["lastname"] = $lastname;
$_SESSION["email"] = $email;
$_SESSION["password"] = $password;
if (strlen($firstname) === 0) {
$_SESSION["msg2"] = ($_SESSION["msg2"] ?? "") . "<li>First Name [ <b>Not Entered</b> ]</li>";
}
if (strlen($lastname) === 0) {
$_SESSION["msg2"] = ($_SESSION["msg2"] ?? "") . "<li>Last Name [ <b>Not Entered</b> ]</li>";
}
if (strlen($email) === 0) {
$_SESSION["msg2"] = ($_SESSION["msg2"] ?? "") . "<li>Email [ <b>Not Entered</b> ]</li>";
} elseif (strlen($email) <= 2) {
$_SESSION["msg2"] = ($_SESSION["msg2"] ?? "") . "<li>Email [ <b>Not Long Enough</b> ]</li>";
}
if (dbCount("SELECT clientid FROM client WHERE email='$email' AND clientid!='$clientid'") > 0) {
$_SESSION["msg2"] = ($_SESSION["msg2"] ?? "") . "<li>Email [ <b>Already Used</b> ]</li>";
}
if (strlen($password) > 0 && strlen($password) < 4) {
$_SESSION["msg2"] = ($_SESSION["msg2"] ?? "") . "<li>Password [ <b>Not Long Enough</b> ]</li>";
}
if (!empty($_SESSION["msg2"])) {
$_SESSION["msg1"] = "Validation Error!";
header("Location: profile.php");
exit;
}
unset(
$_SESSION["firstname"],
$_SESSION["lastname"],
$_SESSION["email"],
$_SESSION["password"]
);
if ($password === "") {
// Leave the password untouched when the field is left blank.
dbExec(
"UPDATE client
SET firstname='$firstname',
lastname='$lastname',
email='$email'
WHERE clientid='$clientid'"
);
} else {
$hashed = hashPassword($password);
dbExec(
"UPDATE client
SET firstname='$firstname',
lastname='$lastname',
email='$email',
password='$hashed'
WHERE clientid='$clientid'"
);
}
$message = "Client Edited: <a href=\"clientsummary.php?id=$clientid\">$firstname $lastname</a>";
$logName = dbEscape(($_SESSION["clientfirstname"] ?? "") . " " . ($_SESSION["clientlastname"] ?? ""));
$logIp = dbEscape($_SERVER["REMOTE_ADDR"] ?? "");
dbExec(
"INSERT INTO log
SET clientid='$clientid',
message='$message',
name='$logName',
ip='$logIp'"
);
$_SESSION["msg1"] = "Profile Updated Successfully!";
$_SESSION["msg2"] = "Your changes to your profile have been saved.";
header("Location: index.php");
exit;