Add constants for file extensions Extract helper functions for common operations Improve PDF file detection Simplify directory scanning operations Standardize template resolution Optimize content rendering pipeline
137 lines
5.2 KiB
PHP
137 lines
5.2 KiB
PHP
<?php
|
|
|
|
// Load modular components
|
|
require_once __DIR__ . '/constants.php';
|
|
require_once __DIR__ . '/helpers.php';
|
|
require_once __DIR__ . '/config.php';
|
|
require_once __DIR__ . '/content.php';
|
|
require_once __DIR__ . '/rendering.php';
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Handle frontpage
|
|
if (empty($requestPath)) {
|
|
$contentFiles = findAllContentFiles($contentDir, $currentLang, $defaultLang);
|
|
if (!empty($contentFiles)) {
|
|
renderMultipleFiles($contentFiles, $contentDir);
|
|
}
|
|
}
|
|
|
|
// Parse and handle request
|
|
$parsedPath = parseRequestPath($requestPath, $contentDir, $hasTrailingSlash, $currentLang, $defaultLang);
|
|
|
|
switch ($parsedPath['type']) {
|
|
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']);
|
|
|
|
case 'file':
|
|
// Direct file access or legacy single file
|
|
// 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']);
|
|
|
|
case 'directory':
|
|
$dir = $parsedPath['path'];
|
|
if (file_exists("$dir/index.php")) {
|
|
renderFile("$dir/index.php");
|
|
}
|
|
|
|
// Check for page content files in this directory
|
|
$pageContent = null;
|
|
$contentFiles = findAllContentFiles($dir, $currentLang, $defaultLang);
|
|
if (!empty($contentFiles)) {
|
|
$pageContent = implode('', array_map('renderContentFile', $contentFiles));
|
|
}
|
|
|
|
// Load metadata for this directory
|
|
$metadata = loadMetadata($dir, $currentLang, $defaultLang);
|
|
|
|
// 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')) {
|
|
$templateName = str_replace('.php', '', $templateName);
|
|
}
|
|
$customTemplate = dirname(__DIR__) . "/custom/templates/$templateName.php";
|
|
$defaultTemplate = __DIR__ . "/default/templates/$templateName.php";
|
|
|
|
if (file_exists($customTemplate)) {
|
|
$listTemplate = $customTemplate;
|
|
} elseif (file_exists($defaultTemplate)) {
|
|
$listTemplate = $defaultTemplate;
|
|
}
|
|
// If template doesn't exist, fall back to default $listTemplate
|
|
}
|
|
|
|
// Build list items
|
|
$subdirs = getSubdirectories($dir);
|
|
$langPrefix = getLangPrefix($currentLang, $defaultLang);
|
|
|
|
$items = array_filter(array_map(function($item) use ($dir, $requestPath, $currentLang, $defaultLang, $langPrefix) {
|
|
$itemPath = "$dir/$item";
|
|
|
|
// Check if content exists for current language
|
|
if ($currentLang !== $defaultLang) {
|
|
$contentFiles = findAllContentFiles($itemPath, $currentLang, $defaultLang);
|
|
if (empty($contentFiles)) return null;
|
|
}
|
|
|
|
$metadata = loadMetadata($itemPath, $currentLang, $defaultLang);
|
|
$coverImage = findCoverImage($itemPath);
|
|
$pdfFile = findPdfFile($itemPath);
|
|
|
|
$title = $metadata['title'] ?? extractTitle($itemPath, $currentLang, $defaultLang) ?? $item;
|
|
$date = null;
|
|
if (isset($metadata['date'])) {
|
|
$date = formatNorwegianDate($metadata['date']);
|
|
} else {
|
|
$date = extractDateFromFolder($item) ?: date("F d, Y", filemtime($itemPath));
|
|
}
|
|
|
|
// Use translated slug if available, otherwise use folder name
|
|
$urlSlug = ($currentLang !== $defaultLang && $metadata && isset($metadata['slug']))
|
|
? $metadata['slug']
|
|
: $item;
|
|
|
|
$baseUrl = $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug);
|
|
|
|
return [
|
|
'title' => $title,
|
|
'date' => $date,
|
|
'url' => $baseUrl,
|
|
'cover' => $coverImage ? "$baseUrl/$coverImage" : null,
|
|
'summary' => $metadata['summary'] ?? null,
|
|
'pdf' => $pdfFile ? "$baseUrl/$pdfFile" : null,
|
|
'redirect' => $metadata['redirect'] ?? null
|
|
];
|
|
}, $subdirs));
|
|
|
|
ob_start();
|
|
include $listTemplate;
|
|
$content = ob_get_clean();
|
|
|
|
// Build navigation for base template
|
|
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang);
|
|
|
|
include $baseTemplate;
|
|
exit;
|
|
|
|
case 'not_found':
|
|
renderTemplate("<h1>404 Not Found</h1><p>The requested resource was not found.</p>", 404);
|
|
}
|