servnest/pg-act/ns/sync.php

58 lines
1.7 KiB
PHP

<?php declare(strict_types=1);
$el_nb = count($_POST['syncs']);
if ($el_nb < 1 OR $el_nb > 8)
output(403, 'Wrong elements number.');
foreach ($_POST['syncs'] as $i => &$sync) {
if (($sync['source'] ?? '') === '') {
unset($_POST['syncs'][$i]);
continue;
}
$sync['source'] = formatAbsoluteDomain($sync['source']);
nsCheckZonePossession($sync['destination']);
}
$new_syncs = array_values($_POST['syncs']);
$new_destinations = array_column($new_syncs, 'destination');
if (count($new_destinations) !== count(array_unique($new_destinations)))
output(403, _('Multiple source domains can\'t be applied to the same target domain.'));
rateLimit();
$current_syncs = query('select', 'ns-syncs', ['username' => $_SESSION['id']], ['source', 'destination']);
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);
}
try {
DB->beginTransaction();
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'],
]);
DB->commit();
} catch (Exception $e) {
DB->rollback();
output(500, 'Database error.', [$e->getMessage()]);
}
output(200, _('Synchronized records updated.'));