From e17c45760a8c1a1089878e02d5cd9d2b5dadca9f Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Mon, 17 Aug 2026 21:33:32 +0200 Subject: [PATCH] Make new NC connections API-first with optional login fallback --- include/nc_connector_create_api.inc.php | 110 ++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 include/nc_connector_create_api.inc.php diff --git a/include/nc_connector_create_api.inc.php b/include/nc_connector_create_api.inc.php new file mode 100644 index 0000000..dee9a23 --- /dev/null +++ b/include/nc_connector_create_api.inc.php @@ -0,0 +1,110 @@ + 'Name', + 'nc_host' => 'PostgreSQL-Host', + 'nc_database' => 'Datenbank', + 'nc_user' => 'Reader-Benutzer', + 'nc_db_password' => 'Reader-Passwort', + 'nc_source_view' => 'Source-View', + 'nc_activity_view' => 'Activity-View', + 'nc_gallery_root' => 'Galerie-Pfad', + ); + + foreach ($required as $field => $label) + { + if (trim((string)($_POST[$field] ?? '')) === '') + { + throw new RuntimeException($label.' fehlt.'); + } + } + + $fallback_user = trim((string)($_POST['nc_piwigo_user'] ?? '')); + $fallback_password = (string)($_POST['nc_piwigo_password'] ?? ''); + if (($fallback_user === '') !== ($fallback_password === '')) + { + throw new RuntimeException('Fallback-Benutzer und Fallback-Passwort muessen entweder beide angegeben oder beide leer gelassen werden.'); + } + + $api = bratonien_tools_nc_api_credentials(); + if (($api['key_id'] ?? '') === '' && $fallback_user === '') + { + throw new RuntimeException('Es ist weder ein gespeicherter Piwigo-API-Zugang noch ein Benutzername/Passwort-Fallback vorhanden.'); + } + + $port = (int)($_POST['nc_port'] ?? 5432); + if ($port < 1 || $port > 65535) + { + throw new RuntimeException('Ungueltiger PostgreSQL-Port.'); + } + + $gallery_root = rtrim(trim((string)$_POST['nc_gallery_root']), '/'); + if ($gallery_root === '' || $gallery_root[0] !== '/') + { + throw new RuntimeException('Der Galerie-Pfad muss ein absoluter Pfad sein.'); + } + + $config = array( + 'host' => trim((string)$_POST['nc_host']), + 'port' => (string)$port, + 'database' => trim((string)$_POST['nc_database']), + 'user' => trim((string)$_POST['nc_user']), + 'source_view' => trim((string)$_POST['nc_source_view']), + 'activity_view' => trim((string)$_POST['nc_activity_view']), + 'gallery_root' => $gallery_root, + 'state_dir' => '', + 'status_file' => '', + 'quiet_seconds' => max(0, (int)($_POST['nc_quiet_seconds'] ?? 120)), + 'max_wait_seconds' => max(60, (int)($_POST['nc_max_wait_seconds'] ?? 900)), + 'full_sync_seconds' => max(300, (int)($_POST['nc_full_sync_seconds'] ?? 86400)), + 'storages' => bratonien_tools_nc_connector_parse_storages($_POST['nc_storages'] ?? ''), + 'origin' => 'native', + 'piwigo_auth' => 'api-first', + ); + + bratonien_tools_nc_connector_view_name($config['source_view']); + bratonien_tools_nc_connector_view_name($config['activity_view']); + + $table = bratonien_tools_nc_connector_table(); + bratonien_tools_nc_connector_ensure_table(); + $now = date('Y-m-d H:i:s'); + $connection_key = 'local-'.bin2hex(random_bytes(12)); + $secret_blob = bratonien_tools_nc_connector_encrypt_credentials( + (string)$_POST['nc_db_password'], + $fallback_user, + $fallback_password + ); + $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.'); + } + + pwg_query("INSERT INTO `$table` + (connection_key, name, adapter, enabled, takeover_state, config_json, secret_blob, created, updated) + VALUES ('".pwg_db_real_escape_string($connection_key)."', + '".pwg_db_real_escape_string(trim((string)$_POST['nc_name']))."', + 'local', 0, 'disabled', + '".pwg_db_real_escape_string($config_json)."', + '".pwg_db_real_escape_string($secret_blob)."', + '".pwg_db_real_escape_string($now)."', + '".pwg_db_real_escape_string($now)."')"); + + $id = (int)pwg_db_insert_id(); + $config['state_dir'] = '/var/lib/bratonien-tools/nc-connector/connection-'.$id; + $config['status_file'] = $config['state_dir'].'/connector-status.json'; + $config_json = json_encode($config, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + pwg_query("UPDATE `$table` SET config_json='".pwg_db_real_escape_string($config_json)."' WHERE id=".$id); + + return array( + 'message' => $fallback_user === '' + ? 'Nextcloud-Verbindung wurde API-first ohne dauerhaft gespeicherten Benutzername/Passwort-Fallback angelegt. Bitte technisch pruefen und danach im LXC aktivieren.' + : 'Nextcloud-Verbindung wurde API-first mit verschluesseltem Benutzername/Passwort-Fallback angelegt. Bitte technisch pruefen und danach im LXC aktivieren.' + ); +}