yarmo.eu/index.php
Yarmo Mackenbach f5acb6dbaa Fix typo
2020-06-21 00:46:48 +02:00

386 lines
12 KiB
PHP

<?php
include_once __DIR__ . '/vendor/autoload.php';
include 'functions.php';
use Pagerange\Markdown\MetaParsedown;
// Init router
$router = new AltoRouter();
// Router mapping
$router->map('GET', '/', function() {}, 'home');
$router->map('GET', '/rss', function() {}, 'rss');
$router->map('GET', '/rss.xml', function() {}, 'rss-xml');
$router->map('GET', '/rss/all', function() {}, 'rss-all');
$router->map('GET', '/rss/blog', function() {}, 'rss-blog');
$router->map('GET', '/rss/notes', function() {}, 'rss-notes');
$router->map('GET', '/about', function() {}, 'about');
$router->map('GET', '/feeds', function() {}, 'feeds');
$router->map('GET', '/uses', function() {}, 'uses');
$router->map('GET', '/now', function() {}, 'now');
$router->map('GET', '/blog', function() {}, 'blog');
$router->map('GET', '/blog/[*:slug]', function() {}, 'blog_post');
$router->map('GET', '/notes', function() {}, 'notes');
$router->map('GET', '/notes/[*:slug]', function() {}, 'notes_post');
$router->map('GET', '/post/[*:slug]', function() {}, 'post');
$router->map('GET', '/projects', function() {}, 'projects');
$router->map('GET', '/projects/[*:slug]', function() {}, 'projects_details');
$router->map('GET', '/foss', function() {}, 'foss');
$router->map('GET', '/music', function() {}, 'music');
$router->map('GET', '/vinyl', function() {}, 'vinyl');
$router->map('GET', '/aotw', function() {}, 'aotw');
$router->map('GET', '/pgp', function() {}, 'pgp');
$router->map('GET', '/contact', function() {}, 'contact');
// Router matching
$match = $router->match();
// Template engine settings and variables
$basetitle = 'yarmo';
$environment = getenv('ENVIRONMENT') ?: 'production';
$options = [
'paths' => [
'views/',
],
'cache_dir' => 'cache/',
'enable_profiler' => false,
'profiler' => [
'time_precision' => 3,
'line_height' => 30,
'display' => true,
'log' => false,
],
];
$variables = [
'title' => $basetitle
];
// If we are dealing with the home page
if ($match['name'] == 'home') {
$variables['posts'] = getBlogPosts($variables['params']);
$variables['title'] = 'Blog — '.$basetitle;
// $variables['icons'] = getIcons();
}
// If we are dealing with the about page
if ($match['name'] == 'about') {
}
// If we are dealing with the rss feed
if ($match['name'] == 'rss' || $match['name'] == 'rss-all' || $match['name'] == 'rss-xml') {
$blogposts = getBlogPosts($variables['params']);
$notes = getNotes($variables['params']);
$variables['posts'] = array_merge($blogposts, $notes);
$variables['feed']['title'] = "Yarmo's blog and notes";
$variables['feed']['description'] = "Blog and notes feed for yarmo.eu discussing homelab and selfhosted stuff";
$variables['feed']['url'] = "https://yarmo.eu";
$variables['feed']['linkself'] = "https://yarmo.eu/rss/all";
}
// If we are dealing with the blog rss feed
if ($match['name'] == 'rss-blog') {
$variables['posts'] = getBlogPosts($variables['params']);
$variables['feed']['title'] = "Yarmo's blog";
$variables['feed']['description'] = "Blog feed for yarmo.eu discussing homelab and selfhosted stuff";
$variables['feed']['url'] = "https://yarmo.eu/blog";
$variables['feed']['linkself'] = "https://yarmo.eu/rss/blog";
}
// If we are dealing with the notes rss feed
if ($match['name'] == 'rss-notes') {
$variables['posts'] = getNotes($variables['params']);
$variables['feed']['title'] = "Yarmo's blog";
$variables['feed']['description'] = "Notes feed for yarmo.eu discussing homelab and selfhosted stuff";
$variables['feed']['url'] = "https://yarmo.eu/notes";
$variables['feed']['linkself'] = "https://yarmo.eu/rss/notes";
}
// If we are dealing with the feeds
if ($match['name'] == 'feeds') {
$variables['title'] = 'Feeds — '.$basetitle;
}
// If we are dealing with the uses
if ($match['name'] == 'uses') {
$variables['title'] = 'Uses — '.$basetitle;
}
// If we are dealing with the now
if ($match['name'] == 'now') {
$variables['title'] = 'Now — '.$basetitle;
}
// If we are dealing with the blog
if ($match['name'] == 'blog') {
$variables['posts'] = getBlogPosts($variables['params']);
$variables['title'] = 'Blog — '.$basetitle;
}
// If we are dealing with the notes
if ($match['name'] == 'notes') {
$variables['posts'] = getNotes($variables['params']);
$variables['title'] = 'Notes — '.$basetitle;
}
// If we are dealing with a post (old format)
if (($match['name'] == 'blog_post') || ($match['name'] == 'notes_post')) {
header('Location: https://yarmo.eu/post/'.$match['params']['slug']);
}
// If we are dealing with a post
if ($match['name'] == 'post') {
$variables['params']['slug'] = $match['params']['slug'];
$variables['post'] = getBlogPosts($variables['params']);
if (count($variables['post']) == 0) {
$variables['post'] = getNotes($variables['params']);
}
if (count($variables['post']) > 0) {
$variables['post'] = $variables['post'][0];
$variables['title'] = htmlspecialchars_decode(str_replace('&middot;','·',$variables['post']['title'])).' — '.$basetitle;
} else {
$match['name'] = '404';
}
}
// If we are dealing with the projects
if ($match['name'] == 'projects') {
$variables['projects'] = getProjects($variables['params']);
$variables['title'] = 'Projects — '.$basetitle;
}
// If we are dealing with a project's details
if ($match['name'] == 'projects_details') {
$variables['params']['slug'] = $match['params']['slug'];
$variables['project'] = getProjects($variables['params']);
$variables['project'] = $variables['project'][0];
$variables['title'] = htmlspecialchars_decode(str_replace('&middot;','·',$variables['project']['title'])).' — '.$basetitle;
}
// If we are dealing with foss
if ($match['name'] == 'foss') {
$variables['foss'] = getFOSS();
$variables['title'] = 'FOSS — '.$basetitle;
}
// If we are dealing with vinyl
if ($match['name'] == 'vinyl') {
$variables['vinyl'] = getVinyl();
$variables['albums'] = $variables['vinyl']['albums'];
$variables['title'] = 'Vinyl — '.$basetitle;
}
// If we are dealing with Album Of The Week
if ($match['name'] == 'aotw') {
$variables['albums'] = getAOTW();
$variables['title'] = 'Album Of The Week — '.$basetitle;
}
// If we are dealing with the pgp page
if ($match['name'] == 'pgp') {
$variables['title'] = 'PGP — '.$basetitle;
}
// If we are dealing with the contact page
if ($match['name'] == 'contact') {
$variables['title'] = 'Contact me — '.$basetitle;
}
// Add Phug dyninclude
Phug::addKeyword('dyninclude', function ($args) {
return array(
'beginPhp' => 'echo file_get_contents(' . $args . ');',
);
});
// PRODUCTION
// Render the appropriate route
if ($environment === 'production') {
if(is_array($match) && is_callable($match['target'])) {
switch ($match['name']) {
case 'home':
// Phug optimizer
\Phug\Optimizer::call('displayFile', ['index', $variables], $options);
// Phug::displayFile('index', $variables, $options);
break;
case 'about':
// Phug optimizer
\Phug\Optimizer::call('displayFile', ['about', $variables], $options);
// Phug::displayFile('index', $variables, $options);
break;
case 'rss':
case 'rss-xml':
case 'rss-all':
case 'rss-blog':
case 'rss-notes':
getRSS($variables);
break;
case 'feeds':
\Phug\Optimizer::call('displayFile', ['feeds', $variables], $options);
break;
case 'uses':
\Phug\Optimizer::call('displayFile', ['uses', $variables], $options);
break;
case 'now':
\Phug\Optimizer::call('displayFile', ['now', $variables], $options);
break;
case 'blog':
\Phug\Optimizer::call('displayFile', ['blog', $variables], $options);
break;
case 'blog_post':
\Phug\Optimizer::call('displayFile', ['blog_post', $variables], $options);
break;
case 'notes':
\Phug\Optimizer::call('displayFile', ['notes', $variables], $options);
break;
case 'notes_post':
\Phug\Optimizer::call('displayFile', ['notes_post', $variables], $options);
break;
case 'post':
\Phug\Optimizer::call('displayFile', ['post', $variables], $options);
break;
case 'projects':
\Phug\Optimizer::call('displayFile', ['projects', $variables], $options);
break;
case 'projects_details':
\Phug\Optimizer::call('displayFile', ['projects_details', $variables], $options);
break;
case 'foss':
\Phug\Optimizer::call('displayFile', ['foss', $variables], $options);
break;
case 'music':
\Phug\Optimizer::call('displayFile', ['music', $variables], $options);
break;
case 'vinyl':
\Phug\Optimizer::call('displayFile', ['vinyl', $variables], $options);
break;
case 'aotw':
\Phug\Optimizer::call('displayFile', ['aotw', $variables], $options);
break;
case 'contact':
\Phug\Optimizer::call('displayFile', ['contact', $variables], $options);
break;
case 'pgp':
\Phug\Optimizer::call('displayFile', ['pgp', $variables], $options);
break;
default:
\Phug\Optimizer::call('displayFile', ['404', $variables], $options);
break;
}
} else {
// No route was matched
\Phug\Optimizer::call('displayFile', ['404', $variables], $options);
}
exit;
}
# DEVELOPMENT
// Render the appropriate route
if(is_array($match) && is_callable($match['target'])) {
switch ($match['name']) {
case 'home':
Phug::displayFile('index', $variables, $options);
break;
case 'about':
Phug::displayFile('about', $variables, $options);
break;
case 'rss':
case 'rss-xml':
case 'rss-all':
case 'rss-blog':
case 'rss-notes':
getRSS($variables);
break;
case 'feeds':
Phug::displayFile('feeds', $variables, $options);
break;
case 'uses':
Phug::displayFile('uses', $variables, $options);
break;
case 'now':
Phug::displayFile('now', $variables, $options);
break;
case 'blog':
Phug::displayFile('blog', $variables, $options);
break;
case 'blog_post':
Phug::displayFile('blog_post', $variables, $options);
break;
case 'notes':
Phug::displayFile('notes', $variables, $options);
break;
case 'notes_post':
Phug::displayFile('notes_post', $variables, $options);
break;
case 'post':
Phug::displayFile('post', $variables, $options);
break;
case 'projects':
Phug::displayFile('projects', $variables, $options);
break;
case 'projects_details':
Phug::displayFile('projects_details', $variables, $options);
break;
case 'foss':
Phug::displayFile('foss', $variables, $options);
break;
case 'music':
Phug::displayFile('music', $variables, $options);
break;
case 'vinyl':
Phug::displayFile('vinyl', $variables, $options);
break;
case 'aotw':
Phug::displayFile('aotw', $variables, $options);
break;
case 'contact':
Phug::displayFile('contact', $variables, $options);
break;
case 'pgp':
Phug::displayFile('pgp', $variables, $options);
break;
default:
Phug::displayFile('404', $variables, $options);
break;
}
} else {
// No route was matched
Phug::displayFile('404', $variables, $options);
}