1
0

edit_alias.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
  4. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/idfilter.php");
  5. $msg_error = "";
  6. $sSQL = "SELECT * FROM user_auth WHERE id = ?";
  7. $auth_info = get_record_sql($db_link, $sSQL, [$id]);
  8. if (empty($auth_info['dns_name']) || $auth_info['deleted']) {
  9. header("Location: /admin/users/editauth.php?id=" . $id);
  10. exit;
  11. }
  12. // Очистка удалённых алиасов
  13. delete_records($db_link, "user_auth_alias", "auth_id IN (SELECT id FROM user_auth WHERE deleted = 1)");
  14. // Удаление алиасов
  15. if (getPOST("s_remove") !== null) {
  16. $s_id = getPOST("s_id", null, []);
  17. if (!empty($s_id) && is_array($s_id)) {
  18. foreach ($s_id as $val) {
  19. $val = trim($val);
  20. if ($val === '') continue;
  21. LOG_INFO($db_link, "Remove alias id: $val " . dump_record($db_link, 'user_auth_alias', 'id = ?', [$val]));
  22. delete_record($db_link, "user_auth_alias", "id = ?", [(int)$val]);
  23. }
  24. }
  25. header("Location: " . $page_url);
  26. exit;
  27. }
  28. // Сохранение изменений в алиасах
  29. if (getPOST("s_save") !== null) {
  30. $selected_ids = getPOST("s_id", null, []); // отмеченные чекбоксы
  31. $all_ids = getPOST("n_id", null, []); // все ID
  32. $s_aliases = getPOST("s_alias", null, []);
  33. $s_descriptions = getPOST("s_description", null, []);
  34. if (!empty($selected_ids) && is_array($selected_ids)) {
  35. $selected_ids = array_map('intval', $selected_ids);
  36. $selected_set = array_flip($selected_ids);
  37. $domain_zone = ltrim(get_option($db_link, 33), '.');
  38. foreach ($all_ids as $i => $id) {
  39. $id = (int)$id;
  40. if ($id <= 0 || !isset($selected_set[$id])) continue;
  41. $f_dnsname = trim($s_aliases[$i] ?? '');
  42. if ($f_dnsname !== '') {
  43. // Удаляем доменную зону из конца
  44. $escaped_zone = preg_quote($domain_zone, '/');
  45. $f_dnsname = preg_replace('/\.' . $escaped_zone . '$/i', '', $f_dnsname);
  46. $f_dnsname = preg_replace('/\s+/', '-', $f_dnsname);
  47. }
  48. // Валидация
  49. if (
  50. $f_dnsname === '' ||
  51. !checkValidHostname($f_dnsname) ||
  52. !checkUniqHostname($db_link, $id, $f_dnsname)
  53. ) {
  54. continue;
  55. }
  56. $new = [
  57. 'alias' => $f_dnsname,
  58. 'description' => trim($s_descriptions[$i] ?? '')
  59. ];
  60. update_record($db_link, "user_auth_alias", "id = ?", $new, [$id]);
  61. }
  62. }
  63. header("Location: " . $page_url);
  64. exit;
  65. }
  66. // Создание нового алиаса
  67. if (getPOST("s_create") !== null) {
  68. $new_alias = trim(getPOST("s_create_alias", null, ''));
  69. if ($new_alias !== '') {
  70. $domain_zone = ltrim(get_option($db_link, 33), '.');
  71. $f_dnsname = $new_alias;
  72. if ($f_dnsname !== '') {
  73. $escaped_zone = preg_quote($domain_zone, '/');
  74. $f_dnsname = preg_replace('/\.' . $escaped_zone . '$/i', '', $f_dnsname);
  75. $f_dnsname = preg_replace('/\s+/', '-', $f_dnsname);
  76. }
  77. if (
  78. $f_dnsname === '' ||
  79. !checkValidHostname($f_dnsname) ||
  80. !checkUniqHostname($db_link, $id, $f_dnsname)
  81. ) {
  82. $msg_error = "DNS $f_dnsname already exists at: " . searchHostname($db_link, $id, $f_dnsname) . " Discard changes!";
  83. $_SESSION[$page_url]['msg'] = $msg_error;
  84. LOG_ERROR($db_link, $msg_error);
  85. header("Location: " . $_SERVER["REQUEST_URI"]);
  86. exit;
  87. }
  88. $new_rec = [
  89. 'alias' => $f_dnsname,
  90. 'auth_id' => $id
  91. ];
  92. LOG_INFO($db_link, "Create new alias $new_alias");
  93. insert_record($db_link, "user_auth_alias", $new_rec);
  94. }
  95. header("Location: " . $page_url);
  96. exit;
  97. }
  98. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  99. ?>
  100. <div id="cont">
  101. <?php
  102. if (!empty($_SESSION[$page_url]['msg'])) {
  103. print '<div id="msg">' . $_SESSION[$page_url]['msg'] . '</div>';
  104. unset($_SESSION[$page_url]['msg']);
  105. }
  106. ?>
  107. <br>
  108. <form name="def" action="edit_alias.php?id=<?php echo $id; ?>" method="post">
  109. <b><?php print WEB_user_alias_for."&nbsp"; print_url($auth_info['ip'],"/admin/users/editauth.php?id=$id"); ?></b> <br>
  110. <table class="data">
  111. <tr align="center">
  112. <td><input type="checkbox" onClick="checkAll(this.checked);"></td>
  113. <td width=30><b>id</b></td>
  114. <td><b><?php echo WEB_cell_name; ?></b></td>
  115. <td><b><?php echo WEB_cell_description; ?></b></td>
  116. <td>
  117. <!-- Контейнер для кнопок справа -->
  118. <div style="text-align: right; white-space: nowrap;">
  119. <input type="submit" name="s_save" value="<?php echo WEB_btn_save; ?>">
  120. <input type="submit"
  121. onclick="return confirm('<?php echo WEB_msg_delete; ?>?')"
  122. name="s_remove"
  123. value="<?php echo WEB_btn_delete; ?>"
  124. style="margin-left: 8px;">
  125. </div>
  126. </td>
  127. </tr>
  128. <?php
  129. $t_user_auth_alias = get_records_sql($db_link,"SELECT * FROM user_auth_alias WHERE auth_id=? ORDER BY id", [ $id ]);
  130. if (!empty($t_user_auth_alias)) {
  131. foreach ( $t_user_auth_alias as $row ) {
  132. print "<tr align=center>\n";
  133. print "<td class=\"data\" style='padding:0'><input type=checkbox name=s_id[] value='{$row['id']}'></td>\n";
  134. print "<td class=\"data\"><input type=\"hidden\" name='n_id[]' value='{$row['id']}'>{$row['id']}</td>\n";
  135. print "<td class=\"data\"><input type=\"text\" name='s_alias[]' value='{$row['alias']}' pattern=\"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$\"></td>\n";
  136. print "<td class=\"data\"><input type=\"text\" name='s_description[]' value='{$row['description']}'></td>\n";
  137. print "<td class=\"data\"></td>\n";
  138. print "</tr>\n";
  139. }
  140. }
  141. ?>
  142. </table>
  143. <div>
  144. <?php echo WEB_user_dns_add_alias; ?>:
  145. <input type="text" name='s_create_alias' value='' pattern="^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$">
  146. <input type="submit" name="s_create" value="<?php echo WEB_btn_add; ?>">
  147. </div>
  148. </form>
  149. <?php
  150. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  151. ?>