editsubnet.php 12 KB

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