-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
327 lines (258 loc) · 9.86 KB
/
server.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
if(isset($_POST["operazione"])) {
$op = $_POST["operazione"];
// json response array
$response = array("error" => false);
require_once "DB_Functions.php";
$db = new DB_Functions();
switch ($op) {
case "login":
login();
break;
case 'registrazione':
registrazione();
break;
case 'verifica':
verifica();
break;
case 'email_registrazione':
email_registrazione();
break;
case 'email_recupero':
email_recupero();
break;
case 'cambia_password':
cambia_password();
break;
default:
$response["error"] = true;
$response["error_msg"] = "Specificare il tipo di operazione che si vuole effettuare.";
break;
}
echo json_encode($response);
}
// Ritorna le informazione dell'utente se le credenziali sono corrette
function login(){
global $response;
// check if email and password was passed through POST method
if (isset($_POST['email']) && isset($_POST['password']))
{
// receiving the post params
$email = $_POST['email'];
$password = $_POST['password'];
// get the user by email and password
$user = getUserByEmailAndPassword($email, $password);
// check if user was found or not
if ($user != false) {
// user is found
$response["user"] = $user;
} else {
// user is not found with the credentials
$response["error"] = true;
$response["error_msg"] = "Le credenziali di accesso non sono corrette. Riprova.";
}
} else {
// required post params is missing
$response["error"] = true;
$response["error_msg"] = "I parametri email e password sono obbligatori.";
}
}
// Registra un nuovo utente date le credenziali
function registrazione(){
global $response;
if (isset($_POST["email"]) && isset($_POST["password"])) {
$email = $_POST["email"];
$password = $_POST["password"];
if (emailAlreadyUsed($email)) {
$response["error"] = true;
$response["error_msg"] = "L'email inserita è già in uso. Riprova.";
return;
}
$user = storeUser($email, $password);
if ($user) {
$response["user"] = $user;
$_POST["codice"] = $user["codice"];
$result = email_registrazione();
$response["email"] = $result;
} else {
$response["error"] = true;
$response["error_msg"] = "Si è verificato un errore imprevisto durante la registrazione del nuovo utente.";
}
}
else
{
$response["error"] = true;
$response["error_msg"] = "I parametri richiesti sono obbligatori.";
}
}
function verifica() {
global $response;
if (isset($_POST['email']) && isset($_POST['codice']))
{
$email = $_POST["email"];
$codice = $_POST["codice"];
$result = accountVerification($email, $codice);
if ($result == false) {
$response["error"] = true;
$response["error_msg"] = "Questo account non può essere verificato.";
}
} else {
// required post params is missing
$response["error"] = true;
$response["error_msg"] = "I parametri richiesti sono obbligatori.";
}
}
function email_registrazione() {
global $response;
if(isset($_POST["email"]) && isset($_POST["codice"])) {
require_once "Helper_Functions.php";
$helper = new Helper_Functions();
$email = $_POST["email"];
$codice = $_POST["codice"];
// Mail it
$result = $helper->sendEmail($email, $codice);
if (!$result) {
$response["error"] = true;
$response["error_msg"] = "Non è stato possibile inviare l'email.";
}
} else {
$response["error"] = true;
$response["error_msg"] = "I parametri richiesti sono obbligatori.";
}
return $result;
}
function email_recupero(){
global $response, $db;
if(isset($_POST["email"])) {
$email = $_POST["email"];
// Prepare statement
$stmt = $db->conn->prepare("SELECT codice_conferma FROM utenti WHERE email = ?");
$stmt->bind_param("s", $email);
// Try to execute prepered statement
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$codice = $user['codice_conferma'];
}
if (!$codice) {
$response["error"] = true;
$response["error_msg"] = "L'email inserita non è corretta. Riprova.";
return;
}
require_once "Helper_Functions.php";
$helper = new Helper_Functions();
// Mail it
$result = $helper->sendEmailRecupero($email, $codice);
if (!$result)
{
$response["error"] = true;
$response["error_msg"] = "Non è stato possibile inviare l'email.";
}
else
$response["codice"] = $codice;
} else {
$response["error"] = true;
$response["error_msg"] = "I parametri richiesti sono obbligatori.";
}
}
function cambia_password(){
global $response;
if (isset($_POST["email"]) && isset($_POST["password"]) && isset($_POST["codice"]))
{
$email = $_POST["email"];
$password = $_POST["password"];
$codice = $_POST["codice"];
if (!changePassword($email, $password, $codice))
{
$response["error"] = true;
$response["error_msg"] = "Errore imprevisto durante l'aggiornamento della password.";
}
}
else
{
$response["error"] = true;
$response["error_msg"] = "I parametri richiesti sono obbligatori.";
}
}
// Ritorna l'utente con le credenziali passate
function getUserByEmailAndPassword($email, $password) {
global $db;
// Prepare statement
$stmt = $db->conn->prepare("SELECT * FROM utenti WHERE email = ?");
$stmt->bind_param("s", $email);
// Try to execute prepered statement
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$db_password = $user['password'];
// check for password equality
if (password_verify($password, $db_password)) {
// user authentication details are correct
return $user;
}
}
return null;
}
// Inserisce un nuovo utente nel database
function storeUser($email, $password) {
global $db;
$db_password = password_hash($password, PASSWORD_BCRYPT); // encrypted password
$codice = genera_codice();
$data = date("Y-m-d");
// prepare and execute statement to insert new user in DB
$stmt = $db->conn->prepare("INSERT INTO utenti(email, password, codice_conferma, data_registrazione, verificato) VALUES(?, ?, ?, ?, 0)");
$stmt->bind_param("ssss", $email, $db_password, $codice, $data);
$result = $stmt->execute();
$stmt->close();
if ($result) {
$user["email"] = $email;
$user["codice"] = $codice;
return $user;
}
return false;
}
// Verifica l'account dell'utente
function accountVerification($email, $codice) {
global $db;
// check if it's already verified
$new_codice = genera_codice();
// Prepare statement
$stmt = $db->conn->prepare("UPDATE utenti SET verificato = 1, codice_conferma = ? WHERE email = ? AND codice_conferma = ? AND verificato = 0");
$stmt->bind_param("sss", $new_codice, $email, $codice);
// Try to execute prepered statement
if ($stmt->execute())
return $stmt->affected_rows == 1;
return false;
}
function changePassword($email, $password, $codice) {
global $db;
$new_codice = genera_codice();
$db_password = password_hash($password, PASSWORD_BCRYPT); // encrypted password
// Prepare statement
$stmt = $db->conn->prepare("UPDATE utenti SET password = ?, codice_conferma = ? WHERE email = ? AND codice_conferma = ?");
$stmt->bind_param("ssss", $db_password, $new_codice, $email, $codice);
// Try to execute prepered statement
if ($stmt->execute())
{
$result = $stmt->affected_rows;
$stmt->close();
return $result > 0;
}
return false;
}
function emailAlreadyUsed($email) {
global $db;
$stmt = $db->conn->prepare("SELECT email from utenti WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
$stmt->close();
return ($num_rows > 0); // return true if email exist, false otherwise
}
function genera_codice() {
return strtoupper(substr(md5(uniqid("")),8,6));
}
?>