forked from Looky1173/Scratch-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle-users.php
170 lines (154 loc) · 7.74 KB
/
handle-users.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
session_start();
require_once ("includes/autoloader.inc.php");
$request = $_POST["request"];
$response = array("status" => "unprepared");
if($request == "register"){
$ok = true;
$username = $_POST["username"];
$password = $_POST["password"];
$password_repeat = $_POST["password_repeat"];
// Validate password strength
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChars = preg_match('@[^\w]@', $password);
//Check if a username was given
if(!empty($username)){
$handleUser = new HandleUsers;
if($handleUser->checkUsernameAvailability($username) == false){
//Username avaible, continue
if($ok == true){
$authentication = new AuthenticateWithScratch;
$username_check = $authentication->checkValidScratchAccount($username);
if($username_check == "valid username"){
$response = array("status" => "error", "type" => "username-not-registered", "message" => "The provided username is not yet registered on Scratch.");
$ok = false;
}
if($username_check == "invalid username"){
$response = array("status" => "error", "type" => "username-invalid", "message" => "The provided username is invalid and not registered on Scratch.");
$ok = false;
}
if($username_check == "username exists"){
$ok = true;
}
if(!empty($password) && empty($password_repeat) || empty($password) && !empty($password_repeat)){
$response = array("status" => "error", "type" => "password-missing", "message" => "You must fill in both password fields if you have chosen to fill in one!");
$ok = false;
}
if(!empty($password) && !empty($password_repeat)){
if($password != $password_repeat){
$response = array("status" => "error", "type" => "password-mismatch", "message" => "The entered passwords do not match!");
$ok = false;
}elseif(!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8){
$response = array("status" => "error", "type" => "weak-password", "message" => "Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character.");
$ok = false;
}
}
if($ok == true){
//Everything fine, continue
$code = $authentication->generateAuthenticationCode(32);
$response = array("status" => "success", "message" => $code, "username_check" => $username_check, "username" => $username, "password" => $password, "password-repeat" => $password_repeat, "request" => $request);
}
}
}else{
//Username taken, throw error
$response = array("status" => "error", "type" => "username-taken", "message" => "Sorry, this username is not avaible. Please try another!");
$ok = false;
}
}else{
//Missing username, throw error
$response = array("status" => "error", "type" => "username-missing", "message" => "You must enter a username!");
$ok = false;
}
//Encode JSON response
echo json_encode($response);
}elseif($request == "verification"){
$username = $_POST["username"];
$verification_code = $_POST["verification_code"];
$authentication = new AuthenticateWithScratch;
$return = $authentication->authenticate($username, $verification_code);
$response = array("username" => $username, "verification_code" => $verification_code, "return" => $return);
echo json_encode($response);
}elseif($request == "register-final"){
$register = new HandleUsers;
$username = $_POST["username"];
$password = $_POST["password"];
$permission = 0;
date_default_timezone_set("UTC");
$created = date('Y-m-d H:i:s');
$modified = date('Y-m-d H:i:s');
$status = 'normal';
//Register
if($register->register($username, $password, $permission, $created, $modified, $status) == true){
if(!empty($password)){
//Login automatically
if($register->login($username, $password) == true){
//Successful login
echo json_encode(["success" => "true"]);
}else{
//Login failed
echo json_encode(["success" => "false", "message" => "Failed to log in user."]);
}
}else{
//No password was entered, login manually
echo json_encode($register->loginWithoutPassword($username));
}
}else{
echo json_encode(["success" => "false", "message" => "Failed to create user."]);
}
}elseif($request == "login"){
$username = $_POST['username'];
$password = $_POST['password'];
// Validate password strength
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChars = preg_match('@[^\w]@', $password);
$verification_code;
$login = new HandleUsers;
if($login->checkUsernameAvailability($username) == true){
$loginReturn = $login->login($username, $password);
if((!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8) && !empty($password)){
if($loginReturn == "scratch-login"){
$response = array("status" => "error", "type" => "user-without-password", "message" => "This account does not have a password associated with it.");
}else{
$response = array("status" => "error", "type" => "weak-password", "message" => "Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character.");
}
}else{
if($loginReturn === true){
$response = array("status" => "success", "type" => "login", "message" => "Logged in.", "request" => $request, "loginReturn" => $loginReturn);
}elseif($loginReturn == "scratch-login"){
$scratch_login = new AuthenticateWithScratch;
$verification_code = $scratch_login->generateAuthenticationCode(32);
$response = array("status" => "success", "type" => "scratch-login", "message" => $verification_code, "loginReturn" => $loginReturn);
}else{
$response = array("status" => "error", "type" => "login-fail", "message" => "Failed to login in. Please double check your credentials and try again!", "loginReturn" => $loginReturn);
}
}
}else{
$response = array("status" => "error", "type" => "not-registered", "message" => "This username is not yet registered on Scratch Tools.");
}
echo json_encode($response);
}elseif($request == "scratch-login"){
$username = $_POST['username'];
$login = new HandleUsers;
echo json_encode($login->loginWithoutPassword($username));
}elseif($request == "logout"){
$logout = new HandleUsers;
if($logout->logout() == true){
$response = array("status" => "success");
}else{
$response = array("status" => "error");
}
echo json_encode($response);
}elseif($request == "delete-account"){
$delete = new HandleUsers;
if($delete->deleteUser($_SESSION['id']) == true){
$response = array("status" => "success");
}else{
$response = array("status" => "error");
}
echo json_encode($response);
}
?>