'Connector-Verbindung wurde gelöscht und für weitere Laufzeitabrufe gesperrt. Nextcloud- und Piwigo-Bilder blieben unverändert.'); } function bratonien_tools_nc_connector_verify_connection_scoped() { $result = bratonien_tools_nc_connector_verify_managed(); $id = (int)($_POST['connection_id'] ?? 0); $connection = bratonien_tools_nc_connector_connection($id, true); if (!$connection) return $result; $config = is_array($connection['config'] ?? null) ? $connection['config'] : array(); if ((string)($config['origin'] ?? '') !== 'native' || array_key_exists('api_enabled', $config)) return $result; $credentials = bratonien_tools_nc_connector_credentials_from_blob($connection['secret_blob'] ?? ''); $db_password = (string)($credentials['db_password'] ?? ''); $fallback_user = (string)($credentials['piwigo_user'] ?? ''); $fallback_password = (string)($credentials['piwigo_password'] ?? ''); if ($db_password === '') return $result; $api_key_id = ''; $api_key_secret = ''; if ($fallback_user === '') { $legacy_api = bratonien_tools_nc_api_credentials(); $api_key_id = trim((string)($legacy_api['key_id'] ?? '')); $api_key_secret = trim((string)($legacy_api['key_secret'] ?? '')); } $payload = json_encode(array( 'v'=>3, 'db_password'=>$db_password, 'piwigo_user'=>$fallback_user, 'piwigo_password'=>$fallback_password, 'api_key_id'=>$api_key_id, 'api_key_secret'=>$api_key_secret, 'nextcloud_user'=>'', 'nextcloud_password'=>'', ), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if (!is_string($payload)) throw new RuntimeException('Verbindungsauthentifizierung konnte nicht migriert werden.'); $config['piwigo_auth'] = 'connection-scoped'; $config['api_enabled'] = $api_key_id !== ''; $config_json = json_encode($config, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if (!is_string($config_json)) throw new RuntimeException('Connector-Konfiguration konnte nicht migriert werden.'); $blob = bratonien_tools_nc_connector_encrypt_secret($payload); $table = bratonien_tools_nc_connector_table(); 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'=>3, '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'] ?? ''), 'nextcloud_user'=>(string)($decoded['nextcloud_user'] ?? ''), 'nextcloud_password'=>(string)($decoded['nextcloud_password'] ?? ''), ); } return array( 'v'=>3, 'db_password'=>(string)$plain, 'piwigo_user'=>'', 'piwigo_password'=>'', 'api_key_id'=>'', 'api_key_secret'=>'', 'nextcloud_user'=>'', 'nextcloud_password'=>'', ); } function bratonien_tools_nc_connector_store_scoped_secret($id, array $connection, array $credentials) { $config = is_array($connection['config'] ?? null) ? $connection['config'] : array(); $is_webdav = (string)($config['source_mode'] ?? '') === 'webdav-placeholder'; if (!$is_webdav && trim((string)($credentials['db_password'] ?? '')) === '') throw new RuntimeException('Das Datenbankpasswort der Verbindung fehlt.'); if ($is_webdav && (trim((string)($credentials['nextcloud_user'] ?? '')) === '' || (string)($credentials['nextcloud_password'] ?? '') === '')) { throw new RuntimeException('Die Nextcloud-Zugangsdaten der WebDAV-Verbindung fehlen.'); } $credentials['v'] = 3; $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); 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.'); }