From abd968a1c573b1862bd0728f0b7b6a88e307900a Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 28 Feb 2022 02:15:31 +0100 Subject: sauvegarde zip --- controller/admin.php | 3 +- controller/backup.php | 188 ++++++++++++++++++++++++++++++++----------------- controller/visitor.php | 38 +++++++--- 3 files changed, 156 insertions(+), 73 deletions(-) (limited to 'controller') 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) { if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) { - $lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode']; + /*$lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode'];*/ + $lienAlbum[$i] = 'page=album&file_code=' . $Albums->fileList[$i]['fileCode']; $avecLien[$i] = true; $linkDiscoChrono[$i] = 'linkChrono'; // css } 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 @@ open($chemin . $nomFichier, ZipArchive::CREATE | ZipArchive::OVERWRITE)!==TRUE) { - exit("Impossible d'ouvrir le fichier " . $chemin . $nomFichier . ".\n"); - } - - // ajout des fichiers - // chemin complet = data/"pages"/"html ou images"/"tous les fichiers" - $zip->addGlob($chemin . '*/*/*', 0, array('')); - $zip->close(); - - // pour pouvoir manipuler le fichier depuis filezilla ou ssh - chmod($chemin . $nomFichier, 0666); // écriture 4 chiffres - - require('view/backup.php'); -} - -function restaurer($from) -{ - $title = 'Restauration des données'; - - $chemin = "data/"; - $nomFichier = "melaineDATA.zip"; + createZip($cheminDestination, $nomFichier, $dossiersCibles); require('view/backup.php'); } -// inutile function creerMelainePHP() { + $cheminDestination = 'data/'; $nomFichier = "melainePHP.zip"; + // tous les dossiers sauf data et .git + $dossiersCibles = ['model', 'view', 'controller', 'public', 'lib']; + $fichiersALaRacine = ['*.php', '.htaccess', '*.txt']; // robots? + + createZip($cheminDestination, $nomFichier, $dossiersCibles, $fichiersALaRacine); +} - try - { - $Zip = new ZipArchive(); - } - catch (Throwable $e) // absence de l'extension zip - { - echo($erreur); - die(); - } - if ($Zip->open('data/' . $nomFichier, ZipArchive::CREATE | ZipArchive::OVERWRITE)!==TRUE) - { - exit("Impossible d'ouvrir le fichier " . $nomFichier . ".\n"); - } +// les deux premiers paramètres concernent le fichier créé +// le quatrième est optionnel et concerne les fichiers à la racine +function createZip($destinationPath, $zipFileName, array $targetDirectories, array $allPattern = []) +{ + try + { + $Zip = new ZipArchive(); + } + catch (Throwable $e) // l'extension zip n'est pas activée + { + echo($e); + die(); + } + + // note: j'ai utilisé l'option ZipArchive::OVERWRITE, + // ZipArchive::FL_OVERWRITE n'apparaît qu'avec PHP 8 + if ($Zip->open($destinationPath . $zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) + { + exit("Impossible de créer ou d'ouvrir ou créer le fichier data/" . $zipFileName . ".
+ Il est possible que ce fichier existe déjà et soit interdit en écriture.
+

Retour au site

"); + } - // fichiers à la racine - $Zip->addGlob('*.php', 0, array('')); - $Zip->addGlob('.htaccess', 0, array('')); - $Zip->addGlob('*.txt', 0, array('')); + $compteur = 0; - // tous les dossiers sauf data et .git - $listeDossiers = array('model', 'view', 'controller', 'public', 'lib'); - foreach($listeDossiers as $path) + // recherche récursive dans les dossiers dans $directories + foreach($targetDirectories as $path) { - // les deux lignes suivantes me dépassent un peu, - // mais ça marche et c'est comme ça qu'il faut faire + // les deux lignes suivantes reviennent à utiliser le paramètre "-r" dans la console $directory = new RecursiveDirectoryIterator($path); $iterator = new RecursiveIteratorIterator($directory); @@ -87,13 +67,93 @@ function creerMelainePHP() // chemins inutiles . et .. if($info->getFilename() != "." && $info->getFilename() != "..") { - //var_dump($info->getPathname()); $Zip->addGlob($info->getPathname(), 0, array('')); + $compteur++; } } } + // recherche à la racine avec des pattern de noms de fichiers + // possibilité d'un tableau vide (paramètre optionnel) + foreach($allPattern as $onePattern) + { + $Zip->addGlob($onePattern, 0, array('')); + } + $Zip->close(); - chmod('data/' . $nomFichier, 0644); // écriture 4 chiffres (octal) -} \ No newline at end of file + if($compteur > 0) + { + chmod($destinationPath . $zipFileName, 0666); // en octal + } + else + { + $_SESSION['erreur'] = "Extraction des données impossible, le site entier est vide."; + } +} + + +function restaurer($from) +{ + $title = 'Restauration des données'; + $message = ''; + + // recharger la même page en écrivant les données + if(isset($_FILES['archive']) && $_FILES['archive']['error'] == 0) + { + // une copie du zip est conservée dans data/ au cas où + move_uploaded_file($_FILES['archive']['tmp_name'], 'data/' . $_FILES['archive']['name']); + chmod('data/' . $_FILES['archive']['name'], 0666); + + $nomFichier = 'data/' . $_FILES['archive']['name']; + + // extraction + try + { + $Zip = new ZipArchive(); + } + catch (Throwable $e) // l'extension zip n'est pas activée + { + echo($e); + die(); + } + + //var_dump($Zip->open($nomFichier, ZipArchive::RDONLY)); + //var_dump(ZipArchive::ER_NOENT); + + if($Zip->open($nomFichier, ZipArchive::RDONLY) === TRUE) + { + $j = 0; + for($i = 0; $i < $Zip->numFiles; $i++) + { + $nomEntree = $Zip->getNameIndex($i); + if($Zip->extractTo('.', $nomEntree) === TRUE) + { + $j++; + } + else + { + $message = '

Erreur: extraction du zip impossible.

'; + } + chmod($nomEntree, 0666); + } + + if($j == $Zip->numFiles) + { + $message = '

Restauration réussie !!

'; + } + $Zip->close(); + } + else + { + // mauvais fichier + $message = '

Erreur: Impossible d\'ouvrir l\'archive Zip."

'; + } + } + elseif(isset($_FILES['archive']) && $_FILES['archive']['error'] != 0) + { + $message = '

Erreur: Le fichier n\'a pas pu être téléchargé correctement."

'; + } + + require('view/backup.php'); +} 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() // modèle $Albums = new Album($page_actuelle); - $Albums->makeFileList(); + //var_dump($Albums->fileList); die(); // on récupère tout: JSON, HTML, noms et chemins des fichiers $Albums->readAll(); @@ -88,19 +88,22 @@ function discoVisitor() /*$lienAlbum = []; $avecLien = []; $linkDiscoChrono = [];*/ + + // liens vers les albums for($i = 0; $i < $Albums->fileListCount; $i++) { if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) { - $lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode']; + /*$lienAlbum[$i] = 'page=album&album_name=' . $Albums->fileList[$i]['titre'] . '&file_code=' . $Albums->fileList[$i]['fileCode'];*/ + $lienAlbum[$i] = 'page=album&file_code=' . $Albums->fileList[$i]['fileCode']; $avecLien[$i] = true; - $linkDiscoChrono[$i] = 'linkChrono'; // css + $linkDiscoChrono[$i] = 'linkChrono'; // pour le css } else { $lienAlbum[$i] = 'page=discographie#' . $Albums->fileList[$i]['titre']; $avecLien[$i] = false; - $linkDiscoChrono[$i] = 'noLinkChrono'; // css + $linkDiscoChrono[$i] = 'noLinkChrono'; // pour le css } $lienBoutonModif[$i] = 'page=discographie&action=edition&file_code=' . $Albums->fileList[$i]['fileCode']; } @@ -112,12 +115,31 @@ function discoVisitor() } // page d'un album -function album($fileCode, $albumName) +function album($fileCode) { - $page_actuelle = 'discographie'; - $title = $albumName; + $page_actuelle = 'discographie'; // ??? + + $Albums = new Album($page_actuelle); + $Albums->getAllJSON(); + $Albums->fileCode = $fileCode; + $Albums->readOne(); - $album = Album::readOneHTML($fileCode); // simple, efficace + //var_dump($Albums); die(); + + $album = $Albums->oneAlbum['HTMLcontent']; + //$album = Album::readOneHTML($fileCode); + + for($i = 0; $i < $Albums->fileListCount; $i++) + { + if(file_exists('data/discographie/html/' . $Albums->fileList[$i]['fileCode'] . '.html')) + { + $lienAlbum[$i] = 'page=album&file_code=' . $Albums->fileList[$i]['fileCode']; + } + else + { + $lienAlbum[$i] = 'page=discographie#' . $Albums->fileList[$i]['titre']; + } + } // variables $css, $header et $content require('view/album.php'); -- cgit v1.2.3