May 16 edits

main
Bill Ross 9 months ago
parent 31ace4a40f
commit a2b77b5a93
  1. 38
      _debug.php
  2. 57
      index.php

@ -0,0 +1,38 @@
<?php
defined('DEBUG') || define('DEBUG', $debug ?? $config['DEBUG'] ?? false);
if (ob_get_level() === 0) {
ob_start();
}
function debug($msg) {
error_log($msg);
if (DEBUG > 1) print($msg . "\n");
}
register_shutdown_function(function () {
$error = error_get_last();
if ($error !== null) {
// Log fatal error details
$type = $error['type'];
$message = $error['message'];
$file = $error['file'];
$line = $error['line'];
echo "[SHUTDOWN] Fatal error: {$message} in {$file} on line {$line}\n";
// Optional: log to file
// file_put_contents('/path/to/shutdown.log', "[SHUTDOWN] ...\n", FILE_APPEND);
} else {
if (DEBUG > 1) {
DEBUG && header('Content-Type: text/plain');
# define('X','Well');
DEBUG && print("Constants\n".print_r((get_defined_constants(true))['user']??'No Constants', true) . "\n");
DEBUG && print("Functions\n".print_r((get_defined_functions(true))['user']??'No Functions', true) . "\n");
DEBUG && print("Variables\n".print_r(array_diff_key(get_defined_vars(), array_flip(['_SERVER','_GET','_POST','_COOKIE', '_FILES'])), true) . "\n");
DEBUG && print("Includes\n".print_r(get_included_files(), true) . "\n");
}
echo "[SHUTDOWN] Script completed normally.\n";
}
});

@ -0,0 +1,57 @@
<?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);
}
Loading…
Cancel
Save