sync.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php declare(strict_types=1);
  2. $el_nb = count($_POST['syncs']);
  3. if ($el_nb < 1 OR $el_nb > 8)
  4. output(403, 'Wrong elements number.');
  5. foreach ($_POST['syncs'] as $i => &$sync) {
  6. if (($sync['source'] ?? '') === '') {
  7. unset($_POST['syncs'][$i]);
  8. continue;
  9. }
  10. $sync['source'] = formatAbsoluteDomain($sync['source']);
  11. nsCheckZonePossession($sync['destination']);
  12. }
  13. $new_syncs = array_values($_POST['syncs']);
  14. $new_destinations = array_column($new_syncs, 'destination');
  15. if (count($new_destinations) !== count(array_unique($new_destinations)))
  16. output(403, _('Multiple source domains can\'t be applied to the same target domain.'));
  17. rateLimit();
  18. $current_syncs = query('select', 'ns-syncs', ['username' => $_SESSION['id']], ['source', 'destination']);
  19. try {
  20. foreach ($new_syncs as $new_sync)
  21. if (!in_array($new_sync, $current_syncs))
  22. nsSync($new_sync['source'], $new_sync['destination']);
  23. } catch (KdigException | NoDnssecException $e) {
  24. output(403, $e->getMessage() . LF);
  25. }
  26. try {
  27. DB->beginTransaction();
  28. foreach ($current_syncs as $current_sync) // Deletions
  29. if (!in_array($current_sync, $new_syncs))
  30. query('delete', 'ns-syncs', [
  31. 'username' => $_SESSION['id'],
  32. 'source' => $current_sync['source'],
  33. 'destination' => $current_sync['destination'],
  34. ]);
  35. foreach ($new_syncs as $new_sync) // Adds
  36. if (!in_array($new_sync, $current_syncs))
  37. insert('ns-syncs', [
  38. 'username' => $_SESSION['id'],
  39. 'source' => $new_sync['source'],
  40. 'destination' => $new_sync['destination'],
  41. ]);
  42. DB->commit();
  43. } catch (Exception $e) {
  44. DB->rollback();
  45. output(500, 'Database error.', [$e->getMessage()]);
  46. }
  47. output(200, _('Synchronized records updated.'));