mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 14:04:34 +00:00
Make new NC connections API-first with optional login fallback
This commit is contained in:
110
include/nc_connector_create_api.inc.php
Normal file
110
include/nc_connector_create_api.inc.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
if (!defined('PHPWG_ROOT_PATH'))
|
||||
{
|
||||
die('Hacking attempt!');
|
||||
}
|
||||
|
||||
function bratonien_tools_nc_connector_create_local_api_first()
|
||||
{
|
||||
$required = array(
|
||||
'nc_name' => '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.'
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user