The formattening, part 2

This commit is contained in:
2025-03-12 00:38:57 +01:00
parent b6697e5750
commit da8afe735c
35 changed files with 1066 additions and 1033 deletions

View File

@@ -1,67 +1,68 @@
<?php
error_reporting(0);
require_once dirname(dirname(__DIR__)) . implode(DIRECTORY_SEPARATOR, ['', 'inc', 'include.php']);
require_once dirname(__DIR__, 2) . implode(\DIRECTORY_SEPARATOR, ['', 'inc', 'include.php']);
$pdo = new \PDO($DB_DSN, $DB_USER, $DB_PASS);
$pdo = new PDO($DB_DSN, $DB_USER, $DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$userManager = new \pvv\admin\UserManager($pdo);
$userManager = new pvv\admin\UserManager($pdo);
$as = new \SimpleSAML\Auth\Simple('default-sp');
$as = new SimpleSAML\Auth\Simple('default-sp');
$as->requireAuth();
$attrs = $as->getAttributes();
$loginname = $attrs['uid'][0];
if(!$loginname) {
header('Content-Type: text/plain', true, 403);
echo "Du må være logget inn for å se bildegalleriet.\r\n";
exit();
if (!$loginname) {
header('Content-Type: text/plain', true, 403);
echo "Du må være logget inn for å se bildegalleriet.\r\n";
exit;
}
# Sourced from config.php through include.php
// Sourced from config.php through include.php
$galleryDir = $GALLERY_DIR;
$serverPath = $GALLERY_SERVER_PATH;
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'];
$unamefile = $galleryDir . "/usernames.txt";
$unamefile = $galleryDir . '/usernames.txt';
$unamepairs = file($unamefile);
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
//Remove the full path on disk, keep username and relative path to image.
$pos = strpos($path, $GLOBALS["galleryDir"]);
if ($pos !== false) {
$cleanPath = substr_replace($path, "", $pos, strlen($GLOBALS["galleryDir"]));
}
function getDirContents($dir, &$results = []) {
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . \DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
// Remove the full path on disk, keep username and relative path to image.
$pos = strpos($path, $GLOBALS['galleryDir']);
if ($pos !== false) {
$cleanPath = substr_replace($path, '', $pos, strlen($GLOBALS['galleryDir']));
}
//Check if the file is an image
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if (in_array($ext, $GLOBALS["allowedExtensions"])) {
$results[] = $cleanPath;
}
} else if ($value != "." && $value != ".." && $value != ".thumbnails") {
//recursively scan directories
getDirContents($path, $results);
}
// Check if the file is an image
$ext = strtolower(pathinfo($path, \PATHINFO_EXTENSION));
if (in_array($ext, $GLOBALS['allowedExtensions'], true)) {
$results[] = $cleanPath;
}
} elseif ($value != '.' && $value != '..' && $value != '.thumbnails') {
// recursively scan directories
getDirContents($path, $results);
}
return $results;
}
return $results;
}
$images = getDirContents($galleryDir);
function cmpModifyTime($a, $b) {
global $galleryDir;
global $galleryDir;
$mtime_a = filemtime($galleryDir . $a);
$mtime_b = filemtime($galleryDir . $b);
$mtime_a = filemtime($galleryDir . $a);
$mtime_b = filemtime($galleryDir . $b);
return ($mtime_a > $mtime_b) ? -1 : 1;
return ($mtime_a > $mtime_b) ? -1 : 1;
}
usort($images, "cmpModifyTime");
usort($images, 'cmpModifyTime');
$imageTemplate = '
<div class="card">
@@ -77,7 +78,7 @@ $imageTemplate = '
<p class="card-footer-item">%time</p>
</div>
</div>
'
';
?>
@@ -101,34 +102,34 @@ $imageTemplate = '
<main class="gallery-container">
<?php
foreach ($images as $key => $value) {
$modTime = date("d.m.Y H:i", filemtime($galleryDir . $value));
$imguser = explode("/", $value)[1];
$displaypath = implode("/", array_slice(explode("/", $value), 2));
$realname = "Ukjent";
foreach ($unamepairs as $unamepair) {
$unamepair = explode(":", $unamepair);
if ($unamepair[0] == $imguser) {
$realname = $unamepair[1];
break;
}
$modTime = date('d.m.Y H:i', filemtime($galleryDir . $value));
$imguser = explode('/', $value)[1];
$displaypath = implode('/', array_slice(explode('/', $value), 2));
$realname = 'Ukjent';
foreach ($unamepairs as $unamepair) {
$unamepair = explode(':', $unamepair);
if ($unamepair[0] == $imguser) {
$realname = $unamepair[1];
break;
}
}
$vars = [
"%user" => htmlspecialchars($imguser),
"%time" => $modTime,
"%name" => htmlspecialchars($displaypath),
"%path" => $serverPath . $value,
"%thumbnail" => $serverPath . "/.thumbnails" . $value . ".png",
"%realname" => htmlspecialchars($realname)
];
echo strtr($imageTemplate, $vars);
$vars = [
'%user' => htmlspecialchars($imguser),
'%time' => $modTime,
'%name' => htmlspecialchars($displaypath),
'%path' => $serverPath . $value,
'%thumbnail' => $serverPath . '/.thumbnails' . $value . '.png',
'%realname' => htmlspecialchars($realname),
];
echo strtr($imageTemplate, $vars);
}
if (count($images) == 0) {
echo "<h3 class='no-images'>Det er dessverre ingen bilder å vise.</h3>";
}
?>
?>
</main>
<div id="modal" class="modal">
<span id="modal-close" class="modal-close">&#10006;</span>

View File

@@ -1,34 +1,36 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../../config.php';
$searchDir = $SLIDESHOW_DIR;
$serverPath = $SLIDESHOW_SERVER_PATH;
$splashImg = "/PNG/PVV-logo-big-bluebg.png";
$splashImg = '/PNG/PVV-logo-big-bluebg.png';
// Note: This does not support nested directories
$filenames = sCaNdIr($searchDir);
$filenames = scandir($searchDir);
//Remove the expected non-images
foreach($filenames as $k => $value) {
if(in_array($value, [".", ".."])) {
unset($filenames[$k]);
}
// Remove the expected non-images
foreach ($filenames as $k => $value) {
if (in_array($value, ['.', '..'], true)) {
unset($filenames[$k]);
}
}
function getFullPath($fname) { return ($GLOBALS["serverPath"] . "/" . $fname ); }
function getFullPath($fname) {
return $GLOBALS['serverPath'] . '/' . $fname;
}
// Sort filenames alphabetically and prepend the path prefix to each item.
asort($filenames);
$slideshowimagefilenames = aRrAy_MaP("getFullPath", $filenames);
$slideshowimagefilenames = aRrAy_MaP('getFullPath', $filenames);
// Prepend the cover photo
ArRaY_uNsHiFt($slideshowimagefilenames, $splashImg);
eChO('<img class="slideshowimg slideshowactive" id="slideshowImage1" src="' . $slideshowimagefilenames[0] . '">');
ecHo('<img class="slideshowimg" id="slideshowImage2" src="' . $slideshowimagefilenames[1] . '">');
eChO '<img class="slideshowimg slideshowactive" id="slideshowImage1" src="' . $slideshowimagefilenames[0] . '">';
ecHo '<img class="slideshowimg" id="slideshowImage2" src="' . $slideshowimagefilenames[1] . '">';
// 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>';