edit_rules.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_list WHERE id = ?";
  7. $auth_info = get_record_sql($db_link, $sSQL, [$id]);
  8. // Удаление правил
  9. if (getPOST("s_remove") !== null) {
  10. $s_id = getPOST("s_id", null, []);
  11. if (!empty($s_id) && is_array($s_id)) {
  12. foreach ($s_id as $val) {
  13. $val = trim($val);
  14. if ($val === '') continue;
  15. delete_record($db_link, "auth_rules", "id = ?", [(int)$val]);
  16. }
  17. }
  18. header("Location: " . $_SERVER["REQUEST_URI"]);
  19. exit;
  20. }
  21. // Сохранение ОТМЕЧЕННЫХ правил
  22. if (getPOST("s_save") !== null) {
  23. $selected_ids = getPOST("s_id", null, []); // отмеченные чекбоксы
  24. $all_ids = getPOST("n_id", null, []); // все ID
  25. $types = getPOST("s_type", null, []);
  26. $rules = getPOST("s_rule", null, []);
  27. $descriptions = getPOST("s_description", null, []);
  28. if (!empty($selected_ids) && is_array($selected_ids)) {
  29. $selected_ids = array_map('intval', $selected_ids);
  30. $selected_set = array_flip($selected_ids);
  31. foreach ($all_ids as $i => $id) {
  32. $id = (int)$id;
  33. if ($id <= 0 || !isset($selected_set[$id])) continue;
  34. // Получаем тип правила
  35. $rule_type = (int)($types[$i] ?? 3);
  36. $raw_rule = trim($rules[$i] ?? '');
  37. $desc = trim($descriptions[$i] ?? '');
  38. if ($raw_rule === '') continue;
  39. $new_rule = $raw_rule;
  40. // Валидация в зависимости от типа
  41. if ($rule_type == 1) {
  42. // IP-адрес
  43. if (!checkValidIp($new_rule)) {
  44. continue; // пропускаем невалидный IP
  45. }
  46. } elseif ($rule_type == 2) {
  47. // MAC-адрес
  48. $normalized_mac = MayBeMac($new_rule);
  49. if ($normalized_mac === null) {
  50. continue; // пропускаем невалидный MAC
  51. }
  52. $new_rule = $normalized_mac;
  53. }
  54. $new = [
  55. 'rule_type' => $rule_type,
  56. 'rule' => $new_rule,
  57. 'description' => $desc
  58. ];
  59. update_auth_rule($db_link, $new, $id);
  60. }
  61. }
  62. header("Location: " . $_SERVER["REQUEST_URI"]);
  63. exit;
  64. }
  65. // Создание нового правила
  66. if (getPOST("s_create") !== null) {
  67. $new_rule = trim(getPOST("s_new_rule", null, ''));
  68. $rule_type = (int)getPOST("s_new_type", null, 3);
  69. if ($new_rule !== '') {
  70. if ($rule_type == 1 and !checkValidIp($new_rule)) {
  71. header("Location: " . $_SERVER["REQUEST_URI"]);
  72. exit;
  73. }
  74. if ($rule_type == 2 and MayBeMac($new_rule)==null) {
  75. header("Location: " . $_SERVER["REQUEST_URI"]);
  76. exit;
  77. }
  78. if ($rule_type == 2) { $new_rule = MayBeMac($new_rule); }
  79. add_auth_rule($db_link, $new_rule, $rule_type, $id);
  80. }
  81. header("Location: " . $_SERVER["REQUEST_URI"]);
  82. exit;
  83. }
  84. unset($_POST);
  85. fix_auth_rules($db_link);
  86. require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/header.php");
  87. ?>
  88. <div id="cont">
  89. <br>
  90. <form name="def" action="edit_rules.php?id=<?php echo $id; ?>" method="post">
  91. <b><?php print WEB_ou_rules_for_autoassigning . "&nbsp;"; print_url($auth_info['login'], "/admin/users/edituser.php?id=$id"); ?></b>
  92. <br>
  93. <?php echo WEB_ou_rules_order; ?>: hotspot => subnet => mac => hostname => default user
  94. <br><br>
  95. <table class="data">
  96. <tr align="center">
  97. <td><input type="checkbox" onClick="checkAll(this.checked);"></td>
  98. <td width=30><b>id</b></td>
  99. <td><b><?php echo WEB_cell_type; ?></b></td>
  100. <td><b><?php echo WEB_ou_rule; ?></b></td>
  101. <td><b><?php echo WEB_cell_description; ?></b></td>
  102. <td>
  103. <!-- Кнопки управления справа -->
  104. <div style="text-align: right; white-space: nowrap;">
  105. <input type="submit" name="s_save" value="<?php echo WEB_btn_save; ?>">
  106. <input type="submit"
  107. onclick="return confirm('<?php echo WEB_msg_delete; ?>?')"
  108. name="s_remove"
  109. value="<?php echo WEB_btn_delete; ?>"
  110. style="margin-left: 8px;">
  111. </div>
  112. </td>
  113. </tr>
  114. <?php
  115. $t_auth_rules = get_records_sql($db_link, "SELECT * FROM auth_rules WHERE user_id = ? ORDER BY id", [$id]);
  116. foreach ($t_auth_rules as $row) {
  117. print "<tr align=center>\n";
  118. print "<td class=\"data\" style='padding:0'><input type=\"checkbox\" name=\"s_id[]\" value=\"{$row['id']}\"></td>\n";
  119. print "<td class=\"data\"><input type=\"hidden\" name=\"n_id[]\" value=\"{$row['id']}\">{$row['id']}</td>\n";
  120. print "<td class=\"data\">";
  121. print_qa_rule_select("s_type[]", "{$row['rule_type']}");
  122. print "</td>\n";
  123. print "<td class=\"data\"><input type=\"text\" name=\"s_rule[]\" value=\"" . htmlspecialchars($row['rule']) . "\"></td>\n";
  124. print "<td class=\"data\"><input type=\"text\" name=\"s_description[]\" value=\"" . htmlspecialchars($row['description']) . "\"></td>\n";
  125. print "<td class=\"data\"></td>\n";
  126. print "</tr>\n";
  127. }
  128. ?>
  129. </table>
  130. <div style="margin-top: 15px;">
  131. <?php
  132. print WEB_ou_new_rule . "&nbsp;";
  133. print_qa_rule_select("s_new_type", "1");
  134. print '<input type="text" name="s_new_rule" value="">';
  135. ?>
  136. <input type="submit" name="s_create" value="<?php echo WEB_btn_add; ?>">
  137. </div>
  138. </form>
  139. <?php
  140. require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.php");
  141. ?>