1
0

edit_l3int.php 5.9 KB

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