From 15a3ceb01c503610803b6fd2b3e554ae62aca5c1 Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Wed, 19 Aug 2026 13:44:26 +0200 Subject: [PATCH] 0.9.6.10 complete legacy edit credentials and migration readiness --- include/nc_connector_edit.inc.php | 169 +++++++++++++++++++++++++++--- 1 file changed, 156 insertions(+), 13 deletions(-) diff --git a/include/nc_connector_edit.inc.php b/include/nc_connector_edit.inc.php index 673bb09..4bacb04 100644 --- a/include/nc_connector_edit.inc.php +++ b/include/nc_connector_edit.inc.php @@ -4,6 +4,93 @@ if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); } +function bratonien_tools_nc_connector_migration_state(array $connection) +{ + $config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array(); + $credentials = bratonien_tools_nc_connector_scoped_secret($connection); + $missing = array(); + + if (trim((string)($config['nextcloud_url'] ?? '')) === '') $missing[] = 'Nextcloud-Adresse'; + if (trim((string)($credentials['nextcloud_user'] ?? '')) === '') $missing[] = 'Nextcloud-Benutzer'; + if ((string)($credentials['nextcloud_password'] ?? '') === '') $missing[] = 'Nextcloud-Passwort'; + if (trim((string)($credentials['api_key_id'] ?? '')) === '') $missing[] = 'Piwigo-API-Schluessel-ID'; + if (trim((string)($credentials['api_key_secret'] ?? '')) === '') $missing[] = 'Piwigo-API-Geheimnis'; + + return array( + 'ready'=>!$missing, + 'missing'=>$missing, + ); +} + +function bratonien_tools_nc_connector_validate_nextcloud_access($base_url, $username, $password) +{ + $base_url = bratonien_tools_nc_wizard_normalize_url($base_url); + $username = trim((string)$username); + $password = (string)$password; + if ($username === '' || $password === '') throw new RuntimeException('Nextcloud-Benutzer und Passwort werden benoetigt.'); + + $status_response = bratonien_tools_nc_wizard_http($base_url.'/status.php'); + if ($status_response['status'] < 200 || $status_response['status'] >= 300) + { + throw new RuntimeException('Die Nextcloud-Adresse konnte nicht bestaetigt werden.'); + } + $status = json_decode((string)$status_response['body'], true); + if (!is_array($status) || empty($status['installed'])) + { + throw new RuntimeException('Unter dieser Adresse wurde keine installierte Nextcloud erkannt.'); + } + + $user_response = bratonien_tools_nc_wizard_http( + $base_url.'/ocs/v2.php/cloud/user?format=json', + $username, + $password, + array('OCS-APIRequest: true') + ); + if ($user_response['status'] === 401 || $user_response['status'] === 403) + { + throw new RuntimeException('Nextcloud hat Benutzername oder Passwort abgelehnt.'); + } + if ($user_response['status'] < 200 || $user_response['status'] >= 300) + { + throw new RuntimeException('Nextcloud ist erreichbar, aber der Benutzerzugang konnte nicht geprueft werden.'); + } + $user_data = bratonien_tools_nc_wizard_ocs_data($user_response['body']); + $resolved = trim((string)($user_data['id'] ?? $username)); + + return array( + 'base_url'=>$base_url, + 'username'=>$resolved !== '' ? $resolved : $username, + ); +} + +function bratonien_tools_nc_connector_validate_scoped_api($api_key_id, $api_key_secret) +{ + $api_key_id = trim((string)$api_key_id); + $api_key_secret = trim((string)$api_key_secret); + if ($api_key_id === '' || $api_key_secret === '') + { + throw new RuntimeException('Piwigo-API-Schluessel-ID und API-Geheimnis werden benoetigt.'); + } + + $status = bratonien_tools_nc_connector_piwigo_api_request($api_key_id, $api_key_secret, 'pwg.session.getStatus'); + if (!is_array($status)) throw new RuntimeException('Piwigo hat keinen auswertbaren API-Benutzerstatus geliefert.'); + $role = strtolower(trim((string)($status['status'] ?? ''))); + if (!in_array($role, array('admin','webmaster'), true)) + { + throw new RuntimeException('Der API-Key funktioniert, gehoert aber keinem Piwigo-Administrator/Webmaster.'); + } + + $method_result = bratonien_tools_nc_connector_piwigo_api_request($api_key_id, $api_key_secret, 'reflection.getMethodList'); + $method_map = array(); + bratonien_tools_nc_connector_collect_method_names($method_result, $method_map); + $required = array('bratonien.nc.syncProductive', 'bratonien.nc.syncOrphans'); + $missing = array_values(array_diff($required, array_keys($method_map))); + if ($missing) + { + throw new RuntimeException('Der API-Key ist gueltig, aber benoetigte Bratonien-Sync-Methoden fehlen: '.implode(', ', $missing).'.'); + } +} + function bratonien_tools_nc_connector_prepare_webdav_wizard_from_connection(array $connection, $mode) { $id = (int)$connection['id']; @@ -101,7 +188,7 @@ function bratonien_tools_nc_connector_edit_start() } bratonien_tools_nc_connector_prepare_webdav_wizard_from_connection($connection, 'update'); - return array('message'=>'Verbindung #'.$id.' wurde zum Bearbeiten geöffnet.'); + return array('message'=>'Verbindung #'.$id.' wurde zum Bearbeiten geoeffnet.'); } function bratonien_tools_nc_connector_migrate_start() @@ -114,8 +201,14 @@ function bratonien_tools_nc_connector_migrate_start() throw new RuntimeException('Nur eine Legacy-Verbindung kann auf WebDAV migriert werden.'); } + $migration = bratonien_tools_nc_connector_migration_state($connection); + if (empty($migration['ready'])) + { + throw new RuntimeException('Die WebDAV-Migration ist noch nicht bereit. Unter Bearbeiten fehlen: '.implode(', ', $migration['missing']).'.'); + } + 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.'); + return array('message'=>'Die WebDAV-Migration fuer Verbindung #'.$id.' wurde geoeffnet. Die bestehende Legacy-Verbindung bleibt bis zum erfolgreichen Umstieg unveraendert.'); } function bratonien_tools_nc_connector_update_local_friendly() @@ -123,10 +216,10 @@ 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.'); + if ((string)$connection['adapter'] !== 'local') throw new RuntimeException('Diese Bearbeitung ist nur fuer Legacy-Verbindungen vorgesehen.'); $name = trim((string)($_POST['connection_name'] ?? '')); - if ($name === '') throw new RuntimeException('Bitte einen Namen für die Verbindung angeben.'); + if ($name === '') throw new RuntimeException('Bitte einen Namen fuer die Verbindung angeben.'); $config = isset($connection['config']) && is_array($connection['config']) ? $connection['config'] : array(); $host = trim((string)($_POST['nc_host'] ?? '')); @@ -138,11 +231,11 @@ function bratonien_tools_nc_connector_update_local_friendly() $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 ($port < 1 || $port > 65535) throw new RuntimeException('Der Datenbank-Port ist ungueltig.'); 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.'); + if ($source_view === '' || $activity_view === '') throw new RuntimeException('Die gespeicherten Datenbankansichten duerfen nicht leer sein.'); bratonien_tools_nc_connector_view_name($source_view); bratonien_tools_nc_connector_view_name($activity_view); @@ -158,11 +251,54 @@ function bratonien_tools_nc_connector_update_local_friendly() $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.'); + if ($mount === '' || $mount[0] !== '/') throw new RuntimeException('Bei einem Speicherort fehlt ein gueltiger 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.'); + $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('Fuer die Legacy-Verbindung ist kein Datenbankpasswort gespeichert.'); + + $nextcloud_url = trim((string)($_POST['nc_nextcloud_url'] ?? ($config['nextcloud_url'] ?? ''))); + $nextcloud_user = trim((string)($_POST['nc_nextcloud_user'] ?? ($credentials['nextcloud_user'] ?? ''))); + $new_nextcloud_password = (string)($_POST['nc_nextcloud_password'] ?? ''); + if ($new_nextcloud_password !== '') $credentials['nextcloud_password'] = $new_nextcloud_password; + $nextcloud_password = (string)($credentials['nextcloud_password'] ?? ''); + + $api_key_id = trim((string)($_POST['nc_connection_api_key_id'] ?? ($credentials['api_key_id'] ?? ''))); + $new_api_secret = trim((string)($_POST['nc_connection_api_key_secret'] ?? '')); + $old_api_id = trim((string)($credentials['api_key_id'] ?? '')); + if ($api_key_id !== $old_api_id && $new_api_secret === '') + { + throw new RuntimeException('Wenn die API-Schluessel-ID geaendert wird, muss auch das API-Geheimnis neu eingegeben werden.'); + } + if ($new_api_secret !== '') $credentials['api_key_secret'] = $new_api_secret; + $api_key_secret = trim((string)($credentials['api_key_secret'] ?? '')); + + if (($nextcloud_url === '' || $nextcloud_user === '' || $nextcloud_password === '') && ($nextcloud_url !== '' || $nextcloud_user !== '' || $nextcloud_password !== '')) + { + throw new RuntimeException('Nextcloud-Adresse, Nextcloud-Benutzer und Nextcloud-Passwort muessen fuer WebDAV gemeinsam vollstaendig sein.'); + } + if (($api_key_id === '') !== ($api_key_secret === '')) + { + throw new RuntimeException('Piwigo-API-Schluessel-ID und API-Geheimnis muessen gemeinsam vollstaendig sein.'); + } + + if ($nextcloud_url !== '') + { + $validated_nc = bratonien_tools_nc_connector_validate_nextcloud_access($nextcloud_url, $nextcloud_user, $nextcloud_password); + $nextcloud_url = $validated_nc['base_url']; + $nextcloud_user = $validated_nc['username']; + $credentials['nextcloud_user'] = $nextcloud_user; + } + if ($api_key_id !== '') + { + bratonien_tools_nc_connector_validate_scoped_api($api_key_id, $api_key_secret); + $credentials['api_key_id'] = $api_key_id; + } + $config['host'] = $host; $config['port'] = (string)$port; $config['database'] = $database; @@ -174,13 +310,16 @@ function bratonien_tools_nc_connector_update_local_friendly() $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; + if ($nextcloud_url !== '') + { + $config['nextcloud_url'] = $nextcloud_url; + $config['access_user'] = $nextcloud_user; + $config['nextcloud_access_user'] = $nextcloud_user; + } + $config['piwigo_auth'] = 'connection-scoped'; + $config['api_enabled'] = $api_key_id !== '' && $api_key_secret !== ''; 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); @@ -191,5 +330,9 @@ function bratonien_tools_nc_connector_update_local_friendly() $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.'); + $updated = bratonien_tools_nc_connector_connection($id, true); + $migration = $updated ? bratonien_tools_nc_connector_migration_state($updated) : array('ready'=>false); + return array( + 'message'=>'Verbindung #'.$id.' wurde gespeichert. '.(!empty($migration['ready']) ? 'Die WebDAV-Migration ist jetzt bereit.' : 'Die WebDAV-Migration ist noch nicht vollstaendig vorbereitet.'), + ); }