2025-10-02 16:54:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
2025-11-01 22:50:02 +01:00
|
|
|
// Load modular components
|
2025-11-01 22:54:42 +01:00
|
|
|
require_once __DIR__ . '/constants.php';
|
2025-11-01 22:50:02 +01:00
|
|
|
require_once __DIR__ . '/helpers.php';
|
2025-11-01 22:54:42 +01:00
|
|
|
require_once __DIR__ . '/config.php';
|
2025-11-01 22:50:02 +01:00
|
|
|
require_once __DIR__ . '/content.php';
|
|
|
|
|
require_once __DIR__ . '/rendering.php';
|
2025-11-01 18:20:23 +01:00
|
|
|
|
2025-10-02 23:56:16 +02:00
|
|
|
// Check for assets in /custom/assets/ served at root level
|
|
|
|
|
$assetPath = dirname(__DIR__) . '/custom/assets/' . $requestPath;
|
|
|
|
|
if (file_exists($assetPath) && is_file($assetPath)) {
|
|
|
|
|
header('Content-Type: ' . (mime_content_type($assetPath) ?: 'application/octet-stream'));
|
|
|
|
|
readfile($assetPath);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Handle frontpage
|
|
|
|
|
if (empty($requestPath)) {
|
2025-11-01 18:20:23 +01:00
|
|
|
$contentFiles = findAllContentFiles($contentDir, $currentLang, $defaultLang);
|
|
|
|
|
if (!empty($contentFiles)) {
|
|
|
|
|
renderMultipleFiles($contentFiles, $contentDir);
|
2025-10-02 16:54:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse and handle request
|
2025-11-01 18:20:23 +01:00
|
|
|
$parsedPath = parseRequestPath($requestPath, $contentDir, $hasTrailingSlash, $currentLang, $defaultLang);
|
2025-10-02 16:54:47 +02:00
|
|
|
|
|
|
|
|
switch ($parsedPath['type']) {
|
2025-11-01 18:20:23 +01:00
|
|
|
case 'page':
|
|
|
|
|
// Page-type folder with content files (no subdirectories)
|
|
|
|
|
// Redirect to add trailing slash if needed
|
|
|
|
|
if (!empty($parsedPath['needsSlash'])) {
|
|
|
|
|
header('Location: ' . rtrim($_SERVER['REQUEST_URI'], '/') . '/', true, 301);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
renderMultipleFiles($parsedPath['files'], $parsedPath['path']);
|
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
case 'file':
|
2025-11-01 18:20:23 +01:00
|
|
|
// Direct file access or legacy single file
|
2025-10-02 16:54:47 +02:00
|
|
|
// Redirect to add trailing slash if this is a directory-based page
|
|
|
|
|
if (!empty($parsedPath['needsSlash'])) {
|
|
|
|
|
header('Location: ' . rtrim($_SERVER['REQUEST_URI'], '/') . '/', true, 301);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
renderFile($parsedPath['path']);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
case 'directory':
|
|
|
|
|
$dir = $parsedPath['path'];
|
|
|
|
|
if (file_exists("$dir/index.php")) {
|
|
|
|
|
renderFile("$dir/index.php");
|
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-11-01 18:20:23 +01:00
|
|
|
// Check for page content files in this directory
|
2025-10-02 22:50:31 +02:00
|
|
|
$pageContent = null;
|
2025-11-01 18:20:23 +01:00
|
|
|
$contentFiles = findAllContentFiles($dir, $currentLang, $defaultLang);
|
|
|
|
|
if (!empty($contentFiles)) {
|
2025-11-01 22:54:42 +01:00
|
|
|
$pageContent = implode('', array_map('renderContentFile', $contentFiles));
|
2025-10-02 22:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load metadata for this directory
|
|
|
|
|
$metadata = loadMetadata($dir, $currentLang, $defaultLang);
|
|
|
|
|
|
2025-10-15 19:30:27 +02:00
|
|
|
// Select list template based on metadata page_template
|
|
|
|
|
if (isset($metadata['page_template']) && !empty($metadata['page_template'])) {
|
|
|
|
|
$templateName = $metadata['page_template'];
|
|
|
|
|
// Add .php extension if not present
|
|
|
|
|
if (!str_ends_with($templateName, '.php')) {
|
2025-11-01 22:54:42 +01:00
|
|
|
$templateName = str_replace('.php', '', $templateName);
|
2025-10-15 19:30:27 +02:00
|
|
|
}
|
2025-11-01 22:54:42 +01:00
|
|
|
$customTemplate = dirname(__DIR__) . "/custom/templates/$templateName.php";
|
|
|
|
|
$defaultTemplate = __DIR__ . "/default/templates/$templateName.php";
|
2025-10-15 19:30:27 +02:00
|
|
|
|
|
|
|
|
if (file_exists($customTemplate)) {
|
|
|
|
|
$listTemplate = $customTemplate;
|
|
|
|
|
} elseif (file_exists($defaultTemplate)) {
|
|
|
|
|
$listTemplate = $defaultTemplate;
|
|
|
|
|
}
|
|
|
|
|
// If template doesn't exist, fall back to default $listTemplate
|
2025-10-02 22:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-01 22:54:42 +01:00
|
|
|
// Build list items
|
|
|
|
|
$subdirs = getSubdirectories($dir);
|
|
|
|
|
$langPrefix = getLangPrefix($currentLang, $defaultLang);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-11-01 22:54:42 +01:00
|
|
|
$items = array_filter(array_map(function($item) use ($dir, $requestPath, $currentLang, $defaultLang, $langPrefix) {
|
2025-10-02 16:54:47 +02:00
|
|
|
$itemPath = "$dir/$item";
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Check if content exists for current language
|
|
|
|
|
if ($currentLang !== $defaultLang) {
|
2025-11-01 18:20:23 +01:00
|
|
|
$contentFiles = findAllContentFiles($itemPath, $currentLang, $defaultLang);
|
|
|
|
|
if (empty($contentFiles)) return null;
|
2025-10-02 16:54:47 +02:00
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$metadata = loadMetadata($itemPath, $currentLang, $defaultLang);
|
|
|
|
|
$coverImage = findCoverImage($itemPath);
|
2025-10-14 21:08:57 +02:00
|
|
|
$pdfFile = findPdfFile($itemPath);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-11-01 18:20:23 +01:00
|
|
|
$title = $metadata['title'] ?? extractTitle($itemPath, $currentLang, $defaultLang) ?? $item;
|
2025-10-02 16:54:47 +02:00
|
|
|
$date = null;
|
|
|
|
|
if (isset($metadata['date'])) {
|
|
|
|
|
$date = formatNorwegianDate($metadata['date']);
|
|
|
|
|
} else {
|
|
|
|
|
$date = extractDateFromFolder($item) ?: date("F d, Y", filemtime($itemPath));
|
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Use translated slug if available, otherwise use folder name
|
2025-10-02 20:16:52 +02:00
|
|
|
$urlSlug = ($currentLang !== $defaultLang && $metadata && isset($metadata['slug']))
|
|
|
|
|
? $metadata['slug']
|
2025-10-02 16:54:47 +02:00
|
|
|
: $item;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-11-01 22:54:42 +01:00
|
|
|
$baseUrl = $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug);
|
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
return [
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'date' => $date,
|
2025-11-01 22:54:42 +01:00
|
|
|
'url' => $baseUrl,
|
|
|
|
|
'cover' => $coverImage ? "$baseUrl/$coverImage" : null,
|
2025-10-14 21:08:57 +02:00
|
|
|
'summary' => $metadata['summary'] ?? null,
|
2025-11-01 22:54:42 +01:00
|
|
|
'pdf' => $pdfFile ? "$baseUrl/$pdfFile" : null,
|
2025-10-20 22:18:31 +02:00
|
|
|
'redirect' => $metadata['redirect'] ?? null
|
2025-10-02 16:54:47 +02:00
|
|
|
];
|
|
|
|
|
}, $subdirs));
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
ob_start();
|
|
|
|
|
include $listTemplate;
|
|
|
|
|
$content = ob_get_clean();
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Build navigation for base template
|
2025-11-01 18:20:23 +01:00
|
|
|
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
include $baseTemplate;
|
|
|
|
|
exit;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
case 'not_found':
|
|
|
|
|
renderTemplate("<h1>404 Not Found</h1><p>The requested resource was not found.</p>", 404);
|
|
|
|
|
}
|