Blogging structure with SEO markup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
blog/index.php

62 lines
1.7 KiB

<?php
#$debug = 5;
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/lib/debug.php';
require __DIR__ . '/lib/render_markdown.php';
require __DIR__ . '/lib/render_php.php';
# Pattern begins '/' followed by 1 or more characters that are not '/' or '.'
# In particular this blocks attempts to hack into system
#
# Example path '/article/market' will result in 'market' which we can try to
# match with 'market.php' or 'market.md'
# Nullish ?? is used to set default page for article summaries
if (!preg_match('#/([^/.]+)#',$_SERVER['PATH_INFO']??'/index', $m)) {
http_response_code('400');
echo "Request Path info malformed";
exit;
}
$page = $m[1];
if ($page !== 'index') {
# Identify type of file as PHP or Markdown
if (file_exists($page . '.php')) {
$file = $page . '.php';
$type = 'php';
$article = render_php($file);
} else if (file_exists($page . '.md')) {
$file = $page . '.md';
$type = 'markdown';
$article = render_markdown($file);
} else {
# Unknown or invalid path
http_response_code('404');
echo "File '$page' not found";
exit;
}
} else {
$article = [
'meta' => [ 'title' => 'articles' ],
'html' => <<<HTML
<h1>Placeholder for article listings</h1>
HTML,
];
}
$template = file_get_contents('template.inc');
$result = preg_replace_callback('/\[(\w+)\]/', function ($matches) use ($article) {
$key = $matches[1];
return isset($article['meta'][$key]) ? $article['meta'][$key] : $matches[0]; // fallback to original if key not found
}, $template);
$rendered= preg_replace('/\[contents?\]/', $article['html'], $result);
debug("PAGE: $page\n"
."TYPE: $type\n"
."FILE: $file\n"
."PATH: {$_SERVER['PATH_INFO']}\n");
echo $rendered;