forked from space192/PiscineWeb22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryAccount.php
276 lines (269 loc) · 9.21 KB
/
queryAccount.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
<?php
include_once 'const.php';
function getAccount()
{
if(isset($_COOKIE["LOGGED_USER"]))
{
$temp = $_COOKIE["LOGGED_USER"];
}
else if(isset($_SESSION["LOGGED"]))
{
$temp = $_SESSION["LOGGED"];
}
if(isset($temp))
{
global $mysqlConnection;
$memberStatement = $mysqlConnection->prepare("SELECT * FROM utilisateur WHERE LOWER(Mail)='". strtolower($temp) ."';");
$memberStatement->execute();
$result = $memberStatement->fetch();
return $result;
}
else
{
return false;
}
}
function getAccountM()
{
global $mysqlConnection;
$memberStatement = $mysqlConnection->prepare("SELECT * FROM Medecin WHERE ID='". $_SESSION["ID"]."';");
$memberStatement->execute();
$result = $memberStatement->fetch();
return $result;
}
function loginM()
{
if(isset($_POST["Prenom"]) && isset($_POST["Nom"]) && isset($_POST["Mail"]))
{
global $mysqlConnection;
$memberStatement = $mysqlConnection->prepare("SELECT * FROM Medecin WHERE LOWER(Prenom)='". strtolower($_POST["Prenom"]) ."' AND LOWER(Nom)='". strtolower($_POST["Nom"]) ."' AND LOWER(Mail)='" . strtolower($_POST["Mail"]) . "';");
$memberStatement->execute();
$result = $memberStatement->fetch();
if($result != False)
{
$_SESSION["Medecin"] = "1";
$_SESSION["LOGGED"] = $_POST["Mail"];
$_SESSION["Prenom"] = $result["Prenom"];
$_SESSION["ID"] = $result["ID"];
return $result;
}
else
{
echo("non");
return false;
}
}
}
function loginA()
{
if(isset($_POST["Prenom"]) && isset($_POST["Nom"]) && isset($_POST["Mail"]))
{
global $mysqlConnection;
$memberStatement = $mysqlConnection->prepare("SELECT * FROM utilisateur WHERE LOWER(Prenom)='". strtolower($_POST["Prenom"]) ."' AND LOWER(Nom)='". strtolower($_POST["Nom"]) ."' AND LOWER(Mail)='" . strtolower($_POST["Mail"]) . "' AND ADMIN='1';");
$memberStatement->execute();
$result = $memberStatement->fetch();
if($result != False)
{
setcookie(
'ADMIN',
'1',
[
'expires' => time() + 365*24*3600,
'secure' => true,
'httponly' => true,
]
);
setcookie(
'LOGGED_USER',
$_POST["Mail"],
[
'expires' => time() + 365*24*3600,
'secure' => true,
'httponly' => true,
]
);
return $result;
}
else
{
return false;
}
}
}
function loginU()
{
if(isset($_POST["Mail"]) && isset($_POST["pwd"]) )
{
global $mysqlConnection;
$memberStatement = $mysqlConnection->prepare("SELECT ID,Mail,Prenom FROM utilisateur WHERE LOWER(Mail)='". strtolower($_POST["Mail"]) ."' AND pwd='". hash('sha256', $_POST['pwd']) ."';");
$memberStatement->execute();
$result = $memberStatement->fetch();
if($result != False)
{
if(isset($_POST["souvenir"]))
{
setcookie(
'LOGGED_USER',
$_POST["Mail"],
[
'expires' => time() + 365*24*3600,
'secure' => true,
'httponly' => true,
]
);
setcookie(
'Prenom',
$result["Prenom"],
[
'expires' => time() + 365*24*3600,
'secure' => true,
'httponly' => true,
]
);
setcookie(
'ID',
$result["ID"],
[
'expires' => time() + 365*24*3600,
'secure' => true,
'httponly' => true,
]
);
}
else
{
$_SESSION["LOGGED"] = $_POST["Mail"];
$_SESSION["Prenom"] = $result["Prenom"];
$_SESSION["ID"] = $result["ID"];
}
return $result;
}
else
{
return false;
}
}
}
function fieldChange()
{
global $mysqlConnection;
$temp = "UPDATE utilisateur SET ";
foreach($_POST as $key => $value)
{
if($key != "query")
{
if($key == "pwd")
{
$temp = $temp . " " .$key . "='" . hash("sha256", $value) . "',";
}
else if($key == "Mail")
{
$memberStatement = $mysqlConnection->prepare("SELECT Mail FROM utilisateur WHERE LOWER(Mail)='". strtolower($_POST["Mail"]) . "';");
$memberStatement->execute();
$result = $memberStatement->fetch();
if($result != False)
{
return false;
}
else
{
$temp = $temp . " " .$key . "='" . $value . "',";
if(isset($_COOKIE["LOGGED_USER"]))
{
$temp2 = $_COOKIE["LOGGED_USER"];
setcookie(
'LOGGED_USER',
$value,
[
'expires' => time() + 365*24*3600,
'secure' => true,
'httponly' => true,
]
);
}
else if(isset($_SESSION["LOGGED"]))
{
$temp2 = $_SESSION["LOGGED"];
include_once 'const.php';
$_SESSION["LOGGED"] = $value;
}
}
}
else
{
$temp = $temp . " " .$key . "='" . $value . "',";
}
}
}
$temp = substr_replace($temp ,"",-1);
if(isset($temp2))
{
$temp = $temp . " WHERE Mail='" . $temp2 ."'";
}
else
{
if(isset($_SESSION["LOGGED"]))
{
$temp = $temp . " WHERE Mail='" . $_SESSION["LOGGED"] ."'";
}
else if(isset($_COOKIE["LOGGED_USER"]))
{
$temp = $temp . " WHERE Mail='" . $_COOKIE["LOGGED_USER"] ."'";
}
}
$memberStatement = $mysqlConnection->prepare($temp);
return $memberStatement->execute();
}
function InsertUIntoBDD()
{
if(!isset($_POST["Nom"]) || !isset($_POST["Prenom"]) || !isset($_POST["Telephone"]) || !isset($_POST["Mail"]) || (!isset($_POST["Adresse_ligne1"]) && !isset($_POST["Adresse_ligne2"])) || !isset($_POST["Ville"]) || !isset($_POST["Code_postal"]) || !isset($_POST["Pays"])|| !isset($_POST["Carte_vital"]))
{
die;
}
else
{
global $mysqlConnection;
$sql = "";
if(isset($_POST["Adresse_ligne2"]) && isset($_POST["Adresse_ligne1"]))
{
$sql = "INSERT INTO utilisateur (Nom, Prenom, Telephone,Mail,Adresse_ligne1,Adresse_ligne2,Ville,Code_postal,Pays,Carte_vital, pwd) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
$stmt= $mysqlConnection->prepare($sql);
return $stmt->execute([$_POST["Nom"], $_POST["Prenom"], $_POST["Telephone"],$_POST["Mail"],$_POST["Adresse_ligne1"],$_POST["Adresse_ligne2"],$_POST["Ville"],$_POST["Code_postal"],$_POST["Pays"],$_POST["Carte_vital"],hash('sha256', $_POST['pwd'])]);
}
else
{
$sql = "INSERT INTO utilisateur (Nom, Prenom, Telephone,Mail,Adresse_ligne1,Ville,Code_postal,Pays,Carte_vital, pwd) VALUES (?,?,?,?,?,?,?,?,?,?)";
$stmt= $mysqlConnection->prepare($sql);
return $stmt->execute([$_POST["Nom"], $_POST["Prenom"], $_POST["Telephone"],$_POST["Mail"],$_POST["Adresse_ligne1"],$_POST["Ville"],$_POST["Code_postal"],$_POST["Pays"],$_POST["Carte_vital"],hash('sha256', $_POST['pwd'])]);
}
}
}
function InsertMIntoBDD()
{
if(!isset($_POST["Nom"]) || !isset($_POST["Prenom"]) || !isset($_POST["Localisation"]) || !isset($_POST["Telephone"]) || !isset($_POST["Mail"]) || !isset($_POST["Digicode"]))
{
return false;
}
else
{
global $mysqlConnection;
$sql = "INSERT INTO Medecin (Nom, Prenom,Localisation, Telephone,Mail,Digicode) VALUES (?,?,?,?,?,?)";
$stmt= $mysqlConnection->prepare($sql);
return $stmt->execute([$_POST["Nom"], $_POST["Prenom"], $_POST["Localisation"], $_POST["Telephone"],$_POST["Mail"], $_POST["Digicode"]]);
}
}
if(isset($_POST["query"]))
{
switch($_POST["query"])
{
case 1: InsertUIntoBDD(); break; //insererUser dans la BDD
case 2: InsertMIntoBDD(); break; //inserMedecin dans la BDD
case 3: loginM(); break; //login pour les medecins
case 4: loginU(); break; //login pour les useurs
case 5: if(!fieldChange()){header('Location: Account.php?error=1');die();}; break;
case 6: loginA(); break;
}
header('Location: Account.php');
die();
}
?>