From 039fb5160cd08e15074c84a78455a44e0c4074f5 Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Wed, 19 Aug 2026 13:31:19 +0200 Subject: [PATCH] 0.9.6.9 separate edit and migration flows --- include/nc_connector_edit.inc.php | 136 ++++++++++++++++++++++++------ 1 file changed, 111 insertions(+), 25 deletions(-) diff --git a/include/nc_connector_edit.inc.php b/include/nc_connector_edit.inc.php index 06cdb62..673bb09 100644 --- a/include/nc_connector_edit.inc.php +++ b/include/nc_connector_edit.inc.php @@ -4,22 +4,9 @@ if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); } -/** - * Start the normal WebDAV wizard with the values of an existing connection. - * - * Remote/WebDAV connections are edited in place. A legacy/local connection is - * intentionally not mutated: the wizard prepares its WebDAV successor while - * the existing connection remains available as fallback. - */ -function bratonien_tools_nc_connector_edit_start() +function bratonien_tools_nc_connector_prepare_webdav_wizard_from_connection(array $connection, $mode) { - $id = (int)($_POST['connection_id'] ?? 0); - $connection = bratonien_tools_nc_connector_connection($id, true); - if (!$connection) - { - throw new RuntimeException('Connector-Verbindung wurde nicht gefunden.'); - } - + $id = (int)$connection['id']; $config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array(); $credentials = bratonien_tools_nc_connector_scoped_secret($connection); @@ -43,14 +30,11 @@ function bratonien_tools_nc_connector_edit_start() $selected_ids[$path] = $fileid; } - $is_remote = (string)$connection['adapter'] === 'remote' - && (string)($config['source_mode'] ?? '') === 'webdav-placeholder'; - $state = bratonien_tools_nc_wizard_state(); $state = array_merge($state, array( 'editing_connection_id'=>$id, 'editing_adapter'=>(string)$connection['adapter'], - 'editing_mode'=>$is_remote ? 'update' : 'migrate', + 'editing_mode'=>(string)$mode, 'connection_name'=>(string)$connection['name'], 'host_input'=>$base_url, 'base_url'=>$base_url, @@ -100,10 +84,112 @@ function bratonien_tools_nc_connector_edit_start() } bratonien_tools_nc_wizard_store($state); - - return array( - 'message'=>$is_remote - ? 'Verbindung #'.$id.' wurde zum Bearbeiten geöffnet.' - : 'Verbindung #'.$id.' wird im Assistenten auf WebDAV vorbereitet. Die bestehende Legacy-Verbindung bleibt dabei als Fallback erhalten.', - ); +} + +function bratonien_tools_nc_connector_edit_start() +{ + $id = (int)($_POST['connection_id'] ?? 0); + $connection = bratonien_tools_nc_connector_connection($id, true); + if (!$connection) throw new RuntimeException('Connector-Verbindung wurde nicht gefunden.'); + + $config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array(); + $is_webdav = (string)$connection['adapter'] === 'remote' + && (string)($config['source_mode'] ?? '') === 'webdav-placeholder'; + if (!$is_webdav) + { + throw new RuntimeException('Diese Legacy-Verbindung wird direkt bearbeitet. Eine WebDAV-Migration ist eine separate Aktion.'); + } + + bratonien_tools_nc_connector_prepare_webdav_wizard_from_connection($connection, 'update'); + return array('message'=>'Verbindung #'.$id.' wurde zum Bearbeiten geöffnet.'); +} + +function bratonien_tools_nc_connector_migrate_start() +{ + $id = (int)($_POST['connection_id'] ?? 0); + $connection = bratonien_tools_nc_connector_connection($id, true); + if (!$connection) throw new RuntimeException('Connector-Verbindung wurde nicht gefunden.'); + if ((string)$connection['adapter'] !== 'local') + { + throw new RuntimeException('Nur eine Legacy-Verbindung kann auf WebDAV migriert werden.'); + } + + bratonien_tools_nc_connector_prepare_webdav_wizard_from_connection($connection, 'migrate'); + return array('message'=>'Die WebDAV-Migration für Verbindung #'.$id.' wurde geöffnet. Die bestehende Legacy-Verbindung bleibt bis zum erfolgreichen Umstieg unverändert.'); +} + +function bratonien_tools_nc_connector_update_local_friendly() +{ + $id = (int)($_POST['connection_id'] ?? 0); + $connection = bratonien_tools_nc_connector_connection($id, true); + if (!$connection) throw new RuntimeException('Connector-Verbindung wurde nicht gefunden.'); + if ((string)$connection['adapter'] !== 'local') throw new RuntimeException('Diese Bearbeitung ist nur für Legacy-Verbindungen vorgesehen.'); + + $name = trim((string)($_POST['connection_name'] ?? '')); + if ($name === '') throw new RuntimeException('Bitte einen Namen für die Verbindung angeben.'); + + $config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array(); + $host = trim((string)($_POST['nc_host'] ?? '')); + $port = (int)($_POST['nc_port'] ?? 5432); + $database = trim((string)($_POST['nc_database'] ?? '')); + $user = trim((string)($_POST['nc_user'] ?? '')); + $gallery_root = rtrim(trim((string)($_POST['nc_gallery_root'] ?? '')), '/'); + $source_view = trim((string)($_POST['nc_source_view'] ?? '')); + $activity_view = trim((string)($_POST['nc_activity_view'] ?? '')); + + if ($host === '') throw new RuntimeException('Datenbank-Server fehlt.'); + if ($port < 1 || $port > 65535) throw new RuntimeException('Der Datenbank-Port ist ungültig.'); + if ($database === '') throw new RuntimeException('Datenbankname fehlt.'); + if ($user === '') throw new RuntimeException('Reader-Benutzer fehlt.'); + if ($gallery_root === '' || $gallery_root[0] !== '/') throw new RuntimeException('Der Piwigo-Galerieordner muss ein absoluter Pfad sein.'); + if ($source_view === '' || $activity_view === '') throw new RuntimeException('Die gespeicherten Datenbankansichten dürfen nicht leer sein.'); + bratonien_tools_nc_connector_view_name($source_view); + bratonien_tools_nc_connector_view_name($activity_view); + + $storage_ids = isset($_POST['nc_storage_id']) && is_array($_POST['nc_storage_id']) ? $_POST['nc_storage_id'] : array(); + $storage_prefixes = isset($_POST['nc_source_prefix']) && is_array($_POST['nc_source_prefix']) ? $_POST['nc_source_prefix'] : array(); + $storage_mounts = isset($_POST['nc_local_mount']) && is_array($_POST['nc_local_mount']) ? $_POST['nc_local_mount'] : array(); + $storages = array(); + $count = max(count($storage_ids), count($storage_prefixes), count($storage_mounts)); + for ($index = 0; $index < $count; $index++) + { + $storage_id = trim((string)($storage_ids[$index] ?? '')); + $prefix = trim((string)($storage_prefixes[$index] ?? ''), '/'); + $mount = rtrim(trim((string)($storage_mounts[$index] ?? '')), '/'); + if ($storage_id === '' && $prefix === '' && $mount === '') continue; + if ($storage_id === '') throw new RuntimeException('Bei einem Speicherort fehlt die Storage-ID.'); + if ($mount === '' || $mount[0] !== '/') throw new RuntimeException('Bei einem Speicherort fehlt ein gültiger lokaler Pfad.'); + $storages[] = array('storage_id'=>$storage_id, 'source_prefix'=>$prefix, 'local_mount'=>$mount); + } + if (!$storages) throw new RuntimeException('Mindestens ein Speicherort muss vorhanden sein.'); + + $config['host'] = $host; + $config['port'] = (string)$port; + $config['database'] = $database; + $config['user'] = $user; + $config['source_view'] = $source_view; + $config['activity_view'] = $activity_view; + $config['gallery_root'] = $gallery_root; + $config['quiet_seconds'] = max(0, (int)($_POST['nc_quiet_seconds'] ?? ($config['quiet_seconds'] ?? 120))); + $config['max_wait_seconds'] = max(60, (int)($_POST['nc_max_wait_seconds'] ?? ($config['max_wait_seconds'] ?? 900))); + $config['full_sync_seconds'] = max(300, (int)($_POST['nc_full_sync_seconds'] ?? ($config['full_sync_seconds'] ?? 86400))); + $config['storages'] = $storages; + unset($config['verification']); + + $credentials = bratonien_tools_nc_connector_scoped_secret($connection); + $new_db_password = (string)($_POST['nc_db_password'] ?? ''); + if ($new_db_password !== '') $credentials['db_password'] = $new_db_password; + if (trim((string)($credentials['db_password'] ?? '')) === '') throw new RuntimeException('Für die Legacy-Verbindung ist kein Datenbankpasswort gespeichert.'); + + $config_json = json_encode($config, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + if (!is_string($config_json)) throw new RuntimeException('Connector-Konfiguration konnte nicht serialisiert werden.'); + $secret_payload = json_encode($credentials, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + if (!is_string($secret_payload)) throw new RuntimeException('Connector-Zugangsdaten konnten nicht serialisiert werden.'); + $secret_blob = bratonien_tools_nc_connector_encrypt_secret($secret_payload); + + $table = bratonien_tools_nc_connector_table(); + $now = date('Y-m-d H:i:s'); + pwg_query("UPDATE `$table` SET name='".pwg_db_real_escape_string($name)."', config_json='".pwg_db_real_escape_string($config_json)."', secret_blob='".pwg_db_real_escape_string($secret_blob)."', updated='".pwg_db_real_escape_string($now)."' WHERE id=".$id." LIMIT 1"); + + return array('message'=>'Verbindung #'.$id.' wurde gespeichert. Die neuen Werte gelten ab dem nächsten Connector-Lauf.'); }