Created a file viewer and integrated it into the website.

This commit is contained in:
The Wobbler 2024-10-25 23:00:53 +02:00
parent fc34947b48
commit eb785ae012
3 changed files with 96 additions and 0 deletions

50
file_viewer.php Normal file
View file

@ -0,0 +1,50 @@
<?php
function completeScanDir($dir) {
$result = [];
foreach(scandir($dir) as $filename) {
if ($filename[0] === '.') continue;
$filePath = $dir . '/' . $filename;
if (is_dir($filePath)) {
$result[$filePath] = completeScanDir($filePath);
} else {
$result[$filePath] = $filename;
}
}
return $result;
}
function generateCSSCode($directory) {
foreach ($directory as $path => $filename) {
if (is_array($filename)) { // $filename is either a filename or an array of filenames, if so, it represents a directory
$useableName = str_replace("/", "_", $path);
echo "#input_$useableName:checked + .submenu_$useableName,
.submenu_$useableName:hover {
display: block;
}\n";
generateCSSCode($filename);
}
}
}
function generateSubmenusHTML($directory, $indentation) {
foreach ($directory as $path => $filename) {
$useableName = str_replace("/", "_", $path); // CSS classes cant contain slashes
if (is_array($filename)) {
$folderName = end(explode("/", $path));
echo " $indentation<label for='input_$useableName' class='inset hcb-label label_$useableName'>$folderName</label>
$indentation<input type='checkbox' id='input_$useableName' class='hacked-checkbox'>
$indentation<div class='glass inset submenu submenu_$useableName'>\n";
generateSubmenusHTML($filename, "$indentation ");
echo " $indentation</div>
$indentation<br>\n";
} else {
echo " $indentation<a href='$path' class='file-link'>$filename</a>\n";
}
echo " $indentation<br>\n";
}
}
?>

22
file_viewer_styles.css Normal file
View file

@ -0,0 +1,22 @@
.submenu {
display: none;
margin: 8px 0px 0px;
padding: 16px 0px 0px 16px;
}
.file-link {
padding: 0px 16px 0px 16px;
display: block;
}
.hacked-checkbox {
position: absolute;
left: -100vw;
}
.hcb-label {
padding: 8px 16px;
margin: 16px;
cursor: pointer;
border-radius: 16px 16px 0px 0px;
}

View file

@ -1,3 +1,9 @@
<?php
header("Content-type: text/html");
include "file_viewer.php";
?>
<!DOCTYPE html>
<html lang="de"><meta charset="utf-8">
@ -8,6 +14,14 @@
<link rel="icon" sizes="192x192" href="profilbild-workbench.png">
<link rel="stylesheet" href="styles.php">
<link rel="stylesheet" href="file_viewer_styles.css">
<?php
echo " <style>
/* This CSS code is php generated. Hardcoding it would be a programming war crime. :3 */\n\n ";
echo generateCSSCode(completeScanDir("files"));
echo " </style>";
?>
<script type="text/javascript" src="script.js"></script>
@ -108,6 +122,16 @@
<br>
<br>
</div>
<div class="glass inset">
<p>
Hab hier nen kleinen Datenschrottplatz an Mist,
<br>
den ich mal mit irgendwem teilen musste und ich deshalb hier auf den Server geladen hab.
</p>
<?= generateSubmenusHTML(completeScanDir("files"), " ") ?>
</div>
</div>
<div class="topnav-companion"></div>