1
0

edit_rules.php 5.9 KB

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