1
0

editdevice.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. $device = get_record($db_link, 'devices', "id = ?", [$id]);
  6. $user_info = get_record_sql($db_link, "SELECT * FROM user_list WHERE id = ?", [$device['user_id']]);
  7. if (getPOST("editdevice") !== null && isset($id)) {
  8. // === УПРАВЛЕНИЕ ПОРТАМИ ====================================================
  9. $sw_ports = (int)getPOST("f_port_count", null, 0);
  10. $sSQL = "SELECT COUNT(id) FROM device_ports WHERE device_ports.device_id = ?";
  11. $d_ports = (int)get_single_field($db_link, $sSQL, [$id]);
  12. if ($d_ports != $sw_ports) {
  13. LOG_DEBUG($db_link, "Device id: $id changed port count!");
  14. if ($sw_ports > $d_ports) {
  15. $start_port = $d_ports + 1;
  16. LOG_DEBUG($db_link, "Device id: $id add connection for port from $start_port to $sw_ports.");
  17. for ($port = $start_port; $port <= $sw_ports; $port++) {
  18. insert_record($db_link, "device_ports", [
  19. 'device_id' => $id,
  20. 'snmp_index' => $port,
  21. 'port' => $port
  22. ]);
  23. }
  24. }
  25. if ($sw_ports < $d_ports) {
  26. LOG_DEBUG($db_link, "Device id: $id remove connection for port from $d_ports to $sw_ports");
  27. for ($port = $d_ports; $port > $sw_ports; $port--) {
  28. $port_id = get_id_record($db_link, 'device_ports', "device_id = ? AND port = ?", [$id, $port]);
  29. if ($port_id) {
  30. delete_record($db_link, "device_ports", "id = ?", [$port_id]);
  31. delete_records($db_link, "connections", "port_id = ?", [$port_id]);
  32. } else {
  33. LOG_DEBUG($db_link, "Device id: $id port_id not found for port: $port!");
  34. }
  35. }
  36. }
  37. }
  38. // === ОСНОВНЫЕ ДАННЫЕ УСТРОЙСТВА ============================================
  39. $new = [];
  40. $cur_device = get_record_sql($db_link, "SELECT * FROM devices WHERE id = ?", [$id]);
  41. // IP-адрес
  42. $f_ip = trim(getPOST("f_ip", null, ''));
  43. if ($f_ip !== '') {
  44. $new['ip'] = $f_ip;
  45. $new['ip_int'] = ip2long($f_ip);
  46. $cur_auth = get_record_sql($db_link, "SELECT * FROM user_auth WHERE deleted = 0 AND ip = ?", [$f_ip]);
  47. }
  48. // Модель устройства
  49. $f_device_model_id = (int)getPOST("f_device_model_id", null, 0);
  50. if ($f_device_model_id > 0) {
  51. $new['device_model_id'] = $f_device_model_id;
  52. $new['vendor_id'] = get_device_model_vendor($db_link, $f_device_model_id);
  53. }
  54. // Количество портов
  55. $new['port_count'] = $sw_ports;
  56. // Тип устройства
  57. $new['device_type'] = (int)getPOST("f_devtype_id", null, 0);
  58. // === УПРАВЛЕНИЕ ЭКЗЕМПЛЯРАМИ ФИЛЬТРОВ ======================================
  59. if ($new['device_type'] == 2) {
  60. // Это шлюз — должен иметь хотя бы один экземпляр
  61. $instances_count = get_count_records($db_link, 'device_filter_instances', 'device_id = ?', [$id]);
  62. if (empty($instances_count) || $instances_count == 0) {
  63. // Создаём стандартный экземпляр (ID=1)
  64. insert_record($db_link, "device_filter_instances", [
  65. 'instance_id' => 1,
  66. 'device_id' => $id
  67. ]);
  68. }
  69. } else {
  70. // Не шлюз — удаляем все экземпляры
  71. if ($device['device_type'] == 2) {
  72. $instances_count = get_count_records($db_link, 'device_filter_instances', 'device_id = ?', [$id]);
  73. if (!empty($instances_count) && $instances_count > 0) {
  74. delete_records($db_link, 'device_filter_instances', 'device_id = ?', [$id]);
  75. }
  76. }
  77. }
  78. // === ОСТАЛЬНЫЕ ПОЛЯ =========================================================
  79. $new['description'] = trim(getPOST("f_description", null, ''));
  80. $new['sn'] = trim(getPOST("f_sn", null, ''));
  81. $new['firmware'] = trim(getPOST("f_firmware", null, ''));
  82. // SNMP
  83. $new['snmp_version'] = (int)getPOST("f_snmp_version", null, 0);
  84. $new['community'] = substr(trim(getPOST("f_community", null, '')), 0, 50);
  85. $new['snmp3_auth_proto'] = substr(trim(getPOST("f_snmp3_auth_proto", null, '')), 0, 10);
  86. $new['snmp3_priv_proto'] = substr(trim(getPOST("f_snmp3_priv_proto", null, '')), 0, 10);
  87. $new['rw_community'] = substr(trim(getPOST("f_rw_community", null, '')), 0, 50);
  88. $new['snmp3_user_rw'] = substr(trim(getPOST("f_snmp3_user_rw", null, '')), 0, 20);
  89. $new['snmp3_user_ro'] = substr(trim(getPOST("f_snmp3_user_ro", null, '')), 0, 20);
  90. $new['snmp3_user_rw_password']= substr(trim(getPOST("f_snmp3_user_rw_password", null, '')), 0, 20);
  91. $new['snmp3_user_ro_password']= substr(trim(getPOST("f_snmp3_user_ro_password", null, '')), 0, 20);
  92. // ACL и настройки
  93. $new['queue_enabled'] = (int)getPOST("f_queue_enabled", null, 0);
  94. $new['connected_user_only'] = (int)getPOST("f_connected_user_only", null, 0);
  95. $new['dhcp'] = (int)getPOST("f_dhcp", null, 0);
  96. $new['user_acl'] = (int)getPOST("f_user_acl", null, 0);
  97. // Расположение
  98. $new['building_id'] = (int)getPOST("f_building_id", null, 0);
  99. // Доступ
  100. $new['login'] = trim(getPOST("f_login", null, ''));
  101. $f_password = getPOST("f_password", null, '');
  102. if ($f_password !== '' && !preg_match('/^\*+$/', $f_password)) {
  103. $new['password'] = crypt_string($f_password);
  104. }
  105. $new['protocol'] = (int)getPOST("f_protocol", null, 0);
  106. $new['control_port'] = (int)getPOST("f_control_port", null, 0);
  107. $new['netflow_save'] = (int)getPOST("f_save_netflow", null, 0);
  108. // Discovery
  109. $new['discovery'] = trim(getPOST("f_discovery", null, 0));
  110. // Nagios
  111. $f_nagios = (int)getPOST("f_nagios", null, -1);
  112. if ($f_nagios !== -1) {
  113. $new['nagios'] = $f_nagios;
  114. if ($new['nagios'] == 0) {
  115. $new['nagios_status'] = 'UP';
  116. }
  117. } else {
  118. if (!empty($cur_auth)) {
  119. $new['nagios'] = 0;
  120. $new['nagios_status'] = $cur_auth['nagios_status'];
  121. }
  122. }
  123. // === ЗАВИСИМОСТИ ПО ТИПУ УСТРОЙСТВА ========================================
  124. if ($new['device_type'] == 0 || $new['protocol'] < 0) {
  125. $new['queue_enabled'] = 0;
  126. $new['connected_user_only'] = 1;
  127. $new['user_acl'] = 0;
  128. }
  129. // === СОХРАНЕНИЕ =============================================================
  130. update_record($db_link, "devices", "id = ?", $new, [$id]);
  131. header("Location: " . $_SERVER["REQUEST_URI"]);
  132. exit;
  133. }
  134. unset($_POST);
  135. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/header.php");
  136. print_device_submenu($page_url);
  137. print_editdevice_submenu($page_url, $id, $device['device_type'], $user_info['login']);
  138. ?>
  139. <div id="contsubmenu">
  140. <?php
  141. if (!empty($_GET['status'])) {
  142. if ($_GET['status'] === 'locked') {
  143. print '<div id="msg">' . WEB_device_locked . '</div>';
  144. }
  145. }
  146. ?>
  147. <form name="def" action="editdevice.php?id=<?php echo $id; ?>" method="post">
  148. <table class="data">
  149. <tr>
  150. <td><?php echo WEB_cell_name; ?></td>
  151. <td><?php echo WEB_cell_ip; ?></td>
  152. <td><?php echo WEB_cell_type; ?></td>
  153. <?php
  154. if ($device['device_type'] <= 2) {
  155. print "<td>" . WEB_device_port_count . "</td>";
  156. } else {
  157. print "<td></td>";
  158. }
  159. print "</tr>";
  160. print "<tr>\n";
  161. print "<td class='data'><a href=/admin/users/edituser.php?id=" . $device['user_id'] . ">" . $user_info['login'] . "</a></td>\n";
  162. print "<td class='data'>";
  163. print_device_ip_select($db_link, 'f_ip', $device['ip'], $device['user_id']);
  164. print "</td>\n";
  165. print "<td class='data'>";
  166. print_devtype_select($db_link, 'f_devtype_id', $device['device_type']);
  167. print "</td>\n";
  168. if ($device['device_type'] <= 2) {
  169. print "<td class='data'><input type='text' name='f_port_count' value='" . $device['port_count'] . "' size=5></td>\n";
  170. } else {
  171. print "<td></td>";
  172. }
  173. print "</tr>\n";
  174. ?>
  175. </tr>
  176. <td colspan=2><?php echo WEB_cell_host_model; ?></td>
  177. <td><?php echo WEB_cell_host_firmware; ?></td>
  178. <td><?php echo WEB_cell_sn; ?></td>
  179. <?php
  180. //common information
  181. print "<tr>\n";
  182. print "<td class='data' colspan=2>";
  183. print_device_model_select($db_link, 'f_device_model_id', $device['device_model_id']);
  184. print "</td>\n";
  185. print "<td class='data' ><input type='text' name='f_firmware' value='" . $device['firmware'] . "' class='full-width'></td>\n";
  186. print "<td class='data' ><input type='text' name='f_sn' value='" . $device['sn'] . "' class='full-width'></td>\n";
  187. print "</tr>\n";
  188. print "<tr><td colspan=2>" . WEB_location_name . "</td><td colspan=2>" . WEB_cell_description . "</td>";
  189. print "</tr><tr>";
  190. print "<td class='data'>";
  191. print_building_select($db_link, 'f_building_id', $device['building_id']);
  192. print "</td>\n";
  193. print "<td class='data' colspan=3><input type='text' size=50 name='f_description' value='" . $device['description'] . "' class='full-width'></td>\n";
  194. print "</tr>";
  195. //print gateway settings
  196. if ($device['device_type'] == 2) {
  197. print "<tr><td>"; print_url(WEB_device_access_control,"/admin/devices/edit_gw_instances.php?id=$id"); print "</td><td>" . WEB_device_dhcp_server . "</td><td>" . WEB_device_queues_enabled . "</td><td>" . WEB_device_connected_only . "</td></tr>";
  198. print "<tr>";
  199. print "<td class='data'>";
  200. print_qa_select('f_user_acl', $device['user_acl']);
  201. print "</td>\n";
  202. print "<td class='data'>";
  203. print_qa_select('f_dhcp', $device['dhcp']);
  204. print "</td>\n";
  205. print "<td class='data'>";
  206. print_qa_select('f_queue_enabled', $device['queue_enabled']);
  207. print "</td>\n";
  208. print "<td class='data'>";
  209. print_qa_select('f_connected_user_only', $device['connected_user_only']);
  210. print "</td>\n";
  211. print "</tr>\n";
  212. print "<tr><td colspan=2>";
  213. print_url(WEB_list_l3_interfaces, "/admin/devices/edit_l3int.php?id=$id");
  214. print "</td>";
  215. print "<td colspan=2>";
  216. print_url(WEB_list_gateway_subnets, "/admin/devices/edit_gw_subnets.php?id=$id");
  217. print "</td></tr>";
  218. print "<tr><td colspan=2 class='data'>";
  219. print get_l3_interfaces($db_link, $device['id']);
  220. print "</td>";
  221. print "<td colspan=2 class='data'>";
  222. print get_gw_subnets($db_link, $device['id']);
  223. print "</td></tr>";
  224. }
  225. //print router settings
  226. if ($device['device_type'] == 0) {
  227. print "<tr><td>" . WEB_device_dhcp_server . "</td><td></td><td></td><td></td></tr>";
  228. print "<tr>";
  229. print "<td class='data'>";
  230. print_qa_select('f_dhcp', $device['dhcp']);
  231. print "<td class='data' colspan=4>";
  232. print_url(WEB_list_l3_networks, "/admin/devices/edit_gw_subnets.php?id=$id");
  233. print "</tr>\n";
  234. }
  235. //for all active network devices
  236. if ($device['device_type'] <= 2) {
  237. //cli access settings
  238. print "<tr><td>" . WEB_cell_login . "</td><td>" . WEB_cell_password . "</td><td>" . WEB_cell_control_proto . "</td><td>" . WEB_cell_control_port . "</td></tr>";
  239. print "<tr>";
  240. print "<td class='data'><input type='text' name='f_login' value=" . $device['login'] . "></td>\n";
  241. print "<td class='data'><input type='text' name='f_password' value='********'></td>\n";
  242. print "<td class='data'>";
  243. print_control_proto_select('f_protocol', $device['protocol']);
  244. print "</td>\n";
  245. print "<td class='data'><input type='text' name='f_control_port' value=" . $device['control_port'] . "></td>\n";
  246. print "</tr>";
  247. //snmp settings & discovery & nagios
  248. print "<tr><td>" . WEB_network_discovery . "</td><td>" . WEB_nagios . "</td><td>" . WEB_device_save_netflow . "</td><td></td></tr>";
  249. print "<tr>";
  250. print "<td class='data'>";
  251. print_qa_select('f_discovery', $device['discovery']);
  252. print "</td>\n";
  253. print "<td class='data'>";
  254. print_qa_select('f_nagios', $device['nagios']);
  255. print "</td>\n";
  256. print "<td class='data'>";
  257. print_qa_select('f_save_netflow', $device['netflow_save']);
  258. print "</td>\n";
  259. print "<td class='data'></td></tr>";
  260. }
  261. if ($device['snmp_version'] == 3) {
  262. //snmp settings
  263. print "<tr><td>" . WEB_snmp_version . "</td><td>" . WEB_snmp_v3_auth_proto . "</td><td>" . WEB_snmp_v3_priv_proto . "</td><td></td></tr>";
  264. print "<tr><td class='data'>";
  265. print_snmp_select('f_snmp_version', $device['snmp_version']);
  266. print "</td>\n";
  267. print "<td class='data'>";
  268. print_snmp_auth_proto_select('f_snmp3_auth_proto', $device['snmp3_auth_proto']);
  269. print "</td>\n";
  270. print "<td class='data'>";
  271. print_snmp_priv_proto_select('f_snmp3_priv_proto', $device['snmp3_priv_proto']);
  272. print "</td>\n";
  273. print "<td class='data'></td>";
  274. print "</tr>";
  275. print "<tr><td>" . WEB_snmp_v3_user_ro . "</td><td>" . WEB_snmp_v3_ro_password . "</td><td>" . WEB_snmp_v3_user_rw . "</td><td>" . WEB_snmp_v3_rw_password . "</td><td></td>";
  276. print "</tr><tr>";
  277. print "<td class='data'><input type='text' name='f_snmp3_user_ro' value=" . $device['snmp3_user_ro'] . "></td>\n";
  278. print "<td class='data'><input type='text' name='f_snmp3_user_ro_password' minlength='8' value=" . $device['snmp3_user_ro_password'] . "></td>\n";
  279. print "<td class='data'><input type='text' name='f_snmp3_user_rw' value=" . $device['snmp3_user_rw'] . "></td>\n";
  280. print "<td class='data'><input type='text' name='f_snmp3_user_rw_password' minlength='8' value=" . $device['snmp3_user_rw_password'] . "></td>\n";
  281. print "</tr>\n";
  282. } else {
  283. print "<tr><td>" . WEB_snmp_version . "</td><td></td><td></td><td></td></tr>";
  284. print "<tr><td class='data'>";
  285. print_snmp_select('f_snmp_version', $device['snmp_version']);
  286. print "</td><td class='data' colspan=3></td>\n";
  287. print "</tr>";
  288. if ($device['snmp_version'] > 0) {
  289. print "<tr><td>" . WEB_snmp_community_ro . "</td><td>" . WEB_snmp_community_rw . "</td><td></td><td></td></tr>";
  290. print "<tr>\n";
  291. print "<td class='data'><input type='text' name='f_community' value=" . $device['community'] . "></td>\n";
  292. print "<td class='data'><input type='text' name='f_rw_community' value=" . $device['rw_community'] . "></td>\n";
  293. print "<td class='data' colspan=2></td>";
  294. print "</tr>";
  295. }
  296. }
  297. //save button
  298. if ($device['snmp_version'] > 0) {
  299. print "<tr><td colspan=2>" . $device['ip'] . "::" . get_device_model_name($db_link, $device['device_model_id']) . "</td>";
  300. print "<td><button name='mac_walk' onclick=\"window.open('mactable.php?id=" . $id . "')\">" . WEB_device_mac_table . "</button>";
  301. print "<button name='port_walk' onclick=\"window.open('snmpwalk.php?id=" . $id . "')\">" . WEB_device_walk_port_list . "</button></td>";
  302. } else {
  303. print "<tr><td colspan=3>" . $device['ip'] . "::" . get_device_model_name($db_link, $device['device_model_id']) . "</td>";
  304. }
  305. print "<td align=right><input type='submit' id='editdevice' name='editdevice' value='" . WEB_btn_save . "'></td></tr>";
  306. print "</table>\n";
  307. ?>
  308. </form>
  309. <script>
  310. document.getElementById('f_devtype_id').addEventListener('change', function(event) {
  311. const buttonApply = document.getElementById('editdevice');
  312. buttonApply.click();
  313. });
  314. document.getElementById('f_snmp_version').addEventListener('change', function(event) {
  315. const buttonApply = document.getElementById('editdevice');
  316. buttonApply.click();
  317. });
  318. </script>
  319. <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.php"); ?>