Pith - miter
<?php
declare(strict_types=1);
session_start();

if (empty($_SESSION['miter'])) {
header('Location: ../../index.php');
exit;
}

include '../admin/otc.php';
$g_date = date("Y-m-d H:i:s");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// get title
$tenon_title = $_POST["title"] ?? '';
// get post
$tenon_post = str_replace(array("\r\n", "\r", "\n"), "<br>", $_POST["tenon"] ?? '');

// replace file name space
$title_to_file = str_replace(' ', '_', $tenon_title);

// replace non-file-compliant characters
$invalid_chars = ['<', '>', ':', '"', '/', '\\', '|', '?', '*', '\'', '#'];
$title_to_file = str_replace($invalid_chars, '-', $title_to_file);

$title_to_file = basename($title_to_file);

// get file name
$tenon_file = "../../tenons/" . $title_to_file . ".txt";
// complile data
$tenon_data = $tenon_title . "|&|" . $o_date . "|&|" . $g_date . "|&|" . $tenon_post;

// errors
$log_file = __DIR__ . '/../admin/log.txt'; 

$log_custom_error = function($message) use ($log_file, $o_date) {
$error_log_data = ($o_date ?? ' ') . " -- " . $message . PHP_EOL;
file_put_contents($log_file, $error_log_data, FILE_APPEND | LOCK_EX);
};

if (empty($title_to_file)) { 
$log_custom_error("Error: File path is empty.");
header('Location: ../../index.php');
exit;
}

$tenon_dir = dirname($tenon_file);
if (!is_dir($tenon_dir)) { 
$log_custom_error("Error: Dir $tenon_dir does not exist.");
header('Location: ../../index.php');
exit; 
}
if (!is_writable($tenon_dir)) { 
$log_custom_error("Error: Dir $tenon_dir is not writable.");
header('Location: ../../index.php');
exit; 
}

// create and write
try {
$write_status = file_put_contents($tenon_file, $tenon_data, LOCK_EX);
if ($write_status === false) { 
throw new Exception("Failed to write stream."); 
}
} catch (\Throwable $e) { 
$log_custom_error("Exception: " . $e->getMessage());
}

header("location:../../index.php?tenon=" . urlencode($title_to_file));
exit;
} else {
header('Location: ../../index.php');
exit;
}
?>