editdevice.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. // if ($new['queue_enabled']) { $new['user_acl'] = 1; }
  99. // Расположение
  100. $new['building_id'] = (int)getPOST("f_building_id", null, 0);
  101. // Доступ
  102. $new['login'] = trim(getPOST("f_login", null, ''));
  103. $f_password = getPOST("f_password", null, '');
  104. if ($f_password !== '' && !preg_match('/^\*+$/', $f_password)) {
  105. $new['password'] = crypt_string($f_password);
  106. }
  107. $new['protocol'] = (int)getPOST("f_protocol", null, 0);
  108. $new['control_port'] = (int)getPOST("f_control_port", null, 0);
  109. $new['netflow_save'] = (int)getPOST("f_save_netflow", null, 0);
  110. // Discovery
  111. $new['discovery'] = trim(getPOST("f_discovery", null, 0));
  112. // Nagios
  113. $f_nagios = (int)getPOST("f_nagios", null, -1);
  114. if ($f_nagios !== -1) {
  115. $new['nagios'] = $f_nagios;
  116. if ($new['nagios'] == 0) {
  117. $new['nagios_status'] = 'UP';
  118. }
  119. } else {
  120. if (!empty($cur_auth)) {
  121. $new['nagios'] = 0;
  122. $new['nagios_status'] = $cur_auth['nagios_status'];
  123. }
  124. }
  125. // === ЗАВИСИМОСТИ ПО ТИПУ УСТРОЙСТВА ========================================
  126. if ($new['device_type'] == 0 || $new['protocol'] < 0) {
  127. $new['queue_enabled'] = 0;
  128. $new['connected_user_only'] = 1;
  129. $new['user_acl'] = 0;
  130. }
  131. // === СОХРАНЕНИЕ =============================================================
  132. update_record($db_link, "devices", "id = ?", $new, [$id]);
  133. header("Location: " . $_SERVER["REQUEST_URI"]);
  134. exit;
  135. }
  136. unset($_POST);
  137. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/header.php");
  138. print_device_submenu($page_url);
  139. print_editdevice_submenu($page_url, $id, $device['device_type'], $user_info['login']);
  140. ?>
  141. <div id="contsubmenu">
  142. <?php
  143. if (!empty($_GET['status'])) {
  144. if ($_GET['status'] === 'locked') {
  145. print '<div id="msg">' . WEB_device_locked . '</div>';
  146. }
  147. }
  148. ?>
  149. <form name="def" action="editdevice.php?id=<?php echo $id; ?>" method="post">
  150. <table class="data">
  151. <tr>
  152. <td><?php echo WEB_cell_name; ?></td>
  153. <td><?php echo WEB_cell_ip; ?></td>
  154. <td><?php echo WEB_cell_type; ?></td>
  155. <?php
  156. if ($device['device_type'] <= 2) {
  157. print "<td>" . WEB_device_port_count . "</td>";
  158. } else {
  159. print "<td></td>";
  160. }
  161. print "</tr>";
  162. print "<tr>\n";
  163. print "<td class='data'><a href=/admin/users/edituser.php?id=" . $device['user_id'] . ">" . $user_info['login'] . "</a></td>\n";
  164. print "<td class='data'>";
  165. print_device_ip_select($db_link, 'f_ip', $device['ip'], $device['user_id']);
  166. print "</td>\n";
  167. print "<td class='data'>";
  168. print_devtype_select($db_link, 'f_devtype_id', $device['device_type']);
  169. print "</td>\n";
  170. if ($device['device_type'] <= 2) {
  171. print "<td class='data'><input type='text' name='f_port_count' value='" . $device['port_count'] . "' size=5></td>\n";
  172. } else {
  173. print "<td></td>";
  174. }
  175. print "</tr>\n";
  176. ?>
  177. </tr>
  178. <td colspan=2><?php echo WEB_cell_host_model; ?></td>
  179. <td><?php echo WEB_cell_host_firmware; ?></td>
  180. <td><?php echo WEB_cell_sn; ?></td>
  181. <?php
  182. //common information
  183. print "<tr>\n";
  184. print "<td class='data' colspan=2>";
  185. print_device_model_select($db_link, 'f_device_model_id', $device['device_model_id']);
  186. print "</td>\n";
  187. print "<td class='data' ><input type='text' name='f_firmware' value='" . $device['firmware'] . "' class='full-width'></td>\n";
  188. print "<td class='data' ><input type='text' name='f_sn' value='" . $device['sn'] . "' class='full-width'></td>\n";
  189. print "</tr>\n";
  190. print "<tr><td colspan=2>" . WEB_location_name . "</td><td colspan=2>" . WEB_cell_description . "</td>";
  191. print "</tr><tr>";
  192. print "<td class='data'>";
  193. print_building_select($db_link, 'f_building_id', $device['building_id']);
  194. print "</td>\n";
  195. print "<td class='data' colspan=3><input type='text' size=50 name='f_description' value='" . $device['description'] . "' class='full-width'></td>\n";
  196. print "</tr>";
  197. //print gateway settings
  198. if ($device['device_type'] == 2) {
  199. print "<tr><td>"; print_url(WEB_device_access_control,"/admin/devices/edit_gw_instances.php?id=$id", 'linkButton'); print "</td><td>" . WEB_device_dhcp_server . "</td><td>" . WEB_device_queues_enabled . "</td><td>" . WEB_device_connected_only . "</td></tr>";
  200. print "<tr>";
  201. print "<td class='data'>";
  202. print_qa_select('f_user_acl', $device['user_acl']);
  203. print "</td>\n";
  204. print "<td class='data'>";
  205. print_qa_select('f_dhcp', $device['dhcp']);
  206. print "</td>\n";
  207. print "<td class='data'>";
  208. print_qa_select('f_queue_enabled', $device['queue_enabled']);
  209. print "</td>\n";
  210. print "<td class='data'>";
  211. print_qa_select('f_connected_user_only', $device['connected_user_only']);
  212. print "</td>\n";
  213. print "</tr>\n";
  214. print "<tr><td colspan=2>";
  215. print_url(WEB_list_l3_interfaces, "/admin/devices/edit_l3int.php?id=$id",'linkButton');
  216. print "</td>";
  217. print "<td colspan=2>";
  218. print_url(WEB_list_gateway_subnets, "/admin/devices/edit_gw_subnets.php?id=$id", 'linkButton');
  219. print "</td></tr>";
  220. print "<tr><td colspan=2 class='data'>";
  221. print get_l3_interfaces($db_link, $device['id']);
  222. print "</td>";
  223. print "<td colspan=2 class='data'>";
  224. print get_gw_subnets($db_link, $device['id']);
  225. print "</td></tr>";
  226. }
  227. //print router settings
  228. if ($device['device_type'] == 0) {
  229. print "<tr><td>" . WEB_device_dhcp_server . "</td><td></td><td></td><td></td></tr>";
  230. print "<tr>";
  231. print "<td class='data'>";
  232. print_qa_select('f_dhcp', $device['dhcp']);
  233. print "<td class='data' colspan=4>";
  234. print_url(WEB_list_l3_networks, "/admin/devices/edit_gw_subnets.php?id=$id", 'linkButton');
  235. print "</tr>\n";
  236. }
  237. //for all active network devices
  238. if ($device['device_type'] <= 2) {
  239. //cli access settings
  240. 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>";
  241. print "<tr>";
  242. print "<td class='data'><input type='text' name='f_login' value=" . $device['login'] . "></td>\n";
  243. print "<td class='data'><input type='text' name='f_password' value='********'></td>\n";
  244. print "<td class='data'>";
  245. print_control_proto_select('f_protocol', $device['protocol']);
  246. print "</td>\n";
  247. print "<td class='data'><input type='text' name='f_control_port' value=" . $device['control_port'] . "></td>\n";
  248. print "</tr>";
  249. //snmp settings & discovery & nagios
  250. print "<tr><td>" . WEB_network_discovery . "</td><td>" . WEB_nagios . "</td><td>" . WEB_device_save_netflow . "</td><td></td></tr>";
  251. print "<tr>";
  252. print "<td class='data'>";
  253. print_qa_select('f_discovery', $device['discovery']);
  254. print "</td>\n";
  255. print "<td class='data'>";
  256. print_qa_select('f_nagios', $device['nagios']);
  257. print "</td>\n";
  258. print "<td class='data'>";
  259. print_qa_select('f_save_netflow', $device['netflow_save']);
  260. print "</td>\n";
  261. print "<td class='data'></td></tr>";
  262. }
  263. if ($device['snmp_version'] == 3) {
  264. //snmp settings
  265. print "<tr><td>" . WEB_snmp_version . "</td><td>" . WEB_snmp_v3_auth_proto . "</td><td>" . WEB_snmp_v3_priv_proto . "</td><td></td></tr>";
  266. print "<tr><td class='data'>";
  267. print_snmp_select('f_snmp_version', $device['snmp_version']);
  268. print "</td>\n";
  269. print "<td class='data'>";
  270. print_snmp_auth_proto_select('f_snmp3_auth_proto', $device['snmp3_auth_proto']);
  271. print "</td>\n";
  272. print "<td class='data'>";
  273. print_snmp_priv_proto_select('f_snmp3_priv_proto', $device['snmp3_priv_proto']);
  274. print "</td>\n";
  275. print "<td class='data'></td>";
  276. print "</tr>";
  277. 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>";
  278. print "</tr><tr>";
  279. print "<td class='data'><input type='text' name='f_snmp3_user_ro' value=" . $device['snmp3_user_ro'] . "></td>\n";
  280. print "<td class='data'><input type='text' name='f_snmp3_user_ro_password' minlength='8' value=" . $device['snmp3_user_ro_password'] . "></td>\n";
  281. print "<td class='data'><input type='text' name='f_snmp3_user_rw' value=" . $device['snmp3_user_rw'] . "></td>\n";
  282. print "<td class='data'><input type='text' name='f_snmp3_user_rw_password' minlength='8' value=" . $device['snmp3_user_rw_password'] . "></td>\n";
  283. print "</tr>\n";
  284. } else {
  285. print "<tr><td>" . WEB_snmp_version . "</td><td></td><td></td><td></td></tr>";
  286. print "<tr><td class='data'>";
  287. print_snmp_select('f_snmp_version', $device['snmp_version']);
  288. print "</td><td class='data' colspan=3></td>\n";
  289. print "</tr>";
  290. if ($device['snmp_version'] > 0) {
  291. print "<tr><td>" . WEB_snmp_community_ro . "</td><td>" . WEB_snmp_community_rw . "</td><td></td><td></td></tr>";
  292. print "<tr>\n";
  293. print "<td class='data'><input type='text' name='f_community' value=" . $device['community'] . "></td>\n";
  294. print "<td class='data'><input type='text' name='f_rw_community' value=" . $device['rw_community'] . "></td>\n";
  295. print "<td class='data' colspan=2></td>";
  296. print "</tr>";
  297. }
  298. }
  299. //save button
  300. if ($device['snmp_version'] > 0) {
  301. print "<tr><td colspan=2>" . $device['ip'] . "::" . get_device_model_name($db_link, $device['device_model_id']) . "</td>";
  302. print "<td><button name='mac_walk' onclick=\"window.open('mactable.php?id=" . $id . "')\">" . WEB_device_mac_table . "</button>";
  303. print "<button name='port_walk' onclick=\"window.open('snmpwalk.php?id=" . $id . "')\">" . WEB_device_walk_port_list . "</button></td>";
  304. } else {
  305. print "<tr><td colspan=3>" . $device['ip'] . "::" . get_device_model_name($db_link, $device['device_model_id']) . "</td>";
  306. }
  307. print "<td align=right><input type='submit' id='editdevice' name='editdevice' value='" . WEB_btn_save . "'></td></tr>";
  308. print "</table>\n";
  309. ?>
  310. </form>
  311. <script>
  312. document.getElementById('f_devtype_id').addEventListener('change', function(event) {
  313. const buttonApply = document.getElementById('editdevice');
  314. buttonApply.click();
  315. });
  316. document.getElementById('f_snmp_version').addEventListener('change', function(event) {
  317. const buttonApply = document.getElementById('editdevice');
  318. buttonApply.click();
  319. });
  320. </script>
  321. <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.php"); ?>