diff options
| author | polo-pc-greta <ordipolo@gmx.fr> | 2025-03-27 10:13:03 +0100 |
|---|---|---|
| committer | polo-pc-greta <ordipolo@gmx.fr> | 2025-03-27 10:13:03 +0100 |
| commit | df3612ed7e6691530503f79483d2fdbc032d01b8 (patch) | |
| tree | 56d1c68fdc8625f5dad1937a654299d45142c79a /src/controller/password.php | |
| download | cms-df3612ed7e6691530503f79483d2fdbc032d01b8.tar.gz cms-df3612ed7e6691530503f79483d2fdbc032d01b8.tar.bz2 cms-df3612ed7e6691530503f79483d2fdbc032d01b8.zip | |
mise en ligne github
Diffstat (limited to 'src/controller/password.php')
| -rw-r--r-- | src/controller/password.php | 357 |
1 files changed, 357 insertions, 0 deletions
diff --git a/src/controller/password.php b/src/controller/password.php new file mode 100644 index 0000000..d5e66ff --- /dev/null +++ b/src/controller/password.php | |||
| @@ -0,0 +1,357 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/password.php | ||
| 3 | // | ||
| 4 | // test mot de passe et captcha | ||
| 5 | |||
| 6 | declare(strict_types=1); | ||
| 7 | |||
| 8 | use Doctrine\ORM\EntityManager; | ||
| 9 | use App\Entity\User; | ||
| 10 | |||
| 11 | // exécutée dans installation.php à l'ouverture de chaque page | ||
| 12 | function existUsers(EntityManager $entityManager) | ||
| 13 | { | ||
| 14 | // lecture | ||
| 15 | $users = $entityManager->getRepository(User::class)->findAll(); | ||
| 16 | |||
| 17 | // cas particulier table vide | ||
| 18 | if(count($users) === 0) | ||
| 19 | { | ||
| 20 | $_GET = []; | ||
| 21 | $_SESSION['user'] = ''; | ||
| 22 | $_SESSION['admin'] = false; | ||
| 23 | |||
| 24 | // création d'un utilisateur, puis rechargement de la page | ||
| 25 | createPassword($entityManager); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | function createPassword(EntityManager $entityManager) | ||
| 31 | { | ||
| 32 | // fonction exécutée à priori deux fois d'affilée: affichage puis traitement de la saisie | ||
| 33 | |||
| 34 | // II - traitement | ||
| 35 | unset($_SESSION['user']); | ||
| 36 | |||
| 37 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; | ||
| 38 | $captcha = isset($_POST['captcha']) ? controlCaptchaInput($_POST['captcha']) : 0; | ||
| 39 | |||
| 40 | $error = ''; | ||
| 41 | if(!isset($_POST['captcha'])) // page rechargée | ||
| 42 | { | ||
| 43 | //$error = ''; | ||
| 44 | } | ||
| 45 | elseif($captcha == 0) | ||
| 46 | { | ||
| 47 | $error = 'error_non_valid_captcha'; | ||
| 48 | } | ||
| 49 | elseif($captcha_solution == 0) | ||
| 50 | { | ||
| 51 | //$error = ''; | ||
| 52 | } | ||
| 53 | elseif($captcha != $captcha_solution) // le test! | ||
| 54 | { | ||
| 55 | $error = 'bad_solution_captcha'; | ||
| 56 | } | ||
| 57 | elseif(!isset($_POST['password']) || empty($_POST['password']) | ||
| 58 | || (!isset($_POST['login']) || empty($_POST['login']))) | ||
| 59 | { | ||
| 60 | $error = 'bad_login_or_password'; | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | // -> caractères HTML dangereux supprimés | ||
| 65 | $login = Security::secureString($_POST['login']); | ||
| 66 | $password = Security::secureString($_POST['password']); | ||
| 67 | |||
| 68 | // -> prévenir la validation par erreur d'une chaine "vide" | ||
| 69 | $login = removeSpacesTabsCRLF($login); | ||
| 70 | $password = removeSpacesTabsCRLF($password); | ||
| 71 | |||
| 72 | // conformité | ||
| 73 | if(isset($password) && isset($login) | ||
| 74 | && $password == $_POST['password'] && $login == $_POST['login']) | ||
| 75 | { | ||
| 76 | // enregistrement et redirection | ||
| 77 | $password = password_hash($password, PASSWORD_DEFAULT); | ||
| 78 | $user = new App\Entity\User($login, $password); | ||
| 79 | $entityManager->persist($user); | ||
| 80 | $entityManager->flush(); | ||
| 81 | |||
| 82 | header('Location: ' . new URL); | ||
| 83 | exit(); | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | $error = 'bad_password'; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | // inséré dans $captchaHtml puis dans $formulaireNouveauMDP | ||
| 92 | $captcha = createCaptcha(); | ||
| 93 | // enregistrement de la réponse du captcha pour vérification | ||
| 94 | $_SESSION['captcha'] = $captcha[2]; // int | ||
| 95 | |||
| 96 | |||
| 97 | // I - affichage | ||
| 98 | $title = 'Bienvenue nageur bigouden'; | ||
| 99 | $subHeading = 'Veuillez choisir les codes que vous utiliserez pour gérer le site.'; | ||
| 100 | |||
| 101 | // même vue que la fonction changerMotDePasse() | ||
| 102 | require('../src/view/password.php'); | ||
| 103 | |||
| 104 | echo($header); | ||
| 105 | if($error != '') | ||
| 106 | { | ||
| 107 | sleep(1); | ||
| 108 | echo($error_messages[$error]); | ||
| 109 | } | ||
| 110 | echo($formulaireNouveauMDP); | ||
| 111 | echo($error_messages['forbidden_characters']); | ||
| 112 | echo($footer); | ||
| 113 | die; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | function connect(LoginBuilder $builder, EntityManager $entityManager) | ||
| 118 | { | ||
| 119 | // déjà connecté | ||
| 120 | if($_SESSION['admin']) | ||
| 121 | { | ||
| 122 | header('Location: ' . new URL); | ||
| 123 | die; | ||
| 124 | } | ||
| 125 | |||
| 126 | // II - traitement | ||
| 127 | $_SESSION['user'] = ''; | ||
| 128 | $_SESSION['admin'] = false; | ||
| 129 | |||
| 130 | $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; | ||
| 131 | $captcha = isset($_POST['captcha']) ? controlCaptchaInput($_POST['captcha']) : 0; | ||
| 132 | |||
| 133 | $error = ''; | ||
| 134 | if(!isset($_POST['captcha'])) // page rechargée | ||
| 135 | { | ||
| 136 | //$error = ''; | ||
| 137 | } | ||
| 138 | elseif($captcha == 0) | ||
| 139 | { | ||
| 140 | $error = 'error_non_valid_captcha'; | ||
| 141 | } | ||
| 142 | elseif($captcha_solution == 0) | ||
| 143 | { | ||
| 144 | //$error = ''; | ||
| 145 | } | ||
| 146 | elseif($captcha != $captcha_solution) // le test! | ||
| 147 | { | ||
| 148 | $error = 'bad_solution_captcha'; | ||
| 149 | } | ||
| 150 | elseif(!isset($_POST['login']) || empty($_POST['login']) | ||
| 151 | || !isset($_POST['password']) || empty($_POST['password'])) | ||
| 152 | { | ||
| 153 | $error = 'bad_password'; | ||
| 154 | } | ||
| 155 | else // c'est OK | ||
| 156 | { | ||
| 157 | $login = $_POST['login']; | ||
| 158 | $password = $_POST['password']; | ||
| 159 | $user = getUser($login, $entityManager); | ||
| 160 | |||
| 161 | // enregistrement et redirection | ||
| 162 | if(password_verify($password, $user->getPassword())) | ||
| 163 | { | ||
| 164 | session_start(); | ||
| 165 | $_SESSION['user'] = $login; | ||
| 166 | $_SESSION['admin'] = true; | ||
| 167 | $link = new URL(isset($_GET['from']) ? ['page' => $_GET['from']] : []); | ||
| 168 | isset($_GET['id']) ? $link->addParams(['id' => $_GET['id']]) : ''; | ||
| 169 | header('Location: ' . $link); | ||
| 170 | die; | ||
| 171 | } | ||
| 172 | else | ||
| 173 | { | ||
| 174 | $error = 'bad_password'; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | // inséré dans $captchaHtml puis dans $formulaireNouveauMDP | ||
| 179 | $captcha = createCaptcha(); | ||
| 180 | // enregistrement de la réponse du captcha pour vérification | ||
| 181 | $_SESSION['captcha'] = $captcha[2]; // int | ||
| 182 | |||
| 183 | // I - affichage | ||
| 184 | $title = "Connexion"; | ||
| 185 | $subHeading = "Veuillez saisir votre identifiant (e-mail) et votre mot de passe."; | ||
| 186 | |||
| 187 | require('../src/view/password.php'); | ||
| 188 | |||
| 189 | //$builder->addHTML($header); | ||
| 190 | if($error != '') | ||
| 191 | { | ||
| 192 | sleep(1); | ||
| 193 | $builder->addHTML($error_messages[$error]); | ||
| 194 | } | ||
| 195 | $builder->addHTML($formulaireConnexion); | ||
| 196 | //$builder->addHTML($warning_messages['message_cookie']); | ||
| 197 | $builder->addHTML($warning_messages['private_browsing']); | ||
| 198 | $builder->addHTML($footer); | ||
| 199 | |||
| 200 | //die; | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | function changePassword(EntityManager $entityManager) | ||
| 205 | { | ||
| 206 | // fonction exécutée à priori deux fois d'affilée: affichage puis traitement de la saisie | ||
| 207 | |||
| 208 | // OUT !! | ||
| 209 | if(empty($_SESSION['user']) || !$_SESSION['admin']) | ||
| 210 | { | ||
| 211 | $_SESSION['user'] = ''; | ||
| 212 | $_SESSION['admin'] = false; | ||
| 213 | header('Location: index.php'); | ||
| 214 | die; | ||
| 215 | } | ||
| 216 | |||
| 217 | // II - traitement | ||
| 218 | $error = ''; | ||
| 219 | $success = false; | ||
| 220 | if(empty($_POST)) // première fois ou page rechargée | ||
| 221 | { | ||
| 222 | // | ||
| 223 | } | ||
| 224 | elseif(!isset($_POST['login']) || empty($_POST['login']) | ||
| 225 | || !isset($_POST['old_password']) || empty($_POST['old_password']) | ||
| 226 | || !isset($_POST['new_password']) || empty($_POST['new_password'])) | ||
| 227 | { | ||
| 228 | $error = 'bad_login_or_password'; | ||
| 229 | } | ||
| 230 | else | ||
| 231 | { | ||
| 232 | // sécurisation de la saisie | ||
| 233 | $new_password = Security::secureString($_POST['new_password']); | ||
| 234 | $login = Security::secureString($_POST['login']); | ||
| 235 | $old_password = Security::secureString($_POST['old_password']); | ||
| 236 | |||
| 237 | // éviter d'enregistrer une chaîne vide | ||
| 238 | $new_password = removeSpacesTabsCRLF($new_password); | ||
| 239 | |||
| 240 | // tests de conformité | ||
| 241 | if($login != $_POST['login'] || $old_password != $_POST['old_password'] || $new_password != $_POST['new_password']) | ||
| 242 | { | ||
| 243 | $error = 'forbidden_characters'; | ||
| 244 | } | ||
| 245 | else | ||
| 246 | { | ||
| 247 | $user = getUser($login, $entityManager); | ||
| 248 | |||
| 249 | if(password_verify($old_password, $user->getPassword())) | ||
| 250 | { | ||
| 251 | // enregistrement et redirection | ||
| 252 | $new_password = password_hash($new_password, PASSWORD_DEFAULT); | ||
| 253 | $user->setPassword($new_password); | ||
| 254 | $entityManager->flush(); | ||
| 255 | $success = true; | ||
| 256 | } | ||
| 257 | else | ||
| 258 | { | ||
| 259 | $error = 'bad_password'; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | // I - affichage | ||
| 266 | $title = "Nouveau mot de passe"; | ||
| 267 | $subHeading = "Veuillez vous identifier à nouveau puis saisir votre nouveau mot de passe."; | ||
| 268 | |||
| 269 | require('../src/view/password.php'); | ||
| 270 | |||
| 271 | echo($header); | ||
| 272 | if($error != '') | ||
| 273 | { | ||
| 274 | sleep(1); // sécurité TRÈS insuffisante à la force brute | ||
| 275 | echo($error_messages[$error]); | ||
| 276 | } | ||
| 277 | elseif($success) | ||
| 278 | { | ||
| 279 | $success = false; | ||
| 280 | echo($alertJSNewPassword); | ||
| 281 | die; | ||
| 282 | } | ||
| 283 | echo($formulaireModifMDP); | ||
| 284 | echo($footer); | ||
| 285 | die; | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | function getUser(string $login, EntityManager $entityManager): User | ||
| 290 | { | ||
| 291 | $users = $entityManager->getRepository('App\Entity\User')->findBy(['login' => $login]); | ||
| 292 | |||
| 293 | // détection d'un abus | ||
| 294 | if(count($users) === 0) | ||
| 295 | { | ||
| 296 | $_SESSION['user'] = ''; | ||
| 297 | $_SESSION['admin'] = false; | ||
| 298 | |||
| 299 | header('Location: index.php'); // page création d'un mot de passe à l'attérissage | ||
| 300 | die; | ||
| 301 | } | ||
| 302 | |||
| 303 | foreach($users as $user) | ||
| 304 | { | ||
| 305 | if($user->getLogin() === $login) | ||
| 306 | { | ||
| 307 | return $user; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | header('Location: ' . new URL); | ||
| 311 | die; | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | function disconnect(EntityManager $entityManager) | ||
| 316 | { | ||
| 317 | // nettoyage complet | ||
| 318 | $_SESSION = []; // mémoire vive | ||
| 319 | session_destroy(); // fichier côté serveur | ||
| 320 | setcookie('PHPSESSID', '', time() - 4200, '/'); // cookie de session | ||
| 321 | $link = new URL(['page' => $_GET['page']]); | ||
| 322 | isset($_GET['id']) ? $link->addParams(['id' => $_GET['id']]) : ''; | ||
| 323 | header('Location: ' . $link); | ||
| 324 | die; | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | function createCaptcha(): array | ||
| 329 | { | ||
| 330 | $a = rand(2, 9); | ||
| 331 | $b = rand(2, 9); | ||
| 332 | return array(toLettersFrench($a), toLettersFrench($b), $a * $b); | ||
| 333 | } | ||
| 334 | |||
| 335 | function toLettersFrench(int $number): string | ||
| 336 | { | ||
| 337 | return match($number) | ||
| 338 | { | ||
| 339 | 2 => 'deux', | ||
| 340 | 3 => 'trois', | ||
| 341 | 4 => 'quatre', | ||
| 342 | 5 => 'cinq', | ||
| 343 | 6 => 'six', | ||
| 344 | 7 => 'sept', | ||
| 345 | 8 => 'huit', | ||
| 346 | 9 => 'neuf', | ||
| 347 | default => '', // erreur | ||
| 348 | }; | ||
| 349 | } | ||
| 350 | |||
| 351 | // on veut des chiffres | ||
| 352 | function controlCaptchaInput(string $captcha = '0'): int | ||
| 353 | { | ||
| 354 | // $captcha est un POST donc une chaîne, '2.3' est acceptés | ||
| 355 | // (int) supprime les décimales | ||
| 356 | return (is_numeric($captcha) && $captcha == (int) $captcha) ? (int) $captcha : 0; | ||
| 357 | } \ No newline at end of file | ||
