servnest/pg-act/ns/sync.php

58 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2023-07-17 19:15:18 +00:00
<?php declare(strict_types=1);
2023-06-24 14:54:36 +00:00
$el_nb = count($_POST['syncs']);
if ($el_nb < 1 OR $el_nb > 8)
output(403, 'Wrong elements number.');
foreach ($_POST['syncs'] as $i => &$sync) {
2023-06-24 14:54:36 +00:00
if (($sync['source'] ?? '') === '') {
unset($_POST['syncs'][$i]);
continue;
}
$sync['source'] = formatAbsoluteDomain($sync['source']);
nsCheckZonePossession($sync['destination']);
}
$new_syncs = array_values($_POST['syncs']);
2023-06-24 14:54:36 +00:00
$new_destinations = array_column($new_syncs, 'destination');
if (count($new_destinations) !== count(array_unique($new_destinations)))
2023-06-26 02:13:52 +00:00
output(403, _('Multiple source domains can\'t be applied to the same target domain.'));
2023-06-24 14:54:36 +00:00
rateLimit();
$current_syncs = query('select', 'ns-syncs', ['username' => $_SESSION['id']], ['source', 'destination']);
2023-06-24 14:54:36 +00:00
try {
foreach ($new_syncs as $new_sync)
if (!in_array($new_sync, $current_syncs))
nsSync($new_sync['source'], $new_sync['destination']);
} catch (KdigException | NoDnssecException $e) {
output(403, $e->getMessage() . LF);
}
2023-06-24 14:54:36 +00:00
try {
DB->beginTransaction();
2023-06-24 14:54:36 +00:00
foreach ($current_syncs as $current_sync) // Deletions
if (!in_array($current_sync, $new_syncs))
query('delete', 'ns-syncs', [
'username' => $_SESSION['id'],
'source' => $current_sync['source'],
'destination' => $current_sync['destination'],
]);
foreach ($new_syncs as $new_sync) // Adds
if (!in_array($new_sync, $current_syncs))
insert('ns-syncs', [
'username' => $_SESSION['id'],
'source' => $new_sync['source'],
'destination' => $new_sync['destination'],
]);
2023-06-24 14:54:36 +00:00
DB->commit();
} catch (Exception $e) {
DB->rollback();
output(500, 'Database error.', [$e->getMessage()]);
}
output(200, _('Synchronized records updated.'));