edit_l3int.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. $snmp = getSnmpAccess($device);
  7. $user_info = get_record_sql($db_link, "SELECT * FROM user_list WHERE id = ?", [$device['user_id']]);
  8. $int_list = getIpAdEntIfIndex($db_link, $device['ip'], $snmp);
  9. // Удаление L3-интерфейсов
  10. if (getPOST("s_remove") !== null) {
  11. $s_id = getPOST("s_id", null, []);
  12. if (!empty($s_id) && is_array($s_id)) {
  13. foreach ($s_id as $val) {
  14. $val = trim($val);
  15. if ($val === '') continue;
  16. delete_record($db_link, "device_l3_interfaces", "id = ?", [(int)$val]);
  17. }
  18. }
  19. header("Location: " . $_SERVER["REQUEST_URI"]);
  20. exit;
  21. }
  22. // Сохранение ОТМЕЧЕННЫХ L3-интерфейсов
  23. if (getPOST("s_save") !== null) {
  24. $selected_ids = getPOST("s_id", null, []); // отмеченные чекбоксы
  25. $all_ids = getPOST("n_id", null, []); // все ID
  26. $types = getPOST("s_type", null, []);
  27. if (!empty($selected_ids) && is_array($selected_ids)) {
  28. $selected_ids = array_map('intval', $selected_ids);
  29. $selected_set = array_flip($selected_ids);
  30. foreach ($all_ids as $i => $id) {
  31. $id = (int)$id;
  32. if ($id <= 0 || !isset($selected_set[$id])) continue;
  33. $new = [
  34. 'interface_type' => (int)($types[$i] ?? 0)
  35. ];
  36. update_record($db_link, "device_l3_interfaces", "id = ?", $new, [$id]);
  37. }
  38. }
  39. header("Location: " . $_SERVER["REQUEST_URI"]);
  40. exit;
  41. }
  42. // Создание нового L3-интерфейса
  43. if (getPOST("s_create") !== null) {
  44. $create_name = trim(getPOST("s_create_name", null, ''));
  45. if ($create_name !== '') {
  46. $parts = explode(";", $create_name);
  47. if (count($parts) >= 3) {
  48. $new = [
  49. 'name' => preg_replace('/"/', '', trim($parts[0])),
  50. 'snmpin' => trim($parts[1]),
  51. 'interface_type' => (int)trim($parts[2]),
  52. 'device_id' => $id
  53. ];
  54. insert_record($db_link, "device_l3_interfaces", $new);
  55. }
  56. }
  57. header("Location: " . $_SERVER["REQUEST_URI"]);
  58. exit;
  59. }
  60. unset($_POST);
  61. // Автоисправление интерфейсов
  62. $t_l3_interface = get_records_sql($db_link, "SELECT * FROM device_l3_interfaces WHERE device_id = ? ORDER BY name", [$id]);
  63. $int_by_name = [];
  64. foreach ($int_list as $row) {
  65. $row['name'] = preg_replace('/"/', '', $row['name']);
  66. $int_by_name[$row['name']] = $row;
  67. }
  68. $fixed = 0;
  69. // Исправление snmpin по имени
  70. foreach ($t_l3_interface as $row) {
  71. if (empty($row['snmpin']) && !empty($int_by_name[$row['name']])) {
  72. update_record($db_link, 'device_l3_interfaces', 'id = ?', ['snmpin' => $int_by_name[$row['name']]['index']], [$row['id']]);
  73. $fixed = 1;
  74. }
  75. }
  76. // Обновление имени по snmpin
  77. foreach ($t_l3_interface as $row) {
  78. if (!empty($int_list[$row['snmpin']]) && $int_list[$row['snmpin']]['name'] !== $row['name']) {
  79. update_record($db_link, 'device_l3_interfaces', 'id = ?', ['name' => $int_list[$row['snmpin']]['name']], [$row['id']]);
  80. $fixed = 1;
  81. }
  82. }
  83. if ($fixed) {
  84. $t_l3_interface = get_records_sql($db_link, "SELECT * FROM device_l3_interfaces WHERE device_id = ? ORDER BY name", [$id]);
  85. }
  86. require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/header.php");
  87. print_device_submenu($page_url);
  88. print_editdevice_submenu($page_url, $id, $device['device_type'], $user_info['login']);
  89. ?>
  90. <div id="contsubmenu">
  91. <br>
  92. <form name="def" action="edit_l3int.php?id=<?php echo $id; ?>" method="post">
  93. <?php echo WEB_list_l3_interfaces . "<b>"; print_url($device['device_name'], "/admin/devices/editdevice.php?id=$id"); ?></b> <br>
  94. <table class="data">
  95. <tr align="center">
  96. <td><input type="checkbox" onClick="checkAll(this.checked);"></td>
  97. <td width=30><b>id</b></td>
  98. <td><b><?php echo WEB_cell_name; ?></b></td>
  99. <td><b><?php echo WEB_cell_type; ?></b></td>
  100. <td>
  101. <!-- Кнопки управления справа -->
  102. <div style="text-align: right; white-space: nowrap;">
  103. <input type="submit" name="s_save" value="<?php echo WEB_btn_save; ?>">
  104. <input type="submit"
  105. onclick="return confirm('<?php echo WEB_msg_delete; ?>?')"
  106. name="s_remove"
  107. value="<?php echo WEB_btn_remove; ?>"
  108. style="margin-left: 8px;">
  109. </div>
  110. </td>
  111. </tr>
  112. <?php
  113. foreach ($t_l3_interface as $row) {
  114. print "<tr align=center>\n";
  115. print "<td class=\"data\" style='padding:0'><input type=\"checkbox\" name=\"s_id[]\" value=\"{$row['id']}\"></td>\n";
  116. print "<td class=\"data\"><input type=\"hidden\" name=\"n_id[]\" value=\"{$row['id']}\">{$row['snmpin']}</td>\n";
  117. print "<td class=\"data\">" . htmlspecialchars($row['name']) . '/' . htmlspecialchars($int_list[$row['snmpin']]['ip'] ?? '') . "</td>\n";
  118. print "<td class=\"data\">";
  119. print_qa_l3int_select('s_type[]', $row['interface_type']);
  120. print "</td>\n";
  121. print "<td class=\"data\"></td>\n";
  122. print "</tr>\n";
  123. }
  124. ?>
  125. <tr>
  126. <td colspan=4>
  127. <?php
  128. echo WEB_l3_interface_add;
  129. print_add_dev_interface($db_link, $id, $int_list, 's_create_name');
  130. ?>
  131. </td>
  132. <td>
  133. <input type="submit" name="s_create" value="<?php echo WEB_btn_add; ?>">
  134. </td>
  135. </tr>
  136. </table>
  137. </form>
  138. <?php
  139. require_once ($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.php");
  140. ?>