mirror of
https://github.com/itflow-org/itflow
synced 2026-09-16 03:35:12 +00:00
API - Add network create, update and archive endpoints
This commit is contained in:
43
api/v1/networks/archive.php
Normal file
43
api/v1/networks/archive.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
// Parse ID
|
||||
$network_id = intval($_POST['network_id']);
|
||||
|
||||
// Default
|
||||
$update_count = false;
|
||||
|
||||
if (!empty($network_id)) {
|
||||
|
||||
// Fetch network info
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "
|
||||
SELECT network_name
|
||||
FROM networks
|
||||
WHERE network_id = $network_id AND network_client_id = $client_id AND network_archived_at IS NULL
|
||||
LIMIT 1
|
||||
"));
|
||||
|
||||
if ($row) {
|
||||
|
||||
$network_name = escapeSql($row['network_name']);
|
||||
|
||||
// Archive network
|
||||
$update_sql = mysqli_query($mysqli, "
|
||||
UPDATE networks SET network_archived_at = NOW()
|
||||
WHERE network_id = $network_id AND network_client_id = $client_id AND network_archived_at IS NULL
|
||||
");
|
||||
|
||||
if ($update_sql) {
|
||||
$update_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Network", "Archive", "$network_name archived via API ($api_key_name)", $client_id, $network_id);
|
||||
logAudit("API", "Success", "Archived network $network_name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
30
api/v1/networks/create.php
Normal file
30
api/v1/networks/create.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
// Parse Info
|
||||
require_once 'network_model.php';
|
||||
|
||||
// Default
|
||||
$insert_id = false;
|
||||
|
||||
if (!empty($name) && !empty($client_id) && !empty($network)) {
|
||||
|
||||
// Insert network
|
||||
$insert_sql = mysqli_query($mysqli, "INSERT INTO networks SET network_name = '$name', network_description = '$description', network_vlan = $vlan, network = '$network', network_gateway = '$gateway', network_primary_dns = '$primary_dns', network_secondary_dns = '$secondary_dns', network_dhcp_range = '$dhcp_range', network_notes = '$notes', network_location_id = $location_id, network_client_id = $client_id");
|
||||
|
||||
// Check insert & get insert ID
|
||||
if ($insert_sql) {
|
||||
$insert_id = mysqli_insert_id($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Network", "Create", "$name via API ($api_key_name)", $client_id, $insert_id);
|
||||
logAudit("API", "Success", "Created network $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../create_output.php';
|
||||
83
api/v1/networks/network_model.php
Normal file
83
api/v1/networks/network_model.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
// Variable assignment from POST (or: blank/from DB is updating)
|
||||
|
||||
if (isset($_POST['network_name'])) {
|
||||
$name = escapeSql($_POST['network_name']);
|
||||
} elseif ($network_row) {
|
||||
$name = mysqli_real_escape_string($mysqli, $network_row['network_name']);
|
||||
} else {
|
||||
$name = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network_description'])) {
|
||||
$description = escapeSql($_POST['network_description']);
|
||||
} elseif ($network_row) {
|
||||
$description = mysqli_real_escape_string($mysqli, $network_row['network_description']);
|
||||
} else {
|
||||
$description = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network'])) {
|
||||
$network = escapeSql($_POST['network']);
|
||||
} elseif ($network_row) {
|
||||
$network = mysqli_real_escape_string($mysqli, $network_row['network']);
|
||||
} else {
|
||||
$network = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network_vlan'])) {
|
||||
$vlan = intval($_POST['network_vlan']);
|
||||
} elseif ($network_row) {
|
||||
$vlan = intval($network_row['network_vlan']);
|
||||
} else {
|
||||
$vlan = 0;
|
||||
}
|
||||
|
||||
if (isset($_POST['network_gateway'])) {
|
||||
$gateway = escapeSql($_POST['network_gateway']);
|
||||
} elseif ($network_row) {
|
||||
$gateway = mysqli_real_escape_string($mysqli, $network_row['network_gateway']);
|
||||
} else {
|
||||
$gateway = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network_primary_dns'])) {
|
||||
$primary_dns = escapeSql($_POST['network_primary_dns']);
|
||||
} elseif ($network_row) {
|
||||
$primary_dns = mysqli_real_escape_string($mysqli, $network_row['network_primary_dns']);
|
||||
} else {
|
||||
$primary_dns = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network_secondary_dns'])) {
|
||||
$secondary_dns = escapeSql($_POST['network_secondary_dns']);
|
||||
} elseif ($network_row) {
|
||||
$secondary_dns = mysqli_real_escape_string($mysqli, $network_row['network_secondary_dns']);
|
||||
} else {
|
||||
$secondary_dns = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network_dhcp_range'])) {
|
||||
$dhcp_range = escapeSql($_POST['network_dhcp_range']);
|
||||
} elseif ($network_row) {
|
||||
$dhcp_range = mysqli_real_escape_string($mysqli, $network_row['network_dhcp_range']);
|
||||
} else {
|
||||
$dhcp_range = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network_notes'])) {
|
||||
$notes = escapeSql($_POST['network_notes']);
|
||||
} elseif ($network_row) {
|
||||
$notes = mysqli_real_escape_string($mysqli, $network_row['network_notes']);
|
||||
} else {
|
||||
$notes = '';
|
||||
}
|
||||
|
||||
if (isset($_POST['network_location_id'])) {
|
||||
$location_id = intval($_POST['network_location_id']);
|
||||
} elseif ($network_row) {
|
||||
$location_id = intval($network_row['network_location_id']);
|
||||
} else {
|
||||
$location_id = 0;
|
||||
}
|
||||
@@ -8,16 +8,16 @@ require_once '../require_get_method.php';
|
||||
// Specific network via ID (single)
|
||||
if (isset($_GET['network_id'])) {
|
||||
$id = intval($_GET['network_id']);
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM networks WHERE network_id = '$id' AND 1=1 " . apiClientScopeSql('network_client_id') . "");
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM networks WHERE network_id = '$id' AND network_archived_at IS NULL AND 1=1 " . apiClientScopeSql('network_client_id') . "");
|
||||
|
||||
} elseif (isset($_GET['network_name'])) {
|
||||
// Network by name
|
||||
$name = mysqli_real_escape_string($mysqli, $_GET['network_name']);
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM networks WHERE network_name = '$name' AND 1=1 " . apiClientScopeSql('network_client_id') . " ORDER BY network_id LIMIT $limit OFFSET $offset");
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM networks WHERE network_name = '$name' AND network_archived_at IS NULL AND 1=1 " . apiClientScopeSql('network_client_id') . " ORDER BY network_id LIMIT $limit OFFSET $offset");
|
||||
|
||||
} else {
|
||||
// All networks (by client ID or all in general if key permits)
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM networks WHERE 1=1 " . apiClientScopeSql('network_client_id') . " ORDER BY network_id LIMIT $limit OFFSET $offset");
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM networks WHERE network_archived_at IS NULL AND 1=1 " . apiClientScopeSql('network_client_id') . " ORDER BY network_id LIMIT $limit OFFSET $offset");
|
||||
}
|
||||
|
||||
// Output
|
||||
|
||||
43
api/v1/networks/unarchive.php
Normal file
43
api/v1/networks/unarchive.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
// Parse ID
|
||||
$network_id = intval($_POST['network_id']);
|
||||
|
||||
// Default
|
||||
$update_count = false;
|
||||
|
||||
if (!empty($network_id)) {
|
||||
|
||||
// Fetch network info
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "
|
||||
SELECT network_name
|
||||
FROM networks
|
||||
WHERE network_id = $network_id AND network_client_id = $client_id AND network_archived_at IS NOT NULL
|
||||
LIMIT 1
|
||||
"));
|
||||
|
||||
if ($row) {
|
||||
|
||||
$network_name = escapeSql($row['network_name']);
|
||||
|
||||
// Unarchive network
|
||||
$update_sql = mysqli_query($mysqli, "
|
||||
UPDATE networks SET network_archived_at = NULL
|
||||
WHERE network_id = $network_id AND network_client_id = $client_id
|
||||
");
|
||||
|
||||
if ($update_sql) {
|
||||
$update_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Network", "Unarchive", "$network_name unarchived via API ($api_key_name)", $client_id, $network_id);
|
||||
logAudit("API", "Success", "Unarchived network $network_name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
36
api/v1/networks/update.php
Normal file
36
api/v1/networks/update.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
require_once '../validate_api_key.php';
|
||||
|
||||
require_once '../require_post_method.php';
|
||||
|
||||
// Parse ID
|
||||
$network_id = intval($_POST['network_id']);
|
||||
|
||||
// Default
|
||||
$update_count = false;
|
||||
|
||||
if (!empty($network_id)) {
|
||||
|
||||
$network_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM networks WHERE network_id = '$network_id' AND network_client_id = $client_id AND network_archived_at IS NULL LIMIT 1"));
|
||||
|
||||
// Variable assignment from POST - assigning the current database value if a value is not provided
|
||||
require_once 'network_model.php';
|
||||
|
||||
if ($network_row) {
|
||||
|
||||
$update_sql = mysqli_query($mysqli, "UPDATE networks SET network_name = '$name', network_description = '$description', network_vlan = $vlan, network = '$network', network_gateway = '$gateway', network_primary_dns = '$primary_dns', network_secondary_dns = '$secondary_dns', network_dhcp_range = '$dhcp_range', network_notes = '$notes', network_location_id = $location_id WHERE network_id = $network_id AND network_client_id = $client_id AND network_archived_at IS NULL LIMIT 1");
|
||||
|
||||
// Check update & get affected rows
|
||||
if ($update_sql) {
|
||||
$update_count = mysqli_affected_rows($mysqli);
|
||||
|
||||
// Logging
|
||||
logAudit("Network", "Edit", "$name via API ($api_key_name)", $client_id, $network_id);
|
||||
logAudit("API", "Success", "Edited network $name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
require_once '../update_output.php';
|
||||
Reference in New Issue
Block a user