editsubnet.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. if (isset($_POST['s_save'])) {
  6. $new = [];
  7. // === INPUT CLEANING =====================================================================================
  8. $new['subnet'] = trim($_POST['s_subnet']);
  9. $new['vlan_tag'] = normalize_vlan($_POST['s_vlan']);
  10. $new['office'] = intval_or_zero($_POST['s_office']);
  11. $new['hotspot'] = intval_or_zero($_POST['s_hotspot']);
  12. $new['vpn'] = intval_or_zero($_POST['s_vpn']);
  13. $new['free'] = intval_or_zero($_POST['s_free']);
  14. $new['dhcp'] = intval_or_zero($_POST['s_dhcp']);
  15. $new['dhcp_lease_time'] = intval_or_zero($_POST['s_lease_time']);
  16. $new['static'] = intval_or_zero($_POST['s_static']);
  17. $new['discovery'] = intval_or_zero($_POST['s_discovery']);
  18. $new['notify'] = intval_or_zero($_POST['s_notify']);
  19. $new['dhcp_update_hostname'] = intval_or_zero($_POST['s_dhcp_update']);
  20. $new['comment'] = trim($_POST['s_comment']);
  21. // === CIDR / RANGE PROCESSING =============================================================================
  22. $range = cidrToRange($new['subnet']);
  23. $first_ip = $range[0];
  24. $last_ip = $range[1];
  25. $cidr = $range[2][1] ?? null;
  26. $new['subnet'] = ($cidr && $cidr <= 32) ? "$first_ip/$cidr" : '';
  27. $new['ip_int_start'] = ip2long($first_ip);
  28. $new['ip_int_stop'] = ip2long($last_ip);
  29. // === GATEWAY ===============================================================================================
  30. $gateway_fallback = ip2long($range[5]);
  31. $new['gateway'] = get_dhcp_gateway($_POST['s_gateway'], $gateway_fallback);
  32. // === GATEWAY VALIDATION ===================================================================================
  33. if ( !$new['gateway'] || $new['gateway'] <= $new['ip_int_start'] || $new['gateway'] >= $new['ip_int_stop'] ) {
  34. // если введённый gateway не подходит — используем дефолтный
  35. $new['gateway'] = $gateway_fallback;
  36. }
  37. // === DHCP RANGE VALIDATION ================================================================================
  38. $dhcp_start = ip2long(trim($_POST['s_dhcp_start']));
  39. $dhcp_stop = ip2long(trim($_POST['s_dhcp_stop']));
  40. if (!validate_dhcp_range(
  41. $dhcp_start,
  42. $dhcp_stop,
  43. $new['ip_int_start'],
  44. $new['ip_int_stop']
  45. )) {
  46. // fallback пул
  47. $dhcp_start = ip2long($range[3]);
  48. $dhcp_stop = ip2long($range[4]);
  49. }
  50. $new['dhcp_start'] = $dhcp_start;
  51. $new['dhcp_stop'] = $dhcp_stop;
  52. // === MODE DEPENDENCY RULES ================================================================================
  53. if ($new['hotspot']) {
  54. $new['dhcp_update_hostname'] = 0;
  55. $new['discovery'] = 0;
  56. $new['vpn'] = 0;
  57. }
  58. if ($new['vpn']) {
  59. $new['discovery'] = 0;
  60. $new['dhcp'] = 0;
  61. }
  62. if ($new['office']) {
  63. $new['free'] = 0;
  64. }
  65. if (!$new['office']) {
  66. $new['discovery'] = 0;
  67. $new['dhcp'] = 0;
  68. $new['static'] = 0;
  69. $new['dhcp_update_hostname'] = 0;
  70. $new['gateway'] = 0;
  71. $new['dhcp_start'] = 0;
  72. $new['dhcp_stop'] = 0;
  73. }
  74. update_record($db_link, "subnets", "id='$id'", $new);
  75. header("Location: /admin/customers/index-subnets.php");
  76. exit;
  77. }
  78. unset($_POST);
  79. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/header.php");
  80. print_control_submenu($page_url);
  81. $sSQL = "SELECT * FROM subnets WHERE id=$id";
  82. $subnet_info = get_record_sql($db_link, $sSQL);
  83. ?>
  84. <div id="cont">
  85. <?php if (!empty($_SESSION[$page_url]['msg'])): ?>
  86. <div id="msg"><?php echo $_SESSION[$page_url]['msg']; ?></div>
  87. <?php unset($_SESSION[$page_url]['msg']); ?>
  88. <?php endif; ?>
  89. <br>
  90. <form name="def" action="editsubnet.php?id=<?php echo $id; ?>" method="post">
  91. <input type="hidden" name="id" value="<?php echo $id; ?>">
  92. <table class="data">
  93. <tr>
  94. <td></td>
  95. <td><b><?php echo WEB_network_subnet; ?></b></td>
  96. <td class="data">
  97. <input type="text" name="s_subnet" value="<?php echo $subnet_info['subnet']; ?>" size="18">
  98. </td>
  99. <td>
  100. <button name="s_save" value="save"><?php echo WEB_btn_save; ?></button>
  101. </td>
  102. </tr>
  103. <tr>
  104. <td></td>
  105. <td><b><?php echo WEB_cell_comment; ?></b></td>
  106. <td colspan="2" class="data">
  107. <input type="text" name="s_comment" value="<?php echo $subnet_info['comment']; ?>">
  108. </td>
  109. </tr>
  110. <tr>
  111. <td></td>
  112. <td><b><?php echo WEB_network_vlan; ?></b></td>
  113. <td colspan="2" class="data">
  114. <input type="text" name="s_vlan" value="<?php echo $subnet_info['vlan_tag']; ?>" pattern="^(409[0-6]|(40[0-8]|[1-3]\d\d|[1-9]\d|[1-9])\d|[1-9])$">
  115. </td>
  116. </tr>
  117. <tr>
  118. <td></td>
  119. <td><b><?php echo WEB_network_gateway; ?></b></td>
  120. <?php
  121. $cell_disabled = '';
  122. $cl = 'data';
  123. if ($subnet_info['office'] and !$subnet_info['vpn']) {
  124. $default_range = cidrToRange($subnet_info['subnet']);
  125. if (!isset($subnet_info['dhcp_start']) or !($subnet_info['dhcp_start'] > 0)) {
  126. $subnet_info['dhcp_start'] = ip2long($default_range[3]);
  127. }
  128. if (!isset($subnet_info['dhcp_stop']) or !($subnet_info['dhcp_stop'] > 0)) {
  129. $subnet_info['dhcp_stop'] = ip2long($default_range[4]);
  130. }
  131. } else {
  132. $cell_disabled = 'readonly';
  133. $cl = 'down';
  134. }
  135. ?>
  136. <td colspan="2" class="<?php echo $cl; ?>">
  137. <input type="text" name="s_gateway" value="<?php echo long2ip($subnet_info['gateway']); ?>" size="15" <?php echo $cell_disabled; ?>>
  138. </td>
  139. </tr>
  140. <tr>
  141. <td></td>
  142. <td><b><?php echo WEB_network_use_dhcp; ?></b></td>
  143. <td colspan="2" class="<?php echo $subnet_info['dhcp'] ? 'up' : 'data'; ?>">
  144. <?php print_qa_select("s_dhcp", $subnet_info['dhcp']); ?>
  145. </td>
  146. </tr>
  147. <tr>
  148. <td></td>
  149. <td><b><?php echo WEB_network_static; ?></b></td>
  150. <td colspan="2" class="<?php echo $subnet_info['static'] ? 'up' : 'data'; ?>">
  151. <?php print_qa_select("s_static", $subnet_info['static']); ?>
  152. </td>
  153. </tr>
  154. <tr>
  155. <td></td>
  156. <td><b><?php echo WEB_network_dhcp_first; ?></b></td>
  157. <td colspan="2" class="data">
  158. <input type="text" name="s_dhcp_start" value="<?php echo long2ip($subnet_info['dhcp_start']); ?>" size="15" <?php echo $cell_disabled; ?>>
  159. </td>
  160. </tr>
  161. <tr>
  162. <td></td>
  163. <td><b><?php echo WEB_network_dhcp_last; ?></b></td>
  164. <td colspan="2" class="data">
  165. <input type="text" name="s_dhcp_stop" value="<?php echo long2ip($subnet_info['dhcp_stop']); ?>" size="15" <?php echo $cell_disabled; ?>>
  166. </td>
  167. </tr>
  168. <tr>
  169. <td></td>
  170. <td><b><?php echo WEB_network_dhcp_leasetime; ?></b></td>
  171. <td colspan="2" class="data">
  172. <input type="text" name="s_lease_time" value="<?php echo $subnet_info['dhcp_lease_time']; ?>" size="3" <?php echo $cell_disabled; ?>>
  173. </td>
  174. </tr>
  175. <tr>
  176. <td></td>
  177. <td><b><?php echo WEB_network_office_subnet; ?></b></td>
  178. <?php
  179. $row_cl = $subnet_info['office'] ? 'data' : 'down';
  180. $cl = $subnet_info['office'] ? 'up' : 'data';
  181. ?>
  182. <td colspan="2" class="<?php echo $cl; ?>">
  183. <?php print_qa_select("s_office", $subnet_info['office']); ?>
  184. </td>
  185. </tr>
  186. <tr>
  187. <td></td>
  188. <td><b><?php echo WEB_network_hotspot; ?></b></td>
  189. <?php
  190. $cl = ($row_cl === 'data' and $subnet_info['hotspot']) ? 'up' : $row_cl;
  191. ?>
  192. <td colspan="2" class="<?php echo $cl; ?>">
  193. <?php print_qa_select_ext("s_hotspot", $subnet_info['hotspot'], !$subnet_info['office']); ?>
  194. </td>
  195. </tr>
  196. <tr>
  197. <td></td>
  198. <td><b><?php echo WEB_network_vpn; ?></b></td>
  199. <?php
  200. $cl = ($row_cl === 'data' and $subnet_info['vpn']) ? 'up' : $row_cl;
  201. ?>
  202. <td colspan="2" class="<?php echo $cl; ?>">
  203. <?php print_qa_select_ext("s_vpn", $subnet_info['vpn'], !$subnet_info['office']); ?>
  204. </td>
  205. </tr>
  206. <tr>
  207. <td></td>
  208. <td><b><?php echo WEB_network_free; ?></b></td>
  209. <td colspan="2" class="<?php echo $subnet_info['free'] ? 'up' : $row_cl; ?>">
  210. <?php print_qa_select("s_free", $subnet_info['free']); ?>
  211. </td>
  212. </tr>
  213. <tr>
  214. <td></td>
  215. <td><b><?php echo WEB_network_dyndns; ?></b></td>
  216. <?php
  217. $cl = ($row_cl === 'data' and $subnet_info['dhcp_update_hostname']) ? 'up' : $row_cl;
  218. ?>
  219. <td colspan="2" class="<?php echo $cl; ?>">
  220. <?php print_qa_select_ext("s_dhcp_update", $subnet_info['dhcp_update_hostname'], !$subnet_info['office']); ?>
  221. </td>
  222. </tr>
  223. <tr>
  224. <td></td>
  225. <td><b><?php echo WEB_network_discovery; ?></b></td>
  226. <?php
  227. $cl = ($row_cl === 'data' and $subnet_info['discovery']) ? 'up' : $row_cl;
  228. ?>
  229. <td colspan="2" class="<?php echo $cl; ?>">
  230. <?php print_qa_select_ext("s_discovery", $subnet_info['discovery'], !$subnet_info['office']); ?>
  231. </td>
  232. </tr>
  233. <tr>
  234. <td></td>
  235. <td><b><?php echo WEB_network_notify; ?></b></td>
  236. <td colspan="2" class="data">
  237. <?php print renderNotifyCombobox("s_notify", $subnet_info['notify']); ?>
  238. </td>
  239. </tr>
  240. </table>
  241. <?php if (isset($msg_error) && $msg_error): ?>
  242. <div id="msg"><b><?php echo $msg_error; ?></b></div>
  243. <?php endif; ?>
  244. </form>
  245. <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.php"); ?>