Thumb source: =h((string)$thumbMeta['source_file'])?>
= $titleText ?>= $titleText ?>
=nl2br(h($meta['description']), false)?>
“=h(trim((string)$meta['quote_da'], "\"“”"))?>”
=h(implode(' · ', $detailParts))?>
'ae','Æ'=>'ae','ø'=>'o','Ø'=>'o','å'=>'aa','Å'=>'aa', 'ä'=>'ae','Ä'=>'ae','ö'=>'oe','Ö'=>'oe','ü'=>'ue','Ü'=>'ue','ß'=>'ss','ẞ'=>'ss', 'č'=>'c','Č'=>'c','ć'=>'c','Ć'=>'c','ž'=>'z','Ž'=>'z','š'=>'s','Š'=>'s','đ'=>'d','Đ'=>'d', ]); if (function_exists('iconv')) { $x = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s); if ($x !== false) $s = $x; } $s = lower_text($s); return preg_replace('/[^a-z0-9]+/', ' ', $s) ?? ''; } function ci_contains($haystack, $needle){ $haystack = (string)$haystack; $needle = (string)$needle; if ($needle === '') return true; if (function_exists('mb_stripos') && mb_stripos($haystack, $needle, 0, 'UTF-8') !== false) return true; if (strpos(lower_text($haystack), lower_text($needle)) !== false) return true; $foldHay = fold_text($haystack); $foldNeedle = trim(fold_text($needle)); if ($foldNeedle === '') return false; return strpos($foldHay, $foldNeedle) !== false; } function ascii_safe($s){ $s = strtr((string)$s, [ 'æ'=>'ae','Æ'=>'Ae','ø'=>'o','Ø'=>'O','å'=>'aa','Å'=>'Aa', 'ä'=>'ae','Ä'=>'Ae','ö'=>'oe','Ö'=>'Oe','ü'=>'ue','Ü'=>'Ue','ß'=>'ss','ẞ'=>'SS', 'č'=>'c','Č'=>'C','ć'=>'c','Ć'=>'C','ž'=>'z','Ž'=>'Z','š'=>'s','Š'=>'S','đ'=>'d','Đ'=>'D', ]); if (function_exists('iconv')) { $x = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s); if ($x !== false) $s = $x; } return preg_replace('/[^\x20-\x7E]/', '', $s) ?? ''; } function slugify($s){ $s = ascii_safe($s); $s = strtolower(trim($s)); $s = preg_replace('/[^a-z0-9]+/', '-', $s); return trim($s, '-') ?: 'movie'; } function rrmdir($dir){ if(!is_dir($dir)) return; foreach(scandir($dir) as $f){ if($f==='.'||$f==='..') continue; $p="$dir/$f"; is_dir($p)?rrmdir($p):unlink($p);} rmdir($dir); } function apply_thumbnail_overlay($imagePath, $title, $teaser) { $logFile = __DIR__ . '/logs/image_magick.log'; file_put_contents($logFile, "--- New Thumbnail Job --- ", FILE_APPEND); $fontPath = __DIR__ . '/assets/fonts/IBMPlexSans-SemiBold.ttf'; $titleFontPath = is_file(__DIR__ . '/assets/fonts/BebasNeue-Regular.ttf') ? __DIR__ . '/assets/fonts/BebasNeue-Regular.ttf' : $fontPath; $teaserFontPath = is_file(__DIR__ . '/assets/fonts/NotoSans-Bold.ttf') ? __DIR__ . '/assets/fonts/NotoSans-Bold.ttf' : $fontPath; if (!is_file($fontPath)) { throw new RuntimeException('Thumbnail font file is missing.'); } $scale = 3; $baseW = 1200; $baseH = 630; $imgW = $baseW * $scale; $imgH = $baseH * $scale; $targetW = $baseW; $targetH = $baseH; $normalize = static function ($text): string { $text = trim((string)$text); if ($text === '') return ''; $text = preg_replace('/\s+/u', ' ', $text) ?? $text; return trim($text); }; $wrapText = static function ($text, int $width) use ($normalize): string { $text = $normalize($text); return $text === '' ? '' : wordwrap($text, $width, " ", false); }; $clampWrappedText = static function ($text, int $width, int $maxLines, bool $cutLongWords = false) use ($normalize): string { $text = $normalize($text); if ($text === '') return ''; $words = preg_split('/\s+/u', $text, -1, PREG_SPLIT_NO_EMPTY) ?: []; if (!$words) return ''; $candidate = implode(' ', $words); while (true) { $wrapped = wordwrap($candidate, $width, " ", $cutLongWords); $lineCount = substr_count($wrapped, " ") + 1; if ($lineCount <= $maxLines || count($words) <= 1) return $wrapped; array_pop($words); $candidate = implode(' ', $words) . '…'; } }; $fitTitleText = static function ($text) use ($wrapText): array { $bestText = ''; $bestSize = 95; $bestWidth = 28; $bestLines = 1; for ($size = 84; $size >= 54; $size -= 4) { $width = max(16, min(46, (int)round(23 + (84 - $size) / 2.7))); $candidate = $wrapText($text, $width); $lines = substr_count($candidate, " ") + 1; $bestText = $candidate; $bestSize = $size; $bestWidth = $width; $bestLines = $lines; if ($lines <= 2) break; } return [$bestText, $bestSize, $bestWidth, $bestLines]; }; [$titleText, $titleFontSizeBase, $titleWrapWidth, $titleLines] = $fitTitleText($title); $teaserText = $clampWrappedText($teaser, 34, 6, true); $teaserLines = max(1, substr_count($teaserText, " ") + 1); $baseLeft = max(14, (int)round($baseW * 0.046)); $baseTitleY = max(20, (int)round($baseH * 0.062)); $baseTitleLineStep = max(68, (int)round($baseH * 0.043)); $baseTeaserY = max((int)round($baseH * 0.235), $baseTitleY + ($titleLines * $baseTitleLineStep) + max(54, (int)round($baseH * 0.03))); $left = $baseLeft * $scale; $titleY = $baseTitleY * $scale; $titleLineStep = $baseTitleLineStep * $scale; $teaserY = $baseTeaserY * $scale; $shadowOffset = 3 * $scale; $titleLineSpacing = 2 * $scale; $teaserLineSpacing = -18 * $scale; $titlePointSize = $titleFontSizeBase * $scale; $teaserPointSize = 58 * $scale; $shadowFill = 'rgba(0,0,0,0.35)'; $titleFill = '#F3F4F6'; $teaserFill = '#EADFC9'; $overlayBase = tempnam(sys_get_temp_dir(), 'thumb_overlay_'); $outputPath = dirname($imagePath) . '/' . basename($imagePath) . '.tmp.' . uniqid('', true) . '.jpg'; if ($overlayBase === false) { throw new RuntimeException('Failed to create temporary thumbnail overlay file.'); } $overlayPath = $overlayBase . '.png'; @unlink($overlayBase); $cmd1 = sprintf( '/usr/bin/convert -size %dx%d xc:none -font %s -pointsize %d -fill %s -interline-spacing %d -gravity northwest -annotate +%d+%d %s -fill %s -annotate +%d+%d %s -font %s -pointsize %d -fill %s -interline-spacing %d -gravity northwest -annotate +%d+%d %s -fill %s -annotate +%d+%d %s %s 2>&1', $imgW, $imgH, escapeshellarg($titleFontPath), $titlePointSize, escapeshellarg($shadowFill), $titleLineSpacing, $left + $shadowOffset, $titleY + $shadowOffset, escapeshellarg($titleText), escapeshellarg($titleFill), $left, $titleY, escapeshellarg($titleText), escapeshellarg($teaserFontPath), $teaserPointSize, escapeshellarg($shadowFill), $teaserLineSpacing, $left + $shadowOffset, $teaserY + $shadowOffset, escapeshellarg($teaserText), escapeshellarg($teaserFill), $left, $teaserY, escapeshellarg($teaserText), escapeshellarg($overlayPath) ); file_put_contents($logFile, "Overlay CMD: " . $cmd1 . " ", FILE_APPEND); $out1 = shell_exec($cmd1); file_put_contents($logFile, "Overlay Output: " . $out1 . " ", FILE_APPEND); if (!is_file($overlayPath) || filesize($overlayPath) < 100) { @unlink($overlayPath); @unlink($outputPath); throw new RuntimeException('Failed to render thumbnail overlay: ' . trim((string)$out1)); } $maxBytes = 600 * 1024; $qualities = [96, 92, 88, 84, 80, 76, 72, 68, 64]; $finalPath = ''; $finalSize = 0; $finalOut = ''; foreach ($qualities as $quality) { $attemptPath = dirname($imagePath) . '/' . basename($imagePath) . '.tmp.' . uniqid('', true) . '.jpg'; $cmd2 = sprintf( '/usr/bin/convert -filter Lanczos -define filter:blur=0.85 %s -resize %dx%d^ -gravity center -extent %dx%d -modulate 70,82 %s -composite -resize %dx%d -gravity center -extent %dx%d -unsharp 0x0.75+0.75+0.008 -strip -interlace Plane -sampling-factor 4:2:0 -quality %d %s 2>&1', escapeshellarg($imagePath), $imgW, $imgH, $imgW, $imgH, escapeshellarg($overlayPath), $targetW, $targetH, $targetW, $targetH, $quality, escapeshellarg($attemptPath) ); file_put_contents($logFile, "Composite CMD (q=$quality): " . $cmd2 . " ", FILE_APPEND); $out2 = shell_exec($cmd2); file_put_contents($logFile, "Composite Output (q=$quality): " . $out2 . " ", FILE_APPEND); if (!is_file($attemptPath) || filesize($attemptPath) < 100) { @unlink($attemptPath); $finalOut = trim((string)$out2); continue; } $finalPath = $attemptPath; $finalSize = filesize($attemptPath); $finalOut = trim((string)$out2); if ($finalSize <= $maxBytes) break; } if ($finalPath === '') { @unlink($overlayPath); @unlink($outputPath); throw new RuntimeException('Failed to compose thumbnail overlay: ' . $finalOut); } if ($finalSize > $maxBytes) { file_put_contents($logFile, "Thumbnail still above size limit: " . $finalSize . " bytes ", FILE_APPEND); } if (!@rename($finalPath, $imagePath)) { @unlink($overlayPath); @unlink($finalPath); throw new RuntimeException('Failed to replace thumbnail image.'); } @unlink($overlayPath); @unlink($outputPath); } function input_dirs($base){ return glob($base.'/*', GLOB_ONLYDIR) ?: []; } 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 article_id_is_valid($id){ return is_string($id) && preg_match('/^[0-9]{14}[a-f0-9]{16}$/', $id); } function generate_article_id(){ return gmdate('YmdHis') . bin2hex(random_bytes(8)); } function read_article_id($dir){ $idFile = rtrim((string)$dir, '/').'/'.ARTICLE_ID_FILE; if (!is_file($idFile)) return ''; $id = trim((string)file_get_contents($idFile)); return article_id_is_valid($id) ? $id : ''; } function write_article_id($dir, $id){ if (!article_id_is_valid($id)) throw new RuntimeException('Invalid article id.'); $idFile = rtrim((string)$dir, '/').'/'.ARTICLE_ID_FILE; $tmp = $idFile . '.tmp.' . bin2hex(random_bytes(4)); if (file_put_contents($tmp, $id . "\n", LOCK_EX) === false) throw new RuntimeException('Cannot write article id.'); @chmod($tmp, 0664); if (!rename($tmp, $idFile)) { @unlink($tmp); throw new RuntimeException('Cannot save article id.'); } } function ensure_article_id($dir, &$seenIds = null){ $existing = read_article_id($dir); if ($existing !== '' && (!is_array($seenIds) || empty($seenIds[$existing]))) { if (is_array($seenIds)) $seenIds[$existing] = basename((string)$dir); return $existing; } do { $id = generate_article_id(); } while (is_array($seenIds) && !empty($seenIds[$id])); write_article_id($dir, $id); if (is_array($seenIds)) $seenIds[$id] = basename((string)$dir); return $id; } function article_index_signature($dirs){ $names = array_map('basename', $dirs); sort($names, SORT_STRING); $parts = []; foreach ($names as $name) $parts[] = $name; return hash('sha256', implode("\n", $parts)); } function load_articles_index($base, $dirs = null){ if ($dirs === null) $dirs = input_dirs($base); $cacheFile = __DIR__ . '/cache/articles-index.json'; $signature = article_index_signature($dirs); $cache = is_file($cacheFile) ? json_decode((string)file_get_contents($cacheFile), true) : []; if (is_array($cache) && ($cache['version'] ?? 0) === 1 && ($cache['signature'] ?? '') === $signature && is_array($cache['by_id'] ?? null) && is_array($cache['by_dir'] ?? null) && !in_array('', $cache['by_dir'], true)) { return $cache; } $byId = []; $byDir = []; foreach ($dirs as $dir) { $name = basename((string)$dir); $id = read_article_id($dir); if ($id === '' || !empty($byId[$id])) { $id = ensure_article_id($dir, $byId); } if ($id !== '' && empty($byId[$id])) $byId[$id] = $name; $byDir[$name] = $id; } ksort($byId, SORT_STRING); ksort($byDir, SORT_STRING); $out = [ 'version' => 1, 'generated_at' => date('c'), 'signature' => $signature, 'by_id' => $byId, 'by_dir' => $byDir, ]; file_put_contents($cacheFile, json_encode($out, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX); return $out; } function resolve_input_dir($base, $idOrDir, $articleIndex = null){ $token = trim((string)$idOrDir); if ($token === '') throw new RuntimeException('Invalid input directory.'); if ($articleIndex === null) $articleIndex = load_articles_index($base); if (article_id_is_valid($token)) { $name = (string)($articleIndex['by_id'][$token] ?? ''); if ($name !== '') return safe_input_dir($base, $name); } return safe_input_dir($base, $token); } function resolve_input_dir_from_request($base, $id, $dir, $articleIndex = null){ $id = trim((string)$id); $dir = trim((string)$dir); if ($id !== '') { try { return resolve_input_dir($base, $id, $articleIndex); } catch (Throwable $e) { if ($dir !== '') return resolve_input_dir($base, $dir, $articleIndex); throw $e; } } return resolve_input_dir($base, $dir, $articleIndex); } function read_data($dir){ $data = ['title'=>'','teaser'=>'','quote_da'=>'','date'=>'','place'=>'','description'=>'','captions'=>[],'video_audio'=>[]]; $file = $dir . '/data.txt'; if (!is_file($file)) return $data; $lines = file($file, FILE_IGNORE_NEW_LINES); for ($i = 0; $i < count($lines); $i++) { $line = trim($lines[$i]); if ($line === '' || str_starts_with($line, '#') || !str_contains($line, ':')) continue; [$key, $value] = array_map('trim', explode(':', $line, 2)); $low = strtolower($key); if ($value === '|' && in_array($low, ['description','desc','synopsis'], true)) { $block = []; while ($i + 1 < count($lines)) { $next = $lines[$i + 1]; if (trim($next) !== '' && $next[0] !== ' ' && $next[0] !== "\t") break; $i++; if (str_starts_with($next, ' ')) $next = substr($next, 2); elseif (str_starts_with($next, ' ') || str_starts_with($next, "\t")) $next = substr($next, 1); $block[] = rtrim($next); } $data['description'] = trim(implode("\n", $block)); } elseif (in_array($low, ['title','name'], true)) $data['title'] = $value; elseif (in_array($low, ['teaser','tagline','subtitle'], true)) $data['teaser'] = $value; elseif ($low === 'quote_da') $data['quote_da'] = trim($value, "\"“”"); elseif (in_array($low, ['date','dates','when'], true)) $data['date'] = $value; elseif (in_array($low, ['place','location','where'], true)) $data['place'] = $value; elseif (in_array($low, ['description','desc','synopsis'], true)) $data['description'] = $value; elseif (str_ends_with($low, ' audio')) { $fileKey = trim(substr($key, 0, -6)); if ($fileKey !== '' && in_array(strtolower($value), ['1','yes','true','on'], true)) $data['video_audio'][$fileKey] = true; } elseif ($key !== '') $data['captions'][$key] = $value; } return $data; } function write_data($dir, $post){ $title = trim($post['title'] ?? ''); $teaser = trim($post['teaser'] ?? ''); $quote_da = trim($post['quote_da'] ?? ''); $place = trim($post['place'] ?? ''); $date = trim($post['date'] ?? ''); $description = trim($post['description'] ?? ''); if (function_exists('mb_substr')) $description = mb_substr($description, 0, 5000, 'UTF-8'); else $description = substr($description, 0, 5000); $lines = []; if ($title !== '') $lines[] = 'Title: ' . $title; if ($teaser !== '') $lines[] = 'Teaser: ' . $teaser; if ($quote_da !== '') $lines[] = 'Quote_da: ' . trim($quote_da, "\"“”"); if ($place !== '') $lines[] = 'Location: ' . $place; if ($date !== '') $lines[] = 'Date: ' . $date; if ($description !== '') { $lines[] = 'Description: |'; $description = str_replace(["\r\n", "\r"], "\n", $description); foreach (explode("\n", $description) as $descLine) $lines[] = ' ' . rtrim($descLine); } $useAudioFiles = $post['use_audio_files'] ?? []; $useAudio = is_array($useAudioFiles) ? array_flip(array_map('basename', array_map('strval', $useAudioFiles))) : []; foreach (array_keys($useAudio) as $fileName) if ($fileName !== '') $lines[] = $fileName . ' audio: yes'; $captionFiles = $post['caption_files'] ?? []; $captions = $post['captions'] ?? []; if (is_array($captionFiles) && is_array($captions)) { foreach ($captionFiles as $i => $fileName) { $fileName = basename((string)$fileName); $caption = trim((string)($captions[$i] ?? '')); if ($fileName !== '' && $caption !== '') $lines[] = $fileName . ': ' . $caption; } } file_put_contents($dir . '/data.txt', implode("\n", $lines) . "\n"); } function editable_file($f){ $ext = strtolower(pathinfo((string)$f, PATHINFO_EXTENSION)); return $f !== '' && $f[0] !== '.' && $f !== 'data.txt' && in_array($ext, ['jpg','jpeg','png','webp','gif','mp4','mov','m4v','avi','mkv','webm','mp3','wav','m4a','aac','ogg','flac'], true); } function natural_file_compare($a, $b){ return strnatcasecmp((string)$a, (string)$b); } function is_visual_media_file($name){ $ext = strtolower(pathinfo((string)$name, PATHINFO_EXTENSION)); return in_array($ext, ['jpg','jpeg','png','webp','gif','mp4','mov','m4v','avi','mkv','webm'], true); } function parse_date_from_filename($path) { $filename = basename($path); if (!preg_match('/(?:^|[^0-9])(\\d{8})[_-]?(\\d{6})(?:[^0-9]|$)/', $filename, $m)) { return null; } $y = (int)substr($m[1], 0, 4); $mo = (int)substr($m[1], 4, 2); $d = (int)substr($m[1], 6, 2); $hh = (int)substr($m[2], 0, 2); $mi = (int)substr($m[2], 2, 2); $ss = (int)substr($m[2], 4, 2); if (!checkdate($mo, $d, $y) || $hh > 23 || $mi > 59 || $ss > 59) { return null; } return $m[1] . $m[2]; } function mvlog_local_timezone(){ static $tz = null; if ($tz instanceof DateTimeZone) return $tz; $name = trim((string)@file_get_contents('/etc/timezone')); if ($name === '') $name = (string)date_default_timezone_get(); if ($name === '') $name = 'UTC'; try { $tz = new DateTimeZone($name); } catch (Throwable $e) { $tz = new DateTimeZone('UTC'); } return $tz; } function parse_embedded_datetime_to_local($value){ $value = trim((string)$value); if ($value === '') return null; try { $dt = new DateTimeImmutable($value); return $dt->setTimezone(mvlog_local_timezone())->format('YmdHis'); } catch (Throwable $e) { return null; } } function media_file_date($path){ $ext = strtolower(pathinfo((string)$path, PATHINFO_EXTENSION)); // Priority 2: Exif/embedded metadata if (in_array($ext, ['jpg','jpeg','tif','tiff'], true) && function_exists('exif_read_data')) { $exif = @exif_read_data($path); foreach (['DateTimeOriginal','DateTimeDigitized','DateTime'] as $k) { $value = (string)($exif[$k] ?? ''); if ($value !== '' && preg_match('/^(\\d{4}):(\\d{2}):(\\d{2})(?:\\s+(\\d{2}):(\\d{2}):(\\d{2}))?/', $value, $m)) { $date = $m[1] . $m[2] . $m[3]; $time = isset($m[4]) ? ($m[4] . $m[5] . $m[6]) : '000000'; return $date . $time; } } } if (in_array($ext, ['mp4','mov','m4v','avi','mkv','webm'], true)) { $ffprobe = trim((string)shell_exec('command -v ffprobe 2>/dev/null')); if ($ffprobe !== '') { $json = shell_exec(escapeshellarg($ffprobe).' -v quiet -print_format json -show_entries format_tags=creation_time:stream_tags=creation_time '.escapeshellarg($path).' 2>/dev/null'); $data = json_decode((string)$json, true); $candidates = []; if (is_array($data['format']['tags'] ?? null)) $candidates[] = (string)($data['format']['tags']['creation_time'] ?? ''); foreach (($data['streams'] ?? []) as $stream) if (is_array($stream['tags'] ?? null)) $candidates[] = (string)($stream['tags']['creation_time'] ?? ''); foreach ($candidates as $value) { $normalized = parse_embedded_datetime_to_local($value); if ($normalized !== null) { return $normalized; } if (preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})/', $value, $m)) { return $m[1].$m[2].$m[3].'000000'; } } } } // Priority 3: filename parsing $from_filename = parse_date_from_filename($path); if ($from_filename) { return $from_filename; } return null; } function media_sort_datetime($path){ return media_file_date($path) ?? date('YmdHis', filemtime($path)); } function list_files($dir){ $files = array_values(array_filter(scandir($dir), fn($f)=>editable_file($f) && is_file($dir.'/'.$f))); $orderFile = $dir . '/order.json'; $media = []; $other = []; foreach ($files as $name) { if (is_visual_media_file($name)) $media[] = $name; else $other[] = $name; } // Priority 1: explicit order file, but regenerate if file set changed. if (is_file($orderFile)) { $order = json_decode((string)file_get_contents($orderFile), true); if (is_array($order)) { $mediaSet = array_fill_keys($media, true); $orderSet = []; $sortedMedia = []; foreach ($order as $name) { if (!is_string($name) || !is_visual_media_file($name)) continue; if (isset($mediaSet[$name]) && !isset($orderSet[$name])) { $orderSet[$name] = true; $sortedMedia[] = $name; } } $sameSet = (count($orderSet) === count($mediaSet)); if ($sameSet) { foreach ($mediaSet as $name => $_) { if (!isset($orderSet[$name])) { $sameSet = false; break; } } } if ($sameSet) { return array_merge($sortedMedia, $other); } } } // Missing/invalid/stale order.json: sort by datetime and recreate order.json usort($media, function($a, $b) use ($dir) { $da = media_sort_datetime($dir . '/' . $a); $db = media_sort_datetime($dir . '/' . $b); if ($da === $db) return 0; // never fallback to filename ordering return strcmp($da, $db); }); file_put_contents($orderFile, json_encode(array_values($media), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); @chmod($orderFile, 0664); return array_merge($media, $other); } function media_sort_files($dir, $files){ return list_files($dir); } function visual_order_files($dir){ $all = list_files($dir); return array_values(array_filter($all, fn($name)=>is_visual_media_file($name))); } function write_visual_order($dir, $order){ $clean = []; $seen = []; foreach ((array)$order as $name) { if (!is_string($name)) continue; $name = basename($name); if (!is_visual_media_file($name) || isset($seen[$name])) continue; if (!is_file($dir . '/' . $name)) continue; $seen[$name] = true; $clean[] = $name; } file_put_contents($dir . '/order.json', json_encode(array_values($clean), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); @chmod($dir . '/order.json', 0664); return $clean; } function move_visual_file_order($dir, $file, $direction){ $file = basename((string)$file); if (!is_visual_media_file($file) || !is_file($dir . '/' . $file)) throw new RuntimeException('Invalid media file for reordering.'); $direction = strtolower(trim((string)$direction)); if (!in_array($direction, ['up','down'], true)) throw new RuntimeException('Invalid move direction.'); $order = visual_order_files($dir); $idx = array_search($file, $order, true); if ($idx === false) throw new RuntimeException('File not found in visual order.'); $newIdx = $idx; if ($direction === 'up' && $idx > 0) { [$order[$idx - 1], $order[$idx]] = [$order[$idx], $order[$idx - 1]]; $newIdx = $idx - 1; } elseif ($direction === 'down' && $idx < count($order) - 1) { [$order[$idx], $order[$idx + 1]] = [$order[$idx + 1], $order[$idx]]; $newIdx = $idx + 1; } write_visual_order($dir, $order); return [ 'changed' => $newIdx !== $idx, 'index' => $newIdx, 'count' => count($order), ]; } function input_dir_date_from_media($dir){ $dates = []; foreach (list_files($dir) as $file) { $date = media_file_date($dir . '/' . $file); if ($date !== null) $dates[] = $date; } if ($dates) { sort($dates, SORT_STRING); return $dates[0]; } return date('Ymd'); } function input_dir_date_from_text($text){ $text = trim((string)$text); if ($text === '') return null; if (preg_match('/^(\d{4})[.-]?(\d{2})[.-]?(\d{2})$/', $text, $m) && checkdate((int)$m[2], (int)$m[3], (int)$m[1])) return "$m[1]$m[2]$m[3]"; if (preg_match('/^(\d{1,2})[\/. -](\d{1,2})[\/. -](\d{4})$/', $text, $m) && checkdate((int)$m[2], (int)$m[1], (int)$m[3])) return sprintf('%04d%02d%02d', $m[3], $m[2], $m[1]); $ts = strtotime($text); if ($ts !== false) return date('Ymd', $ts); if (preg_match('/^(jan|january|feb|february|mar|march|apr|april|may|jun|june|jul|july|aug|august|sep|sept|september|oct|october|nov|november|dec|december)\s+(\d{4})$/i', $text, $m)) { $months = ['jan'=>1,'january'=>1,'feb'=>2,'february'=>2,'mar'=>3,'march'=>3,'apr'=>4,'april'=>4,'may'=>5,'jun'=>6,'june'=>6,'jul'=>7,'july'=>7,'aug'=>8,'august'=>8,'sep'=>9,'sept'=>9,'september'=>9,'oct'=>10,'october'=>10,'nov'=>11,'november'=>11,'dec'=>12,'december'=>12]; return sprintf('%04d%02d01', (int)$m[2], $months[strtolower($m[1])]); } return null; } function unique_input_dir($base, $slug){ $dir = $base . '/' . $slug; if (!file_exists($dir)) return $dir; for ($i = 2; ; $i++) { $candidate = $base . '/' . $slug . '-' . $i; if (!file_exists($candidate)) return $candidate; } } function input_dir_signature($dir){ $script = <<<'PY' import hashlib, json, os, sys root = sys.argv[1] entries = [] for dirpath, dirnames, filenames in os.walk(root): dirnames[:] = [d for d in dirnames if d != '.movmaker-lock'] for name in filenames: if name in {'.movmaker-state.json', '.movmaker-enabled', '.movmaker-preview', '.mvlog-hidden', '.mvlog-permalink', '.mvlog-id', '.mvlog-hide-map'}: continue path = os.path.join(dirpath, name) rel = os.path.relpath(path, root) st = os.stat(path) mtime_ns = getattr(st, 'st_mtime_ns', int(st.st_mtime * 1_000_000_000)) entries.append([rel, st.st_size, mtime_ns]) entries.sort() print(hashlib.sha256(json.dumps(entries, ensure_ascii=False, separators=(',', ':')).encode('utf-8')).hexdigest()) PY; $cmd = 'python3 -c ' . escapeshellarg($script) . ' ' . escapeshellarg($dir); $output = trim(shell_exec($cmd) ?? ''); return $output !== '' ? $output : ''; } function input_dir_info($dir){ $data = read_data($dir); $files = list_files($dir); return [ 'name' => basename($dir), 'article_id' => ensure_article_id($dir), 'title' => $data['title'] ?: basename($dir), 'sort_date' => input_dir_date_from_text($data['date'] ?? '') ?? input_dir_date_from_media($dir), 'file_count' => count($files), 'signature' => input_dir_signature($dir), ]; } function load_input_dir_cache($dirs){ $cacheFile = __DIR__ . '/cache/input-dirs.json'; $cache = is_file($cacheFile) ? json_decode((string)file_get_contents($cacheFile), true) : []; if (!is_array($cache) || ($cache['version'] ?? 0) !== 1) $cache = ['version'=>1,'items'=>[]]; $oldItems = is_array($cache['items'] ?? null) ? $cache['items'] : []; $items = []; $changed = false; foreach ($dirs as $dir) { $name = basename($dir); $sig = input_dir_signature($dir); $cached = $oldItems[$name] ?? null; if (is_array($cached) && ($cached['signature'] ?? '') === $sig && !empty($cached['article_id'])) { $items[$name] = $cached; } else { $items[$name] = input_dir_info($dir); $changed = true; } } if (array_diff(array_keys($oldItems), array_keys($items))) $changed = true; if ($changed) file_put_contents($cacheFile, json_encode(['version'=>1,'generated_at'=>date('c'),'items'=>$items], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX); return $items; } function sort_input_dirs_by_metadata_date(&$dirs, $dirInfo){ usort($dirs, function($a, $b) use ($dirInfo) { $ad = $dirInfo[basename($a)]['sort_date'] ?? '00000000'; $bd = $dirInfo[basename($b)]['sort_date'] ?? '00000000'; $cmp = strcmp($bd, $ad); return $cmp !== 0 ? $cmp : strnatcasecmp(basename($b), basename($a)); }); } function orphan_videos($config){ $referenced = []; foreach (input_dirs($config['uploads_dir']) as $dir) { $stateFile = $dir . '/.movmaker-state.json'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; if (is_array($state) && !empty($state['output'])) $referenced[basename((string)$state['output'])] = true; } $videos = glob($config['videos_dir'].'/*.{mp4,webm,mov,m4v}', GLOB_BRACE) ?: []; return array_values(array_filter($videos, fn($v)=>empty($referenced[basename($v)]))); } function input_dir_status($dir){ $stateFile = $dir . '/.movmaker-state.json'; $lockDir = $dir . '/.movmaker-lock'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; $status = is_array($state) ? (string)($state['status'] ?? '') : ''; if (is_dir($lockDir)) return $status !== '' ? $status : 'processing'; return in_array($status, ['processing', 'stale', 'error'], true) ? $status : ''; } function input_dir_running($dir){ $stateFile = $dir . '/.movmaker-state.json'; $lockDir = $dir . '/.movmaker-lock'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; $status = is_array($state) ? (string)($state['status'] ?? '') : ''; return is_dir($lockDir) || $status === 'processing'; } function input_dir_enabled($dir){ return is_file($dir . '/.movmaker-enabled'); } function set_input_dir_enabled($dir, $enabled){ $path = $dir . '/.movmaker-enabled'; if ($enabled) { file_put_contents($path, "enabled\n"); @chmod($path, 0664); } elseif (is_file($path)) unlink($path); } function input_dir_preview($dir){ if (is_file($dir . '/.movmaker-preview')) return true; $stateFile = $dir . '/.movmaker-state.json'; if (!is_file($stateFile)) return false; $state = json_decode((string)file_get_contents($stateFile), true); return is_array($state) && !empty($state['preview']); } function set_input_dir_preview($dir, $preview){ $path = $dir . '/.movmaker-preview'; if ($preview) { file_put_contents($path, "preview\n"); @chmod($path, 0664); } elseif (is_file($path)) unlink($path); $stateFile = $dir . '/.movmaker-state.json'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; if (!is_array($state)) $state = []; $state['article_id'] = ensure_article_id($dir); $state['preview'] = (bool)$preview; $state['preview_updated_at'] = gmdate('c'); file_put_contents($stateFile, json_encode($state, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n"); @chmod($stateFile, 0664); } function input_dir_visible($dir){ return !is_file($dir . '/.mvlog-hidden'); } function set_input_dir_visible($dir, $visible){ $path = $dir . '/.mvlog-hidden'; if ($visible) { if (is_file($path)) unlink($path); } else { file_put_contents($path, "hidden\n"); @chmod($path, 0664); } } function input_dir_permalink($dir){ return is_file($dir . '/' . PERMALINK_FILE); } function input_dir_can_permalink($dir, $config){ $state = input_dir_state($dir); $output = basename((string)($state['output'] ?? '')); return $output !== '' && is_file($config['videos_dir'] . '/' . $output) && !input_dir_preview($dir); } function set_input_dir_permalink($dir, $permalink){ $path = $dir . '/' . PERMALINK_FILE; if ($permalink) { file_put_contents($path, "permalink\n"); @chmod($path, 0664); } elseif (is_file($path)) unlink($path); } function input_dir_map_hidden($dir){ return is_file($dir . '/' . MAP_HIDDEN_FILE); } function set_input_dir_map_hidden($dir, $hidden){ $path = $dir . '/' . MAP_HIDDEN_FILE; if ($hidden) { file_put_contents($path, "hide-map\n"); @chmod($path, 0664); } elseif (is_file($path)) unlink($path); } function is_image_media_file($name){ $ext = strtolower(pathinfo((string)$name, PATHINFO_EXTENSION)); return in_array($ext, ['jpg','jpeg','png','webp','gif'], true); } function public_url_path($path){ $path = str_replace('\\', '/', trim((string)$path)); if ($path === '') return ''; $parts = array_values(array_filter(explode('/', $path), static fn($p) => $p !== '')); return implode('/', array_map('rawurlencode', $parts)); } function video_thumb_public_url($config, $videoName){ $videoName = basename((string)$videoName); if ($videoName === '') return ''; $info = video_thumb_source_info($config, $videoName); $sourcePath = (string)($info['source_path'] ?? ''); if ($sourcePath !== '') { $abs = str_starts_with($sourcePath, '/') ? $sourcePath : __DIR__ . '/' . ltrim($sourcePath, '/'); if (is_file($abs)) return public_url_path($sourcePath) . '?v=' . filemtime($abs); } $sourceFile = basename((string)($info['source_file'] ?? '')); $inputDir = basename((string)($info['input_dir'] ?? '')); if ($sourceFile !== '' && $inputDir !== '') { $rel = 'in-dir/' . $inputDir . '/' . $sourceFile; $abs = __DIR__ . '/' . $rel; if (is_file($abs)) return public_url_path($rel) . '?v=' . filemtime($abs); } return ''; } function video_thumb_source_info($config, $videoName){ $videoName = basename((string)$videoName); if ($videoName === '') return []; $base = pathinfo($videoName, PATHINFO_FILENAME); $file = $config['videos_dir'] . '/' . $base . '_thumb.json'; if (!is_file($file)) return []; $data = json_decode((string)file_get_contents($file), true); return is_array($data) ? $data : []; } function ensure_state_article_id($dir){ $articleId = ensure_article_id($dir); $stateFile = $dir . '/.movmaker-state.json'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; if (!is_array($state)) $state = []; if (($state['article_id'] ?? '') !== $articleId) { $state['article_id'] = $articleId; file_put_contents($stateFile, json_encode($state, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n"); @chmod($stateFile, 0664); } return $articleId; } function input_dir_state($dir){ $stateFile = $dir . '/.movmaker-state.json'; $state = is_file($stateFile) ? json_decode((string)file_get_contents($stateFile), true) : []; if (!is_array($state)) $state = []; $state['article_id'] = ($state['article_id'] ?? '') ?: read_article_id($dir); return $state; } function active_worker_jobs($config){ $jobs = []; foreach (input_dirs($config['uploads_dir']) as $dir) { $name = basename($dir); $state = input_dir_state($dir); $articleId = (string)($state['article_id'] ?? '') ?: ensure_article_id($dir); $status = (string)($state['status'] ?? ''); $locked = is_dir($dir . '/.movmaker-lock'); if ($locked || $status === 'processing') { $started = ''; if (is_file($dir . '/.movmaker-lock/started_at')) $started = trim((string)file_get_contents($dir . '/.movmaker-lock/started_at')); if ($started === '' && !empty($state)) $started = (string)($state['started_at'] ?? $state['updated_at'] ?? ''); $jobs[] = ['name'=>$name,'id'=>$articleId,'status'=>$status ?: ($locked ? 'processing' : 'unknown'),'started_at'=>$started]; } } return $jobs; } function format_job_age($startedAt){ $ts = strtotime((string)$startedAt); if (!$ts) return ''; $seconds = max(0, time() - $ts); $h = intdiv($seconds, 3600); $m = intdiv($seconds % 3600, 60); $s = $seconds % 60; if ($h > 0) return $h . 'h ' . $m . 'm'; if ($m > 0) return $m . 'm ' . $s . 's'; return $s . 's'; } function input_dir_output($dir){ $state = input_dir_state($dir); return basename((string)($state['output'] ?? '')); } function regenerate_input_dir_maps($dir, $config){ $logFile = '/var/log/mvlog_map.log'; $python = trim((string)shell_exec('command -v python3 2>/dev/null')); $script = __DIR__ . '/bin/gpsmap.py'; if ($python === '' || !is_file($script)) { file_put_contents($logFile, '[mvlog] map regenerate skipped for ' . basename((string)$dir) . ' (python=' . ($python !== '' ? 'yes' : 'no') . ', script=' . (is_file($script) ? 'yes' : 'no') . ")\n", FILE_APPEND); return; } $state = input_dir_state($dir); $targets = []; foreach (['output', 'preview_output'] as $key) { $name = basename((string)($state[$key] ?? '')); if ($name === '') continue; $targets[] = $config['videos_dir'] . '/' . pathinfo($name, PATHINFO_FILENAME) . '_map.png'; } // Fallback target if no output filename is known yet. if (!$targets) { $targets[] = $config['videos_dir'] . '/' . basename((string)$dir) . '_map.png'; } $targets = array_values(array_unique($targets)); file_put_contents($logFile, '[mvlog] map regenerate requested for ' . basename((string)$dir) . ' targets=' . implode(', ', array_map('basename', $targets)) . "\n", FILE_APPEND); foreach ($targets as $outPng) { file_put_contents($logFile, '[mvlog] map regenerate start for ' . basename((string)$dir) . ' target=' . basename((string)$outPng) . "\n", FILE_APPEND); $cmd = escapeshellarg($python) . ' ' . escapeshellarg($script) . ' ' . escapeshellarg($dir) . ' ' . escapeshellarg($outPng) . ' 2>&1'; $output = (string)shell_exec($cmd); if (is_file($outPng)) { @chmod($outPng, 0664); clearstatcache(true, $outPng); $size = @filesize($outPng); file_put_contents($logFile, '[mvlog] map regenerate done for ' . basename((string)$dir) . ' target=' . basename((string)$outPng) . ' bytes=' . ($size !== false ? $size : 'unknown') . "\n", FILE_APPEND); } else { file_put_contents($logFile, '[mvlog] map regenerate failed for ' . basename((string)$dir) . ' target=' . basename((string)$outPng) . ' output=' . trim($output) . "\n", FILE_APPEND); } } } function cached_video_metadata($output, $fallback){ $meta = [ 'title'=>$fallback['title'] ?? '', 'teaser'=>$fallback['teaser'] ?? '', 'date'=>$fallback['date'] ?? '', 'sort_date'=>$fallback['date'] ?? '', 'location'=>$fallback['place'] ?? '', 'quote_da'=>$fallback['quote_da'] ?? '', 'description'=>$fallback['description'] ?? '' ]; $cacheFile = __DIR__ . '/cache/videos.json'; $cache = is_file($cacheFile) ? json_decode((string)file_get_contents($cacheFile), true) : []; $cached = $cache['items'][$output]['metadata'] ?? null; if (is_array($cached)) { foreach (['title','teaser','date','sort_date','location','quote_da','description'] as $k) if (!empty($cached[$k])) $meta[$k] = $cached[$k]; } return $meta; } function paginate($items, $page, $perPage = 8){ $total = count($items); $pages = max(1, (int)ceil($total / $perPage)); $page = max(1, min((int)$page, $pages)); return [$page, $pages, array_slice($items, ($page - 1) * $perPage, $perPage), $total]; } function page_links($tab, $page, $pages){ if ($pages <= 1) return ''; $extra = ''; if ($tab === 'edit') { $q = trim((string)($_GET['q'] ?? '')); if ($q !== '') $extra .= '&q=' . rawurlencode($q); if (!empty($_GET['f_preview'])) $extra .= '&f_preview=1'; if (!empty($_GET['f_render'])) $extra .= '&f_render=1'; if (!empty($_GET['f_shown'])) $extra .= '&f_shown=1'; } $html = '
No orphan videos.
=page_links('videos',$videoPage,$videoPages)?>No input directories yet.
Thumb source: =h((string)$thumbMeta['source_file'])?>
=nl2br(h($meta['description']), false)?>
“=h(trim((string)$meta['quote_da'], "\"“”"))?>”
=h(implode(' · ', $detailParts))?>
in-dir/=h(basename($editDir))?>
in-dir/=h(basename($editDir))?>