Copyright (C) 2020-2024 All Rights Reserved. Contributor(s): Mark J Crane */ //includes files require_once dirname(__DIR__, 2) . "/resources/require.php"; //check permissions require_once "resources/check_auth.php"; if (permission_exists('device_profile_add') || permission_exists('device_profile_edit')) { //access granted } else { echo "access denied"; exit; } //add multi-lingual support $language = new text; $text = $language->get(); //action add or update if (isset($_REQUEST["id"])) { $action = "update"; $device_profile_uuid = $_REQUEST["id"]; $id = $_REQUEST["id"]; } else { $action = "add"; } //process the user data and save it to the database if (count($_POST) > 0 && empty($_POST["persistformvar"])) { //process the http post data by submitted action if (!empty($_POST['action']) && is_uuid($_POST['device_profile_uuid'])) { $array[0]['checked'] = 'true'; $array[0]['uuid'] = $_POST['device_profile_uuid']; switch ($_POST['action']) { case 'delete': if (permission_exists('device_profile_delete')) { $obj = new device; $obj->delete_profiles($array); } break; } header('Location: device_profiles.php'); exit; } //get http post variables and set them to php variables $device_profile_uuid = $_POST["device_profile_uuid"]; $device_profile_name = $_POST["device_profile_name"]; $device_profile_keys = $_POST["device_profile_keys"]; $device_profile_settings = $_POST["device_profile_settings"]; $device_profile_enabled = $_POST["device_profile_enabled"] ?? 'false'; $device_profile_description = $_POST["device_profile_description"]; $device_profile_keys_delete = $_POST["device_profile_keys_delete"] ?? null; $device_profile_settings_delete = $_POST["device_profile_settings_delete"] ?? null; //set the domain_uuid for users that do not have the permission if (permission_exists('device_profile_domain')) { //allowed to updat the domain_uuid $domain_uuid = $_POST["domain_uuid"]; } else { if ($action == 'add') { //use the current domain $domain_uuid = $_SESSION['domain_uuid']; } else { //keep the current domain_uuid $sql = "select domain_uuid from v_device_profiles "; $sql .= "where device_profile_uuid = :device_profile_uuid "; $parameters['device_profile_uuid'] = $device_profile_uuid; $database = new database; $domain_uuid = $database->execute($sql, $parameters, 'column'); } } //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_profile_name)) { $msg .= $text['message-required']." ".$text['label-device_profile_name']."
\n"; } //if (empty($device_profile_keys)) { $msg .= $text['message-required']." ".$text['label-device_profile_keys']."
\n"; } //if (empty($device_profile_settings)) { $msg .= $text['message-required']." ".$text['label-device_profile_settings']."
\n"; } //if (empty($domain_uuid)) { $msg .= $text['message-required']." ".$text['label-domain_uuid']."
\n"; } if (empty($device_profile_enabled)) { $msg .= $text['message-required']." ".$text['label-device_profile_enabled']."
\n"; } //if (empty($device_profile_description)) { $msg .= $text['message-required']." ".$text['label-device_profile_description']."
\n"; } if (!empty($msg) && empty($_POST["persistformvar"])) { require_once "resources/header.php"; require_once "resources/persist_form_var.php"; echo "
\n"; echo "
\n"; echo $msg."
"; echo "
\n"; persistformvar($_POST); echo "
\n"; require_once "resources/footer.php"; return; } //add the device_profile_uuid if (empty($_POST["device_profile_uuid"])) { $device_profile_uuid = uuid(); } //prepare the array $array['device_profiles'][0]["device_profile_uuid"] = $device_profile_uuid; $array['device_profiles'][0]["device_profile_name"] = $device_profile_name; $array['device_profiles'][0]["domain_uuid"] = $domain_uuid; $array['device_profiles'][0]["device_profile_enabled"] = $device_profile_enabled; $array['device_profiles'][0]["device_profile_description"] = $device_profile_description; $y = 0; foreach ($device_profile_keys as $row) { if (strlen($row['profile_key_vendor']) > 0 && !empty($row['profile_key_id'])) { $array['device_profiles'][0]['device_profile_keys'][$y]["domain_uuid"] = $domain_uuid; $array['device_profiles'][0]['device_profile_keys'][$y]["device_profile_key_uuid"] = $row["device_profile_key_uuid"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_category"] = $row["profile_key_category"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_id"] = $row["profile_key_id"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_vendor"] = $row["profile_key_vendor"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_type"] = $row["profile_key_type"]; if (isset($row["profile_key_subtype"])) { $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_subtype"] = $row["profile_key_subtype"]; } $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_line"] = $row["profile_key_line"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_value"] = $row["profile_key_value"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_extension"] = $row["profile_key_extension"] ?? null; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_protected"] = $row["profile_key_protected"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_label"] = $row["profile_key_label"]; $array['device_profiles'][0]['device_profile_keys'][$y]["profile_key_icon"] = $row["profile_key_icon"] ?? null; $y++; } } $y = 0; if (permission_exists("device_profile_setting_edit")) { foreach ($device_profile_settings as $row) { if (strlen($row['profile_setting_name']) > 0 && !empty($row['profile_setting_enabled'])) { $array['device_profiles'][0]['device_profile_settings'][$y]["domain_uuid"] = $domain_uuid; $array['device_profiles'][0]['device_profile_settings'][$y]["device_profile_setting_uuid"] = $row["device_profile_setting_uuid"]; $array['device_profiles'][0]['device_profile_settings'][$y]["profile_setting_name"] = $row["profile_setting_name"]; $array['device_profiles'][0]['device_profile_settings'][$y]["profile_setting_value"] = $row["profile_setting_value"]; $array['device_profiles'][0]['device_profile_settings'][$y]["profile_setting_enabled"] = $row["profile_setting_enabled"]; $array['device_profiles'][0]['device_profile_settings'][$y]["profile_setting_description"] = $row["profile_setting_description"]; $y++; } } } //save to the data $database = new database; $database->app_name = 'Device Profiles'; $database->app_uuid = 'bb2531c3-97e6-428f-9a19-cbac1b96f5b7'; $database->save($array); //remove checked keys if ( $action == 'update' && permission_exists('device_profile_key_delete') && is_array($device_profile_keys_delete) && @sizeof($device_profile_keys_delete) != 0 ) { $obj = new device; $obj->device_profile_uuid = $device_profile_uuid; $obj->delete_profile_keys($device_profile_keys_delete); } //remove checked settings if ( $action == 'update' && permission_exists('device_profile_setting_delete') && is_array($device_profile_settings_delete) && @sizeof($device_profile_settings_delete) != 0 ) { $obj = new device; $obj->device_profile_uuid = $device_profile_uuid; $obj->delete_profile_settings($device_profile_settings_delete); } //redirect the user if (isset($action)) { if ($action == "add") { $_SESSION["message"] = $text['message-add']; } if ($action == "update") { $_SESSION["message"] = $text['message-update']; } header('Location: device_profile_edit.php?id='.$device_profile_uuid); return; } } //pre-populate the form if (!empty($_GET) && is_array($_GET) && (empty($_POST["persistformvar"]) || $_POST["persistformvar"] != "true")) { $device_profile_uuid = $_GET["id"]; $sql = "select * from v_device_profiles "; $sql .= "where device_profile_uuid = :device_profile_uuid "; //$sql .= "and domain_uuid = :domain_uuid "; //$parameters['domain_uuid'] = $_SESSION['domain_uuid']; $parameters['device_profile_uuid'] = $device_profile_uuid; $database = new database; $result = $database->execute($sql, $parameters, 'all'); foreach ($result as $row) { $domain_uuid = $row["domain_uuid"]; $device_profile_name = $row["device_profile_name"]; $device_profile_keys = $row["device_profile_keys"] ?? null; $device_profile_settings = $row["device_profile_settings"] ?? null; $device_profile_enabled = $row["device_profile_enabled"]; $device_profile_description = $row["device_profile_description"]; } unset ($sql, $parameters); } //set the defaults if (empty($device_profile_enabled)) { $device_profile_enabled = 'true'; } //get the child data if (!empty($device_profile_uuid)) { $sql = "select * from v_device_profile_keys "; $sql .= "where device_profile_uuid = :device_profile_uuid "; //$sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) "; $sql .= "order by profile_key_vendor asc, "; $sql .= "case profile_key_category "; $sql .= "when 'line' then 1 "; $sql .= "when 'memory' then 2 "; $sql .= "when 'programmable' then 3 "; $sql .= "when 'expansion' then 4 "; $sql .= "when 'expansion-1' then 5 "; $sql .= "when 'expansion-2' then 6 "; $sql .= "when 'expansion-3' then 7 "; $sql .= "when 'expansion-4' then 8 "; $sql .= "when 'expansion-5' then 9 "; $sql .= "when 'expansion-6' then 10 "; $sql .= "else 100 end, "; $sql .= "profile_key_id asc "; //$parameters['domain_uuid'] = $_SESSION['domain_uuid']; $parameters['device_profile_uuid'] = $device_profile_uuid; $database = new database; $device_profile_keys = $database->execute($sql, $parameters, 'all'); unset ($sql, $parameters); } //get the vendor count $vendor_count = 0; if (!empty($device_profile_keys) && is_array($device_profile_keys) && @sizeof($device_profile_keys) != 0) { $previous_vendor = ''; foreach($device_profile_keys as $row) { if ($previous_vendor != $row['profile_key_vendor']) { $previous_vendor = $row['profile_key_vendor']; $vendor_count++; } } } //determine whether to show the key_subtype $show_key_subtype = false; if (!empty($device_profile_keys) && is_array($device_profile_keys) && @sizeof($device_profile_keys) != 0) { foreach($device_profile_keys as $row) { if ($row['profile_key_vendor'] == 'fanvil') { $show_key_subtype = true; } } } //get the vendors $sql = "select * "; $sql .= "from v_device_vendors as v "; $sql .= "where enabled = 'true' "; $sql .= "order by name asc "; $database = new database; $vendors = $database->select($sql, null, 'all'); unset($sql); //get the vendor functions $sql = "select v.name as vendor_name, f.type, f.subtype, f.value "; $sql .= "from v_device_vendors as v, v_device_vendor_functions as f "; $sql .= "where v.device_vendor_uuid = f.device_vendor_uuid "; $sql .= "and v.enabled = 'true' "; $sql .= "and f.enabled = 'true' "; $sql .= "order by v.name asc, f.type asc "; $database = new database; $vendor_functions = $database->select($sql, null, 'all'); unset($sql); //add the $device_profile_key_uuid if (empty($device_profile_key_uuid)) { $device_profile_key_uuid = uuid(); } //add an empty row if (empty($device_profile_keys) || !is_array($device_profile_keys) || count($device_profile_keys) == 0) { $rows = $_SESSION['devices']['profile_key_add_rows']['numeric']; $id = 0; } if (!empty($device_profile_keys) && is_array($device_profile_keys) && count($device_profile_keys) > 0) { $rows = $_SESSION['devices']['profile_key_edit_rows']['numeric']; $id = count($device_profile_keys)+1; } for ($x = 0; $x < $rows; $x++) { $device_profile_keys[$id]['domain_uuid'] = $domain_uuid; $device_profile_keys[$id]['device_profile_uuid'] = $device_profile_uuid ?? null; $device_profile_keys[$id]['device_profile_key_uuid'] = ''; $device_profile_keys[$id]['profile_key_category'] = ''; $device_profile_keys[$id]['profile_key_id'] = ''; $device_profile_keys[$id]['profile_key_vendor'] = ''; $device_profile_keys[$id]['profile_key_type'] = ''; $device_profile_keys[$id]['profile_key_subtype'] = ''; $device_profile_keys[$id]['profile_key_line'] = ''; $device_profile_keys[$id]['profile_key_value'] = ''; $device_profile_keys[$id]['profile_key_extension'] = ''; $device_profile_keys[$id]['profile_key_protected'] = ''; $device_profile_keys[$id]['profile_key_label'] = ''; $device_profile_keys[$id]['profile_key_icon'] = ''; $id++; } //get the child data if (!empty($device_profile_uuid)) { $sql = "select * from v_device_profile_settings "; $sql .= "where device_profile_uuid = :device_profile_uuid "; //$sql .= "and domain_uuid = '".$domain_uuid."' "; $sql .= "order by profile_setting_name asc"; //$parameters['domain_uuid'] = $_SESSION['domain_uuid']; $parameters['device_profile_uuid'] = $device_profile_uuid; $database = new database; $device_profile_settings = $database->execute($sql, $parameters, 'all'); unset ($sql, $parameters); } //add the $device_profile_setting_uuid if (empty($device_profile_setting_uuid)) { $device_profile_setting_uuid = uuid(); } //add an empty row $x = !empty($device_profile_settings) && is_array($device_profile_settings) ? count($device_profile_settings) : 0; $device_profile_settings[$x]['domain_uuid'] = $domain_uuid; $device_profile_settings[$x]['device_profile_uuid'] = $device_profile_uuid ?? null; $device_profile_settings[$x]['device_profile_setting_uuid'] = ''; $device_profile_settings[$x]['profile_setting_name'] = ''; $device_profile_settings[$x]['profile_setting_value'] = ''; $device_profile_settings[$x]['profile_setting_enabled'] = ''; $device_profile_settings[$x]['profile_setting_description'] = ''; //filter the uuid if (empty($device_profile_uuid) || !is_uuid($device_profile_uuid)) { $device_profile_uuid = null; } //create token $object = new token; $token = $object->create($_SERVER['PHP_SELF']); //show the header $document['title'] = $text['title-device_profile']; require_once "resources/header.php"; //show the content echo "
\n"; echo "
\n"; echo "
".$text['title-device_profile']."
\n"; echo "
\n"; echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'device_profiles.php']); if ($action == 'update') { $button_margin = 'margin-left: 15px;'; if (permission_exists('device_profile_add')) { echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'name'=>'btn_copy','style'=>($button_margin ?? null),'onclick'=>"modal_open('modal-copy','btn_copy');"]); unset($button_margin); } if ( permission_exists('device_profile_delete') || permission_exists('device_profile_key_delete') || permission_exists('device_profile_setting_delete') ) { echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'name'=>'btn_delete','style'=>($button_margin ?? null),'onclick'=>"modal_open('modal-delete','btn_delete');"]); unset($button_margin); } } echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','style'=>'margin-left: 15px;']); echo "
\n"; echo "
\n"; echo "
\n"; if ($action == 'update' && permission_exists('device_profile_add')) { echo modal::create(['id'=>'modal-copy','type'=>'copy','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_copy','style'=>'float: right; margin-left: 15px;','collapse'=>'never','link'=>'device_profile_copy.php?id='.urlencode($device_profile_uuid),'onclick'=>"modal_close();"])]); } if ( $action == 'update' && ( permission_exists('device_profile_delete') || permission_exists('device_profile_key_delete') || permission_exists('device_profile_setting_delete') )) { echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','name'=>'action','value'=>'delete','onclick'=>"modal_close();"])]); } echo $text['description-device_profiles']."\n"; echo "

\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; if (permission_exists('device_profile_setting_edit')) { echo "\n"; echo "\n"; echo "\n"; echo "\n"; } if (permission_exists('device_profile_domain')) { echo "\n"; echo "\n"; echo "\n"; echo "\n"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo " ".$text['label-device_profile_name']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo ($text['description-device_profile_name'] ?? '')."\n"; echo "
\n"; echo " ".$text['label-device_profile_keys']."\n"; echo "\n"; echo " \n"; if ($vendor_count == 0) { echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; if ($show_key_subtype) { echo " \n"; } echo " \n"; echo " \n"; if (permission_exists('device_key_extension')) { echo " \n"; } if (permission_exists('device_key_protected')) { echo " \n"; } echo " \n"; if (permission_exists('device_key_icon')) { echo " \n"; } if (is_array($device_profile_keys) && @sizeof($device_profile_keys) > 1 && permission_exists('device_profile_key_delete')) { echo " \n"; } echo " \n"; } //define the previous profile key vendor $previous_profile_key_vendor = null; $x = 0; foreach($device_profile_keys as $row) { //get the profile key vendor from the key type foreach ($vendor_functions as $function) { if ($row['profile_key_vendor'] == $function['vendor_name'] && $row['profile_key_type'] == $function['value']) { $profile_key_vendor = $function['vendor_name']; } } //prep vendor name for use in ids and classes $device_vendor = str_replace(' ', '_', $row['profile_key_vendor']); //set the column names if ($previous_profile_key_vendor != $row['profile_key_vendor']) { echo " \n"; echo " \n"; } echo " \n"; if (permission_exists('device_key_extension')) { echo " \n"; } if (permission_exists('device_key_protected')) { echo " \n"; } echo " \n"; if (permission_exists('device_key_icon')) { echo " \n"; } if (!empty($device_profile_keys) && is_array($device_profile_keys) && @sizeof($device_profile_keys) > 1 && is_uuid($row["device_profile_key_uuid"]) && permission_exists('device_profile_key_delete')) { echo " \n"; } echo " \n"; } //show all the rows in the array echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; if ($show_key_subtype) { echo " \n"; } echo " \n"; echo " \n"; if (permission_exists('device_key_extension')) { echo " \n"; } if (permission_exists('device_key_protected')) { echo " \n"; } echo " \n"; if (permission_exists('device_key_icon')) { echo " \n"; } if (is_array($device_profile_keys) && @sizeof($device_profile_keys) > 1 && permission_exists('device_profile_key_delete')) { if (is_uuid($row["device_profile_key_uuid"])) { echo " \n"; } echo " \n"; //set the previous vendor $previous_profile_key_vendor = $row['profile_key_vendor']; //increment the array key $x++; } echo "
".$text['label-device_key_category']."".$text['label-device_key_id']."".$text['label-device_key_vendor']."".$text['label-device_key_type']."".$text['label-device_key_subtype']."".$text['label-device_key_line']."".$text['label-device_key_value']."".$text['label-device_key_extension']."".$text['label-device_key_protected']."".$text['label-device_key_label']."".$text['label-device_key_icon']."\n"; echo " ".$text['label-delete']."\n"; echo " \n"; echo "
".$text['label-device_key_category']."\n"; echo " ".$text['label-device_key_id']."\n"; echo " ".$text['label-device_vendor']."\n"; echo " ".$text['label-device_key_type']."\n"; if ($show_key_subtype) { echo " ".$text['label-device_key_subtype']."".$text['label-device_key_line']."\n"; echo " ".$text['label-device_key_value']."".$text['label-device_key_extension']."".$text['label-device_key_protected']."".$text['label-device_key_label']."".$text['label-device_key_icon']."\n"; echo " ".$text['label-delete']."\n"; echo " \n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; //echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; } else { echo " \n"; } echo "
\n"; echo "
\n"; echo ($text['description-profile_key_icon'] ?? '')."\n"; echo "
\n"; echo " ".$text['label-device_profile_settings']."\n"; echo "\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; if (is_array($device_profile_settings) && @sizeof($device_profile_settings) > 1 && permission_exists('device_profile_setting_delete')) { echo " \n"; } echo " \n"; $x = 0; foreach($device_profile_settings as $row) { echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; if (is_array($device_profile_settings) && @sizeof($device_profile_settings) > 1 && permission_exists('device_profile_setting_delete')) { if (is_uuid($row["device_profile_setting_uuid"])) { echo " \n"; } echo " \n"; $x++; } echo "
".$text['label-device_setting_name']."".$text['label-device_setting_value']."".$text['label-enabled']."".$text['label-device_setting_description']."\n"; echo " ".$text['label-delete']."\n"; echo " \n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; } else { echo " \n"; } echo "
\n"; echo "
\n"; echo ($text['description-profile_setting_description'] ?? '')."\n"; echo "
\n"; echo " ".$text['label-domain_uuid']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo ($text['description-domain_uuid'] ?? '')."\n"; echo "
\n"; echo " ".$text['label-device_profile_enabled']."\n"; echo "\n"; if (substr($_SESSION['theme']['input_toggle_style']['text'], 0, 6) == 'switch') { echo " \n"; } else { echo " \n"; } echo "
\n"; echo ($text['description-device_profile_enabled'] ?? '')."\n"; echo "
\n"; echo " ".$text['label-device_profile_description']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo ($text['description-device_profile_description'] ?? '')."\n"; echo "
"; echo "
\n"; echo "

"; echo "\n"; echo "\n"; echo "
"; //include the footer require_once "resources/footer.php"; ?>