mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 21:04:33 +00:00
Merge pull request #39 from Terranom674/fix/09717-remove-missing-webdav-images
0.9.7.17: entfernte Freigaben sicher aus Piwigo bereinigen
This commit is contained in:
@@ -28,18 +28,34 @@ function bratonien_tools_nc_connector_webdav_site_id(array $connection)
|
|||||||
return (int)$row['id'];
|
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)
|
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');
|
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();
|
$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(
|
||||||
$result = pwg_query($query);
|
"SELECT id, path, representative_ext FROM ".IMAGES_TABLE.
|
||||||
|
" WHERE path LIKE '".$escaped."%' ESCAPE '\\\\'"
|
||||||
|
);
|
||||||
while ($row = pwg_db_fetch_assoc($result))
|
while ($row = pwg_db_fetch_assoc($result))
|
||||||
{
|
{
|
||||||
|
if (strpos((string)$row['path'], $db_prefix) !== 0) continue;
|
||||||
$row['id'] = (int)$row['id'];
|
$row['id'] = (int)$row['id'];
|
||||||
$image_rows[$row['id']] = $row;
|
$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)
|
if ($image_rows)
|
||||||
{
|
{
|
||||||
$ids = array_keys($image_rows);
|
delete_elements(array_map('intval', array_keys($image_rows)), false);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($site_id > 0)
|
||||||
|
{
|
||||||
|
delete_site($site_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidate_user_cache(true);
|
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);
|
@file_put_contents($status_dir.'/deleted-'.$id, date('c')."\n", LOCK_EX);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((int)$cleanup['site_id'] > 0)
|
|
||||||
{
|
|
||||||
return array(
|
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.',
|
'message'=>'Verbindung wurde gelöscht. '.(int)$cleanup['images'].' eindeutig zu dieser Verbindung gehörende 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.',
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
Plugin Name: Bratonien Tools
|
Plugin Name: Bratonien Tools
|
||||||
Version: 0.9.7.16
|
Version: 0.9.7.17
|
||||||
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
||||||
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
||||||
Author: Bratonien
|
Author: Bratonien
|
||||||
|
|||||||
@@ -56,6 +56,15 @@ normalize_connector_cache_permissions() {
|
|||||||
# werden zumindest alle eigenen Dateien gruppenschreibbar gehalten.
|
# werden zumindest alle eigenen Dateien gruppenschreibbar gehalten.
|
||||||
normalize_connector_cache_permissions
|
normalize_connector_cache_permissions
|
||||||
|
|
||||||
|
# Dieser Medienlauf wird erst nach einem erfolgreichen WebDAV-Aufbau und einer
|
||||||
|
# erfolgreichen Piwigo-Synchronisierung gestartet. Deshalb darf erst hier aus
|
||||||
|
# "lokale Connector-Datei fehlt" auf eine in Nextcloud entfernte Freigabe/Datei
|
||||||
|
# geschlossen werden. Bei einer nicht erreichbaren Verbindung wird dieser Block
|
||||||
|
# niemals ausgeführt und der vorhandene Piwigo-Bestand bleibt unverändert.
|
||||||
|
php "$SCRIPT_DIR/cleanup-missing-webdav-images.php" \
|
||||||
|
--piwigo-root="$PIWIGO_ROOT" \
|
||||||
|
--connection-id="$CONNECTION_ID"
|
||||||
|
|
||||||
php "$SCRIPT_DIR/lib/precache-webdav-previews.php" \
|
php "$SCRIPT_DIR/lib/precache-webdav-previews.php" \
|
||||||
--mapping="$WEBDAV_MAPPING_FILE" \
|
--mapping="$WEBDAV_MAPPING_FILE" \
|
||||||
--base-url="$WEBDAV_BASE_URL" \
|
--base-url="$WEBDAV_BASE_URL" \
|
||||||
|
|||||||
124
runtime/cleanup-missing-webdav-images.php
Normal file
124
runtime/cleanup-missing-webdav-images.php
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
if (PHP_SAPI !== 'cli')
|
||||||
|
{
|
||||||
|
fwrite(STDERR, "CLI only\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$options = getopt('', array('piwigo-root:', 'connection-id:'));
|
||||||
|
$piwigo_root = rtrim((string)($options['piwigo-root'] ?? ''), '/');
|
||||||
|
$connection_id = (int)($options['connection-id'] ?? 0);
|
||||||
|
if ($piwigo_root === '' || $connection_id < 1)
|
||||||
|
{
|
||||||
|
fwrite(STDERR, "Parameter --piwigo-root und --connection-id werden benoetigt.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$db_config = $piwigo_root.'/local/config/database.inc.php';
|
||||||
|
if (!is_readable($db_config))
|
||||||
|
{
|
||||||
|
fwrite(STDERR, "Piwigo-Datenbankkonfiguration ist nicht lesbar.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$conf = array();
|
||||||
|
$prefixeTable = 'piwigo_';
|
||||||
|
require $db_config;
|
||||||
|
foreach (array('db_host','db_user','db_password','db_base') as $key)
|
||||||
|
{
|
||||||
|
if (!isset($conf[$key]))
|
||||||
|
{
|
||||||
|
fwrite(STDERR, "Piwigo-Datenbankkonfiguration ist unvollstaendig: $key\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$db = new mysqli($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_base']);
|
||||||
|
if ($db->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";
|
||||||
Reference in New Issue
Block a user