summaryrefslogtreecommitdiff
path: root/src/controller/password.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2025-05-22 16:59:25 +0200
committerpolo <ordipolo@gmx.fr>2025-05-22 16:59:25 +0200
commit8c663379dcb9859a060e07681cc9082c025cf203 (patch)
tree5049033871efcde46fc838660cfc2aa2758a96c6 /src/controller/password.php
parent1580f58d5a3e3ef3ab23927ee3b8455b086147f0 (diff)
downloadcms-8c663379dcb9859a060e07681cc9082c025cf203.zip
classe Captcha
Diffstat (limited to 'src/controller/password.php')
-rw-r--r--src/controller/password.php62
1 files changed, 16 insertions, 46 deletions
diff --git a/src/controller/password.php b/src/controller/password.php
index 4748d9d..4b387ca 100644
--- a/src/controller/password.php
+++ b/src/controller/password.php
@@ -31,18 +31,18 @@ function createPassword(EntityManager $entityManager)
31{ 31{
32 // fonction exécutée à priori deux fois d'affilée: affichage puis traitement de la saisie 32 // fonction exécutée à priori deux fois d'affilée: affichage puis traitement de la saisie
33 33
34 // II - traitement
35 unset($_SESSION['user']); 34 unset($_SESSION['user']);
36 35
37 $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; 36 $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0;
38 $captcha = isset($_POST['captcha']) ? controlCaptchaInput($_POST['captcha']) : 0; 37 $captcha_try = isset($_POST['captcha']) ? Captcha::controlInput($_POST['captcha']) : 0;
39 38
39 // II - traitement
40 $error = ''; 40 $error = '';
41 if(!isset($_POST['captcha'])) // page rechargée 41 if(!isset($_POST['captcha'])) // page création d'un compte rechargée
42 { 42 {
43 //$error = ''; 43 //$error = '';
44 } 44 }
45 elseif($captcha == 0) 45 elseif($captcha_try == 0)
46 { 46 {
47 $error = 'error_non_valid_captcha'; 47 $error = 'error_non_valid_captcha';
48 } 48 }
@@ -50,7 +50,7 @@ function createPassword(EntityManager $entityManager)
50 { 50 {
51 //$error = ''; 51 //$error = '';
52 } 52 }
53 elseif($captcha != $captcha_solution) // le test! 53 elseif($captcha_try != $captcha_solution) // le test!
54 { 54 {
55 $error = 'bad_solution_captcha'; 55 $error = 'bad_solution_captcha';
56 } 56 }
@@ -72,6 +72,8 @@ function createPassword(EntityManager $entityManager)
72 if(isset($password) && isset($login) 72 if(isset($password) && isset($login)
73 && $password == $_POST['password'] && $login == $_POST['login']) 73 && $password == $_POST['password'] && $login == $_POST['login'])
74 { 74 {
75 unset($_SESSION['captcha']);
76
75 // enregistrement et redirection 77 // enregistrement et redirection
76 $password = password_hash($password, PASSWORD_DEFAULT); 78 $password = password_hash($password, PASSWORD_DEFAULT);
77 $user = new App\Entity\User($login, $password); 79 $user = new App\Entity\User($login, $password);
@@ -90,9 +92,9 @@ function createPassword(EntityManager $entityManager)
90 } 92 }
91 93
92 // inséré dans $captchaHtml puis dans $formulaireNouveauMDP 94 // inséré dans $captchaHtml puis dans $formulaireNouveauMDP
93 $captcha = createCaptcha(); 95 $captcha = new Captcha;
94 // enregistrement de la réponse du captcha pour vérification 96 // enregistrement de la réponse du captcha pour vérification
95 $_SESSION['captcha'] = $captcha[2]; // int 97 $_SESSION['captcha'] = $captcha->getSolution();
96 98
97 99
98 // I - affichage 100 // I - affichage
@@ -129,14 +131,14 @@ function connect(LoginBuilder $builder, EntityManager $entityManager)
129 $_SESSION['admin'] = false; 131 $_SESSION['admin'] = false;
130 132
131 $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; 133 $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0;
132 $captcha = isset($_POST['captcha']) ? controlCaptchaInput($_POST['captcha']) : 0; 134 $captcha_try = isset($_POST['captcha']) ? Captcha::controlInput($_POST['captcha']) : 0;
133 135
134 $error = ''; 136 $error = '';
135 if(!isset($_POST['captcha'])) // page rechargée 137 if(!isset($_POST['captcha'])) // page rechargée
136 { 138 {
137 //$error = ''; 139 //$error = '';
138 } 140 }
139 elseif($captcha == 0) 141 elseif($captcha_try == 0)
140 { 142 {
141 $error = 'error_non_valid_captcha'; 143 $error = 'error_non_valid_captcha';
142 } 144 }
@@ -144,7 +146,7 @@ function connect(LoginBuilder $builder, EntityManager $entityManager)
144 { 146 {
145 //$error = ''; 147 //$error = '';
146 } 148 }
147 elseif($captcha != $captcha_solution) // le test! 149 elseif($captcha_try != $captcha_solution) // le test!
148 { 150 {
149 $error = 'bad_solution_captcha'; 151 $error = 'bad_solution_captcha';
150 } 152 }
@@ -162,8 +164,8 @@ function connect(LoginBuilder $builder, EntityManager $entityManager)
162 // enregistrement et redirection 164 // enregistrement et redirection
163 if(!empty($user) && $login === $user->getLogin() && password_verify($password, $user->getPassword())) 165 if(!empty($user) && $login === $user->getLogin() && password_verify($password, $user->getPassword()))
164 { 166 {
165 session_start();
166 session_regenerate_id(true); // protection fixation de session, si l'attaquant a créé un cookie de session (attaque XSS), il est remplacé 167 session_regenerate_id(true); // protection fixation de session, si l'attaquant a créé un cookie de session (attaque XSS), il est remplacé
168 //unset($_SESSION['captcha']);
167 $_SESSION['user'] = $login; 169 $_SESSION['user'] = $login;
168 $_SESSION['admin'] = true; 170 $_SESSION['admin'] = true;
169 $link = new URL(isset($_GET['from']) ? ['page' => $_GET['from']] : []); 171 $link = new URL(isset($_GET['from']) ? ['page' => $_GET['from']] : []);
@@ -178,9 +180,9 @@ function connect(LoginBuilder $builder, EntityManager $entityManager)
178 } 180 }
179 181
180 // inséré dans $captchaHtml puis dans $formulaireNouveauMDP 182 // inséré dans $captchaHtml puis dans $formulaireNouveauMDP
181 $captcha = createCaptcha(); 183 $captcha = new Captcha;
182 // enregistrement de la réponse du captcha pour vérification 184 // enregistrement de la réponse du captcha pour vérification
183 $_SESSION['captcha'] = $captcha[2]; // int 185 $_SESSION['captcha'] = $captcha->getSolution(); // int
184 186
185 // I - affichage 187 // I - affichage
186 $title = "Connexion"; 188 $title = "Connexion";
@@ -319,36 +321,4 @@ function disconnect()
319 isset($_GET['id']) ? $link->addParams(['id' => $_GET['id']]) : ''; 321 isset($_GET['id']) ? $link->addParams(['id' => $_GET['id']]) : '';
320 header('Location: ' . $link); 322 header('Location: ' . $link);
321 die; 323 die;
322}
323
324
325function createCaptcha(): array
326{
327 $a = rand(2, 9);
328 $b = rand(2, 9);
329 return array(toLettersFrench($a), toLettersFrench($b), $a * $b);
330}
331
332function toLettersFrench(int $number): string
333{
334 return match($number)
335 {
336 2 => 'deux',
337 3 => 'trois',
338 4 => 'quatre',
339 5 => 'cinq',
340 6 => 'six',
341 7 => 'sept',
342 8 => 'huit',
343 9 => 'neuf',
344 default => '', // erreur
345 };
346}
347
348// on veut des chiffres
349function controlCaptchaInput(string $captcha = '0'): int
350{
351 // $captcha est un POST donc une chaîne, '2.3' est acceptés
352 // (int) supprime les décimales
353 return (is_numeric($captcha) && $captcha == (int) $captcha) ? (int) $captcha : 0;
354} \ No newline at end of file 324} \ No newline at end of file