1
0

edit_alias.php 6.3 KB

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