mirror of
https://github.com/itflow-org/itflow
synced 2026-06-12 04:41:06 +00:00
Networks: Added Import Networks
This commit is contained in:
37
agent/modals/network/network_import.php
Normal file
37
agent/modals/network/network_import.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once '../../../includes/modal_header.php';
|
||||||
|
|
||||||
|
$client_id = intval($_GET['client_id'] ?? 0);
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="modal-header bg-dark">
|
||||||
|
<h5 class="modal-title"><i class="fas fa-fw fa-network-wired mr-2"></i>Import Networks</h5>
|
||||||
|
<button type="button" class="close text-white" data-dismiss="modal">
|
||||||
|
<span>×</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off">
|
||||||
|
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||||
|
<input type="hidden" name="client_id" value="<?= $client_id ?>">
|
||||||
|
|
||||||
|
<div class="modal-body">
|
||||||
|
<p><strong>Format csv file with headings & data:</strong><br>Name, Description, VLAN, Network (CIDR), Gateway, IP Range, Primary DNS, Secondary DNS</p>
|
||||||
|
<hr>
|
||||||
|
<div class="form-group my-4">
|
||||||
|
<input type="file" class="form-control-file" name="file" accept=".csv" required>
|
||||||
|
</div>
|
||||||
|
<hr>
|
||||||
|
<div>Download: <a class="text-bold" href="post.php?download_networks_csv_template=<?= $client_id ?>">sample csv template</a></div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="submit" name="import_networks_csv" class="btn btn-primary text-bold"><i class="fa fa-upload mr-2"></i>Import</button>
|
||||||
|
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fa fa-times mr-2"></i>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
require_once '../../../includes/modal_footer.php';
|
||||||
@@ -79,15 +79,21 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
|||||||
<div class="card-tools">
|
<div class="card-tools">
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/network/network_add.php?<?= $client_url ?>&location_id=<?= $location_filter ?>"><i class="fas fa-plus mr-2"></i>New Network</button>
|
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/network/network_add.php?<?= $client_url ?>&location_id=<?= $location_filter ?>"><i class="fas fa-plus mr-2"></i>New Network</button>
|
||||||
<?php if ($num_rows[0] > 0) { ?>
|
|
||||||
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown"></button>
|
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown"></button>
|
||||||
<div class="dropdown-menu">
|
<div class="dropdown-menu">
|
||||||
|
<?php if ($num_rows[0] > 0) { ?>
|
||||||
<a class="dropdown-item text-dark ajax-modal" href="#"
|
<a class="dropdown-item text-dark ajax-modal" href="#"
|
||||||
data-modal-url="modals/network/network_export.php?<?= $client_url ?>">
|
data-modal-url="modals/network/network_export.php?<?= $client_url ?>">
|
||||||
<i class="fa fa-fw fa-download mr-2"></i>Export
|
<i class="fa fa-fw fa-download mr-2"></i>Export
|
||||||
</a>
|
</a>
|
||||||
|
<?php } ?>
|
||||||
|
<div class="dropdown-divider"></div>
|
||||||
|
<a class="dropdown-item text-dark ajax-modal" href="#"
|
||||||
|
data-modal-url="modals/network/network_import.php?<?= $client_url ?>">
|
||||||
|
<i class="fa fa-fw fa-upload mr-2"></i>Import
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<?php } ?>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ if (isset($_POST['export_networks_csv'])) {
|
|||||||
$file_name_prepend = "$session_company_name-";
|
$file_name_prepend = "$session_company_name-";
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = mysqli_query($mysqli,"SELECT * FROM networks LEFT JOIN client ON client_id = network_client_id WHERE network_archived_at IS NULL $client_query $access_permission_query ORDER BY network_name ASC");
|
$sql = mysqli_query($mysqli,"SELECT * FROM networks LEFT JOIN clients ON client_id = network_client_id WHERE network_archived_at IS NULL $client_query $access_permission_query ORDER BY network_name ASC");
|
||||||
|
|
||||||
$num_rows = mysqli_num_rows($sql);
|
$num_rows = mysqli_num_rows($sql);
|
||||||
|
|
||||||
@@ -227,3 +227,161 @@ if (isset($_POST['export_networks_csv'])) {
|
|||||||
exit;
|
exit;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// Add these two blocks to agent/post/network.php
|
||||||
|
// Place them alongside the existing export_networks_csv block.
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
// CSV Template Download
|
||||||
|
// GET: post.php?download_networks_csv_template=<client_id>
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
if (isset($_GET['download_networks_csv_template'])) {
|
||||||
|
|
||||||
|
$delimiter = ",";
|
||||||
|
$enclosure = '"';
|
||||||
|
$escape = '\\';
|
||||||
|
$filename = "Networks-Template.csv";
|
||||||
|
|
||||||
|
$f = fopen('php://memory', 'w');
|
||||||
|
|
||||||
|
$fields = array('Name', 'Description', 'VLAN', 'Network (CIDR)', 'Gateway', 'IP Range', 'Primary DNS', 'Secondary DNS');
|
||||||
|
fputcsv($f, $fields, $delimiter, $enclosure, $escape);
|
||||||
|
|
||||||
|
// One example row so the user can see expected formatting
|
||||||
|
$example = array('Office LAN', 'Main office network', '10', '192.168.1.0/24', '192.168.1.1', '192.168.1.100-192.168.1.200', '8.8.8.8', '8.8.4.4');
|
||||||
|
fputcsv($f, $example, $delimiter, $enclosure, $escape);
|
||||||
|
|
||||||
|
fseek($f, 0);
|
||||||
|
|
||||||
|
header('Content-Type: text/csv');
|
||||||
|
header('Content-Disposition: attachment; filename="' . $filename . '";');
|
||||||
|
|
||||||
|
fpassthru($f);
|
||||||
|
exit;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
// CSV Import
|
||||||
|
// POST: post.php (name="import_networks_csv")
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
if (isset($_POST['import_networks_csv'])) {
|
||||||
|
|
||||||
|
validateCSRFToken($_POST['csrf_token']);
|
||||||
|
|
||||||
|
enforceUserPermission('module_support', 2);
|
||||||
|
|
||||||
|
$client_id = intval($_POST['client_id']);
|
||||||
|
|
||||||
|
enforceClientAccess();
|
||||||
|
|
||||||
|
$error = false;
|
||||||
|
|
||||||
|
// File provided?
|
||||||
|
if (!empty($_FILES['file']['tmp_name'])) {
|
||||||
|
$file_name = $_FILES['file']['tmp_name'];
|
||||||
|
} else {
|
||||||
|
flash_alert("Please select a file to upload.", 'error');
|
||||||
|
redirect();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check extension
|
||||||
|
$file_extension = strtolower(end(explode('.', $_FILES['file']['name'])));
|
||||||
|
if ($file_extension !== 'csv') {
|
||||||
|
$error = true;
|
||||||
|
flash_alert("Bad file extension — only .csv files are accepted.", 'error');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check not empty
|
||||||
|
elseif ($_FILES['file']['size'] < 1) {
|
||||||
|
$error = true;
|
||||||
|
flash_alert("Bad file size (empty file?).", 'error');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check column count matches the 8-column export/template format
|
||||||
|
else {
|
||||||
|
$f = fopen($file_name, 'r');
|
||||||
|
$f_columns = fgetcsv($f, 1000, ',');
|
||||||
|
fclose($f);
|
||||||
|
|
||||||
|
if (count($f_columns) !== 8) {
|
||||||
|
$error = true;
|
||||||
|
flash_alert("Bad column count — expected 8 columns: Name, Description, VLAN, Network (CIDR), Gateway, IP Range, Primary DNS, Secondary DNS.", 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse and insert
|
||||||
|
if (!$error) {
|
||||||
|
$file = fopen($file_name, 'r');
|
||||||
|
fgetcsv($file, 1000, ','); // Skip header row
|
||||||
|
|
||||||
|
$row_count = 0;
|
||||||
|
$duplicate_count = 0;
|
||||||
|
|
||||||
|
while (($column = fgetcsv($file, 1000, ',')) !== false) {
|
||||||
|
|
||||||
|
$duplicate_detect = 0;
|
||||||
|
|
||||||
|
$name = isset($column[0]) ? sanitizeInput($column[0]) : '';
|
||||||
|
$description = isset($column[1]) ? sanitizeInput($column[1]) : '';
|
||||||
|
$vlan = isset($column[2]) ? intval($column[2]) : 0;
|
||||||
|
$network = isset($column[3]) ? sanitizeInput($column[3]) : '';
|
||||||
|
$gateway = isset($column[4]) ? sanitizeInput($column[4]) : '';
|
||||||
|
$dhcp_range = isset($column[5]) ? sanitizeInput($column[5]) : '';
|
||||||
|
$primary_dns = isset($column[6]) ? sanitizeInput($column[6]) : '';
|
||||||
|
$secondary_dns = isset($column[7]) ? sanitizeInput($column[7]) : '';
|
||||||
|
|
||||||
|
// Skip rows with no name
|
||||||
|
if ($name === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duplicate check — same name + network address for this client
|
||||||
|
$dup_check = mysqli_query($mysqli,
|
||||||
|
"SELECT network_id FROM networks
|
||||||
|
WHERE network_name = '$name'
|
||||||
|
AND network = '$network'
|
||||||
|
AND network_client_id = $client_id
|
||||||
|
AND network_archived_at IS NULL
|
||||||
|
LIMIT 1"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (mysqli_num_rows($dup_check) > 0) {
|
||||||
|
$duplicate_detect = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($duplicate_detect === 0) {
|
||||||
|
mysqli_query($mysqli,
|
||||||
|
"INSERT INTO networks SET
|
||||||
|
network_name = '$name',
|
||||||
|
network_description = '$description',
|
||||||
|
network_vlan = $vlan,
|
||||||
|
network = '$network',
|
||||||
|
network_gateway = '$gateway',
|
||||||
|
network_dhcp_range = '$dhcp_range',
|
||||||
|
network_primary_dns = '$primary_dns',
|
||||||
|
network_secondary_dns = '$secondary_dns',
|
||||||
|
network_client_id = $client_id"
|
||||||
|
);
|
||||||
|
$row_count++;
|
||||||
|
} else {
|
||||||
|
$duplicate_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($file);
|
||||||
|
|
||||||
|
logAction("Network", "Import", "$session_name imported $row_count network(s). $duplicate_count duplicate(s) found and not imported", $client_id);
|
||||||
|
|
||||||
|
flash_alert("$row_count Network(s) imported, $duplicate_count duplicate(s) detected and not imported");
|
||||||
|
|
||||||
|
redirect();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($error) {
|
||||||
|
redirect();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user