diff options
| author | polo <ordipolo@gmx.fr> | 2022-03-01 04:02:05 +0100 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2022-03-01 04:02:05 +0100 |
| commit | 2ac4254829fb27d878044978e4e89f15eeeddd23 (patch) | |
| tree | d6ae70410566884d8bf567b0934cdaee5133a5a1 /controller/password.php | |
| parent | abd968a1c573b1862bd0728f0b7b6a88e307900a (diff) | |
| download | melaine-2ac4254829fb27d878044978e4e89f15eeeddd23.tar.gz melaine-2ac4254829fb27d878044978e4e89f15eeeddd23.tar.bz2 melaine-2ac4254829fb27d878044978e4e89f15eeeddd23.zip | |
factorisation et pages manquantes
Diffstat (limited to 'controller/password.php')
| -rw-r--r-- | controller/password.php | 79 |
1 files changed, 52 insertions, 27 deletions
diff --git a/controller/password.php b/controller/password.php index 4ca9f4f..8f6a875 100644 --- a/controller/password.php +++ b/controller/password.php | |||
| @@ -5,11 +5,25 @@ | |||
| 5 | // affichage | 5 | // affichage |
| 6 | function createPassword() | 6 | function createPassword() |
| 7 | { | 7 | { |
| 8 | // si installation() vient de créer un fichier vide | 8 | // création du fichier |
| 9 | $hashedPassword = trim(file_get_contents('data/password.txt')); | 9 | if(!file_exists('data/password.txt')) |
| 10 | { | ||
| 11 | touch('data/password.txt'); | ||
| 12 | chmod('data/password.txt', 0600); | ||
| 13 | } | ||
| 14 | |||
| 15 | // lecture | ||
| 16 | $hashedPassword = file_get_contents('data/password.txt'); | ||
| 17 | if($hashedPassword === false) | ||
| 18 | { | ||
| 19 | echo('Erreur: ouverture du fichier password.txt impossible.'); | ||
| 20 | exit(); | ||
| 21 | } | ||
| 22 | |||
| 23 | // création du mot de passe | ||
| 10 | if(empty($hashedPassword)) | 24 | if(empty($hashedPassword)) |
| 11 | { | 25 | { |
| 12 | // paranoïa | 26 | // paranoïa? |
| 13 | if(isset($_SESSION['admin'])) | 27 | if(isset($_SESSION['admin'])) |
| 14 | { | 28 | { |
| 15 | unset($_SESSION['admin']); | 29 | unset($_SESSION['admin']); |
| @@ -17,23 +31,31 @@ function createPassword() | |||
| 17 | exit(); | 31 | exit(); |
| 18 | } | 32 | } |
| 19 | 33 | ||
| 20 | // au rechargement: un mot de passe a été saisi | 34 | // au rechargement après saisi |
| 21 | // les espaces/tabulations sont considérés commes des erreurs | 35 | // impossible d'entrer un espace ou une tabulation et de valider par erreur |
| 22 | if(isset($_POST['motdepasse']) && !empty(trim($_POST['motdepasse']))) | 36 | if(isset($_POST['motdepasse']) && !empty($_POST['motdepasse'])) |
| 37 | { | ||
| 38 | // caractères non désirés supprimés | ||
| 39 | require('controller/Security.php'); | ||
| 40 | $password = Security::secureString($_POST['motdepasse']); | ||
| 41 | $password = removeSpacesTabsCRLF($_POST['motdepasse']); | ||
| 42 | } | ||
| 43 | // enregistrement | ||
| 44 | if(isset($password) && $password == $_POST['motdepasse']) | ||
| 23 | { | 45 | { |
| 24 | // enregistrement | ||
| 25 | hashNewPassword($_POST['motdepasse']); | 46 | hashNewPassword($_POST['motdepasse']); |
| 26 | header('Location: index.php'); | 47 | header('Location: index.php'); |
| 27 | } | 48 | } |
| 28 | // 1ère fois | 49 | // 1ère fois |
| 29 | else | 50 | else |
| 30 | { | 51 | { |
| 31 | $title = "Créer un mot de passe"; | 52 | $title = 'Créer un mot de passe'; |
| 32 | $subHeading = "Veuillez choisir le mot de passe que vous utiliserez pour gérer le site."; | 53 | $subHeading = 'Veuillez choisir le mot de passe que vous utiliserez pour gérer le site.'; |
| 33 | 54 | ||
| 34 | require('view/password.php'); | 55 | require('view/password.php'); |
| 35 | 56 | ||
| 36 | echo($header); | 57 | echo($header); |
| 58 | echo($errorBadCharacters); | ||
| 37 | echo($formulaireNouveauMDP); | 59 | echo($formulaireNouveauMDP); |
| 38 | echo($warning); | 60 | echo($warning); |
| 39 | } | 61 | } |
| @@ -103,37 +125,40 @@ function changePassword() | |||
| 103 | $title = "Nouveau mot de passe"; | 125 | $title = "Nouveau mot de passe"; |
| 104 | $subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau."; | 126 | $subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau."; |
| 105 | 127 | ||
| 106 | // cette page utilise la même vue que la fonction connexion() dans controller/visiteur.php | ||
| 107 | require('view/password.php'); | 128 | require('view/password.php'); |
| 108 | |||
| 109 | echo($header); | 129 | echo($header); |
| 110 | 130 | ||
| 111 | // traitements: vérification ancien mot de passe et choix du nouveau | 131 | // conformité du nouveau mot de passe |
| 132 | if(isset($_POST['nouveauMotdepasse']) && !empty($_POST['nouveauMotdepasse'])) | ||
| 133 | { | ||
| 134 | require('controller/Security.php'); | ||
| 135 | $newPassword = Security::secureString($_POST['nouveauMotdepasse']); | ||
| 136 | $newPassword = removeSpacesTabsCRLF($_POST['nouveauMotdepasse']); | ||
| 137 | } | ||
| 138 | |||
| 112 | // bon mot de passe | 139 | // bon mot de passe |
| 113 | //if(isset ($_POST["ancienMotdepasse"]) AND $_POST["ancienMotdepasse"] == $secret) | 140 | if(isset($newPassword) && $newPassword === $_POST['nouveauMotdepasse'] |
| 114 | if(isset ($_POST["ancienMotdepasse"]) AND testPassword($_POST["ancienMotdepasse"])) | 141 | && isset ($_POST["ancienMotdepasse"]) AND testPassword($_POST["ancienMotdepasse"])) |
| 115 | { | 142 | { |
| 116 | // enregistrement | 143 | // enregistrement et confirmation |
| 117 | hashNewPassword($_POST["nouveauMotdepasse"]); | 144 | hashNewPassword($_POST["nouveauMotdepasse"]); |
| 118 | 145 | echo($message); | |
| 119 | // confirmation | ||
| 120 | echo($message); | ||
| 121 | //exit(); | ||
| 122 | |||
| 123 | /*header('Location: index.php?page=' . $_GET['from'] . '&message=nouveau_mdp'); | ||
| 124 | exit();*/ | ||
| 125 | } | 146 | } |
| 126 | |||
| 127 | // mauvais mot de passe | 147 | // mauvais mot de passe |
| 128 | elseif(isset ($_POST["ancienMotdepasse"]) AND !testPassword($_POST["ancienMotdepasse"])) | 148 | elseif(isset ($_POST["ancienMotdepasse"]) AND !testPassword($_POST["ancienMotdepasse"])) |
| 129 | { | 149 | { |
| 130 | // défense aux attaques par force brute | 150 | // défense aux attaques par force brute |
| 131 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site | 151 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site |
| 132 | echo($erreurMDP); | 152 | echo($errorPassword); |
| 133 | sleep(1); | 153 | sleep(1); |
| 134 | echo($formulaireModifMDP); | 154 | echo($formulaireModifMDP); |
| 135 | } | 155 | } |
| 136 | 156 | // erreur de conformité | |
| 157 | elseif(isset($newPassword) && $newPassword !== $_POST['nouveauMotdepasse']) | ||
| 158 | { | ||
| 159 | echo($errorBadCharacters); | ||
| 160 | echo($formulaireModifMDP); | ||
| 161 | } | ||
| 137 | // première arrivée sur la page | 162 | // première arrivée sur la page |
| 138 | else | 163 | else |
| 139 | { | 164 | { |
| @@ -146,7 +171,7 @@ function changePassword() | |||
| 146 | 171 | ||
| 147 | 172 | ||
| 148 | // hachage | 173 | // hachage |
| 149 | function hashNewPassword($newPassword) | 174 | function hashNewPassword(string $newPassword) |
| 150 | { | 175 | { |
| 151 | // "réparation" des espaces accidentels | 176 | // "réparation" des espaces accidentels |
| 152 | $newPassword= trim($newPassword); | 177 | $newPassword= trim($newPassword); |
| @@ -160,7 +185,7 @@ function hashNewPassword($newPassword) | |||
| 160 | chmod('data/password.txt', 0600); | 185 | chmod('data/password.txt', 0600); |
| 161 | } | 186 | } |
| 162 | 187 | ||
| 163 | function testPassword($password) | 188 | function testPassword(string $password): bool |
| 164 | { | 189 | { |
| 165 | // lecture | 190 | // lecture |
| 166 | $oldHashedPassword = file_get_contents('data/password.txt'); | 191 | $oldHashedPassword = file_get_contents('data/password.txt'); |
