From 4c73c43c60134bf2c46ae6c48861e428108f654a Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Thu, 3 Sep 2026 12:48:10 +0200 Subject: [PATCH] Add manual warmup controls and read-only path audit --- include/webdav_warmup_settings.inc.php | 79 ++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/include/webdav_warmup_settings.inc.php b/include/webdav_warmup_settings.inc.php index 6e21905..5d29c2c 100644 --- a/include/webdav_warmup_settings.inc.php +++ b/include/webdav_warmup_settings.inc.php @@ -15,7 +15,6 @@ function bratonien_tools_get_webdav_warmup_settings() 'enabled'=>false, 'batch_size'=>10, 'periodic_hours'=>12, - 'piwigo_base_url'=>'http://127.0.0.1', ); $file = bratonien_tools_webdav_warmup_settings_file(); @@ -28,7 +27,6 @@ function bratonien_tools_get_webdav_warmup_settings() if (array_key_exists('enabled', $saved)) $settings['enabled'] = (bool)$saved['enabled']; if (isset($saved['batch_size'])) $settings['batch_size'] = max(1, min(50, (int)$saved['batch_size'])); if (isset($saved['periodic_hours'])) $settings['periodic_hours'] = max(1, min(168, (int)$saved['periodic_hours'])); - if (!empty($saved['piwigo_base_url'])) $settings['piwigo_base_url'] = rtrim((string)$saved['piwigo_base_url'], '/'); } } return $settings; @@ -47,7 +45,6 @@ function bratonien_tools_save_webdav_warmup_settings() 'enabled'=>$enabled, 'batch_size'=>$batch_size, 'periodic_hours'=>$periodic_hours, - 'piwigo_base_url'=>(string)$current['piwigo_base_url'], ); $json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); if ($json === false) throw new RuntimeException('Warmup-Einstellungen konnten nicht serialisiert werden.'); @@ -72,8 +69,82 @@ function bratonien_tools_save_webdav_warmup_settings() return array('message'=>sprintf( 'WebDAV-Cache-Warmup gespeichert: %s, %d Bilder pro Batch, Eingangsprüfung alle %d Stunden.', - $enabled ? 'aktiv' : 'inaktiv', + $enabled ? 'automatisch aktiv' : 'Automatik deaktiviert', $batch_size, $periodic_hours )); } + +function bratonien_tools_webdav_warmup_php_cli() +{ + if (function_exists('bratonien_tools_find_php_cli')) + { + $php = bratonien_tools_find_php_cli(); + if ($php) return $php; + } + foreach (array('/usr/bin/php', '/usr/bin/php8.4', '/usr/bin/php8.3', '/usr/local/bin/php') as $candidate) + { + if (is_file($candidate) && is_executable($candidate)) return $candidate; + } + return null; +} + +function bratonien_tools_start_webdav_warmup_manual() +{ + if (!function_exists('exec')) throw new RuntimeException('PHP exec() ist deaktiviert; Warmup kann nicht gestartet werden.'); + $php = bratonien_tools_webdav_warmup_php_cli(); + if (!$php) throw new RuntimeException('PHP-CLI wurde für den WebDAV-Warmup nicht gefunden.'); + $dispatcher = realpath(BRATONIEN_TOOLS_PATH.'runtime/webdav-warmup-dispatch.php'); + if (!$dispatcher || !is_file($dispatcher)) throw new RuntimeException('WebDAV-Warmup-Dispatcher wurde nicht gefunden.'); + + $log = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'bratonien-webdav-warmup-dispatch.log'; + $command = 'nohup '.escapeshellarg($php).' '.escapeshellarg($dispatcher).' --mode=manual >> '.escapeshellarg($log).' 2>&1 < /dev/null & echo $!'; + $output = array(); + $exit = 1; + @exec($command, $output, $exit); + $pid = isset($output[0]) ? (int)$output[0] : 0; + if ($exit !== 0 || $pid <= 0) throw new RuntimeException('Manuelle Warmup-Prüfung konnte nicht gestartet werden.'); + + return array('message'=>'WebDAV-Warmup: Prüfung auf neue oder geänderte Bilder wurde gestartet.'); +} + +function bratonien_tools_run_webdav_warmup_audit() +{ + if (!function_exists('exec')) throw new RuntimeException('PHP exec() ist deaktiviert; Pfadaudit kann nicht gestartet werden.'); + $php = bratonien_tools_webdav_warmup_php_cli(); + if (!$php) throw new RuntimeException('PHP-CLI wurde für den Pfadaudit nicht gefunden.'); + $audit = realpath(BRATONIEN_TOOLS_PATH.'runtime/lib/webdav-warmup-audit.php'); + if (!$audit || !is_file($audit)) throw new RuntimeException('WebDAV-Warmup-Pfadaudit wurde nicht gefunden.'); + + $output = array(); + $exit = 1; + @exec(escapeshellarg($php).' '.escapeshellarg($audit).' 2>&1', $output, $exit); + $text = trim(implode(' | ', array_slice($output, -8))); + if ($exit !== 0) + { + throw new RuntimeException('WebDAV-Pfadaudit meldet einen unsicheren Zustand'.($text !== '' ? ': '.$text : '.')); + } + return array('message'=>'WebDAV-Pfadaudit erfolgreich'.($text !== '' ? ': '.$text : '.')); +} + +function bratonien_tools_get_webdav_warmup_status() +{ + $status = array( + 'state'=>'idle', + 'message'=>'Noch kein Warmup-Lauf protokolliert.', + 'updated_at'=>0, + ); + $files = glob(PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'bratonien-webdav-warmup.status-*.json'); + if (!$files) return $status; + + $latest = null; + foreach ($files as $file) + { + if (!is_readable($file)) continue; + $raw = @file_get_contents($file); + $data = $raw !== false ? json_decode($raw, true) : null; + if (!is_array($data)) continue; + if ($latest === null || (int)($data['updated_at'] ?? 0) > (int)($latest['updated_at'] ?? 0)) $latest = $data; + } + return is_array($latest) ? array_merge($status, $latest) : $status; +}