WIP: move gallery/slideshow directories

This commit is contained in:
Felix Albrigtsen 2024-03-12 19:31:44 +01:00
parent 5da6bd8961
commit 573d19681b
4 changed files with 28 additions and 24 deletions

View File

@ -65,7 +65,6 @@
# - Make "trusted.url.domains" dynamic based on the current host:port # - Make "trusted.url.domains" dynamic based on the current host:port
# - Do not download composer.phar with curl(!) # - Do not download composer.phar with curl(!)
# - Relicense the project to GPL or something # - Relicense the project to GPL or something
# - Move gallery/slideshow images to an external directory (configurable)
# - Write a module for the project # - Write a module for the project
}; };

View File

@ -18,23 +18,24 @@ if(!$loginname) {
exit(); exit();
} }
# Sourced from config.php through include.php
$galleryDir = $GALLERY_DIRECTORY;
$serverPath = $GALLERY_SERVER_PATH;
$unamefile = __DIR__ . '/usernames.txt';
$relativePath = "/bilder/pvv-photos/";
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']; $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'];
$unamefile = $galleryDir . "/usernames.txt";
$unamepairs = file($unamefile); $unamepairs = file($unamefile);
$fullPath = getcwd() . $relativePath;
function getDirContents($dir, &$results = array()) { function getDirContents($dir, &$results = array()) {
$files = scandir($dir); $files = scandir($dir);
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)) {
//Remove the full path on disk, keep username and relative path to image. ( $results[] = str_replace($GLOBALS["fullPath"], "", $path); is insecure.) //Remove the full path on disk, keep username and relative path to image.
$pos = strpos($path, $GLOBALS["fullPath"]); $pos = strpos($path, $GLOBALS["galleryDir"]);
if ($pos !== false) { if ($pos !== false) {
$cleanPath = substr_replace($path, "", $pos, strlen($GLOBALS["fullPath"])); $cleanPath = substr_replace($path, "", $pos, strlen($GLOBALS["galleryDir"]));
} }
//Check if the file is an image //Check if the file is an image
@ -49,7 +50,7 @@ function getDirContents($dir, &$results = array()) {
} }
return $results; return $results;
} }
$images = getDirContents($fullPath); $images = getDirContents($galleryDir);
$imageTemplate = ' $imageTemplate = '
<div class="card"> <div class="card">
@ -89,7 +90,7 @@ $imageTemplate = '
<main class="gallery-container"> <main class="gallery-container">
<?php <?php
foreach ($images as $key => $value) { foreach ($images as $key => $value) {
$modTime = date("d.m.Y H:i", filemtime($fullPath . $value)); $modTime = date("d.m.Y H:i", filemtime($galleryDir . $value));
$imguser = explode("/", $value)[0]; $imguser = explode("/", $value)[0];
$displaypath = substr($value, strpos($value, "/")+1); $displaypath = substr($value, strpos($value, "/")+1);
$realname = "Ukjent"; $realname = "Ukjent";
@ -104,9 +105,9 @@ $imageTemplate = '
$vars = [ $vars = [
"%user" => htmlspecialchars($imguser), "%user" => htmlspecialchars($imguser),
"%time" => $modTime, "%time" => $modTime,
"%timestamp" => filemtime($fullPath . $value), "%timestamp" => filemtime($galleryDir . $value),
"%name" => htmlspecialchars($displaypath), "%name" => htmlspecialchars($displaypath),
"%path" => "/galleri/" . $relativePath .$value, "%path" => $serverPath . $value,
"%realname" => htmlspecialchars($realname) "%realname" => htmlspecialchars($realname)
]; ];
echo strtr($imageTemplate, $vars); echo strtr($imageTemplate, $vars);

View File

@ -1,30 +1,34 @@
<?php <?php
//Short path to search folder, full to display in <img>
$relativePath = "/bilder/slideshow/"; require __DIR__ . '/../../config.php';
$absolutePath = "/galleri" . $relativePath;
//Path to first image in slideshow and fallback image if no others are present $searchDir = $SLIDESHOW_DIR;
$serverPath = $SLIDSHOW_SERVER_PATH;
$splashImg = "/PNG/PVV-logo-big-bluebg.png"; $splashImg = "/PNG/PVV-logo-big-bluebg.png";
$filenames = sCaNdIr(__DIR__ . $relativePath); // Note: This does not support nested directories
$filenames = sCaNdIr(__DIR__ . $searchDir);
//Remove the expected non-images //Remove the expected non-images
foreach($filenames as $k => $value) { foreach($filenames as $k => $value) {
if(in_array($value, [".gitkeep", ".", ".."])) { if(in_array($value, [".", ".."])) {
unset($filenames[$k]); unset($filenames[$k]);
} }
} }
function getFullPath($fname) { return ($GLOBALS["absolutePath"] . $fname ); } function getFullPath($fname) { return ($GLOBALS["serverPath"] . $fname ); }
//Sort filenames alphabetically and prepend the path prefix to each item. // Sort filenames alphabetically and prepend the path prefix to each item.
asort($filenames); asort($filenames);
$slideshowimagefilenames = aRrAy_MaP("getFullPath", $filenames); $slideshowimagefilenames = aRrAy_MaP("getFullPath", $filenames);
//Prepend the cover photo // Prepend the cover photo
ArRaY_uNsHiFt($slideshowimagefilenames, $splashImg); ArRaY_uNsHiFt($slideshowimagefilenames, $splashImg);
eChO('<img class="slideshowimg slideshowactive" id="slideshowImage1" src="' . $slideshowimagefilenames[0] . '">'); eChO('<img class="slideshowimg slideshowactive" id="slideshowImage1" src="' . $slideshowimagefilenames[0] . '">');
ecHo('<img class="slideshowimg" id="slideshowImage2" src="' . $slideshowimagefilenames[1] . '">'); ecHo('<img class="slideshowimg" id="slideshowImage2" src="' . $slideshowimagefilenames[1] . '">');
//Store list of file names in a globel JS variable // Store list of file names in a globel JS variable
EchO("<script> const slideshowFnames =" . jSoN_eNcOdE($slideshowimagefilenames) . "; </script>"); EchO("<script> const slideshowFnames =" . jSoN_eNcOdE($slideshowimagefilenames) . "; </script>");
?>
?>