#!/usr/bin/env php connect_errno) { fwrite(STDERR, "Piwigo-Datenbank ist nicht erreichbar: ".$db->connect_error."\n"); exit(1); } $db->set_charset('utf8mb4'); $table = $prefixeTable.'bratonien_tools_nc_connections'; $result = $db->query('SELECT config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1'); if (!$result || !$result->num_rows) { fwrite(STDERR, "Connector-Verbindung #$connection_id wurde nicht gefunden.\n"); exit(1); } $config = json_decode((string)$result->fetch_assoc()['config_json'], true); if (!is_array($config) || (string)($config['source_mode'] ?? '') !== 'webdav-placeholder') { echo "Keine WebDAV-Bereinigung erforderlich.\n"; exit(0); } $gallery_root = rtrim((string)($config['parallel_gallery_root'] ?? ''), '/'); if ($gallery_root === '' || strpos($gallery_root, $piwigo_root.'/') !== 0) { fwrite(STDERR, "Verbindungseigene Gallery-Root ist ungueltig.\n"); exit(1); } $relative = ltrim(substr($gallery_root, strlen($piwigo_root)), '/'); $db_prefix = './'.rtrim($relative, '/').'/'; $escaped_prefix = $db->real_escape_string(addcslashes($db_prefix, "_%\\")); $images_table = $prefixeTable.'images'; $query = "SELECT id, path FROM `{$images_table}` WHERE path LIKE '{$escaped_prefix}%' ESCAPE '\\\\'"; $rows = $db->query($query); if (!$rows) { fwrite(STDERR, "Connector-Bilder konnten nicht gelesen werden: ".$db->error."\n"); exit(1); } $missing_ids = array(); while ($row = $rows->fetch_assoc()) { $path = (string)$row['path']; if (strpos($path, $db_prefix) !== 0) { continue; } $absolute = $piwigo_root.'/'.ltrim(preg_replace('#^\\./#', '', $path), '/'); if (!is_file($absolute)) { $missing_ids[] = (int)$row['id']; } } $db->close(); if (!$missing_ids) { echo "WebDAV-Bereinigung: keine fehlenden Piwigo-Bilder.\n"; exit(0); } // Erst jetzt Piwigo bootstrappen. Dieser Prozess wird nur nach einem erfolgreich // abgeschlossenen WebDAV-Abruf gestartet. Eine nicht erreichbare Verbindung kann // daher niemals ueber diesen Pfad Bilder loeschen. define('PHPWG_ROOT_PATH', $piwigo_root.'/'); $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_NAME'] = 'localhost'; $_SERVER['HTTP_HOST'] = 'localhost'; $_SERVER['SERVER_PORT'] = '80'; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/'; $_SERVER['SCRIPT_NAME'] = '/plugins/bratonien_tools/runtime/cleanup-missing-webdav-images.php'; $_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME']; $_SERVER['QUERY_STRING'] = ''; $_SERVER['HTTPS'] = 'off'; require_once(PHPWG_ROOT_PATH.'include/common.inc.php'); include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); delete_elements($missing_ids, false); update_category('all'); invalidate_user_cache(true); echo 'WebDAV-Bereinigung: '.count($missing_ids)." nicht mehr freigegebene/ vorhandene Bilder aus Piwigo entfernt.\n";