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
// controller/password.php
// fonction exécutée à l'ouverture de chaque page
function existPassword()
{
// création du fichier
if(!file_exists('data/password.txt'))
{
touch('data/password.txt');
chmod('data/password.txt', 0600);
}
// lecture
$hashedPassword = file_get_contents('data/password.txt');
if($hashedPassword === false)
{
echo('Erreur: ouverture du fichier password.txt impossible.');
exit();
}
if(empty($hashedPassword))
{
createPassword();
}
}
function createCaptcha(): array
{
$a = rand(2, 10);
$b = rand(2, 10);
return array(toLettersFrench($a), toLettersFrench($b), $a * $b);
}
function toLettersFrench(int $number): string
{
return match($number)
{
2 => 'deux',
3 => 'trois',
4 => 'quatre',
5 => 'cinq',
6 => 'six',
7 => 'sept',
8 => 'huit',
9 => 'neuf',
10 => 'dix',
};
}
// vérifier qu'on a que des chiffres
function controlCaptchaInput()
{
//$_POST['captcha']
}
function createPassword()
{
// paranoïa?
if(isset($_SESSION['admin']))
{
unset($_SESSION['admin']);
header("Location: index.php");
exit();
}
$captcha = createCaptcha();
require('view/password.php');
$title = 'Bienvenue Melaine Favennec';
$subHeading = 'Veuillez choisir le mot de passe que vous utiliserez pour gérer le site.';
// au rechargement après saisi
if(isset($_POST['motdepasse']) && !empty($_POST['motdepasse']) && isset($_POST['captcha']) && (int) $_POST['captcha'] == $_SESSION['captcha'])
{
// caractères non désirés supprimés
// impossible d'entrer un espace ou une tabulation et de valider par erreur
require('controller/Security.php');
$password = Security::secureString($_POST['motdepasse']);
$password = removeSpacesTabsCRLF($_POST['motdepasse']);
// enregistrement
if(isset($password) && $password == $_POST['motdepasse'])
{
hashNewPassword($_POST['motdepasse']);
unset($_SESSION['captcha']); // nettoyage
header('Location: index.php');
}
// mot de passe non valable
else
{
sleep(1);
echo($header);
echo($errorPassword);
echo($formulaireNouveauMDP);
echo($errorBadCharacters);
}
}
// mauvais captcha
elseif(isset($_POST['captcha']) && (int) $_POST['captcha'] != $_SESSION['captcha'])
{
sleep(1);
echo($header);
echo($errorCaptcha);
echo($formulaireNouveauMDP);
echo($errorBadCharacters);
}
// 1ère fois
else
{
echo($header);
echo($formulaireNouveauMDP);
echo($errorBadCharacters);
//echo($warning); // message pas top
}
// fois suivante (dispense de nettoyer la variable)
$_SESSION['captcha'] = $captcha[2];
exit();
}
function connect()
{
// déjà en mode admin
if($_SESSION['admin'] == 1)
{
header('Location: index.php?page=' . $_GET['from']);
exit();
}
// Ajouter une sécurité par cpatcha avec un "input" supplémentaire
$captcha = createCaptcha();
// Et créer une variable de session pour la réponse au CAPTCHA
$title = "Connexion";
$subHeading = "Veuillez saisir votre mot de passe pour pouvoir apporter des modifications au site.";
// cette page utilise la même vue que la fonction changerMotDePasse() dans controller/admin.php
require('view/password.php');
echo($header);
// bon codes (mot de passe et captcha)
if(isset ($_POST["motdepasse"]) && testPassword($_POST["motdepasse"]) && isset($_POST['captcha']) && (int) $_POST['captcha'] == $_SESSION['captcha'])
{
$_SESSION['admin'] = 1;
unset($_SESSION['captcha']); // nettoyage
header('Location: index.php?page=' . $_GET['from']);
exit();
}
// mauvais captcha
elseif(isset($_POST['captcha']) && (int) $_POST['captcha'] != $_SESSION['captcha'])
{
echo($errorCaptcha);
sleep(1);
echo($formulaireConnexion);
}
// mauvais codes
elseif(isset ($_POST["motdepasse"]) && !testPassword($_POST["motdepasse"]))
{
// défense aux attaques par force brute
// pas parfait, ne marche pas si l'attaquant multiplie les connexions au site
echo($errorPassword);
sleep(1);
echo($formulaireConnexion);
}
// première arrivée sur la page
else
{
echo($formulaireConnexion);
}
// fois suivante (dispense de nettoyer la variable)
$_SESSION['captcha'] = $captcha[2];
echo($messageDeconnect);
echo($footer);
}
function changePassword()
{
// vérification supplémentaire
if($_SESSION['admin'] !== 1)
{
$_SESSION['admin'] = 0;
header('Location: index.php?page=' . $_GET['from']);
exit();
}
$title = "Nouveau mot de passe";
$subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau.";
require('view/password.php');
echo($header);
// conformité du nouveau mot de passe
if(isset($_POST['nouveauMotdepasse']) && !empty($_POST['nouveauMotdepasse']))
{
require('controller/Security.php');
$newPassword = Security::secureString($_POST['nouveauMotdepasse']);
$newPassword = removeSpacesTabsCRLF($_POST['nouveauMotdepasse']);
}
// bon mot de passe
if(isset($newPassword) && $newPassword === $_POST['nouveauMotdepasse']
&& isset ($_POST["ancienMotdepasse"]) && testPassword($_POST["ancienMotdepasse"]))
{
// enregistrement et confirmation
hashNewPassword($_POST["nouveauMotdepasse"]);
echo($message);
}
// mauvais mot de passe
elseif(isset ($_POST["ancienMotdepasse"]) && !testPassword($_POST["ancienMotdepasse"]))
{
// défense aux attaques par force brute
// pas parfait, ne marche pas si l'attaquant multiplie les connexions au site
echo($errorPassword);
sleep(1);
echo($formulaireModifMDP);
}
// erreur de conformité
elseif(isset($newPassword) && $newPassword !== $_POST['nouveauMotdepasse'])
{
echo($errorBadCharacters);
echo($formulaireModifMDP);
}
// première arrivée sur la page
else
{
echo($formulaireModifMDP);
}
//echo($warning);
echo($footer);
}
// hachage
function hashNewPassword(string $newPassword)
{
// "réparation" des espaces accidentels
$newPassword= trim($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
{
// lecture
$oldHashedPassword = file_get_contents('data/password.txt');
// "réparation" des espaces accidentels
$password= trim($password);
$oldHashedPassword = trim($oldHashedPassword);
if(password_verify($password, $oldHashedPassword))
{
return true;
}
else
{
return false;
}
}
|