2025-10-02 16:54:47 +02:00
|
|
|
<?php
|
|
|
|
// Load configuration
|
2025-10-02 20:16:52 +02:00
|
|
|
$configFile = file_exists(__DIR__ . '/../custom/config.ini')
|
|
|
|
? __DIR__ . '/../custom/config.ini'
|
2025-10-02 16:54:47 +02:00
|
|
|
: __DIR__ . '/config.ini';
|
|
|
|
$config = parse_ini_file($configFile, true);
|
|
|
|
|
|
|
|
$defaultLang = $config['languages']['default'] ?? 'no';
|
|
|
|
$availableLangs = array_map('trim', explode(',', $config['languages']['available'] ?? 'no'));
|
|
|
|
|
|
|
|
$contentDir = realpath($_SERVER['DOCUMENT_ROOT']);
|
|
|
|
$requestUri = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?: '/';
|
|
|
|
$hasTrailingSlash = str_ends_with($requestUri, '/') && $requestUri !== '/';
|
|
|
|
$requestPath = trim($requestUri, '/');
|
|
|
|
|
|
|
|
// Extract language from URL
|
|
|
|
$currentLang = $defaultLang;
|
|
|
|
$pathParts = explode('/', $requestPath);
|
|
|
|
if (!empty($pathParts[0]) && in_array($pathParts[0], $availableLangs) && $pathParts[0] !== $defaultLang) {
|
|
|
|
$currentLang = $pathParts[0];
|
|
|
|
array_shift($pathParts);
|
|
|
|
$requestPath = implode('/', $pathParts);
|
|
|
|
}
|
|
|
|
|
2025-10-03 11:40:02 +02:00
|
|
|
// Use custom templates with fallback to defaults
|
|
|
|
$customBaseTemplate = dirname(__DIR__) . '/custom/templates/base.php';
|
|
|
|
$defaultBaseTemplate = __DIR__ . '/default/templates/base.php';
|
|
|
|
$baseTemplate = file_exists($customBaseTemplate) ? $customBaseTemplate : $defaultBaseTemplate;
|
|
|
|
|
|
|
|
$customPageTemplate = dirname(__DIR__) . '/custom/templates/page.php';
|
|
|
|
$defaultPageTemplate = __DIR__ . '/default/templates/page.php';
|
|
|
|
$pageTemplate = file_exists($customPageTemplate) ? $customPageTemplate : $defaultPageTemplate;
|
|
|
|
|
|
|
|
$customListTemplate = dirname(__DIR__) . '/custom/templates/list.php';
|
|
|
|
$defaultListTemplate = __DIR__ . '/default/templates/list.php';
|
|
|
|
$listTemplate = file_exists($customListTemplate) ? $customListTemplate : $defaultListTemplate;
|
2025-10-02 16:54:47 +02:00
|
|
|
|
|
|
|
// Build file patterns with language variants
|
|
|
|
function buildFilePatterns(string $lang, string $defaultLang): array {
|
|
|
|
$extensions = ['php', 'html', 'md'];
|
|
|
|
$patterns = ['page' => [], 'single' => []];
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
foreach ($extensions as $ext) {
|
|
|
|
// Language-specific files first (if not default language)
|
|
|
|
if ($lang !== $defaultLang) {
|
|
|
|
$patterns['page'][] = "page.$lang.$ext";
|
|
|
|
$patterns['single'][] = "single.$lang.$ext";
|
|
|
|
$patterns['single'][] = "post.$lang.$ext";
|
|
|
|
$patterns['single'][] = "article.$lang.$ext";
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Default files
|
|
|
|
$patterns['page'][] = "page.$ext";
|
|
|
|
$patterns['single'][] = "single.$ext";
|
|
|
|
$patterns['single'][] = "post.$ext";
|
|
|
|
$patterns['single'][] = "article.$ext";
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
return $patterns;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pageFilePatterns = buildFilePatterns($currentLang, $defaultLang);
|
|
|
|
|
|
|
|
function findMatchingFile(string $dir, array $patterns): ?string {
|
|
|
|
foreach ($patterns as $pattern) {
|
|
|
|
if (file_exists($file = "$dir/$pattern")) return $file;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveTranslatedPath(string $requestPath, string $contentDir, string $lang, string $defaultLang): string {
|
|
|
|
// If default language, no translation needed
|
|
|
|
if ($lang === $defaultLang) {
|
|
|
|
return $requestPath;
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$parts = explode('/', trim($requestPath, '/'));
|
|
|
|
$resolvedParts = [];
|
|
|
|
$currentPath = $contentDir;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
foreach ($parts as $segment) {
|
|
|
|
if (empty($segment)) continue;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Check all subdirectories for slug matches
|
|
|
|
$found = false;
|
|
|
|
if (is_dir($currentPath)) {
|
|
|
|
$subdirs = array_filter(
|
|
|
|
scandir($currentPath) ?: [],
|
|
|
|
fn($item) => !in_array($item, ['.', '..']) && is_dir("$currentPath/$item")
|
|
|
|
);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
foreach ($subdirs as $dir) {
|
|
|
|
$metadata = loadMetadata("$currentPath/$dir", $lang, $defaultLang);
|
|
|
|
if ($metadata && isset($metadata['slug']) && $metadata['slug'] === $segment) {
|
|
|
|
$resolvedParts[] = $dir;
|
|
|
|
$currentPath .= "/$dir";
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// If no slug match, use segment as-is
|
|
|
|
if (!$found) {
|
|
|
|
$resolvedParts[] = $segment;
|
|
|
|
$currentPath .= "/$segment";
|
|
|
|
}
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
return implode('/', $resolvedParts);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseRequestPath(string $requestPath, string $contentDir, array $patterns, bool $hasTrailingSlash, string $lang, string $defaultLang): array {
|
|
|
|
// Resolve translated slugs to actual directory names
|
|
|
|
$resolvedPath = resolveTranslatedPath($requestPath, $contentDir, $lang, $defaultLang);
|
|
|
|
$contentPath = rtrim($contentDir, '/') . '/' . ltrim($resolvedPath, '/');
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
if (is_file($contentPath)) {
|
|
|
|
return ['type' => 'file', 'path' => realpath($contentPath)];
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
if (is_dir($contentPath)) {
|
2025-10-02 22:50:31 +02:00
|
|
|
// Check if directory has subdirectories
|
|
|
|
$hasSubdirs = !empty(array_filter(
|
|
|
|
scandir($contentPath) ?: [],
|
|
|
|
fn($item) => !in_array($item, ['.', '..']) && is_dir("$contentPath/$item")
|
|
|
|
));
|
|
|
|
|
|
|
|
// If directory has subdirectories, treat as directory (for list views)
|
|
|
|
// Otherwise, if it has a content file, treat as file
|
|
|
|
if (!$hasSubdirs && ($file = findMatchingFile($contentPath, $patterns['single']) ?: findMatchingFile($contentPath, $patterns['page']))) {
|
2025-10-02 16:54:47 +02:00
|
|
|
return ['type' => 'file', 'path' => $file, 'needsSlash' => !$hasTrailingSlash];
|
|
|
|
}
|
|
|
|
return ['type' => 'directory', 'path' => realpath($contentPath)];
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
return ['type' => 'not_found', 'path' => $contentPath];
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadMetadata(string $dirPath, string $lang, string $defaultLang): ?array {
|
|
|
|
$metadataFile = "$dirPath/metadata.ini";
|
|
|
|
if (!file_exists($metadataFile)) return null;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$metadata = parse_ini_file($metadataFile, true);
|
|
|
|
if (!$metadata) return null;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Extract base metadata (non-section values)
|
|
|
|
$baseMetadata = array_filter($metadata, fn($key) => !is_array($metadata[$key]), ARRAY_FILTER_USE_KEY);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// If current language is not default, merge language-specific overrides
|
|
|
|
if ($lang !== $defaultLang && isset($metadata[$lang]) && is_array($metadata[$lang])) {
|
|
|
|
$baseMetadata = array_merge($baseMetadata, $metadata[$lang]);
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
return $baseMetadata ?: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractTitle(string $filePath, array $patterns): ?string {
|
|
|
|
$file = findMatchingFile($filePath, $patterns['single']) ?: findMatchingFile($filePath, $patterns['page']);
|
|
|
|
if (!$file) return null;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
|
|
|
$content = file_get_contents($file);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
if ($ext === 'md' && preg_match('/^#\s+(.+)$/m', $content, $matches)) {
|
|
|
|
return trim($matches[1]);
|
|
|
|
}
|
|
|
|
if (in_array($ext, ['html', 'php']) && preg_match('/<h1[^>]*>(.*?)<\/h1>/i', $content, $matches)) {
|
|
|
|
return strip_tags($matches[1]);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatNorwegianDate(string $dateString): string {
|
|
|
|
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $dateString, $matches)) {
|
|
|
|
$months = ['januar', 'februar', 'mars', 'april', 'mai', 'juni', 'juli', 'august', 'september', 'oktober', 'november', 'desember'];
|
|
|
|
$day = (int)$matches[3];
|
|
|
|
$month = $months[(int)$matches[2] - 1];
|
|
|
|
$year = $matches[1];
|
|
|
|
return "$day. $month $year";
|
|
|
|
}
|
|
|
|
return $dateString;
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractDateFromFolder(string $folderName): ?string {
|
|
|
|
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})-/', $folderName, $matches)) {
|
|
|
|
return formatNorwegianDate($matches[1] . '-' . $matches[2] . '-' . $matches[3]);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findCoverImage(string $dirPath): ?string {
|
|
|
|
$extensions = ['jpg', 'jpeg', 'png', 'webp', 'gif'];
|
|
|
|
foreach ($extensions as $ext) {
|
|
|
|
if (file_exists("$dirPath/cover.$ext")) {
|
|
|
|
return "cover.$ext";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2025-10-02 20:16:52 +02:00
|
|
|
function loadTranslations(string $lang): array {
|
|
|
|
$translationFile = dirname(__DIR__) . "/custom/languages/$lang.ini";
|
|
|
|
if (file_exists($translationFile)) {
|
|
|
|
return parse_ini_file($translationFile) ?: [];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
function buildNavigation(string $contentDir, string $currentLang, string $defaultLang, array $pageFilePatterns): array {
|
|
|
|
$navItems = [];
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Scan top-level directories in content
|
|
|
|
$items = array_filter(
|
|
|
|
scandir($contentDir) ?: [],
|
|
|
|
fn($item) => !in_array($item, ['.', '..']) && is_dir("$contentDir/$item")
|
|
|
|
);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
foreach ($items as $item) {
|
|
|
|
$itemPath = "$contentDir/$item";
|
|
|
|
$metadata = loadMetadata($itemPath, $currentLang, $defaultLang);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Check if this item should be in menu
|
|
|
|
if (!$metadata || empty($metadata['menu'])) {
|
|
|
|
continue;
|
|
|
|
}
|
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) {
|
|
|
|
$extensions = ['php', 'html', 'md'];
|
|
|
|
$hasContent = false;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
|
|
|
// Check for language-specific content files
|
2025-10-02 16:54:47 +02:00
|
|
|
foreach ($extensions as $ext) {
|
|
|
|
if (file_exists("$itemPath/single.$currentLang.$ext") ||
|
|
|
|
file_exists("$itemPath/post.$currentLang.$ext") ||
|
|
|
|
file_exists("$itemPath/article.$currentLang.$ext") ||
|
|
|
|
file_exists("$itemPath/page.$currentLang.$ext")) {
|
|
|
|
$hasContent = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
|
|
|
// If no language-specific files, check if metadata has title for this language
|
|
|
|
if (!$hasContent && $metadata && isset($metadata['title'])) {
|
|
|
|
$hasContent = true;
|
|
|
|
}
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
if (!$hasContent) continue;
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Extract title and build URL
|
2025-10-02 20:16:52 +02:00
|
|
|
$title = $metadata['title'] ?? extractTitle($itemPath, $pageFilePatterns) ?? ucfirst($item);
|
2025-10-02 16:54:47 +02:00
|
|
|
$langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : '';
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Use translated slug if available
|
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-10-02 16:54:47 +02:00
|
|
|
$navItems[] = [
|
|
|
|
'title' => $title,
|
|
|
|
'url' => $langPrefix . '/' . urlencode($urlSlug) . '/',
|
|
|
|
'order' => (int)($metadata['menu_order'] ?? 999)
|
|
|
|
];
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Sort by menu_order
|
|
|
|
usort($navItems, fn($a, $b) => $a['order'] <=> $b['order']);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
return $navItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderTemplate(string $content, int $statusCode = 200): void {
|
|
|
|
global $baseTemplate, $contentDir, $currentLang, $defaultLang, $pageFilePatterns;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Build navigation for templates
|
|
|
|
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang, $pageFilePatterns);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Load frontpage metadata for home button label
|
|
|
|
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
|
|
|
|
$homeLabel = $frontpageMetadata['slug'] ?? 'Home';
|
2025-10-02 20:16:52 +02:00
|
|
|
|
|
|
|
// Load translations
|
|
|
|
$translations = loadTranslations($currentLang);
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
http_response_code($statusCode);
|
|
|
|
include $baseTemplate;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderFile(string $filePath): void {
|
2025-10-02 20:16:52 +02:00
|
|
|
global $baseTemplate, $pageTemplate, $contentDir, $currentLang, $defaultLang, $pageFilePatterns;
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$realPath = realpath($filePath);
|
|
|
|
if (!$realPath || !str_starts_with($realPath, $contentDir) || !is_readable($realPath)) {
|
2025-10-02 20:16:52 +02:00
|
|
|
renderTemplate("<article><h1>403 Forbidden</h1><p>Access denied.</p></article>", 403);
|
2025-10-02 16:54:47 +02:00
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$ext = pathinfo($realPath, PATHINFO_EXTENSION);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
if (in_array($ext, ['php', 'html', 'md'])) {
|
|
|
|
ob_start();
|
|
|
|
if ($ext === 'md') {
|
|
|
|
if (!class_exists('Parsedown')) {
|
|
|
|
require_once __DIR__ . '/vendor/Parsedown.php';
|
|
|
|
}
|
|
|
|
echo '<article>' . (new Parsedown())->text(file_get_contents($realPath)) . '</article>';
|
|
|
|
} else {
|
|
|
|
include $realPath;
|
|
|
|
}
|
|
|
|
$content = ob_get_clean();
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Build navigation for templates
|
|
|
|
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang, $pageFilePatterns);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Load metadata for current page/directory
|
|
|
|
$pageDir = dirname($realPath);
|
|
|
|
$pageMetadata = loadMetadata($pageDir, $currentLang, $defaultLang);
|
|
|
|
$pageTitle = $pageMetadata['title'] ?? null;
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Load frontpage metadata for home button label
|
|
|
|
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
|
|
|
|
$homeLabel = $frontpageMetadata['slug'] ?? 'Home';
|
2025-10-02 20:16:52 +02:00
|
|
|
|
|
|
|
// Load translations
|
|
|
|
$translations = loadTranslations($currentLang);
|
|
|
|
|
|
|
|
// Wrap content with page template
|
|
|
|
ob_start();
|
|
|
|
include $pageTemplate;
|
|
|
|
$content = ob_get_clean();
|
|
|
|
|
|
|
|
// Wrap with base template
|
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
|
|
|
// Serve other file types directly
|
|
|
|
header('Content-Type: ' . (mime_content_type($realPath) ?: 'application/octet-stream'));
|
|
|
|
readfile($realPath);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
// Try language-specific frontpage first, then default
|
|
|
|
$frontPage = null;
|
|
|
|
if ($currentLang !== $defaultLang && file_exists("$contentDir/frontpage.$currentLang.php")) {
|
|
|
|
$frontPage = "$contentDir/frontpage.$currentLang.php";
|
|
|
|
} elseif (file_exists("$contentDir/frontpage.php")) {
|
|
|
|
$frontPage = "$contentDir/frontpage.php";
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
if ($frontPage) {
|
|
|
|
renderFile($frontPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse and handle request
|
|
|
|
$parsedPath = parseRequestPath($requestPath, $contentDir, $pageFilePatterns, $hasTrailingSlash, $currentLang, $defaultLang);
|
|
|
|
|
|
|
|
switch ($parsedPath['type']) {
|
|
|
|
case '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']);
|
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-10-02 22:50:31 +02:00
|
|
|
// Check for page content file in this directory
|
|
|
|
$pageContent = null;
|
|
|
|
if ($pageFile = findMatchingFile($dir, $pageFilePatterns['page'])) {
|
|
|
|
$ext = pathinfo($pageFile, PATHINFO_EXTENSION);
|
|
|
|
ob_start();
|
|
|
|
if ($ext === 'md') {
|
|
|
|
if (!class_exists('Parsedown')) {
|
|
|
|
require_once __DIR__ . '/vendor/Parsedown.php';
|
|
|
|
}
|
|
|
|
echo (new Parsedown())->text(file_get_contents($pageFile));
|
|
|
|
} else {
|
|
|
|
include $pageFile;
|
|
|
|
}
|
|
|
|
$pageContent = ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load metadata for this directory
|
|
|
|
$metadata = loadMetadata($dir, $currentLang, $defaultLang);
|
|
|
|
|
|
|
|
// Check for custom list template (prefer list-grid.php over list.php)
|
|
|
|
$customListGridTemplate = dirname(__DIR__) . '/custom/templates/list-grid.php';
|
|
|
|
if (file_exists($customListGridTemplate)) {
|
|
|
|
$listTemplate = $customListGridTemplate;
|
|
|
|
}
|
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Default directory listing
|
|
|
|
$subdirs = array_filter(
|
|
|
|
scandir($dir) ?: [],
|
|
|
|
fn($item) => !in_array($item, ['.', '..']) && is_dir("$dir/$item")
|
|
|
|
);
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$items = array_filter(array_map(function($item) use ($dir, $requestPath, $currentLang, $defaultLang, $pageFilePatterns) {
|
|
|
|
$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) {
|
|
|
|
// For non-default languages, only check language-specific files
|
|
|
|
$extensions = ['php', 'html', 'md'];
|
|
|
|
$hasContent = false;
|
|
|
|
foreach ($extensions as $ext) {
|
|
|
|
if (file_exists("$itemPath/single.$currentLang.$ext") ||
|
|
|
|
file_exists("$itemPath/post.$currentLang.$ext") ||
|
|
|
|
file_exists("$itemPath/article.$currentLang.$ext") ||
|
|
|
|
file_exists("$itemPath/page.$currentLang.$ext")) {
|
|
|
|
$hasContent = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$hasContent) return null;
|
|
|
|
}
|
2025-10-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
// Build patterns for rendering (includes fallbacks)
|
|
|
|
$patterns = buildFilePatterns($currentLang, $defaultLang);
|
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-02 20:16:52 +02:00
|
|
|
|
2025-10-02 16:54:47 +02:00
|
|
|
$title = $metadata['title'] ?? extractTitle($itemPath, $patterns) ?? $item;
|
|
|
|
$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
|
|
|
$langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : '';
|
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-10-02 16:54:47 +02:00
|
|
|
return [
|
|
|
|
'title' => $title,
|
|
|
|
'date' => $date,
|
|
|
|
'url' => $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug),
|
|
|
|
'cover' => $coverImage ? $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug) . '/' . $coverImage : null,
|
|
|
|
'summary' => $metadata['summary'] ?? null
|
|
|
|
];
|
|
|
|
}, $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
|
|
|
|
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang, $pageFilePatterns);
|
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);
|
|
|
|
}
|