site-accueil-insa/photos.php
2022-06-20 15:45:48 +02:00

303 lines
No EOL
11 KiB
PHP

<?php
ob_start(); // Start reading html
//header('Location: construction.php');
define("urlParam", "path");
define("photoRoot", "photos_folders/photos");
/**
* Get active path from url and prevent from seeing folders before 'photos/'
* @return string current path
*/
function getActivePath()
{
$dir = '';
if (isset($_GET[urlParam]))
$dir = $_GET[urlParam];
$folders = explode(DIRECTORY_SEPARATOR, $dir);
$currentPath = "";
foreach ($folders as $value) {
if ($value != ".." && $value != "." && $value != "") {
$currentPath .= DIRECTORY_SEPARATOR . $value;
}
}
return $currentPath;
}
/**
* Get active folder from the active path
* @param string $path path representing the active folder
* @return string active folder name
*/
function GetActiveFolder($path)
{
$dir = explode(DIRECTORY_SEPARATOR, $path);
return $dir[sizeof($dir) - 1]; // Last item after /
}
/**
* Check whether the current album is available for download as a .zip file
* @param string $path path to search the album in
* @return bool True if an album is available, false otherwise
*/
function isAlbumAvailable($path)
{
$dir = photoRoot . $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 and creates them on the page
* @param string $path path to search directories in
*/
function createDirectories($path)
{
$path = photoRoot . $path;
$displayedItems = 0;
$folders = getDirectories($path);
foreach ($folders as $key => $value) {
$folderTitle = $value;
$photos = getTotalPhotoCount($path . DIRECTORY_SEPARATOR . $value);
$albums = getTotalAlbumCount($path . DIRECTORY_SEPARATOR . $value);
$folderLink = "?" . urlParam . "=" . getActivePath() . DIRECTORY_SEPARATOR . $value;
?>
<a href="<?= $folderLink ?>">
<li class="directory-li">
<span id="folderTitle"><?= $folderTitle ?></span>
<?php if ($photos > 1): ?>
<span id="folderPhotos"><?= $photos ?> photos</span>
<?php elseif ($photos == 1): ?>
<span id="folderPhotos"><?= $photos ?> photo</span>
<?php else: ?>
<span id="folderPhotos">vide</span>
<?php endif; ?>
<?php if ($albums > 1): ?>
<span id="folderAlbums"><?= $albums ?> albums</span>
<?php elseif ($albums == 1): ?>
<span id="folderAlbums"><?= $albums ?> album</span>
<?php endif; ?>
</li>
</a>
<?php
$displayedItems++;
}
}
/**
* Get all photos in the specified path and creates them on the page
* @param string $path path to search photos in
*/
function createPhotos($path_in)
{
$path = photoRoot . "_thumb" . $path_in;
$path_no_thumb = photoRoot . $path_in;
$files = scandir($path);
$displayedItems = 0;
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
$realPath_no_thumb = realpath($path_no_thumb . DIRECTORY_SEPARATOR . $value);
if (isValidImage($realPath) AND isValidImage($realPath_no_thumb)) {
$imageSrc = $path . DIRECTORY_SEPARATOR . $value;
$img_no_thumb = $path_no_thumb . DIRECTORY_SEPARATOR . $value;
$imageId = "photo-" . $displayedItems;
?><a download="" href="<?= $img_no_thumb ?>"><img src="<?= $imageSrc ?>" class="photo" id="<?= $imageId ?>" alt=""/></a><?php
$displayedItems++;
}
}
}
/**
* get folders in the given path
* @param string $path path to search folders in
* @return array array of folders
*/
function getDirectories($path)
{
$files = scandir($path);
$folders = [];
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (isValidDirectory($realPath, $value)) {
array_push($folders, $value);
}
}
return $folders;
}
/**
* Counts directories in the specified folder
* @param string $path path to search directories in
* @return int directories count
*/
function getDirectoriesCount($path)
{
$files = scandir($path);
$dirCount = 0;
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (isValidDirectory($realPath, $value)) {
$dirCount++;
}
}
return $dirCount;
}
/**
* Counts images in the specified folder
* @param string $path path to search photos in
* @return int photo count
*/
function getPhotoCount($path)
{
$files = scandir($path);
$fileCount = 0;
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (isValidImage($realPath)) {
$fileCount++;
}
}
return $fileCount;
}
/**
* Get the count of all directories, recursively from the path specified
* @param string $path root for search
* @return int total number of directories
*/
function getTotalAlbumCount($path)
{
$folders = getDirectories($path);
$total = sizeof($folders);
foreach ($folders as $key => $value) {
$total += getTotalAlbumCount($path . DIRECTORY_SEPARATOR . $value);
}
return $total;
}
function getTotalPhotoCount($path)
{
$folders = getDirectories($path);
$total = getPhotoCount($path);
foreach ($folders as $key => $value) {
$total += getTotalPhotoCount($path . DIRECTORY_SEPARATOR . $value);
}
return $total;
}
/**
* Check if the given image is valid
* @param string $imagePath absolute path of the image
* @return bool True if the file is a jpg, jpeg or png, false otherwise
*/
function isValidImage($imagePath)
{
$ext = pathinfo($imagePath, PATHINFO_EXTENSION);
return !is_dir($imagePath) && ($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG" || $ext == "png" || $ext == "PNG");
}
/**
* Check if the given folder is valid (is not '.' or '..')
* @param string $directoryPath directory path
* @param string $directory directory name
* @return bool True if the directory is valid, false otherwise
*/
function isValidDirectory($directoryPath, $directory)
{
return is_dir($directoryPath) && $directory != "." && $directory != ".." && substr($directory, 0, 1) !== ".";
}
/**
* Creates buttons representing the actual path for easier navigation
* @param string $path Actual Path
*/
function generatePath($path)
{
$folders = explode(DIRECTORY_SEPARATOR, $path);
$currentPath = "";
$pathTitle = "Menu";
$pathLink = "?" . urlParam . "=";
echo '<li><a href="'.$pathLink.'" class="link">'.$pathTitle.'</a></li>';
foreach ($folders as $value) {
if ($value != "") {
$pathTitle = $value;
$currentPath .= DIRECTORY_SEPARATOR . $value;
$pathLink = "?" . urlParam . "=" . $currentPath;
echo ' > <li><a href="'.$pathLink.'" class="link">'.$pathTitle.'</a></li>';
}
}
}
?>
<div class="inner">
<div class="box-jaune">
<span class="corners corners-top"></span>
<span class="corners corners-bottom"></span>
<div class="title">Les photos</div>
<span class="circles circles-top"></span>
<span class="circles circles-bottom"></span>
</div>
<section>
<p>Clique sur le dossier de ton choix pour afficher les photos. Il faut que tu sois inscrit à l'INSA pour pouvoir
les regarder (et oui, pas de spoiler).</p>
<p>Il te suffit ensuite de cliquer sur la photo que tu veux télécharger.</p>
<p>Si tu ne peux pas voir les photos (la fenêtre pour entrer le mot de passe ne s'affiche pas), ouvre cette page avec un autre navigateur.</p>
</section>
<section align="left">
<ul class="photos-path">
<li>Chemin : </li>
<?php
generatePath(getActivePath());
?>
</ul>
<ul class="directory-ul">
<?php if (getDirectoriesCount(photoRoot . getActivePath()) > 0): ?>
<div class="photos-folder-container">
<?php
createDirectories(getActivePath());
?>
</div>
<?php endif; ?>
</ul>
<?php if (isAlbumAvailable(getActivePath())): ?>
<a download=""
href="photos_folders/photos<?php echo getActivePath() . DIRECTORY_SEPARATOR . GetActiveFolder(getActivePath()) ?>.zip"
id="downloadAlbum">
<span id="downloadText"><i class="fas fa-download"></i>Télécharger</span>
<span id="albumPhotoCount"><?php echo getPhotoCount(photoRoot . getActivePath()) ?> photos</span>
</a>
<?php endif; ?>
<?php if (getPhotoCount(photoRoot . getActivePath()) > 0): ?>
<div class="photos">
<?php
createPhotos(getActivePath());
?>
</div>
<?php endif; ?>
</section>
</div>
<br>
<?php
$infopage = ["", "Photos", ob_get_clean(), "", "photos"]; //relativepath, pagetitle, pagecontent, pagescript, pagename | cf structure/template.php ligne 2 à 6
include("structure/template.php");
?>