diff options
author | polo <ordipolo@gmx.fr> | 2025-05-22 16:59:25 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2025-05-22 16:59:25 +0200 |
commit | 8c663379dcb9859a060e07681cc9082c025cf203 (patch) | |
tree | 5049033871efcde46fc838660cfc2aa2758a96c6 | |
parent | 1580f58d5a3e3ef3ab23927ee3b8455b086147f0 (diff) | |
download | cms-8c663379dcb9859a060e07681cc9082c025cf203.zip |
classe Captcha
-rw-r--r-- | public/index.php | 26 | ||||
-rw-r--r-- | src/controller/Captcha.php | 52 | ||||
-rw-r--r-- | src/controller/password.php | 62 | ||||
-rw-r--r-- | src/view/password.php | 17 |
4 files changed, 90 insertions, 67 deletions
diff --git a/public/index.php b/public/index.php index aa080c4..170c23d 100644 --- a/public/index.php +++ b/public/index.php | |||
@@ -3,7 +3,9 @@ | |||
3 | 3 | ||
4 | declare(strict_types=1); | 4 | declare(strict_types=1); |
5 | 5 | ||
6 | // -- prétraitement -- | 6 | |
7 | /* -- partie 1: prétraitement -- */ | ||
8 | |||
7 | // une nouvelle classe? taper: composer dump-autoload -o | 9 | // une nouvelle classe? taper: composer dump-autoload -o |
8 | require "../vendor/autoload.php"; | 10 | require "../vendor/autoload.php"; |
9 | 11 | ||
@@ -31,17 +33,15 @@ $_SESSION['admin'] = !isset($_SESSION['admin']) ? false : $_SESSION['admin']; // | |||
31 | 33 | ||
32 | // login, mot de passe et captcha | 34 | // login, mot de passe et captcha |
33 | require '../src/controller/password.php'; | 35 | require '../src/controller/password.php'; |
34 | existUsers($entityManager); | 36 | existUsers($entityManager); // si la table user est vide, on en crée un |
35 | 37 | ||
36 | // -- navigation avec les GET -- | ||
37 | $current_page = 'accueil'; | ||
38 | if(!empty($_GET['page'])) | ||
39 | { | ||
40 | $current_page = htmlspecialchars($_GET['page']); | ||
41 | } | ||
42 | define('CURRENT_PAGE', $current_page); | ||
43 | 38 | ||
44 | // -- traitement des POST (formulaires et AJAX) -- | 39 | /* -- partie 2: affichage d'une page ou traitement d'un POST -- */ |
40 | |||
41 | // navigation avec les GET | ||
42 | define('CURRENT_PAGE', !empty($_GET['page']) ? htmlspecialchars($_GET['page']) : 'accueil'); | ||
43 | |||
44 | // traitement des POST (formulaires et AJAX) | ||
45 | require '../src/controller/post.php'; | 45 | require '../src/controller/post.php'; |
46 | 46 | ||
47 | // id des articles | 47 | // id des articles |
@@ -67,11 +67,11 @@ elseif($_SESSION['admin'] && isset($_GET['page']) && isset($_GET['action']) && $ | |||
67 | MainBuilder::$modif_mode = true; | 67 | MainBuilder::$modif_mode = true; |
68 | } | 68 | } |
69 | 69 | ||
70 | // -- contrôleurs -- | 70 | // contrôleur principal |
71 | $director = new Director($entityManager, true); | 71 | $director = new Director($entityManager, true); |
72 | $director->makeRootNode($id); | 72 | $director->makeRootNode($id); |
73 | $node = $director->getNode(); | 73 | $node = $director->getNode(); |
74 | 74 | ||
75 | // -- vues -- | 75 | // vues |
76 | $view_builder = new ViewBuilder($node); | 76 | $view_builder = new ViewBuilder($node); |
77 | echo $view_builder->render(); // et voilà! | 77 | echo $view_builder->render(); // et voilà! \ No newline at end of file |
diff --git a/src/controller/Captcha.php b/src/controller/Captcha.php new file mode 100644 index 0000000..3253b95 --- /dev/null +++ b/src/controller/Captcha.php | |||
@@ -0,0 +1,52 @@ | |||
1 | <?php | ||
2 | // src/controller/Captcha.php | ||
3 | |||
4 | declare(strict_types=1); | ||
5 | |||
6 | class Captcha | ||
7 | { | ||
8 | private int $a; | ||
9 | private int $b; | ||
10 | private int $solution; | ||
11 | |||
12 | public function __construct(){ | ||
13 | $this->a = rand(2, 9); | ||
14 | $this->b = rand(2, 9); | ||
15 | $this->solution = $this->a * $this->b; | ||
16 | } | ||
17 | |||
18 | public function getA(): string | ||
19 | { | ||
20 | return $this->toLettersFrench($this->a); | ||
21 | } | ||
22 | public function getB(): string | ||
23 | { | ||
24 | return $this->toLettersFrench($this->b); | ||
25 | } | ||
26 | public function getSolution(): int | ||
27 | { | ||
28 | return $this->solution; | ||
29 | } | ||
30 | |||
31 | private function toLettersFrench(int $number): string | ||
32 | { | ||
33 | return match($number){ | ||
34 | 2 => 'deux', | ||
35 | 3 => 'trois', | ||
36 | 4 => 'quatre', | ||
37 | 5 => 'cinq', | ||
38 | 6 => 'six', | ||
39 | 7 => 'sept', | ||
40 | 8 => 'huit', | ||
41 | 9 => 'neuf', | ||
42 | default => '', // erreur | ||
43 | }; | ||
44 | } | ||
45 | static public function controlInput(string $input = '0'): int | ||
46 | { | ||
47 | // un POST est une chaîne qu'on doit convertir en nombre si: | ||
48 | // test de format: $input est un nombre | ||
49 | // test d'intégrité: supprimer les décimales avec (int) ne change pas la valeur du nombre | ||
50 | return is_numeric($input) && $input == (int)$input ? (int)$input : 0; | ||
51 | } | ||
52 | } \ No newline at end of file | ||
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 | |||
325 | function createCaptcha(): array | ||
326 | { | ||
327 | $a = rand(2, 9); | ||
328 | $b = rand(2, 9); | ||
329 | return array(toLettersFrench($a), toLettersFrench($b), $a * $b); | ||
330 | } | ||
331 | |||
332 | function 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 | ||
349 | function 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 |
diff --git a/src/view/password.php b/src/view/password.php index aadfbae..b8a090e 100644 --- a/src/view/password.php +++ b/src/view/password.php | |||
@@ -10,14 +10,18 @@ | |||
10 | declare(strict_types=1); | 10 | declare(strict_types=1); |
11 | 11 | ||
12 | // insertion du captcha | 12 | // insertion du captcha |
13 | ob_start(); | 13 | $captchaHtml = ''; |
14 | ?> | 14 | if(isset($captcha)) // instance de Captcha? |
15 | { | ||
16 | ob_start(); | ||
17 | ?> | ||
15 | <p>Montrez que vous n'êtes pas un robot.<br> | 18 | <p>Montrez que vous n'êtes pas un robot.<br> |
16 | <label for="captcha" >Combien font <?= $captcha[0] ?> fois <?= $captcha[1] ?>?</label> | 19 | <label for="captcha" >Combien font <?= $captcha->getA() ?> fois <?= $captcha->getB() ?>?</label> |
17 | <input required type="text" id="captcha" name="captcha" autocomplete="off" size="1"> | 20 | <input required type="text" id="captcha" name="captcha" autocomplete="off" size="1"> |
18 | </p> | 21 | </p> |
19 | <?php | 22 | <?php |
20 | $captchaHtml = ob_get_clean(); | 23 | $captchaHtml = ob_get_clean(); |
24 | } | ||
21 | 25 | ||
22 | 26 | ||
23 | // formulaire connexion | 27 | // formulaire connexion |
@@ -70,9 +74,6 @@ ob_start(); | |||
70 | <input id="new_password" type="password" name="new_password" required autocomplete="off"> | 74 | <input id="new_password" type="password" name="new_password" required autocomplete="off"> |
71 | <br><br> | 75 | <br><br> |
72 | <input type="submit" value="Valider" > | 76 | <input type="submit" value="Valider" > |
73 | <!-- <a href="index.php<?= $from ?>" > | ||
74 | <input type="button" value="Annuler"> | ||
75 | </a> --> | ||
76 | </form> | 77 | </form> |
77 | <?php | 78 | <?php |
78 | $formulaireModifMDP = ob_get_clean(); | 79 | $formulaireModifMDP = ob_get_clean(); |