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

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

// get title
$panel_title = $_POST["title"] ?? '';

// get post
$panel_post = $_POST["panel"] ?? '';

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

$title_to_file = preg_replace('/[<>\:"\/\\\\\|\?\*\'#]/', '_', $title_to_file);
$title_to_file = str_replace(['..', "\0"], '_', $title_to_file);

// get file name
$panel_file = "../../panels/{$title_to_file}.txt";

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

function log_custom_error(string $message, string $log_file, string $o_date): void {
error_log("{$o_date} -- {$message}" . PHP_EOL, 3, $log_file);
}

if (empty($title_to_file)) { 
log_custom_error("Error: File path is empty.", $log_file, $o_date);
exit;
}

$panel_fold = dirname($panel_file);
if (!is_dir($panel_fold)) { 
log_custom_error("Error: Dir {$panel_fold} does not exist.", $log_file, $o_date);
exit; 
}

if (!is_writable($panel_fold)) { 
log_custom_error("Error: Dir {$panel_fold} is not writable.", $log_file, $o_date);
exit; 
}

// write file
try {
$write_result = file_put_contents($panel_file, $panel_post, LOCK_EX);
if ($write_result === false) { 
throw new Exception("Failed to open stream."); 
}
} catch (\Throwable $e) { 
log_custom_error("Exception: " . $e->getMessage(), $log_file, $o_date);
exit;
}

// write user/panel
// 1 = Show 2 = Hide
if (isset($_POST["hidetitle"]) && $_POST["hidetitle"] === 'yes') {
$hide_title = '2';
} else {
$hide_title = '1';
}	

// create new entry
$panel_insert = $hide_title . "" . $title_to_file;
$panel_usr_file = "../../user/panels.txt";

$usr_write = file_put_contents($panel_usr_file, "\n" . $panel_insert, FILE_APPEND | LOCK_EX);
if ($usr_write === false) {
log_custom_error("Error: Failed to write to panels.txt.", $log_file, $o_date);
}

header("Location: ../../index.php?user=panels");
exit;