Packet-Fence/admin/configuration/new_switch_function.php
2025-06-28 06:23:17 +05:30

80 lines
2.4 KiB
PHP

<?php
session_start();
$activePage = 'User';
$activeSubPage = 'Createuser';
include("../elements/functions.php");
// Retrieve values from the form
$ip = $_POST['ip_address'];
$desc = $_POST['description'];
$switch_type = $_POST['access']; // Access dropdown
$group = 'default'; // Assuming default group for now
$selected_option = ''; // Initialize variable for selected option
// Determine selected option based on switch type
if ($switch_type == 'Alcatel') {
$selected_option = $_POST['alcatel_option']; // Alcatel dropdown option
} elseif ($switch_type == 'Cisco') {
$selected_option = $_POST['cisco_option']; // Cisco dropdown option
} elseif ($switch_type == 'Dlink') {
$selected_option = $_POST['dlink_option']; // Dlink dropdown option
}
// Check if session token exists
if (isset($_SESSION['token'])) {
$token = $_SESSION['token'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Load API endpoints
$jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
$jsonData = file_get_contents($jsonFilePath);
$endpoints = json_decode($jsonData, true);
$url = $endpoints['switches'];
// Prepare data for API call
$data = [
"description" => $desc,
"group" => $group,
"id" => $ip,
"type" => $selected_option
];
// Make API request
$response = make_post_request($url, $data);
if ($response === FALSE) {
$response = json_encode(['status' => 'error', 'message' => 'An error occurred']);
}
// Decode response data
$response_data = json_decode($response, true);
// Handle different response statuses
if ($response_data['status'] === 201) {
$_SESSION['message'] = "Switch Created Successfully";
$redirect_page = "./switch.php";
} elseif ($response_data['status'] === 409) {
$_SESSION['message'] = "Switch not created. IP Address already exists.";
$redirect_page = "./new_switch.php";
} else {
$_SESSION['message'] = $response_data['message'] ?? 'An unexpected error occurred.';
$redirect_page = "./switch.php";
}
// Redirect to the appropriate page
header('Location: ' . $redirect_page);
exit();
} else {
header('Location: ../index.php');
exit();
}
} else {
header('Location: ../index.php');
exit();
}
?>