Initial commit
This commit is contained in:
commit
2994f7cf6d
16 changed files with 2766 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
/custom*
|
||||
/content*
|
||||
/docs*
|
||||
135
CLAUDE.md
Normal file
135
CLAUDE.md
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
# PnP Development Guidelines
|
||||
|
||||
## Philosophy
|
||||
|
||||
**Just enough, nothing more.** This framework applies minimal PHP to enable modern conveniences while remaining maintainable for years or decades. Avoid rapidly changing components and dependencies. The code should be readable, simple, and strictly necessary.
|
||||
|
||||
## Core Principles
|
||||
|
||||
### Minimalism
|
||||
- Use only what is strictly necessary to achieve the goal
|
||||
- No frameworks, no build tools, no package managers for frontend code
|
||||
- Avoid abstractions unless they provide clear, lasting value
|
||||
- Sparse commenting—only mark main sections
|
||||
|
||||
### Technology Stack
|
||||
- **Allowed:** HTML, PHP (8.3+), CSS
|
||||
- **Not allowed:** JavaScript
|
||||
- Use modern PHP features when they improve readability or performance
|
||||
- Leverage modern CSS features for smart, efficient styling
|
||||
|
||||
### File-Based Routing
|
||||
- Folder hierarchy dictates URL structure
|
||||
- Drop a file (`.md`, `.php`, `.html`) in a folder and it renders immediately
|
||||
- Assets placed in content directories are automatically accessible
|
||||
- Frontpage is handled by `/content/frontpage.php`
|
||||
- Directories with subfolders trigger list views
|
||||
|
||||
### Template System
|
||||
- Custom templates override defaults (never modify defaults)
|
||||
- Custom templates live in `/app/custom/`
|
||||
- Default templates provide fallback behavior
|
||||
- Docs get their own template variants for specialized presentation
|
||||
- Templates use PHP includes—simple and straightforward
|
||||
|
||||
### Content Conventions
|
||||
|
||||
#### File Naming
|
||||
- Page files: `page.php`, `page.html`, `page.md`
|
||||
- Single/article files: `article.md`, `post.md`, `single.md` (and `.php`/`.html` variants)
|
||||
- Index overrides: `index.php` in any directory takes precedence
|
||||
- Cover images: `cover.jpg`, `cover.webp`, etc.
|
||||
|
||||
#### Date Formatting
|
||||
- Folder names can include dates: `YYYY-MM-DD-title`
|
||||
- Dates are automatically extracted and formatted (Norwegian format: "23. oktober 2025")
|
||||
- Metadata can override automatic date detection
|
||||
|
||||
#### Metadata
|
||||
- Use `metadata.ini` files for structured data
|
||||
- Common fields: `title`, `date`, `summary`
|
||||
- Metadata overrides automatic title/date extraction
|
||||
|
||||
### HTML & CSS Standards
|
||||
- Classless CSS where possible
|
||||
- HTML should be highly compliant with best practices
|
||||
- Use semantic HTML5 elements
|
||||
- Modern CSS features: custom properties, nesting, `oklch()` colors, grid, clamp, logical properties
|
||||
- Responsive by default using fluid typography and flexible layouts
|
||||
|
||||
### Security
|
||||
- Path validation prevents directory traversal
|
||||
- Files must be within document root
|
||||
- MIME types properly set for all served content
|
||||
- No direct execution of arbitrary user input
|
||||
|
||||
### Code Style
|
||||
|
||||
#### PHP
|
||||
- Modern syntax: arrow functions, null coalescing, match expressions
|
||||
- Use type hints when practical
|
||||
- Ternary operators for simple conditionals
|
||||
- Keep functions focused and single-purpose
|
||||
|
||||
#### CSS
|
||||
- Use CSS variables for theming
|
||||
- Nesting for component-scoped styles
|
||||
- Grid for layout, not tables or excessive flexbox
|
||||
- `clamp()` for responsive sizing
|
||||
- Avoid pixel values where relative units work better
|
||||
|
||||
#### Templates
|
||||
- Escape output: `htmlspecialchars()` for user-generated content
|
||||
- Short echo tags: `<?= $var ?>`
|
||||
- Minimize logic in templates—prepare data beforehand
|
||||
|
||||
### Performance Considerations
|
||||
- Page load time displayed in footer (transparency and pride in performance)
|
||||
- CSS versioned with MD5 hash for cache busting
|
||||
- Minimal HTTP requests through direct includes
|
||||
- No JavaScript means no parsing delay
|
||||
|
||||
### Extensibility Patterns
|
||||
- Third-party code goes in `/app/vendor/`
|
||||
- Custom code goes in `/app/custom/`
|
||||
- Never modify defaults—always override
|
||||
- Use fallback chains: custom → default → docs variant
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Adding Custom Styles
|
||||
Place CSS in `/app/custom/styles/base.css`—it automatically overrides default styles.
|
||||
|
||||
### Creating Custom Templates
|
||||
Place templates in `/app/custom/templates/`—they automatically override defaults.
|
||||
|
||||
### Adding Fonts
|
||||
Place font files in `/app/custom/fonts/` and reference them in your custom CSS.
|
||||
|
||||
### Creating Content
|
||||
1. Create a directory for your content
|
||||
2. Add a content file: `page.md`, `article.md`, etc.
|
||||
3. Optionally add `metadata.ini` for title, date, summary
|
||||
4. Optionally add `cover.jpg` for list view thumbnails
|
||||
5. Content is immediately accessible at the URL matching the directory path
|
||||
|
||||
### List Views
|
||||
When a directory contains subdirectories:
|
||||
- Default list template generates automatic listings
|
||||
- Each item shows title, date, optional cover image, optional summary
|
||||
- Override with custom list template for specialized presentation
|
||||
|
||||
## What This Framework Is Not
|
||||
|
||||
- Not a CMS with an admin panel
|
||||
- Not a single-page application
|
||||
- Not a JavaScript framework
|
||||
- Not opinionated about design aesthetics
|
||||
- Not trying to scale to millions of users
|
||||
|
||||
## What This Framework Is
|
||||
|
||||
- A simple, intuitive way to publish content
|
||||
- A foundation that will work for decades
|
||||
- A teaching tool for understanding web fundamentals
|
||||
- A protest against complexity for complexity's sake
|
||||
24
README.md
Normal file
24
README.md
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# PNP (Plug'n Play) - A 'just enough' PHP framework for simple websites
|
||||
|
||||
HTML lacks native support for including repeating elements, like the site header and footer. By using PHP sparsely, we can add this feature and some other modern conviniences, while providing a nearly maintanance-free framework that will chugh along for years, possibly decades, by avoiding a bunch of rapidly changing components and dependencies. Since reasonably up-to-date PHP versions is available at the vast majority of shared hosting providers, it makes sence to combine HTML with PHP.
|
||||
|
||||
The mission statement is to apply just enough PHP to make the core features work. No admin panel, just an intuitive folder-based workflow. Intuitive defaults are applied, with optional overrides and customizations.
|
||||
|
||||
## Main features from the users perspective
|
||||
|
||||
- The user should be able to create a new folder and drop in a markdown, PHP or HTML file and have it rendered imediatly. The directory hierarchy dictate the URL, for example `/www/articles/2025/10-23-title-of-article/article.md` would translate to the following URL: `domain.tld/articles/2025/-10-23-title-of-article/`.
|
||||
- The user can drop images and files in a directory and use them as assets. If a user drops a jpeg or webp into the folder mentioned above, the image can be refferenced in the markdown, like so: `[!alt text for the image](filname.jpg). The image will be renderend inline in the text. The same goes for PDF's or other document files, only they will not be rendered, but can be linked to.
|
||||
- If a directory contains one or more subfolders, it is assumed that it is a parent folder containing multiple instances of the same type of content, for example a list of articles, blog posts and so on. A default list view will be applied showing title and date, but the view can be overriden and defined for each content type / folder.
|
||||
|
||||
## Main features from a developers perspective
|
||||
|
||||
- The code is readable and adds nothing more than strictly neccessary
|
||||
- Only HTML, PHP and CSS is allowed – no JS!
|
||||
- Sparsely commented, only the main sections
|
||||
- Custom templates is located at /app/custom
|
||||
- Third party scripts are added to /app/vendors
|
||||
- Supports PHP 8.3+ and makes use of modern PHP features whenever possible
|
||||
- Uses a PHP to make a simple router
|
||||
- Uses .htaccess to make the PHP router handle all requests.
|
||||
- The HTML is classles and highly compliant with best practices
|
||||
- The CSS uses modern features to apply styles as effective and smart as possible
|
||||
8
app/config.ini
Normal file
8
app/config.ini
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
; PnP Framework Configuration
|
||||
|
||||
[languages]
|
||||
; Default language (used when no language prefix in URL)
|
||||
default = "no"
|
||||
|
||||
; Available languages (comma-separated language codes)
|
||||
available = "no,en"
|
||||
3
app/default/config.ini
Normal file
3
app/default/config.ini
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[languages]
|
||||
default = "en"
|
||||
available = "en"
|
||||
214
app/default/docs/styles/base.css
Normal file
214
app/default/docs/styles/base.css
Normal file
|
|
@ -0,0 +1,214 @@
|
|||
/* MINIMAL RESET */
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
|
||||
/* VARIABLES */
|
||||
:root {
|
||||
--font-body: Georgia, "Times New Roman", serif;
|
||||
--font-heading: system-ui, -apple-system, sans-serif;
|
||||
--color-primary: #2563eb;
|
||||
--color-primary-dark: #1e40af;
|
||||
--color-bg: #f8fafc;
|
||||
--color-bg-alt: #ffffff;
|
||||
--color-text: #1e293b;
|
||||
--color-text-light: #64748b;
|
||||
--color-border: #e2e8f0;
|
||||
}
|
||||
|
||||
/* GLOBAL */
|
||||
html {
|
||||
font-family: var(--font-body);
|
||||
font-size: 18px;
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
color: var(--color-text);
|
||||
background-color: var(--color-bg);
|
||||
}
|
||||
|
||||
img { max-width: 100%; height: auto; }
|
||||
|
||||
a {
|
||||
color: var(--color-primary);
|
||||
text-decoration: none;
|
||||
}
|
||||
a:hover {
|
||||
color: var(--color-primary-dark);
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
/* LAYOUT */
|
||||
.docs-container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
display: grid;
|
||||
grid-template-columns: 250px 1fr;
|
||||
gap: 2rem;
|
||||
padding: 2rem 1rem;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.docs-container {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
/* HEADER */
|
||||
header {
|
||||
grid-column: 1 / -1;
|
||||
border-bottom: 2px solid var(--color-border);
|
||||
padding-bottom: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
header h1 {
|
||||
font-family: var(--font-heading);
|
||||
font-size: 1.8rem;
|
||||
color: var(--color-primary);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* SIDEBAR */
|
||||
.sidebar {
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.sidebar h2 {
|
||||
font-family: var(--font-heading);
|
||||
font-size: 0.85rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
color: var(--color-text-light);
|
||||
margin-bottom: 0.5rem;
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
|
||||
.sidebar h2:first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.sidebar ul {
|
||||
list-style: none;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.sidebar li {
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.sidebar a {
|
||||
display: block;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 0.25rem;
|
||||
}
|
||||
|
||||
.sidebar a:hover {
|
||||
background-color: var(--color-bg);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
/* MAIN CONTENT */
|
||||
main {
|
||||
background-color: var(--color-bg-alt);
|
||||
padding: 2rem;
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
article h1 {
|
||||
font-family: var(--font-heading);
|
||||
font-size: 2.2rem;
|
||||
margin-bottom: 1rem;
|
||||
color: var(--color-text);
|
||||
font-weight: 600;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
article h2 {
|
||||
font-family: var(--font-heading);
|
||||
font-size: 1.6rem;
|
||||
margin-top: 2rem;
|
||||
margin-bottom: 0.75rem;
|
||||
color: var(--color-text);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
article h3 {
|
||||
font-family: var(--font-heading);
|
||||
font-size: 1.2rem;
|
||||
margin-top: 1.5rem;
|
||||
margin-bottom: 0.5rem;
|
||||
color: var(--color-text);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
article p {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
article ul, article ol {
|
||||
margin-bottom: 1rem;
|
||||
padding-left: 2rem;
|
||||
}
|
||||
|
||||
article li {
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
article code {
|
||||
background-color: var(--color-bg);
|
||||
padding: 0.2rem 0.4rem;
|
||||
border-radius: 0.25rem;
|
||||
font-size: 0.9em;
|
||||
font-family: 'Courier New', monospace;
|
||||
}
|
||||
|
||||
article pre {
|
||||
background-color: var(--color-bg);
|
||||
padding: 1rem;
|
||||
border-radius: 0.5rem;
|
||||
overflow-x: auto;
|
||||
margin-bottom: 1rem;
|
||||
border: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
article pre code {
|
||||
background: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
article blockquote {
|
||||
border-left: 4px solid var(--color-primary);
|
||||
padding-left: 1rem;
|
||||
margin: 1rem 0;
|
||||
color: var(--color-text-light);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* LIST VIEW */
|
||||
.doc-list article h1 {
|
||||
font-size: 1.4rem;
|
||||
margin-top: 1.5rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.doc-list article h1:first-of-type {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.doc-list p {
|
||||
color: var(--color-text-light);
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
/* FOOTER */
|
||||
footer {
|
||||
grid-column: 1 / -1;
|
||||
border-top: 2px solid var(--color-border);
|
||||
padding-top: 1rem;
|
||||
margin-top: 2rem;
|
||||
text-align: center;
|
||||
font-size: 0.85rem;
|
||||
color: var(--color-text-light);
|
||||
}
|
||||
46
app/default/docs/templates/base.php
vendored
Normal file
46
app/default/docs/templates/base.php
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="<?= $currentLang ?? 'en' ?>">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="/app/docs-styles/base.css">
|
||||
<title><?= $pageTitle ?? 'Documentation' ?></title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="docs-container">
|
||||
<header>
|
||||
<h1><a href="/docs">PnP Documentation</a></h1>
|
||||
</header>
|
||||
|
||||
<aside class="sidebar">
|
||||
<h2>Tutorials</h2>
|
||||
<ul>
|
||||
<li><a href="/docs/tutorials/getting-started">Getting Started</a></li>
|
||||
</ul>
|
||||
|
||||
<h2>How-To Guides</h2>
|
||||
<ul>
|
||||
<li><a href="/docs/how-to/customize-templates">Customize Templates</a></li>
|
||||
</ul>
|
||||
|
||||
<h2>Explanation</h2>
|
||||
<ul>
|
||||
<li><a href="/docs/explanation/routing">Routing System</a></li>
|
||||
</ul>
|
||||
|
||||
<h2>Reference</h2>
|
||||
<ul>
|
||||
<li><a href="/docs/reference/metadata">Metadata</a></li>
|
||||
</ul>
|
||||
</aside>
|
||||
|
||||
<main>
|
||||
<?= $content ?>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>PnP Framework Documentation</p>
|
||||
</footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
14
app/default/docs/templates/list.php
vendored
Normal file
14
app/default/docs/templates/list.php
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<div class="doc-list">
|
||||
<?php foreach ($items as $item): ?>
|
||||
<article>
|
||||
<h1>
|
||||
<a href="<?= htmlspecialchars($item['url']) ?>">
|
||||
<?= htmlspecialchars($item['title']) ?>
|
||||
</a>
|
||||
</h1>
|
||||
<?php if ($item['summary']): ?>
|
||||
<p><?= htmlspecialchars($item['summary']) ?></p>
|
||||
<?php endif; ?>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
47
app/default/styles/base.css
Normal file
47
app/default/styles/base.css
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/* MINIMAL RESET */
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
|
||||
/* GLOBAL */
|
||||
body {
|
||||
font-family: system-ui, sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
img { max-width: 100%; height: auto; }
|
||||
|
||||
a { color: #0066cc; text-decoration: none; }
|
||||
a:hover { text-decoration: underline; }
|
||||
|
||||
/* HEADER */
|
||||
header {
|
||||
border-bottom: 2px solid #eee;
|
||||
padding-bottom: 1rem;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
header h1 { font-size: 1.5rem; }
|
||||
|
||||
/* MAIN */
|
||||
main { margin-bottom: 2rem; }
|
||||
|
||||
article { margin-bottom: 2rem; }
|
||||
|
||||
h1 {
|
||||
font-size: 1.8rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
p { margin-bottom: 1rem; }
|
||||
|
||||
/* FOOTER */
|
||||
footer {
|
||||
border-top: 2px solid #eee;
|
||||
padding-top: 1rem;
|
||||
text-align: center;
|
||||
font-size: 0.9rem;
|
||||
color: #666;
|
||||
}
|
||||
29
app/default/templates/base.php
Normal file
29
app/default/templates/base.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="<?= $currentLang ?? 'en' ?>">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="<?= file_exists(dirname(dirname(__DIR__)) . '/custom/styles/base.css') ? '/app/styles/base.css' : '/app/default-styles/base.css' ?>">
|
||||
<title><?= htmlspecialchars($pageTitle ?? 'Site') ?></title>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1><a href="/">Webfolder demo</a></h1>
|
||||
<?php if (!empty($navigation)): ?>
|
||||
<nav>
|
||||
<ul>
|
||||
<?php foreach ($navigation as $item): ?>
|
||||
<li><a href="<?= htmlspecialchars($item['url']) ?>"><?= htmlspecialchars($item['title']) ?></a></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</nav>
|
||||
<?php endif; ?>
|
||||
</header>
|
||||
<main>
|
||||
<?= $content ?>
|
||||
</main>
|
||||
<footer>
|
||||
<p>© <?= date('Y') ?></p>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
18
app/default/templates/list.php
Normal file
18
app/default/templates/list.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<article>
|
||||
<?php foreach ($items as $item): ?>
|
||||
<article>
|
||||
<?php if ($item['cover']): ?>
|
||||
<img src="<?= htmlspecialchars($item['cover']) ?>" alt="<?= htmlspecialchars($item['title']) ?>">
|
||||
<?php endif; ?>
|
||||
<h1>
|
||||
<a href="<?= htmlspecialchars($item['url']) ?>">
|
||||
<?= htmlspecialchars($item['title']) ?>
|
||||
</a>
|
||||
</h1>
|
||||
<p><?= htmlspecialchars($item['date']) ?></p>
|
||||
<?php if ($item['summary']): ?>
|
||||
<p><?= htmlspecialchars($item['summary']) ?></p>
|
||||
<?php endif; ?>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</article>
|
||||
414
app/router.php
Normal file
414
app/router.php
Normal file
|
|
@ -0,0 +1,414 @@
|
|||
<?php
|
||||
// Load configuration
|
||||
$configFile = file_exists(__DIR__ . '/../custom/config.ini')
|
||||
? __DIR__ . '/../custom/config.ini'
|
||||
: __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);
|
||||
}
|
||||
|
||||
// Determine if this is a docs request
|
||||
$isDocsRequest = str_starts_with($requestPath, 'docs');
|
||||
|
||||
// Use docs templates for /docs, otherwise use custom/default templates
|
||||
if ($isDocsRequest) {
|
||||
$baseTemplate = __DIR__ . '/default/docs/templates/base.php';
|
||||
$listTemplate = __DIR__ . '/default/docs/templates/list.php';
|
||||
} else {
|
||||
$customBaseTemplate = dirname(__DIR__) . '/custom/templates/base.php';
|
||||
$defaultBaseTemplate = __DIR__ . '/default/templates/base.php';
|
||||
$baseTemplate = file_exists($customBaseTemplate) ? $customBaseTemplate : $defaultBaseTemplate;
|
||||
|
||||
$customListTemplate = dirname(__DIR__) . '/custom/templates/list.php';
|
||||
$defaultListTemplate = __DIR__ . '/default/templates/list.php';
|
||||
$listTemplate = file_exists($customListTemplate) ? $customListTemplate : $defaultListTemplate;
|
||||
}
|
||||
|
||||
// Build file patterns with language variants
|
||||
function buildFilePatterns(string $lang, string $defaultLang): array {
|
||||
$extensions = ['php', 'html', 'md'];
|
||||
$patterns = ['page' => [], 'single' => []];
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
// Default files
|
||||
$patterns['page'][] = "page.$ext";
|
||||
$patterns['single'][] = "single.$ext";
|
||||
$patterns['single'][] = "post.$ext";
|
||||
$patterns['single'][] = "article.$ext";
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
$parts = explode('/', trim($requestPath, '/'));
|
||||
$resolvedParts = [];
|
||||
$currentPath = $contentDir;
|
||||
|
||||
foreach ($parts as $segment) {
|
||||
if (empty($segment)) continue;
|
||||
|
||||
// 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")
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no slug match, use segment as-is
|
||||
if (!$found) {
|
||||
$resolvedParts[] = $segment;
|
||||
$currentPath .= "/$segment";
|
||||
}
|
||||
}
|
||||
|
||||
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, '/');
|
||||
|
||||
if (is_file($contentPath)) {
|
||||
return ['type' => 'file', 'path' => realpath($contentPath)];
|
||||
}
|
||||
|
||||
if (is_dir($contentPath)) {
|
||||
if ($file = findMatchingFile($contentPath, $patterns['single']) ?: findMatchingFile($contentPath, $patterns['page'])) {
|
||||
return ['type' => 'file', 'path' => $file, 'needsSlash' => !$hasTrailingSlash];
|
||||
}
|
||||
return ['type' => 'directory', 'path' => realpath($contentPath)];
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
$metadata = parse_ini_file($metadataFile, true);
|
||||
if (!$metadata) return null;
|
||||
|
||||
// Extract base metadata (non-section values)
|
||||
$baseMetadata = array_filter($metadata, fn($key) => !is_array($metadata[$key]), ARRAY_FILTER_USE_KEY);
|
||||
|
||||
// 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]);
|
||||
}
|
||||
|
||||
return $baseMetadata ?: null;
|
||||
}
|
||||
|
||||
function extractTitle(string $filePath, array $patterns): ?string {
|
||||
$file = findMatchingFile($filePath, $patterns['single']) ?: findMatchingFile($filePath, $patterns['page']);
|
||||
if (!$file) return null;
|
||||
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
$content = file_get_contents($file);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function buildNavigation(string $contentDir, string $currentLang, string $defaultLang, array $pageFilePatterns): array {
|
||||
$navItems = [];
|
||||
|
||||
// Scan top-level directories in content
|
||||
$items = array_filter(
|
||||
scandir($contentDir) ?: [],
|
||||
fn($item) => !in_array($item, ['.', '..']) && is_dir("$contentDir/$item")
|
||||
);
|
||||
|
||||
foreach ($items as $item) {
|
||||
$itemPath = "$contentDir/$item";
|
||||
$metadata = loadMetadata($itemPath, $currentLang, $defaultLang);
|
||||
|
||||
// Check if this item should be in menu
|
||||
if (!$metadata || empty($metadata['menu'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if content exists for current language
|
||||
if ($currentLang !== $defaultLang) {
|
||||
$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) continue;
|
||||
}
|
||||
|
||||
// Extract title and build URL
|
||||
$title = $metadata['title'] ?? extractTitle($itemPath, $pageFilePatterns) ?? $item;
|
||||
$langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : '';
|
||||
|
||||
// Use translated slug if available
|
||||
$urlSlug = ($currentLang !== $defaultLang && $metadata && isset($metadata['slug']))
|
||||
? $metadata['slug']
|
||||
: $item;
|
||||
|
||||
$navItems[] = [
|
||||
'title' => $title,
|
||||
'url' => $langPrefix . '/' . urlencode($urlSlug) . '/',
|
||||
'order' => (int)($metadata['menu_order'] ?? 999)
|
||||
];
|
||||
}
|
||||
|
||||
// Sort by menu_order
|
||||
usort($navItems, fn($a, $b) => $a['order'] <=> $b['order']);
|
||||
|
||||
return $navItems;
|
||||
}
|
||||
|
||||
function renderTemplate(string $content, int $statusCode = 200): void {
|
||||
global $baseTemplate, $contentDir, $currentLang, $defaultLang, $pageFilePatterns;
|
||||
|
||||
// Build navigation for templates
|
||||
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang, $pageFilePatterns);
|
||||
|
||||
// Load frontpage metadata for home button label
|
||||
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
|
||||
$homeLabel = $frontpageMetadata['slug'] ?? 'Home';
|
||||
|
||||
http_response_code($statusCode);
|
||||
include $baseTemplate;
|
||||
exit;
|
||||
}
|
||||
|
||||
function renderFile(string $filePath): void {
|
||||
global $baseTemplate, $contentDir, $currentLang, $defaultLang, $pageFilePatterns;
|
||||
|
||||
$realPath = realpath($filePath);
|
||||
if (!$realPath || !str_starts_with($realPath, $contentDir) || !is_readable($realPath)) {
|
||||
renderTemplate("<h1>403 Forbidden</h1><p>Access denied.</p>", 403);
|
||||
}
|
||||
|
||||
$ext = pathinfo($realPath, PATHINFO_EXTENSION);
|
||||
|
||||
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();
|
||||
|
||||
// Build navigation for templates
|
||||
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang, $pageFilePatterns);
|
||||
|
||||
// Load metadata for current page/directory
|
||||
$pageDir = dirname($realPath);
|
||||
$pageMetadata = loadMetadata($pageDir, $currentLang, $defaultLang);
|
||||
$pageTitle = $pageMetadata['title'] ?? null;
|
||||
|
||||
// Load frontpage metadata for home button label
|
||||
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
|
||||
$homeLabel = $frontpageMetadata['slug'] ?? 'Home';
|
||||
|
||||
include $baseTemplate;
|
||||
exit;
|
||||
}
|
||||
|
||||
// Serve other file types directly
|
||||
header('Content-Type: ' . (mime_content_type($realPath) ?: 'application/octet-stream'));
|
||||
readfile($realPath);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 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";
|
||||
}
|
||||
|
||||
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']);
|
||||
|
||||
case 'directory':
|
||||
$dir = $parsedPath['path'];
|
||||
if (file_exists("$dir/index.php")) {
|
||||
renderFile("$dir/index.php");
|
||||
}
|
||||
|
||||
// Default directory listing
|
||||
$subdirs = array_filter(
|
||||
scandir($dir) ?: [],
|
||||
fn($item) => !in_array($item, ['.', '..']) && is_dir("$dir/$item")
|
||||
);
|
||||
|
||||
$items = array_filter(array_map(function($item) use ($dir, $requestPath, $currentLang, $defaultLang, $pageFilePatterns) {
|
||||
$itemPath = "$dir/$item";
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Build patterns for rendering (includes fallbacks)
|
||||
$patterns = buildFilePatterns($currentLang, $defaultLang);
|
||||
|
||||
$metadata = loadMetadata($itemPath, $currentLang, $defaultLang);
|
||||
$coverImage = findCoverImage($itemPath);
|
||||
|
||||
$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));
|
||||
}
|
||||
|
||||
$langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : '';
|
||||
|
||||
// Use translated slug if available, otherwise use folder name
|
||||
$urlSlug = ($currentLang !== $defaultLang && $metadata && isset($metadata['slug']))
|
||||
? $metadata['slug']
|
||||
: $item;
|
||||
|
||||
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));
|
||||
|
||||
ob_start();
|
||||
include $listTemplate;
|
||||
$content = ob_get_clean();
|
||||
|
||||
// Build navigation for base template
|
||||
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang, $pageFilePatterns);
|
||||
|
||||
include $baseTemplate;
|
||||
exit;
|
||||
|
||||
case 'not_found':
|
||||
renderTemplate("<h1>404 Not Found</h1><p>The requested resource was not found.</p>", 404);
|
||||
}
|
||||
44
app/static.php
Normal file
44
app/static.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
// Serve static files from /app directory
|
||||
$file = $_GET['file'] ?? '';
|
||||
$file = str_replace(['../', '..\\'], '', $file); // Prevent directory traversal
|
||||
|
||||
// Map request paths to actual file paths
|
||||
$basePath = __DIR__ . '/';
|
||||
$customBasePath = dirname(__DIR__) . '/';
|
||||
|
||||
if (str_starts_with($file, 'styles/')) {
|
||||
$filePath = $customBasePath . 'custom/' . $file;
|
||||
} elseif (str_starts_with($file, 'fonts/')) {
|
||||
$filePath = $customBasePath . 'custom/' . $file;
|
||||
} elseif (str_starts_with($file, 'default-styles/')) {
|
||||
$filePath = $basePath . 'default/' . substr($file, 15); // Remove 'default-styles/' prefix
|
||||
} elseif (str_starts_with($file, 'docs-styles/')) {
|
||||
$filePath = $basePath . 'default/docs/styles/' . substr($file, 12); // Remove 'docs-styles/' prefix
|
||||
} else {
|
||||
http_response_code(404);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Check if file exists and is readable
|
||||
if (!file_exists($filePath) || !is_readable($filePath)) {
|
||||
http_response_code(404);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Determine MIME type based on extension
|
||||
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
||||
$mimeTypes = [
|
||||
'css' => 'text/css',
|
||||
'js' => 'application/javascript',
|
||||
'woff' => 'font/woff',
|
||||
'woff2' => 'font/woff2',
|
||||
'ttf' => 'font/ttf',
|
||||
'otf' => 'font/otf',
|
||||
'eot' => 'application/vnd.ms-fontobject',
|
||||
'svg' => 'image/svg+xml',
|
||||
];
|
||||
|
||||
$mimeType = $mimeTypes[$ext] ?? (mime_content_type($filePath) ?: 'application/octet-stream');
|
||||
header('Content-Type: ' . $mimeType);
|
||||
readfile($filePath);
|
||||
1712
app/vendor/Parsedown.php
vendored
Normal file
1712
app/vendor/Parsedown.php
vendored
Normal file
File diff suppressed because it is too large
Load diff
27
development/apache/custom.conf
Normal file
27
development/apache/custom.conf
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<Directory /var/www/>
|
||||
Options Indexes FollowSymLinks
|
||||
AllowOverride All
|
||||
Require all granted
|
||||
</Directory>
|
||||
|
||||
# Alias for app assets (styles, fonts, etc.)
|
||||
Alias /app/styles /var/www/custom/styles
|
||||
Alias /app/fonts /var/www/custom/fonts
|
||||
Alias /app/default-styles /var/www/app/default/styles
|
||||
|
||||
<Directory /var/www/custom/styles>
|
||||
Require all granted
|
||||
</Directory>
|
||||
|
||||
<Directory /var/www/custom/fonts>
|
||||
Require all granted
|
||||
</Directory>
|
||||
|
||||
<Directory /var/www/app/default/styles>
|
||||
Require all granted
|
||||
</Directory>
|
||||
|
||||
# Enable mod_rewrite
|
||||
<IfModule !mod_rewrite.c>
|
||||
LoadModule rewrite_module modules/mod_rewrite.so
|
||||
</IfModule>
|
||||
28
development/compose.yaml
Normal file
28
development/compose.yaml
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
version: '3.8'
|
||||
|
||||
services:
|
||||
custom:
|
||||
image: php:8.3.12-apache
|
||||
container_name: folderweb-custom
|
||||
working_dir: /var/www/html/
|
||||
volumes:
|
||||
- ../app:/var/www/app:z
|
||||
- ../content:/var/www/html:z
|
||||
- ../custom:/var/www/custom:z
|
||||
- ../docs:/var/www/html/docs:z
|
||||
- ./apache/custom.conf:/etc/apache2/conf-available/custom.conf:z
|
||||
ports:
|
||||
- "4040:80"
|
||||
command: bash -c "a2enconf custom && a2enmod rewrite && apache2-foreground"
|
||||
default:
|
||||
image: php:8.3.12-apache
|
||||
container_name: folderweb-default
|
||||
working_dir: /var/www/html/
|
||||
volumes:
|
||||
- ../app:/var/www/app:z
|
||||
- ../content:/var/www/html:z
|
||||
- ../docs:/var/www/html/docs:z
|
||||
- ./apache/custom.conf:/etc/apache2/conf-available/custom.conf:z
|
||||
ports:
|
||||
- "8080:80"
|
||||
command: bash -c "a2enconf custom && a2enmod rewrite && apache2-foreground"
|
||||
Loading…
Add table
Reference in a new issue