site-accueil-insa/photos.php

294 lines
8.5 KiB
PHP
Raw Normal View History

2018-03-25 13:22:28 +02:00
<?php
2018-03-25 14:19:53 +02:00
ob_start(); // Start reading html
define("urlParam", "path");
2018-10-04 14:05:53 +02:00
define("photoRoot", "photos_folders/photos");
/**
* Get active path from url and prevent from seeing folders before 'photos/'
* @return string current path
*/
function getActivePath()
2018-03-25 14:19:53 +02:00
{
$dir = '';
if (isset($_GET[urlParam]))
$dir = $_GET[urlParam];
2018-04-19 08:47:19 +02:00
$folders = explode(DIRECTORY_SEPARATOR, $dir);
$currentPath = "";
foreach ($folders as $value) {
if ($value != ".." && $value != "." && $value != "") {
$currentPath .= DIRECTORY_SEPARATOR . $value;
2018-04-19 08:47:19 +02:00
}
2018-03-25 13:22:28 +02:00
}
2018-04-19 08:47:19 +02:00
return $currentPath;
2018-03-25 14:19:53 +02:00
}
2018-03-25 13:22:28 +02:00
/**
* 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)
2018-03-25 14:19:53 +02:00
{
$path = photoRoot . $path;
2018-03-25 14:19:53 +02:00
$displayedItems = 0;
2018-04-23 18:20:39 +02:00
$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;
include("includes/photos/folder_template.php");
$displayedItems++;
2018-03-25 13:22:28 +02:00
}
2018-03-25 14:19:53 +02:00
}
2018-03-25 13:22:28 +02:00
2018-04-23 18:20:39 +02:00
/**
* Get all photos in the specified path and creates them on the page
* @param string $path path to search photos in
*/
function createPhotos($path)
2018-03-25 14:19:53 +02:00
{
$path = photoRoot . "_thumb" . $path;
$files = scandir($path);
2018-03-25 14:19:53 +02:00
$displayedItems = 0;
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (isValidImage($realPath)) {
$imageSrc = $path . DIRECTORY_SEPARATOR . $value;
$imageId = "photo-" . $displayedItems;
include("includes/photos/photo_template.php");
$displayedItems++;
2018-03-25 13:22:28 +02:00
}
}
}
2018-04-23 18:20:39 +02:00
/**
* 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++;
}
2018-03-25 14:19:53 +02:00
}
return $dirCount;
2018-03-25 14:19:53 +02:00
}
2018-03-25 13:22:28 +02:00
/**
* Counts images in the specified folder
* @param string $path path to search photos in
* @return int photo count
*/
function getPhotoCount($path)
2018-03-25 14:19:53 +02:00
{
$files = scandir($path);
$fileCount = 0;
foreach ($files as $key => $value) {
$realPath = realpath($path . DIRECTORY_SEPARATOR . $value);
if (isValidImage($realPath)) {
$fileCount++;
}
}
return $fileCount;
}
2018-04-23 18:20:39 +02:00
/**
* 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);
2018-05-06 21:47:33 +02:00
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)
{
2018-11-08 14:19:21 +01:00
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);
2018-03-25 14:19:53 +02:00
$currentPath = "";
2018-04-19 08:47:19 +02:00
$pathTitle = "Menu";
$pathLink = "?" . urlParam . "=";
2018-04-19 08:47:19 +02:00
include("includes/photos/path_template.php");
2018-03-25 14:19:53 +02:00
foreach ($folders as $value) {
if ($value != "") {
$pathTitle = $value;
$currentPath .= DIRECTORY_SEPARATOR . $value;
$pathLink = "?" . urlParam . "=" . $currentPath;
2018-03-25 14:19:53 +02:00
include("includes/photos/path_template.php");
2018-03-25 13:22:28 +02:00
}
}
2018-03-25 14:19:53 +02:00
}
2018-03-25 13:22:28 +02:00
?>
2018-06-05 20:03:33 +02:00
<div id="photo-overlay" style="display:none">
2018-04-21 15:20:18 +02:00
<img src="" id="img-big">
2018-06-05 20:03:33 +02:00
<div id="close-back" onclick="closeBig()"></div>
<div id="loading" onclick="closeBig()">
<i class="fas fa-spinner fa-spin"></i>
</div>
2018-04-23 12:04:40 +02:00
2018-06-05 20:03:33 +02:00
<div id="photo-buttons">
<i id="right" class="fas fa-arrow-right" onclick="displayNext(1)"></i>
<i id="left" class="fas fa-arrow-left" onclick="displayNext(-1)"></i>
<div id="photo-control">
<i id="close" class="fas fa-times" onclick="closeBig()"></i>
<a id="img_big_download" download="" href="">
<i id="download" class="fas fa-download"></i>
</a>
<a href="" id="img_big_link">
<i id="fullscreen" class="fas fa-expand-arrows-alt"></i>
</a>
2018-04-21 15:20:18 +02:00
</div>
</div>
2018-06-05 20:03:33 +02:00
</div>
2018-03-25 13:22:28 +02:00
2018-06-05 20:03:33 +02:00
<h1 id="photos_title">Photos</h1>
<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>
2018-06-15 11:40:33 +02:00
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
2018-06-05 20:03:33 +02:00
un autre navigateur.
</p>
<ul class="photos-path">
<li><p>Chemin : </p></li>
<?php
generatePath(getActivePath());
?>
</ul>
2018-04-23 18:20:39 +02:00
<?php if (getDirectoriesCount(photoRoot . getActivePath()) > 0): ?>
2018-06-05 20:03:33 +02:00
<div class="photos-folder">
<?php
createDirectories(getActivePath());
?>
</div>
<?php endif; ?>
<?php if (isAlbumAvailable(getActivePath())): ?>
<a download=""
2018-11-08 14:19:21 +01:00
href="photos_folders/photos<?php echo getActivePath() . DIRECTORY_SEPARATOR . GetActiveFolder(getActivePath()) ?>.zip"
2018-06-05 20:03:33 +02:00
id="download-album">
<span id="download-text"><i class="fas fa-download"></i>Télécharger l'album</span>
<span id="album-photo-count"><?php echo getPhotoCount(photoRoot . getActivePath()) ?> photos</span>
</a>
<?php endif; ?>
2018-04-23 18:20:39 +02:00
<?php if (getPhotoCount(photoRoot . getActivePath()) > 0): ?>
<div class="photos">
<?php
createPhotos(getActivePath());
?>
</div>
<?php endif; ?>
2018-06-05 20:03:33 +02:00
<link rel="stylesheet" type="text/css" media="screen" href="assets/css/photos.css">
<?php
$pageContent = ob_get_clean(); // Store html content in variable
$pageTitle = "Photos";
$pageScripts = "<script src=\"assets/js/photos.js\"></script><script type=\"text/javascript\" src=\"assets/js/jquery.mousewheel.min.js\"></script>";
include("includes/template.php"); // Display template with variable content
2018-06-05 20:03:33 +02:00
?>