diff options
| author | polo <ordipolo@gmx.fr> | 2024-02-08 04:31:14 +0100 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2024-02-08 04:31:14 +0100 |
| commit | ccc9a05b758f1dc0313b96807edfc447a9e8d278 (patch) | |
| tree | 114808e506b2bedabdc3ad09770edd7e5eb64e81 /controller | |
| parent | 1ad47a7ca38e679a50c8dfee004db88b1633d7cf (diff) | |
| download | melaine-ccc9a05b758f1dc0313b96807edfc447a9e8d278.tar.gz melaine-ccc9a05b758f1dc0313b96807edfc447a9e8d278.tar.bz2 melaine-ccc9a05b758f1dc0313b96807edfc447a9e8d278.zip | |
RGPD cookie uniquement en mode admin et avertissement, logo journal
Diffstat (limited to 'controller')
| -rw-r--r-- | controller/ajax.php | 15 | ||||
| -rw-r--r-- | controller/backup.php | 8 | ||||
| -rw-r--r-- | controller/ckeditor.php | 7 | ||||
| -rw-r--r-- | controller/config.php | 4 | ||||
| -rw-r--r-- | controller/cookies.php | 66 | ||||
| -rw-r--r-- | controller/installation.php | 15 | ||||
| -rw-r--r-- | controller/password.php | 346 |
7 files changed, 285 insertions, 176 deletions
diff --git a/controller/ajax.php b/controller/ajax.php index 1a4909e..96df9cb 100644 --- a/controller/ajax.php +++ b/controller/ajax.php | |||
| @@ -4,12 +4,12 @@ | |||
| 4 | // traitement des requêtes AJAX | 4 | // traitement des requêtes AJAX |
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | // -> insertion d'une image dans l'éditeur | 7 | // éditeur |
| 8 | // -> insertion d'une image | ||
| 8 | if(isset($_GET['action']) && isset($_GET['page']) && $_GET['action'] == 'upload_image') | 9 | if(isset($_GET['action']) && isset($_GET['page']) && $_GET['action'] == 'upload_image') |
| 9 | { | 10 | { |
| 10 | // sécurité !! | 11 | // sécurité !! |
| 11 | if(!isset($_SESSION['admin']) || $_SESSION['admin'] != 1 | 12 | if(!isset($_SESSION) || !isset($_FILES['upload']) || empty($_FILES['upload'])) |
| 12 | || !isset($_FILES['upload']) || empty($_FILES['upload'])) | ||
| 13 | { | 13 | { |
| 14 | // sans effet? | 14 | // sans effet? |
| 15 | header('Location: index.php?erreur=image_ajax'); | 15 | header('Location: index.php?erreur=image_ajax'); |
| @@ -48,7 +48,7 @@ if(isset($_GET['action']) && isset($_GET['page']) && $_GET['action'] == 'upload_ | |||
| 48 | if(isset($_GET['action']) && $_GET['action'] == 'restauration' | 48 | if(isset($_GET['action']) && $_GET['action'] == 'restauration' |
| 49 | && isset($_GET['file_name']) && isset($_GET['file_size'])) | 49 | && isset($_GET['file_name']) && isset($_GET['file_size'])) |
| 50 | { | 50 | { |
| 51 | if(!isset($_SESSION['admin']) || $_SESSION['admin'] != 1) | 51 | if(!isset($_SESSION)) |
| 52 | { | 52 | { |
| 53 | header('Location: index.php?erreur=file_infos_ajax'); | 53 | header('Location: index.php?erreur=file_infos_ajax'); |
| 54 | } | 54 | } |
| @@ -56,8 +56,6 @@ if(isset($_GET['action']) && $_GET['action'] == 'restauration' | |||
| 56 | { | 56 | { |
| 57 | $_SESSION['fileSize'] = $_GET['file_size']; | 57 | $_SESSION['fileSize'] = $_GET['file_size']; |
| 58 | $_SESSION['fileName'] = $_GET['file_name']; | 58 | $_SESSION['fileName'] = $_GET['file_name']; |
| 59 | //echo("file infos send"); | ||
| 60 | //var_dump($_SESSION['fileName']); | ||
| 61 | exit(); // stop !! | 59 | exit(); // stop !! |
| 62 | } | 60 | } |
| 63 | } | 61 | } |
| @@ -65,8 +63,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'restauration' | |||
| 65 | if(isset($_GET['action']) && $_GET['action'] == 'restauration' | 63 | if(isset($_GET['action']) && $_GET['action'] == 'restauration' |
| 66 | && isset($_GET['chunk_name']) && isset($_FILES['blob'])) | 64 | && isset($_GET['chunk_name']) && isset($_FILES['blob'])) |
| 67 | { | 65 | { |
| 68 | 66 | if(!isset($_SESSION)) | |
| 69 | if(!isset($_SESSION['admin']) || $_SESSION['admin'] != 1) | ||
| 70 | { | 67 | { |
| 71 | header('Location: index.php?erreur=upload_ajax'); | 68 | header('Location: index.php?erreur=upload_ajax'); |
| 72 | } | 69 | } |
| @@ -79,7 +76,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'restauration' | |||
| 79 | } | 76 | } |
| 80 | } | 77 | } |
| 81 | 78 | ||
| 82 | // nettoyage | 79 | // nettoyage systématique |
| 83 | if(!isset($_GET['action']) || $_GET['action'] != 'restauration') | 80 | if(!isset($_GET['action']) || $_GET['action'] != 'restauration') |
| 84 | { | 81 | { |
| 85 | if(isset($_SESSION['fileName'])) | 82 | if(isset($_SESSION['fileName'])) |
diff --git a/controller/backup.php b/controller/backup.php index c10ca21..52f300f 100644 --- a/controller/backup.php +++ b/controller/backup.php | |||
| @@ -10,7 +10,7 @@ function pageSauvegarde($from) | |||
| 10 | $date = date("d-m-Y", time()); | 10 | $date = date("d-m-Y", time()); |
| 11 | $nomFichier = "melaineDATA_" . $date; | 11 | $nomFichier = "melaineDATA_" . $date; |
| 12 | // ne prendre que les dossiers pour exclure les fichiers password.txt, melaineDATA.zip et melainePHP.zip existant | 12 | // ne prendre que les dossiers pour exclure les fichiers password.txt, melaineDATA.zip et melainePHP.zip existant |
| 13 | $dossiersCibles = [ 'data/archives', 'data/musique', 'data/jaime', 'data/presse', 'data/livres', 'data/legal', 'data/discographie', 'data/melaine', 'data/peinture' ]; | 13 | $dossiersCibles = [ 'data/menu', 'data/archives', 'data/musique', 'data/jaime', 'data/presse', 'data/livres', 'data/legal', 'data/discographie', 'data/melaine', 'data/peinture' ]; |
| 14 | //$fichiersALaRacine = []; | 14 | //$fichiersALaRacine = []; |
| 15 | 15 | ||
| 16 | createZip($cheminDestination, $nomFichier, $dossiersCibles); | 16 | createZip($cheminDestination, $nomFichier, $dossiersCibles); |
| @@ -177,7 +177,7 @@ function pageRestauration($from) | |||
| 177 | $message = restoreData($path); | 177 | $message = restoreData($path); |
| 178 | } | 178 | } |
| 179 | 179 | ||
| 180 | // variables obtenues en AJAX | 180 | // variables crées dans ajax.php |
| 181 | if(isset($_SESSION['fileName'])) | 181 | if(isset($_SESSION['fileName'])) |
| 182 | { | 182 | { |
| 183 | unset($_SESSION['fileName']); | 183 | unset($_SESSION['fileName']); |
| @@ -206,8 +206,8 @@ function pageRestauration($from) | |||
| 206 | require('view/backup.php'); | 206 | require('view/backup.php'); |
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | // upload AJAX d'un zip dans file_upload.js | 209 | // upload AJAX d'un zip en morceaux dans file_upload.js |
| 210 | function uploadChunkAndMerge() | 210 | function uploadChunkAndMerge() // appel dans ajax.php |
| 211 | { | 211 | { |
| 212 | // $_GET['chunk_name'] n'est pas utilisé pour l'instant | 212 | // $_GET['chunk_name'] n'est pas utilisé pour l'instant |
| 213 | 213 | ||
diff --git a/controller/ckeditor.php b/controller/ckeditor.php index 1707128..d0c5824 100644 --- a/controller/ckeditor.php +++ b/controller/ckeditor.php | |||
| @@ -18,8 +18,9 @@ function preparationCKeditor($html) | |||
| 18 | header('Location: index.php?erreur=empty_input'); | 18 | header('Location: index.php?erreur=empty_input'); |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | // supprimer espaces, tabulations et sauts de ligne en début et fin de chaine | 21 | // sécuriser le HTML |
| 22 | $html = trim($html); | 22 | require('controller/Security.php'); |
| 23 | $html = Security::secureString($html); | ||
| 23 | 24 | ||
| 24 | // supprimer les sauts de ligne | 25 | // supprimer les sauts de ligne |
| 25 | $sautsDeLigne = array("\n", "\r", "\r\n"); | 26 | $sautsDeLigne = array("\n", "\r", "\r\n"); |
| @@ -58,7 +59,7 @@ function getFileCodeFromHTTPReferrer(): string | |||
| 58 | function submitCKeditor() | 59 | function submitCKeditor() |
| 59 | { | 60 | { |
| 60 | // déjà fait mais on ne sait jamais | 61 | // déjà fait mais on ne sait jamais |
| 61 | if(!isset($_SESSION['admin']) || $_SESSION['admin'] != 1) | 62 | if(!isset($_SESSION)) |
| 62 | { | 63 | { |
| 63 | header('Location: index.php?page=' . $_GET['page'] . '&erreur=connexion'); | 64 | header('Location: index.php?page=' . $_GET['page'] . '&erreur=connexion'); |
| 64 | exit(); | 65 | exit(); |
diff --git a/controller/config.php b/controller/config.php index 6fc7000..804671b 100644 --- a/controller/config.php +++ b/controller/config.php | |||
| @@ -34,7 +34,7 @@ else | |||
| 34 | echo('<script>alert(\'Erreur: Une de ces extensions de PHP est nécessaire: imagemagick ou GD. Veuillez activer une des deux dans le fichier php.ini ou installer le paquet php-imagick ou php-gd.\');</script>'); | 34 | echo('<script>alert(\'Erreur: Une de ces extensions de PHP est nécessaire: imagemagick ou GD. Veuillez activer une des deux dans le fichier php.ini ou installer le paquet php-imagick ou php-gd.\');</script>'); |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | // format dans lequel créer les sauvegardes | 37 | // format dans lequel créer les sauvegardes, |
| 38 | // zip uniquement pour l'instant | 38 | // zip uniquement pour l'instant |
| 39 | if(extension_loaded("zip")) | 39 | if(extension_loaded("zip")) |
| 40 | { | 40 | { |
| @@ -48,7 +48,7 @@ else | |||
| 48 | 48 | ||
| 49 | // conversion des 2M du php.ini en 2000000 | 49 | // conversion des 2M du php.ini en 2000000 |
| 50 | // note: les kibi, mébi et gibi sont inutiles ici | 50 | // note: les kibi, mébi et gibi sont inutiles ici |
| 51 | function returnBytes ($size_str) // chaine du style '2M' | 51 | function returnBytes($size_str) // chaine du style '2M' |
| 52 | { | 52 | { |
| 53 | switch (substr ($size_str, -1)) | 53 | switch (substr ($size_str, -1)) |
| 54 | { | 54 | { |
diff --git a/controller/cookies.php b/controller/cookies.php new file mode 100644 index 0000000..f5c7e64 --- /dev/null +++ b/controller/cookies.php | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | <?php | ||
| 2 | // controller/cookies.php | ||
| 3 | |||
| 4 | // nettoyage du cookie de session de l'ancienne version du site | ||
| 5 | $cookie = 'PHPSESSID'; | ||
| 6 | if(isset($_COOKIE[$cookie])) | ||
| 7 | { | ||
| 8 | deleteCookie($cookie); | ||
| 9 | header("Location: index.php"); | ||
| 10 | exit(); | ||
| 11 | } | ||
| 12 | |||
| 13 | // session_start() | ||
| 14 | $cookie = 'session_admin'; | ||
| 15 | if(isset($_COOKIE[$cookie])) // détection du cookie | ||
| 16 | { | ||
| 17 | if(session_status() === PHP_SESSION_NONE) | ||
| 18 | { | ||
| 19 | session_name($cookie); | ||
| 20 | /*session_set_cookie_params([ | ||
| 21 | 'lifetime' => 0, | ||
| 22 | 'path' => '/', | ||
| 23 | 'domain' => $_SERVER['HTTP_HOST'], | ||
| 24 | 'secure' => true, | ||
| 25 | 'httponly' => true, | ||
| 26 | 'samesite' => 'strict']); | ||
| 27 | session_start();*/ | ||
| 28 | |||
| 29 | /*session_start([ | ||
| 30 | 'cookie_lifetime' => 0, | ||
| 31 | 'cookie_path' => '/', | ||
| 32 | 'cookie_domain' => $_SERVER['HTTP_HOST'], | ||
| 33 | 'cookie_secure' => true, | ||
| 34 | 'cookie_httponly' => true, | ||
| 35 | 'cookie_samesite' => 'strict']);*/ | ||
| 36 | session_start(); | ||
| 37 | |||
| 38 | // le cookie ne semble pas prendre les paramètres! pourquoi? | ||
| 39 | //var_dump(session_get_cookie_params()); | ||
| 40 | } | ||
| 41 | else // cas anormal: session déjà démarrée, mode parano activé! | ||
| 42 | { | ||
| 43 | deleteCookie($cookie); | ||
| 44 | header("Location: index.php"); | ||
| 45 | exit(); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | // erreurs affichées au rechargement (lesquelles déjà?) | ||
| 50 | if(isset($_SESSION['erreur']) && !empty($_SESSION['erreur'])) | ||
| 51 | { | ||
| 52 | echo('<script>alert(\'' . $_SESSION['erreur'] . '\');</script>'); | ||
| 53 | unset($_SESSION['erreur']); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | function deleteCookie(string $name) | ||
| 58 | { | ||
| 59 | unset($_COOKIE[$name]); // utile? | ||
| 60 | setcookie($name, '', time() - 4200, '/'); // suppression | ||
| 61 | |||
| 62 | // cookie supprimé au prochain chargement de la page | ||
| 63 | // forcer un rechargement pour effet immédiat | ||
| 64 | //header("Location: index.php"); | ||
| 65 | //exit(); | ||
| 66 | } | ||
diff --git a/controller/installation.php b/controller/installation.php index f0d2890..90dcd28 100644 --- a/controller/installation.php +++ b/controller/installation.php | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | <?php | 1 | <?php |
| 2 | // controller/installation.php | 2 | // controller/installation.php |
| 3 | // | ||
| 4 | // .htaccess, dépendances et création des dossiers | ||
| 3 | 5 | ||
| 4 | // premier démarrage du site | 6 | // premier démarrage du site |
| 5 | function installation() | 7 | function installation() |
| @@ -23,7 +25,8 @@ function installation() | |||
| 23 | } | 25 | } |
| 24 | 26 | ||
| 25 | // extensions de php | 27 | // extensions de php |
| 26 | // nécessité de 'dom' à confirmer | 28 | // nécessité de "imagick" OU de "gd" (on préfère imagick) |
| 29 | // "dom" n'est pas nécessaire pour l'instant | ||
| 27 | $extensions = ['imagick', 'gd', 'zip', 'mbstring', 'dom']; | 30 | $extensions = ['imagick', 'gd', 'zip', 'mbstring', 'dom']; |
| 28 | foreach($extensions as $extension) | 31 | foreach($extensions as $extension) |
| 29 | { | 32 | { |
| @@ -132,8 +135,14 @@ function installation() | |||
| 132 | createZipMelainePHP(); | 135 | createZipMelainePHP(); |
| 133 | } | 136 | } |
| 134 | 137 | ||
| 135 | // création d'un mot de passe si password.txt est vide | 138 | |
| 136 | existPassword(); | 139 | // mot de passe |
| 140 | if(!file_exists('data/password.txt')) | ||
| 141 | { | ||
| 142 | touch('data/password.txt'); | ||
| 143 | chmod('data/password.txt', 0600); | ||
| 144 | } | ||
| 145 | existPassword(); // création si fichier vide | ||
| 137 | } | 146 | } |
| 138 | 147 | ||
| 139 | function createIndexPHP($path, $droitsFichiers) | 148 | function createIndexPHP($path, $droitsFichiers) |
diff --git a/controller/password.php b/controller/password.php index fa4f2c1..5e556e9 100644 --- a/controller/password.php +++ b/controller/password.php | |||
| @@ -1,27 +1,31 @@ | |||
| 1 | <?php | 1 | <?php |
| 2 | // controller/password.php | 2 | // controller/password.php |
| 3 | // | ||
| 4 | // fichier data/password.txt et test captcha | ||
| 3 | 5 | ||
| 4 | // exécutée dans installation.php à l'ouverture de chaque page | 6 | // exécutée dans installation.php à l'ouverture de chaque page |
| 5 | function existPassword() | 7 | function existPassword() |
| 6 | { | 8 | { |
| 7 | // création du fichier | ||
| 8 | if(!file_exists('data/password.txt')) | ||
| 9 | { | ||
| 10 | touch('data/password.txt'); | ||
| 11 | chmod('data/password.txt', 0600); | ||
| 12 | } | ||
| 13 | |||
| 14 | // lecture | 9 | // lecture |
| 15 | $hashedPassword = file_get_contents('data/password.txt'); | 10 | $file_name = 'data/password.txt'; |
| 11 | $hashedPassword = file_get_contents($file_name); | ||
| 16 | if($hashedPassword === false) | 12 | if($hashedPassword === false) |
| 17 | { | 13 | { |
| 18 | echo('Erreur: ouverture du fichier password.txt impossible.'); | 14 | echo('Erreur: ouverture du fichier ' . $file_name . ' impossible.'); |
| 19 | exit(); | 15 | exit(); |
| 20 | } | 16 | } |
| 21 | 17 | ||
| 18 | $file_name = 'data/tmp/solution.txt'; | ||
| 22 | if(empty($hashedPassword)) | 19 | if(empty($hashedPassword)) |
| 23 | { | 20 | { |
| 21 | unset($_GET); | ||
| 24 | createPassword(); | 22 | createPassword(); |
| 23 | exit(); | ||
| 24 | } | ||
| 25 | // nettoyage | ||
| 26 | elseif(isset($_GET['page']) && $_GET['page'] != 'connexion' && file_exists($file_name)) | ||
| 27 | { | ||
| 28 | unlink($file_name); | ||
| 25 | } | 29 | } |
| 26 | } | 30 | } |
| 27 | 31 | ||
| @@ -90,210 +94,247 @@ function controlCaptchaInput(): int | |||
| 90 | 94 | ||
| 91 | function createPassword() | 95 | function createPassword() |
| 92 | { | 96 | { |
| 93 | // paranoïa? | 97 | // I - traitement |
| 94 | if(isset($_SESSION['admin'])) | 98 | // cas anormal: session déjà existante |
| 99 | global $cookie; // nom du cookie dans cookies.php | ||
| 100 | if(isset($_COOKIE[$cookie])) | ||
| 95 | { | 101 | { |
| 96 | unset($_SESSION['admin']); | 102 | deleteCookie($cookie); // cookie supprimé au rechargement |
| 97 | header("Location: index.php"); | 103 | header("Location: index.php"); |
| 98 | exit(); | 104 | exit(); |
| 99 | } | 105 | } |
| 100 | 106 | ||
| 101 | $captcha = createCaptcha(); | ||
| 102 | |||
| 103 | $title = 'Bienvenue Melaine Favennec'; | ||
| 104 | $subHeading = 'Veuillez choisir le mot de passe que vous utiliserez pour gérer le site.'; | ||
| 105 | require('view/password.php'); | ||
| 106 | |||
| 107 | // contrôle de la saisie | 107 | // contrôle de la saisie |
| 108 | if(isset($_POST['motdepasse']) && !empty($_POST['motdepasse']) && isset($_POST['captcha']) && (int) $_POST['captcha'] == $_SESSION['captcha']) | 108 | $file_name = 'data/tmp/solution.txt'; |
| 109 | $error = ''; | ||
| 110 | if(file_exists($file_name)) | ||
| 109 | { | 111 | { |
| 110 | // caractères non désirés supprimés | 112 | $captcha_solution = file_get_contents($file_name); |
| 111 | // impossible d'entrer un espace ou une tabulation et de valider par erreur | 113 | unlink($file_name); |
| 112 | require('controller/Security.php'); | ||
| 113 | $password = Security::secureString($_POST['motdepasse']); | ||
| 114 | $password = removeSpacesTabsCRLF($_POST['motdepasse']); | ||
| 115 | 114 | ||
| 116 | // enregistrement | 115 | if(empty($_POST)) // page rechargée |
| 117 | if(isset($password) && $password == $_POST['motdepasse']) | 116 | {} |
| 117 | elseif(!isset($_POST['captcha']) || controlCaptchaInput() == 0) | ||
| 118 | { | ||
| 119 | $error = 'error_non_valid_captcha'; | ||
| 120 | } | ||
| 121 | elseif($_POST['captcha'] != $captcha_solution) | ||
| 118 | { | 122 | { |
| 119 | hashNewPassword($_POST['motdepasse']); | 123 | $error = 'bad_solution_captcha'; |
| 120 | unset($_SESSION['captcha']); // nettoyage | 124 | } |
| 121 | header('Location: index.php'); | 125 | elseif(!isset($_POST['motdepasse']) || empty($_POST['motdepasse'])) |
| 126 | { | ||
| 127 | $error = 'bad_password'; | ||
| 122 | } | 128 | } |
| 123 | // mot de passe non valable | ||
| 124 | else | 129 | else |
| 125 | { | 130 | { |
| 126 | sleep(1); | 131 | // -> caractères HTML dangereux supprimés |
| 127 | echo($header); | 132 | // -> empêche validation par erreur d'une chaine "vide" |
| 128 | echo($errorPassword); | 133 | require('controller/Security.php'); |
| 129 | echo($formulaireNouveauMDP); | 134 | $password = Security::secureString($_POST['motdepasse']); |
| 130 | echo($errorBadCharacters); | 135 | $password = removeSpacesTabsCRLF($_POST['motdepasse']); |
| 136 | |||
| 137 | // enregistrement et redirection | ||
| 138 | if(isset($password) && $password == $_POST['motdepasse']) | ||
| 139 | { | ||
| 140 | hashNewPassword($_POST['motdepasse']); | ||
| 141 | header('Location: index.php'); | ||
| 142 | exit(); | ||
| 143 | } | ||
| 144 | else | ||
| 145 | { | ||
| 146 | $error = 'bad_password'; | ||
| 147 | } | ||
| 131 | } | 148 | } |
| 132 | } | 149 | } |
| 133 | // saisie non valide | 150 | else // première fois |
| 134 | elseif(isset($_POST['captcha']) && controlCaptchaInput() == 0) | 151 | {} |
| 135 | { | 152 | |
| 136 | sleep(1); | 153 | // inséré dans $captchaHtml puis dans $formulaireNouveauMDP |
| 137 | echo($header); | 154 | $captcha = createCaptcha(); |
| 138 | echo($errorNonValidCaptcha); | 155 | // enregistrement de la réponse du captcha pour vérification |
| 139 | echo($formulaireNouveauMDP); | 156 | saveSolutionCaptcha($captcha[2]); |
| 140 | echo($errorBadCharacters); | 157 | |
| 141 | } | 158 | |
| 142 | // mauvais captcha | 159 | // II - affichage |
| 143 | elseif(isset($_POST['captcha']) && (int) $_POST['captcha'] != $_SESSION['captcha']) | 160 | $title = 'Bienvenue Melaine Favennec'; |
| 161 | $subHeading = 'Veuillez choisir le mot de passe que vous utiliserez pour gérer le site.'; | ||
| 162 | |||
| 163 | // même vue que la fonction changerMotDePasse() | ||
| 164 | require('view/password.php'); | ||
| 165 | |||
| 166 | echo($header); | ||
| 167 | if($error != '') | ||
| 144 | { | 168 | { |
| 145 | sleep(1); | 169 | sleep(1); |
| 146 | echo($header); | 170 | echo($error_messages[$error]); |
| 147 | echo($errorCaptcha); | ||
| 148 | echo($formulaireNouveauMDP); | ||
| 149 | echo($errorBadCharacters); | ||
| 150 | } | ||
| 151 | // 1ère fois | ||
| 152 | else | ||
| 153 | { | ||
| 154 | echo($header); | ||
| 155 | echo($formulaireNouveauMDP); | ||
| 156 | echo($errorBadCharacters); | ||
| 157 | //echo($warning); // message pas top | ||
| 158 | } | 171 | } |
| 172 | echo($formulaireNouveauMDP); | ||
| 173 | echo($error_messages['forbidden_characters']); | ||
| 159 | echo($footer); | 174 | echo($footer); |
| 160 | |||
| 161 | // fois suivante (dispense de nettoyer la variable) | ||
| 162 | $_SESSION['captcha'] = $captcha[2]; | ||
| 163 | exit(); | ||
| 164 | } | 175 | } |
| 165 | 176 | ||
| 166 | 177 | ||
| 167 | function connect() | 178 | function connect() |
| 168 | { | 179 | { |
| 180 | // I - traitement | ||
| 169 | // déjà en mode admin | 181 | // déjà en mode admin |
| 170 | if($_SESSION['admin'] == 1) | 182 | global $cookie; // nom du cookie dans cookies.php |
| 183 | if(isset($_COOKIE[$cookie])) | ||
| 171 | { | 184 | { |
| 172 | header('Location: index.php?page=' . $_GET['from']); | 185 | header('Location: index.php?page=' . $_GET['from']); |
| 173 | exit(); | 186 | exit(); |
| 174 | } | 187 | } |
| 175 | 188 | ||
| 176 | // Ajouter une sécurité par cpatcha avec un "input" supplémentaire | 189 | // contrôle de la saisie |
| 190 | $file_name = 'data/tmp/solution.txt'; | ||
| 191 | $error = ''; | ||
| 192 | if(file_exists($file_name)) | ||
| 193 | { | ||
| 194 | $captcha_solution = file_get_contents($file_name); | ||
| 195 | unlink($file_name); | ||
| 196 | |||
| 197 | if(empty($_POST)) // page rechargée | ||
| 198 | {} | ||
| 199 | elseif(!isset($_POST['captcha']) || controlCaptchaInput() == 0) | ||
| 200 | { | ||
| 201 | $error = 'error_non_valid_captcha'; | ||
| 202 | } | ||
| 203 | elseif($_POST['captcha'] != $captcha_solution) | ||
| 204 | { | ||
| 205 | $error = 'bad_solution_captcha'; | ||
| 206 | } | ||
| 207 | elseif(!isset($_POST['motdepasse']) || empty($_POST['motdepasse'])) | ||
| 208 | { | ||
| 209 | $error = 'bad_password'; | ||
| 210 | } | ||
| 211 | else | ||
| 212 | { | ||
| 213 | // enregistrement et redirection | ||
| 214 | if(testPassword($_POST["motdepasse"])) | ||
| 215 | { | ||
| 216 | session_name($cookie); | ||
| 217 | session_start(); | ||
| 218 | header('Location: index.php?page=' . $_GET['from']); | ||
| 219 | exit(); | ||
| 220 | } | ||
| 221 | else | ||
| 222 | { | ||
| 223 | $error = 'bad_password'; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | else // première fois | ||
| 228 | {} | ||
| 229 | |||
| 230 | // inséré dans $captchaHtml puis dans $formulaireNouveauMDP | ||
| 177 | $captcha = createCaptcha(); | 231 | $captcha = createCaptcha(); |
| 178 | // Et créer une variable de session pour la réponse au CAPTCHA | 232 | // enregistrement de la réponse du captcha pour vérification |
| 233 | saveSolutionCaptcha($captcha[2]); | ||
| 234 | |||
| 179 | 235 | ||
| 236 | // II - affichage | ||
| 180 | $title = "Connexion"; | 237 | $title = "Connexion"; |
| 181 | $subHeading = "Veuillez saisir votre mot de passe pour pouvoir apporter des modifications au site."; | 238 | $subHeading = "Veuillez saisir votre mot de passe pour pouvoir apporter des modifications au site."; |
| 182 | 239 | // même vue que la fonction changerMotDePasse() | |
| 183 | // cette page utilise la même vue que la fonction changerMotDePasse() dans controller/admin.php | ||
| 184 | require('view/password.php'); | 240 | require('view/password.php'); |
| 185 | 241 | ||
| 186 | echo($header); | 242 | echo($header); |
| 187 | 243 | //echo($warning_messages['message_disconnect']); | |
| 188 | // bon codes (mot de passe et captcha) | 244 | if($error != '') |
| 189 | if(isset ($_POST["motdepasse"]) && testPassword($_POST["motdepasse"]) && isset($_POST['captcha']) && (int) $_POST['captcha'] == $_SESSION['captcha']) | ||
| 190 | { | ||
| 191 | $_SESSION['admin'] = 1; | ||
| 192 | unset($_SESSION['captcha']); // nettoyage | ||
| 193 | header('Location: index.php?page=' . $_GET['from']); | ||
| 194 | exit(); | ||
| 195 | } | ||
| 196 | // saisie non valide | ||
| 197 | elseif(isset($_POST['captcha']) && controlCaptchaInput() == 0) | ||
| 198 | { | ||
| 199 | echo($errorNonValidCaptcha); | ||
| 200 | sleep(1); | ||
| 201 | echo($formulaireConnexion); | ||
| 202 | } | ||
| 203 | // mauvais captcha | ||
| 204 | elseif(isset($_POST['captcha']) && (int) $_POST['captcha'] != $_SESSION['captcha']) | ||
| 205 | { | 245 | { |
| 206 | echo($errorCaptcha); | ||
| 207 | sleep(1); | 246 | sleep(1); |
| 208 | echo($formulaireConnexion); | 247 | echo($error_messages[$error]); |
| 209 | } | 248 | } |
| 249 | echo($formulaireConnexion); | ||
| 250 | echo($warning_messages['message_cookie']); | ||
| 251 | echo($warning_messages['private_browsing']); | ||
| 252 | echo($footer); | ||
| 253 | } | ||
| 210 | 254 | ||
| 211 | // mauvais codes | ||
| 212 | elseif(isset ($_POST["motdepasse"]) && !testPassword($_POST["motdepasse"])) | ||
| 213 | { | ||
| 214 | // défense aux attaques par force brute | ||
| 215 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site | ||
| 216 | echo($errorPassword); | ||
| 217 | sleep(1); | ||
| 218 | echo($formulaireConnexion); | ||
| 219 | } | ||
| 220 | 255 | ||
| 221 | // première arrivée sur la page | 256 | function saveSolutionCaptcha(string $solution) |
| 222 | else | 257 | { |
| 223 | { | 258 | $file_name = 'data/tmp/solution.txt'; |
| 224 | echo($formulaireConnexion); | 259 | if(!file_exists($file_name)) |
| 225 | } | 260 | { |
| 226 | 261 | touch($file_name); | |
| 227 | // fois suivante (dispense de nettoyer la variable) | 262 | chmod($file_name, 0600); |
| 228 | $_SESSION['captcha'] = $captcha[2]; | 263 | } |
| 229 | 264 | file_put_contents($file_name, $solution); | |
| 230 | echo($messageDeconnect); | ||
| 231 | echo($footer); | ||
| 232 | } | 265 | } |
| 233 | 266 | ||
| 267 | |||
| 234 | function changePassword() | 268 | function changePassword() |
| 235 | { | 269 | { |
| 270 | // I - traitement | ||
| 236 | // vérification supplémentaire | 271 | // vérification supplémentaire |
| 237 | if($_SESSION['admin'] !== 1) | 272 | global $cookie; // nom du cookie dans cookies.php |
| 273 | if(!isset($_SESSION)) | ||
| 238 | { | 274 | { |
| 239 | $_SESSION['admin'] = 0; | 275 | deleteCookie($cookie); |
| 240 | header('Location: index.php?page=' . $_GET['from']); | 276 | header('Location: index.php?page=' . $_GET['from']); |
| 241 | exit(); | 277 | exit(); |
| 242 | } | 278 | } |
| 243 | 279 | ||
| 244 | $title = "Nouveau mot de passe"; | 280 | // contrôle de la saisie |
| 245 | $subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau."; | 281 | $error = ''; |
| 246 | 282 | $success = false; | |
| 247 | require('view/password.php'); | 283 | if(empty($_POST)) // première fois ou page rechargée |
| 248 | echo($header); | 284 | {} |
| 249 | 285 | elseif(!isset($_POST['nouveauMotdepasse']) || empty($_POST['nouveauMotdepasse'])|| !isset($_POST['ancienMotdepasse']) || empty($_POST['ancienMotdepasse'])) | |
| 250 | // conformité du nouveau mot de passe | 286 | { |
| 251 | if(isset($_POST['nouveauMotdepasse']) && !empty($_POST['nouveauMotdepasse'])) | 287 | $error = 'bad_password'; |
| 288 | } | ||
| 289 | else | ||
| 252 | { | 290 | { |
| 253 | require('controller/Security.php'); | 291 | require('controller/Security.php'); |
| 254 | $newPassword = Security::secureString($_POST['nouveauMotdepasse']); | 292 | $newPassword = Security::secureString($_POST['nouveauMotdepasse']); |
| 255 | $newPassword = removeSpacesTabsCRLF($_POST['nouveauMotdepasse']); | 293 | $newPassword = removeSpacesTabsCRLF($_POST['nouveauMotdepasse']); |
| 256 | } | ||
| 257 | 294 | ||
| 258 | // bon mot de passe | 295 | if(isset($newPassword) && $newPassword !== $_POST['nouveauMotdepasse']) // erreur de conformité |
| 259 | if(isset($newPassword) && $newPassword === $_POST['nouveauMotdepasse'] | 296 | { |
| 260 | && isset ($_POST["ancienMotdepasse"]) && testPassword($_POST["ancienMotdepasse"])) | 297 | $error = 'forbidden_characters'; |
| 261 | { | 298 | } |
| 262 | // enregistrement et confirmation | 299 | else |
| 263 | hashNewPassword($_POST["nouveauMotdepasse"]); | 300 | { |
| 264 | echo($message); | 301 | if(testPassword($_POST["ancienMotdepasse"])) |
| 302 | { | ||
| 303 | // enregistrement et confirmation | ||
| 304 | hashNewPassword($_POST["nouveauMotdepasse"]); | ||
| 305 | $success = true; | ||
| 306 | } | ||
| 307 | else | ||
| 308 | { | ||
| 309 | $error = 'bad_password'; | ||
| 310 | } | ||
| 311 | } | ||
| 265 | } | 312 | } |
| 266 | // mauvais mot de passe | 313 | |
| 267 | elseif(isset ($_POST["ancienMotdepasse"]) && !testPassword($_POST["ancienMotdepasse"])) | 314 | |
| 315 | // II - affichage | ||
| 316 | $title = "Nouveau mot de passe"; | ||
| 317 | $subHeading = "Veuillez saisir votre actuel mot de passe suivi du nouveau."; | ||
| 318 | |||
| 319 | require('view/password.php'); | ||
| 320 | |||
| 321 | echo($header); | ||
| 322 | if($error != '') | ||
| 268 | { | 323 | { |
| 269 | // défense aux attaques par force brute | ||
| 270 | // pas parfait, ne marche pas si l'attaquant multiplie les connexions au site | ||
| 271 | echo($errorPassword); | ||
| 272 | sleep(1); | 324 | sleep(1); |
| 273 | echo($formulaireModifMDP); | 325 | echo($error_messages[$error]); |
| 274 | } | 326 | } |
| 275 | // erreur de conformité | 327 | elseif($success) |
| 276 | elseif(isset($newPassword) && $newPassword !== $_POST['nouveauMotdepasse']) | ||
| 277 | { | 328 | { |
| 278 | echo($errorBadCharacters); | 329 | echo($alertJSNewPassword); |
| 279 | echo($formulaireModifMDP); | ||
| 280 | } | 330 | } |
| 281 | // première arrivée sur la page | 331 | echo($formulaireModifMDP); |
| 282 | else | ||
| 283 | { | ||
| 284 | echo($formulaireModifMDP); | ||
| 285 | } | ||
| 286 | |||
| 287 | //echo($warning); | ||
| 288 | echo($footer); | 332 | echo($footer); |
| 289 | } | 333 | } |
| 290 | 334 | ||
| 291 | 335 | ||
| 292 | // hachage | ||
| 293 | function hashNewPassword(string $newPassword) | 336 | function hashNewPassword(string $newPassword) |
| 294 | { | 337 | { |
| 295 | // "réparation" des espaces accidentels | ||
| 296 | $newPassword= trim($newPassword); | ||
| 297 | // hachage | 338 | // hachage |
| 298 | $newHashedPassword = password_hash($newPassword, PASSWORD_DEFAULT); | 339 | $newHashedPassword = password_hash($newPassword, PASSWORD_DEFAULT); |
| 299 | 340 | ||
| @@ -306,14 +347,9 @@ function hashNewPassword(string $newPassword) | |||
| 306 | 347 | ||
| 307 | function testPassword(string $password): bool | 348 | function testPassword(string $password): bool |
| 308 | { | 349 | { |
| 309 | // lecture | 350 | $hashedPassword = file_get_contents('data/password.txt'); |
| 310 | $oldHashedPassword = file_get_contents('data/password.txt'); | ||
| 311 | |||
| 312 | // "réparation" des espaces accidentels | ||
| 313 | $password= trim($password); | ||
| 314 | $oldHashedPassword = trim($oldHashedPassword); | ||
| 315 | 351 | ||
| 316 | if(password_verify($password, $oldHashedPassword)) | 352 | if(password_verify($password, $hashedPassword)) |
| 317 | { | 353 | { |
| 318 | return true; | 354 | return true; |
| 319 | } | 355 | } |
