From 251d6b298c1cb9c1ace3dbbe00ff45dfd0baec79 Mon Sep 17 00:00:00 2001 From: hbrain Date: Mon, 25 May 2026 20:45:34 +0200 Subject: [PATCH] Add per-file captions to admin form --- new.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/new.php b/new.php index 7aaf6b2..382716f 100644 --- a/new.php +++ b/new.php @@ -7,20 +7,21 @@ function rrmdir($dir){ if(!is_dir($dir)) return; foreach(scandir($dir) as $f){ i function input_dirs($base){ $dirs = glob($base.'/*', GLOB_ONLYDIR) ?: []; usort($dirs, fn($a,$b)=>filemtime($b)<=>filemtime($a)); return $dirs; } function safe_input_dir($base, $name){ $name = basename((string)$name); $path = realpath($base . '/' . $name); $root = realpath($base); if (!$path || !$root || !str_starts_with($path, $root . DIRECTORY_SEPARATOR) || !is_dir($path)) throw new RuntimeException('Invalid input directory.'); return $path; } function read_data($dir){ - $data = ['title'=>'','date'=>'','place'=>'','description'=>'']; $file = $dir . '/data.txt'; + $data = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>'']; $file = $dir . '/data.txt'; if (!is_file($file)) return $data; - $header = []; $description = []; $inDescription = false; + $header = []; $description = []; $captions = []; $inDescription = false; foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) { $line = trim($line); if ($line === '' && ($header || $inDescription)) { $inDescription = true; continue; } if ($line === '' || str_starts_with($line, '#')) continue; - if (str_contains($line, ':')) continue; // key:value is only for per-file captions + if (str_contains($line, ':')) { $captions[] = $line; continue; } // key:value is only for per-file captions if ($inDescription) $description[] = $line; else $header[] = $line; } if ($header) $data['title'] = $header[0]; if (count($header) > 1) $data['place'] = $header[1]; if (count($header) > 2) $data['date'] = $header[2]; if ($description) $data['description'] = implode("\n", $description); + if ($captions) $data['captions'] = implode("\n", $captions); return $data; } function write_data($dir, $post){ @@ -28,12 +29,20 @@ function write_data($dir, $post){ $place = trim($post['place'] ?? ''); $date = trim($post['date'] ?? ''); $description = trim($post['description'] ?? ''); + $captions = trim($post['captions'] ?? ''); if (function_exists('mb_substr')) $description = mb_substr($description, 0, 5000, 'UTF-8'); else $description = substr($description, 0, 5000); $lines = []; if ($title !== '') $lines[] = $title; if ($place !== '') $lines[] = $place; if ($place !== '' && $date !== '') $lines[] = $date; if ($description !== '') { $lines[] = ''; $lines[] = $description; } + if ($captions !== '') { + if ($description === '') $lines[] = ''; + foreach (preg_split('/\R/', $captions) as $captionLine) { + $captionLine = trim($captionLine); + if ($captionLine !== '' && str_contains($captionLine, ':')) $lines[] = $captionLine; + } + } file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n"); } function list_files($dir){ $files = array_values(array_filter(scandir($dir), fn($f)=>$f!=='.' && $f!=='..' && is_file($dir.'/'.$f) && $f !== 'data.txt')); natcasesort($files); return $files; } @@ -93,13 +102,13 @@ try { } catch (Throwable $e) { $err = $e->getMessage(); } $dirs = input_dirs($config['uploads_dir']); $editName = $_GET['edit'] ?? ''; -$editDir = null; $editData = ['title'=>'','date'=>'','place'=>'','description'=>'']; $editFiles = []; +$editDir = null; $editData = ['title'=>'','date'=>'','place'=>'','description'=>'','captions'=>'']; $editFiles = []; if ($editName !== '') { try { $editDir = safe_input_dir($config['uploads_dir'], $editName); $editData = read_data($editDir); $editFiles = list_files($editDir); } catch (Throwable $e) { $err = $e->getMessage(); } } ?> Admin - <?=h($config['site_name'])?>

MVLog Admin

Existing input dirs

No input directories yet.


ยท files
Edit
-

Edit input dir

in-dir/

Files

-

Add new movie input

+

Edit input dir

in-dir/

Files

+

Add new movie input