diff options
| author | polo <ordipolo@gmx.fr> | 2022-07-26 01:47:53 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2022-07-26 01:47:53 +0200 |
| commit | 8681ecc6f83507861899a8bcae21850dfc5e61d2 (patch) | |
| tree | 4414dafb2be493125b57437af07041ca2814b7b1 /controller | |
| parent | 251a6926a1a09e4cc9da1b4f91be7c3a8283bf3f (diff) | |
| download | melaine-8681ecc6f83507861899a8bcae21850dfc5e61d2.tar.gz melaine-8681ecc6f83507861899a8bcae21850dfc5e61d2.tar.bz2 melaine-8681ecc6f83507861899a8bcae21850dfc5e61d2.zip | |
captcha
Diffstat (limited to 'controller')
| -rw-r--r-- | controller/Security.php | 3 | ||||
| -rw-r--r-- | controller/installation.php | 2 | ||||
| -rw-r--r-- | controller/password.php | 148 | ||||
| -rw-r--r-- | controller/visitor.php | 1 |
4 files changed, 114 insertions, 40 deletions
diff --git a/controller/Security.php b/controller/Security.php index ac1149e..c4724de 100644 --- a/controller/Security.php +++ b/controller/Security.php | |||
| @@ -39,7 +39,8 @@ function fixLinks($data) | |||
| 39 | // regex pour détecter les balises <a> et ajouter http:// au début des liens si nécessaire | 39 | // regex pour détecter les balises <a> et ajouter http:// au début des liens si nécessaire |
| 40 | $pattern = '#(<a.*href=")((?!https?://).*)(".*>)#'; | 40 | $pattern = '#(<a.*href=")((?!https?://).*)(".*>)#'; |
| 41 | $remplacement = '$1http://$2$3'; | 41 | $remplacement = '$1http://$2$3'; |
| 42 | // le "while" est ici parce que preg_replace s'arrête après avec trouvé et modifié un pattern, pourquoi?? | 42 | // le "while" est ici parce que preg_replace s'arrête après avec matché une fois |
| 43 | // plus exactement, le .* à la fin fait que la chaine détectée va jusqu'à la fin des données (à corriger peut-être avec </a>) | ||
| 43 | while(preg_match($pattern, $data)) | 44 | while(preg_match($pattern, $data)) |
| 44 | { | 45 | { |
| 45 | $data = preg_replace($pattern, $remplacement, $data); | 46 | $data = preg_replace($pattern, $remplacement, $data); |
diff --git a/controller/installation.php b/controller/installation.php index e99a06e..5aa5ac9 100644 --- a/controller/installation.php +++ b/controller/installation.php | |||
| @@ -117,7 +117,7 @@ function installation() | |||
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | // création d'un mot de passe si password.txt est vide | 119 | // création d'un mot de passe si password.txt est vide |
| 120 | createPassword(); | 120 | existPassword(); |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | function createIndexPHP($path, $droitsFichiers) | 123 | function createIndexPHP($path, $droitsFichiers) |
diff --git a/controller/password.php b/controller/password.php index e739363..e01e4dd 100644 --- a/controller/password.php +++ b/controller/password.php | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | <?php | 1 | <?php |
| 2 | // controller/password.php | 2 | // controller/password.php |
| 3 | 3 | ||
| 4 | // affichage | 4 | // fonction exécutée à l'ouverture de chaque page |
| 5 | function createPassword() | 5 | function existPassword() |
| 6 | { | 6 | { |
| 7 | // création du fichier | 7 | // création du fichier |
| 8 | if(!file_exists('data/password.txt')) | 8 | if(!file_exists('data/password.txt')) |
| @@ -18,50 +18,110 @@ function createPassword() | |||
| 18 | echo('Erreur: ouverture du fichier password.txt impossible.'); | 18 | echo('Erreur: ouverture du fichier password.txt impossible.'); |
| 19 | exit(); | 19 | exit(); |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | // création du mot de passe | ||
| 23 | if(empty($hashedPassword)) | 22 | if(empty($hashedPassword)) |
| 24 | { | 23 | { |
| 25 | // paranoïa? | 24 | createPassword(); |
| 26 | if(isset($_SESSION['admin'])) | 25 | } |
| 27 | { | 26 | } |
| 28 | unset($_SESSION['admin']); | 27 | |
| 29 | header("Location: index.php"); | 28 | |
| 30 | exit(); | 29 | function createCaptcha(): array |
| 31 | } | 30 | { |
| 31 | $a = rand(2, 10); | ||
| 32 | $b = rand(2, 10); | ||
| 33 | return array(toLettersFrench($a), toLettersFrench($b), $a * $b); | ||
| 34 | } | ||
| 35 | |||
| 36 | function toLettersFrench(int $number): string | ||
| 37 | { | ||
| 38 | return match($number) | ||
| 39 | { | ||
| 40 | 2 => 'deux', | ||
| 41 | 3 => 'trois', | ||
| 42 | 4 => 'quatre', | ||
| 43 | 5 => 'cinq', | ||
| 44 | 6 => 'six', | ||
| 45 | 7 => 'sept', | ||
| 46 | 8 => 'huit', | ||
| 47 | 9 => 'neuf', | ||
| 48 | 10 => 'dix', | ||
| 49 | }; | ||
| 50 | } | ||
| 32 | 51 | ||
| 33 | // au rechargement après saisi | 52 | // vérifier qu'on a que des chiffres |
| 53 | function controlCaptchaInput() | ||
| 54 | { | ||
| 55 | //$_POST['captcha'] | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | function createPassword() | ||
| 60 | { | ||
| 61 | // paranoïa? | ||
| 62 | if(isset($_SESSION['admin'])) | ||
| 63 | { | ||
| 64 | unset($_SESSION['admin']); | ||
| 65 | header("Location: index.php"); | ||
| 66 | exit(); | ||
| 67 | } | ||
| 68 | |||
| 69 | $captcha = createCaptcha(); | ||
| 70 | require('view/password.php'); | ||
| 71 | |||
| 72 | $title = 'Bienvenue Melaine Favennec'; | ||
| 73 | $subHeading = 'Veuillez choisir le mot de passe que vous utiliserez pour gérer le site.'; | ||
| 74 | |||
| 75 | // au rechargement après saisi | ||
| 76 | if(isset($_POST['motdepasse']) && !empty($_POST['motdepasse']) && isset($_POST['captcha']) && (int) $_POST['captcha'] == $_SESSION['captcha']) | ||
| 77 | { | ||
| 78 | // caractères non désirés supprimés | ||
| 34 | // impossible d'entrer un espace ou une tabulation et de valider par erreur | 79 | // impossible d'entrer un espace ou une tabulation et de valider par erreur |
| 35 | if(isset($_POST['motdepasse']) && !empty($_POST['motdepasse'])) | 80 | require('controller/Security.php'); |
| 36 | { | 81 | $password = Security::secureString($_POST['motdepasse']); |
| 37 | // caractères non désirés supprimés | 82 | $password = removeSpacesTabsCRLF($_POST['motdepasse']); |
| 38 | require('controller/Security.php'); | 83 | |
| 39 | $password = Security::secureString($_POST['motdepasse']); | ||
| 40 | $password = removeSpacesTabsCRLF($_POST['motdepasse']); | ||
| 41 | } | ||
| 42 | // enregistrement | 84 | // enregistrement |
| 43 | if(isset($password) && $password == $_POST['motdepasse']) | 85 | if(isset($password) && $password == $_POST['motdepasse']) |
| 44 | { | 86 | { |
| 45 | hashNewPassword($_POST['motdepasse']); | 87 | hashNewPassword($_POST['motdepasse']); |
| 88 | unset($_SESSION['captcha']); // nettoyage | ||
| 46 | header('Location: index.php'); | 89 | header('Location: index.php'); |
| 47 | } | 90 | } |
| 48 | // 1ère fois | 91 | // mot de passe non valable |
| 49 | else | 92 | else |
| 50 | { | 93 | { |
| 51 | $title = 'Créer un mot de passe'; | 94 | sleep(1); |
| 52 | $subHeading = 'Veuillez choisir le mot de passe que vous utiliserez pour gérer le site.'; | ||
| 53 | |||
| 54 | require('view/password.php'); | ||
| 55 | |||
| 56 | echo($header); | 95 | echo($header); |
| 57 | echo($errorBadCharacters); | 96 | echo($errorPassword); |
| 58 | echo($formulaireNouveauMDP); | 97 | echo($formulaireNouveauMDP); |
| 59 | echo($warning); | 98 | echo($errorBadCharacters); |
| 60 | } | 99 | } |
| 61 | exit(); | ||
| 62 | } | 100 | } |
| 101 | // mauvais captcha | ||
| 102 | elseif(isset($_POST['captcha']) && (int) $_POST['captcha'] != $_SESSION['captcha']) | ||
| 103 | { | ||
| 104 | sleep(1); | ||
| 105 | echo($header); | ||
| 106 | echo($errorCaptcha); | ||
| 107 | echo($formulaireNouveauMDP); | ||
| 108 | echo($errorBadCharacters); | ||
| 109 | } | ||
| 110 | // 1ère fois | ||
| 111 | else | ||
| 112 | { | ||
| 113 | echo($header); | ||
| 114 | echo($formulaireNouveauMDP); | ||
| 115 | echo($errorBadCharacters); | ||
| 116 | //echo($warning); // message pas top | ||
| 117 | } | ||
| 118 | |||
| 119 | // fois suivante (dispense de nettoyer la variable) | ||
| 120 | $_SESSION['captcha'] = $captcha[2]; | ||
| 121 | exit(); | ||
| 63 | } | 122 | } |
| 64 | 123 | ||
| 124 | |||
| 65 | function connect() | 125 | function connect() |
| 66 | { | 126 | { |
| 67 | // déjà en mode admin | 127 | // déjà en mode admin |
| @@ -72,6 +132,7 @@ function connect() | |||
| 72 | } | 132 | } |
| 73 | 133 | ||
| 74 | // Ajouter une sécurité par cpatcha avec un "input" supplémentaire | 134 | // Ajouter une sécurité par cpatcha avec un "input" supplémentaire |
| 135 | $captcha = createCaptcha(); | ||
| 75 | // Et créer une variable de session pour la réponse au CAPTCHA | 136 | // Et créer une variable de session pour la réponse au CAPTCHA |
| 76 | 137 | ||
| 77 | $title = "Connexion"; | 138 | $title = "Connexion"; |
| @@ -82,20 +143,29 @@ function connect() | |||
| 82 | 143 | ||
| 83 | echo($header); | 144 | echo($header); |
| 84 | 145 | ||
| 85 | // bon mot de passe | 146 | // bon codes (mot de passe et captcha) |
| 86 | if(isset ($_POST["motdepasse"]) AND testPassword($_POST["motdepasse"])) | 147 | if(isset ($_POST["motdepasse"]) && testPassword($_POST["motdepasse"]) && isset($_POST['captcha']) && (int) $_POST['captcha'] == $_SESSION['captcha']) |
| 87 | { | 148 | { |
| 88 | $_SESSION['admin'] = 1; | 149 | $_SESSION['admin'] = 1; |
| 150 | unset($_SESSION['captcha']); // nettoyage | ||
| 89 | header('Location: index.php?page=' . $_GET['from']); | 151 | header('Location: index.php?page=' . $_GET['from']); |
| 90 | exit(); | 152 | exit(); |
| 91 | } | 153 | } |
| 92 | 154 | ||
| 93 | // mauvais mot de passe | 155 | // mauvais captcha |
| 94 | elseif(isset ($_POST["motdepasse"]) AND !testPassword($_POST["motdepasse"])) | 156 | elseif(isset($_POST['captcha']) && (int) $_POST['captcha'] != $_SESSION['captcha']) |
| 157 | { | ||
| 158 | echo($errorCaptcha); | ||
| 159 | sleep(1); | ||
| 160 | echo($formulaireConnexion); | ||
| 161 | } | ||
| 162 | |||
| 163 | // mauvais codes | ||
| 164 | elseif(isset ($_POST["motdepasse"]) && !testPassword($_POST["motdepasse"])) | ||
| 95 | { | 165 | { |
| 96 | // défense aux attaques par force brute | 166 | // défense aux attaques par force brute |
| 97 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site | 167 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site |
| 98 | echo($erreurMDP); | 168 | echo($errorPassword); |
| 99 | sleep(1); | 169 | sleep(1); |
| 100 | echo($formulaireConnexion); | 170 | echo($formulaireConnexion); |
| 101 | } | 171 | } |
| @@ -106,6 +176,10 @@ function connect() | |||
| 106 | echo($formulaireConnexion); | 176 | echo($formulaireConnexion); |
| 107 | } | 177 | } |
| 108 | 178 | ||
| 179 | // fois suivante (dispense de nettoyer la variable) | ||
| 180 | $_SESSION['captcha'] = $captcha[2]; | ||
| 181 | |||
| 182 | echo($messageDeconnect); | ||
| 109 | echo($footer); | 183 | echo($footer); |
| 110 | } | 184 | } |
| 111 | 185 | ||
| @@ -119,8 +193,6 @@ function changePassword() | |||
| 119 | exit(); | 193 | exit(); |
| 120 | } | 194 | } |
| 121 | 195 | ||
| 122 | // Ajouter une sécurité par cpatcha avec un "input" supplémentaire | ||
| 123 | // Et créer une variable de session pour la réponse au CAPTCHA | ||
| 124 | $title = "Nouveau mot de passe"; | 196 | $title = "Nouveau mot de passe"; |
| 125 | $subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau."; | 197 | $subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau."; |
| 126 | 198 | ||
| @@ -137,14 +209,14 @@ function changePassword() | |||
| 137 | 209 | ||
| 138 | // bon mot de passe | 210 | // bon mot de passe |
| 139 | if(isset($newPassword) && $newPassword === $_POST['nouveauMotdepasse'] | 211 | if(isset($newPassword) && $newPassword === $_POST['nouveauMotdepasse'] |
| 140 | && isset ($_POST["ancienMotdepasse"]) AND testPassword($_POST["ancienMotdepasse"])) | 212 | && isset ($_POST["ancienMotdepasse"]) && testPassword($_POST["ancienMotdepasse"])) |
| 141 | { | 213 | { |
| 142 | // enregistrement et confirmation | 214 | // enregistrement et confirmation |
| 143 | hashNewPassword($_POST["nouveauMotdepasse"]); | 215 | hashNewPassword($_POST["nouveauMotdepasse"]); |
| 144 | echo($message); | 216 | echo($message); |
| 145 | } | 217 | } |
| 146 | // mauvais mot de passe | 218 | // mauvais mot de passe |
| 147 | elseif(isset ($_POST["ancienMotdepasse"]) AND !testPassword($_POST["ancienMotdepasse"])) | 219 | elseif(isset ($_POST["ancienMotdepasse"]) && !testPassword($_POST["ancienMotdepasse"])) |
| 148 | { | 220 | { |
| 149 | // défense aux attaques par force brute | 221 | // défense aux attaques par force brute |
| 150 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site | 222 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site |
| @@ -164,7 +236,7 @@ function changePassword() | |||
| 164 | echo($formulaireModifMDP); | 236 | echo($formulaireModifMDP); |
| 165 | } | 237 | } |
| 166 | 238 | ||
| 167 | echo($warning); | 239 | //echo($warning); |
| 168 | echo($footer); | 240 | echo($footer); |
| 169 | } | 241 | } |
| 170 | 242 | ||
diff --git a/controller/visitor.php b/controller/visitor.php index c561097..e0cd7fc 100644 --- a/controller/visitor.php +++ b/controller/visitor.php | |||
| @@ -33,6 +33,7 @@ function pageArticlesSimplesVisitor(string $page, string $title, string $headerP | |||
| 33 | // données dans $Articles->fileList['content'] | 33 | // données dans $Articles->fileList['content'] |
| 34 | $Articles->readAll(); | 34 | $Articles->readAll(); |
| 35 | $Articles->fileList = array_reverse($Articles->fileList); | 35 | $Articles->fileList = array_reverse($Articles->fileList); |
| 36 | //var_dump($Articles->fileList[0]); | ||
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | // Assemblage | 39 | // Assemblage |
