Portions created by the Initial Developer are Copyright (C) 2008-2024
the Initial Developer. All Rights Reserved.
*/
//includes files
require_once dirname(__DIR__, 2) . "/resources/require.php";
//check permissions
require_once "resources/check_auth.php";
if (permission_exists('device_add') || permission_exists('device_edit')) {
//access granted
}
else {
echo "access denied";
exit;
}
//add multi-lingual support
$language = new text;
$text = $language->get();
//set the defaults
$device_model = '';
$device_firmware_version = '';
$device_template ='';
//get the domain uuid
$domain_uuid = $_SESSION['domain_uuid'] ?? '';
//initialize the database object
$database = database::new();
//initialize the settigns object
$settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid]);
//action add or update
if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
$action = "update";
$device_uuid = $_REQUEST["id"];
}
else {
$action = "add";
}
//get the total device count from the database, check the limit, if defined
if ($action == 'add') {
if (!empty($_SESSION['limit']['devices']['numeric']) && $_SESSION['limit']['devices']['numeric']) {
$sql = "select count(*) from v_devices where domain_uuid = :domain_uuid ";
$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
$total_devices = $database->select($sql, $parameters, 'column');
if ($total_devices >= $_SESSION['limit']['devices']['numeric']) {
message::add($text['message-maximum_devices'].' '.$_SESSION['limit']['devices']['numeric'], 'negative');
header('Location: devices.php');
exit;
}
unset($sql, $parameters, $total_devices);
}
}
//get http post variables and set them to php variables
if (count($_POST) > 0) {
//process the http post data by submitted action
if (!empty($_POST['action']) && is_uuid($_POST['device_uuid'])) {
$array[0]['checked'] = 'true';
$array[0]['uuid'] = $_POST['device_uuid'];
switch ($_POST['action']) {
case 'delete':
if (permission_exists('device_delete')) {
$obj = new device;
$obj->delete($array);
}
break;
}
header('Location: devices.php');
exit;
}
//device device address
if (permission_exists('device_address')) {
$device_address = $_POST["device_address"];
}
else {
$sql = "select device_address from v_devices ";
$sql .= "where device_uuid = :device_uuid ";
$parameters['device_uuid'] = $device_uuid;
$row = $database->select($sql, $parameters, 'row');
if (is_array($row) && @sizeof($row) != 0) {
$device_address = $row["device_address"];
}
unset($sql, $parameters, $row);
}
//devices
$domain_uuid = $_POST["domain_uuid"];
$device_uuid = $_POST["device_uuid"] ?? null;
//$device_provisioned_ip = $_POST["device_provisioned_ip"];
$domain_uuid = $_POST["domain_uuid"];
$device_label = $_POST["device_label"];
$device_label = $_POST["device_label"];
$device_user_uuid = $_POST["device_user_uuid"];
$device_username = $_POST["device_username"];
$device_password = $_POST["device_password"];
$device_vendor = $_POST["device_vendor"];
$device_location = $_POST["device_location"];
$device_uuid_alternate = $_POST["device_uuid_alternate"] ?? null;
$device_model = $_POST["device_model"] ?? null;
$device_firmware_version = $_POST["device_firmware_version"] ?? null;
$device_enabled = $_POST["device_enabled"] ?? 'false';
$device_template = $_POST["device_template"];
$device_description = $_POST["device_description"];
//lines
$device_lines = $_POST["device_lines"];
$device_lines_delete = $_POST["device_lines_delete"] ?? null;
//$line_number = $_POST["line_number"];
//$server_address = $_POST["server_address"];
//$outbound_proxy_primary = $_POST["outbound_proxy_primary"];
//$outbound_proxy_secondary = $_POST["outbound_proxy_secondary"];
//$label = $_POST["label"];
//$display_name = $_POST["display_name"];
//$user_id = $_POST["user_id"];
//$auth_id = $_POST["auth_id"];
//$password = $_POST["password"];
//profile
$device_profile_uuid = $_POST["device_profile_uuid"] ?? null;
//keys
$device_keys = $_POST["device_keys"];
$device_keys_delete = $_POST["device_keys_delete"] ?? null;
//$device_key_category = $_POST["device_key_category"];
//$device_key_id = $_POST["device_key_id"];
//$device_key_type = $_POST["device_key_type"];
//$device_key_subtype = $_POST["device_key_subtype"];
//$device_key_line = $_POST["device_key_line"];
//$device_key_value = $_POST["device_key_value"];
//$device_key_extension = $_POST["device_key_extension"];
//$device_key_label = $_POST["device_key_label"];
//$device_key_icon = $_POST["device_key_icon"];
//settings
$device_settings = $_POST["device_settings"];
$device_settings_delete = $_POST["device_settings_delete"] ?? null;
//$device_setting_category = $_POST["device_setting_category"]);
//$device_setting_subcategory = $_POST["device_setting_subcategory"];
//$device_setting_name = $_POST["device_setting_name"];
//$device_setting_value = $_POST["device_setting_value"];
//$device_setting_enabled = $_POST["device_setting_enabled"];
//$device_setting_description = $_POST["device_setting_description"];
//normalize the address
if (!empty($device_address)) {
$device_address = strtolower($device_address);
$device_address = preg_replace('#[^a-fA-F0-9./]#', '', $device_address);
}
}
//use the device address to get the vendor
if (empty($device_vendor)) {
$device_vendor = device::get_vendor($device_address ?? null);
}
//add or update the database
if (!empty($_POST) && empty($_POST["persistformvar"])) {
//validate the token
$token = new token;
if (!$token->validate($_SERVER['PHP_SELF'])) {
message::add($text['message-invalid_token'],'negative');
header('Location: devices.php');
exit;
}
//check for all required data
$msg = '';
if (empty($device_address)) { $msg .= $text['message-required'].$text['label-device_address']."
\n"; }
//if (empty($device_label)) { $msg .= "Please provide: Label
\n"; }
//if (empty($device_vendor)) { $msg .= "Please provide: Vendor
\n"; }
//if (empty($device_model)) { $msg .= "Please provide: Model
\n"; }
//if (empty($device_firmware_version)) { $msg .= "Please provide: Firmware Version
\n"; }
//if (empty($device_enabled)) { $msg .= "Please provide: Enabled
\n"; }
//if (empty($device_template)) { $msg .= "Please provide: Template
\n"; }
//if (empty($device_username)) { $msg .= "Please provide: Username
\n"; }
//if (empty($device_password)) { $msg .= "Please provide: Password
\n"; }
//if (empty($device_description)) { $msg .= "Please provide: Description
\n"; }
if (!empty($msg)) {
require_once "resources/header.php";
require_once "resources/persist_form_var.php";
echo "
| \n";
echo $msg." "; echo " |