blob: 5e556e90f8af113746717d67e7241d7a13ba556e (
plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
<?php
// controller/password.php
//
// fichier data/password.txt et test captcha
// exécutée dans installation.php à l'ouverture de chaque page
function existPassword()
{
// lecture
$file_name = 'data/password.txt';
$hashedPassword = file_get_contents($file_name);
if($hashedPassword === false)
{
echo('Erreur: ouverture du fichier ' . $file_name . ' impossible.');
exit();
}
$file_name = 'data/tmp/solution.txt';
if(empty($hashedPassword))
{
unset($_GET);
createPassword();
exit();
}
// nettoyage
elseif(isset($_GET['page']) && $_GET['page'] != 'connexion' && file_exists($file_name))
{
unlink($file_name);
}
}
function createCaptcha(): array
{
$a = rand(2, 9);
$b = rand(2, 9);
return array(toLettersFrench($a), toLettersFrench($b), $a * $b);
}
function toLettersFrench(int $number): string
{
switch($number)
{
case 2:
return 'deux';
break;
case 3:
return 'trois';
break;
case 4:
return 'quatre';
break;
case 5:
return 'cinq';
break;
case 6:
return 'six';
break;
case 7:
return 'sept';
break;
case 8:
return 'huit';
break;
case 9:
return 'neuf';
break;
default:
return ''; // erreur
};
}
// on veut des chiffres
function controlCaptchaInput(): int
{
if(is_numeric($_POST['captcha'])) // '2.3' est acceptés
{
// (int) supprime les décimales
if($_POST['captcha'] == (int) $_POST['captcha'])
{
return (int) $_POST['captcha'];
}
else
{
return 0;
}
}
else
{
return 0;
}
}
function createPassword()
{
// I - traitement
// cas anormal: session déjà existante
global $cookie; // nom du cookie dans cookies.php
if(isset($_COOKIE[$cookie]))
{
deleteCookie($cookie); // cookie supprimé au rechargement
header("Location: index.php");
exit();
}
// contrôle de la saisie
$file_name = 'data/tmp/solution.txt';
$error = '';
if(file_exists($file_name))
{
$captcha_solution = file_get_contents($file_name);
unlink($file_name);
if(empty($_POST)) // page rechargée
{}
elseif(!isset($_POST['captcha']) || controlCaptchaInput() == 0)
{
$error = 'error_non_valid_captcha';
}
elseif($_POST['captcha'] != $captcha_solution)
{
$error = 'bad_solution_captcha';
}
elseif(!isset($_POST['motdepasse']) || empty($_POST['motdepasse']))
{
$error = 'bad_password';
}
else
{
// -> caractères HTML dangereux supprimés
// -> empêche validation par erreur d'une chaine "vide"
require('controller/Security.php');
$password = Security::secureString($_POST['motdepasse']);
$password = removeSpacesTabsCRLF($_POST['motdepasse']);
// enregistrement et redirection
if(isset($password) && $password == $_POST['motdepasse'])
{
hashNewPassword($_POST['motdepasse']);
header('Location: index.php');
exit();
}
else
{
$error = 'bad_password';
}
}
}
else // première fois
{}
// inséré dans $captchaHtml puis dans $formulaireNouveauMDP
$captcha = createCaptcha();
// enregistrement de la réponse du captcha pour vérification
saveSolutionCaptcha($captcha[2]);
// II - affichage
$title = 'Bienvenue Melaine Favennec';
$subHeading = 'Veuillez choisir le mot de passe que vous utiliserez pour gérer le site.';
// même vue que la fonction changerMotDePasse()
require('view/password.php');
echo($header);
if($error != '')
{
sleep(1);
echo($error_messages[$error]);
}
echo($formulaireNouveauMDP);
echo($error_messages['forbidden_characters']);
echo($footer);
}
function connect()
{
// I - traitement
// déjà en mode admin
global $cookie; // nom du cookie dans cookies.php
if(isset($_COOKIE[$cookie]))
{
header('Location: index.php?page=' . $_GET['from']);
exit();
}
// contrôle de la saisie
$file_name = 'data/tmp/solution.txt';
$error = '';
if(file_exists($file_name))
{
$captcha_solution = file_get_contents($file_name);
unlink($file_name);
if(empty($_POST)) // page rechargée
{}
elseif(!isset($_POST['captcha']) || controlCaptchaInput() == 0)
{
$error = 'error_non_valid_captcha';
}
elseif($_POST['captcha'] != $captcha_solution)
{
$error = 'bad_solution_captcha';
}
elseif(!isset($_POST['motdepasse']) || empty($_POST['motdepasse']))
{
$error = 'bad_password';
}
else
{
// enregistrement et redirection
if(testPassword($_POST["motdepasse"]))
{
session_name($cookie);
session_start();
header('Location: index.php?page=' . $_GET['from']);
exit();
}
else
{
$error = 'bad_password';
}
}
}
else // première fois
{}
// inséré dans $captchaHtml puis dans $formulaireNouveauMDP
$captcha = createCaptcha();
// enregistrement de la réponse du captcha pour vérification
saveSolutionCaptcha($captcha[2]);
// II - affichage
$title = "Connexion";
$subHeading = "Veuillez saisir votre mot de passe pour pouvoir apporter des modifications au site.";
// même vue que la fonction changerMotDePasse()
require('view/password.php');
echo($header);
//echo($warning_messages['message_disconnect']);
if($error != '')
{
sleep(1);
echo($error_messages[$error]);
}
echo($formulaireConnexion);
echo($warning_messages['message_cookie']);
echo($warning_messages['private_browsing']);
echo($footer);
}
function saveSolutionCaptcha(string $solution)
{
$file_name = 'data/tmp/solution.txt';
if(!file_exists($file_name))
{
touch($file_name);
chmod($file_name, 0600);
}
file_put_contents($file_name, $solution);
}
function changePassword()
{
// I - traitement
// vérification supplémentaire
global $cookie; // nom du cookie dans cookies.php
if(!isset($_SESSION))
{
deleteCookie($cookie);
header('Location: index.php?page=' . $_GET['from']);
exit();
}
// contrôle de la saisie
$error = '';
$success = false;
if(empty($_POST)) // première fois ou page rechargée
{}
elseif(!isset($_POST['nouveauMotdepasse']) || empty($_POST['nouveauMotdepasse'])|| !isset($_POST['ancienMotdepasse']) || empty($_POST['ancienMotdepasse']))
{
$error = 'bad_password';
}
else
{
require('controller/Security.php');
$newPassword = Security::secureString($_POST['nouveauMotdepasse']);
$newPassword = removeSpacesTabsCRLF($_POST['nouveauMotdepasse']);
if(isset($newPassword) && $newPassword !== $_POST['nouveauMotdepasse']) // erreur de conformité
{
$error = 'forbidden_characters';
}
else
{
if(testPassword($_POST["ancienMotdepasse"]))
{
// enregistrement et confirmation
hashNewPassword($_POST["nouveauMotdepasse"]);
$success = true;
}
else
{
$error = 'bad_password';
}
}
}
// II - affichage
$title = "Nouveau mot de passe";
$subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau.";
require('view/password.php');
echo($header);
if($error != '')
{
sleep(1);
echo($error_messages[$error]);
}
elseif($success)
{
echo($alertJSNewPassword);
}
echo($formulaireModifMDP);
echo($footer);
}
function hashNewPassword(string $newPassword)
{
// hachage
$newHashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
// écriture
$file = fopen('data/password.txt', 'w');
fputs($file, $newHashedPassword);
fclose($file);
chmod('data/password.txt', 0600);
}
function testPassword(string $password): bool
{
$hashedPassword = file_get_contents('data/password.txt');
if(password_verify($password, $hashedPassword))
{
return true;
}
else
{
return false;
}
}
|