Navigation

This commit is contained in:
Dusk 2022-01-27 16:31:53 +01:00
parent e0e1097b49
commit c28756c6fa
2 changed files with 54 additions and 13 deletions

27
include/utils.php Normal file
View File

@ -0,0 +1,27 @@
<?php
function get_protocol() :string {
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
return "https://";
else
return "http://";
}
/* Pass either 'http://' or 'https://' asc parameter*/
function base_url(string $protocol = 'http://') {
// Append the host(domain name, ip) to the URL.
$url = $protocol . $_SERVER['HTTP_HOST'];
return $url . '/';
}
function parent_url(string $protocol = 'http://') : string {
// Append the host(domain name, ip) to the URL.
$url = $protocol . $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$url .= $_SERVER['REQUEST_URI'];
// If URL is already the base
if (base_url($protocol) === $url)
return $url;
return dirname($url);
}

View File

@ -3,7 +3,9 @@
<?php <?php
include 'include/header.php'; include 'include/header.php';
include 'include/utils.php';
/* Functions */
function link_from_file(string $filename, string $name = null, string $css_class = null ) : string { function link_from_file(string $filename, string $name = null, string $css_class = null ) : string {
// Check if css_class is null // Check if css_class is null
$css_class = $css_class ? " class=\"$css_class\"" : ''; $css_class = $css_class ? " class=\"$css_class\"" : '';
@ -31,29 +33,21 @@ function filter_files_dir($files_array, string $path) : array {
return $ret_array; return $ret_array;
} }
// HTML Head
echo($header); echo($header);
echo("\n"); echo("\n");
?> /* Misc. Variables */
<body>
<h1>Magatzem de Dusk</h1>
<?php
// Debug
//var_dump($_SERVER);
// Debug
// Debug
//var_dump($files_in_folder);
// Debug
$not_found_error = '<h1>File not found, error 404</h1>'; $not_found_error = '<h1>File not found, error 404</h1>';
$basedir = 'content'; $basedir = 'content';
$request_uri = $_SERVER['REQUEST_URI']; $request_uri = $_SERVER['REQUEST_URI'];
$path = $basedir . $request_uri; $path = $basedir . $request_uri;
/**/
// Check if URI is correct
if(!is_dir($path)) { if(!is_dir($path)) {
echo($not_found_error); echo($not_found_error);
die(); die();
@ -62,6 +56,14 @@ if(!is_dir($path)) {
$files_in_folder = scandir($path); $files_in_folder = scandir($path);
$resource_array = filter_files_dir($files_in_folder, $path); $resource_array = filter_files_dir($files_in_folder, $path);
/* Debug */
/* var_dump($_SERVER);
* var_dump($files_in_folder); */
?>
<body>
<h1>Magatzem de Dusk</h1>
<?php
// Show folder links // Show folder links
{ {
if(!empty($resource_array['dir'])) { if(!empty($resource_array['dir'])) {
@ -92,6 +94,18 @@ echo("\n");
echo("\n"); echo("\n");
$base_url = base_url();
$parent_url = parent_url();
//Navigation
echo <<<NAV
<nav>
<ul>
<li><a href={$base_url}>Inici</a></li>
<li><a href={$parent_url}>Arrere</a></li>
</ul>
</nav>
NAV;
?> ?>
</body> </body>
</html> </html>