edit_group.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. $ou_info = get_record_sql($db_link,'SELECT * FROM ou WHERE id=?', [$id]);
  6. // Сохранение настроек OU
  7. if (getPOST("save") !== null) {
  8. $new = [
  9. 'ou_name' => trim(getPOST("f_group_name", null, $ou_info['ou_name'])),
  10. 'default_users' => (int)getPOST("f_default", null, 0),
  11. 'default_hotspot' => (int)getPOST("f_default_hotspot", null, 0),
  12. 'nagios_dir' => trim(getPOST("f_nagios", null, '')),
  13. 'nagios_host_use' => trim(getPOST("f_nagios_host", null, '')),
  14. 'nagios_ping' => trim(getPOST("f_nagios_ping", null, 0)),
  15. 'nagios_default_service' => trim(getPOST("f_nagios_service", null, '')),
  16. 'queue_id' => (int)getPOST("f_queue_id", null, 0),
  17. 'filter_group_id' => (int)getPOST("f_filter_group_id", null, 0),
  18. 'enabled' => (int)getPOST("f_enabled", null, 0),
  19. 'dynamic' => (int)getPOST("f_dynamic", null, 0)
  20. ];
  21. // Обработка life_duration
  22. if ($new['dynamic']) {
  23. $tmp_life_duration = str_replace(',', '.', getPOST("f_life_duration", null, 0));
  24. $new['life_duration'] = (!empty($tmp_life_duration) && is_numeric($tmp_life_duration))
  25. ? (float)$tmp_life_duration
  26. : 0;
  27. } else {
  28. $new['life_duration'] = 0;
  29. }
  30. // Сброс флагов по умолчанию
  31. if ($new['default_users']) {
  32. update_records($db_link, "ou", "id != ?", ['default_users' => 0], [$id]);
  33. }
  34. if ($new['default_hotspot']) {
  35. update_records($db_link, "ou", "id != ?", ['default_hotspot' => 0], [$id]);
  36. }
  37. update_record($db_link, "ou", "id = ?", $new, [$id]);
  38. header("Location: " . $_SERVER["REQUEST_URI"]);
  39. exit;
  40. }
  41. // Удаление правил авторизации
  42. if (getPOST("s_remove") !== null) {
  43. $s_id = getPOST("s_id", null, []);
  44. if (!empty($s_id) && is_array($s_id)) {
  45. foreach ($s_id as $val) {
  46. $val = trim($val);
  47. if ($val === '') continue;
  48. LOG_INFO($db_link, "Remove rule id: $val " . dump_record($db_link, 'auth_rules', 'id = ?', [$val]));
  49. delete_record($db_link, "auth_rules", "id = ?", [(int)$val]);
  50. }
  51. }
  52. header("Location: " . $_SERVER["REQUEST_URI"]);
  53. exit;
  54. }
  55. // Сохранение изменений в правилах
  56. if (getPOST("s_save") !== null) {
  57. $s_ids = getPOST("s_id", null, []);
  58. $n_ids = getPOST("n_id", null, []);
  59. $s_types = getPOST("s_type", null, []);
  60. $s_rules = getPOST("s_rule", null, []);
  61. $s_descriptions = getPOST("s_description", null, []);
  62. if (is_array($s_ids) && is_array($n_ids)) {
  63. // Преобразуем ID в целые числа
  64. $n_ids = array_map('intval', $n_ids);
  65. $s_ids = array_map('intval', $s_ids);
  66. foreach ($s_ids as $save_id) {
  67. if ($save_id <= 0) continue;
  68. $idx = array_search($save_id, $n_ids, true);
  69. if ($idx === false) continue;
  70. // Получаем тип правила
  71. $rule_type = (int)($s_types[$idx] ?? 3);
  72. // Получаем и очищаем правило
  73. $raw_rule = trim($s_rules[$idx] ?? '');
  74. if ($raw_rule === '') continue;
  75. $new_rule = $raw_rule;
  76. // Валидация в зависимости от типа
  77. if ($rule_type == 1) {
  78. // IP-адрес
  79. if (!checkValidIp($new_rule)) {
  80. continue; // пропускаем невалидный IP
  81. }
  82. } elseif ($rule_type == 2) {
  83. // MAC-адрес
  84. $normalized_mac = MayBeMac($new_rule);
  85. if ($normalized_mac === null) {
  86. continue; // пропускаем невалидный MAC
  87. }
  88. $new_rule = $normalized_mac;
  89. }
  90. // Для других типов (3 и т.д.) — без валидации
  91. $new = [
  92. 'rule_type' => $rule_type,
  93. 'rule' => $new_rule,
  94. 'description' => trim($s_descriptions[$idx] ?? '')
  95. ];
  96. update_record($db_link, "auth_rules", "id = ?", $new, [$save_id]);
  97. }
  98. }
  99. header("Location: " . $_SERVER["REQUEST_URI"]);
  100. exit;
  101. }
  102. // Создание нового правила
  103. if (getPOST("s_create") !== null) {
  104. $new_rule = trim(getPOST("s_new_rule", null, ''));
  105. if ($new_rule !== '') {
  106. $rule_type = (int)getPOST("s_new_type", null, 3);
  107. if ($rule_type == 1 and !checkValidIp($new_rule)) {
  108. header("Location: " . $_SERVER["REQUEST_URI"]);
  109. exit;
  110. }
  111. if ($rule_type == 2 and MayBeMac($new_rule)==null) {
  112. header("Location: " . $_SERVER["REQUEST_URI"]);
  113. exit;
  114. }
  115. if ($rule_type == 2) { $new_rule = MayBeMac($new_rule); }
  116. $new = [
  117. 'rule_type' => $rule_type,
  118. 'rule' => $new_rule,
  119. 'ou_id' => $id
  120. ];
  121. LOG_INFO($db_link, "Create new rule $new_rule for ou_id: $id");
  122. insert_record($db_link, "auth_rules", $new);
  123. }
  124. header("Location: " . $_SERVER["REQUEST_URI"]);
  125. exit;
  126. }
  127. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  128. fix_auth_rules($db_link);
  129. ?>
  130. <div id="cont">
  131. <form name="def" action="edit_group.php?id=<?php echo $id; ?>" method="post">
  132. <input type="hidden" name="id" value=<?php echo $id; ?>>
  133. <table class="data">
  134. <tr align="center">
  135. <td colspan=2><b><?php echo WEB_cell_name; ?></b></td>
  136. <td><b>Default</b></td>
  137. <td width=100><b>Hotspot</b></td>
  138. <td><b><?php print WEB_cell_dynamic; ?></b></td>
  139. </tr>
  140. <?php
  141. print "<tr align=center>\n";
  142. print "<td colspan=2 class=\"data\"><input type=\"text\" name='f_group_name' value='{$ou_info['ou_name']}' style=\"width:95%;\"></td>\n";
  143. if ($ou_info['default_users']) { $cl = "up"; } else { $cl="data"; }
  144. print "<td class=\"$cl\">"; print_qa_select("f_default",$ou_info['default_users']); print "</td>\n";
  145. if ($ou_info['default_hotspot']) { $cl = "up"; } else { $cl="data"; }
  146. print "<td class=\"$cl\">"; print_qa_select("f_default_hotspot",$ou_info['default_hotspot']); print "</td>\n";
  147. print "<td class=\"data\">"; print_qa_select("f_dynamic",$ou_info['dynamic']); print "</td>\n";
  148. ?>
  149. <tr>
  150. <td><b>Nagios directory</b></td>
  151. <td><b>Host template</b></td>
  152. <td><b>Ping</b></td>
  153. <td><b>Host service</b></td>
  154. <td></td>
  155. </tr>
  156. <?php
  157. print "<td class=\"data\"><input type=\"text\" name='f_nagios' value='{$ou_info['nagios_dir']}'></td>\n";
  158. print "<td class=\"data\"><input type=\"text\" name='f_nagios_host' value='{$ou_info['nagios_host_use']}'></td>\n";
  159. print "<td class=\"data\">"; print_qa_select("f_nagios_ping",$ou_info['nagios_ping']); print "</td>\n";
  160. print "<td class=\"data\"><input type=\"text\" name='f_nagios_service' value='{$ou_info['nagios_default_service']}'></td>\n";
  161. print "<td class=\"data\"></td>\n";
  162. ?>
  163. </tr>
  164. <tr><td colspan=4><?php echo WEB_ou_autoclient_rules; ?></td></tr>
  165. <tr>
  166. <td class="data"><?php print WEB_cell_enabled."&nbsp"; print_qa_select('f_enabled', $ou_info['enabled']); ?></td>
  167. <td class="data"><?php print WEB_cell_filter."&nbsp"; print_filter_group_select($db_link, 'f_filter_group_id', $ou_info['filter_group_id']); ?></td>
  168. <td class="data"><?php print WEB_cell_shaper."&nbsp"; print_queue_select($db_link, 'f_queue_id', $ou_info['queue_id']); ?></td>
  169. <td class="data" align=right><?php print WEB_cell_life_hours."&nbsp";
  170. print "<input type='number' step='0.01' min='0.01' id='f_life_duration' name='f_life_duration' value='" . htmlspecialchars($ou_info['life_duration'])."'";
  171. if (!$ou_info['dynamic']) { print "disabled"; }; print " style=\"width:35%;\" ></td>\n"; ?>
  172. <?php print "<td align=right class=\"data\"><button id='save' name='save' value='{$ou_info['id']}'>".WEB_btn_save."</button></td>\n"; ?>
  173. </tr>
  174. </table>
  175. <br>
  176. <b><?php echo WEB_ou_rules_for_autoassigning."&nbsp"; print $ou_info['ou_name']; ?></b>
  177. <br>
  178. <?php echo WEB_ou_rules_order; ?>: hotspot => subnet => mac => hostname => default user
  179. <br><br>
  180. <table class="data">
  181. <tr align="center">
  182. <td></td>
  183. <td width=30><b>id</b></td>
  184. <td><b><?php echo WEB_cell_type; ?></b></td>
  185. <td><b><?php echo WEB_ou_rule; ?></b></td>
  186. <td><b><?php echo WEB_cell_description; ?></b></td>
  187. <td><input type="submit" onclick="return confirm('<?php echo WEB_msg_delete; ?>?')" name="s_remove" value="<?php echo WEB_btn_delete; ?>"></td>
  188. <?php print "<td><button id='s_save' name='s_save' value='s_save'>".WEB_btn_save."</button></td>"; ?>
  189. </tr>
  190. <?php
  191. $t_auth_rules = get_records_sql($db_link,"SELECT * FROM auth_rules WHERE ou_id=? ORDER BY id", [ $id ]);
  192. foreach ( $t_auth_rules as $row ) {
  193. print "<tr align=center>\n";
  194. print "<td class=\"data\" style='padding:0'><input type=checkbox name=s_id[] value='{$row['id']}'></td>\n";
  195. print "<td class=\"data\"><input type=\"hidden\" name='n_id[]' value='{$row['id']}'>{$row['id']}</td>\n";
  196. print "<td class=\"data\">"; print_qa_rule_select("s_type[]","{$row['rule_type']}"); print "</td>\n";
  197. print "<td class=\"data\"><input type=\"text\" name='s_rule[]' value='{$row['rule']}'></td>\n";
  198. print "<td class=\"data\"><input type=\"text\" name='s_description[]' value='{$row['description']}'></td>\n";
  199. print "<td colspan=2 class=\"data\"></td>\n";
  200. print "</tr>\n";
  201. }
  202. ?>
  203. </table>
  204. <div>
  205. <?php print WEB_ou_new_rule."&nbsp"; print_qa_rule_select("s_new_type","1");
  206. print "<input type=\"text\" name='s_new_rule' value=''>"; ?>
  207. <input type="submit" name="s_create" value="<?php echo WEB_btn_add; ?>">
  208. </div>
  209. </form>
  210. <script>
  211. document.getElementById('f_dynamic').addEventListener('change', function(event) {
  212. const selectValue = this.value;
  213. const inputField = document.getElementById('f_life_duration');
  214. if (selectValue === '1') {
  215. inputField.disabled = false;
  216. inputField.value=24;
  217. } else {
  218. inputField.disabled = true;
  219. }
  220. });
  221. </script>
  222. <?php
  223. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.simple.php");
  224. ?>