diff --git a/include/nc_connector_delete_safe.inc.php b/include/nc_connector_delete_safe.inc.php index 3353ab7..5e4fe91 100644 --- a/include/nc_connector_delete_safe.inc.php +++ b/include/nc_connector_delete_safe.inc.php @@ -28,18 +28,34 @@ function bratonien_tools_nc_connector_webdav_site_id(array $connection) return (int)$row['id']; } +function bratonien_tools_nc_connector_gallery_db_prefix(array $connection) +{ + $config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array(); + $gallery_root = rtrim((string)($config['parallel_gallery_root'] ?? ''), '/'); + $piwigo_root = rtrim(PHPWG_ROOT_PATH, '/'); + if ($gallery_root === '' || strpos($gallery_root, $piwigo_root.'/') !== 0) return ''; + + $relative = ltrim(substr($gallery_root, strlen($piwigo_root)), '/'); + return $relative === '' ? '' : './'.rtrim($relative, '/').'/'; +} + function bratonien_tools_nc_connector_remove_webdav_piwigo_content(array $connection) { - $site_id = bratonien_tools_nc_connector_webdav_site_id($connection); - if ($site_id < 1) return array('site_id'=>0, 'images'=>0); - include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); + $site_id = bratonien_tools_nc_connector_webdav_site_id($connection); + $db_prefix = bratonien_tools_nc_connector_gallery_db_prefix($connection); + if ($db_prefix === '') return array('site_id'=>$site_id, 'images'=>0); + + $escaped = pwg_db_real_escape_string(addcslashes($db_prefix, '_%\\')); $image_rows = array(); - $query = '\nSELECT DISTINCT i.id, i.path, i.representative_ext\n FROM '.IMAGES_TABLE.' AS i\n LEFT JOIN '.CATEGORIES_TABLE.' AS sc ON sc.id = i.storage_category_id\n LEFT JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id\n LEFT JOIN '.CATEGORIES_TABLE.' AS vc ON vc.id = ic.category_id\n WHERE sc.site_id = '.$site_id.' OR vc.site_id = '.$site_id.'\n;'; - $result = pwg_query($query); + $result = pwg_query( + "SELECT id, path, representative_ext FROM ".IMAGES_TABLE. + " WHERE path LIKE '".$escaped."%' ESCAPE '\\\\'" + ); while ($row = pwg_db_fetch_assoc($result)) { + if (strpos((string)$row['path'], $db_prefix) !== 0) continue; $row['id'] = (int)$row['id']; $image_rows[$row['id']] = $row; } @@ -56,20 +72,14 @@ function bratonien_tools_nc_connector_remove_webdav_piwigo_content(array $connec } } - delete_site($site_id); - if ($image_rows) { - $ids = array_keys($image_rows); - $remaining = query2array( - 'SELECT id FROM '.IMAGES_TABLE.' WHERE id IN ('.implode(',', array_map('intval', $ids)).')', - null, - 'id' - ); - if ($remaining) - { - delete_elements(array_map('intval', $remaining), false); - } + delete_elements(array_map('intval', array_keys($image_rows)), false); + } + + if ($site_id > 0) + { + delete_site($site_id); } invalidate_user_cache(true); @@ -110,14 +120,7 @@ function bratonien_tools_nc_connector_delete_safe() @file_put_contents($status_dir.'/deleted-'.$id, date('c')."\n", LOCK_EX); } - if ((int)$cleanup['site_id'] > 0) - { - return array( - 'message'=>'Verbindung wurde gelöscht. Die zugehörigen Piwigo-Alben und '.(int)$cleanup['images'].' Bilder wurden aus Piwigo entfernt. Nextcloud-Dateien blieben unverändert.', - ); - } - return array( - 'message'=>'Verbindung wurde gelöscht. Verbliebene Laufzeitdaten werden automatisch bereinigt. Quelldateien blieben unverändert.', + 'message'=>'Verbindung wurde gelöscht. '.(int)$cleanup['images'].' eindeutig zu dieser Verbindung gehörende Bilder wurden aus Piwigo entfernt. Nextcloud-Dateien blieben unverändert.', ); } diff --git a/main.inc.php b/main.inc.php index af94367..260baa5 100644 --- a/main.inc.php +++ b/main.inc.php @@ -1,7 +1,7 @@ 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";