File "explore_arbo06.php"

Full Path: /home/analogde/www/FTP/Monsta-FTP-master/explore_arbo06.php
File size: 9.12 KB
MIME-type: text/html
Charset: utf-8

<!DOCTYPE html>
<html lang="fr">
<head>
   
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Module gestion des versions</title>
    <link rel="icon" type="image/x-icon" href="version.png">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
    a { color: #abcde1}

    table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #f4f4f4;
        }

        button {
            padding: 8px 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background-color: #0056b3;
        }
/*
        .container {
  height: 200px;
  position: relative;
  border: 3px solid green;
}

.vertical-center {
  margin: 0;
  position: absolute;
  top: 50%;
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}
*/
.center_bouton {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 50px;
  /*border: 3px solid green; */
}

/*
    .button-container {
            display: flex;
            flex-direction: column;
            width: 200px;
        }
        .button-container button {
            margin: 5px 0;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        .button-container button:hover {
            background-color: #0056b3;
        }
*/
    </style>
<!--
<style>
.navLink { color: blue; }
</style>

<a class="navLink">...</a>
-->
    <script>
        // Fonction pour revenir à la page source
        function goBack() 
        {
            window.location.href = "<?php echo htmlspecialchars("style06.php"); ?>";
        }
    </script>
</head>

<?php


    $modalMessage = '';
    $showModal = false;

    $page_reference = isset($_GET['source']) ? htmlspecialchars($_GET['source']) : '';
    $fichier = isset($_GET['fichier']) ? htmlspecialchars($_GET['fichier']) : '';
    $chemin = isset($_GET['path']) ? htmlspecialchars($_GET['path']) : '';

    if( isset($_GET['telechargement'] ) )
    {
        echo " ACTION ! " . $_GET['telechargement'] ."<br>";

        $file = $_GET['telechargement'];
        
        echo " Selection " .$file ."<br>";
       
        $chemin = $file;

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($chemin));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($chemin));
        ob_clean();
        flush();
        readfile($chemin);

        $modalMessage = "Vous avez cliqué sur le bouton : $file";
        $showModal = true;

        exit;
    } 
    /*else
    {
       echo " ACTION : rien " ."<br>";
    }*/

    
    // mise en forme d'une partie du chemin
    $chemin_format = str_replace("~", "/home/analogde", $chemin);
    // creation du chemin complet avec le nom du fichier
    $recherche = $chemin_format ."/" .$fichier;
    
    // Vérifier si le fichier existe
    if (file_exists($recherche)) 
    {
            //echo " Yes, il existe....";
    
            //$nom_repertoire = str_replace("~", "/home/analogde", $chemin);

            // Lire le contenu du répertoire
            $fichiers_scan = scandir($chemin_format);
    
            $fileList = [];

            // Parcourir les fichiers
            foreach ($fichiers_scan as $file) 
            {
                // Ignorer les entrées spéciales "." et ".."
                if ($file !== "." && $file !== "..") 
                {
                    $fileList[] = $file;
                    //echo "<li>$file</li>";
                }
            }
            //echo "</ul>";

            // Extraire la partie du nom sans extension
            $baseName = pathinfo($fichier, PATHINFO_FILENAME);

            // Tableau pour stocker les fichiers correspondants
            $matchingFiles = [];

             // on ajoute le fichier en version initial
            $matchingFiles[] = $fichier;

            // Parcourir la liste des fichiers
            foreach ($fileList as $file) 
            {
                // Extraire la partie du nom sans extension
                $currentBaseName = pathinfo($file, PATHINFO_FILENAME);
                // Vérifier si le fichier partage une partie commune de nom avec le fichier de référence
                if (strpos($currentBaseName, $baseName) === 0 && $currentBaseName !== $baseName)
                {
                    $matchingFiles[] = $file;
                }
            }

          
            $filesData = [];

            foreach ($matchingFiles as $file) 
            {
                $recherche = $chemin_format ."/" .$file;

                 // Récupérer les informations sur le fichier
                $fileSize = filesize($recherche); // Taille en octets
                $fileCreationTime = filectime($recherche); // Timestamp de la création

                $taille = human_filesize($fileSize, $precision = 2);

                // Ajouter les informations au tableau
                $filesData[] = [
                                'name' => $file,
                                'size' => $taille,
                                'creation_date' => date('d-m-Y H:i:s', $fileCreationTime), // Formater la date
                            ];
            }

    } 
    else 
    {
        echo "Fichier introuvable";
    }
    

    function human_filesize($size, $precision = 2) {
        $units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
        $step = 1024;
        $i = 0;
        while (($size / $step) > 0.9) {
            $size = $size / $step;
            $i++;
        }
        return round($size, $precision).$units[$i];
    }    
?>


<body>
    
<!-- Table qui contient les liens qui sont générés en fonction des version de fichier disponibles -->

<div class="container mt-5">

<h1 class="text-center mb-4">Liste des versions disponibles</h1>

<table class="table table-striped table-hover table-bordered">
        <thead class="table-dark">
            <tr>
                <th>Nom</th>
                <th>Taille</th>
                <th>Heure</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            //foreach ($rows as $index => $row):
                foreach ($filesData as $row) 
                {
                    $telechargement = $chemin_format ."/" .$row['name'];
                    $url = "explore_arbo06.php?source=$page_reference&fichier=$fichier&path=$chemin&telechargement=$telechargement";
            ?>
                <tr>
                    <td><?= htmlspecialchars($row['name']) ?></td>
                    <td><?= htmlspecialchars($row['size']) ?></td>
                    <td><?= htmlspecialchars($row['creation_date']) ?></td>
                    <td>
                        <!-- Bouton qui renvoie une valeur via GET -->
                        <!--
                        <button onclick="location.href='?button=<?= $index + 1 ?>'">Action <?= $index + 1 ?></button>
                        -->
                        <button onclick="window.location.href='<?php echo $url; ?>'">Téléchargement</button>

                    </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
</div>
   

    <div class="center_bouton">
    <button onclick="goBack()">Retour à l'explorateur</button>
    </div>


    <!-- Fenêtre modale Bootstrap -->
    <div class="modal fade" id="actionModal" tabindex="-1" aria-labelledby="actionModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="actionModalLabel">Information</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <?= htmlspecialchars($modalMessage) ?>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
                </div>
            </div>
        </div>
    </div>

 <!-- Lien vers Bootstrap JS -->
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <?php if ($showModal): ?>
        <script>
            // Afficher la fenêtre modale si $showModal est vrai
            var actionModal = new bootstrap.Modal(document.getElementById('actionModal'));
            actionModal.show();
        </script>
    <?php endif; ?>

</body>
</html>