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.
57 lines
1.6 KiB
57 lines
1.6 KiB
<?php
|
|
|
|
$debug = 5;
|
|
require '_debug.php';
|
|
|
|
|
|
# Pattern begins '/' foloowed 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'
|
|
|
|
if (preg_match('#/([^/.]+)#',$_SERVER['PATH_INFO'], $m) === false) {
|
|
http_response_code('400');
|
|
echo "Request {$_SERVER['PATH_INFO']} malformed";
|
|
exit;
|
|
}
|
|
$page = $m[1];
|
|
|
|
# Identify type of file as PHP or Markdown
|
|
if (file_exists($page . '.php')) {
|
|
$file = $page . '.php';
|
|
$type = 'php';
|
|
} else if (file_exists($page . '.md')) {
|
|
$file = $page . '.md';
|
|
$type = 'markdown';
|
|
} else {
|
|
# Unknown or invalid path
|
|
http_response_code('404');
|
|
echo "File '$page' not found";
|
|
exit;
|
|
}
|
|
|
|
debug("PAGE: $page\n"
|
|
."TYPE: $type\n"
|
|
."PATH: {$_SERVER['PATH_INFO']}\n");
|
|
|
|
# Load file, we do include rather than file_get_contents so PHP can be
|
|
# executed if needed
|
|
ob_start();
|
|
include __DIR__ . '/' . $file;
|
|
$contents = ob_get_clean();
|
|
|
|
# Split off head matter
|
|
#
|
|
# this pattern uses the 'ms' modifier to treat multiline strings as a
|
|
# single string, and let '\s', '.' etc treat newlines as whitespace
|
|
# I match an _optional_ HTML ( '<!-- nnnn -->') comment if we want to wrap
|
|
# the headmatter in a comment.
|
|
#
|
|
# The headmatter is delimited by either '----' or '++++' as per
|
|
# Markdown/yaml standards.
|
|
# Technical note that '+' is a regex character so \+{3,} instead of ++++
|
|
if (preg_match('/^(<!--\s*)?(-{3,}|\+{3,})(.*)(-{3,}|\+{3,})(-->)?(.*)/ms',$contents, $parts) === true) {
|
|
$var_dump($parts);
|
|
}
|
|
|
|
|