Display album download only if available

This commit is contained in:
Keplyx 2018-04-22 19:46:59 +02:00
parent 1fbbaca5d7
commit 1321def3e1

View file

@ -1,31 +1,55 @@
<?php <?php
ob_start(); // Start reading html ob_start(); // Start reading html
// Get active folder from url and prevent from seeing folders before photos/
function getActiveFolder() define("urlParam", "path");
// Get active path from url and prevent from seeing folders before photos/
function getActivePath()
{ {
$dir = $_GET['folder']; $dir = $_GET[urlParam];
$folders = explode(DIRECTORY_SEPARATOR, $dir); $folders = explode(DIRECTORY_SEPARATOR, $dir);
$currentPath = ""; $currentPath = "";
foreach ($folders as $value) { foreach ($folders as $value) {
if ($value != "..") { if ($value != ".." && $value != "." && $value != "") {
$currentPath .= $value . DIRECTORY_SEPARATOR; $currentPath .= DIRECTORY_SEPARATOR.$value;
} }
} }
return $currentPath; return $currentPath;
} }
// Get active folder from the active path
function GetActiveFolder($path) {
$dir = explode(DIRECTORY_SEPARATOR, $path);
return $dir[sizeof($dir) - 1]; // Last item after /
}
function isAlbumAvailable($path) {
$dir = "photos".$path;
$files = scandir($dir);
$valid = false;
foreach ($files as $key => $value) {
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if (!is_dir($path)) {
$valid = pathinfo($path, PATHINFO_EXTENSION) == "zip";
if ($valid)
break;
}
}
return $valid;
}
// Get all directories in the specified path // Get all directories in the specified path
function getDirectories($dir) function getDirectories($dir)
{ {
$dir = "photos/".$dir; $dir = "photos".$dir;
$files = scandir($dir); $files = scandir($dir);
$displayedItems = 0; $displayedItems = 0;
foreach ($files as $key => $value) { foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value); $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (is_dir($path) && $value != "." && $value != "..") { if (is_dir($path) && $value != "." && $value != "..") {
$folderTitle = $value; $folderTitle = $value;
$folderLink = "?folder=" . getActiveFolder() . $value; $folderLink = "?".urlParam."=".getActivePath().DIRECTORY_SEPARATOR.$value;
$folderClass = "";
include("includes/photos/folder_template.php"); include("includes/photos/folder_template.php");
$displayedItems++; $displayedItems++;
} }
@ -39,16 +63,16 @@ function getDirectories($dir)
// Get all photos in the specified path // Get all photos in the specified path
function getPhotos($dir) function getPhotos($dir)
{ {
$dir = "photos/".$dir; $dir = "photos".$dir;
$files = scandir($dir); $files = scandir($dir);
$displayedItems = 0; $displayedItems = 0;
foreach ($files as $key => $value) { foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value); $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if (!is_dir($path)) { if (!is_dir($path)) {
$ext = pathinfo($path, PATHINFO_EXTENSION); $ext = pathinfo($path, PATHINFO_EXTENSION);
if ($ext == "bmp" || $ext == "jpg" || $ext == "jpeg" || $ext == "png") { if ($ext == "bmp" || $ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "gif") {
$imageSrc = $dir . $value; $imageSrc = $dir.DIRECTORY_SEPARATOR.$value;
$imageId = "photo-" . $displayedItems; $imageId = "photo-".$displayedItems;
include("includes/photos/photo_template.php"); include("includes/photos/photo_template.php");
$displayedItems++; $displayedItems++;
} }
@ -66,13 +90,13 @@ function generatePath($dir)
$folders = explode(DIRECTORY_SEPARATOR, $dir); $folders = explode(DIRECTORY_SEPARATOR, $dir);
$currentPath = ""; $currentPath = "";
$pathTitle = "Menu"; $pathTitle = "Menu";
$pathLink = "?folder="; $pathLink = "?".urlParam."=";
include("includes/photos/path_template.php"); include("includes/photos/path_template.php");
foreach ($folders as $value) { foreach ($folders as $value) {
if ($value != "") { if ($value != "") {
$pathTitle = $value; $pathTitle = $value;
$currentPath .= $value . DIRECTORY_SEPARATOR; $currentPath .= DIRECTORY_SEPARATOR.$value;
$pathLink = "?folder=" . $currentPath; $pathLink = "?".urlParam."=".$currentPath;
include("includes/photos/path_template.php"); include("includes/photos/path_template.php");
} }
} }
@ -107,21 +131,24 @@ function generatePath($dir)
<ul class="photos_path"> <ul class="photos_path">
<li><p>Chemin : </p></li> <li><p>Chemin : </p></li>
<?php <?php
generatePath(getActiveFolder()); generatePath(getActivePath());
?> ?>
</ul> </ul>
<div class="photos_folder"> <div class="photos_folder">
<?php <?php
getDirectories(getActiveFolder()); getDirectories(getActivePath());
?> ?>
</div> </div>
<div id="download_album"> <?php if(isAlbumAvailable(getActivePath())): ?>
<a download="" href="photos<?php echo getActivePath().DIRECTORY_SEPARATOR.GetActiveFolder(getActivePath())?>.zip" id="download_album">
<i class="fas fa-download"></i> <i class="fas fa-download"></i>
<p>Télécharger l'album</p> <p>Télécharger l'album</p>
</div> </a>
<?php endif; ?>
<div class="photos"> <div class="photos">
<?php <?php
getPhotos(getActiveFolder()); getPhotos(getActivePath());
?> ?>
</div> </div>
<script src="assets/scripts/photosScript.js"></script> <script src="assets/scripts/photosScript.js"></script>