NeoKino-PHP/index.php

114 lines
2.1 KiB
PHP
Raw Normal View History

2022-01-26 23:25:24 +00:00
<!DOCTYPE html>
<html>
2022-01-26 23:25:24 +00:00
<?php
include 'include/header.php';
2022-01-27 15:31:53 +00:00
include 'include/utils.php';
2022-01-27 15:31:53 +00:00
/* Functions */
2022-01-26 23:25:24 +00:00
function link_from_file(string $filename, string $name = null, string $css_class = null ) : string {
// Check if css_class is null
$css_class = $css_class ? " class=\"$css_class\"" : '';
2022-01-26 23:25:24 +00:00
// Check if name is null
$name = $name ?: basename($filename);
return sprintf('<a href=%s%s>%s</a>', $filename, $css_class, $name);
2022-01-26 23:25:24 +00:00
}
function filter_files_dir($files_array, string $path) : array {
$ret_array = ['dir' => [], 'file' => []];
foreach($files_array as $file) {
// Hidden Files are not shown
if(substr($file, 0, 1) == '.') {
continue;
}
2022-01-26 23:25:24 +00:00
if(is_dir($path . $file)) {
// Append dir to array
$ret_array['dir'][] = $file;
}
else {
// Append file to array
$ret_array['file'][] = $file;
}
}
return $ret_array;
}
2022-01-27 15:31:53 +00:00
// HTML Head
echo($header);
echo("\n");
2022-01-27 15:31:53 +00:00
/* Misc. Variables */
2022-01-26 23:30:51 +00:00
$not_found_error = '<h1>File not found, error 404</h1>';
$basedir = 'content';
$request_uri = $_SERVER['REQUEST_URI'];
$path = $basedir . $request_uri;
2022-01-27 15:31:53 +00:00
/**/
2022-01-26 23:30:51 +00:00
2022-01-27 15:31:53 +00:00
// Check if URI is correct
2022-01-26 23:40:40 +00:00
if(!is_dir($path)) {
echo($not_found_error);
die();
}
$files_in_folder = scandir($path);
2022-01-26 23:25:24 +00:00
$resource_array = filter_files_dir($files_in_folder, $path);
2022-01-27 15:31:53 +00:00
/* Debug */
/* var_dump($_SERVER);
* var_dump($files_in_folder); */
?>
<body>
<h1>Magatzem de Dusk</h1>
<?php
2022-01-26 23:25:24 +00:00
// Show folder links
{
if(!empty($resource_array['dir'])) {
echo('<h2>Directoris</h2>');
}
echo('<ul>');
2022-01-26 23:25:24 +00:00
foreach($resource_array['dir'] as $dir) {
echo('<li>' . link_from_file($request_uri . $dir, null, "dir_link") . '</li>');
2022-01-26 23:25:24 +00:00
echo('<br/>');
}
echo('</ul>');
2022-01-26 23:25:24 +00:00
}
echo("\n");
2022-01-26 23:25:24 +00:00
//Show file links
{
if(!empty($resource_array['file'])) {
echo('<h2>Fitxers</h2>');
}
echo('<ul>');
2022-01-26 23:25:24 +00:00
foreach($resource_array['file'] as $file) {
echo('<li>' . link_from_file($request_uri . $file, null, 'file_link') . '</li>');
2022-01-26 23:25:24 +00:00
echo('<br/>');
}
echo('</ul>');
2022-01-26 23:25:24 +00:00
}
echo("\n");
2022-01-26 23:25:24 +00:00
2022-01-27 15:39:57 +00:00
$base_url = base_url(get_protocol());
$parent_url = parent_url(get_protocol());
2022-01-27 15:31:53 +00:00
//Navigation
echo <<<NAV
<nav>
<ul>
<li><a href={$base_url}>Inici</a></li>
<li><a href={$parent_url}>Arrere</a></li>
</ul>
</nav>
2022-01-27 15:39:57 +00:00
2022-01-27 15:31:53 +00:00
NAV;
2022-01-27 15:39:57 +00:00
?>
2022-01-26 23:25:24 +00:00
</body>
</html>