From 81a28a92823a872be879baac08d124b3fe9690df Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Tue, 18 Aug 2026 16:09:31 +0200 Subject: [PATCH] Fallback-Aenderungen erhalten verbindungseigene API --- include/nc_connector_connection_scope.inc.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/include/nc_connector_connection_scope.inc.php b/include/nc_connector_connection_scope.inc.php index e2a4ab1..33e46a2 100644 --- a/include/nc_connector_connection_scope.inc.php +++ b/include/nc_connector_connection_scope.inc.php @@ -140,3 +140,77 @@ function bratonien_tools_nc_connector_verify_connection_scoped() pwg_query("UPDATE `$table` SET config_json='".pwg_db_real_escape_string($config_json)."', secret_blob='".pwg_db_real_escape_string($blob)."' WHERE id=".$id." LIMIT 1"); return $result; } + +function bratonien_tools_nc_connector_scoped_secret(array $connection) +{ + $plain = bratonien_tools_nc_connector_decrypt_secret($connection['secret_blob'] ?? ''); + $decoded = json_decode($plain, true); + if (is_array($decoded) && array_key_exists('db_password', $decoded)) + { + return array( + 'v'=>2, + 'db_password'=>(string)($decoded['db_password'] ?? ''), + 'piwigo_user'=>(string)($decoded['piwigo_user'] ?? ''), + 'piwigo_password'=>(string)($decoded['piwigo_password'] ?? ''), + 'api_key_id'=>(string)($decoded['api_key_id'] ?? ''), + 'api_key_secret'=>(string)($decoded['api_key_secret'] ?? ''), + ); + } + + return array( + 'v'=>2, + 'db_password'=>(string)$plain, + 'piwigo_user'=>'', + 'piwigo_password'=>'', + 'api_key_id'=>'', + 'api_key_secret'=>'', + ); +} + +function bratonien_tools_nc_connector_store_scoped_secret($id, array $connection, array $credentials) +{ + if (trim((string)($credentials['db_password'] ?? '')) === '') throw new RuntimeException('Das Datenbankpasswort der Verbindung fehlt.'); + $payload = json_encode($credentials, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + if (!is_string($payload)) throw new RuntimeException('Verbindungszugangsdaten konnten nicht serialisiert werden.'); + $blob = bratonien_tools_nc_connector_encrypt_secret($payload); + + $config = is_array($connection['config'] ?? null) ? $connection['config'] : array(); + if (array_key_exists('api_enabled', $config)) + { + $config['api_enabled'] = trim((string)($credentials['api_key_id'] ?? '')) !== '' && trim((string)($credentials['api_key_secret'] ?? '')) !== ''; + } + $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.'); + + $table = bratonien_tools_nc_connector_table(); + $now = date('Y-m-d H:i:s'); + pwg_query("UPDATE `$table` SET secret_blob='".pwg_db_real_escape_string($blob)."', config_json='".pwg_db_real_escape_string($config_json)."', updated='".pwg_db_real_escape_string($now)."' WHERE id=".(int)$id." LIMIT 1"); +} + +function bratonien_tools_nc_connector_fallback_save_scoped() +{ + $id = (int)($_POST['connection_id'] ?? 0); + $username = trim((string)($_POST['nc_fallback_user'] ?? '')); + $password = (string)($_POST['nc_fallback_password'] ?? ''); + if ($username === '' || $password === '') throw new RuntimeException('Benutzername und Passwort für den Fallback müssen angegeben werden.'); + + $connection = bratonien_tools_nc_connector_connection($id, true); + if (!$connection) throw new RuntimeException('Connector-Verbindung wurde nicht gefunden.'); + $credentials = bratonien_tools_nc_connector_scoped_secret($connection); + $credentials['piwigo_user'] = $username; + $credentials['piwigo_password'] = $password; + bratonien_tools_nc_connector_store_scoped_secret($id, $connection, $credentials); + return array('message'=>'Piwigo-Benutzername und Passwort wurden als Fallback dieser Verbindung gespeichert.'); +} + +function bratonien_tools_nc_connector_fallback_delete_scoped() +{ + $id = (int)($_POST['connection_id'] ?? 0); + $connection = bratonien_tools_nc_connector_connection($id, true); + if (!$connection) throw new RuntimeException('Connector-Verbindung wurde nicht gefunden.'); + $credentials = bratonien_tools_nc_connector_scoped_secret($connection); + $credentials['piwigo_user'] = ''; + $credentials['piwigo_password'] = ''; + bratonien_tools_nc_connector_store_scoped_secret($id, $connection, $credentials); + return array('message'=>'Fallback dieser Verbindung wurde gelöscht. Ein vorhandener API-Zugang blieb unverändert.'); +}