diff options
| author | polo <ordipolo@gmx.fr> | 2022-02-28 02:15:31 +0100 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2022-02-28 02:15:31 +0100 |
| commit | abd968a1c573b1862bd0728f0b7b6a88e307900a (patch) | |
| tree | ef985fe5757b4516c199a06eb37a5e22f7c5b259 /controller | |
| parent | f19db76cc3b21785619ffd24bfb34692a3aaa66a (diff) | |
| download | melaine-abd968a1c573b1862bd0728f0b7b6a88e307900a.tar.gz melaine-abd968a1c573b1862bd0728f0b7b6a88e307900a.tar.bz2 melaine-abd968a1c573b1862bd0728f0b7b6a88e307900a.zip | |
sauvegarde zip
Diffstat (limited to 'controller')
| -rw-r--r-- | controller/admin.php | 3 | ||||
| -rw-r--r-- | controller/backup.php | 188 | ||||
| -rw-r--r-- | controller/visitor.php | 38 |
3 files changed, 156 insertions, 73 deletions
diff --git a/controller/admin.php b/controller/admin.php index 50f15e2..b8ef7ff 100644 --- a/controller/admin.php +++ b/controller/admin.php | |||
| @@ -191,7 +191,8 @@ function discoEdit($fileCode, $suppression) | |||
| 191 | { | 191 | { |
| 192 | if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) | 192 | if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) |
| 193 | { | 193 | { |
| 194 | $lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode']; | 194 | /*$lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode'];*/ |
| 195 | $lienAlbum[$i] = 'page=album&file_code=' . $Albums->fileList[$i]['fileCode']; | ||
| 195 | $avecLien[$i] = true; | 196 | $avecLien[$i] = true; |
| 196 | $linkDiscoChrono[$i] = 'linkChrono'; // css | 197 | $linkDiscoChrono[$i] = 'linkChrono'; // css |
| 197 | } | 198 | } |
diff --git a/controller/backup.php b/controller/backup.php index 13b68df..81d72b9 100644 --- a/controller/backup.php +++ b/controller/backup.php | |||
| @@ -1,84 +1,64 @@ | |||
| 1 | <?php | 1 | <?php |
| 2 | // controller/backup.php | 2 | // controller/backup.php |
| 3 | 3 | ||
| 4 | // note: faire que cette fonction soit exécutée automatiquement de temps en temps | ||
| 4 | function sauvegarder($from) | 5 | function sauvegarder($from) |
| 5 | { | 6 | { |
| 6 | $title = 'extraction des données'; | 7 | $title = 'extraction des données'; |
| 7 | 8 | ||
| 8 | $chemin = "data/"; | 9 | $cheminDestination = "data/"; |
| 9 | $nomFichier = "melaineDATA.zip"; | 10 | $date = date("d-m-Y", time()); |
| 11 | $nomFichier = "melaineDATA_" . $date . ".zip"; | ||
| 12 | // ne prendre que les dossiers pour exclure les fichiers password.txt, melaineDATA.zip et melainePHP.zip existant | ||
| 13 | $dossiersCibles = [ 'data/archives', 'data/concerts', 'data/liens', 'data/presse', 'data/ateliers', 'data/discographie', 'data/melaine', 'data/peinture' ]; | ||
| 14 | //$fichiersALaRacine = []; | ||
| 10 | 15 | ||
| 11 | // création ou remplacement d'une archive zip | 16 | createZip($cheminDestination, $nomFichier, $dossiersCibles); |
| 12 | // une erreur est jetée si l'extension zip n'est pas activée | ||
| 13 | try | ||
| 14 | { | ||
| 15 | $zip = new ZipArchive(); | ||
| 16 | } | ||
| 17 | catch (Throwable $e) | ||
| 18 | { | ||
| 19 | echo($erreur); | ||
| 20 | die(); | ||
| 21 | } | ||
| 22 | |||
| 23 | // création du fichier zip vide | ||
| 24 | // j'ai utilisé l'option ZipArchive::OVERWRITE, | ||
| 25 | // ZipArchive::FL_OVERWRITE n'apparaît qu'avec PHP 8 | ||
| 26 | if ($zip->open($chemin . $nomFichier, ZipArchive::CREATE | ZipArchive::OVERWRITE)!==TRUE) { | ||
| 27 | exit("Impossible d'ouvrir le fichier <i>" . $chemin . $nomFichier . "</i>.\n"); | ||
| 28 | } | ||
| 29 | |||
| 30 | // ajout des fichiers | ||
| 31 | // chemin complet = data/"pages"/"html ou images"/"tous les fichiers" | ||
| 32 | $zip->addGlob($chemin . '*/*/*', 0, array('')); | ||
| 33 | $zip->close(); | ||
| 34 | |||
| 35 | // pour pouvoir manipuler le fichier depuis filezilla ou ssh | ||
| 36 | chmod($chemin . $nomFichier, 0666); // écriture 4 chiffres | ||
| 37 | |||
| 38 | require('view/backup.php'); | ||
| 39 | } | ||
| 40 | |||
| 41 | function restaurer($from) | ||
| 42 | { | ||
| 43 | $title = 'Restauration des données'; | ||
| 44 | |||
| 45 | $chemin = "data/"; | ||
| 46 | $nomFichier = "melaineDATA.zip"; | ||
| 47 | 17 | ||
| 48 | require('view/backup.php'); | 18 | require('view/backup.php'); |
| 49 | } | 19 | } |
| 50 | 20 | ||
| 51 | // inutile | ||
| 52 | function creerMelainePHP() | 21 | function creerMelainePHP() |
| 53 | { | 22 | { |
| 23 | $cheminDestination = 'data/'; | ||
| 54 | $nomFichier = "melainePHP.zip"; | 24 | $nomFichier = "melainePHP.zip"; |
| 25 | // tous les dossiers sauf data et .git | ||
| 26 | $dossiersCibles = ['model', 'view', 'controller', 'public', 'lib']; | ||
| 27 | $fichiersALaRacine = ['*.php', '.htaccess', '*.txt']; // robots? | ||
| 28 | |||
| 29 | createZip($cheminDestination, $nomFichier, $dossiersCibles, $fichiersALaRacine); | ||
| 30 | } | ||
| 55 | 31 | ||
| 56 | try | ||
| 57 | { | ||
| 58 | $Zip = new ZipArchive(); | ||
| 59 | } | ||
| 60 | catch (Throwable $e) // absence de l'extension zip | ||
| 61 | { | ||
| 62 | echo($erreur); | ||
| 63 | die(); | ||
| 64 | } | ||
| 65 | 32 | ||
| 66 | if ($Zip->open('data/' . $nomFichier, ZipArchive::CREATE | ZipArchive::OVERWRITE)!==TRUE) | 33 | // les deux premiers paramètres concernent le fichier créé |
| 67 | { | 34 | // le quatrième est optionnel et concerne les fichiers à la racine |
| 68 | exit("Impossible d'ouvrir le fichier <i>" . $nomFichier . "</i>.\n"); | 35 | function createZip($destinationPath, $zipFileName, array $targetDirectories, array $allPattern = []) |
| 69 | } | 36 | { |
| 37 | try | ||
| 38 | { | ||
| 39 | $Zip = new ZipArchive(); | ||
| 40 | } | ||
| 41 | catch (Throwable $e) // l'extension zip n'est pas activée | ||
| 42 | { | ||
| 43 | echo($e); | ||
| 44 | die(); | ||
| 45 | } | ||
| 46 | |||
| 47 | // note: j'ai utilisé l'option ZipArchive::OVERWRITE, | ||
| 48 | // ZipArchive::FL_OVERWRITE n'apparaît qu'avec PHP 8 | ||
| 49 | if ($Zip->open($destinationPath . $zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) | ||
| 50 | { | ||
| 51 | exit("Impossible de créer ou d'ouvrir ou créer le fichier <i>data/" . $zipFileName . ".</i><br /> | ||
| 52 | Il est possible que ce fichier existe déjà et soit interdit en écriture.<br /> | ||
| 53 | <p><a href=\"index.php\" >Retour au site</a></p>"); | ||
| 54 | } | ||
| 70 | 55 | ||
| 71 | // fichiers à la racine | 56 | $compteur = 0; |
| 72 | $Zip->addGlob('*.php', 0, array('')); | ||
| 73 | $Zip->addGlob('.htaccess', 0, array('')); | ||
| 74 | $Zip->addGlob('*.txt', 0, array('')); | ||
| 75 | 57 | ||
| 76 | // tous les dossiers sauf data et .git | 58 | // recherche récursive dans les dossiers dans $directories |
| 77 | $listeDossiers = array('model', 'view', 'controller', 'public', 'lib'); | 59 | foreach($targetDirectories as $path) |
| 78 | foreach($listeDossiers as $path) | ||
| 79 | { | 60 | { |
| 80 | // les deux lignes suivantes me dépassent un peu, | 61 | // les deux lignes suivantes reviennent à utiliser le paramètre "-r" dans la console |
| 81 | // mais ça marche et c'est comme ça qu'il faut faire | ||
| 82 | $directory = new RecursiveDirectoryIterator($path); | 62 | $directory = new RecursiveDirectoryIterator($path); |
| 83 | $iterator = new RecursiveIteratorIterator($directory); | 63 | $iterator = new RecursiveIteratorIterator($directory); |
| 84 | 64 | ||
| @@ -87,13 +67,93 @@ function creerMelainePHP() | |||
| 87 | // chemins inutiles . et .. | 67 | // chemins inutiles . et .. |
| 88 | if($info->getFilename() != "." && $info->getFilename() != "..") | 68 | if($info->getFilename() != "." && $info->getFilename() != "..") |
| 89 | { | 69 | { |
| 90 | //var_dump($info->getPathname()); | ||
| 91 | $Zip->addGlob($info->getPathname(), 0, array('')); | 70 | $Zip->addGlob($info->getPathname(), 0, array('')); |
| 71 | $compteur++; | ||
| 92 | } | 72 | } |
| 93 | } | 73 | } |
| 94 | } | 74 | } |
| 95 | 75 | ||
| 76 | // recherche à la racine avec des pattern de noms de fichiers | ||
| 77 | // possibilité d'un tableau vide (paramètre optionnel) | ||
| 78 | foreach($allPattern as $onePattern) | ||
| 79 | { | ||
| 80 | $Zip->addGlob($onePattern, 0, array('')); | ||
| 81 | } | ||
| 82 | |||
| 96 | $Zip->close(); | 83 | $Zip->close(); |
| 97 | 84 | ||
| 98 | chmod('data/' . $nomFichier, 0644); // écriture 4 chiffres (octal) | 85 | if($compteur > 0) |
| 99 | } \ No newline at end of file | 86 | { |
| 87 | chmod($destinationPath . $zipFileName, 0666); // en octal | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | $_SESSION['erreur'] = "Extraction des données impossible, le site entier est vide."; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | function restaurer($from) | ||
| 97 | { | ||
| 98 | $title = 'Restauration des données'; | ||
| 99 | $message = ''; | ||
| 100 | |||
| 101 | // recharger la même page en écrivant les données | ||
| 102 | if(isset($_FILES['archive']) && $_FILES['archive']['error'] == 0) | ||
| 103 | { | ||
| 104 | // une copie du zip est conservée dans data/ au cas où | ||
| 105 | move_uploaded_file($_FILES['archive']['tmp_name'], 'data/' . $_FILES['archive']['name']); | ||
| 106 | chmod('data/' . $_FILES['archive']['name'], 0666); | ||
| 107 | |||
| 108 | $nomFichier = 'data/' . $_FILES['archive']['name']; | ||
| 109 | |||
| 110 | // extraction | ||
| 111 | try | ||
| 112 | { | ||
| 113 | $Zip = new ZipArchive(); | ||
| 114 | } | ||
| 115 | catch (Throwable $e) // l'extension zip n'est pas activée | ||
| 116 | { | ||
| 117 | echo($e); | ||
| 118 | die(); | ||
| 119 | } | ||
| 120 | |||
| 121 | //var_dump($Zip->open($nomFichier, ZipArchive::RDONLY)); | ||
| 122 | //var_dump(ZipArchive::ER_NOENT); | ||
| 123 | |||
| 124 | if($Zip->open($nomFichier, ZipArchive::RDONLY) === TRUE) | ||
| 125 | { | ||
| 126 | $j = 0; | ||
| 127 | for($i = 0; $i < $Zip->numFiles; $i++) | ||
| 128 | { | ||
| 129 | $nomEntree = $Zip->getNameIndex($i); | ||
| 130 | if($Zip->extractTo('.', $nomEntree) === TRUE) | ||
| 131 | { | ||
| 132 | $j++; | ||
| 133 | } | ||
| 134 | else | ||
| 135 | { | ||
| 136 | $message = '<p style="color: red;" >Erreur: extraction du zip impossible.</p>'; | ||
| 137 | } | ||
| 138 | chmod($nomEntree, 0666); | ||
| 139 | } | ||
| 140 | |||
| 141 | if($j == $Zip->numFiles) | ||
| 142 | { | ||
| 143 | $message = '<p style="color: red;" >Restauration réussie !!</p>'; | ||
| 144 | } | ||
| 145 | $Zip->close(); | ||
| 146 | } | ||
| 147 | else | ||
| 148 | { | ||
| 149 | // mauvais fichier | ||
| 150 | $message = '<p style="color: red;" >Erreur: Impossible d\'ouvrir l\'archive Zip."</p>'; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | elseif(isset($_FILES['archive']) && $_FILES['archive']['error'] != 0) | ||
| 154 | { | ||
| 155 | $message = '<p style="color: red;" >Erreur: Le fichier n\'a pas pu être téléchargé correctement."</p>'; | ||
| 156 | } | ||
| 157 | |||
| 158 | require('view/backup.php'); | ||
| 159 | } | ||
diff --git a/controller/visitor.php b/controller/visitor.php index 2656798..f0dc877 100644 --- a/controller/visitor.php +++ b/controller/visitor.php | |||
| @@ -59,7 +59,7 @@ function discoVisitor() | |||
| 59 | 59 | ||
| 60 | // modèle | 60 | // modèle |
| 61 | $Albums = new Album($page_actuelle); | 61 | $Albums = new Album($page_actuelle); |
| 62 | $Albums->makeFileList(); | 62 | //var_dump($Albums->fileList); die(); |
| 63 | 63 | ||
| 64 | // on récupère tout: JSON, HTML, noms et chemins des fichiers | 64 | // on récupère tout: JSON, HTML, noms et chemins des fichiers |
| 65 | $Albums->readAll(); | 65 | $Albums->readAll(); |
| @@ -88,19 +88,22 @@ function discoVisitor() | |||
| 88 | /*$lienAlbum = []; | 88 | /*$lienAlbum = []; |
| 89 | $avecLien = []; | 89 | $avecLien = []; |
| 90 | $linkDiscoChrono = [];*/ | 90 | $linkDiscoChrono = [];*/ |
| 91 | |||
| 92 | // liens vers les albums | ||
| 91 | for($i = 0; $i < $Albums->fileListCount; $i++) | 93 | for($i = 0; $i < $Albums->fileListCount; $i++) |
| 92 | { | 94 | { |
| 93 | if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) | 95 | if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) |
| 94 | { | 96 | { |
| 95 | $lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode']; | 97 | /*$lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode'];*/ |
| 98 | $lienAlbum[$i] = 'page=album&file_code=' . $Albums->fileList[$i]['fileCode']; | ||
| 96 | $avecLien[$i] = true; | 99 | $avecLien[$i] = true; |
| 97 | $linkDiscoChrono[$i] = 'linkChrono'; // css | 100 | $linkDiscoChrono[$i] = 'linkChrono'; // pour le css |
| 98 | } | 101 | } |
| 99 | else | 102 | else |
| 100 | { | 103 | { |
| 101 | $lienAlbum[$i] = 'page=discographie#' . $Albums->fileList[$i]['titre']; | 104 | $lienAlbum[$i] = 'page=discographie#' . $Albums->fileList[$i]['titre']; |
| 102 | $avecLien[$i] = false; | 105 | $avecLien[$i] = false; |
| 103 | $linkDiscoChrono[$i] = 'noLinkChrono'; // css | 106 | $linkDiscoChrono[$i] = 'noLinkChrono'; // pour le css |
| 104 | } | 107 | } |
| 105 | $lienBoutonModif[$i] = 'page=discographie&action=edition&file_code=' . $Albums->fileList[$i]['fileCode']; | 108 | $lienBoutonModif[$i] = 'page=discographie&action=edition&file_code=' . $Albums->fileList[$i]['fileCode']; |
| 106 | } | 109 | } |
| @@ -112,12 +115,31 @@ function discoVisitor() | |||
| 112 | } | 115 | } |
| 113 | 116 | ||
| 114 | // page d'un album | 117 | // page d'un album |
| 115 | function album($fileCode, $albumName) | 118 | function album($fileCode) |
| 116 | { | 119 | { |
| 117 | $page_actuelle = 'discographie'; | 120 | $page_actuelle = 'discographie'; // ??? |
| 118 | $title = $albumName; | 121 | |
| 122 | $Albums = new Album($page_actuelle); | ||
| 123 | $Albums->getAllJSON(); | ||
| 124 | $Albums->fileCode = $fileCode; | ||
| 125 | $Albums->readOne(); | ||
| 119 | 126 | ||
| 120 | $album = Album::readOneHTML($fileCode); // simple, efficace | 127 | //var_dump($Albums); die(); |
| 128 | |||
| 129 | $album = $Albums->oneAlbum['HTMLcontent']; | ||
| 130 | //$album = Album::readOneHTML($fileCode); | ||
| 131 | |||
| 132 | for($i = 0; $i < $Albums->fileListCount; $i++) | ||
| 133 | { | ||
| 134 | if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) | ||
| 135 | { | ||
| 136 | $lienAlbum[$i] = 'page=album&file_code=' . $Albums->fileList[$i]['fileCode']; | ||
| 137 | } | ||
| 138 | else | ||
| 139 | { | ||
| 140 | $lienAlbum[$i] = 'page=discographie#' . $Albums->fileList[$i]['titre']; | ||
| 141 | } | ||
| 142 | } | ||
| 121 | 143 | ||
| 122 | // variables $css, $header et $content | 144 | // variables $css, $header et $content |
| 123 | require('view/album.php'); | 145 | require('view/album.php'); |
